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.util.Locale;
    21
    22  import javax.faces.FacesException;
    23  import javax.faces.application.ViewHandler;
    24  import javax.faces.component.UIViewRoot;
    25  import javax.faces.context.FacesContext;
    26
    27  import javax.portlet.PortletURL;
    28  import javax.portlet.RenderResponse;
    29
    30  import org.apache.commons.logging.Log;
    31  import org.apache.commons.logging.LogFactory;
    32
    33  /***
    34   * <p>
    35   * View handler for JSF portlet bridge.
    36   * </p>
    37   *
    38   * @author <a href="mailto:dlestrat@apache.org">David Le Strat </a>
    39   */
    40  public class PortletViewHandlerImpl extends ViewHandler
    41  {
    42      /*** The Log instance for this class. */
    43      private static final Log log = LogFactory.getLog(PortletViewHandlerImpl.class);
    44
    45      /*** The ViewHandler. */
    46      private ViewHandler handler;
    47
    48      /***
    49       * <p>
    50       * Construct a new <code>ViewHandler</code> instance that delegates
    51       * non-portlet-specific behavior to the specified implementation.
    52       *
    53       * @param handler The <code>ViewHandler</code> instance
    54       */
    55      public PortletViewHandlerImpl(ViewHandler handler)
    56      {
    57          if (log.isInfoEnabled())
    58          {
    59              log.info("Delegating to " + handler + "");
    60          }
    61          this.handler = handler;
    62      }
    63
    64      /***
    65       * @see javax.faces.application.ViewHandler#calculateLocale(javax.faces.context.FacesContext)
    66       */
    67      public Locale calculateLocale(FacesContext facesContext)
    68      {
    69          return handler.calculateLocale(facesContext);
    70      }
    71
    72      /***
    73       * @see javax.faces.application.ViewHandler#calculateRenderKitId(javax.faces.context.FacesContext)
    74       */
    75      public String calculateRenderKitId(FacesContext facesContext)
    76      {
    77          return handler.calculateRenderKitId(facesContext);
    78      }
    79
    80      /***
    81       * @see javax.faces.application.ViewHandler#createView(javax.faces.context.FacesContext,
    82       *      java.lang.String)
    83       */
    84      public UIViewRoot createView(FacesContext facesContext, String viewId)
    85      {
    86          PortletUIViewRoot portletViewRoot = null;
    87          // Create the regular UIViewRoot from the ViewHandler.
    88          UIViewRoot root = handler.createView( facesContext, viewId );
    89          if ( root != null )
    90          {
    91              if ( root instanceof PortletUIViewRoot )
    92              {
    93                portletViewRoot = ( PortletUIViewRoot )root;
    94              }
    95              else
    96              {
    97                // Create a PortletUIViewRoot that will namespace properly.
    98                portletViewRoot = new PortletUIViewRoot( root );
    99              }
    100             facesContext.setViewRoot( portletViewRoot );
    101         }
    102
    103         return portletViewRoot;
    104     }
    105
    106     /***
    107      * @see javax.faces.application.ViewHandler#getActionURL(javax.faces.context.FacesContext,
    108      *      java.lang.String)
    109      */
    110     public String getActionURL(FacesContext facesContext, String viewId)
    111     {
    112         Object response = facesContext.getExternalContext().getResponse();
    113         if (!(response instanceof RenderResponse))
    114         {
    115             throw new IllegalStateException("Must be a RenderResponse");
    116         }
    117         RenderResponse renderResponse = (RenderResponse) response;
    118         PortletURL actionURL = renderResponse.createActionURL();
    119         return (actionURL.toString());
    120     }
    121
    122     /***
    123      * @see javax.faces.application.ViewHandler#getResourceURL(javax.faces.context.FacesContext,
    124      *      java.lang.String)
    125      */
    126     public String getResourceURL(FacesContext facesContext, String path)
    127     {
    128         return handler.getResourceURL(facesContext, path);
    129     }
    130
    131     /***
    132      * @see javax.faces.application.ViewHandler#renderView(javax.faces.context.FacesContext,
    133      *      javax.faces.component.UIViewRoot)
    134      */
    135     public void renderView(FacesContext facesContext, UIViewRoot viewToRender) throws IOException, FacesException
    136     {
    137         handler.renderView(facesContext, viewToRender);
    138     }
    139
    140     /***
    141      * @see javax.faces.application.ViewHandler#restoreView(javax.faces.context.FacesContext,
    142      *      java.lang.String)
    143      */
    144     public UIViewRoot restoreView(FacesContext facesContext, String viewId)
    145     {
    146         PortletUIViewRoot portletViewRoot = null;
    147         // Restore the UIViewRoot from the ViewHandler.
    148         UIViewRoot root = handler.restoreView( facesContext, viewId );
    149         if ( root != null )
    150         {
    151             // Is the root is already an instance of PortletUIViewRoot?
    152             if ( root instanceof PortletUIViewRoot )
    153             {
    154                 portletViewRoot = ( PortletUIViewRoot )root;
    155             }
    156             else  // The root is not a PortletUIViewRoot.
    157             {
    158                 // Create a PortletViewRoot that will namespace properly.
    159                 portletViewRoot = new PortletUIViewRoot( root );
    160             }
    161             facesContext.setViewRoot( portletViewRoot );
    162         }
    163         return portletViewRoot;
    164     }
    165
    166     /***
    167      * @see javax.faces.application.ViewHandler#writeState(javax.faces.context.FacesContext)
    168      */
    169     public void writeState(FacesContext facesContext) throws IOException
    170     {
    171         handler.writeState(facesContext);
    172     }
    173
    174 }