View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  package org.apache.portals.bridges.jsf;
    18
    19  import java.io.IOException;
    20  import java.io.InputStream;
    21  import java.net.MalformedURLException;
    22  import java.net.URL;
    23  import java.security.Principal;
    24  import java.util.Enumeration;
    25  import java.util.Iterator;
    26  import java.util.Locale;
    27  import java.util.Map;
    28  import java.util.Set;
    29
    30  import javax.faces.FacesException;
    31  import javax.faces.context.ExternalContext;
    32  import javax.portlet.ActionResponse;
    33  import javax.portlet.PortletContext;
    34  import javax.portlet.PortletException;
    35  import javax.portlet.PortletRequest;
    36  import javax.portlet.PortletRequestDispatcher;
    37  import javax.portlet.PortletResponse;
    38  import javax.portlet.RenderRequest;
    39  import javax.portlet.RenderResponse;
    40
    41  import org.apache.commons.logging.Log;
    42  import org.apache.commons.logging.LogFactory;
    43
    44  /***
    45   * <p>
    46   * JSF 1.0 PRD2, 6.1.1
    47   * </p>
    48   * <p>
    49   * See MyFaces project for servlet implementation.
    50   * </p>
    51   *
    52   * @author <a href="dlestrat@apache.org">David Le Strat </a>
    53   */
    54  public class PortletExternalContextImpl extends ExternalContext
    55  {
    56      private static final Log log = LogFactory.getLog(PortletExternalContextImpl.class);
    57
    58      /*** The init parameter map attribute. */
    59      private static final String INIT_PARAMETER_MAP_ATTRIBUTE = InitParameterMap.class.getName();
    60
    61      /*** The portlet context. */
    62      private PortletContext portletContext;
    63
    64      /*** The portlet request. */
    65      private PortletRequest portletRequest;
    66
    67      /*** The portlet response. */
    68      private PortletResponse portletResponse;
    69
    70      /*** The application map. */
    71      private Map applicationMap;
    72
    73      /*** The session map. */
    74      private Map sessionMap;
    75
    76      /*** The request map. */
    77      private Map requestMap;
    78
    79      /*** The request parameter map. */
    80      private Map requestParameterMap;
    81
    82      /*** The request parameter values map. */
    83      private Map requestParameterValuesMap;
    84
    85      /*** The request header map. */
    86      private Map requestHeaderMap;
    87
    88      /*** The request header values map. */
    89      private Map requestHeaderValuesMap;
    90
    91      /*** The request cookie map. */
    92      private Map requestCookieMap;
    93
    94      /*** The init parameter map. */
    95      private Map initParameterMap;
    96
    97      /*** The request path info. */
    98      private String requestPathInfo;
    99
    100     /*** The request servlet path. */
    101     private String requestServletPath;
    102
    103     /***
    104      * @param portletContext The {@link PortletContext}.
    105      * @param portletRequest The {@link PortletRequest}.
    106      * @param portletResponse The {@link PortletResponse}.
    107      */
    108     public PortletExternalContextImpl(PortletContext portletContext, PortletRequest portletRequest,
    109             PortletResponse portletResponse)
    110     {
    111         this.portletContext = portletContext;
    112         this.portletRequest = portletRequest;
    113         this.portletResponse = portletResponse;
    114         this.applicationMap = null;
    115         this.sessionMap = null;
    116         this.requestMap = null;
    117         this.requestParameterMap = null;
    118         this.requestParameterValuesMap = null;
    119         this.requestHeaderMap = null;
    120         this.requestHeaderValuesMap = null;
    121         this.requestCookieMap = null;
    122         this.initParameterMap = null;
    123         this.requestPathInfo = null;
    124         this.requestServletPath = null;
    125     }
    126
    127     /***
    128      * <p>
    129      * Reset the member variables.
    130      * </p>
    131      */
    132     public void release()
    133     {
    134         this.portletContext = null;
    135         this.portletRequest = null;
    136         this.portletResponse = null;
    137         this.applicationMap = null;
    138         this.sessionMap = null;
    139         this.requestMap = null;
    140         this.requestParameterMap = null;
    141         this.requestParameterValuesMap = null;
    142         this.requestHeaderMap = null;
    143         this.requestHeaderValuesMap = null;
    144         this.requestCookieMap = null;
    145         this.initParameterMap = null;
    146         this.requestPathInfo = null;
    147         this.requestServletPath = null;
    148     }
    149
    150     /***
    151      * @see javax.faces.context.ExternalContext#getSession(boolean)
    152      */
    153     public Object getSession(boolean create)
    154     {
    155         return this.portletRequest.getPortletSession(create);
    156     }
    157
    158     /***
    159      * @see javax.faces.context.ExternalContext#getContext()
    160      */
    161     public Object getContext()
    162     {
    163         return this.portletContext;
    164     }
    165
    166     /***
    167      * @see javax.faces.context.ExternalContext#getRequest()
    168      */
    169     public Object getRequest()
    170     {
    171         return this.portletRequest;
    172     }
    173
    174     /***
    175      * @see javax.faces.context.ExternalContext#getResponse()
    176      */
    177     public Object getResponse()
    178     {
    179         return this.portletResponse;
    180     }
    181
    182     /***
    183      * @see javax.faces.context.ExternalContext#getApplicationMap()
    184      */
    185     public Map getApplicationMap()
    186     {
    187         if (this.applicationMap == null)
    188         {
    189             this.applicationMap = new ApplicationMap(this.portletContext);
    190         }
    191         return this.applicationMap;
    192     }
    193
    194     /***
    195      * @see javax.faces.context.ExternalContext#getSessionMap()
    196      */
    197     public Map getSessionMap()
    198     {
    199         if (this.sessionMap == null)
    200         {
    201             this.sessionMap = new SessionMap(this.portletRequest);
    202         }
    203         return this.sessionMap;
    204     }
    205
    206     /***
    207      * @see javax.faces.context.ExternalContext#getRequestMap()
    208      */
    209     public Map getRequestMap()
    210     {
    211         if (this.requestMap == null)
    212         {
    213             this.requestMap = new RequestMap(this.portletRequest);
    214         }
    215         return this.requestMap;
    216     }
    217
    218     /***
    219      * @see javax.faces.context.ExternalContext#getRequestParameterMap()
    220      */
    221     public Map getRequestParameterMap()
    222     {
    223         if (this.requestParameterMap == null)
    224         {
    225             this.requestParameterMap = new RequestParameterMap(this.portletRequest);
    226         }
    227         return this.requestParameterMap;
    228     }
    229
    230     /***
    231      * @see javax.faces.context.ExternalContext#getRequestParameterValuesMap()
    232      */
    233     public Map getRequestParameterValuesMap()
    234     {
    235         if (this.requestParameterValuesMap == null)
    236         {
    237             this.requestParameterValuesMap = new RequestParameterValuesMap(this.portletRequest);
    238         }
    239         return this.requestParameterValuesMap;
    240     }
    241
    242     /***
    243      * @see javax.faces.context.ExternalContext#getRequestParameterNames()
    244      */
    245     public Iterator getRequestParameterNames()
    246     {
    247         final Enumeration names = this.portletRequest.getParameterNames();
    248         Iterator it = new Iterator()
    249         {
    250             public boolean hasNext()
    251             {
    252                 return names.hasMoreElements();
    253             }
    254
    255             public Object next()
    256             {
    257                 return names.nextElement();
    258             }
    259
    260             public void remove()
    261             {
    262                 throw new UnsupportedOperationException(this.getClass().getName() + " UnsupportedOperationException");
    263             }
    264         };
    265         return it;
    266     }
    267
    268     /***
    269      * @see javax.faces.context.ExternalContext#getRequestHeaderMap()
    270      */
    271     public Map getRequestHeaderMap()
    272     {
    273         if (this.requestHeaderMap == null)
    274         {
    275             requestHeaderMap = new RequestHeaderMap(this.portletRequest);
    276             // TODO Hack to fix issue with MyFaces 1.0.6
    277             //requestHeaderMap.put("Content-Type", portletRequest.getResponseContentType());
    278         }
    279         return requestHeaderMap;
    280     }
    281
    282     /***
    283      * @see javax.faces.context.ExternalContext#getRequestHeaderValuesMap()
    284      */
    285     public Map getRequestHeaderValuesMap()
    286     {
    287         if (this.requestHeaderValuesMap == null)
    288         {
    289             requestHeaderValuesMap = new RequestHeaderValuesMap(this.portletRequest);
    290         }
    291         return requestHeaderValuesMap;
    292     }
    293
    294     /***
    295      * @see javax.faces.context.ExternalContext#getRequestCookieMap()
    296      */
    297     public Map getRequestCookieMap()
    298     {
    299         return null;
    300     }
    301
    302     /***
    303      * @see javax.faces.context.ExternalContext#getRequestLocale()
    304      */
    305     public Locale getRequestLocale()
    306     {
    307         return this.portletRequest.getLocale();
    308     }
    309
    310     /***
    311      * @see javax.faces.context.ExternalContext#getRequestPathInfo()
    312      */
    313     public String getRequestPathInfo()
    314     {
    315         return null;
    316     }
    317
    318     /***
    319      * @see javax.faces.context.ExternalContext#getRequestContextPath()
    320      */
    321     public String getRequestContextPath()
    322     {
    323         return this.portletRequest.getContextPath();
    324     }
    325
    326     /***
    327      * @see javax.faces.context.ExternalContext#getInitParameter(java.lang.String)
    328      */
    329     public String getInitParameter(String s)
    330     {
    331         return this.portletContext.getInitParameter(s);
    332     }
    333
    334     /***
    335      * @see javax.faces.context.ExternalContext#getInitParameterMap()
    336      */
    337     public Map getInitParameterMap()
    338     {
    339         if (this.initParameterMap == null)
    340         {
    341             if ((this.initParameterMap = (Map) this.portletContext.getAttribute(INIT_PARAMETER_MAP_ATTRIBUTE)) == null)
    342             {
    343                 this.initParameterMap = new InitParameterMap(this.portletContext);
    344                 this.portletContext.setAttribute(INIT_PARAMETER_MAP_ATTRIBUTE, this.initParameterMap);
    345             }
    346         }
    347         return this.initParameterMap;
    348     }
    349
    350     /***
    351      * @see javax.faces.context.ExternalContext#getResourcePaths(java.lang.String)
    352      */
    353     public Set getResourcePaths(String s)
    354     {
    355         return this.portletContext.getResourcePaths(s);
    356     }
    357
    358     /***
    359      * @see javax.faces.context.ExternalContext#getResourceAsStream(java.lang.String)
    360      */
    361     public InputStream getResourceAsStream(String s)
    362     {
    363         return this.portletContext.getResourceAsStream(s);
    364     }
    365
    366     /***
    367      * @see javax.faces.context.ExternalContext#encodeActionURL(java.lang.String)
    368      */
    369     public String encodeActionURL(String s)
    370     {
    371         if (null != s)
    372         {
    373             if (s.startsWith("http") || (s.startsWith("/")))
    374             {
    375                 return this.portletResponse.encodeURL(s);
    376             }
    377         }
    378         return s;
    379     }
    380
    381     /***
    382      * @see javax.faces.context.ExternalContext#encodeResourceURL(java.lang.String)
    383      */
    384     public String encodeResourceURL(String s)
    385     {
    386         if (null != s)
    387         {
    388             if (s.startsWith("http") || (s.startsWith("/")))
    389             {
    390                 return this.portletResponse.encodeURL(s);
    391             }
    392         }
    393         return s;
    394     }
    395
    396     /***
    397      * @see javax.faces.context.ExternalContext#encodeNamespace(java.lang.String)
    398      */
    399     public String encodeNamespace(String pNamespace)
    400     {
    401        /* define locals */
    402        String vRetEncodedNamespace = null;
    403
    404        if (!(this.portletResponse instanceof RenderResponse))
    405        {
    406             throw new IllegalArgumentException("Only RenderResponse can be used to encode namespace");
    407        }
    408        else
    409        {
    410           vRetEncodedNamespace = ((RenderResponse)this.portletResponse).getNamespace()+pNamespace;
    411        }
    412
    413        return vRetEncodedNamespace;
    414      };
    415
    416     /***
    417      * @see javax.faces.context.ExternalContext#dispatch(java.lang.String)
    418      */
    419     public void dispatch(String requestURI) throws IOException, FacesException
    420     {
    421         if (!(this.portletResponse instanceof RenderResponse))
    422         {
    423             throw new IllegalArgumentException("Only RenderResponse can be dispatched");
    424         }
    425         if (!(this.portletRequest instanceof RenderRequest))
    426         {
    427             throw new IllegalArgumentException("Only RenderRequest can be dispatched");
    428         }
    429         PortletRequestDispatcher portletRequestDispatcher = this.portletContext.getRequestDispatcher(requestURI);
    430         try
    431         {
    432             portletRequestDispatcher
    433                     .include((RenderRequest) this.portletRequest, (RenderResponse) this.portletResponse);
    434         }
    435         catch (PortletException e)
    436         {
    437             if (e.getMessage() != null)
    438             {
    439                 throw new FacesException(e.getMessage(), e);
    440             }
    441             else
    442             {
    443                 throw new FacesException(e);
    444             }
    445         }
    446     }
    447
    448     /***
    449      * @see javax.faces.context.ExternalContext#getRequestServletPath()
    450      */
    451     public String getRequestServletPath()
    452     {
    453         return (String) this.portletRequest.getAttribute(FacesPortlet.REQUEST_SERVLET_PATH);
    454     }
    455
    456     /***
    457      * @see javax.faces.context.ExternalContext#getAuthType()
    458      */
    459     public String getAuthType()
    460     {
    461         return this.portletRequest.getAuthType();
    462     }
    463
    464     /***
    465      * @see javax.faces.context.ExternalContext#getRemoteUser()
    466      */
    467     public String getRemoteUser()
    468     {
    469         return this.portletRequest.getRemoteUser();
    470     }
    471
    472     /***
    473      * @see javax.faces.context.ExternalContext#isUserInRole(java.lang.String)
    474      */
    475     public boolean isUserInRole(String role)
    476     {
    477         return this.portletRequest.isUserInRole(role);
    478     }
    479
    480     /***
    481      * @see javax.faces.context.ExternalContext#getUserPrincipal()
    482      */
    483     public Principal getUserPrincipal()
    484     {
    485         return this.portletRequest.getUserPrincipal();
    486     }
    487
    488     /***
    489      * @see javax.faces.context.ExternalContext#log(java.lang.String)
    490      */
    491     public void log(String message)
    492     {
    493         this.portletContext.log(message);
    494     }
    495
    496     /***
    497      * @see javax.faces.context.ExternalContext#log(java.lang.String, java.lang.Throwable)
    498      */
    499     public void log(String message, Throwable t)
    500     {
    501         this.portletContext.log(message, t);
    502     }
    503
    504     /***
    505      * @see javax.faces.context.ExternalContext#redirect(java.lang.String)
    506      */
    507     public void redirect(String url) throws IOException
    508     {
    509         if (this.portletResponse instanceof ActionResponse)
    510         {
    511             ActionResponse r = (ActionResponse)this.portletResponse;
    512             r.sendRedirect(url);
    513         }
    514         else
    515             throw new IOException("Cannot redirect from render phase");
    516     }
    517
    518     /***
    519      * @see javax.faces.context.ExternalContext#getRequestLocales()
    520      */
    521     public Iterator getRequestLocales()
    522     {
    523         return new EnumerationIterator(this.portletRequest.getLocales());
    524     }
    525
    526     /***
    527      * @see javax.faces.context.ExternalContext#getResource(java.lang.String)
    528      */
    529     public URL getResource(String s) throws MalformedURLException
    530     {
    531         return this.portletContext.getResource(s);
    532     }
    533 }