001 // Copyright 2007, 2008, 2009 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package org.apache.tapestry5.internal.services;
016
017 import org.apache.tapestry5.ContentType;
018 import org.apache.tapestry5.MarkupWriter;
019 import org.apache.tapestry5.SymbolConstants;
020 import org.apache.tapestry5.internal.InternalConstants;
021 import org.apache.tapestry5.ioc.annotations.Inject;
022 import org.apache.tapestry5.ioc.annotations.Symbol;
023 import org.apache.tapestry5.json.JSONObject;
024 import org.apache.tapestry5.services.MarkupWriterFactory;
025 import org.apache.tapestry5.services.PartialMarkupRenderer;
026 import org.apache.tapestry5.services.Request;
027 import org.apache.tapestry5.services.Response;
028
029 import java.io.IOException;
030 import java.io.PrintWriter;
031
032 public class AjaxPartialResponseRendererImpl implements AjaxPartialResponseRenderer
033 {
034 private final MarkupWriterFactory factory;
035
036 private final Request request;
037
038 private final Response response;
039
040 private final PartialMarkupRenderer partialMarkupRenderer;
041
042 private final String outputEncoding;
043
044 public AjaxPartialResponseRendererImpl(MarkupWriterFactory factory,
045
046 Request request,
047
048 Response response,
049
050 PartialMarkupRenderer partialMarkupRenderer,
051
052 @Inject @Symbol(SymbolConstants.CHARSET)
053 String outputEncoding)
054 {
055 this.factory = factory;
056 this.request = request;
057 this.response = response;
058 this.partialMarkupRenderer = partialMarkupRenderer;
059 this.outputEncoding = outputEncoding;
060 }
061
062 public void renderPartialPageMarkup() throws IOException
063 {
064 // This is a complex area as we are trying to keep public and private services properly
065 // seperated, and trying to keep stateless and stateful (i.e., perthread scope) services
066 // seperated. So we inform the stateful queue service what it needs to do here ...
067
068 ContentType pageContentType =
069 (ContentType) request.getAttribute(InternalConstants.CONTENT_TYPE_ATTRIBUTE_NAME);
070
071 ContentType contentType = new ContentType(InternalConstants.JSON_MIME_TYPE, outputEncoding);
072
073 MarkupWriter writer = factory.newPartialMarkupWriter(pageContentType);
074
075 JSONObject reply = new JSONObject();
076
077 // ... and here, the pipeline eventually reaches the PRQ to let it render the root render command.
078
079 partialMarkupRenderer.renderMarkup(writer, reply);
080
081 PrintWriter pw = response.getPrintWriter(contentType.toString());
082
083 pw.print(reply);
084
085 pw.close();
086 }
087 }