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.webapp.filter;
20  
21  import javax.faces.FacesException;
22  import javax.faces.context.FacesContext;
23  import javax.faces.context.FacesContextFactory;
24  import javax.faces.lifecycle.Lifecycle;
25  import javax.servlet.ServletContext;
26  import javax.servlet.ServletRequest;
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  
30  import org.apache.myfaces.renderkit.html.util.AddResource;
31  import org.apache.myfaces.renderkit.html.util.AddResourceFactory;
32  import org.apache.myfaces.tomahawk.util.ExternalContextUtils;
33  
34  /**
35   * The objective of this factory is this:
36   * <ol>
37   * <li> Wrap a multipart request (used for t:inputFileUpload), 
38   *    so the request could be correctly decoded.</li>
39   * <li>If a buffered instance of AddResource is configured, 
40   *    ExtensionsFilter must buffer and add the resource reference 
41   *    to the head of jsf pages (for example when it is used 
42   *    DefaultAddResource)</li>
43   * </ol>
44   * 
45   * @since 1.1.7
46   * @author Martin Marinschek (latest modification by $Author: lu4242 $)
47   * @version $Revision: 697311 $ $Date: 2008-09-19 20:14:01 -0500 (Fri, 19 Sep 2008) $
48   */
49  public class TomahawkFacesContextFactory extends FacesContextFactory {
50  
51      /**
52       * Disable or enable this factory to wrap the request using 
53       * TomahawkFacesContextWrapper as an alternative to ExtensionsFilter.
54       * If ExtensionsFilter is configured, use of TomahawkFacesContextWrapper
55       * is skipped
56       */
57      public static final String DISABLE_TOMAHAWK_FACES_CONTEXT_WRAPPER = 
58          "org.apache.myfaces.DISABLE_TOMAHAWK_FACES_CONTEXT_WRAPPER";
59      
60      public static final boolean DISABLE_TOMAHAWK_FACES_CONTEXT_WRAPPER_DEFAULT = true;
61      
62      private FacesContextFactory delegate;
63  
64      public TomahawkFacesContextFactory(FacesContextFactory delegate) {
65          this.delegate = delegate;
66      }
67  
68      public FacesContext getFacesContext(Object context, Object request, Object response, Lifecycle lifecycle) throws FacesException {
69          
70          if(!ExternalContextUtils.getRequestType(context, request).isPortlet())
71          {
72              ServletRequest servletRequest = (ServletRequest) request;
73              ServletContext servletContext = (ServletContext) context;
74              // If no ExtensionsFilter wraps before the call, and 
75              //there is not a value indicating disabling the wrapping,
76              //use TomahawkFacesContextWrapper 
77              if (servletRequest.getAttribute(ExtensionsFilter.DOFILTER_CALLED) == null &&
78                      !getBooleanValue(servletContext.getInitParameter(
79                              DISABLE_TOMAHAWK_FACES_CONTEXT_WRAPPER),
80                              DISABLE_TOMAHAWK_FACES_CONTEXT_WRAPPER_DEFAULT))
81              {
82                  //This is servlet world
83                  //For handle buffered response we need to wrap response object here,
84                  //so all response will be written and then on facesContext
85                  //release() method write to the original response.
86                  //This could not be done on TomahawkFacesContextWrapper
87                  //constructor, because the delegate ExternalContext do
88                  //calls like dispatch, forward and redirect, that requires
89                  //the wrapped response instance to work properly.            
90                  AddResource addResource = AddResourceFactory.getInstance((HttpServletRequest)request,(ServletContext)context);
91                  
92                  if (addResource.requiresBuffer())
93                  {
94                      ExtensionsResponseWrapper extensionsResponseWrapper = new ExtensionsResponseWrapper((HttpServletResponse)response);
95                      return new TomahawkFacesContextWrapper(delegate.getFacesContext(context, request, extensionsResponseWrapper, lifecycle),
96                              extensionsResponseWrapper);
97                  }
98                  else
99                  {
100                     return new TomahawkFacesContextWrapper(delegate.getFacesContext(context, request, response, lifecycle));
101                 }
102             }
103             else
104             {
105                 //The wrapping was done by extensions filter, so 
106                 //TomahawkFacesContextWrapper is not needed
107                 return delegate.getFacesContext(context, request, response, lifecycle);
108             }
109         }
110         else
111         {
112             if (!PortletUtils.isDisabledTomahawkFacesContextWrapper(context))
113             {            
114                 return new TomahawkFacesContextWrapper(delegate.getFacesContext(context, request, response, lifecycle));
115             }
116         }    
117         return delegate.getFacesContext(context, request, response, lifecycle);
118     }
119     
120     private static boolean getBooleanValue(String initParameter, boolean defaultVal)
121     {
122         if(initParameter == null || initParameter.trim().length()==0)
123             return defaultVal;
124 
125         return (initParameter.equalsIgnoreCase("on") || initParameter.equals("1") || initParameter.equalsIgnoreCase("true"));
126     }    
127 }