1   /**
2    * Copyright (c) 2000-2009 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.HttpHeaders;
26  import com.liferay.portal.kernel.util.GetterUtil;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.servlet.filters.BasePortalFilter;
30  
31  import java.io.IOException;
32  
33  import java.text.DateFormat;
34  import java.text.SimpleDateFormat;
35  
36  import java.util.Calendar;
37  import java.util.Enumeration;
38  import java.util.GregorianCalendar;
39  import java.util.Locale;
40  import java.util.TimeZone;
41  
42  import javax.servlet.FilterChain;
43  import javax.servlet.FilterConfig;
44  import javax.servlet.ServletException;
45  import javax.servlet.http.HttpServletRequest;
46  import javax.servlet.http.HttpServletResponse;
47  import javax.servlet.http.HttpSession;
48  
49  /**
50   * <a href="HeaderFilter.java.html"><b><i>View Source</i></b></a>
51   *
52   * @author Brian Wing Shun Chan
53   * @author Raymond Augé
54   *
55   */
56  public class HeaderFilter extends BasePortalFilter {
57  
58      public void init(FilterConfig filterConfig) {
59          super.init(filterConfig);
60  
61          _filterConfig = filterConfig;
62          _timeZone = TimeZone.getTimeZone(_TIME_ZONE);
63      }
64  
65      protected void processFilter(
66              HttpServletRequest request, HttpServletResponse response,
67              FilterChain filterChain)
68          throws IOException, ServletException {
69  
70          Enumeration<String> enu = _filterConfig.getInitParameterNames();
71  
72          while (enu.hasMoreElements()) {
73              String name = enu.nextElement();
74  
75              String value = _filterConfig.getInitParameter(name);
76  
77              if (name.equals(_EXPIRES) && Validator.isNumber(value)) {
78                  int seconds = GetterUtil.getInteger(value);
79  
80                  Calendar cal = new GregorianCalendar();
81  
82                  cal.add(Calendar.SECOND, seconds);
83  
84                  DateFormat dateFormat = new SimpleDateFormat(
85                      _DATE_FORMAT, Locale.US);
86  
87                  dateFormat.setTimeZone(_timeZone);
88  
89                  value = dateFormat.format(cal.getTime());
90              }
91  
92              // LEP-5895
93  
94              boolean addHeader = true;
95  
96              if (name.equalsIgnoreCase(HttpHeaders.CACHE_CONTROL)) {
97                  HttpSession session = request.getSession(false);
98  
99                  if ((session == null) || session.isNew()) {
100                     addHeader = false;
101                 }
102             }
103 
104             if (addHeader) {
105                 response.addHeader(name, value);
106             }
107         }
108 
109         processFilter(HeaderFilter.class, request, response, filterChain);
110     }
111 
112     private static final String _DATE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss z";
113 
114     private static final String _EXPIRES = "Expires";
115 
116     private static final String _TIME_ZONE = StringPool.UTC;
117 
118     private FilterConfig _filterConfig;
119     private TimeZone _timeZone;
120 
121 }