View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.engine.servlet;
18  
19  import java.util.TimeZone;
20  
21  import javax.servlet.Filter;
22  import javax.servlet.FilterChain;
23  import javax.servlet.FilterConfig;
24  import javax.servlet.ServletException;
25  import javax.servlet.ServletRequest;
26  import javax.servlet.ServletResponse;
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  
30  public class StaticResourceCachingFilter implements Filter
31  {
32      // constants
33  
34      private static final String HTTP_HEADER_EXPIRES = "Expires" ;
35      private static final String HTTP_HEADER_CACHE_CONTROL = "Cache-Control" ;
36      private static final String HTTP_HEADER_CACHE_MAX_AGE = "max-age" ;
37      private static final String HTTP_HEADER_CACHE_MAX_AGE_EQ = "max-age=" ;
38  
39      private static String PARAM_EXPIRES_HOURS = "ExpireHours";
40  
41      private static final TimeZone GMT_TIME_ZONE = TimeZone.getTimeZone( "GMT" );
42  
43  
44      // members
45      
46      private double expires_in_hours = 0.0;
47      private int max_age = 0;
48  
49      // constructor
50  
51      public StaticResourceCachingFilter()
52      {
53          super() ;
54      }
55  
56  
57      // protocol
58  
59      public void init( FilterConfig config )
60      {
61          try
62          {
63              expires_in_hours = Double.parseDouble( config.getInitParameter( PARAM_EXPIRES_HOURS ) );
64          }
65          catch ( NumberFormatException ex )
66          {
67              expires_in_hours = 0;
68          }
69          max_age = (int)(expires_in_hours * 60);
70      }
71      
72      public void doFilter( ServletRequest aRequest, ServletResponse aResponse, FilterChain chain )
73          throws java.io.IOException, ServletException
74      {
75          HttpServletRequest request = (HttpServletRequest)aRequest;
76          HttpServletResponse response = (HttpServletResponse)aResponse;
77          if ( max_age > 0 )
78          {
79              String cacheControlVal = HTTP_HEADER_CACHE_MAX_AGE_EQ + max_age;
80              response.setHeader( HTTP_HEADER_CACHE_CONTROL, cacheControlVal );
81          }
82          chain.doFilter( request, response );
83      }
84  
85      public void destroy()
86      {
87      }
88  
89  
90      /* unused (we're only doing Cache-Control max-age), but works for generating Expires header
91      private String createExpiresHeader( int expiresInHours )
92      {
93          SimpleDateFormat sdf = new SimpleDateFormat( "EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US );
94          sdf.setTimeZone( GMT_TIME_ZONE );
95          Calendar cal = Calendar.getInstance();
96          cal.add( Calendar.HOUR, expiresInHours );
97          long millis = cal.getTimeInMillis();
98          Date d = new Date( millis );
99          return sdf.format( d );
100     }
101      */
102 }