1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package javax.faces.webapp;
20
21 import java.io.IOException;
22 import java.util.logging.Level;
23 import java.util.logging.Logger;
24
25 import javax.faces.FacesException;
26 import javax.faces.FactoryFinder;
27 import javax.faces.application.ResourceHandler;
28 import javax.faces.context.FacesContext;
29 import javax.faces.context.FacesContextFactory;
30 import javax.faces.lifecycle.Lifecycle;
31 import javax.faces.lifecycle.LifecycleFactory;
32 import javax.servlet.Servlet;
33 import javax.servlet.ServletConfig;
34 import javax.servlet.ServletException;
35 import javax.servlet.ServletRequest;
36 import javax.servlet.ServletResponse;
37 import javax.servlet.annotation.MultipartConfig;
38 import javax.servlet.http.HttpServletRequest;
39 import javax.servlet.http.HttpServletResponse;
40
41 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFWebConfigParam;
42
43
44
45 @MultipartConfig
46 public final class FacesServlet implements Servlet
47 {
48
49 private static final Logger log = Logger.getLogger(FacesServlet.class.getName());
50
51
52
53
54
55
56 @JSFWebConfigParam(since="1.1")
57 public static final String CONFIG_FILES_ATTR = "javax.faces.CONFIG_FILES";
58
59
60
61
62 @JSFWebConfigParam(since="1.1")
63 public static final String LIFECYCLE_ID_ATTR = "javax.faces.LIFECYCLE_ID";
64
65
66
67
68 @JSFWebConfigParam(since="2.3")
69 public static final String DISABLE_FACESSERVLET_TO_XHTML_PARAM_NAME = "javax.faces.DISABLE_FACESSERVLET_TO_XHTML";
70
71 private static final String SERVLET_INFO = "FacesServlet of the MyFaces API implementation";
72
73 private ServletConfig _servletConfig;
74 private FacesContextFactory _facesContextFactory;
75 private Lifecycle _lifecycle;
76
77 public FacesServlet()
78 {
79 super();
80 }
81
82 public void destroy()
83 {
84 _servletConfig = null;
85 _facesContextFactory = null;
86 _lifecycle = null;
87 if (log.isLoggable(Level.FINEST))
88 {
89 log.finest("destroy");
90 }
91 }
92
93 public ServletConfig getServletConfig()
94 {
95 return _servletConfig;
96 }
97
98 public String getServletInfo()
99 {
100 return SERVLET_INFO;
101 }
102
103 private String getLifecycleId()
104 {
105
106
107
108 String serLifecycleId = _servletConfig.getInitParameter(LIFECYCLE_ID_ATTR);
109 String appLifecycleId = _servletConfig.getServletContext().getInitParameter(LIFECYCLE_ID_ATTR);
110 appLifecycleId = serLifecycleId == null ? appLifecycleId : serLifecycleId;
111 return appLifecycleId != null ? appLifecycleId : LifecycleFactory.DEFAULT_LIFECYCLE;
112 }
113
114 public void init(ServletConfig servletConfig) throws ServletException
115 {
116 if (log.isLoggable(Level.FINEST))
117 {
118 log.finest("init begin");
119 }
120 _servletConfig = servletConfig;
121 _facesContextFactory = (FacesContextFactory)FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
122
123
124
125
126
127 LifecycleFactory lifecycleFactory = (LifecycleFactory)FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
128 _lifecycle = lifecycleFactory.getLifecycle(getLifecycleId());
129 if (log.isLoggable(Level.FINEST))
130 {
131 log.finest("init end");
132 }
133 }
134
135 public void service(ServletRequest request, ServletResponse response) throws IOException, ServletException
136 {
137
138
139
140 HttpServletRequest httpRequest = (HttpServletRequest)request;
141 String pathInfo = httpRequest.getPathInfo();
142
143
144
145
146
147
148
149
150
151
152
153
154
155 if (pathInfo != null && (pathInfo.startsWith("/WEB-INF") || pathInfo.startsWith("/META-INF")))
156 {
157 StringBuffer buffer = new StringBuffer();
158
159 buffer.append(" Someone is trying to access a secure resource : ").append(pathInfo);
160 buffer.append("\n remote address is ").append(httpRequest.getRemoteAddr());
161 buffer.append("\n remote host is ").append(httpRequest.getRemoteHost());
162 buffer.append("\n remote user is ").append(httpRequest.getRemoteUser());
163 buffer.append("\n request URI is ").append(httpRequest.getRequestURI());
164
165 log.warning(buffer.toString());
166
167
168
169 ((HttpServletResponse)response).sendError(HttpServletResponse.SC_NOT_FOUND);
170 return;
171 }
172
173
174
175 if (log.isLoggable(Level.FINEST))
176 {
177 log.finest("service begin");
178 }
179
180
181 FacesContext facesContext = prepareFacesContext(request, response);
182
183 try
184 {
185
186
187
188
189
190 ResourceHandler resourceHandler = facesContext.getApplication().getResourceHandler();
191
192
193 if (resourceHandler.isResourceRequest(facesContext))
194 {
195
196 resourceHandler.handleResourceRequest(facesContext);
197 }
198 else
199 {
200
201 _lifecycle.attachWindow(facesContext);
202
203
204 _lifecycle.execute(facesContext);
205
206 _lifecycle.render(facesContext);
207 }
208 }
209 catch (FacesException e)
210 {
211
212
213
214 Throwable cause = e.getCause();
215 if (cause == null)
216 {
217
218
219
220 throw new ServletException(e.getLocalizedMessage(), e);
221 }
222 else if (cause instanceof ServletException)
223 {
224
225 throw (ServletException)cause;
226 }
227 else if (cause instanceof IOException)
228 {
229
230 throw (IOException)cause;
231 }
232 else
233 {
234
235
236 throw new ServletException(cause.getLocalizedMessage(), cause);
237 }
238 }
239 finally
240 {
241
242 facesContext.release();
243 }
244 if (log.isLoggable(Level.FINEST))
245 {
246 log.finest("service end");
247 }
248 }
249
250 private FacesContext prepareFacesContext(ServletRequest request, ServletResponse response)
251 {
252 FacesContext facesContext =
253 _facesContextFactory.getFacesContext(_servletConfig.getServletContext(), request, response, _lifecycle);
254 return facesContext;
255 }
256 }