You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lu...@apache.org on 2008/07/08 22:36:58 UTC

svn commit: r674942 - in /myfaces/tomahawk/trunk: core/src/main/java/org/apache/myfaces/tomahawk/util/ core/src/main/java/org/apache/myfaces/webapp/filter/ examples/simple/src/main/webapp/WEB-INF/

Author: lu4242
Date: Tue Jul  8 13:36:57 2008
New Revision: 674942

URL: http://svn.apache.org/viewvc?rev=674942&view=rev
Log:
MYFACES-434 MyFaces's Portlet enhancement (fix bad buffered response wrapping on TomahawkFacesContextWrapper)

Modified:
    myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/tomahawk/util/ExternalContextUtils.java
    myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/webapp/filter/ExtensionsFilter.java
    myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/webapp/filter/TomahawkFacesContextFactory.java
    myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/webapp/filter/TomahawkFacesContextWrapper.java
    myfaces/tomahawk/trunk/examples/simple/src/main/webapp/WEB-INF/web.xml

Modified: myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/tomahawk/util/ExternalContextUtils.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/tomahawk/util/ExternalContextUtils.java?rev=674942&r1=674941&r2=674942&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/tomahawk/util/ExternalContextUtils.java (original)
+++ myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/tomahawk/util/ExternalContextUtils.java Tue Jul  8 13:36:57 2008
@@ -154,16 +154,67 @@
         return RequestType.SERVLET;
     }
 
+    /**
+     * This method is used when a ExternalContext object is not available,
+     * like in TomahawkFacesContextFactory.  
+     * 
+     * @param config
+     * @param request
+     * @return
+     */
+    public static final RequestType getRequestType(Object config, Object request)
+    {
+        //Stuff is laid out strangely in this class in order to optimize
+        //performance.  We want to do as few instanceof's as possible so
+        //things are laid out according to the expected frequency of the
+        //various requests occurring.
+
+        if(_PORTLET_CONFIG_CLASS != null)
+        {
+            if (_PORTLET_CONFIG_CLASS.isInstance(config))
+            {
+                //We are inside of a portlet container
+                
+                if(_PORTLET_RENDER_REQUEST_CLASS.isInstance(request))
+                {
+                    return RequestType.RENDER;
+                }
+                
+                if(_PORTLET_RESOURCE_REQUEST_CLASS != null)
+                {
+                    if(_PORTLET_ACTION_REQUEST_CLASS.isInstance(request))
+                    {
+                        return RequestType.ACTION;
+                    }
+
+                    //We are in a JSR-286 container
+                    if(_PORTLET_RESOURCE_REQUEST_CLASS.isInstance(request))
+                    {
+                        return RequestType.RESOURCE;
+                    }
+                    
+                    return RequestType.EVENT;
+                }
+                
+                return RequestType.ACTION;
+            }
+        }
+        
+        return RequestType.SERVLET;
+    }
+
     private static final Log _LOG = LogFactory.getLog(ExternalContextUtils.class);
 
     private static final Class    _PORTLET_ACTION_REQUEST_CLASS;
     private static final Class _PORTLET_RENDER_REQUEST_CLASS;
     private static final Class _PORTLET_RESOURCE_REQUEST_CLASS; //Will be present in JSR-286 containers only
     private static final Class    _PORTLET_CONTEXT_CLASS;
+    private static final Class    _PORTLET_CONFIG_CLASS;
     
     static
     {
         Class context;
+        Class config;
         Class actionRequest;
         Class renderRequest;
         Class resourceRequest;
@@ -171,6 +222,7 @@
         {
             ClassLoader loader = Thread.currentThread().getContextClassLoader();
             context = loader.loadClass("javax.portlet.PortletContext");
+            config = loader.loadClass("javax.portlet.PortletConfig");
             actionRequest = loader.loadClass("javax.portlet.ActionRequest");
             renderRequest = loader.loadClass("javax.portlet.RenderRequest");
             
@@ -186,12 +238,14 @@
         catch (ClassNotFoundException e)
         {
             context = null;
+            config = null;
             actionRequest = null;
             renderRequest = null;
             resourceRequest = null;
         }
 
         _PORTLET_CONTEXT_CLASS = context;
+        _PORTLET_CONFIG_CLASS = config;
         _PORTLET_ACTION_REQUEST_CLASS = actionRequest;
         _PORTLET_RENDER_REQUEST_CLASS = renderRequest;
         _PORTLET_RESOURCE_REQUEST_CLASS = resourceRequest;

Modified: myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/webapp/filter/ExtensionsFilter.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/webapp/filter/ExtensionsFilter.java?rev=674942&r1=674941&r2=674942&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/webapp/filter/ExtensionsFilter.java (original)
+++ myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/webapp/filter/ExtensionsFilter.java Tue Jul  8 13:36:57 2008
@@ -226,6 +226,7 @@
             throw new ServletException(th);
         }
 
+        /*
         try
         {
             addResource.responseStarted();
@@ -291,8 +292,9 @@
         {
             addResource.responseFinished();         
         }
+        */
         
-        //chain.doFilter(extendedRequest, response);
+        chain.doFilter(extendedRequest, response);
     }
 
     public boolean isValidContentType(String contentType)

Modified: myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/webapp/filter/TomahawkFacesContextFactory.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/webapp/filter/TomahawkFacesContextFactory.java?rev=674942&r1=674941&r2=674942&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/webapp/filter/TomahawkFacesContextFactory.java (original)
+++ myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/webapp/filter/TomahawkFacesContextFactory.java Tue Jul  8 13:36:57 2008
@@ -18,11 +18,18 @@
  */
 package org.apache.myfaces.webapp.filter;
 
-import javax.faces.context.FacesContextFactory;
+import javax.faces.FacesException;
 import javax.faces.context.FacesContext;
+import javax.faces.context.FacesContextFactory;
 import javax.faces.lifecycle.Lifecycle;
-import javax.faces.FacesException;
-
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.commons.fileupload.FileUpload;
+import org.apache.myfaces.renderkit.html.util.AddResource;
+import org.apache.myfaces.renderkit.html.util.AddResourceFactory;
+import org.apache.myfaces.tomahawk.util.ExternalContextUtils;
 
 public class TomahawkFacesContextFactory extends FacesContextFactory {
 
@@ -33,6 +40,25 @@
     }
 
     public FacesContext getFacesContext(Object context, Object request, Object response, Lifecycle lifecycle) throws FacesException {
+        
+        if(!ExternalContextUtils.getRequestType(context, request).isPortlet())
+        {
+            //This is servlet world
+            //For handle buffered response we need to wrap response object here,
+            //so all response will be written and then on facesContext
+            //release() method write to the original response.
+            //This could not be done on TomahawkFacesContextWrapper
+            //constructor, because the delegate ExternalContext do
+            //calls like dispatch, forward and redirect, that requires
+            //the wrapped response instance to work properly.
+            AddResource addResource = AddResourceFactory.getInstance((HttpServletRequest)request,(ServletContext)context);
+            
+            if (addResource.requiresBuffer())
+            {
+                ExtensionsResponseWrapper extensionsResponseWrapper = new ExtensionsResponseWrapper((HttpServletResponse)response);
+                return new TomahawkFacesContextWrapper(delegate.getFacesContext(context, request, extensionsResponseWrapper, lifecycle));
+            }
+        }
         return new TomahawkFacesContextWrapper(delegate.getFacesContext(context, request, response, lifecycle));
     }
 }

Modified: myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/webapp/filter/TomahawkFacesContextWrapper.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/webapp/filter/TomahawkFacesContextWrapper.java?rev=674942&r1=674941&r2=674942&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/webapp/filter/TomahawkFacesContextWrapper.java (original)
+++ myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/webapp/filter/TomahawkFacesContextWrapper.java Tue Jul  8 13:36:57 2008
@@ -38,7 +38,6 @@
 import org.apache.myfaces.renderkit.html.util.AddResource;
 import org.apache.myfaces.renderkit.html.util.AddResourceFactory;
 import org.apache.myfaces.tomahawk.util.ExternalContextUtils;
-import org.apache.myfaces.webapp.filter.portlet.PortletExternalContextWrapper;
 import org.apache.myfaces.webapp.filter.servlet.ServletExternalContextWrapper;
 
 /**
@@ -54,7 +53,6 @@
 
         this.delegate = delegate;
         
-        //if(delegate.getExternalContext().getResponse() instanceof PortletResponse) {
         if(ExternalContextUtils.getRequestType(delegate.getExternalContext()).isPortlet()) {
             //todo do something here - with the multipart-wrapper. rest should be fine
             //javax.portlet.PortletRequest portletRequest = (javax.portlet.PortletRequest) delegate.getExternalContext().getRequest();
@@ -87,7 +85,13 @@
             HttpServletRequest extendedRequest = httpRequest;
             HttpServletResponse extendedResponse = httpResponse;
 
-            // For multipart/form-data requests
+            // For multipart/form-data requests we need to encapsulate
+            // the request using MultipartRequestWrapper. This could not be
+            // done on TomahawkFacesContextFactory.getFacesContext(...)
+            // because we need an ExternalContext instance to get
+            // the init params for the ExtensionsFilter and initialize
+            // MultipartRequestWrapper with the correct values.
+            
             boolean multipartContent = false;
             if (FileUpload.isMultipartContent(httpRequest)) {
                 multipartContent = true;
@@ -102,8 +106,12 @@
 
             if (addResource.requiresBuffer())
             {
-                extensionsResponseWrapper = new ExtensionsResponseWrapper(httpResponse);
-                extendedResponse = extensionsResponseWrapper;
+                //If the request requires buffer, this was already
+                //wrapped (on TomahawkFacesContextFactory.getFacesContext(...) ),
+                //but we need to save the wrapped response value 
+                //on a local variable to then reference it on release() 
+                //method and parse the old response.
+                extensionsResponseWrapper = (ExtensionsResponseWrapper) extendedResponse; 
             }
 
             externalContextDelegate = new ServletExternalContextWrapper(
@@ -239,9 +247,12 @@
                 HttpServletResponse servletResponse = extensionsResponseWrapper.getDelegate();
                 HttpServletRequest servletRequest = (HttpServletRequest) getExternalContext().getRequest();
 
+                String contentType = extensionsResponseWrapper.getContentType();
+                
                 // only parse HTML responses
-                if (extensionsResponseWrapper.getContentType() != null && isValidContentType(extensionsResponseWrapper.getContentType()))
+                if (contentType != null && isValidContentType(contentType))
                 {
+                    String oldResponse = extensionsResponseWrapper.toString();
                     addResource.parseResponse(servletRequest, extensionsResponseWrapper.toString(),
                             servletResponse);
 

Modified: myfaces/tomahawk/trunk/examples/simple/src/main/webapp/WEB-INF/web.xml
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/examples/simple/src/main/webapp/WEB-INF/web.xml?rev=674942&r1=674941&r2=674942&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/examples/simple/src/main/webapp/WEB-INF/web.xml (original)
+++ myfaces/tomahawk/trunk/examples/simple/src/main/webapp/WEB-INF/web.xml Tue Jul  8 13:36:57 2008
@@ -91,6 +91,7 @@
     </description>
     <param-name>org.apache.myfaces.ADD_RESOURCE_CLASS</param-name>
     <param-value>org.apache.myfaces.renderkit.html.util.DefaultAddResource</param-value>
+    <!--param-value>org.apache.myfaces.renderkit.html.util.NonBufferingAddResource</param-value-->
     <!--param-value>org.apache.myfaces.component.html.util.StreamingAddResource</param-value-->
   </context-param>