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 2012/11/16 19:59:57 UTC

svn commit: r1410541 - /myfaces/core/branches/2.0.x/impl/src/main/java/org/apache/myfaces/application/ResourceHandlerImpl.java

Author: lu4242
Date: Fri Nov 16 18:59:56 2012
New Revision: 1410541

URL: http://svn.apache.org/viewvc?rev=1410541&view=rev
Log:
MYFACES-3636 Add new Web Context Parameter to set Resource Buffer Size (thank to Jesse Collins for provide this patch)

Modified:
    myfaces/core/branches/2.0.x/impl/src/main/java/org/apache/myfaces/application/ResourceHandlerImpl.java

Modified: myfaces/core/branches/2.0.x/impl/src/main/java/org/apache/myfaces/application/ResourceHandlerImpl.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2.0.x/impl/src/main/java/org/apache/myfaces/application/ResourceHandlerImpl.java?rev=1410541&r1=1410540&r2=1410541&view=diff
==============================================================================
--- myfaces/core/branches/2.0.x/impl/src/main/java/org/apache/myfaces/application/ResourceHandlerImpl.java (original)
+++ myfaces/core/branches/2.0.x/impl/src/main/java/org/apache/myfaces/application/ResourceHandlerImpl.java Fri Nov 16 18:59:56 2012
@@ -67,8 +67,6 @@ public class ResourceHandlerImpl extends
     //private static final Log log = LogFactory.getLog(ResourceHandlerImpl.class);
     private static final Logger log = Logger.getLogger(ResourceHandlerImpl.class.getName());
 
-    private static final int _BUFFER_SIZE = 2048;
-    
     /**
      * Allow slash in the library name of a Resource. 
      */
@@ -78,7 +76,17 @@ public class ResourceHandlerImpl extends
             "org.apache.myfaces.STRICT_JSF_2_ALLOW_SLASH_LIBRARY_NAME";
     public static final boolean INIT_PARAM_STRICT_JSF_2_ALLOW_SLASH_LIBRARY_NAME_DEFAULT = false;
     
+    /**
+     * Define the default buffer size that is used between Resource.getInputStream() and 
+     * httpServletResponse.getOutputStream() when rendering resources using the default
+     * ResourceHandler.
+     */
+    @JSFWebConfigParam(since="2.1.10, 2.0.16", defaultValue="2048", group="resources")
+    public static final String INIT_PARAM_RESOURCE_BUFFER_SIZE = "org.apache.myfaces.RESOURCE_BUFFER_SIZE";
+    private static final int INIT_PARAM_RESOURCE_BUFFER_SIZE_DEFAULT = 2048;
+    
     private Boolean _allowSlashLibraryName;
+    private int _resourceBufferSize = -1;
 
     @Override
     public Resource createResource(String resourceName)
@@ -305,7 +313,8 @@ public class ResourceHandlerImpl extends
             // ServletResponseWrapper (like ResponseSwitch).
             // Since we are handling a resource, we can expect to get an 
             // HttpServletResponse.
-            Object response = facesContext.getExternalContext().getResponse();
+            ExternalContext extContext = facesContext.getExternalContext();
+            Object response = extContext.getResponse();
             HttpServletResponse httpServletResponse = ExternalContextUtils.getHttpServletResponse(response);
             if (httpServletResponse == null)
             {
@@ -379,12 +388,16 @@ public class ResourceHandlerImpl extends
                 httpServletResponse.setHeader(entry.getKey(), entry.getValue());
             }
     
+            // Sets the preferred buffer size for the body of the response
+            extContext.setResponseBufferSize(this.getResourceBufferSize());
+            
             //serve up the bytes (taken from trinidad ResourceServlet)
             try
             {
                 InputStream in = resource.getInputStream();
                 OutputStream out = httpServletResponse.getOutputStream();
-                byte[] buffer = new byte[_BUFFER_SIZE];
+                //byte[] buffer = new byte[_BUFFER_SIZE];
+                byte[] buffer = new byte[this.getResourceBufferSize()];
     
                 try
                 {
@@ -681,4 +694,16 @@ public class ResourceHandlerImpl extends
         return _allowSlashLibraryName;
     }
 
+    protected int getResourceBufferSize()
+    {
+        if (_resourceBufferSize == -1)
+        {
+            _resourceBufferSize = WebConfigParamUtils.getIntegerInitParameter(
+                FacesContext.getCurrentInstance().getExternalContext(),
+                INIT_PARAM_RESOURCE_BUFFER_SIZE,
+                INIT_PARAM_RESOURCE_BUFFER_SIZE_DEFAULT);
+        }
+        return _resourceBufferSize;
+    }
+
 }