jstrachan 2002/12/17 00:32:56
Modified: jelly/src/java/org/apache/commons/jelly/servlet
JellyServlet.java
Added: jelly/src/java/org/apache/commons/jelly/servlet
JellyServletContext.java
Log:
Applied patch from Kelvin Tan to improve the jelly Servlet and provide a
JellyServletContext
Revision Changes Path
1.3 +34 -22
jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/servlet/JellyServlet.java
Index: JellyServlet.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/servlet/JellyServlet.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- JellyServlet.java 11 Dec 2002 12:41:02 -0000 1.2
+++ JellyServlet.java 17 Dec 2002 08:32:56 -0000 1.3
@@ -95,11 +95,11 @@
*/
public static final String RESPONSE = "response";
- protected void doGet(
+ protected void doGet(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
-
+
doRequest(request, response);
}
@@ -107,7 +107,7 @@
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
-
+
doRequest(request, response);
}
@@ -120,11 +120,11 @@
*/
protected void doRequest(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
-
+
JellyContext context = createContext(req, res);
- URL template = getTemplate(req);
try {
- runScript(template, context, req, res);
+ URL script = getScript(req);
+ runScript(script, context, req, res);
}
catch (Exception e) {
error(req, res, e);
@@ -140,27 +140,39 @@
protected JellyContext createContext(
HttpServletRequest req,
HttpServletResponse res) {
-
- JellyContext ctx = new JellyContext();
+
+ JellyContext ctx = new JellyServletContext(getServletContext());
ctx.setVariable(REQUEST, req);
ctx.setVariable(RESPONSE, res);
return ctx;
}
/**
+ * <p>
+ * Either use the query parameter "script", or the URI itself
+ * to denote the script to run.
+ * </p>
+ * <p>
+ * Example: script=index.jelly or http://localhost:8080/foo/index.jelly.
+ * </p>
+ *
* @see org.apache.velocity.servlet.VelocityServlet#getTemplate
* @param req
* @return
* @throws MalformedURLException
*/
- protected URL getTemplate(HttpServletRequest req)
+ protected URL getScript(HttpServletRequest req)
throws MalformedURLException {
-
- String script = req.getParameter("template");
- if (script == null) {
- script = req.getServletPath();
+
+ String scriptUrl = req.getParameter("script");
+ if (scriptUrl == null) {
+ scriptUrl = req.getPathInfo();
}
- return getServletContext().getResource(script);
+ URL url = getServletContext().getResource(scriptUrl);
+ if (url == null) {
+ throw new IllegalArgumentException("Invalid script url:" + scriptUrl);
+ }
+ return url;
}
/**
@@ -179,7 +191,7 @@
HttpServletRequest req,
HttpServletResponse res)
throws IOException, UnsupportedEncodingException, Exception {
-
+
ServletOutputStream output = res.getOutputStream();
XMLOutput xmlOutput = XMLOutput.createXMLOutput(output);
context.runScript(script, xmlOutput);
@@ -204,12 +216,12 @@
HttpServletResponse response,
Exception cause)
throws ServletException, IOException {
-
+
StringBuffer html = new StringBuffer();
html.append("<html>");
html.append("<title>Error</title>");
html.append("<body bgcolor=\"#ffffff\">");
- html.append("<h2>JellyServlet : Error processing the template</h2>");
+ html.append("<h2>JellyServlet : Error processing the script</h2>");
html.append("<pre>");
String why = cause.getMessage();
if (why != null && why.trim().length() > 0) {
1.1
jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/servlet/JellyServletContext.java
Index: JellyServletContext.java
===================================================================
/*
*
/home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/servlet/JellyServletContext.java,v
1.1 2002/12/17 08:32:56 jstrachan Exp
* 1.1
* 2002/12/17 08:32:56
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* JellyServletContext.java,v 1.1 2002/12/17 08:32:56 jstrachan Exp
*/
package org.apache.commons.jelly.servlet;
import org.apache.commons.jelly.JellyContext;
import javax.servlet.ServletContext;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
/**
*
* @author <a href="mailto:[EMAIL PROTECTED]">Kelvin Tan</a>
* @version 1.1
*/
public class JellyServletContext extends JellyContext {
private ServletContext ctx;
public JellyServletContext() {
}
public JellyServletContext(ServletContext ctx) {
super();
this.ctx = ctx;
}
public JellyServletContext(JellyContext parent, ServletContext ctx) {
super(parent);
this.ctx = ctx;
}
/**
* Allow access of relative URIs when performing <j:include>.
* @param s
* @return
* @throws MalformedURLException
*/
public URL getResource(String s) throws MalformedURLException {
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);
}
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>