View Javadoc

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.portlet;
20  
21  import java.io.IOException;
22  
23  import javax.faces.FacesException;
24  import javax.faces.application.Application;
25  import javax.faces.application.ViewHandler;
26  import javax.faces.application.ViewHandlerWrapper;
27  import javax.faces.component.UIViewRoot;
28  import javax.faces.context.FacesContext;
29  import javax.portlet.PortletRequest;
30  import javax.portlet.PortletURL;
31  import javax.portlet.RenderResponse;
32  
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  
36  /**
37   * TODO: test this portlet view handler in portlet environment
38   * 
39   * @author Mathias Broekelmann (latest modification by $Author: mbr $)
40   * @version $Revision: 517404 $ $Date: 2007-03-12 16:17:24 -0500 (Mon, 12 Mar 2007) $
41   */
42  public class PortletViewHandler extends ViewHandlerWrapper
43  {
44      private final ViewHandler _viewHandler;
45  
46      private static final Log log = LogFactory.getLog(PortletViewHandler.class);
47  
48      public PortletViewHandler(ViewHandler viewHandler)
49      {
50          _viewHandler = viewHandler;
51      }
52  
53      @Override
54      protected ViewHandler getWrapped()
55      {
56          return _viewHandler;
57      }
58  
59      @Override
60      public UIViewRoot restoreView(FacesContext context, String viewId)
61      {
62          if (PortletUtil.isPortletRequest(context))
63          {
64              PortletRequest request = (PortletRequest) context.getExternalContext().getRequest();
65              String portletViewId = request.getParameter(MyFacesGenericPortlet.VIEW_ID);
66              Application application = context.getApplication();
67              ViewHandler applicationViewHandler = application.getViewHandler();
68              String renderKitId = applicationViewHandler.calculateRenderKitId(context);
69              UIViewRoot viewRoot = application.getStateManager().restoreView(context, portletViewId, renderKitId);
70              return viewRoot;
71          }
72          return super.restoreView(context, viewId);
73      }
74  
75      @Override
76      public UIViewRoot createView(FacesContext context, String viewId)
77      {
78          UIViewRoot viewRoot = super.createView(context, viewId);
79          if (PortletUtil.isPortletRequest(context))
80          {
81              PortletRequest request = (PortletRequest) context.getExternalContext().getRequest();
82              viewRoot.setViewId(request.getParameter(MyFacesGenericPortlet.VIEW_ID));
83          }
84          return viewRoot;
85      }
86  
87      @Override
88      public String getActionURL(FacesContext context, String viewId)
89      {
90          if (PortletUtil.isRenderResponse(context))
91          {
92              RenderResponse response = (RenderResponse) context.getExternalContext().getResponse();
93              PortletURL url = response.createActionURL();
94              url.setParameter(MyFacesGenericPortlet.VIEW_ID, viewId);
95              return url.toString();
96          }
97          return super.getActionURL(context, viewId);
98      }
99  
100     @Override
101     public void renderView(FacesContext context, UIViewRoot viewToRender) throws IOException, FacesException
102     {
103         if (PortletUtil.isPortletRequest(context))
104         {
105             if (viewToRender.isRendered())
106             {
107                 if (log.isTraceEnabled())
108                     log.trace("It is a portlet request. Dispatching to view");
109                 context.getExternalContext().dispatch(viewToRender.getViewId());
110             }
111         }
112         else
113         {
114             super.renderView(context, viewToRender);
115         }
116     }
117 }