001// Copyright 2007-2014 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
015package org.apache.tapestry5.internal.services;
016
017import org.apache.tapestry5.ContentType;
018import org.apache.tapestry5.MarkupWriter;
019import org.apache.tapestry5.SymbolConstants;
020import org.apache.tapestry5.TapestryConstants;
021import org.apache.tapestry5.internal.InternalConstants;
022import org.apache.tapestry5.ioc.IOOperation;
023import org.apache.tapestry5.ioc.annotations.Inject;
024import org.apache.tapestry5.ioc.annotations.Symbol;
025import org.apache.tapestry5.json.JSONObject;
026import org.apache.tapestry5.services.*;
027
028import java.io.IOException;
029import java.io.PrintWriter;
030
031public class AjaxPartialResponseRendererImpl implements AjaxPartialResponseRenderer
032{
033    private final MarkupWriterFactory factory;
034
035    private final Request request;
036
037    private final Response response;
038
039    private final PartialMarkupRenderer partialMarkupRenderer;
040
041    private final String outputEncoding;
042
043    private final boolean compactJSON;
044
045    public AjaxPartialResponseRendererImpl(MarkupWriterFactory factory,
046
047                                           Request request,
048
049                                           Response response,
050
051                                           PartialMarkupRenderer partialMarkupRenderer,
052
053                                           @Inject
054                                           @Symbol(SymbolConstants.CHARSET)
055                                           String outputEncoding,
056
057                                           @Symbol(SymbolConstants.COMPACT_JSON)
058                                           boolean compactJSON)
059    {
060        this.factory = factory;
061        this.request = request;
062        this.response = response;
063        this.partialMarkupRenderer = partialMarkupRenderer;
064        this.outputEncoding = outputEncoding;
065        this.compactJSON = compactJSON;
066    }
067
068    public void renderPartialPageMarkup(final JSONObject reply) throws IOException
069    {
070        assert reply != null;
071
072        request.setAttribute(TapestryConstants.RESPONSE_RENDERER, new IOOperation<Void>()
073        {
074            public Void perform() throws IOException
075            {
076                // This is a complex area as we are trying to keep public and private services properly
077                // separated, and trying to keep stateless and stateful (i.e., perthread scope) services
078                // separated. So we inform the stateful queue service what it needs to do here ...
079
080                ContentType contentType = new ContentType(InternalConstants.JSON_MIME_TYPE, outputEncoding);
081
082                String pageName = (String) request.getAttribute(InternalConstants.PAGE_NAME_ATTRIBUTE_NAME);
083
084                MarkupWriter writer = factory.newPartialMarkupWriter(pageName);
085
086                // ... and here, the pipeline eventually reaches the PRQ to let it render the root render command.
087
088                partialMarkupRenderer.renderMarkup(writer, reply);
089
090                PrintWriter pw = response.getPrintWriter(contentType.toString());
091
092                reply.print(pw, compactJSON);
093
094                pw.close();
095
096                return null;
097            }
098        });
099    }
100
101    public void renderPartialPageMarkup() throws IOException
102    {
103        renderPartialPageMarkup(new JSONObject());
104    }
105}