View Javadoc

1   /*
2    * Copyright 2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    * 
8    * http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package org.apache.portals.bridges.jsf;
17  
18  import java.io.IOException;
19  import java.util.Locale;
20  
21  import javax.faces.FacesException;
22  import javax.faces.application.ViewHandler;
23  import javax.faces.component.UIViewRoot;
24  import javax.faces.context.FacesContext;
25  
26  import javax.portlet.PortletURL;
27  import javax.portlet.RenderResponse;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  
32  /***
33   * <p>
34   * View handler for JSF portlet bridge.
35   * </p>
36   * 
37   * @author <a href="mailto:dlestrat@apache.org">David Le Strat </a>
38   */
39  public class PortletViewHandlerImpl extends ViewHandler
40  {
41      /*** The Log instance for this class. */
42      private static final Log log = LogFactory.getLog(PortletViewHandlerImpl.class);
43  
44      /*** The ViewHandler. */
45      private ViewHandler handler;
46  
47      /***
48       * <p>
49       * Construct a new <code>ViewHandler</code> instance that delegates
50       * non-portlet-specific behavior to the specified implementation.
51       * 
52       * @param handler The <code>ViewHandler</code> instance
53       */
54      public PortletViewHandlerImpl(ViewHandler handler)
55      {
56          if (log.isInfoEnabled())
57          {
58              log.info("Delegating to " + handler + "");
59          }
60          this.handler = handler;
61      }
62  
63      /***
64       * @see javax.faces.application.ViewHandler#calculateLocale(javax.faces.context.FacesContext)
65       */
66      public Locale calculateLocale(FacesContext facesContext)
67      {
68          return handler.calculateLocale(facesContext);
69      }
70  
71      /***
72       * @see javax.faces.application.ViewHandler#calculateRenderKitId(javax.faces.context.FacesContext)
73       */
74      public String calculateRenderKitId(FacesContext facesContext)
75      {
76          return handler.calculateRenderKitId(facesContext);
77      }
78  
79      /***
80       * @see javax.faces.application.ViewHandler#createView(javax.faces.context.FacesContext,
81       *      java.lang.String)
82       */
83      public UIViewRoot createView(FacesContext facesContext, String viewId)
84      {        
85          UIViewRoot root = handler.createView(facesContext, viewId);
86          if (root != null)
87          {
88              facesContext.setViewRoot(root);
89          }
90          
91          return root;
92      }
93  
94      /***
95       * @see javax.faces.application.ViewHandler#getActionURL(javax.faces.context.FacesContext,
96       *      java.lang.String)
97       */
98      public String getActionURL(FacesContext facesContext, String viewId)
99      {
100         Object response = facesContext.getExternalContext().getResponse();
101         if (!(response instanceof RenderResponse))
102         {
103             throw new IllegalStateException("Must be a RenderResponse");
104         }
105         RenderResponse renderResponse = (RenderResponse) response;
106         PortletURL actionURL = renderResponse.createActionURL();
107         return (actionURL.toString());
108     }
109 
110     /***
111      * @see javax.faces.application.ViewHandler#getResourceURL(javax.faces.context.FacesContext,
112      *      java.lang.String)
113      */
114     public String getResourceURL(FacesContext facesContext, String path)
115     {
116         if (path.length() > 0 && path.charAt(0) == '/')
117         {
118             return facesContext.getExternalContext().getRequestContextPath() + path;
119         }
120         else
121         {
122             return facesContext.getExternalContext().getRequestContextPath() + "/" + path;
123         }
124     }
125 
126     /***
127      * @see javax.faces.application.ViewHandler#renderView(javax.faces.context.FacesContext,
128      *      javax.faces.component.UIViewRoot)
129      */
130     public void renderView(FacesContext facesContext, UIViewRoot viewToRender) throws IOException, FacesException
131     {
132         handler.renderView(facesContext, viewToRender);
133     }
134 
135     /***
136      * @see javax.faces.application.ViewHandler#restoreView(javax.faces.context.FacesContext,
137      *      java.lang.String)
138      */
139     public UIViewRoot restoreView(FacesContext facesContext, String viewId)
140     {
141         UIViewRoot root = handler.restoreView(facesContext, viewId);
142         if (root != null)
143         {
144             facesContext.setViewRoot(root);
145         }
146         return root;
147     }
148 
149     /***
150      * @see javax.faces.application.ViewHandler#writeState(javax.faces.context.FacesContext)
151      */
152     public void writeState(FacesContext facesContext) throws IOException
153     {
154         handler.writeState(facesContext);
155     }
156 
157 }