KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > services > impl > ResponseRendererImpl


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// 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.services.impl;
16
17 import java.io.IOException JavaDoc;
18 import java.io.PrintWriter JavaDoc;
19
20 import org.apache.tapestry.IMarkupWriter;
21 import org.apache.tapestry.IPage;
22 import org.apache.tapestry.IRequestCycle;
23 import org.apache.tapestry.markup.MarkupWriterSource;
24 import org.apache.tapestry.services.RequestLocaleManager;
25 import org.apache.tapestry.services.ResponseRenderer;
26 import org.apache.tapestry.util.ContentType;
27 import org.apache.tapestry.web.WebResponse;
28
29 /**
30  * Responsible for rendering a response to the client.
31  *
32  * @author Howard M. Lewis Ship
33  * @since 4.0
34  */

35 public class ResponseRendererImpl implements ResponseRenderer
36 {
37     private RequestLocaleManager _localeManager;
38
39     private MarkupWriterSource _markupWriterSource;
40
41     private WebResponse _webResponse;
42
43     /**
44      * Inside a {@link org.apache.tapestry.util.ContentType}, the output encoding is called
45      * "charset".
46      */

47
48     public static final String JavaDoc ENCODING_KEY = "charset";
49
50     public void renderResponse(IRequestCycle cycle) throws IOException JavaDoc
51     {
52         _localeManager.persistLocale();
53
54         IPage page = cycle.getPage();
55
56         ContentType contentType = page.getResponseContentType();
57
58         String JavaDoc encoding = contentType.getParameter(ENCODING_KEY);
59
60         if (encoding == null)
61         {
62             encoding = cycle.getEngine().getOutputEncoding();
63
64             contentType.setParameter(ENCODING_KEY, encoding);
65         }
66
67         PrintWriter JavaDoc printWriter = _webResponse.getPrintWriter(contentType);
68
69         IMarkupWriter writer = _markupWriterSource.newMarkupWriter(printWriter, contentType);
70
71         cycle.renderPage(writer);
72
73         writer.close();
74     }
75
76     public void setLocaleManager(RequestLocaleManager localeManager)
77     {
78         _localeManager = localeManager;
79     }
80
81     public void setMarkupWriterSource(MarkupWriterSource markupWriterSource)
82     {
83         _markupWriterSource = markupWriterSource;
84     }
85
86     public void setWebResponse(WebResponse webResponse)
87     {
88         _webResponse = webResponse;
89     }
90 }
Popular Tags