1 package org.apache.turbine.services.jsp;
2
3 /* ====================================================================
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 2001 The Apache Software Foundation. All rights
7 * reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution,
22 * if any, must include the following acknowledgment:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowledgment may appear in the software itself,
26 * if and wherever such third-party acknowledgments normally appear.
27 *
28 * 4. The names "Apache" and "Apache Software Foundation" and
29 * "Apache Turbine" must not be used to endorse or promote products
30 * derived from this software without prior written permission. For
31 * written permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache",
34 * "Apache Turbine", nor may "Apache" appear in their name, without
35 * prior written permission of the Apache Software Foundation.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57 import java.io.IOException;
58 import javax.servlet.RequestDispatcher;
59 import javax.servlet.ServletConfig;
60 import javax.servlet.ServletContext;
61 import javax.servlet.http.HttpServletRequest;
62 import org.apache.commons.configuration.Configuration;
63 import org.apache.turbine.services.InitializationException;
64 import org.apache.turbine.services.jsp.util.JspLink;
65 import org.apache.turbine.services.servlet.TurbineServlet;
66 import org.apache.turbine.services.template.BaseTemplateEngineService;
67 import org.apache.turbine.services.template.TurbineTemplate;
68 import org.apache.turbine.util.RunData;
69 import org.apache.turbine.util.TurbineException;
70
71
72 /***
73 * This is a Service that can process JSP templates from within a Turbine
74 * screen.
75 *
76 * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
77 * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
78 * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
79 */
80 public class TurbineJspService extends BaseTemplateEngineService
81 implements JspService
82 {
83 /*** The base path[s] prepended to filenames given in arguments */
84 private String[] templatePaths;
85
86 /*** The relative path[s] prepended to filenames */
87 private String[] relativeTemplatePaths;
88
89 /*** The buffer size for the output stream. */
90 private int bufferSize;
91
92 /***
93 * Performs early initialization of this Turbine service.
94 */
95 public void init(ServletConfig config) throws InitializationException
96 {
97 try
98 {
99 initJsp();
100 registerConfiguration("jsp");
101 setInit(true);
102 }
103 catch (Exception e)
104 {
105 throw new InitializationException(
106 "TurbineJspService failed to initialize", e);
107 }
108 }
109
110 /***
111 * Adds some convenience objects to the request. For example an instance
112 * of JspLink which can be used to generate links to other templates.
113 *
114 * @param RunData the turbine rundata object
115 */
116 public void addDefaultObjects(RunData data)
117 {
118 HttpServletRequest req = data.getRequest();
119 req.setAttribute(LINK, new JspLink(data));
120 req.setAttribute(RUNDATA, data);
121 }
122
123 /***
124 * The buffer size
125 *
126 */
127 public int getDefaultBufferSize()
128 {
129 return bufferSize;
130 }
131
132 /***
133 * Process the request
134 *
135 * @param RunData
136 * @param String the filename of the template.
137 * @throws TurbineException Any exception trown while processing will be
138 * wrapped into a TurbineException and rethrown.
139 */
140 public void handleRequest(RunData data, String filename)
141 throws TurbineException
142 {
143 handleRequest(data, filename, false);
144 }
145
146 /***
147 * Process the request
148 *
149 * @param RunData
150 * @param String the filename of the template.
151 * @param boolean whether to perform a forward or include.
152 * @throws TurbineException Any exception trown while processing will be
153 * wrapped into a TurbineException and rethrown.
154 */
155 public void handleRequest(RunData data, String filename, boolean isForward)
156 throws TurbineException
157 {
158 /*** template name with relative path */
159 String relativeTemplateName = getRelativeTemplateName(filename);
160
161 if (relativeTemplateName == null)
162 {
163 throw new TurbineException(
164 "Template " + filename + " not found in template paths");
165 }
166
167 // get the RequestDispatcher for the JSP
168 RequestDispatcher dispatcher = data.getServletContext()
169 .getRequestDispatcher(relativeTemplateName);
170
171 try
172 {
173 if (isForward)
174 {
175 // forward the request to the JSP
176 dispatcher.forward(data.getRequest(), data.getResponse());
177 }
178 else
179 {
180 data.getOut().flush();
181 // include the JSP
182 dispatcher.include(data.getRequest(), data.getResponse());
183 }
184 }
185 catch(Exception e)
186 {
187 // as JSP service is in Alpha stage, let's try hard to send the
188 // error message to the browser, to speed up debugging
189 try
190 {
191 data.getOut().print("Error encountered processing a template: "
192 + filename);
193 e.printStackTrace(data.getOut());
194 }
195 catch(IOException ignored)
196 {
197 }
198
199 // pass the exception to the caller according to the general
200 // contract for tamplating services in Turbine
201 throw new TurbineException(
202 "Error encountered processing a template:" + filename, e);
203 }
204 }
205
206 /***
207 * This method sets up the template cache.
208 */
209 private void initJsp() throws Exception
210 {
211 ServletContext context = TurbineServlet.getServletContext();
212 Configuration config = getConfiguration();
213
214 /*
215 * Use the turbine template service to translate
216 * the template paths.
217 */
218 templatePaths = TurbineTemplate.translateTemplatePaths(
219 config.getStringArray("templates"));
220
221 /*
222 * Set relative paths from config.
223 * Needed for javax.servlet.RequestDispatcher
224 */
225 relativeTemplatePaths = config.getStringArray("templates");
226
227 /*
228 * Make sure that the relative paths begin with /
229 */
230 for (int i = 0; i < relativeTemplatePaths.length; i++)
231 {
232 if (!relativeTemplatePaths[i].startsWith("/"))
233 {
234 relativeTemplatePaths[i] = "/" + relativeTemplatePaths[i];
235 }
236 }
237
238 bufferSize = config.getInt("buffer.size", 8192);
239
240 /*
241 * Register with the template service.
242 */
243 registerConfiguration("jsp");
244 }
245
246 /***
247 * Determine whether a given template exists. This service
248 * currently only supports file base template hierarchies
249 * so we will use the utility methods provided by
250 * the template service to do the searching.
251 *
252 * @param String template
253 * @return boolean
254 */
255 public boolean templateExists(String template)
256 {
257 return TurbineTemplate.templateExists(template, templatePaths);
258 }
259 /***
260 * Searchs for a template in the default.template path[s] and
261 * returns the template name with a relative path which is
262 * required by <a href="http://java.sun.com/products/servlet/2.3/javadoc/javax/servlet/ServletContext.html#getRequestDispatcher(java.lang.String)">
263 * javax.servlet.RequestDispatcher</a>
264 *
265 * @param String template
266 * @return String
267 */
268
269 public String getRelativeTemplateName(String template)
270 {
271 /*
272 * A dummy String[] object used to pass a String to
273 * TurbineTemplate.templateExists
274 */
275 String[] testTemplatePath = new String[1];
276
277 /***
278 * Find which template path the template is in
279 */
280 for (int i = 0; i < relativeTemplatePaths.length; i++)
281 {
282 testTemplatePath[0] = TurbineServlet.getRealPath(
283 relativeTemplatePaths[i]);
284 if (TurbineTemplate.templateExists(template, testTemplatePath))
285 {
286 return relativeTemplatePaths[i] + template;
287 }
288 }
289 return null;
290 }
291 }
This page was automatically generated by Maven