[Yanel-commits] rev 28112 - in public/yanel/trunk/src: core/java/org/wyona/yanel/core/util webapp/WEB-INF webapp/src/java/org/wyona/yanel/servlet

simon at wyona.com simon at wyona.com
Thu Oct 25 11:31:23 CEST 2007


Author: simon
Date: 2007-10-25 11:31:23 +0200 (Thu, 25 Oct 2007)
New Revision: 28112

Modified:
   public/yanel/trunk/src/core/java/org/wyona/yanel/core/util/DateUtil.java
   public/yanel/trunk/src/webapp/WEB-INF/web.xml
   public/yanel/trunk/src/webapp/src/java/org/wyona/yanel/servlet/YanelServlet.java
Log:
fixes bug#5611 thanks to josias

Modified: public/yanel/trunk/src/core/java/org/wyona/yanel/core/util/DateUtil.java
===================================================================
--- public/yanel/trunk/src/core/java/org/wyona/yanel/core/util/DateUtil.java	2007-10-25 09:31:18 UTC (rev 28111)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/util/DateUtil.java	2007-10-25 09:31:23 UTC (rev 28112)
@@ -4,6 +4,7 @@
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.Date;
+import java.util.TimeZone;
 
 /**
  * This class provides some static methods for date conversion, in order
@@ -11,6 +12,7 @@
  */
 public class DateUtil {
     protected static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
+    protected static DateFormat rfc822DateFormat = new SimpleDateFormat("EEE, dd MMM yyyy kk:mm:ss zzz");
     
     public static synchronized String format(Date date) {
         return dateFormat.format(date);
@@ -19,5 +21,19 @@
     public static synchronized Date parse(String dateString) throws ParseException {
         return dateFormat.parse(dateString);
     }
+    
+    public static synchronized String formatRFC822(Date date) {
+        return rfc822DateFormat.format(date);
+    }
+    
+    public static synchronized Date parseRFC822(String dateString) throws ParseException {
+        return rfc822DateFormat.parse(dateString);
+    }
+    
+    public static synchronized String formatRFC822GMT(Date date) {
+        DateFormat format = (DateFormat)rfc822DateFormat.clone();
+        format.setTimeZone(TimeZone.getTimeZone("GMT"));
+        return format.format(date);
+    }
 
 }
\ No newline at end of file

Modified: public/yanel/trunk/src/webapp/WEB-INF/web.xml
===================================================================
--- public/yanel/trunk/src/webapp/WEB-INF/web.xml	2007-10-25 09:31:18 UTC (rev 28111)
+++ public/yanel/trunk/src/webapp/WEB-INF/web.xml	2007-10-25 09:31:23 UTC (rev 28112)
@@ -38,6 +38,12 @@
      <param-value>on</param-value>
      <!--<param-value>off</param-value>-->
     </init-param>
+
+    <!-- Allow client-side caching of static htdocs content. Specify value in hours. -->
+    <init-param>
+     <param-name>static-content-cache-expires</param-name>
+     <param-value>2</param-value>
+    </init-param>
   </servlet>
 
   <servlet-mapping>

Modified: public/yanel/trunk/src/webapp/src/java/org/wyona/yanel/servlet/YanelServlet.java
===================================================================
--- public/yanel/trunk/src/webapp/src/java/org/wyona/yanel/servlet/YanelServlet.java	2007-10-25 09:31:18 UTC (rev 28111)
+++ public/yanel/trunk/src/webapp/src/java/org/wyona/yanel/servlet/YanelServlet.java	2007-10-25 09:31:23 UTC (rev 28112)
@@ -12,6 +12,7 @@
 import java.io.PrintWriter;
 import java.io.Writer;
 import java.net.URL;
+import java.util.Calendar;
 import java.util.Enumeration;
 import java.util.HashMap;
 
@@ -123,6 +124,7 @@
     private String toolbarMasterSwitch = "off";
     private String reservedPrefix;
     private String servletContextRealPath;
+    private int cacheExpires = 0;
     
     public static final String DEFAULT_ENCODING = "UTF-8";
 
@@ -151,6 +153,10 @@
             sslPort = config.getInitParameter("ssl-port");
             toolbarMasterSwitch = config.getInitParameter("toolbar-master-switch");
             reservedPrefix = yanel.getReservedPrefix();
+            String expires = config.getInitParameter("static-content-cache-expires");
+            if (expires != null) {
+                this.cacheExpires = Integer.parseInt(expires);
+            }
         } catch (Exception e) {
             log.error(e);
             throw new ServletException(e.getMessage(), e);
@@ -2145,6 +2151,10 @@
                     while ((bytesRead = in.read(buffer)) != -1) {
                         out.write(buffer, 0, bytesRead);
                     }
+                    // allow client-side caching:
+                    if (cacheExpires != 0) {
+                        setExpiresHeader(response, cacheExpires);
+                    }
                     return;
                 } else {
                     log.error("No such file or directory: " + resourceFile);
@@ -2170,6 +2180,10 @@
                 while ((bytesRead = in.read(buffer)) != -1) {
                     out.write(buffer, 0, bytesRead);
                 }
+                // allow client-side caching:
+                if (cacheExpires != 0) {
+                    setExpiresHeader(response, cacheExpires);
+                }
                 return;
             } else {
                 log.error("No such file or directory: " + globalFile);
@@ -2178,6 +2192,13 @@
             }
         }
     }
+    
+    private void setExpiresHeader(HttpServletResponse response, int hours) {
+        Calendar calendar = Calendar.getInstance();
+        calendar.add(Calendar.HOUR_OF_DAY, hours);
+        String expires = DateUtil.formatRFC822GMT(calendar.getTime());
+        response.setHeader("Expires", expires);
+    }
 
     /**
      *
@@ -2194,7 +2215,7 @@
             } else {
                 // try to guess if we have to set the default encoding
                 String mimeType = view.getMimeType();
-                if (mimeType.startsWith("text") || 
+                if (mimeType != null && mimeType.startsWith("text") || 
                     mimeType.equals("application/xml") || 
                     mimeType.equals("application/xhtml+xml") || 
                     mimeType.equals("application/atom+xml") || 



More information about the Yanel-commits mailing list