From 81d93dc6a5853e24de02bac17cfe70657887e69a Mon Sep 17 00:00:00 2001 From: adam Date: Mon, 11 Apr 2005 07:04:51 +0000 Subject: [PATCH] added Jelly darcs-hash:20050411070451-5007d-a9b1a918f229f79e7971bc45cb596ce765132313.gz --- src/org/ibex/jetty/Jelly.java | 248 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 248 insertions(+) create mode 100644 src/org/ibex/jetty/Jelly.java diff --git a/src/org/ibex/jetty/Jelly.java b/src/org/ibex/jetty/Jelly.java new file mode 100644 index 0000000..aebbcf4 --- /dev/null +++ b/src/org/ibex/jetty/Jelly.java @@ -0,0 +1,248 @@ +/* + * Copyright 2002,2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.ibex.jetty; + +import org.apache.commons.jelly.JellyContext; +import javax.servlet.ServletContext; +import java.io.*; +import java.net.*; + +import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.io.UnsupportedEncodingException; +import java.net.MalformedURLException; +import java.net.URL; + +import javax.servlet.ServletException; +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.commons.jelly.JellyContext; +import org.apache.commons.jelly.JellyException; +import org.apache.commons.jelly.XMLOutput; + +/** + * Servlet for handling display of Jelly-fied XML files. Modelled after VelocityServlet. + * + * @author Kelvin Tan + * @version $Revision: 1.7 $ + */ +public class Jelly extends HttpServlet { + /** + * The HTTP request object context key. + */ + public static final String REQUEST = "request"; + + /** + * The HTTP response object context key. + */ + public static final String RESPONSE = "response"; + + protected void doGet( + HttpServletRequest request, + HttpServletResponse response) + throws ServletException, IOException { + + doRequest(request, response); + } + + protected void doPost( + HttpServletRequest request, + HttpServletResponse response) + throws ServletException, IOException { + + doRequest(request, response); + } + + /** + * Handles all requests + * @param req HttpServletRequest object containing client request + * @param res HttpServletResponse object for the response + * @throws ServletException + * @throws IOException + */ + protected void doRequest(HttpServletRequest req, HttpServletResponse res) + throws ServletException, IOException { + + JellyContext context = createContext(req, res); + try { + URL script = getScript(req); + runScript(script, context, req, res); + } + catch (Exception e) { + error(req, res, e); + } + } + + /** + * @see org.apache.velocity.servlet.VelocityServlet#createContext + * @param req + * @param res + * @return + */ + protected JellyContext createContext( + HttpServletRequest req, + HttpServletResponse res) { + + JellyContext ctx = new JellyServletContext(getServletContext()); + ctx.setVariable(REQUEST, req); + ctx.setVariable(RESPONSE, res); + return ctx; + } + + /** + *

+ * Either use the query parameter "script", or the URI itself + * to denote the script to run. + *

+ *

+ * Example: script=index.jelly or http://localhost:8080/foo/index.jelly. + *

+ * + * @see org.apache.velocity.servlet.VelocityServlet#getTemplate + * @param req + * @return + * @throws MalformedURLException + */ + protected URL getScript(HttpServletRequest req) throws MalformedURLException { + return new URL("file:" + new File(getServletContext().getRealPath(req.getServletPath())).getAbsolutePath()); + } + + /** + * @see org.apache.velocity.servlet.VelocityServlet#mergeTemplate + * @param script + * @param context + * @param req + * @param res + * @throws IOException + * @throws UnsupportedEncodingException + * @throws JellyException + */ + protected void runScript( + URL script, + JellyContext context, + HttpServletRequest req, + HttpServletResponse res) + throws IOException, UnsupportedEncodingException, JellyException { + + ServletOutputStream output = res.getOutputStream(); + XMLOutput xmlOutput = XMLOutput.createXMLOutput(output); + context.runScript(script, xmlOutput); + xmlOutput.flush(); + xmlOutput.close(); + output.flush(); + } + + /** + * Invoked when there is an error thrown in any part of doRequest() processing. + *

+ * Default will send a simple HTML response indicating there was a problem. + *

+ * Ripped from VelocityServlet. + * + * @param request original HttpServletRequest from servlet container. + * @param response HttpServletResponse object from servlet container. + * @param cause Exception that was thrown by some other part of process. + */ + protected void error( + HttpServletRequest request, + HttpServletResponse response, + Exception cause) + throws ServletException, IOException { + + StringBuffer html = new StringBuffer(); + html.append(""); + html.append("Error"); + html.append(""); + html.append("

JellyServlet : Error processing the script

"); + html.append("
");
+        String why = cause.getMessage();
+        if (why != null && why.trim().length() > 0) {
+            html.append(why);
+            html.append("
"); + } + + StringWriter sw = new StringWriter(); + cause.printStackTrace(new PrintWriter(sw)); + + html.append(sw.toString()); + html.append("
"); + html.append(""); + html.append(""); + response.getOutputStream().print(html.toString()); + } + + +/** + * + * @author Kelvin Tan + * @version 1.1 + */ +public static class JellyServletContext extends JellyContext { + + private ServletContext ctx; + + public JellyServletContext() { + init(); + } + + public JellyServletContext(ServletContext ctx) { + super(); + this.ctx = ctx; + init(); + } + + public JellyServletContext(JellyContext parent, ServletContext ctx) { + super(parent); + this.ctx = ctx; + init(); + } + + private void init() { + registerTagLibrary("jelly:core", "org.apache.commons.jelly.tags.core.CoreTagLibrary"); + } + + /** + * Allow access of relative URIs when performing <j:include>. + * @param s + * @return + * @throws MalformedURLException + */ + public URL getResource(String s) throws MalformedURLException { + System.err.println("getResource(s)"); + return ctx.getResource(s); + } + + /** + * Allow access of relative URIs when performing <j:include>. + * @param s + * @return + */ + public InputStream getResourceAsStream(String s) { + return ctx.getResourceAsStream(s); + } + + protected JellyContext createChildContext() + { + return new JellyServletContext(this, ctx); + } +} + +} -- 1.7.10.4