Coverage Report - org.apache.myfaces.context.portlet.PortletExternalContextImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
PortletExternalContextImpl
0%
0/118
0%
0/36
0
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements.  See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership.  The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License.  You may obtain a copy of the License at
 9  
  *
 10  
  *   http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied.  See the License for the
 16  
  * specific language governing permissions and limitations
 17  
  * under the License.
 18  
  */
 19  
 package org.apache.myfaces.context.portlet;
 20  
 
 21  
 import java.io.IOException;
 22  
 import java.io.InputStream;
 23  
 import java.net.MalformedURLException;
 24  
 import java.net.URL;
 25  
 import java.security.Principal;
 26  
 import java.util.Collections;
 27  
 import java.util.Iterator;
 28  
 import java.util.Locale;
 29  
 import java.util.Map;
 30  
 import java.util.Set;
 31  
 
 32  
 import javax.faces.FacesException;
 33  
 import javax.faces.context.ExternalContext;
 34  
 import javax.portlet.ActionRequest;
 35  
 import javax.portlet.ActionResponse;
 36  
 import javax.portlet.PortletContext;
 37  
 import javax.portlet.PortletException;
 38  
 import javax.portlet.PortletRequest;
 39  
 import javax.portlet.PortletRequestDispatcher;
 40  
 import javax.portlet.PortletResponse;
 41  
 import javax.portlet.RenderRequest;
 42  
 import javax.portlet.RenderResponse;
 43  
 
 44  
 import org.apache.myfaces.context.ReleaseableExternalContext;
 45  
 import org.apache.myfaces.util.EnumerationIterator;
 46  
 
 47  
 /**
 48  
  * An ExternalContext implementation for JSF applications that run inside a Portlet.
 49  
  * 
 50  
  * @deprecated Replaced by jsr 301 portlet bridge. 
 51  
  * @author Stan Silvert (latest modification by $Author: lu4242 $)
 52  
  * @version $Revision: 827879 $ $Date: 2009-10-20 22:10:03 -0500 (Tue, 20 Oct 2009) $
 53  
  */
 54  
 public class PortletExternalContextImpl extends ExternalContext implements ReleaseableExternalContext
 55  
 {
 56  
 
 57  0
     private static final String INIT_PARAMETER_MAP_ATTRIBUTE = InitParameterMap.class.getName();
 58  
 
 59  
     PortletContext _portletContext;
 60  
     PortletRequest _portletRequest;
 61  
     PortletResponse _portletResponse;
 62  
 
 63  
     private Map<String, Object> _applicationMap;
 64  
     private Map<String, Object> _sessionMap;
 65  
     private Map<String, Object> _requestMap;
 66  
     private Map<String, String> _requestParameterMap;
 67  
     private Map<String, String[]> _requestParameterValuesMap;
 68  
     private Map<String, String> _requestHeaderMap;
 69  
     private Map<String, String[]> _requestHeaderValuesMap;
 70  
     private Map<String, String> _initParameterMap;
 71  
 
 72  
     private ActionRequest _actionRequest;
 73  
 
 74  
     /** Creates a new instance of PortletFacesContextImpl */
 75  
     public PortletExternalContextImpl(PortletContext portletContext, PortletRequest portletRequest,
 76  
             PortletResponse portletResponse)
 77  0
     {
 78  0
         _portletContext = portletContext;
 79  0
         _portletRequest = portletRequest;
 80  0
         _portletResponse = portletResponse;
 81  0
         _actionRequest =  isActionRequest(portletRequest) ? (ActionRequest)portletRequest : null;
 82  0
     }
 83  
 
 84  
     public void dispatch(String path) throws IOException
 85  
     {
 86  0
         if (_actionRequest != null)
 87  
         { // dispatch only allowed for RenderRequest
 88  0
             String msg = "Can not call dispatch() during a portlet ActionRequest";
 89  0
             throw new UnsupportedOperationException(msg);
 90  
         }
 91  
 
 92  0
         PortletRequestDispatcher requestDispatcher
 93  
             = _portletContext.getRequestDispatcher(path); //TODO: figure out why I need named dispatcher
 94  
         try
 95  
         {
 96  0
             requestDispatcher.include((RenderRequest) _portletRequest, (RenderResponse) _portletResponse);
 97  
         }
 98  0
         catch (PortletException e)
 99  
         {
 100  0
             if (e.getMessage() != null)
 101  
             {
 102  0
                 throw new FacesException(e.getMessage(), e);
 103  
             }
 104  0
             throw new FacesException(e);            
 105  0
         }
 106  0
     }
 107  
 
 108  
     public String encodeActionURL(String url)
 109  
     {
 110  0
         checkNull(url, "url");
 111  0
         return _portletResponse.encodeURL(url);
 112  
     }
 113  
 
 114  
     public String encodeNamespace(String name)
 115  
     {
 116  0
         if (_actionRequest != null)
 117  
         {
 118  
             // encodeNamespace only allowed for RenderRequest
 119  0
             throw new UnsupportedOperationException("Can not call encodeNamespace() during a portlet ActionRequest");
 120  
         }
 121  
 
 122  
         // we render out the name and then the namespace as
 123  
         // e.g. for JSF-ids, it is important to keep the _id prefix
 124  
         // to know that id creation has happened automatically
 125  0
         return name + ((RenderResponse) _portletResponse).getNamespace();
 126  
     }
 127  
 
 128  
     @Override
 129  
     public String encodeResourceURL(String url)
 130  
     {
 131  0
         checkNull(url, "url");
 132  0
         return _portletResponse.encodeURL(url);
 133  
     }
 134  
 
 135  
     @Override
 136  
     public Map<String, Object> getApplicationMap()
 137  
     {
 138  0
         if (_applicationMap == null)
 139  
         {
 140  0
             _applicationMap = new ApplicationMap(_portletContext);
 141  
         }
 142  0
         return _applicationMap;
 143  
     }
 144  
 
 145  
     @Override
 146  
     public String getAuthType()
 147  
     {
 148  0
         return _portletRequest.getAuthType();
 149  
     }
 150  
 
 151  
     @Override
 152  
     public Object getContext()
 153  
     {
 154  0
         return _portletContext;
 155  
     }
 156  
 
 157  
     @Override
 158  
     public String getInitParameter(String name)
 159  
     {
 160  0
         return _portletContext.getInitParameter(name);
 161  
     }
 162  
 
 163  
     @Override
 164  
     @SuppressWarnings("unchecked")
 165  
     public Map<String, String> getInitParameterMap()
 166  
     {
 167  0
         if (_initParameterMap == null)
 168  
         {
 169  
             // We cache it as an attribute in PortletContext itself (is this circular reference a problem?)
 170  0
             if ((_initParameterMap = (Map<String, String>) _portletContext.getAttribute(INIT_PARAMETER_MAP_ATTRIBUTE)) == null)
 171  
             {
 172  0
                 _initParameterMap = new InitParameterMap(_portletContext);
 173  0
                 _portletContext.setAttribute(INIT_PARAMETER_MAP_ATTRIBUTE, _initParameterMap);
 174  
             }
 175  
         }
 176  0
         return _initParameterMap;
 177  
     }
 178  
 
 179  
     @Override
 180  
     public String getRemoteUser()
 181  
     {
 182  0
         return _portletRequest.getRemoteUser();
 183  
     }
 184  
 
 185  
     @Override
 186  
     public Object getRequest()
 187  
     {
 188  0
         return _portletRequest;
 189  
     }
 190  
 
 191  
     @Override
 192  
     public String getRequestContentType()
 193  
     {
 194  0
         return null;
 195  
     }
 196  
 
 197  
     @Override
 198  
     public String getRequestContextPath()
 199  
     {
 200  0
         return _portletRequest.getContextPath();
 201  
     }
 202  
 
 203  
     @Override
 204  
     @SuppressWarnings("unchecked")
 205  
     public Map<String, Object> getRequestCookieMap()
 206  
     {
 207  0
         return Collections.EMPTY_MAP;
 208  
     }
 209  
 
 210  
     @Override
 211  
     public Map<String, String> getRequestHeaderMap()
 212  
     {
 213  0
         if (_requestHeaderMap == null)
 214  
         {
 215  0
             _requestHeaderMap = new RequestHeaderMap(_portletRequest);
 216  
         }
 217  0
         return _requestHeaderMap;
 218  
     }
 219  
 
 220  
     @Override
 221  
     public Map<String, String[]> getRequestHeaderValuesMap()
 222  
     {
 223  0
         if (_requestHeaderValuesMap == null)
 224  
         {
 225  0
             _requestHeaderValuesMap = new RequestHeaderValuesMap(_portletRequest);
 226  
         }
 227  0
         return _requestHeaderValuesMap;
 228  
     }
 229  
 
 230  
     @Override
 231  
     public Locale getRequestLocale()
 232  
     {
 233  0
         return _portletRequest.getLocale();
 234  
     }
 235  
 
 236  
     @SuppressWarnings("unchecked")
 237  
     @Override
 238  
     public Iterator<Locale> getRequestLocales()
 239  
     {
 240  0
         return new EnumerationIterator(_portletRequest.getLocales());
 241  
     }
 242  
 
 243  
     @Override
 244  
     public Map<String, Object> getRequestMap()
 245  
     {
 246  0
         if (_requestMap == null)
 247  
         {
 248  0
             _requestMap = new RequestMap(_portletRequest);
 249  
         }
 250  0
         return _requestMap;
 251  
     }
 252  
 
 253  
     @Override
 254  
     public Map<String, String> getRequestParameterMap()
 255  
     {
 256  0
         if (_requestParameterMap == null)
 257  
         {
 258  0
             _requestParameterMap = new RequestParameterMap(_portletRequest);
 259  
         }
 260  0
         return _requestParameterMap;
 261  
     }
 262  
 
 263  
     @SuppressWarnings("unchecked")
 264  
     @Override
 265  
     public Iterator<String> getRequestParameterNames()
 266  
     {
 267  0
         return new EnumerationIterator(_portletRequest.getParameterNames());
 268  
     }
 269  
 
 270  
     @Override
 271  
     public Map<String, String[]> getRequestParameterValuesMap()
 272  
     {
 273  0
         if (_requestParameterValuesMap == null)
 274  
         {
 275  0
             _requestParameterValuesMap = new RequestParameterValuesMap(_portletRequest);
 276  
         }
 277  0
         return _requestParameterValuesMap;
 278  
     }
 279  
 
 280  
     @Override
 281  
     public String getRequestPathInfo()
 282  
     {
 283  0
         return null; // must return null
 284  
     }
 285  
 
 286  
     @Override
 287  
     public String getRequestServletPath()
 288  
     {
 289  0
         return null; // must return null
 290  
     }
 291  
 
 292  
     @Override
 293  
     public URL getResource(String path) throws MalformedURLException
 294  
     {
 295  0
         checkNull(path, "path");
 296  
 
 297  0
         return _portletContext.getResource(path);
 298  
     }
 299  
 
 300  
     @Override
 301  
     public InputStream getResourceAsStream(String path)
 302  
     {
 303  0
         checkNull(path, "path");
 304  
 
 305  0
         return _portletContext.getResourceAsStream(path);
 306  
     }
 307  
 
 308  
     @Override
 309  
     @SuppressWarnings("unchecked")
 310  
     public Set<String> getResourcePaths(String path)
 311  
     {
 312  0
         checkNull(path, "path");
 313  0
         return _portletContext.getResourcePaths(path);
 314  
     }
 315  
 
 316  
     @Override
 317  
     public Object getResponse()
 318  
     {
 319  0
         return _portletResponse;
 320  
     }
 321  
 
 322  
     @Override
 323  
     public String getResponseContentType()
 324  
     {
 325  0
         return null;
 326  
     }
 327  
 
 328  
     @Override
 329  
     public Object getSession(boolean create)
 330  
     {
 331  0
         return _portletRequest.getPortletSession(create);
 332  
     }
 333  
 
 334  
     @Override
 335  
     public Map<String, Object> getSessionMap()
 336  
     {
 337  0
         if (_sessionMap == null)
 338  
         {
 339  0
             _sessionMap = new SessionMap(_portletRequest);
 340  
         }
 341  0
         return _sessionMap;
 342  
     }
 343  
 
 344  
     @Override
 345  
     public Principal getUserPrincipal()
 346  
     {
 347  0
         return _portletRequest.getUserPrincipal();
 348  
     }
 349  
 
 350  
     @Override
 351  
     public boolean isUserInRole(String role)
 352  
     {
 353  0
         checkNull(role, "role");
 354  
 
 355  0
         return _portletRequest.isUserInRole(role);
 356  
     }
 357  
 
 358  
     @Override
 359  
     public void log(String message)
 360  
     {
 361  0
         checkNull(message, "message");
 362  
 
 363  0
         _portletContext.log(message);
 364  0
     }
 365  
 
 366  
     @Override
 367  
     public void log(String message, Throwable exception)
 368  
     {
 369  0
         checkNull(message, "message");
 370  0
         checkNull(exception, "exception");
 371  
 
 372  0
         _portletContext.log(message, exception);
 373  0
     }
 374  
 
 375  
     @Override
 376  
     public void redirect(String url) throws IOException
 377  
     {
 378  0
         if (_actionRequest instanceof ActionResponse)
 379  
         {
 380  0
             ((ActionResponse) _portletResponse).sendRedirect(url);
 381  
         }
 382  
         else
 383  
         {
 384  0
             throw new IllegalArgumentException("Only ActionResponse supported");
 385  
         }
 386  0
     }
 387  
 
 388  
     public void release()
 389  
     {
 390  0
         _portletContext = null;
 391  0
         _portletRequest = null;
 392  0
         _portletResponse = null;
 393  0
         _applicationMap = null;
 394  0
         _sessionMap = null;
 395  0
         _requestMap = null;
 396  0
         _requestParameterMap = null;
 397  0
         _requestParameterValuesMap = null;
 398  0
         _requestHeaderMap = null;
 399  0
         _requestHeaderValuesMap = null;
 400  0
         _initParameterMap = null;
 401  0
         _actionRequest = null;
 402  0
     }
 403  
 
 404  
     /**
 405  
      * @since JSF 1.2
 406  
      * @param request
 407  
      */
 408  
     @Override
 409  
     public void setRequest(java.lang.Object request)
 410  
     {
 411  0
         this._portletRequest = (PortletRequest) request;
 412  0
         this._actionRequest = isActionRequest(_portletRequest) ? (ActionRequest) request : null;
 413  0
     }
 414  
 
 415  
     /**
 416  
      * @since JSF 1.2
 417  
      * @param encoding
 418  
      * @throws java.io.UnsupportedEncodingException
 419  
      */
 420  
     public void setRequestCharacterEncoding(java.lang.String encoding)
 421  
         throws java.io.UnsupportedEncodingException{
 422  
       
 423  0
         if(_actionRequest != null)
 424  0
             _actionRequest.setCharacterEncoding(encoding);
 425  
         else
 426  0
             throw new UnsupportedOperationException("Can not set request character encoding to value '" + encoding
 427  
                     + "'. Request is not an action request");
 428  0
     }
 429  
 
 430  
     @Override
 431  
     public String getRequestCharacterEncoding()
 432  
     {
 433  0
         if(_actionRequest != null)
 434  0
             return _actionRequest.getCharacterEncoding();
 435  0
         throw new UnsupportedOperationException("Can not get request character encoding. Request is not an action request");
 436  
     }
 437  
     
 438  
     @Override
 439  
     public String getResponseCharacterEncoding()
 440  
     {
 441  0
         return null;
 442  
     }
 443  
     
 444  
     /**
 445  
      * @since JSF 1.2
 446  
      * @param response
 447  
      */
 448  
     @Override
 449  
     public void setResponse(java.lang.Object response)
 450  
     {
 451  0
         this._portletResponse = (PortletResponse) response;
 452  0
     }
 453  
 
 454  
     /**
 455  
      * @since JSF 1.2
 456  
      * @param encoding
 457  
      */
 458  
     @Override
 459  
     public void setResponseCharacterEncoding(java.lang.String encoding)
 460  
     {
 461  
         // nope!
 462  0
     }
 463  
 
 464  
     private void checkNull(Object o, String param)
 465  
     {
 466  0
         if (o == null)
 467  
         {
 468  0
             throw new NullPointerException(param + " can not be null.");
 469  
         }
 470  0
     }
 471  
     
 472  
     private boolean isActionRequest(PortletRequest portletRequest)
 473  
     {
 474  0
         return portletRequest instanceof ActionRequest;
 475  
     }
 476  
 
 477  
 }