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