You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by bo...@apache.org on 2008/02/08 17:22:39 UTC
svn commit: r619929 -
/myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/servlet/ResourceServlet.java
Author: bommel
Date: Fri Feb 8 08:22:34 2008
New Revision: 619929
URL: http://svn.apache.org/viewvc?rev=619929&view=rev
Log:
(TOBAGO-618) Improved caching in ResourceServlet and allow configuration of copy buffer size in ResourceServlet
Modified:
myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/servlet/ResourceServlet.java
Modified: myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/servlet/ResourceServlet.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/servlet/ResourceServlet.java?rev=619929&r1=619928&r2=619929&view=diff
==============================================================================
--- myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/servlet/ResourceServlet.java (original)
+++ myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/servlet/ResourceServlet.java Fri Feb 8 08:22:34 2008
@@ -29,7 +29,7 @@
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
-import java.util.Date;
+import java.io.OutputStream;
/**
* <p><pre>
@@ -42,6 +42,12 @@
* <param-name>expires</param-name>
* <param-value>14400</param-value>
* </init-param>
+ * <init-param>
+ * <description>The value for the copy buffer size.
+ * Default is 4096.</description>
+ * <param-name>bufferSize</param-name>
+ * <param-value>4096</param-value>
+ * </init-param>
* </servlet>
* <servlet-mapping>
* <servlet-name>ResourceServlet</servlet-name>
@@ -60,19 +66,25 @@
private static final Log LOG = LogFactory.getLog(ResourceServlet.class);
- public static final String DOJO_RESOURCE_PREFIX
- = "org/apache/myfaces/tobago/renderkit/html/standard/standard/script/dojo/";
-
private Long expires;
+ private int bufferSize;
public void init(ServletConfig servletConfig) throws ServletException {
super.init(servletConfig);
String expiresString = servletConfig.getInitParameter("expires");
-
expires = null;
if (expiresString != null) {
try {
- expires = new Long(expiresString);
+ expires = new Long(expiresString) * 1000;
+ } catch (NumberFormatException e) {
+ LOG.error("Caught: " + e.getMessage(), e);
+ }
+ }
+ String bufferSizeString = servletConfig.getInitParameter("bufferSize");
+ bufferSize = 1024 * 4;
+ if (bufferSizeString != null) {
+ try {
+ bufferSize = Integer.parseInt(bufferSizeString);
} catch (NumberFormatException e) {
LOG.error("Caught: " + e.getMessage(), e);
}
@@ -80,7 +92,7 @@
}
@Override
- protected void service(
+ protected void doGet(
HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
@@ -88,21 +100,18 @@
String resource = requestURI.substring(
request.getContextPath().length() + 1); // todo: make it "stable"
- //if (resource.startsWith(DOJO_RESOURCE_PREFIX)) {
- // // Todo : dojo version
- // resource = "dojo-release-1.0.1/" + resource.substring(DOJO_RESOURCE_PREFIX.length());
- //}
if (expires != null) {
- response.setHeader("Cache-Control", "max-age=" + expires);
- response.setDateHeader("Expires", new Date().getTime() + (expires * 1000));
+ response.setDateHeader("Last-Modified", 0);
+ response.setHeader("Cache-Control", "Public");
+ response.setDateHeader("Expires", System.currentTimeMillis() + expires);
}
String contentType = MimeTypeUtils.getMimeTypeForFile(requestURI);
if (contentType != null) {
response.setContentType(contentType);
} else {
- LOG.warn("Unsupported file extension, will be ignored for security "
- + "reasons; resource='" + resource + "'");
+ LOG.warn("Unsupported file extension, will be ignored for security reasons; resource='"
+ + resource + "'");
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
}
InputStream inputStream = null;
@@ -110,12 +119,30 @@
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
inputStream = classLoader.getResourceAsStream(resource);
if (inputStream != null) {
- IOUtils.copy(inputStream, response.getOutputStream());
+ OutputStream outputStream = response.getOutputStream();
+ copy(inputStream, outputStream);
} else {
LOG.warn("Resource '" + resource + "' not found!");
}
} finally {
IOUtils.closeQuietly(inputStream);
+ }
+ }
+
+ @Override
+ protected long getLastModified(HttpServletRequest request) {
+ if (expires != null) {
+ return 0;
+ } else {
+ return super.getLastModified(request);
+ }
+ }
+
+ private void copy(InputStream inputStream, OutputStream outputStream) throws IOException {
+ byte[] buffer = new byte[bufferSize];
+ int count;
+ while (-1 != (count = inputStream.read(buffer))) {
+ outputStream.write(buffer, 0, count);
}
}
}