Coverage report

| %line | %branch
--- | --- | ---
org.apache.commons.jelly.servlet.JellyServlet |
0% |
0% |

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 | 0 |
     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 | 0 |
             doRequest(request, response);
59 | 0 |
         }
60 |  |
61 |  |
         protected void doPost(
62 |  |
             HttpServletRequest request,
63 |  |
             HttpServletResponse response)
64 |  |
             throws ServletException, IOException {
65 |  |
66 | 0 |
             doRequest(request, response);
67 | 0 |
         }
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 | 0 |
             JellyContext context = createContext(req, res);
80 |  |
             try {
81 | 0 |
                 URL script = getScript(req);
82 | 0 |
                 runScript(script, context, req, res);
83 |  |
             }
84 | 0 |
             catch (Exception e) {
85 | 0 |
                 error(req, res, e);
86 | 0 |
             }
87 | 0 |
         }
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 | 0 |
             JellyContext ctx = new JellyServletContext(getServletContext());
100 | 0 |
             ctx.setVariable(REQUEST, req);
101 | 0 |
             ctx.setVariable(RESPONSE, res);
102 | 0 |
             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 | 0 |
             String scriptUrl = req.getParameter("script");
123 | 0 |
             if (scriptUrl == null) {
124 | 0 |
                 scriptUrl = req.getPathInfo();
125 |  |
             }
126 | 0 |
             URL url = getServletContext().getResource(scriptUrl);
127 | 0 |
             if (url == null) {
128 | 0 |
                 throw new IllegalArgumentException("Invalid script url:" + scriptUrl);
129 |  |
             }
130 | 0 |
             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 | 0 |
             ServletOutputStream output = res.getOutputStream();
151 | 0 |
             XMLOutput xmlOutput = XMLOutput.createXMLOutput(output);
152 | 0 |
             context.runScript(script, xmlOutput);
153 | 0 |
             xmlOutput.flush();
154 | 0 |
             xmlOutput.close();
155 | 0 |
             output.flush();
156 | 0 |
         }
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 | 0 |
             StringBuffer html = new StringBuffer();
176 | 0 |
             html.append("<html>");
177 | 0 |
             html.append("<title>Error</title>");
178 | 0 |
             html.append("<body bgcolor=\"#ffffff\">");
179 | 0 |
             html.append("<h2>JellyServlet : Error processing the script</h2>");
180 | 0 |
             html.append("<pre>");
181 | 0 |
             String why = cause.getMessage();
182 | 0 |
             if (why != null && why.trim().length() > 0) {
183 | 0 |
                 html.append(why);
184 | 0 |
                 html.append("<br>");
185 |  |
             }
186 |  |
187 | 0 |
             StringWriter sw = new StringWriter();
188 | 0 |
             cause.printStackTrace(new PrintWriter(sw));
189 |  |
190 | 0 |
             html.append(sw.toString());
191 | 0 |
             html.append("</pre>");
192 | 0 |
             html.append("</body>");
193 | 0 |
             html.append("</html>");
194 | 0 |
             response.getOutputStream().print(html.toString());
195 | 0 |
         }
196 |  |
     }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.