Coverage Report - org.apache.tapestry.services.impl.DefaultResponseBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultResponseBuilder
47% 
50% 
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  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  0
             String encoding = contentType.getParameter(ENCODING_KEY);
 132  
 
 133  0
             if (encoding == null)
 134  
             {
 135  0
                 encoding = cycle.getEngine().getOutputEncoding();
 136  
 
 137  0
                 contentType.setParameter(ENCODING_KEY, encoding);
 138  
             }
 139  
 
 140  0
             PrintWriter printWriter = _webResponse.getPrintWriter(contentType);
 141  
 
 142  0
             _writer = _markupWriterSource.newMarkupWriter(printWriter, contentType);
 143  
         }
 144  
 
 145  
         // render response
 146  
 
 147  0
         _prs = new PageRenderSupportImpl(_assetFactory, _namespace, cycle.getPage().getLocation(), this);
 148  
 
 149  0
         TapestryUtils.storePageRenderSupport(cycle, _prs);
 150  
 
 151  0
         cycle.renderPage(this);
 152  
 
 153  0
         TapestryUtils.removePageRenderSupport(cycle);
 154  
 
 155  0
         flush();
 156  
 
 157  0
         if (_closeWriter)
 158  0
             _writer.close();
 159  0
     }
 160  
 
 161  
     public void flush()
 162  
       throws IOException
 163  
     {
 164  
         // Important - causes any cookies stored to properly be written out before the
 165  
         // rest of the response starts being written - see TAPESTRY-825
 166  
 
 167  0
         _writer.flush();
 168  0
     }
 169  
 
 170  
     /**
 171  
      *
 172  
      * {@inheritDoc}
 173  
      */
 174  
     public void render(IMarkupWriter writer, IRender render, IRequestCycle cycle)
 175  
     {
 176  76
         if (writer == null)
 177  2
             render.render(_writer, cycle);
 178  
         else
 179  74
             render.render(writer, cycle);
 180  76
     }
 181  
 
 182  
     /**
 183  
      * {@inheritDoc}
 184  
      */
 185  
     public void updateComponent(String id)
 186  
     {
 187  0
     }
 188  
 
 189  
     /**
 190  
      * {@inheritDoc}
 191  
      */
 192  
     public boolean contains(IComponent target)
 193  
     {
 194  0
         return false;
 195  
     }
 196  
 
 197  
     /**
 198  
      * {@inheritDoc}
 199  
      */
 200  
     public boolean explicitlyContains(IComponent target)
 201  
     {
 202  0
         return false;
 203  
     }
 204  
 
 205  
     /**
 206  
      * {@inheritDoc}
 207  
      */
 208  
     public IMarkupWriter getWriter()
 209  
     {
 210  2
         if (_writer == null)
 211  1
             return NullWriter.getSharedInstance();
 212  
 
 213  1
         return _writer;
 214  
     }
 215  
 
 216  
     /**
 217  
      * {@inheritDoc}
 218  
      */
 219  
     public IMarkupWriter getWriter(String id, String type)
 220  
     {
 221  1
         if (_writer == null)
 222  0
             return NullWriter.getSharedInstance();
 223  
 
 224  1
         return _writer;
 225  
     }
 226  
 
 227  
     /**
 228  
      * {@inheritDoc}
 229  
      */
 230  
     public boolean isBodyScriptAllowed(IComponent target)
 231  
     {
 232  3
         return true;
 233  
     }
 234  
 
 235  
     /**
 236  
      * {@inheritDoc}
 237  
      */
 238  
     public boolean isExternalScriptAllowed(IComponent target)
 239  
     {
 240  4
         return true;
 241  
     }
 242  
 
 243  
     /**
 244  
      * {@inheritDoc}
 245  
      */
 246  
     public boolean isInitializationScriptAllowed(IComponent target)
 247  
     {
 248  7
         return true;
 249  
     }
 250  
 
 251  
     /**
 252  
      * {@inheritDoc}
 253  
      */
 254  
     public boolean isImageInitializationAllowed(IComponent target)
 255  
     {
 256  1
         return true;
 257  
     }
 258  
 
 259  
     /**
 260  
      * {@inheritDoc}
 261  
      */
 262  
     public String getPreloadedImageReference(IComponent target, IAsset source)
 263  
     {
 264  0
         return _prs.getPreloadedImageReference(target, source);
 265  
     }
 266  
 
 267  
     /**
 268  
      * {@inheritDoc}
 269  
      */
 270  
     public String getPreloadedImageReference(IComponent target, String url)
 271  
     {
 272  0
         return _prs.getPreloadedImageReference(target, url);
 273  
     }
 274  
 
 275  
     /**
 276  
      * {@inheritDoc}
 277  
      */
 278  
     public String getPreloadedImageReference(String url)
 279  
     {
 280  0
         return _prs.getPreloadedImageReference(url);
 281  
     }
 282  
 
 283  
     /**
 284  
      * {@inheritDoc}
 285  
      */
 286  
     public void addBodyScript(IComponent target, String script)
 287  
     {
 288  0
         _prs.addBodyScript(target, script);
 289  0
     }
 290  
 
 291  
     /**
 292  
      * {@inheritDoc}
 293  
      */
 294  
     public void addBodyScript(String script)
 295  
     {
 296  0
         _prs.addBodyScript(script);
 297  0
     }
 298  
 
 299  
     /**
 300  
      * {@inheritDoc}
 301  
      */
 302  
     public void addExternalScript(IComponent target, Resource resource)
 303  
     {
 304  0
         _prs.addExternalScript(target, resource);
 305  0
     }
 306  
 
 307  
     /**
 308  
      * {@inheritDoc}
 309  
      */
 310  
     public void addExternalScript(Resource resource)
 311  
     {
 312  0
         _prs.addExternalScript(resource);
 313  0
     }
 314  
 
 315  
     /**
 316  
      * {@inheritDoc}
 317  
      */
 318  
     public void addInitializationScript(IComponent target, String script)
 319  
     {
 320  0
         _prs.addInitializationScript(target, script);
 321  0
     }
 322  
 
 323  
     /**
 324  
      * {@inheritDoc}
 325  
      */
 326  
     public void addInitializationScript(String script)
 327  
     {
 328  0
         _prs.addInitializationScript(script);
 329  0
     }
 330  
 
 331  
     public void addScriptAfterInitialization(IComponent target, String script)
 332  
     {
 333  0
         _prs.addScriptAfterInitialization(target, script);
 334  0
     }
 335  
 
 336  
     /**
 337  
      * {@inheritDoc}
 338  
      */
 339  
     public String getUniqueString(String baseValue)
 340  
     {
 341  0
         return _prs.getUniqueString(baseValue);
 342  
     }
 343  
 
 344  
     /**
 345  
      * {@inheritDoc}
 346  
      */
 347  
     public void writeBodyScript(IMarkupWriter writer, IRequestCycle cycle)
 348  
     {
 349  0
         _prs.writeBodyScript(writer, cycle);
 350  0
     }
 351  
 
 352  
     /**
 353  
      * {@inheritDoc}
 354  
      */
 355  
     public void writeInitializationScript(IMarkupWriter writer)
 356  
     {
 357  0
         _prs.writeInitializationScript(writer);
 358  0
     }
 359  
 
 360  
     /**
 361  
      * {@inheritDoc}
 362  
      */
 363  
     public void beginBodyScript(IMarkupWriter writer, IRequestCycle cycle)
 364  
     {
 365  4
         writer.begin("script");
 366  4
         writer.attribute("type", "text/javascript");
 367  4
         writer.printRaw("<!--");
 368  4
         writer.println();
 369  4
     }
 370  
 
 371  
     /**
 372  
      * {@inheritDoc}
 373  
      */
 374  
     public void endBodyScript(IMarkupWriter writer, IRequestCycle cycle)
 375  
     {
 376  4
         writer.println();
 377  4
         writer.printRaw("// -->");
 378  4
         writer.end();
 379  4
     }
 380  
 
 381  
     /**
 382  
      * {@inheritDoc}
 383  
      */
 384  
     public void writeBodyScript(IMarkupWriter writer, String script, IRequestCycle cycle)
 385  
     {
 386  3
         writer.printRaw(script);
 387  3
     }
 388  
 
 389  
     /**
 390  
      * {@inheritDoc}
 391  
      */
 392  
     public void writeExternalScript(IMarkupWriter writer, String url, IRequestCycle cycle)
 393  
     {
 394  4
         writer.begin("script");
 395  4
         writer.attribute("type", "text/javascript");
 396  4
         writer.attribute("src", url);
 397  4
         writer.end();
 398  4
         writer.println();
 399  4
     }
 400  
 
 401  
     /**
 402  
      * {@inheritDoc}
 403  
      */
 404  
     public void writeImageInitializations(IMarkupWriter writer, String script, String preloadName, IRequestCycle cycle)
 405  
     {
 406  
 
 407  3
         writer.println();
 408  3
         writer.printRaw("dojo.addOnLoad(function(e) {\n");
 409  
 
 410  3
         writer.printRaw(preloadName + " = [];\n");
 411  3
         writer.printRaw("if (document.images)\n");
 412  3
         writer.printRaw("{\n");
 413  3
         writer.printRaw(script);
 414  3
         writer.printRaw("}\n");
 415  
 
 416  3
         writer.printRaw("});");
 417  3
     }
 418  
 
 419  
     /**
 420  
      * {@inheritDoc}
 421  
      */
 422  
     public void writeInitializationScript(IMarkupWriter writer, String script)
 423  
     {
 424  2
         writer.begin("script");
 425  2
         writer.attribute("type", "text/javascript");
 426  2
         writer.printRaw("<!--\n");
 427  
 
 428  2
         writer.printRaw("dojo.addOnLoad(function(e) {\n");
 429  
 
 430  2
         writer.printRaw(script);
 431  
 
 432  2
         writer.printRaw("});");
 433  
 
 434  2
         writer.printRaw("\n// -->");
 435  2
         writer.end();
 436  2
     }
 437  
 
 438  
     /**
 439  
      * This implementation does nothing.
 440  
      * {@inheritDoc}
 441  
      */
 442  
     public void addStatusMessage(IMarkupWriter normalWriter, String category, String text)
 443  
     {
 444  0
     }
 445  
 }