View Javadoc

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;
20  import java.io.PrintWriter;
21  import java.io.StringWriter;
22  import java.io.UnsupportedEncodingException;
23  import java.net.MalformedURLException;
24  import java.net.URL;
25  
26  import javax.servlet.ServletException;
27  import javax.servlet.ServletOutputStream;
28  import javax.servlet.http.HttpServlet;
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
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 {
43      /***
44       * The HTTP request object context key.
45       */
46      public static final String REQUEST = "request";
47  
48      /***
49       * The HTTP response object context key.
50       */
51      public static final String RESPONSE = "response";
52  
53      protected void doGet(
54          HttpServletRequest request,
55          HttpServletResponse response)
56          throws ServletException, IOException {
57  
58          doRequest(request, response);
59      }
60  
61      protected void doPost(
62          HttpServletRequest request,
63          HttpServletResponse response)
64          throws ServletException, IOException {
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 req, HttpServletResponse res)
77          throws ServletException, IOException {
78  
79          JellyContext context = createContext(req, res);
80          try {
81              URL script = getScript(req);
82              runScript(script, context, req, res);
83          }
84          catch (Exception 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 req,
97          HttpServletResponse 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 getScript(HttpServletRequest req)
120         throws MalformedURLException {
121 
122         String scriptUrl = req.getParameter("script");
123         if (scriptUrl == null) {
124             scriptUrl = req.getPathInfo();
125         }
126         URL url = getServletContext().getResource(scriptUrl);
127         if (url == null) {
128             throw new IllegalArgumentException("Invalid script url:" + scriptUrl);
129         }
130         return url;
131     }
132 
133     /***
134      * @see org.apache.velocity.servlet.VelocityServlet#mergeTemplate
135      * @param script
136      * @param context
137      * @param req
138      * @param res
139      * @throws IOException
140      * @throws UnsupportedEncodingException
141      * @throws JellyException
142      */
143     protected void runScript(
144         URL script,
145         JellyContext context,
146         HttpServletRequest req,
147         HttpServletResponse res)
148         throws IOException, UnsupportedEncodingException, JellyException {
149 
150         ServletOutputStream output = res.getOutputStream();
151         XMLOutput xmlOutput = XMLOutput.createXMLOutput(output);
152         context.runScript(script, xmlOutput);
153         xmlOutput.flush();
154         xmlOutput.close();
155         output.flush();
156     }
157 
158     /***
159      * Invoked when there is an error thrown in any part of doRequest() processing.
160      * <br><br>
161      * Default will send a simple HTML response indicating there was a problem.
162      *<br><br>
163      * Ripped from VelocityServlet.
164      *
165      * @param request original HttpServletRequest from servlet container.
166      * @param response HttpServletResponse object from servlet container.
167      * @param cause  Exception that was thrown by some other part of process.
168      */
169     protected void error(
170         HttpServletRequest request,
171         HttpServletResponse response,
172         Exception cause)
173         throws ServletException, IOException {
174 
175         StringBuffer html = new StringBuffer();
176         html.append("<html>");
177         html.append("<title>Error</title>");
178         html.append("<body bgcolor=\"#ffffff\">");
179         html.append("<h2>JellyServlet : Error processing the script</h2>");
180         html.append("<pre>");
181         String why = cause.getMessage();
182         if (why != null && why.trim().length() > 0) {
183             html.append(why);
184             html.append("<br>");
185         }
186 
187         StringWriter sw = new StringWriter();
188         cause.printStackTrace(new PrintWriter(sw));
189 
190         html.append(sw.toString());
191         html.append("</pre>");
192         html.append("</body>");
193         html.append("</html>");
194         response.getOutputStream().print(html.toString());
195     }
196 }