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