Coverage Report - org.apache.tapestry.services.impl.DefaultResponseBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultResponseBuilder
47% 
50% 
1.229
 
 1  
 // Copyright Mar 18, 2006 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  
 package org.apache.tapestry.services.impl;
 15  
 
 16  
 import org.apache.hivemind.Resource;
 17  
 import org.apache.hivemind.util.Defense;
 18  
 import org.apache.tapestry.*;
 19  
 import org.apache.tapestry.asset.AssetFactory;
 20  
 import org.apache.tapestry.engine.NullWriter;
 21  
 import org.apache.tapestry.markup.MarkupWriterSource;
 22  
 import org.apache.tapestry.services.RequestLocaleManager;
 23  
 import org.apache.tapestry.services.ResponseBuilder;
 24  
 import org.apache.tapestry.util.ContentType;
 25  
 import org.apache.tapestry.util.PageRenderSupportImpl;
 26  
 import org.apache.tapestry.web.WebResponse;
 27  
 
 28  
 import java.io.IOException;
 29  
 import java.io.PrintWriter;
 30  
 
 31  
 
 32  
 /**
 33  
  * Manages normal html responses for tapestry request/response cycles.
 34  
  * 
 35  
  * @author jkuhnert
 36  
  */
 37  
 public class DefaultResponseBuilder implements ResponseBuilder
 38  
 {   
 39  
     private final AssetFactory _assetFactory;
 40  
     
 41  
     private final String _namespace;
 42  
     
 43  
     private PageRenderSupportImpl _prs;
 44  
     
 45  
     private RequestLocaleManager _localeManager;
 46  
     
 47  
     private MarkupWriterSource _markupWriterSource;
 48  
 
 49  
     private WebResponse _webResponse;
 50  
     
 51  
     /** Default writer for rendering html output. */
 52  
     private IMarkupWriter _writer;
 53  
     
 54  40
     private boolean _closeWriter = true;
 55  
     
 56  
     /**
 57  
      * Portlet constructor.
 58  
      * 
 59  
      * @param writer
 60  
      */
 61  
     public DefaultResponseBuilder(IMarkupWriter writer, 
 62  
             AssetFactory assetFactory, String namespace, boolean closeWriter)
 63  0
     {
 64  0
         _writer = writer;
 65  0
         _assetFactory = assetFactory;
 66  0
         _namespace = namespace;
 67  0
         _closeWriter = closeWriter;
 68  0
     }
 69  
     
 70  
     /**
 71  
      * Used in testing only.
 72  
      * @param writer
 73  
      */
 74  
     public DefaultResponseBuilder(IMarkupWriter writer)
 75  40
     {
 76  40
         _writer = writer;
 77  40
         _assetFactory = null;
 78  40
         _namespace = null;
 79  40
     }
 80  
     
 81  
     /**
 82  
      * Creates a new response builder with the required services it needs
 83  
      * to render the response when {@link #renderResponse(IRequestCycle)} is called.
 84  
      * 
 85  
      * @param localeManager 
 86  
      *          Used to set the locale on the response.
 87  
      * @param markupWriterSource
 88  
      *          Creates IMarkupWriter instance to be used.
 89  
      * @param webResponse
 90  
      *          Web response for output stream.
 91  
      */
 92  
     public DefaultResponseBuilder(RequestLocaleManager localeManager, 
 93  
             MarkupWriterSource markupWriterSource, WebResponse webResponse,
 94  
             AssetFactory assetFactory, String namespace)
 95  0
     {
 96  0
         Defense.notNull(assetFactory, "assetService");
 97  
         
 98  0
         _localeManager = localeManager;
 99  0
         _markupWriterSource = markupWriterSource;
 100  0
         _webResponse = webResponse;
 101  
         
 102  
         // Used by PageRenderSupport
 103  
         
 104  0
         _assetFactory = assetFactory;
 105  0
         _namespace = namespace;
 106  0
     }
 107  
     
 108  
     /**
 109  
      * 
 110  
      * {@inheritDoc}
 111  
      */
 112  
     public boolean isDynamic()
 113  
     {
 114  0
         return false;
 115  
     }
 116  
     
 117  
     /**
 118  
      * 
 119  
      * {@inheritDoc}
 120  
      */
 121  
     public void renderResponse(IRequestCycle cycle)
 122  
     throws IOException
 123  
     {
 124  0
         if (_writer == null) {
 125  
             
 126  0
             _localeManager.persistLocale();
 127  
             
 128  0
             IPage page = cycle.getPage();
 129  
 
 130  0
             ContentType contentType = page.getResponseContentType();
 131  
 
 132  0
             String encoding = contentType.getParameter(ENCODING_KEY);
 133  
 
 134  0
             if (encoding == null)
 135  
             {
 136  0
                 encoding = cycle.getEngine().getOutputEncoding();
 137  
 
 138  0
                 contentType.setParameter(ENCODING_KEY, encoding);
 139  
             }
 140  
             
 141  0
             PrintWriter printWriter = _webResponse.getPrintWriter(contentType);
 142  
             
 143  0
             _writer = _markupWriterSource.newMarkupWriter(printWriter, contentType);
 144  
         }
 145  
         
 146  
         // render response
 147  
         
 148  0
         _prs = new PageRenderSupportImpl(_assetFactory, _namespace, cycle.getPage().getLocation(), this);
 149  
         
 150  0
         TapestryUtils.storePageRenderSupport(cycle, _prs);
 151  
         
 152  0
         cycle.renderPage(this);
 153  
         
 154  0
         TapestryUtils.removePageRenderSupport(cycle);
 155  
         
 156  0
         flush();
 157  
 
 158  0
         if (_closeWriter)
 159  0
             _writer.close();
 160  0
     }
 161  
     
 162  
     public void flush()
 163  
     throws IOException
 164  
     {
 165  
         // Important - causes any cookies stored to properly be written out before the
 166  
         // rest of the response starts being written - see TAPESTRY-825
 167  
 
 168  0
         _writer.flush();
 169  0
     }
 170  
     
 171  
     /**
 172  
      * 
 173  
      * {@inheritDoc}
 174  
      */
 175  
     public void render(IMarkupWriter writer, IRender render, IRequestCycle cycle)
 176  
     {
 177  76
         if (writer == null)
 178  2
             render.render(_writer, cycle);
 179  
         else
 180  74
             render.render(writer, cycle);
 181  76
     }
 182  
     
 183  
     /** 
 184  
      * {@inheritDoc}
 185  
      */
 186  
     public void updateComponent(String id)
 187  
     {
 188  0
     }
 189  
     
 190  
     /** 
 191  
      * {@inheritDoc}
 192  
      */
 193  
     public boolean contains(IComponent target)
 194  
     {
 195  0
         return false;
 196  
     }
 197  
     
 198  
     /**
 199  
      * {@inheritDoc}
 200  
      */
 201  
     public boolean explicitlyContains(IComponent target)
 202  
     {
 203  0
         return false;
 204  
     }
 205  
 
 206  
     /** 
 207  
      * {@inheritDoc}
 208  
      */
 209  
     public IMarkupWriter getWriter()
 210  
     {
 211  2
         if (_writer == null)
 212  1
             return NullWriter.getSharedInstance();
 213  
         
 214  1
         return _writer;
 215  
     }
 216  
     
 217  
     /** 
 218  
      * {@inheritDoc}
 219  
      */
 220  
     public IMarkupWriter getWriter(String id, String type)
 221  
     {
 222  1
         if (_writer == null)
 223  0
             return NullWriter.getSharedInstance();
 224  
         
 225  1
         return _writer;
 226  
     }
 227  
     
 228  
     /** 
 229  
      * {@inheritDoc}
 230  
      */
 231  
     public boolean isBodyScriptAllowed(IComponent target)
 232  
     {
 233  3
         return true;
 234  
     }
 235  
 
 236  
     /** 
 237  
      * {@inheritDoc}
 238  
      */
 239  
     public boolean isExternalScriptAllowed(IComponent target)
 240  
     {
 241  4
         return true;
 242  
     }
 243  
 
 244  
     /** 
 245  
      * {@inheritDoc}
 246  
      */
 247  
     public boolean isInitializationScriptAllowed(IComponent target)
 248  
     {
 249  7
         return true;
 250  
     }
 251  
     
 252  
     /**
 253  
      * {@inheritDoc}
 254  
      */
 255  
     public boolean isImageInitializationAllowed(IComponent target)
 256  
     {
 257  1
         return true;
 258  
     }
 259  
     
 260  
     /**
 261  
      * {@inheritDoc}
 262  
      */
 263  
     public String getPreloadedImageReference(IComponent target, IAsset source)
 264  
     {
 265  0
         return _prs.getPreloadedImageReference(target, source);
 266  
     }
 267  
     
 268  
     /**
 269  
      * {@inheritDoc}
 270  
      */
 271  
     public String getPreloadedImageReference(IComponent target, String url)
 272  
     {
 273  0
         return _prs.getPreloadedImageReference(target, url);
 274  
     }
 275  
 
 276  
     /**
 277  
      * {@inheritDoc}
 278  
      */
 279  
     public String getPreloadedImageReference(String url)
 280  
     {
 281  0
         return _prs.getPreloadedImageReference(url);
 282  
     }
 283  
 
 284  
     /**
 285  
      * {@inheritDoc}
 286  
      */
 287  
     public void addBodyScript(IComponent target, String script)
 288  
     {
 289  0
         _prs.addBodyScript(target, script);
 290  0
     }
 291  
 
 292  
     /**
 293  
      * {@inheritDoc}
 294  
      */
 295  
     public void addBodyScript(String script)
 296  
     {
 297  0
         _prs.addBodyScript(script);
 298  0
     }
 299  
     
 300  
     /**
 301  
      * {@inheritDoc}
 302  
      */
 303  
     public void addExternalScript(IComponent target, Resource resource)
 304  
     {
 305  0
         _prs.addExternalScript(target, resource);
 306  0
     }
 307  
 
 308  
     /**
 309  
      * {@inheritDoc}
 310  
      */
 311  
     public void addExternalScript(Resource resource)
 312  
     {
 313  0
         _prs.addExternalScript(resource);
 314  0
     }
 315  
 
 316  
     /**
 317  
      * {@inheritDoc}
 318  
      */
 319  
     public void addInitializationScript(IComponent target, String script)
 320  
     {
 321  0
         _prs.addInitializationScript(target, script);
 322  0
     }
 323  
 
 324  
     /**
 325  
      * {@inheritDoc}
 326  
      */
 327  
     public void addInitializationScript(String script)
 328  
     {
 329  0
         _prs.addInitializationScript(script);
 330  0
     }
 331  
 
 332  
     /**
 333  
      * {@inheritDoc}
 334  
      */
 335  
     public String getUniqueString(String baseValue)
 336  
     {
 337  0
         return _prs.getUniqueString(baseValue);
 338  
     }
 339  
     
 340  
     /**
 341  
      * {@inheritDoc}
 342  
      */
 343  
     public void writeBodyScript(IMarkupWriter writer, IRequestCycle cycle)
 344  
     {
 345  0
         _prs.writeBodyScript(writer, cycle);
 346  0
     }
 347  
     
 348  
     /**
 349  
      * {@inheritDoc}
 350  
      */
 351  
     public void writeInitializationScript(IMarkupWriter writer)
 352  
     {
 353  0
         _prs.writeInitializationScript(writer);
 354  0
     }
 355  
     
 356  
     /** 
 357  
      * {@inheritDoc}
 358  
      */
 359  
     public void beginBodyScript(IMarkupWriter writer, IRequestCycle cycle)
 360  
     {
 361  4
         writer.begin("script");
 362  4
         writer.attribute("type", "text/javascript");
 363  4
         writer.printRaw("<!--");
 364  4
         writer.println();
 365  4
     }
 366  
     
 367  
     /** 
 368  
      * {@inheritDoc}
 369  
      */
 370  
     public void endBodyScript(IMarkupWriter writer, IRequestCycle cycle)
 371  
     {
 372  4
         writer.println();
 373  4
         writer.printRaw("// -->");
 374  4
         writer.end();
 375  4
     }
 376  
 
 377  
     /** 
 378  
      * {@inheritDoc}
 379  
      */
 380  
     public void writeBodyScript(IMarkupWriter writer, String script, IRequestCycle cycle)
 381  
     {
 382  3
         writer.printRaw(script);
 383  3
     }
 384  
 
 385  
     /** 
 386  
      * {@inheritDoc}
 387  
      */
 388  
     public void writeExternalScript(IMarkupWriter writer, String url, IRequestCycle cycle)
 389  
     {        
 390  4
         writer.begin("script");
 391  4
         writer.attribute("type", "text/javascript");
 392  4
         writer.attribute("src", url);
 393  4
         writer.end();
 394  4
         writer.println();
 395  4
     }
 396  
     
 397  
     /** 
 398  
      * {@inheritDoc}
 399  
      */
 400  
     public void writeImageInitializations(IMarkupWriter writer, String script, String preloadName, IRequestCycle cycle)
 401  
     {
 402  
         
 403  3
         writer.println();
 404  3
         writer.printRaw("dojo.addOnLoad(function(e) {\n");
 405  
         
 406  3
         writer.printRaw(preloadName + " = [];\n");
 407  3
         writer.printRaw("if (document.images)\n");
 408  3
         writer.printRaw("{\n");
 409  3
         writer.printRaw(script);
 410  3
         writer.printRaw("}\n");
 411  
         
 412  3
         writer.printRaw("});");
 413  3
     }
 414  
     
 415  
     /** 
 416  
      * {@inheritDoc}
 417  
      */
 418  
     public void writeInitializationScript(IMarkupWriter writer, String script)
 419  
     {
 420  2
         writer.begin("script");
 421  2
         writer.attribute("type", "text/javascript");
 422  2
         writer.printRaw("<!--\n");
 423  
         
 424  2
         writer.printRaw("dojo.addOnLoad(function(e) {\n");
 425  
         
 426  2
         writer.printRaw(script);
 427  
         
 428  2
         writer.printRaw("});");
 429  
         
 430  2
         writer.printRaw("\n// -->");
 431  2
         writer.end();
 432  2
     }
 433  
 
 434  
     /**
 435  
      * This implementation does nothing.
 436  
      * {@inheritDoc}
 437  
      */
 438  
     public void addStatusMessage(IMarkupWriter normalWriter, String category, String text)
 439  
     {
 440  0
     }
 441  
 }