1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.FastDateFormatFactoryUtil;
27  import com.liferay.portal.kernel.util.GetterUtil;
28  import com.liferay.portal.kernel.util.HttpUtil;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.portal.servlet.filters.BasePortalFilter;
32  
33  import java.text.Format;
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  public class HeaderFilter extends BasePortalFilter {
56  
57      public void init(FilterConfig filterConfig) {
58          super.init(filterConfig);
59  
60          _filterConfig = filterConfig;
61          _dateFormat = FastDateFormatFactoryUtil.getSimpleDateFormat(
62              _DATE_FORMAT, Locale.US, 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                  value = _dateFormat.format(cal.getTime());
100             }
101 
102             // LEP-5895
103 
104             boolean addHeader = true;
105 
106             if (name.equalsIgnoreCase(HttpHeaders.CACHE_CONTROL)) {
107                 HttpSession session = request.getSession(false);
108 
109                 if ((session == null) || session.isNew()) {
110                     addHeader = false;
111                 }
112             }
113 
114             if (addHeader) {
115                 response.addHeader(name, value);
116             }
117         }
118 
119         long lastModified = getLastModified(request);
120         long ifModifiedSince = request.getDateHeader(
121             HttpHeaders.IF_MODIFIED_SINCE);
122 
123         if (lastModified > 0) {
124             response.setDateHeader(HttpHeaders.LAST_MODIFIED, lastModified);
125 
126             if (lastModified <= ifModifiedSince) {
127                 response.setDateHeader(
128                     HttpHeaders.LAST_MODIFIED, ifModifiedSince);
129                 response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
130 
131                 return;
132             }
133         }
134 
135         processFilter(HeaderFilter.class, request, response, filterChain);
136     }
137 
138     private static final String _DATE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss z";
139 
140     private static final String _EXPIRES = "Expires";
141 
142     private static final String _TIME_ZONE = StringPool.UTC;
143 
144     private Format _dateFormat;
145     private FilterConfig _filterConfig;
146 
147 }