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