added Jelly
authoradam <adam@megacz.com>
Mon, 11 Apr 2005 07:04:51 +0000 (07:04 +0000)
committeradam <adam@megacz.com>
Mon, 11 Apr 2005 07:04:51 +0000 (07:04 +0000)
darcs-hash:20050411070451-5007d-a9b1a918f229f79e7971bc45cb596ce765132313.gz

src/org/ibex/jetty/Jelly.java [new file with mode: 0644]

diff --git a/src/org/ibex/jetty/Jelly.java b/src/org/ibex/jetty/Jelly.java
new file mode 100644 (file)
index 0000000..aebbcf4
--- /dev/null
@@ -0,0 +1,248 @@
+/*\r
+ * Copyright 2002,2004 The Apache Software Foundation.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *      http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+package org.ibex.jetty;\r
+\r
+import org.apache.commons.jelly.JellyContext;\r
+import javax.servlet.ServletContext;\r
+import java.io.*;\r
+import java.net.*;\r
+\r
+import java.io.IOException;\r
+import java.io.PrintWriter;\r
+import java.io.StringWriter;\r
+import java.io.UnsupportedEncodingException;\r
+import java.net.MalformedURLException;\r
+import java.net.URL;\r
+\r
+import javax.servlet.ServletException;\r
+import javax.servlet.ServletOutputStream;\r
+import javax.servlet.http.HttpServlet;\r
+import javax.servlet.http.HttpServletRequest;\r
+import javax.servlet.http.HttpServletResponse;\r
+\r
+import org.apache.commons.jelly.JellyContext;\r
+import org.apache.commons.jelly.JellyException;\r
+import org.apache.commons.jelly.XMLOutput;\r
+\r
+/**\r
+ * Servlet for handling display of Jelly-fied XML files. Modelled after VelocityServlet.\r
+ *\r
+ * @author Kelvin Tan\r
+ * @version $Revision: 1.7 $\r
+ */\r
+public class Jelly extends HttpServlet {\r
+    /**\r
+     * The HTTP request object context key.\r
+     */\r
+    public static final String REQUEST = "request";\r
+\r
+    /**\r
+     * The HTTP response object context key.\r
+     */\r
+    public static final String RESPONSE = "response";\r
+\r
+    protected void doGet(\r
+        HttpServletRequest request,\r
+        HttpServletResponse response)\r
+        throws ServletException, IOException {\r
+\r
+        doRequest(request, response);\r
+    }\r
+\r
+    protected void doPost(\r
+        HttpServletRequest request,\r
+        HttpServletResponse response)\r
+        throws ServletException, IOException {\r
+\r
+        doRequest(request, response);\r
+    }\r
+\r
+    /**\r
+     * Handles all requests\r
+     * @param req HttpServletRequest object containing client request\r
+     * @param res HttpServletResponse object for the response\r
+     * @throws ServletException\r
+     * @throws IOException\r
+     */\r
+    protected void doRequest(HttpServletRequest req, HttpServletResponse res)\r
+        throws ServletException, IOException {\r
+\r
+        JellyContext context = createContext(req, res);\r
+        try {\r
+            URL script = getScript(req);\r
+            runScript(script, context, req, res);\r
+        }\r
+        catch (Exception e) {\r
+            error(req, res, e);\r
+        }\r
+    }\r
+\r
+    /**\r
+     * @see org.apache.velocity.servlet.VelocityServlet#createContext\r
+     * @param req\r
+     * @param res\r
+     * @return\r
+     */\r
+    protected JellyContext createContext(\r
+        HttpServletRequest req,\r
+        HttpServletResponse res) {\r
+\r
+        JellyContext ctx = new JellyServletContext(getServletContext());\r
+        ctx.setVariable(REQUEST, req);\r
+        ctx.setVariable(RESPONSE, res);\r
+        return ctx;\r
+    }\r
+\r
+    /**\r
+     * <p>\r
+     * Either use the query parameter "script", or the URI itself\r
+     * to denote the script to run.\r
+     * </p>\r
+     * <p>\r
+     * Example: script=index.jelly or http://localhost:8080/foo/index.jelly.\r
+     * </p>\r
+     *\r
+     * @see org.apache.velocity.servlet.VelocityServlet#getTemplate\r
+     * @param req\r
+     * @return\r
+     * @throws MalformedURLException\r
+     */\r
+    protected URL getScript(HttpServletRequest req) throws MalformedURLException {\r
+        return new URL("file:" + new File(getServletContext().getRealPath(req.getServletPath())).getAbsolutePath());\r
+    }\r
+\r
+    /**\r
+     * @see org.apache.velocity.servlet.VelocityServlet#mergeTemplate\r
+     * @param script\r
+     * @param context\r
+     * @param req\r
+     * @param res\r
+     * @throws IOException\r
+     * @throws UnsupportedEncodingException\r
+     * @throws JellyException\r
+     */\r
+    protected void runScript(\r
+        URL script,\r
+        JellyContext context,\r
+        HttpServletRequest req,\r
+        HttpServletResponse res)\r
+        throws IOException, UnsupportedEncodingException, JellyException {\r
+\r
+        ServletOutputStream output = res.getOutputStream();\r
+        XMLOutput xmlOutput = XMLOutput.createXMLOutput(output);\r
+        context.runScript(script, xmlOutput);\r
+        xmlOutput.flush();\r
+        xmlOutput.close();\r
+        output.flush();\r
+    }\r
+\r
+    /**\r
+     * Invoked when there is an error thrown in any part of doRequest() processing.\r
+     * <br><br>\r
+     * Default will send a simple HTML response indicating there was a problem.\r
+     *<br><br>\r
+     * Ripped from VelocityServlet.\r
+     *\r
+     * @param request original HttpServletRequest from servlet container.\r
+     * @param response HttpServletResponse object from servlet container.\r
+     * @param cause  Exception that was thrown by some other part of process.\r
+     */\r
+    protected void error(\r
+        HttpServletRequest request,\r
+        HttpServletResponse response,\r
+        Exception cause)\r
+        throws ServletException, IOException {\r
+\r
+        StringBuffer html = new StringBuffer();\r
+        html.append("<html>");\r
+        html.append("<title>Error</title>");\r
+        html.append("<body bgcolor=\"#ffffff\">");\r
+        html.append("<h2>JellyServlet : Error processing the script</h2>");\r
+        html.append("<pre>");\r
+        String why = cause.getMessage();\r
+        if (why != null && why.trim().length() > 0) {\r
+            html.append(why);\r
+            html.append("<br>");\r
+        }\r
+\r
+        StringWriter sw = new StringWriter();\r
+        cause.printStackTrace(new PrintWriter(sw));\r
+\r
+        html.append(sw.toString());\r
+        html.append("</pre>");\r
+        html.append("</body>");\r
+        html.append("</html>");\r
+        response.getOutputStream().print(html.toString());\r
+    }\r
+\r
+\r
+/**\r
+ *\r
+ * @author <a href="mailto:kelvint@apache.org">Kelvin Tan</a>\r
+ * @version 1.1\r
+ */\r
+public static class JellyServletContext extends JellyContext {\r
+\r
+    private ServletContext ctx;\r
+\r
+    public JellyServletContext() {\r
+        init();\r
+    }\r
+\r
+    public JellyServletContext(ServletContext ctx) {\r
+        super();\r
+        this.ctx = ctx;\r
+        init();\r
+    }\r
+\r
+    public JellyServletContext(JellyContext parent, ServletContext ctx) {\r
+        super(parent);\r
+        this.ctx = ctx;\r
+        init();\r
+    }\r
+\r
+    private void init() {\r
+        registerTagLibrary("jelly:core", "org.apache.commons.jelly.tags.core.CoreTagLibrary");\r
+    }\r
+\r
+    /**\r
+     * Allow access of relative URIs when performing &lt;j:include&gt;.\r
+     * @param s\r
+     * @return\r
+     * @throws MalformedURLException\r
+     */\r
+    public URL getResource(String s) throws MalformedURLException {\r
+        System.err.println("getResource(s)");\r
+        return ctx.getResource(s);\r
+    }\r
+\r
+    /**\r
+     * Allow access of relative URIs when performing &lt;j:include&gt;.\r
+     * @param s\r
+     * @return\r
+     */\r
+    public InputStream getResourceAsStream(String s) {\r
+        return ctx.getResourceAsStream(s);\r
+    }\r
+\r
+    protected JellyContext createChildContext()\r
+    {\r
+        return new JellyServletContext(this, ctx);\r
+    }\r
+}\r
+\r
+}\r