1   /**
2    * Copyright (c) 2000-2008 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.portal.servlet.filters.header;
24  
25  import com.liferay.portal.kernel.servlet.BaseFilter;
26  import com.liferay.portal.kernel.util.GetterUtil;
27  import com.liferay.portal.kernel.util.Validator;
28  
29  import java.io.IOException;
30  
31  import java.text.DateFormat;
32  import java.text.SimpleDateFormat;
33  
34  import java.util.Calendar;
35  import java.util.Enumeration;
36  import java.util.GregorianCalendar;
37  import java.util.Locale;
38  import java.util.TimeZone;
39  
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   * @author Raymond Augé
52   *
53   */
54  public class HeaderFilter extends BaseFilter {
55  
56      public void init(FilterConfig config) throws ServletException {
57          super.init(config);
58  
59          _config = config;
60          _timeZone = TimeZone.getTimeZone(_TIME_ZONE);
61      }
62  
63      public void doFilter(
64              ServletRequest req, ServletResponse res, FilterChain chain)
65          throws IOException, ServletException {
66  
67          HttpServletResponse httpRes = (HttpServletResponse)res;
68  
69          Enumeration<String> enu = _config.getInitParameterNames();
70  
71          while (enu.hasMoreElements()) {
72              String name = enu.nextElement();
73  
74              String value = _config.getInitParameter(name);
75  
76              if (name.equals(_EXPIRES) && Validator.isNumber(value)) {
77                  int seconds = GetterUtil.getInteger(value);
78  
79                  Calendar cal = new GregorianCalendar();
80  
81                  cal.add(Calendar.SECOND, seconds);
82  
83                  DateFormat dateFormat = new SimpleDateFormat(
84                      _DATE_FORMAT, Locale.US);
85  
86                  dateFormat.setTimeZone(_timeZone);
87  
88                  value = dateFormat.format(cal.getTime());
89              }
90  
91              httpRes.addHeader(name, value);
92          }
93  
94          doFilter(HeaderFilter.class, req, res, chain);
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 }