1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.servlet.filters.header;
21  
22  import com.liferay.portal.kernel.servlet.HttpHeaders;
23  import com.liferay.portal.kernel.util.GetterUtil;
24  import com.liferay.portal.kernel.util.HttpUtil;
25  import com.liferay.portal.kernel.util.StringPool;
26  import com.liferay.portal.kernel.util.Validator;
27  import com.liferay.portal.servlet.filters.BasePortalFilter;
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.Map;
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   * @author Eduardo Lundgren
54   *
55   */
56  public class HeaderFilter extends BasePortalFilter {
57  
58      public void init(FilterConfig filterConfig) throws ServletException {
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 IOException, ServletException {
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 }