01: // Copyright 2006, 2007 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // Unless required by applicable law or agreed to in writing, software
10: // distributed under the License is distributed on an "AS IS" BASIS,
11: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: // See the License for the specific language governing permissions and
13: // limitations under the License.
14:
15: package org.apache.tapestry.internal.services;
16:
17: import java.io.IOException;
18: import java.io.PrintWriter;
19:
20: import org.apache.tapestry.ComponentResources;
21: import org.apache.tapestry.MarkupWriter;
22: import org.apache.tapestry.TapestryConstants;
23: import org.apache.tapestry.internal.structure.Page;
24: import org.apache.tapestry.internal.util.ContentType;
25: import org.apache.tapestry.services.MarkupWriterFactory;
26: import org.apache.tapestry.services.MetaDataLocator;
27: import org.apache.tapestry.services.Response;
28:
29: public class PageResponseRendererImpl implements PageResponseRenderer {
30: public static final String CHARSET = "charset";
31:
32: private final PageMarkupRenderer _markupRenderer;
33:
34: private final MarkupWriterFactory _markupWriterFactory;
35:
36: private final MetaDataLocator _metaDataLocator;
37:
38: public PageResponseRendererImpl(
39: MarkupWriterFactory markupWriterFactory,
40: PageMarkupRenderer markupRenderer,
41: MetaDataLocator metaDataLocator) {
42: _markupWriterFactory = markupWriterFactory;
43: _markupRenderer = markupRenderer;
44: _metaDataLocator = metaDataLocator;
45: }
46:
47: public void renderPageResponse(Page page, Response response)
48: throws IOException {
49: ComponentResources pageResources = page.getRootComponent()
50: .getComponentResources();
51:
52: String contentTypeString = _metaDataLocator.findMeta(
53: TapestryConstants.RESPONSE_CONTENT_TYPE, pageResources);
54: ContentType contentType = new ContentType(contentTypeString);
55:
56: // Make sure thre's always a charset specified.
57:
58: String encoding = contentType.getParameter(CHARSET);
59: if (encoding == null) {
60: encoding = _metaDataLocator.findMeta(
61: TapestryConstants.RESPONSE_ENCODING, pageResources);
62: contentType.setParameter(CHARSET, encoding);
63: }
64:
65: // Eventually we'll have to do work to figure out the correct markup type, content type,
66: // whatever. Right now its defaulting to plain HTML.
67:
68: MarkupWriter writer = _markupWriterFactory.newMarkupWriter();
69:
70: _markupRenderer.renderPageMarkup(page, writer);
71:
72: PrintWriter pw = response
73: .getPrintWriter(contentType.toString());
74:
75: writer.toMarkup(pw);
76:
77: pw.flush();
78: }
79:
80: }
|