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 private static final String SERVLET_INFO = "FacesServlet of the MyFaces API implementation";
66
67 private ServletConfig _servletConfig;
68 private FacesContextFactory _facesContextFactory;
69 private Lifecycle _lifecycle;
70
71 public FacesServlet()
72 {
73 super();
74 }
75
76 public void destroy()
77 {
78 _servletConfig = null;
79 _facesContextFactory = null;
80 _lifecycle = null;
81 if (log.isLoggable(Level.FINEST))
82 {
83 log.finest("destroy");
84 }
85 }
86
87 public ServletConfig getServletConfig()
88 {
89 return _servletConfig;
90 }
91
92 public String getServletInfo()
93 {
94 return SERVLET_INFO;
95 }
96
97 private String getLifecycleId()
98 {
99
100
101
102 String serLifecycleId = _servletConfig.getInitParameter(LIFECYCLE_ID_ATTR);
103 String appLifecycleId = _servletConfig.getServletContext().getInitParameter(LIFECYCLE_ID_ATTR);
104 appLifecycleId = serLifecycleId == null ? appLifecycleId : serLifecycleId;
105 return appLifecycleId != null ? appLifecycleId : LifecycleFactory.DEFAULT_LIFECYCLE;
106 }
107
108 public void init(ServletConfig servletConfig) throws ServletException
109 {
110 if (log.isLoggable(Level.FINEST))
111 {
112 log.finest("init begin");
113 }
114 _servletConfig = servletConfig;
115 _facesContextFactory = (FacesContextFactory)FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
116
117
118
119
120
121 LifecycleFactory lifecycleFactory = (LifecycleFactory)FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
122 _lifecycle = lifecycleFactory.getLifecycle(getLifecycleId());
123 if (log.isLoggable(Level.FINEST))
124 {
125 log.finest("init end");
126 }
127 }
128
129 public void service(ServletRequest request, ServletResponse response) throws IOException, ServletException
130 {
131
132
133
134 HttpServletRequest httpRequest = (HttpServletRequest)request;
135 String pathInfo = httpRequest.getPathInfo();
136
137
138
139
140
141
142
143
144
145
146
147
148
149 if (pathInfo != null && (pathInfo.startsWith("/WEB-INF") || pathInfo.startsWith("/META-INF")))
150 {
151 StringBuffer buffer = new StringBuffer();
152
153 buffer.append(" Someone is trying to access a secure resource : ").append(pathInfo);
154 buffer.append("\n remote address is ").append(httpRequest.getRemoteAddr());
155 buffer.append("\n remote host is ").append(httpRequest.getRemoteHost());
156 buffer.append("\n remote user is ").append(httpRequest.getRemoteUser());
157 buffer.append("\n request URI is ").append(httpRequest.getRequestURI());
158
159 log.warning(buffer.toString());
160
161
162
163 ((HttpServletResponse)response).sendError(HttpServletResponse.SC_NOT_FOUND);
164 return;
165 }
166
167
168
169 if (log.isLoggable(Level.FINEST))
170 {
171 log.finest("service begin");
172 }
173
174
175 FacesContext facesContext = prepareFacesContext(request, response);
176
177 try
178 {
179
180
181
182
183
184 ResourceHandler resourceHandler = facesContext.getApplication().getResourceHandler();
185
186
187 if (resourceHandler.isResourceRequest(facesContext))
188 {
189
190 resourceHandler.handleResourceRequest(facesContext);
191 }
192 else
193 {
194
195 _lifecycle.attachWindow(facesContext);
196
197
198 _lifecycle.execute(facesContext);
199
200 _lifecycle.render(facesContext);
201 }
202 }
203 catch (FacesException e)
204 {
205
206
207
208 Throwable cause = e.getCause();
209 if (cause == null)
210 {
211
212
213
214 throw new ServletException(e.getLocalizedMessage(), e);
215 }
216 else if (cause instanceof ServletException)
217 {
218
219 throw (ServletException)cause;
220 }
221 else if (cause instanceof IOException)
222 {
223
224 throw (IOException)cause;
225 }
226 else
227 {
228
229
230 throw new ServletException(cause.getLocalizedMessage(), cause);
231 }
232 }
233 finally
234 {
235
236 facesContext.release();
237 }
238 if (log.isLoggable(Level.FINEST))
239 {
240 log.finest("service end");
241 }
242 }
243
244 private FacesContext prepareFacesContext(ServletRequest request, ServletResponse response)
245 {
246 FacesContext facesContext =
247 _facesContextFactory.getFacesContext(_servletConfig.getServletContext(), request, response, _lifecycle);
248 return facesContext;
249 }
250 }