001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.servlet.filters.header;
016    
017    import com.liferay.portal.kernel.servlet.HttpHeaders;
018    import com.liferay.portal.kernel.util.FastDateFormatFactoryUtil;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.HttpUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.TimeZoneUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.servlet.filters.BasePortalFilter;
025    
026    import java.text.Format;
027    
028    import java.util.Calendar;
029    import java.util.Enumeration;
030    import java.util.GregorianCalendar;
031    import java.util.Locale;
032    import java.util.Map;
033    
034    import javax.servlet.FilterChain;
035    import javax.servlet.FilterConfig;
036    import javax.servlet.http.HttpServletRequest;
037    import javax.servlet.http.HttpServletResponse;
038    import javax.servlet.http.HttpSession;
039    
040    /**
041     * @author Brian Wing Shun Chan
042     * @author Raymond Augé
043     * @author Eduardo Lundgren
044     */
045    public class HeaderFilter extends BasePortalFilter {
046    
047            public void init(FilterConfig filterConfig) {
048                    super.init(filterConfig);
049    
050                    _filterConfig = filterConfig;
051                    _dateFormat = FastDateFormatFactoryUtil.getSimpleDateFormat(
052                            _DATE_FORMAT, Locale.US, TimeZoneUtil.getTimeZone(_TIME_ZONE));
053            }
054    
055            protected long getLastModified(HttpServletRequest request) {
056                    long lasModified = -1;
057    
058                    Map<String, String[]> parameterMap = HttpUtil.getParameterMap(
059                            request.getQueryString());
060    
061                    String[] value = parameterMap.get("t");
062    
063                    if ((value != null) && (value.length > 0)) {
064                            lasModified = GetterUtil.getLong(value[0]);
065                    }
066    
067                    return lasModified;
068            }
069    
070            protected void processFilter(
071                            HttpServletRequest request, HttpServletResponse response,
072                            FilterChain filterChain)
073                    throws Exception {
074    
075                    Enumeration<String> enu = _filterConfig.getInitParameterNames();
076    
077                    while (enu.hasMoreElements()) {
078                            String name = enu.nextElement();
079    
080                            if (name.equals(_URL_REGEX_PATTERN)) {
081                                    continue;
082                            }
083    
084                            String value = _filterConfig.getInitParameter(name);
085    
086                            if (name.equals(_EXPIRES) && Validator.isNumber(value)) {
087                                    int seconds = GetterUtil.getInteger(value);
088    
089                                    Calendar cal = new GregorianCalendar();
090    
091                                    cal.add(Calendar.SECOND, seconds);
092    
093                                    value = _dateFormat.format(cal.getTime());
094                            }
095    
096                            // LEP-5895
097    
098                            boolean addHeader = true;
099    
100                            if (name.equalsIgnoreCase(HttpHeaders.CACHE_CONTROL)) {
101                                    HttpSession session = request.getSession(false);
102    
103                                    if ((session == null) || session.isNew()) {
104                                            addHeader = false;
105                                    }
106                            }
107    
108                            if (addHeader) {
109                                    response.addHeader(name, value);
110                            }
111                    }
112    
113                    long lastModified = getLastModified(request);
114                    long ifModifiedSince = request.getDateHeader(
115                            HttpHeaders.IF_MODIFIED_SINCE);
116    
117                    if (lastModified > 0) {
118                            response.setDateHeader(HttpHeaders.LAST_MODIFIED, lastModified);
119    
120                            if (lastModified <= ifModifiedSince) {
121                                    response.setDateHeader(
122                                            HttpHeaders.LAST_MODIFIED, ifModifiedSince);
123                                    response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
124    
125                                    return;
126                            }
127                    }
128    
129                    processFilter(HeaderFilter.class, request, response, filterChain);
130            }
131    
132            private static final String _DATE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss z";
133    
134            private static final String _EXPIRES = "Expires";
135    
136            private static final String _TIME_ZONE = StringPool.UTC;
137    
138            private static final String _URL_REGEX_PATTERN = "url-regex-pattern";
139    
140            private Format _dateFormat;
141            private FilterConfig _filterConfig;
142    
143    }