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.StringPool;
25  import com.liferay.portal.kernel.util.Validator;
26  import com.liferay.portal.servlet.filters.BasePortalFilter;
27  
28  import java.io.IOException;
29  
30  import java.text.DateFormat;
31  import java.text.SimpleDateFormat;
32  
33  import java.util.Calendar;
34  import java.util.Enumeration;
35  import java.util.GregorianCalendar;
36  import java.util.Locale;
37  import java.util.TimeZone;
38  
39  import javax.servlet.FilterChain;
40  import javax.servlet.FilterConfig;
41  import javax.servlet.ServletException;
42  import javax.servlet.http.HttpServletRequest;
43  import javax.servlet.http.HttpServletResponse;
44  import javax.servlet.http.HttpSession;
45  
46  /**
47   * <a href="HeaderFilter.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Brian Wing Shun Chan
50   * @author Raymond Augé
51   *
52   */
53  public class HeaderFilter extends BasePortalFilter {
54  
55      public void init(FilterConfig filterConfig) throws ServletException {
56          super.init(filterConfig);
57  
58          _filterConfig = filterConfig;
59          _timeZone = TimeZone.getTimeZone(_TIME_ZONE);
60      }
61  
62      protected void processFilter(
63              HttpServletRequest request, HttpServletResponse response,
64              FilterChain filterChain)
65          throws IOException, ServletException {
66  
67          Enumeration<String> enu = _filterConfig.getInitParameterNames();
68  
69          while (enu.hasMoreElements()) {
70              String name = enu.nextElement();
71  
72              String value = _filterConfig.getInitParameter(name);
73  
74              if (name.equals(_EXPIRES) && Validator.isNumber(value)) {
75                  int seconds = GetterUtil.getInteger(value);
76  
77                  Calendar cal = new GregorianCalendar();
78  
79                  cal.add(Calendar.SECOND, seconds);
80  
81                  DateFormat dateFormat = new SimpleDateFormat(
82                      _DATE_FORMAT, Locale.US);
83  
84                  dateFormat.setTimeZone(_timeZone);
85  
86                  value = dateFormat.format(cal.getTime());
87              }
88  
89              // LEP-5895
90  
91              boolean addHeader = true;
92  
93              if (name.equalsIgnoreCase(HttpHeaders.CACHE_CONTROL)) {
94                  HttpSession session = request.getSession(false);
95  
96                  if ((session == null) || session.isNew()) {
97                      addHeader = false;
98                  }
99              }
100 
101             if (addHeader) {
102                 response.addHeader(name, value);
103             }
104         }
105 
106         processFilter(HeaderFilter.class, request, response, filterChain);
107     }
108 
109     private static final String _DATE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss z";
110 
111     private static final String _EXPIRES = "Expires";
112 
113     private static final String _TIME_ZONE = StringPool.UTC;
114 
115     private FilterConfig _filterConfig;
116     private TimeZone _timeZone;
117 
118 }