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.lifecycle;
20  
21  import java.util.Map;
22  
23  import javax.el.ValueExpression;
24  import javax.faces.FacesException;
25  import javax.faces.application.ViewHandler;
26  import javax.faces.component.UIComponent;
27  import javax.faces.context.ExternalContext;
28  import javax.faces.context.FacesContext;
29  import javax.faces.render.ResponseStateManager;
30  
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  import org.apache.myfaces.shared_impl.renderkit.RendererUtils;
34  import org.apache.myfaces.shared_impl.util.Assert;
35  import org.apache.myfaces.shared_impl.util.RestoreStateUtils;
36  
37  /**
38   * @author Mathias Broekelmann (latest modification by $Author: lu4242 $)
39   * @version $Revision: 825575 $ $Date: 2009-10-15 12:18:20 -0500 (Thu, 15 Oct 2009) $
40   */
41  public class DefaultRestoreViewSupport implements RestoreViewSupport
42  {
43      private static final String JAVAX_SERVLET_INCLUDE_SERVLET_PATH = "javax.servlet.include.servlet_path";
44  
45      private static final String JAVAX_SERVLET_INCLUDE_PATH_INFO = "javax.servlet.include.path_info";
46      
47      /**
48       * Constant defined on javax.portlet.faces.Bridge class that helps to 
49       * define if the current request is a portlet request or not.
50       */
51      private static final String PORTLET_LIFECYCLE_PHASE = "javax.portlet.faces.phase";
52  
53      private final Log log = LogFactory.getLog(DefaultRestoreViewSupport.class);
54  
55      public void processComponentBinding(FacesContext facesContext, UIComponent component)
56      {
57          ValueExpression binding = component.getValueExpression("binding");
58          if (binding != null)
59          {
60              binding.setValue(facesContext.getELContext(), component);
61          }
62          
63          //This part is for make compatibility with t:aliasBean, because
64          //this components has its own code before and after binding is 
65          //set for child components.
66          RestoreStateUtils.recursivelyHandleComponentReferencesAndSetValid(facesContext,component);
67  
68          //The required behavior for the spec is call recursively this method
69          //for walk the component tree. 
70          //for (Iterator<UIComponent> iter = component.getFacetsAndChildren(); iter.hasNext();)
71          //{
72          //    processComponentBinding(facesContext, iter.next());
73          //}
74      }
75  
76      public String calculateViewId(FacesContext facesContext)
77      {
78          Assert.notNull(facesContext);
79          ExternalContext externalContext = facesContext.getExternalContext();
80          Map<String, Object> requestMap = externalContext.getRequestMap();
81  
82          String viewId = null;
83          boolean traceEnabled = log.isTraceEnabled();
84          
85          if (requestMap.containsKey(PORTLET_LIFECYCLE_PHASE))
86          {
87              viewId = (String) externalContext.getRequestPathInfo();
88          }
89          else
90          {
91              viewId = (String) requestMap.get(JAVAX_SERVLET_INCLUDE_PATH_INFO);
92              if (viewId != null)
93              {
94                  if (traceEnabled)
95                  {
96                      log.trace("Calculated viewId '" + viewId + "' from request param '" + JAVAX_SERVLET_INCLUDE_PATH_INFO
97                              + "'");
98                  }
99              }
100             else
101             {
102                 viewId = externalContext.getRequestPathInfo();
103                 if (viewId != null && traceEnabled)
104                 {
105                     log.trace("Calculated viewId '" + viewId + "' from request path info");
106                 }
107             }
108     
109             if (viewId == null)
110             {
111                 viewId = (String) requestMap.get(JAVAX_SERVLET_INCLUDE_SERVLET_PATH);
112                 if (viewId != null && traceEnabled)
113                 {
114                     log.trace("Calculated viewId '" + viewId + "' from request param '"
115                             + JAVAX_SERVLET_INCLUDE_SERVLET_PATH + "'");
116                 }
117             }
118         }
119         
120         if (viewId == null)
121         {
122             viewId = externalContext.getRequestServletPath();
123             if (viewId != null && traceEnabled)
124             {
125                 log.trace("Calculated viewId '" + viewId + "' from request servlet path");
126             }
127         }
128 
129         if (viewId == null)
130         {
131             throw new FacesException("Could not determine view id.");
132         }
133 
134         return viewId;
135     }
136 
137     public boolean isPostback(FacesContext facesContext)
138     {
139         ViewHandler viewHandler = facesContext.getApplication().getViewHandler();
140         String renderkitId = viewHandler.calculateRenderKitId(facesContext);
141         ResponseStateManager rsm = RendererUtils.getResponseStateManager(facesContext, renderkitId);
142         return rsm.isPostback(facesContext);
143     }
144 }