Coverage Report - org.apache.tiles.request.portlet.PortletRequest
 
Classes in this File Line Coverage Branch Coverage Complexity
PortletRequest
87%
67/77
47%
18/38
2.348
 
 1  
 /*
 2  
  * $Id$
 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  
 package org.apache.tiles.request.portlet;
 22  
 
 23  
 import java.io.IOException;
 24  
 import java.io.OutputStream;
 25  
 import java.io.PrintWriter;
 26  
 import java.io.Writer;
 27  
 import java.util.Arrays;
 28  
 import java.util.Collections;
 29  
 import java.util.List;
 30  
 import java.util.Locale;
 31  
 import java.util.Map;
 32  
 
 33  
 import javax.portlet.PortletContext;
 34  
 import javax.portlet.PortletException;
 35  
 import javax.portlet.PortletRequestDispatcher;
 36  
 import javax.portlet.PortletResponse;
 37  
 import javax.portlet.PortletSession;
 38  
 
 39  
 import org.apache.tiles.request.AbstractClientRequest;
 40  
 import org.apache.tiles.request.ApplicationContext;
 41  
 import org.apache.tiles.request.attribute.Addable;
 42  
 import org.apache.tiles.request.collection.HeaderValuesMap;
 43  
 import org.apache.tiles.request.collection.ReadOnlyEnumerationMap;
 44  
 import org.apache.tiles.request.collection.ScopeMap;
 45  
 import org.apache.tiles.request.portlet.delegate.RequestDelegate;
 46  
 import org.apache.tiles.request.portlet.delegate.ResponseDelegate;
 47  
 import org.apache.tiles.request.portlet.extractor.HeaderExtractor;
 48  
 import org.apache.tiles.request.portlet.extractor.RequestScopeExtractor;
 49  
 import org.apache.tiles.request.portlet.extractor.SessionScopeExtractor;
 50  
 
 51  
 /**
 52  
  * Portlet-based TilesApplicationContext implementation.
 53  
  *
 54  
  * @version $Rev$ $Date$
 55  
  */
 56  
 public class PortletRequest extends AbstractClientRequest {
 57  
 
 58  
     /**
 59  
      * The native available scopes.
 60  
      */
 61  1
     private static final List<String> SCOPES
 62  
             = Collections.unmodifiableList(Arrays.asList(REQUEST_SCOPE, "portletSession", "session", APPLICATION_SCOPE));
 63  
 
 64  
     /**
 65  
      * <p>The lazily instantiated <code>Map</code> of header name-value
 66  
      * combinations (immutable).</p>
 67  
      */
 68  28
     private Map<String, String> header = null;
 69  
 
 70  
 
 71  
     /**
 72  
      * <p>The lazily instantiated <code>Map</code> of header name-value
 73  
      * combinations (write-only).</p>
 74  
      */
 75  28
     private Addable<String> responseHeaders = null;
 76  
 
 77  
 
 78  
     /**
 79  
      * <p>The lazily instantitated <code>Map</code> of header name-values
 80  
      * combinations (immutable).</p>
 81  
      */
 82  28
     private Map<String, String[]> headerValues = null;
 83  
 
 84  
     /**
 85  
      * The <code>PortletContext</code> for this application.
 86  
      */
 87  28
     protected PortletContext context = null;
 88  
 
 89  
     /**
 90  
      * <p>The <code>PortletRequest</code> for this request.</p>
 91  
      */
 92  28
     protected javax.portlet.PortletRequest request = null;
 93  
 
 94  
     /**
 95  
      * The delegate to get information about parameters.
 96  
      */
 97  
     protected RequestDelegate requestDelegate;
 98  
 
 99  
 
 100  
     /**
 101  
      * <p>The lazily instantiated <code>Map</code> of request scope
 102  
      * attributes.</p>
 103  
      */
 104  28
     private Map<String, Object> requestScope = null;
 105  
 
 106  
 
 107  
     /**
 108  
      * <p>The <code>PortletResponse</code> for this request.</p>
 109  
      */
 110  28
     protected PortletResponse response = null;
 111  
 
 112  
     /**
 113  
      * The delegate to get information from a response (output stream, writer, etc.).
 114  
      */
 115  
     protected ResponseDelegate responseDelegate;
 116  
 
 117  
 
 118  
     /**
 119  
      * <p>The lazily instantiated <code>Map</code> of session scope
 120  
      * attributes.</p>
 121  
      */
 122  28
     private Map<String, Object> sessionScope = null;
 123  
 
 124  
     /**
 125  
      * <p>The lazily instantiated <code>Map</code> of portlet session scope
 126  
      * attributes.</p>
 127  
      */
 128  28
     private Map<String, Object> portletSessionScope = null;
 129  
 
 130  
 
 131  
     /**
 132  
      * Creates a new instance of PortletTilesRequestContext.
 133  
      *
 134  
      * @param applicationContext The Tiles application context.
 135  
      * @param context The portlet context to use.
 136  
      * @param request The request object to use.
 137  
      * @param response The response object to use.
 138  
      * @param requestDelegate The request delegate.
 139  
      * @param responseDelegate The response delegate.
 140  
      */
 141  
     public PortletRequest(ApplicationContext applicationContext,
 142  
             PortletContext context, javax.portlet.PortletRequest request,
 143  
             PortletResponse response, RequestDelegate requestDelegate, ResponseDelegate responseDelegate) {
 144  28
         super(applicationContext);
 145  
 
 146  
         // Save the specified Portlet API object references
 147  28
         this.context = context;
 148  28
         this.request = request;
 149  28
         this.response = response;
 150  28
         this.requestDelegate = requestDelegate;
 151  28
         this.responseDelegate = responseDelegate;
 152  28
     }
 153  
 
 154  
     /**
 155  
      * <p>Return the {@link PortletRequest} for this context.</p>
 156  
      *
 157  
      * @return The used portlet request.
 158  
      */
 159  
     public javax.portlet.PortletRequest getRequest() {
 160  1
         return (this.request);
 161  
     }
 162  
 
 163  
     /**
 164  
      * <p>Return the {@link PortletResponse} for this context.</p>
 165  
      *
 166  
      * @return The used portlet response.
 167  
      */
 168  
     public PortletResponse getResponse() {
 169  1
         return (this.response);
 170  
     }
 171  
 
 172  
     /**
 173  
      * Returns the portlet context.
 174  
      *
 175  
      * @return The portlet context.
 176  
      */
 177  
     public PortletContext getPortletContext() {
 178  7
         return context;
 179  
     }
 180  
 
 181  
     /** {@inheritDoc} */
 182  
     public Map<String, String> getHeader() {
 183  1
         if ((header == null) && (request != null)) {
 184  1
             header = new ReadOnlyEnumerationMap<String>(new HeaderExtractor(request, null));
 185  
         }
 186  1
         return (header);
 187  
     }
 188  
 
 189  
     /** {@inheritDoc} */
 190  
     public Addable<String> getResponseHeaders() {
 191  1
         if ((responseHeaders == null) && (request != null)) {
 192  1
             responseHeaders = new HeaderExtractor(null, response);
 193  
         }
 194  1
         return (responseHeaders);
 195  
     }
 196  
 
 197  
     /** {@inheritDoc} */
 198  
     public Map<String, String[]> getHeaderValues() {
 199  1
         if ((headerValues == null) && (request != null)) {
 200  1
             headerValues = new HeaderValuesMap(new HeaderExtractor(request, response));
 201  
         }
 202  1
         return (headerValues);
 203  
     }
 204  
 
 205  
     /** {@inheritDoc} */
 206  
     public Map<String, Object> getRequestScope() {
 207  1
         if ((requestScope == null) && (request != null)) {
 208  1
             requestScope = new ScopeMap(new RequestScopeExtractor(request));
 209  
         }
 210  1
         return (requestScope);
 211  
     }
 212  
 
 213  
     /** {@inheritDoc} */
 214  
     public Map<String, Object> getSessionScope() {
 215  1
         if ((sessionScope == null) && (request != null)) {
 216  1
             sessionScope = new ScopeMap(new SessionScopeExtractor(request,
 217  
                     PortletSession.APPLICATION_SCOPE));
 218  
         }
 219  1
         return (sessionScope);
 220  
     }
 221  
 
 222  
     /** {@inheritDoc} */
 223  
     public Map<String, Object> getPortletSessionScope() {
 224  1
         if ((portletSessionScope == null) && (request != null)) {
 225  1
             portletSessionScope = new ScopeMap(new SessionScopeExtractor(
 226  
                     request, PortletSession.APPLICATION_SCOPE));
 227  
         }
 228  1
         return (portletSessionScope);
 229  
     }
 230  
 
 231  
     @Override
 232  
     public List<String> getAvailableScopes() {
 233  0
         return SCOPES;
 234  
     }
 235  
 
 236  
     /** {@inheritDoc} */
 237  
     public Locale getRequestLocale() {
 238  1
         return request.getLocale();
 239  
     }
 240  
 
 241  
     @Override
 242  
     public Map<String, String> getParam() {
 243  1
         return requestDelegate.getParam();
 244  
     }
 245  
 
 246  
     @Override
 247  
     public Map<String, String[]> getParamValues() {
 248  1
         return requestDelegate.getParamValues();
 249  
     }
 250  
 
 251  
     /** {@inheritDoc} */
 252  
     public boolean isUserInRole(String role) {
 253  1
         return request.isUserInRole(role);
 254  
     }
 255  
 
 256  
     @Override
 257  
     public OutputStream getOutputStream() throws IOException {
 258  1
         return responseDelegate.getOutputStream();
 259  
     }
 260  
 
 261  
     @Override
 262  
     public PrintWriter getPrintWriter() throws IOException {
 263  1
         return responseDelegate.getPrintWriter();
 264  
     }
 265  
 
 266  
     @Override
 267  
     public Writer getWriter() throws IOException {
 268  1
         return responseDelegate.getWriter();
 269  
     }
 270  
 
 271  
     @Override
 272  
     public boolean isResponseCommitted() {
 273  1
         return responseDelegate.isResponseCommitted();
 274  
     }
 275  
 
 276  
     @Override
 277  
     public void setContentType(String contentType) {
 278  1
         responseDelegate.setContentType(contentType);
 279  1
     }
 280  
 
 281  
     /** {@inheritDoc} */
 282  
     public void doForward(String path) throws IOException {
 283  4
         if (responseDelegate.isResponseCommitted()) {
 284  1
             doInclude(path);
 285  1
             return;
 286  
         }
 287  
 
 288  
         try {
 289  3
             PortletRequestDispatcher rd = getPortletContext()
 290  
                     .getRequestDispatcher(path);
 291  
 
 292  3
             if (rd == null) {
 293  1
                 throw new IOException(
 294  
                         "No portlet request dispatcher returned for path '"
 295  
                                 + path + "'");
 296  
             }
 297  
 
 298  2
             rd.forward(request, response);
 299  1
         } catch (PortletException e) {
 300  1
             throw new IOException("PortletException while including path '"
 301  
                     + path + "'.", e);
 302  1
         }
 303  1
     }
 304  
 
 305  
     /** {@inheritDoc} */
 306  
     public void doInclude(String path) throws IOException {
 307  
         try {
 308  4
             PortletRequestDispatcher rd = getPortletContext()
 309  
                     .getRequestDispatcher(path);
 310  
 
 311  4
             if (rd == null) {
 312  1
                 throw new IOException(
 313  
                         "No portlet request dispatcher returned for path '"
 314  
                                 + path + "'");
 315  
             }
 316  
 
 317  3
             rd.include(request, response);
 318  1
         } catch (PortletException e) {
 319  1
             throw new IOException("PortletException while including path '"
 320  
                     + path + "'.", e);
 321  2
         }
 322  2
     }
 323  
 
 324  
     @Override
 325  
     public Map<String, Object> getContext(String scope) {
 326  0
         if(REQUEST_SCOPE.equals(scope)){
 327  0
             return getRequestScope();
 328  0
         }else if("session".equals(scope)){
 329  0
             return getSessionScope();
 330  0
         }else if("portletSession".equals(scope)){
 331  0
             return getPortletSessionScope();
 332  0
         }else if(APPLICATION_SCOPE.equals(scope)){
 333  0
             return getApplicationScope();
 334  
         }
 335  0
         throw new IllegalArgumentException(scope + " does not exist. Call getAvailableScopes() first to check.");
 336  
     }
 337  
 }