Coverage Report - org.apache.tapestry5.internal.services.AjaxPartialResponseRendererImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
AjaxPartialResponseRendererImpl
100%
16/16
N/A
0
 
 1  
 // Copyright 2007, 2008, 2009 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.tapestry5.internal.services;
 16  
 
 17  
 import org.apache.tapestry5.ContentType;
 18  
 import org.apache.tapestry5.MarkupWriter;
 19  
 import org.apache.tapestry5.SymbolConstants;
 20  
 import org.apache.tapestry5.internal.InternalConstants;
 21  
 import org.apache.tapestry5.ioc.annotations.Inject;
 22  
 import org.apache.tapestry5.ioc.annotations.Symbol;
 23  
 import org.apache.tapestry5.json.JSONObject;
 24  
 import org.apache.tapestry5.services.MarkupWriterFactory;
 25  
 import org.apache.tapestry5.services.PartialMarkupRenderer;
 26  
 import org.apache.tapestry5.services.Request;
 27  
 import org.apache.tapestry5.services.Response;
 28  
 
 29  
 import java.io.IOException;
 30  
 import java.io.PrintWriter;
 31  
 
 32  
 public class AjaxPartialResponseRendererImpl implements AjaxPartialResponseRenderer
 33  
 {
 34  
     private final MarkupWriterFactory factory;
 35  
 
 36  
     private final Request request;
 37  
 
 38  
     private final Response response;
 39  
 
 40  
     private final PartialMarkupRenderer partialMarkupRenderer;
 41  
 
 42  
     private final String outputEncoding;
 43  
 
 44  
     public AjaxPartialResponseRendererImpl(MarkupWriterFactory factory,
 45  
 
 46  
                                            Request request,
 47  
 
 48  
                                            Response response,
 49  
 
 50  
                                            PartialMarkupRenderer partialMarkupRenderer,
 51  
 
 52  
                                            @Inject @Symbol(SymbolConstants.CHARSET)
 53  
                                            String outputEncoding)
 54  2
     {
 55  2
         this.factory = factory;
 56  2
         this.request = request;
 57  2
         this.response = response;
 58  2
         this.partialMarkupRenderer = partialMarkupRenderer;
 59  2
         this.outputEncoding = outputEncoding;
 60  2
     }
 61  
 
 62  
     public void renderPartialPageMarkup() throws IOException
 63  
     {
 64  
         // This is a complex area as we are trying to keep public and private services properly
 65  
         // seperated, and trying to keep stateless and stateful (i.e., perthread scope) services
 66  
         // seperated. So we inform the stateful queue service what it needs to do here ...
 67  
 
 68  36
         ContentType pageContentType =
 69  
                 (ContentType) request.getAttribute(InternalConstants.CONTENT_TYPE_ATTRIBUTE_NAME);
 70  
 
 71  36
         ContentType contentType = new ContentType(InternalConstants.JSON_MIME_TYPE, outputEncoding);
 72  
 
 73  36
         MarkupWriter writer = factory.newPartialMarkupWriter(pageContentType);
 74  
 
 75  36
         JSONObject reply = new JSONObject();
 76  
 
 77  
         // ... and here, the pipeline eventually reaches the PRQ to let it render the root render command.
 78  
 
 79  36
         partialMarkupRenderer.renderMarkup(writer, reply);
 80  
 
 81  36
         PrintWriter pw = response.getPrintWriter(contentType.toString());
 82  
 
 83  36
         pw.print(reply);
 84  
 
 85  36
         pw.close();
 86  36
     }
 87  
 }