1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package javax.faces.webapp; |
17 | |
|
18 | |
import java.io.IOException; |
19 | |
import java.util.Map; |
20 | |
|
21 | |
import javax.faces.FacesException; |
22 | |
import javax.faces.FactoryFinder; |
23 | |
import javax.faces.context.ExternalContext; |
24 | |
import javax.faces.context.FacesContext; |
25 | |
import javax.faces.context.FacesContextFactory; |
26 | |
import javax.faces.event.PhaseId; |
27 | |
import javax.faces.internal.PhaseUtil; |
28 | |
import javax.faces.internal.WebAppUtil; |
29 | |
import javax.faces.internal.WindowIdUtil; |
30 | |
import javax.faces.internal.scope.PageScope; |
31 | |
import javax.faces.lifecycle.Lifecycle; |
32 | |
import javax.faces.lifecycle.LifecycleFactory; |
33 | |
import javax.servlet.Servlet; |
34 | |
import javax.servlet.ServletConfig; |
35 | |
import javax.servlet.ServletContext; |
36 | |
import javax.servlet.ServletException; |
37 | |
import javax.servlet.ServletRequest; |
38 | |
import javax.servlet.ServletResponse; |
39 | |
import javax.servlet.http.HttpServletResponse; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | 0 | public final class FacesServlet implements Servlet { |
45 | |
|
46 | |
public static final String CONFIG_FILES_ATTR = "javax.faces.CONFIG_FILES"; |
47 | |
|
48 | |
public static final String LIFECYCLE_ID_ATTR = "javax.faces.LIFECYCLE_ID"; |
49 | |
|
50 | 0 | private ServletConfig config = null; |
51 | |
|
52 | 0 | private FacesContextFactory facesContextFactory = null; |
53 | |
|
54 | 0 | private Lifecycle lifecycle = null; |
55 | |
|
56 | |
public void destroy() { |
57 | 0 | config = null; |
58 | 0 | facesContextFactory = null; |
59 | 0 | lifecycle = null; |
60 | 0 | } |
61 | |
|
62 | |
public ServletConfig getServletConfig() { |
63 | 0 | return config; |
64 | |
} |
65 | |
|
66 | |
public String getServletInfo() { |
67 | 0 | return "Teeda - A JSF implementation with DI x AOP by Seasar Foundation -"; |
68 | |
} |
69 | |
|
70 | |
public void init(ServletConfig config) throws ServletException { |
71 | 0 | this.config = config; |
72 | 0 | facesContextFactory = (FacesContextFactory) WebAppUtil |
73 | |
.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY); |
74 | 0 | LifecycleFactory lifecycleFactory = (LifecycleFactory) WebAppUtil |
75 | |
.getFactory(FactoryFinder.LIFECYCLE_FACTORY); |
76 | 0 | String lifecycleId = WebAppUtil.getLifecycleId(config); |
77 | 0 | lifecycle = lifecycleFactory.getLifecycle(lifecycleId); |
78 | 0 | } |
79 | |
|
80 | |
public void service(ServletRequest request, ServletResponse response) |
81 | |
throws ServletException, IOException { |
82 | 0 | PhaseUtil.setCurrentPhase(PhaseId.ANY_PHASE); |
83 | 0 | final ServletContext servletContext = config.getServletContext(); |
84 | 0 | final FacesContext context = facesContextFactory.getFacesContext( |
85 | |
servletContext, request, response, lifecycle); |
86 | |
|
87 | 0 | final ExternalContext externalContext = context.getExternalContext(); |
88 | 0 | final String pathInfo = externalContext.getRequestPathInfo(); |
89 | 0 | if (pathInfo != null && |
90 | |
(pathInfo.startsWith("/WEB-INF") || pathInfo |
91 | |
.startsWith("/META-INF"))) { |
92 | 0 | final HttpServletResponse hres = (HttpServletResponse) response; |
93 | 0 | hres.sendError(HttpServletResponse.SC_NOT_FOUND); |
94 | 0 | return; |
95 | |
} |
96 | |
try { |
97 | 0 | WindowIdUtil.setupWindowId(externalContext); |
98 | 0 | final Map pageScope = PageScope.getOrCreateContext(context); |
99 | 0 | synchronized (pageScope) { |
100 | 0 | lifecycle.execute(context); |
101 | 0 | lifecycle.render(context); |
102 | 0 | } |
103 | 0 | } catch (FacesException e) { |
104 | 0 | Throwable t = e.getCause(); |
105 | 0 | if (t == null) { |
106 | 0 | throw new ServletException(e.getMessage(), e); |
107 | |
} else { |
108 | 0 | if (t instanceof ServletException) { |
109 | 0 | throw ((ServletException) t); |
110 | 0 | } else if (t instanceof IOException) { |
111 | 0 | throw ((IOException) t); |
112 | |
} else { |
113 | 0 | throw new ServletException(t.getMessage(), t); |
114 | |
} |
115 | |
} |
116 | |
} finally { |
117 | 0 | context.release(); |
118 | 0 | PhaseUtil.clearPhase(); |
119 | 0 | } |
120 | 0 | } |
121 | |
|
122 | |
} |