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.HttpUtil;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.servlet.filters.BasePortalFilter;
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.Map;
40  import java.util.TimeZone;
41  
42  import javax.servlet.FilterChain;
43  import javax.servlet.FilterConfig;
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   * @author Eduardo Lundgren
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 long getLastModified(HttpServletRequest request) {
66          long lasModified = -1;
67  
68          Map<String, String[]> parameterMap = HttpUtil.getParameterMap(
69              request.getQueryString());
70  
71          String[] value = parameterMap.get("t");
72  
73          if ((value != null) && (value.length > 0)) {
74              lasModified = GetterUtil.getLong(value[0]);
75          }
76  
77          return lasModified;
78      }
79  
80      protected void processFilter(
81              HttpServletRequest request, HttpServletResponse response,
82              FilterChain filterChain)
83          throws Exception {
84  
85          Enumeration<String> enu = _filterConfig.getInitParameterNames();
86  
87          while (enu.hasMoreElements()) {
88              String name = enu.nextElement();
89  
90              String value = _filterConfig.getInitParameter(name);
91  
92              if (name.equals(_EXPIRES) && Validator.isNumber(value)) {
93                  int seconds = GetterUtil.getInteger(value);
94  
95                  Calendar cal = new GregorianCalendar();
96  
97                  cal.add(Calendar.SECOND, seconds);
98  
99                  DateFormat dateFormat = new SimpleDateFormat(
100                     _DATE_FORMAT, Locale.US);
101 
102                 dateFormat.setTimeZone(_timeZone);
103 
104                 value = dateFormat.format(cal.getTime());
105             }
106 
107             // LEP-5895
108 
109             boolean addHeader = true;
110 
111             if (name.equalsIgnoreCase(HttpHeaders.CACHE_CONTROL)) {
112                 HttpSession session = request.getSession(false);
113 
114                 if ((session == null) || session.isNew()) {
115                     addHeader = false;
116                 }
117             }
118 
119             if (addHeader) {
120                 response.addHeader(name, value);
121             }
122         }
123 
124         long lastModified = getLastModified(request);
125         long ifModifiedSince = request.getDateHeader(
126             HttpHeaders.IF_MODIFIED_SINCE);
127 
128         if (lastModified > 0) {
129             response.setDateHeader(HttpHeaders.LAST_MODIFIED, lastModified);
130 
131             if (lastModified <= ifModifiedSince) {
132                 response.setDateHeader(
133                     HttpHeaders.LAST_MODIFIED, ifModifiedSince);
134                 response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
135 
136                 return;
137             }
138         }
139 
140         processFilter(HeaderFilter.class, request, response, filterChain);
141     }
142 
143     private static final String _DATE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss z";
144 
145     private static final String _EXPIRES = "Expires";
146 
147     private static final String _TIME_ZONE = StringPool.UTC;
148 
149     private FilterConfig _filterConfig;
150     private TimeZone _timeZone;
151 
152 }