You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by im...@apache.org on 2006/11/16 00:38:22 UTC

svn commit: r475495 - /myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/renderkit/html/util/MyFacesResourceLoader.java

Author: imario
Date: Wed Nov 15 15:38:21 2006
New Revision: 475495

URL: http://svn.apache.org/viewvc?view=rev&rev=475495
Log:
TOMAHAWK-780: added content-length to served resources - if possible

Modified:
    myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/renderkit/html/util/MyFacesResourceLoader.java

Modified: myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/renderkit/html/util/MyFacesResourceLoader.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/renderkit/html/util/MyFacesResourceLoader.java?view=diff&rev=475495&r1=475494&r2=475495
==============================================================================
--- myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/renderkit/html/util/MyFacesResourceLoader.java (original)
+++ myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/renderkit/html/util/MyFacesResourceLoader.java Wed Nov 15 15:38:21 2006
@@ -33,6 +33,8 @@
 import java.util.Calendar;
 import java.util.Date;
 import java.util.ResourceBundle;
+import java.net.URL;
+import java.net.URLConnection;
 
 /**
  * A ResourceLoader capable of fetching resources from the classpath,
@@ -139,8 +141,11 @@
 
         try
         {
-            is = componentClass.getResourceAsStream(resource);
-            if (is == null)
+			// is = componentClass.getResourceAsStream(resource);
+			//if (is == null)
+
+			URL url = componentClass.getResource(resource);
+			if (url == null)
             {
                 response.sendError(HttpServletResponse.SC_NOT_FOUND, "Unable to find resource "
                         + resource + " for component " + component
@@ -152,7 +157,12 @@
             }
             else
             {
-                defineContentHeaders(request, response, resource);
+				URLConnection con = url.openConnection();
+				int contentLength = con.getContentLength();
+
+				is = con.getInputStream();
+
+				defineContentHeaders(request, response, resource, contentLength);
                 defineCaching(request, response, resource);
                 writeResource(request, response, is);
             }
@@ -164,7 +174,7 @@
         }
     }
 
-    /**
+	/**
      * Copy the content of the specified input stream to the servlet response.
      */
     protected void writeResource(HttpServletRequest request, HttpServletResponse response,
@@ -213,9 +223,14 @@
      * The mime-type output is determined by the resource filename suffix.
      */
     protected void defineContentHeaders(HttpServletRequest request, HttpServletResponse response,
-            String resource)
+										String resource, int contentLength)
     {
-        if (resource.endsWith(".js"))
+		if (contentLength > -1)
+		{
+			response.setContentLength(contentLength);
+		}
+		
+		if (resource.endsWith(".js"))
             response.setContentType(org.apache.myfaces.shared_tomahawk.renderkit.html.HTML.SCRIPT_TYPE_TEXT_JAVASCRIPT);
         else if (resource.endsWith(".css"))
             response.setContentType(org.apache.myfaces.shared_tomahawk.renderkit.html.HTML.STYLE_TYPE_TEXT_CSS);