1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.mc.test.core.mock; |
20 | |
|
21 | |
import java.beans.BeanInfo; |
22 | |
import java.io.IOException; |
23 | |
|
24 | |
import javax.faces.FacesException; |
25 | |
import javax.faces.FactoryFinder; |
26 | |
import javax.faces.application.Application; |
27 | |
import javax.faces.application.Resource; |
28 | |
import javax.faces.application.ViewHandler; |
29 | |
import javax.faces.component.UIViewRoot; |
30 | |
import javax.faces.context.ExternalContext; |
31 | |
import javax.faces.context.FacesContext; |
32 | |
import javax.faces.context.ResponseWriter; |
33 | |
import javax.faces.render.RenderKit; |
34 | |
import javax.faces.render.RenderKitFactory; |
35 | |
import javax.faces.view.StateManagementStrategy; |
36 | |
import javax.faces.view.ViewDeclarationLanguage; |
37 | |
import javax.faces.view.ViewMetadata; |
38 | |
import javax.servlet.http.HttpServletResponse; |
39 | |
|
40 | |
import org.apache.myfaces.shared.application.InvalidViewIdException; |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | 0 | public class MockDefaultViewDeclarationLanguage extends ViewDeclarationLanguage |
55 | |
{ |
56 | |
public static final String DUMMY_VIEW_CREATE_HOOK = "oam.dummy.create.UIViewRoot"; |
57 | |
public static final String DUMMY_VIEW_RESTORE_HOOK = "oam.dummy.restore.UIViewRoot"; |
58 | |
|
59 | |
@Override |
60 | |
public void buildView(FacesContext context, UIViewRoot view) |
61 | |
throws IOException |
62 | |
{ |
63 | 0 | } |
64 | |
|
65 | |
@Override |
66 | |
public UIViewRoot createView(FacesContext context, String viewId) |
67 | |
{ |
68 | 0 | if (context.getAttributes().containsKey(DUMMY_VIEW_CREATE_HOOK)) |
69 | |
{ |
70 | 0 | UIViewRoot root = (UIViewRoot) context.getAttributes().remove(DUMMY_VIEW_CREATE_HOOK); |
71 | 0 | Application application = context.getApplication(); |
72 | 0 | ViewHandler handler = application.getViewHandler(); |
73 | 0 | root.setLocale(handler.calculateLocale(context)); |
74 | 0 | root.setRenderKitId(handler.calculateRenderKitId(context)); |
75 | 0 | root.setViewId(viewId); |
76 | 0 | return root; |
77 | |
} |
78 | |
else |
79 | |
{ |
80 | |
try |
81 | |
{ |
82 | 0 | viewId = calculateViewId(context, viewId); |
83 | |
|
84 | 0 | Application application = context.getApplication(); |
85 | |
|
86 | 0 | UIViewRoot newViewRoot = (UIViewRoot) application.createComponent(UIViewRoot.COMPONENT_TYPE); |
87 | 0 | UIViewRoot oldViewRoot = context.getViewRoot(); |
88 | 0 | if (oldViewRoot == null) |
89 | |
{ |
90 | |
|
91 | |
|
92 | |
|
93 | 0 | ViewHandler handler = application.getViewHandler(); |
94 | 0 | newViewRoot.setLocale(handler.calculateLocale(context)); |
95 | 0 | newViewRoot.setRenderKitId(handler.calculateRenderKitId(context)); |
96 | 0 | } |
97 | |
else |
98 | |
{ |
99 | |
|
100 | |
|
101 | |
|
102 | 0 | newViewRoot.setLocale(oldViewRoot.getLocale()); |
103 | 0 | newViewRoot.setRenderKitId(oldViewRoot.getRenderKitId()); |
104 | |
} |
105 | |
|
106 | |
|
107 | 0 | newViewRoot.setViewId(viewId); |
108 | |
|
109 | 0 | return newViewRoot; |
110 | |
} |
111 | 0 | catch (InvalidViewIdException e) |
112 | |
{ |
113 | |
|
114 | |
|
115 | 0 | sendSourceNotFound(context, e.getMessage()); |
116 | |
|
117 | |
|
118 | 0 | return null; |
119 | |
} |
120 | |
} |
121 | |
} |
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
protected String calculateViewId(FacesContext context, String viewId) |
132 | |
{ |
133 | 0 | if (viewId != null) |
134 | |
{ |
135 | 0 | throw new InvalidViewIdException(); |
136 | |
} |
137 | 0 | return null; |
138 | |
} |
139 | |
|
140 | |
protected void sendSourceNotFound(FacesContext context, String message) |
141 | |
{ |
142 | 0 | HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse(); |
143 | |
try |
144 | |
{ |
145 | 0 | context.responseComplete(); |
146 | 0 | response.sendError(HttpServletResponse.SC_NOT_FOUND, message); |
147 | |
} |
148 | 0 | catch (IOException ioe) |
149 | |
{ |
150 | 0 | throw new FacesException(ioe); |
151 | 0 | } |
152 | 0 | } |
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
public static void hookCreateView(FacesContext context, UIViewRoot root) |
162 | |
{ |
163 | 0 | context.getAttributes().put(DUMMY_VIEW_CREATE_HOOK, root); |
164 | 0 | } |
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
public static void hookRestoreView(FacesContext context, UIViewRoot root) |
174 | |
{ |
175 | 0 | context.getAttributes().put(DUMMY_VIEW_RESTORE_HOOK, root); |
176 | 0 | } |
177 | |
|
178 | |
@Override |
179 | |
public BeanInfo getComponentMetadata(FacesContext context, |
180 | |
Resource componentResource) |
181 | |
{ |
182 | 0 | return null; |
183 | |
} |
184 | |
|
185 | |
@Override |
186 | |
public Resource getScriptComponentResource(FacesContext context, |
187 | |
Resource componentResource) |
188 | |
{ |
189 | 0 | return null; |
190 | |
} |
191 | |
|
192 | |
@Override |
193 | |
public StateManagementStrategy getStateManagementStrategy( |
194 | |
FacesContext context, String viewId) |
195 | |
{ |
196 | 0 | return null; |
197 | |
} |
198 | |
|
199 | |
@Override |
200 | |
public ViewMetadata getViewMetadata(FacesContext context, String viewId) |
201 | |
{ |
202 | 0 | return null; |
203 | |
} |
204 | |
|
205 | |
@Override |
206 | |
public void renderView(FacesContext context, UIViewRoot view) |
207 | |
throws IOException |
208 | |
{ |
209 | 0 | checkNull(context, "context"); |
210 | 0 | checkNull(view, "view"); |
211 | |
|
212 | 0 | ExternalContext externalContext = context.getExternalContext(); |
213 | 0 | ResponseWriter responseWriter = context.getResponseWriter(); |
214 | 0 | if (responseWriter == null) |
215 | |
{ |
216 | 0 | RenderKitFactory renderFactory = (RenderKitFactory) |
217 | |
FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY); |
218 | 0 | RenderKit renderKit = renderFactory.getRenderKit(context, view.getRenderKitId()); |
219 | |
|
220 | 0 | responseWriter = renderKit.createResponseWriter(externalContext.getResponseOutputWriter(), |
221 | |
null, externalContext.getRequestCharacterEncoding()); |
222 | 0 | context.setResponseWriter(responseWriter); |
223 | |
} |
224 | |
|
225 | |
|
226 | |
|
227 | 0 | responseWriter.startDocument(); |
228 | |
|
229 | 0 | view.encodeAll(context); |
230 | |
|
231 | |
|
232 | 0 | responseWriter.endDocument(); |
233 | |
|
234 | 0 | responseWriter.flush(); |
235 | 0 | } |
236 | |
|
237 | |
@Override |
238 | |
public UIViewRoot restoreView(FacesContext context, String viewId) |
239 | |
{ |
240 | 0 | checkNull(context, "context"); |
241 | |
|
242 | |
|
243 | 0 | if (context.getAttributes().containsKey(DUMMY_VIEW_RESTORE_HOOK)) |
244 | |
{ |
245 | 0 | UIViewRoot root = (UIViewRoot) context.getAttributes().remove(DUMMY_VIEW_RESTORE_HOOK); |
246 | 0 | root.setViewId(viewId); |
247 | 0 | return root; |
248 | |
} |
249 | |
else |
250 | |
{ |
251 | 0 | Application application = context.getApplication(); |
252 | |
|
253 | 0 | ViewHandler applicationViewHandler = application.getViewHandler(); |
254 | |
|
255 | 0 | String renderKitId = applicationViewHandler.calculateRenderKitId(context); |
256 | |
|
257 | 0 | UIViewRoot viewRoot = application.getStateManager().restoreView(context, viewId, renderKitId); |
258 | |
|
259 | 0 | return viewRoot; |
260 | |
} |
261 | |
} |
262 | |
|
263 | |
protected void checkNull(final Object o, final String param) |
264 | |
{ |
265 | 0 | if (o == null) |
266 | |
{ |
267 | 0 | throw new NullPointerException(param + " can not be null."); |
268 | |
} |
269 | 0 | } |
270 | |
} |