KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > jelly > servlet > JellyServlet


1 /*
2  * Copyright 2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.commons.jelly.servlet;
18
19 import java.io.IOException JavaDoc;
20 import java.io.PrintWriter JavaDoc;
21 import java.io.StringWriter JavaDoc;
22 import java.io.UnsupportedEncodingException JavaDoc;
23 import java.net.MalformedURLException JavaDoc;
24 import java.net.URL JavaDoc;
25
26 import javax.servlet.ServletException JavaDoc;
27 import javax.servlet.ServletOutputStream JavaDoc;
28 import javax.servlet.http.HttpServlet JavaDoc;
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import javax.servlet.http.HttpServletResponse JavaDoc;
31
32 import org.apache.commons.jelly.JellyContext;
33 import org.apache.commons.jelly.JellyException;
34 import org.apache.commons.jelly.XMLOutput;
35
36 /**
37  * Servlet for handling display of Jelly-fied XML files. Modelled after VelocityServlet.
38  *
39  * @author Kelvin Tan
40  * @version $Revision: 155420 $
41  */

42 public class JellyServlet extends HttpServlet JavaDoc {
43     /**
44      * The HTTP request object context key.
45      */

46     public static final String JavaDoc REQUEST = "request";
47
48     /**
49      * The HTTP response object context key.
50      */

51     public static final String JavaDoc RESPONSE = "response";
52
53     protected void doGet(
54         HttpServletRequest JavaDoc request,
55         HttpServletResponse JavaDoc response)
56         throws ServletException JavaDoc, IOException JavaDoc {
57
58         doRequest(request, response);
59     }
60
61     protected void doPost(
62         HttpServletRequest JavaDoc request,
63         HttpServletResponse JavaDoc response)
64         throws ServletException JavaDoc, IOException JavaDoc {
65
66         doRequest(request, response);
67     }
68
69     /**
70      * Handles all requests
71      * @param req HttpServletRequest object containing client request
72      * @param res HttpServletResponse object for the response
73      * @throws ServletException
74      * @throws IOException
75      */

76     protected void doRequest(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
77         throws ServletException JavaDoc, IOException JavaDoc {
78
79         JellyContext context = createContext(req, res);
80         try {
81             URL JavaDoc script = getScript(req);
82             runScript(script, context, req, res);
83         }
84         catch (Exception JavaDoc e) {
85             error(req, res, e);
86         }
87     }
88
89     /**
90      * @see org.apache.velocity.servlet.VelocityServlet#createContext
91      * @param req
92      * @param res
93      * @return
94      */

95     protected JellyContext createContext(
96         HttpServletRequest JavaDoc req,
97         HttpServletResponse JavaDoc res) {
98
99         JellyContext ctx = new JellyServletContext(getServletContext());
100         ctx.setVariable(REQUEST, req);
101         ctx.setVariable(RESPONSE, res);
102         return ctx;
103     }
104
105     /**
106      * <p>
107      * Either use the query parameter "script", or the URI itself
108      * to denote the script to run.
109      * </p>
110      * <p>
111      * Example: script=index.jelly or http://localhost:8080/foo/index.jelly.
112      * </p>
113      *
114      * @see org.apache.velocity.servlet.VelocityServlet#getTemplate
115      * @param req
116      * @return
117      * @throws MalformedURLException
118      */

119     protected URL JavaDoc getScript(HttpServletRequest JavaDoc req)
120         throws MalformedURLException JavaDoc {
121
122         String JavaDoc scriptUrl = req.getParameter("script");
123         if (scriptUrl == null) {
124             scriptUrl = req.getPathInfo();
125         }
126         if (scriptUrl == null) {
127             scriptUrl = req.getServletPath();
128         }
129         URL JavaDoc url = getServletContext().getResource(scriptUrl);
130         if (url == null) {
131             throw new IllegalArgumentException JavaDoc("Invalid script url:" + scriptUrl);
132         }
133         return url;
134     }
135
136     /**
137      * @see org.apache.velocity.servlet.VelocityServlet#mergeTemplate
138      * @param script
139      * @param context
140      * @param req
141      * @param res
142      * @throws IOException
143      * @throws UnsupportedEncodingException
144      * @throws JellyException
145      */

146     protected void runScript(
147         URL JavaDoc script,
148         JellyContext context,
149         HttpServletRequest JavaDoc req,
150         HttpServletResponse JavaDoc res)
151         throws IOException JavaDoc, UnsupportedEncodingException JavaDoc, JellyException {
152
153         ServletOutputStream JavaDoc output = res.getOutputStream();
154         XMLOutput xmlOutput = XMLOutput.createXMLOutput(output);
155         context.runScript(script, xmlOutput);
156         xmlOutput.flush();
157         xmlOutput.close();
158         output.flush();
159     }
160
161     /**
162      * Invoked when there is an error thrown in any part of doRequest() processing.
163      * <br><br>
164      * Default will send a simple HTML response indicating there was a problem.
165      *<br><br>
166      * Ripped from VelocityServlet.
167      *
168      * @param request original HttpServletRequest from servlet container.
169      * @param response HttpServletResponse object from servlet container.
170      * @param cause Exception that was thrown by some other part of process.
171      */

172     protected void error(
173         HttpServletRequest JavaDoc request,
174         HttpServletResponse JavaDoc response,
175         Exception JavaDoc cause)
176         throws ServletException JavaDoc, IOException JavaDoc {
177
178         StringBuffer JavaDoc html = new StringBuffer JavaDoc();
179         html.append("<html>");
180         html.append("<title>Error</title>");
181         html.append("<body bgcolor=\"#ffffff\">");
182         html.append("<h2>JellyServlet : Error processing the script</h2>");
183         html.append("<pre>");
184         String JavaDoc why = cause.getMessage();
185         if (why != null && why.trim().length() > 0) {
186             html.append(why);
187             html.append("<br>");
188         }
189
190         StringWriter JavaDoc sw = new StringWriter JavaDoc();
191         cause.printStackTrace(new PrintWriter JavaDoc(sw));
192
193         html.append(sw.toString());
194         html.append("</pre>");
195         html.append("</body>");
196         html.append("</html>");
197         response.getOutputStream().print(html.toString());
198     }
199 }
200
Popular Tags