1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.filters.header;
24  
25  import com.liferay.portal.kernel.util.GetterUtil;
26  import com.liferay.portal.kernel.util.Validator;
27  
28  import java.io.IOException;
29  
30  import java.text.DateFormat;
31  import java.text.SimpleDateFormat;
32  
33  import java.util.Calendar;
34  import java.util.Enumeration;
35  import java.util.GregorianCalendar;
36  import java.util.Locale;
37  import java.util.TimeZone;
38  
39  import javax.servlet.Filter;
40  import javax.servlet.FilterChain;
41  import javax.servlet.FilterConfig;
42  import javax.servlet.ServletException;
43  import javax.servlet.ServletRequest;
44  import javax.servlet.ServletResponse;
45  import javax.servlet.http.HttpServletResponse;
46  
47  /**
48   * <a href="HeaderFilter.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   *
52   */
53  public class HeaderFilter implements Filter {
54  
55      public void init(FilterConfig config) {
56          _config = config;
57          _timeZone = TimeZone.getTimeZone(_TIME_ZONE);
58      }
59  
60      public void doFilter(
61              ServletRequest req, ServletResponse res, FilterChain chain)
62          throws IOException, ServletException {
63  
64          HttpServletResponse httpRes = (HttpServletResponse)res;
65  
66          Enumeration enu = _config.getInitParameterNames();
67  
68          while (enu.hasMoreElements()) {
69              String name = (String)enu.nextElement();
70  
71              String value = _config.getInitParameter(name);
72  
73              if (name.equals(_EXPIRES) && Validator.isNumber(value)) {
74                  int seconds = GetterUtil.getInteger(value);
75  
76                  Calendar cal = new GregorianCalendar();
77  
78                  cal.add(Calendar.SECOND, seconds);
79  
80                  DateFormat dateFormat = new SimpleDateFormat(
81                      _DATE_FORMAT, Locale.US);
82  
83                  dateFormat.setTimeZone(_timeZone);
84  
85                  value = dateFormat.format(cal.getTime());
86              }
87  
88              httpRes.addHeader(name, value);
89          }
90  
91          chain.doFilter(req, res);
92      }
93  
94      public void destroy() {
95      }
96  
97      private static final String _DATE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss z";
98  
99      private static final String _EXPIRES = "Expires";
100 
101     private static final String _TIME_ZONE = "GMT";
102 
103     private FilterConfig _config;
104     private TimeZone _timeZone;
105 
106 }