Coverage Report - org.apache.tiles.request.velocity.VelocityRequest
 
Classes in this File Line Coverage Branch Coverage Complexity
VelocityRequest
97%
35/36
75%
9/12
2.5
 
 1  
 /*
 2  
  * $Id: VelocityRequest.java 1332186 2012-04-30 13:20:15Z nlebas $
 3  
  *
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  * http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 package org.apache.tiles.request.velocity;
 23  
 
 24  
 import java.io.IOException;
 25  
 import java.io.PrintWriter;
 26  
 import java.io.Writer;
 27  
 import java.util.ArrayList;
 28  
 import java.util.Collections;
 29  
 import java.util.List;
 30  
 import java.util.Map;
 31  
 
 32  
 import javax.servlet.RequestDispatcher;
 33  
 import javax.servlet.ServletException;
 34  
 import javax.servlet.http.HttpServletRequest;
 35  
 import javax.servlet.http.HttpServletResponse;
 36  
 
 37  
 import org.apache.tiles.request.AbstractViewRequest;
 38  
 import org.apache.tiles.request.ApplicationContext;
 39  
 import org.apache.tiles.request.DispatchRequest;
 40  
 import org.apache.tiles.request.servlet.ExternalWriterHttpServletResponse;
 41  
 import org.apache.tiles.request.servlet.ServletRequest;
 42  
 import org.apache.tiles.request.servlet.ServletUtil;
 43  
 import org.apache.velocity.context.Context;
 44  
 
 45  
 /**
 46  
  * The implementation of the Tiles request context specific for Velocity.
 47  
  *
 48  
  * @version $Rev: 1332186 $ $Date: 2012-04-30 23:20:15 +1000 (Mon, 30 Apr 2012) $
 49  
  */
 50  
 public class VelocityRequest extends AbstractViewRequest {
 51  
 
 52  
     /**
 53  
      * The native available scopes, in fact only "page".
 54  
      */
 55  
     private final List<String> scopes;
 56  
 
 57  
     /**
 58  
      * The Velocity current context.
 59  
      */
 60  
     private final Context ctx;
 61  
 
 62  
     /**
 63  
      * The writer to use to render the response. It may be null, if not necessary.
 64  
      */
 65  
     private Writer writer;
 66  
 
 67  
     /**
 68  
      * The map of the page scope.
 69  
      */
 70  
     private Map<String, Object> pageScope;
 71  
 
 72  
     /**
 73  
      * Factory method to create a Velocity request.
 74  
      *
 75  
      * @param applicationContext The application context.
 76  
      * @param request The request.
 77  
      * @param response The response.
 78  
      * @param velocityContext The Velocity context.
 79  
      * @param writer The writer to write into.
 80  
      * @return The request.
 81  
      */
 82  
     public static VelocityRequest createVelocityRequest(
 83  
             ApplicationContext applicationContext, HttpServletRequest request,
 84  
             HttpServletResponse response, Context velocityContext, Writer writer) {
 85  2
         DispatchRequest servletRequest = new ServletRequest(
 86  
                 applicationContext, request, response);
 87  2
         VelocityRequest velocityRequest = new VelocityRequest(
 88  
                 servletRequest, velocityContext, writer);
 89  2
         return velocityRequest;
 90  
     }
 91  
 
 92  
     /**
 93  
      * Constructor.
 94  
      *
 95  
      * @param enclosedRequest The request that exposes non-Velocity specific properties
 96  
      * @param ctx The Velocity current context.
 97  
      * @param writer The writer to use to render the response. It may be null, if not necessary.
 98  
      */
 99  
     public VelocityRequest(
 100  
             DispatchRequest enclosedRequest, Context ctx, Writer writer) {
 101  12
         super(enclosedRequest);
 102  12
         List<String> scopes = new ArrayList<String>();
 103  12
         scopes.addAll(enclosedRequest.getAvailableScopes());
 104  12
         scopes.add("page");
 105  12
         this.scopes = Collections.unmodifiableList(scopes);
 106  12
         this.ctx = ctx;
 107  12
         this.writer = writer;
 108  12
     }
 109  
 
 110  
     @Override
 111  
     public List<String> getAvailableScopes() {
 112  1
         return scopes;
 113  
     }
 114  
 
 115  
     /** {@inheritDoc} */
 116  
     @Override
 117  
     protected void doInclude(String path) throws IOException {
 118  3
         ServletRequest servletRequest = org.apache.tiles.request.servlet.ServletUtil.getServletRequest(this);
 119  3
         HttpServletRequest request = servletRequest.getRequest();
 120  3
         HttpServletResponse response = servletRequest.getResponse();
 121  3
         RequestDispatcher rd = request.getRequestDispatcher(path);
 122  
 
 123  3
         if (rd == null) {
 124  1
             throw new IOException("No request dispatcher returned for path '"
 125  
                     + path + "'");
 126  
         }
 127  
 
 128  2
         PrintWriter printWriter = getPrintWriter();
 129  
         try {
 130  2
             rd.include(request, new ExternalWriterHttpServletResponse(response,
 131  
                     printWriter));
 132  1
         } catch (ServletException ex) {
 133  1
             throw ServletUtil.wrapServletException(ex, "ServletException including path '"
 134  
                     + path + "'.");
 135  1
         }
 136  1
     }
 137  
 
 138  
     /** {@inheritDoc} */
 139  
     @Override
 140  
     public PrintWriter getPrintWriter() {
 141  5
         if (writer == null) {
 142  1
             throw new IllegalStateException(
 143  
                     "A writer-less Tiles request has been created, cannot return a PrintWriter");
 144  
         }
 145  4
         if (writer instanceof PrintWriter) {
 146  1
             return (PrintWriter) writer;
 147  
         }
 148  3
         return new PrintWriter(writer);
 149  
     }
 150  
 
 151  
     /** {@inheritDoc} */
 152  
     @Override
 153  
     public Writer getWriter() {
 154  2
         if (writer == null) {
 155  1
             throw new IllegalStateException(
 156  
                     "A writer-less Tiles request has been created, cannot return a PrintWriter");
 157  
         }
 158  1
         return writer;
 159  
     }
 160  
 
 161  
     /**
 162  
      * Returns the page scope.
 163  
      *
 164  
      * @return The page scope.
 165  
      */
 166  
     public Map<String, Object> getPageScope() {
 167  1
         if (pageScope == null) {
 168  1
             pageScope = new VelocityScopeMap(ctx);
 169  
         }
 170  1
         return pageScope;
 171  
     }
 172  
 
 173  
     @Override
 174  
     public Map<String, Object> getContext(String scope) {
 175  0
         return "page".equals(scope) ? getPageScope() : super.getContext(scope);
 176  
     }
 177  
 
 178  
 }