1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
package org.apache.tapestry.services.impl; |
15 | |
|
16 | |
import org.apache.hivemind.Resource; |
17 | |
import org.apache.hivemind.util.Defense; |
18 | |
import org.apache.tapestry.*; |
19 | |
import org.apache.tapestry.asset.AssetFactory; |
20 | |
import org.apache.tapestry.engine.NullWriter; |
21 | |
import org.apache.tapestry.markup.MarkupWriterSource; |
22 | |
import org.apache.tapestry.services.RequestLocaleManager; |
23 | |
import org.apache.tapestry.services.ResponseBuilder; |
24 | |
import org.apache.tapestry.util.ContentType; |
25 | |
import org.apache.tapestry.util.PageRenderSupportImpl; |
26 | |
import org.apache.tapestry.web.WebResponse; |
27 | |
|
28 | |
import java.io.IOException; |
29 | |
import java.io.PrintWriter; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
public class DefaultResponseBuilder implements ResponseBuilder |
38 | |
{ |
39 | |
private final AssetFactory _assetFactory; |
40 | |
|
41 | |
private final String _namespace; |
42 | |
|
43 | |
private PageRenderSupportImpl _prs; |
44 | |
|
45 | |
private RequestLocaleManager _localeManager; |
46 | |
|
47 | |
private MarkupWriterSource _markupWriterSource; |
48 | |
|
49 | |
private WebResponse _webResponse; |
50 | |
|
51 | |
|
52 | |
private IMarkupWriter _writer; |
53 | |
|
54 | 0 | private boolean _closeWriter = true; |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
public DefaultResponseBuilder(IRequestCycle cycle, IMarkupWriter writer, |
62 | |
AssetFactory assetFactory, String namespace, boolean closeWriter) |
63 | 0 | { |
64 | 0 | _writer = writer; |
65 | 0 | _assetFactory = assetFactory; |
66 | 0 | _namespace = namespace; |
67 | 0 | _closeWriter = closeWriter; |
68 | |
|
69 | 0 | _prs = new PageRenderSupportImpl(_assetFactory, _namespace, this, cycle); |
70 | 0 | } |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
public DefaultResponseBuilder(IMarkupWriter writer) |
77 | 0 | { |
78 | 0 | _writer = writer; |
79 | 0 | _assetFactory = null; |
80 | 0 | _namespace = null; |
81 | 0 | } |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
public DefaultResponseBuilder(IRequestCycle cycle, RequestLocaleManager localeManager, |
101 | |
MarkupWriterSource markupWriterSource, WebResponse webResponse, |
102 | |
AssetFactory assetFactory, String namespace) |
103 | 0 | { |
104 | 0 | Defense.notNull(assetFactory, "assetService"); |
105 | |
|
106 | 0 | _localeManager = localeManager; |
107 | 0 | _markupWriterSource = markupWriterSource; |
108 | 0 | _webResponse = webResponse; |
109 | |
|
110 | |
|
111 | |
|
112 | 0 | _assetFactory = assetFactory; |
113 | 0 | _namespace = namespace; |
114 | |
|
115 | 0 | _prs = new PageRenderSupportImpl(_assetFactory, _namespace, this, cycle); |
116 | 0 | } |
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
public boolean isDynamic() |
123 | |
{ |
124 | 0 | return false; |
125 | |
} |
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
public void renderResponse(IRequestCycle cycle) |
132 | |
throws IOException |
133 | |
{ |
134 | 0 | if (_writer == null) |
135 | |
{ |
136 | 0 | _localeManager.persistLocale(); |
137 | |
|
138 | 0 | IPage page = cycle.getPage(); |
139 | |
|
140 | 0 | ContentType contentType = page.getResponseContentType(); |
141 | 0 | String encoding = contentType.getParameter(ENCODING_KEY); |
142 | |
|
143 | 0 | if (encoding == null) |
144 | |
{ |
145 | 0 | encoding = cycle.getEngine().getOutputEncoding(); |
146 | |
|
147 | 0 | contentType.setParameter(ENCODING_KEY, encoding); |
148 | |
} |
149 | |
|
150 | 0 | PrintWriter printWriter = _webResponse.getPrintWriter(contentType); |
151 | |
|
152 | 0 | _writer = _markupWriterSource.newMarkupWriter(printWriter, contentType); |
153 | |
} |
154 | |
|
155 | |
|
156 | |
|
157 | 0 | TapestryUtils.storePageRenderSupport(cycle, _prs); |
158 | |
|
159 | 0 | cycle.renderPage(this); |
160 | |
|
161 | 0 | TapestryUtils.removePageRenderSupport(cycle); |
162 | |
|
163 | 0 | flush(); |
164 | |
|
165 | 0 | if (_closeWriter) |
166 | 0 | _writer.close(); |
167 | 0 | } |
168 | |
|
169 | |
public void flush() |
170 | |
throws IOException |
171 | |
{ |
172 | |
|
173 | |
|
174 | |
|
175 | 0 | _writer.flush(); |
176 | 0 | } |
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
public void render(IMarkupWriter writer, IRender render, IRequestCycle cycle) |
183 | |
{ |
184 | 0 | if (writer == null) |
185 | 0 | render.render(_writer, cycle); |
186 | |
else |
187 | 0 | render.render(writer, cycle); |
188 | 0 | } |
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
public void updateComponent(String id) |
194 | |
{ |
195 | 0 | } |
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
public boolean contains(IComponent target) |
201 | |
{ |
202 | 0 | return false; |
203 | |
} |
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
public boolean explicitlyContains(IComponent target) |
209 | |
{ |
210 | 0 | return false; |
211 | |
} |
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
public IMarkupWriter getWriter() |
217 | |
{ |
218 | 0 | if (_writer == null) |
219 | 0 | return NullWriter.getSharedInstance(); |
220 | |
|
221 | 0 | return _writer; |
222 | |
} |
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
public IMarkupWriter getWriter(String id, String type) |
228 | |
{ |
229 | 0 | if (_writer == null) |
230 | 0 | return NullWriter.getSharedInstance(); |
231 | |
|
232 | 0 | return _writer; |
233 | |
} |
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
public boolean isBodyScriptAllowed(IComponent target) |
239 | |
{ |
240 | 0 | return true; |
241 | |
} |
242 | |
|
243 | |
|
244 | |
|
245 | |
|
246 | |
public boolean isExternalScriptAllowed(IComponent target) |
247 | |
{ |
248 | 0 | return true; |
249 | |
} |
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
public boolean isInitializationScriptAllowed(IComponent target) |
255 | |
{ |
256 | 0 | return true; |
257 | |
} |
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
public boolean isImageInitializationAllowed(IComponent target) |
263 | |
{ |
264 | 0 | return true; |
265 | |
} |
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
public String getPreloadedImageReference(IComponent target, IAsset source) |
271 | |
{ |
272 | 0 | return _prs.getPreloadedImageReference(target, source); |
273 | |
} |
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
public String getPreloadedImageReference(IComponent target, String url) |
279 | |
{ |
280 | 0 | return _prs.getPreloadedImageReference(target, url); |
281 | |
} |
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
public String getPreloadedImageReference(String url) |
287 | |
{ |
288 | 0 | return _prs.getPreloadedImageReference(url); |
289 | |
} |
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
public void addBodyScript(IComponent target, String script) |
295 | |
{ |
296 | 0 | _prs.addBodyScript(target, script); |
297 | 0 | } |
298 | |
|
299 | |
|
300 | |
|
301 | |
|
302 | |
public void addBodyScript(String script) |
303 | |
{ |
304 | 0 | _prs.addBodyScript(script); |
305 | 0 | } |
306 | |
|
307 | |
|
308 | |
|
309 | |
|
310 | |
public void addExternalScript(IComponent target, Resource resource) |
311 | |
{ |
312 | 0 | _prs.addExternalScript(target, resource); |
313 | 0 | } |
314 | |
|
315 | |
|
316 | |
|
317 | |
|
318 | |
public void addExternalScript(Resource resource) |
319 | |
{ |
320 | 0 | _prs.addExternalScript(resource); |
321 | 0 | } |
322 | |
|
323 | |
|
324 | |
|
325 | |
|
326 | |
public void addInitializationScript(IComponent target, String script) |
327 | |
{ |
328 | 0 | _prs.addInitializationScript(target, script); |
329 | 0 | } |
330 | |
|
331 | |
|
332 | |
|
333 | |
|
334 | |
public void addInitializationScript(String script) |
335 | |
{ |
336 | 0 | _prs.addInitializationScript(script); |
337 | 0 | } |
338 | |
|
339 | |
public void addScriptAfterInitialization(IComponent target, String script) |
340 | |
{ |
341 | 0 | _prs.addScriptAfterInitialization(target, script); |
342 | 0 | } |
343 | |
|
344 | |
|
345 | |
|
346 | |
|
347 | |
public String getUniqueString(String baseValue) |
348 | |
{ |
349 | 0 | return _prs.getUniqueString(baseValue); |
350 | |
} |
351 | |
|
352 | |
|
353 | |
|
354 | |
|
355 | |
public void writeBodyScript(IMarkupWriter writer, IRequestCycle cycle) |
356 | |
{ |
357 | 0 | _prs.writeBodyScript(writer, cycle); |
358 | 0 | } |
359 | |
|
360 | |
|
361 | |
|
362 | |
|
363 | |
public void writeInitializationScript(IMarkupWriter writer) |
364 | |
{ |
365 | 0 | _prs.writeInitializationScript(writer); |
366 | 0 | } |
367 | |
|
368 | |
|
369 | |
|
370 | |
|
371 | |
public void beginBodyScript(IMarkupWriter writer, IRequestCycle cycle) |
372 | |
{ |
373 | 0 | writer.begin("script"); |
374 | 0 | writer.attribute("type", "text/javascript"); |
375 | 0 | writer.printRaw("<!--"); |
376 | 0 | writer.println(); |
377 | 0 | } |
378 | |
|
379 | |
|
380 | |
|
381 | |
|
382 | |
public void endBodyScript(IMarkupWriter writer, IRequestCycle cycle) |
383 | |
{ |
384 | 0 | writer.println(); |
385 | 0 | writer.printRaw("// -->"); |
386 | 0 | writer.end(); |
387 | 0 | } |
388 | |
|
389 | |
|
390 | |
|
391 | |
|
392 | |
public void writeBodyScript(IMarkupWriter writer, String script, IRequestCycle cycle) |
393 | |
{ |
394 | 0 | writer.printRaw(script); |
395 | 0 | } |
396 | |
|
397 | |
|
398 | |
|
399 | |
|
400 | |
public void writeExternalScript(IMarkupWriter writer, String url, IRequestCycle cycle) |
401 | |
{ |
402 | 0 | writer.begin("script"); |
403 | 0 | writer.attribute("type", "text/javascript"); |
404 | 0 | writer.attribute("src", url); |
405 | 0 | writer.end(); |
406 | 0 | writer.println(); |
407 | 0 | } |
408 | |
|
409 | |
|
410 | |
|
411 | |
|
412 | |
public void writeImageInitializations(IMarkupWriter writer, String script, String preloadName, IRequestCycle cycle) |
413 | |
{ |
414 | |
|
415 | 0 | writer.println(); |
416 | 0 | writer.printRaw("tapestry.addOnLoad(function(e) {\n"); |
417 | |
|
418 | 0 | writer.printRaw(preloadName + " = [];\n"); |
419 | 0 | writer.printRaw("if (document.images)\n"); |
420 | 0 | writer.printRaw("{\n"); |
421 | 0 | writer.printRaw(script); |
422 | 0 | writer.printRaw("}\n"); |
423 | |
|
424 | 0 | writer.printRaw("});"); |
425 | 0 | } |
426 | |
|
427 | |
|
428 | |
|
429 | |
|
430 | |
public void writeInitializationScript(IMarkupWriter writer, String script) |
431 | |
{ |
432 | 0 | writer.begin("script"); |
433 | 0 | writer.attribute("type", "text/javascript"); |
434 | 0 | writer.printRaw("<!--\n"); |
435 | |
|
436 | 0 | writer.printRaw("tapestry.addOnLoad(function(e) {\n"); |
437 | |
|
438 | 0 | writer.printRaw(script); |
439 | |
|
440 | 0 | writer.printRaw("});"); |
441 | |
|
442 | 0 | writer.printRaw("\n// -->"); |
443 | 0 | writer.end(); |
444 | 0 | } |
445 | |
|
446 | |
|
447 | |
|
448 | |
|
449 | |
|
450 | |
public void addStatusMessage(IMarkupWriter normalWriter, String category, String text) |
451 | |
{ |
452 | 0 | } |
453 | |
} |