1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.servlet.filters.header;
16  
17  import com.liferay.portal.kernel.servlet.HttpHeaders;
18  import com.liferay.portal.kernel.util.FastDateFormatFactoryUtil;
19  import com.liferay.portal.kernel.util.GetterUtil;
20  import com.liferay.portal.kernel.util.HttpUtil;
21  import com.liferay.portal.kernel.util.StringPool;
22  import com.liferay.portal.kernel.util.TimeZoneUtil;
23  import com.liferay.portal.kernel.util.Validator;
24  import com.liferay.portal.servlet.filters.BasePortalFilter;
25  
26  import java.text.Format;
27  
28  import java.util.Calendar;
29  import java.util.Enumeration;
30  import java.util.GregorianCalendar;
31  import java.util.Locale;
32  import java.util.Map;
33  
34  import javax.servlet.FilterChain;
35  import javax.servlet.FilterConfig;
36  import javax.servlet.http.HttpServletRequest;
37  import javax.servlet.http.HttpServletResponse;
38  import javax.servlet.http.HttpSession;
39  
40  /**
41   * <a href="HeaderFilter.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   * @author Raymond Augé
45   * @author Eduardo Lundgren
46   */
47  public class HeaderFilter extends BasePortalFilter {
48  
49      public void init(FilterConfig filterConfig) {
50          super.init(filterConfig);
51  
52          _filterConfig = filterConfig;
53          _dateFormat = FastDateFormatFactoryUtil.getSimpleDateFormat(
54              _DATE_FORMAT, Locale.US, TimeZoneUtil.getTimeZone(_TIME_ZONE));
55      }
56  
57      protected long getLastModified(HttpServletRequest request) {
58          long lasModified = -1;
59  
60          Map<String, String[]> parameterMap = HttpUtil.getParameterMap(
61              request.getQueryString());
62  
63          String[] value = parameterMap.get("t");
64  
65          if ((value != null) && (value.length > 0)) {
66              lasModified = GetterUtil.getLong(value[0]);
67          }
68  
69          return lasModified;
70      }
71  
72      protected void processFilter(
73              HttpServletRequest request, HttpServletResponse response,
74              FilterChain filterChain)
75          throws Exception {
76  
77          Enumeration<String> enu = _filterConfig.getInitParameterNames();
78  
79          while (enu.hasMoreElements()) {
80              String name = enu.nextElement();
81  
82              if (name.equals(_URL_REGEX_PATTERN)) {
83                  continue;
84              }
85  
86              String value = _filterConfig.getInitParameter(name);
87  
88              if (name.equals(_EXPIRES) && Validator.isNumber(value)) {
89                  int seconds = GetterUtil.getInteger(value);
90  
91                  Calendar cal = new GregorianCalendar();
92  
93                  cal.add(Calendar.SECOND, seconds);
94  
95                  value = _dateFormat.format(cal.getTime());
96              }
97  
98              // LEP-5895
99  
100             boolean addHeader = true;
101 
102             if (name.equalsIgnoreCase(HttpHeaders.CACHE_CONTROL)) {
103                 HttpSession session = request.getSession(false);
104 
105                 if ((session == null) || session.isNew()) {
106                     addHeader = false;
107                 }
108             }
109 
110             if (addHeader) {
111                 response.addHeader(name, value);
112             }
113         }
114 
115         long lastModified = getLastModified(request);
116         long ifModifiedSince = request.getDateHeader(
117             HttpHeaders.IF_MODIFIED_SINCE);
118 
119         if (lastModified > 0) {
120             response.setDateHeader(HttpHeaders.LAST_MODIFIED, lastModified);
121 
122             if (lastModified <= ifModifiedSince) {
123                 response.setDateHeader(
124                     HttpHeaders.LAST_MODIFIED, ifModifiedSince);
125                 response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
126 
127                 return;
128             }
129         }
130 
131         processFilter(HeaderFilter.class, request, response, filterChain);
132     }
133 
134     private static final String _DATE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss z";
135 
136     private static final String _EXPIRES = "Expires";
137 
138     private static final String _TIME_ZONE = StringPool.UTC;
139 
140     private static final String _URL_REGEX_PATTERN = "url-regex-pattern";
141 
142     private Format _dateFormat;
143     private FilterConfig _filterConfig;
144 
145 }