1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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.Validator;
23  import com.liferay.portal.servlet.filters.BasePortalFilter;
24  
25  import java.text.Format;
26  
27  import java.util.Calendar;
28  import java.util.Enumeration;
29  import java.util.GregorianCalendar;
30  import java.util.Locale;
31  import java.util.Map;
32  import java.util.TimeZone;
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, TimeZone.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              String value = _filterConfig.getInitParameter(name);
83  
84              if (name.equals(_EXPIRES) && Validator.isNumber(value)) {
85                  int seconds = GetterUtil.getInteger(value);
86  
87                  Calendar cal = new GregorianCalendar();
88  
89                  cal.add(Calendar.SECOND, seconds);
90  
91                  value = _dateFormat.format(cal.getTime());
92              }
93  
94              // LEP-5895
95  
96              boolean addHeader = true;
97  
98              if (name.equalsIgnoreCase(HttpHeaders.CACHE_CONTROL)) {
99                  HttpSession session = request.getSession(false);
100 
101                 if ((session == null) || session.isNew()) {
102                     addHeader = false;
103                 }
104             }
105 
106             if (addHeader) {
107                 response.addHeader(name, value);
108             }
109         }
110 
111         long lastModified = getLastModified(request);
112         long ifModifiedSince = request.getDateHeader(
113             HttpHeaders.IF_MODIFIED_SINCE);
114 
115         if (lastModified > 0) {
116             response.setDateHeader(HttpHeaders.LAST_MODIFIED, lastModified);
117 
118             if (lastModified <= ifModifiedSince) {
119                 response.setDateHeader(
120                     HttpHeaders.LAST_MODIFIED, ifModifiedSince);
121                 response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
122 
123                 return;
124             }
125         }
126 
127         processFilter(HeaderFilter.class, request, response, filterChain);
128     }
129 
130     private static final String _DATE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss z";
131 
132     private static final String _EXPIRES = "Expires";
133 
134     private static final String _TIME_ZONE = StringPool.UTC;
135 
136     private Format _dateFormat;
137     private FilterConfig _filterConfig;
138 
139 }