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