1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.lifecycle; |
20 | |
|
21 | |
import javax.faces.FacesException; |
22 | |
import javax.faces.application.Application; |
23 | |
import javax.faces.application.ViewExpiredException; |
24 | |
import javax.faces.application.ViewHandler; |
25 | |
import javax.faces.component.UIViewRoot; |
26 | |
import javax.faces.context.ExternalContext; |
27 | |
import javax.faces.context.FacesContext; |
28 | |
import javax.faces.event.PhaseId; |
29 | |
import javax.portlet.PortletRequest; |
30 | |
|
31 | |
import org.apache.commons.logging.Log; |
32 | |
import org.apache.commons.logging.LogFactory; |
33 | |
import org.apache.myfaces.portlet.MyFacesGenericPortlet; |
34 | |
import org.apache.myfaces.portlet.PortletUtil; |
35 | |
import org.apache.myfaces.util.DebugUtils; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | 0 | class RestoreViewExecutor implements PhaseExecutor |
46 | |
{ |
47 | |
|
48 | 0 | private static final Log log = LogFactory.getLog(RestoreViewExecutor.class); |
49 | |
private RestoreViewSupport _restoreViewSupport; |
50 | |
|
51 | |
public boolean execute(FacesContext facesContext) |
52 | |
{ |
53 | 0 | if (facesContext == null) |
54 | |
{ |
55 | 0 | throw new FacesException("FacesContext is null"); |
56 | |
} |
57 | |
|
58 | |
|
59 | 0 | Application application = facesContext.getApplication(); |
60 | 0 | ViewHandler viewHandler = application.getViewHandler(); |
61 | 0 | viewHandler.initView(facesContext); |
62 | |
|
63 | 0 | UIViewRoot viewRoot = facesContext.getViewRoot(); |
64 | |
|
65 | 0 | RestoreViewSupport restoreViewSupport = getRestoreViewSupport(); |
66 | |
|
67 | 0 | if (viewRoot != null) |
68 | |
{ |
69 | 0 | if (log.isTraceEnabled()) |
70 | 0 | log.trace("View already exists in the FacesContext"); |
71 | |
|
72 | 0 | viewRoot.setLocale(facesContext.getExternalContext().getRequestLocale()); |
73 | 0 | restoreViewSupport.processComponentBinding(facesContext, viewRoot); |
74 | 0 | return false; |
75 | |
} |
76 | |
|
77 | 0 | String viewId = restoreViewSupport.calculateViewId(facesContext); |
78 | |
|
79 | |
|
80 | 0 | if (restoreViewSupport.isPostback(facesContext)) |
81 | |
{ |
82 | 0 | if (log.isTraceEnabled()) |
83 | 0 | log.trace("Request is a postback"); |
84 | |
|
85 | 0 | viewRoot = viewHandler.restoreView(facesContext, viewId); |
86 | 0 | if (viewRoot == null) |
87 | |
{ |
88 | 0 | throw new ViewExpiredException( |
89 | |
"No saved view state could be found for the view identifier: " |
90 | |
+ viewId, viewId); |
91 | |
} |
92 | 0 | restoreViewSupport.processComponentBinding(facesContext, viewRoot); |
93 | |
} |
94 | |
else |
95 | |
{ |
96 | 0 | if (log.isTraceEnabled()) |
97 | 0 | log.trace("Request is not a postback. New UIViewRoot will be created"); |
98 | |
|
99 | 0 | viewRoot = viewHandler.createView(facesContext, viewId); |
100 | 0 | facesContext.renderResponse(); |
101 | |
} |
102 | |
|
103 | 0 | facesContext.setViewRoot(viewRoot); |
104 | |
|
105 | 0 | return false; |
106 | |
} |
107 | |
|
108 | |
protected RestoreViewSupport getRestoreViewSupport() |
109 | |
{ |
110 | 0 | if (_restoreViewSupport == null) |
111 | |
{ |
112 | 0 | _restoreViewSupport = new DefaultRestoreViewSupport(); |
113 | |
} |
114 | 0 | return _restoreViewSupport; |
115 | |
} |
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
public void setRestoreViewSupport(RestoreViewSupport restoreViewSupport) |
122 | |
{ |
123 | 0 | _restoreViewSupport = restoreViewSupport; |
124 | 0 | } |
125 | |
|
126 | |
public PhaseId getPhase() |
127 | |
{ |
128 | 0 | return PhaseId.RESTORE_VIEW; |
129 | |
} |
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
private static String deriveViewId(FacesContext facesContext) |
135 | |
{ |
136 | 0 | ExternalContext externalContext = facesContext.getExternalContext(); |
137 | |
|
138 | 0 | if (PortletUtil.isPortletRequest(facesContext)) |
139 | |
{ |
140 | 0 | PortletRequest request = (PortletRequest) externalContext.getRequest(); |
141 | 0 | return request.getParameter(MyFacesGenericPortlet.VIEW_ID); |
142 | |
} |
143 | |
|
144 | 0 | String viewId = externalContext.getRequestPathInfo(); |
145 | 0 | if (viewId == null) |
146 | |
{ |
147 | |
|
148 | 0 | viewId = externalContext.getRequestServletPath(); |
149 | 0 | DebugUtils.assertError(viewId != null, log, |
150 | |
"RequestServletPath is null, cannot determine viewId of current page."); |
151 | 0 | if (viewId == null) |
152 | 0 | return null; |
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | 0 | String defaultSuffix = externalContext.getInitParameter(ViewHandler.DEFAULT_SUFFIX_PARAM_NAME); |
159 | 0 | String suffix = defaultSuffix != null ? defaultSuffix : ViewHandler.DEFAULT_SUFFIX; |
160 | 0 | DebugUtils.assertError(suffix.charAt(0) == '.', log, "Default suffix must start with a dot!"); |
161 | |
|
162 | 0 | int slashPos = viewId.lastIndexOf('/'); |
163 | 0 | int extensionPos = viewId.lastIndexOf('.'); |
164 | 0 | if (extensionPos == -1 || extensionPos <= slashPos) |
165 | |
{ |
166 | 0 | log.error("Assumed extension mapping, but there is no extension in " + viewId); |
167 | 0 | viewId = null; |
168 | |
} |
169 | |
else |
170 | |
{ |
171 | 0 | viewId = viewId.substring(0, extensionPos) + suffix; |
172 | |
} |
173 | |
} |
174 | |
|
175 | 0 | return viewId; |
176 | |
} |
177 | |
} |