1 /*
2 * $Id: FacesServlet.java,v 1.34 2007/07/16 17:06:42 rlubke Exp $
3 */
4
5 /*
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
7 *
8 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
9 *
10 * The contents of this file are subject to the terms of either the GNU
11 * General Public License Version 2 only ("GPL") or the Common Development
12 * and Distribution License("CDDL") (collectively, the "License"). You
13 * may not use this file except in compliance with the License. You can obtain
14 * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
15 * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
16 * language governing permissions and limitations under the License.
17 *
18 * When distributing the software, include this License Header Notice in each
19 * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
20 * Sun designates this particular file as subject to the "Classpath" exception
21 * as provided by Sun in the GPL Version 2 section of the License file that
22 * accompanied this code. If applicable, add the following below the License
23 * Header, with the fields enclosed by brackets [] replaced by your own
24 * identifying information: "Portions Copyrighted [year]
25 * [name of copyright owner]"
26 *
27 * Contributor(s):
28 *
29 * If you wish your version of this file to be governed by only the CDDL or
30 * only the GPL Version 2, indicate your decision by adding "[Contributor]
31 * elects to include this software in this distribution under the [CDDL or GPL
32 * Version 2] license." If you don't indicate a single choice of license, a
33 * recipient has the option to distribute your version of this file under
34 * either the CDDL, the GPL Version 2 or to extend the choice of license to
35 * its licensees as provided above. However, if you add GPL Version 2 code
36 * and therefore, elected the GPL Version 2 license, then the option applies
37 * only if the new code is made subject to such option by the copyright
38 * holder.
39 */
40
41 package javax.faces.webapp;
42
43
44 import javax.faces.FacesException;
45 import javax.faces.FactoryFinder;
46 import javax.faces.context.FacesContext;
47 import javax.faces.context.FacesContextFactory;
48 import javax.faces.lifecycle.Lifecycle;
49 import javax.faces.lifecycle.LifecycleFactory;
50 import javax.servlet.Servlet;
51 import javax.servlet.ServletConfig;
52 import javax.servlet.ServletException;
53 import javax.servlet.ServletRequest;
54 import javax.servlet.ServletResponse;
55 import javax.servlet.UnavailableException;
56 import javax.servlet.http.HttpServletRequest;
57 import javax.servlet.http.HttpServletResponse;
58
59 import java.io.IOException;
60 import java.util.ResourceBundle;
61 import java.util.logging.Level;
62 import java.util.logging.Logger;
63
64
65 /**
66 * <p><strong>FacesServlet</strong> is a servlet that manages the request
67 * processing lifecycle for web applications that are utilizing JavaServer
68 * Faces to construct the user interface.</p>
69 */
70
71 public final class FacesServlet implements Servlet {
72
73
74 /**
75 * <p>Context initialization parameter name for a comma delimited list
76 * of context-relative resource paths (in addition to
77 * <code>/WEB-INF/faces-config.xml</code> which is loaded automatically
78 * if it exists) containing JavaServer Faces configuration information.</p>
79 */
80 public static final String CONFIG_FILES_ATTR =
81 "javax.faces.CONFIG_FILES";
82
83
84 /**
85 * <p>Context initialization parameter name for the lifecycle identifier
86 * of the {@link Lifecycle} instance to be utilized.</p>
87 */
88 public static final String LIFECYCLE_ID_ATTR =
89 "javax.faces.LIFECYCLE_ID";
90
91
92 /**
93 * The <code>Logger</code> for this class.
94 */
95 private static final Logger LOGGER =
96 Logger.getLogger("javax.faces.webapp", "javax.faces.LogStrings");
97
98
99 /**
100 * <p>Factory for {@link FacesContext} instances.</p>
101 */
102 private FacesContextFactory facesContextFactory = null;
103
104
105 /**
106 * <p>The {@link Lifecycle} instance to use for request processing.</p>
107 */
108 private Lifecycle lifecycle = null;
109
110
111 /**
112 * <p>The <code>ServletConfig</code> instance for this servlet.</p>
113 */
114 private ServletConfig servletConfig = null;
115
116
117 /**
118 * <p>Release all resources acquired at startup time.</p>
119 */
120 public void destroy() {
121
122 facesContextFactory = null;
123 lifecycle = null;
124 servletConfig = null;
125
126 }
127
128
129 /**
130 * <p>Return the <code>ServletConfig</code> instance for this servlet.</p>
131 */
132 public ServletConfig getServletConfig() {
133
134 return (this.servletConfig);
135
136 }
137
138
139 /**
140 * <p>Return information about this Servlet.</p>
141 */
142 public String getServletInfo() {
143
144 return (this.getClass().getName());
145
146 }
147
148
149 /**
150 * <p>Acquire the factory instances we will require.</p>
151 *
152 * @throws ServletException if, for any reason, the startup of
153 * this Faces application failed. This includes errors in the
154 * config file that is parsed before or during the processing of
155 * this <code>init()</code> method.
156 */
157 public void init(ServletConfig servletConfig) throws ServletException {
158
159 // Save our ServletConfig instance
160 this.servletConfig = servletConfig;
161
162 // Acquire our FacesContextFactory instance
163 try {
164 facesContextFactory = (FacesContextFactory)
165 FactoryFinder.getFactory
166 (FactoryFinder.FACES_CONTEXT_FACTORY);
167 } catch (FacesException e) {
168 ResourceBundle rb = LOGGER.getResourceBundle();
169 String msg = rb.getString("severe.webapp.facesservlet.init_failed");
170 Throwable rootCause = (e.getCause() != null) ? e.getCause() : e;
171 LOGGER.log(Level.SEVERE, msg, rootCause);
172 throw new UnavailableException(msg);
173 }
174
175 // Acquire our Lifecycle instance
176 try {
177 LifecycleFactory lifecycleFactory = (LifecycleFactory)
178 FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
179 String lifecycleId ;
180
181 // First look in the servlet init-param set
182 if (null == (lifecycleId = servletConfig.getInitParameter(LIFECYCLE_ID_ATTR))) {
183 // If not found, look in the context-param set
184 lifecycleId = servletConfig.getServletContext().getInitParameter
185 (LIFECYCLE_ID_ATTR);
186 }
187
188 if (lifecycleId == null) {
189 lifecycleId = LifecycleFactory.DEFAULT_LIFECYCLE;
190 }
191 lifecycle = lifecycleFactory.getLifecycle(lifecycleId);
192 } catch (FacesException e) {
193 Throwable rootCause = e.getCause();
194 if (rootCause == null) {
195 throw e;
196 } else {
197 throw new ServletException(e.getMessage(), rootCause);
198 }
199 }
200
201 }
202
203
204 /**
205 * <p>Process an incoming request, and create the corresponding
206 * response, by executing the request processing lifecycle.</p>
207 *
208 * <p>If the <code>request</code> and <code>response</code>
209 * arguments to this method are not instances of
210 * <code>HttpServletRequest</code> and
211 * <code>HttpServletResponse</code>, respectively, the results of
212 * invoking this method are undefined.</p>
213 *
214 * <p>This method must respond to requests that start with the
215 * following strings by invoking the <code>sendError</code> method
216 * on the response argument (cast to
217 * <code>HttpServletResponse</code>), passing the code
218 * <code>HttpServletResponse.SC_NOT_FOUND</code> as the
219 * argument. </p>
220 *
221 * <ul>
222 *
223 <pre><code>
224 /WEB-INF/
225 /WEB-INF
226 /META-INF/
227 /META-INF
228 </code></pre>
229 *
230 * </ul>
231 *
232 *
233 *
234 * @param request The servlet request we are processing
235 * @param response The servlet response we are creating
236 *
237 * @throws IOException if an input/output error occurs during processing
238 * @throws ServletException if a servlet error occurs during processing
239 */
240 public void service(ServletRequest request,
241 ServletResponse response)
242 throws IOException, ServletException {
243
244 // If prefix mapped, then ensure requests for /WEB-INF are
245 // not processed.
246 String pathInfo = ((HttpServletRequest) request).getPathInfo();
247 if (pathInfo != null) {
248 pathInfo = pathInfo.toUpperCase();
249 if (pathInfo.startsWith("/WEB-INF/")
250 || pathInfo.equals("/WEB-INF")
251 || pathInfo.startsWith("/META-INF/")
252 || pathInfo.equals("/META-INF")) {
253 ((HttpServletResponse) response).
254 sendError(HttpServletResponse.SC_NOT_FOUND);
255 return;
256 }
257 }
258
259 // Acquire the FacesContext instance for this request
260 FacesContext context = facesContextFactory.getFacesContext
261 (servletConfig.getServletContext(), request, response, lifecycle);
262
263 // Execute the request processing lifecycle for this request
264 try {
265 lifecycle.execute(context);
266 lifecycle.render(context);
267 } catch (FacesException e) {
268 Throwable t = e.getCause();
269 if (t == null) {
270 throw new ServletException(e.getMessage(), e);
271 } else {
272 if (t instanceof ServletException) {
273 throw ((ServletException) t);
274 } else if (t instanceof IOException) {
275 throw ((IOException) t);
276 } else {
277 throw new ServletException(t.getMessage(), t);
278 }
279 }
280 }
281 finally {
282 // Release the FacesContext instance for this request
283 context.release();
284 }
285
286 }
287
288
289 }