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