001    /**
002     * Copyright (c) 2000-2011 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    import com.liferay.portal.util.PortalUtil;
026    import com.liferay.portal.util.PropsValues;
027    
028    import java.text.Format;
029    
030    import java.util.Calendar;
031    import java.util.Enumeration;
032    import java.util.GregorianCalendar;
033    import java.util.Locale;
034    import java.util.Map;
035    
036    import javax.servlet.FilterChain;
037    import javax.servlet.FilterConfig;
038    import javax.servlet.http.HttpServletRequest;
039    import javax.servlet.http.HttpServletResponse;
040    import javax.servlet.http.HttpSession;
041    
042    /**
043     * @author Brian Wing Shun Chan
044     * @author Raymond Augé
045     * @author Eduardo Lundgren
046     */
047    public class HeaderFilter extends BasePortalFilter {
048    
049            @Override
050            public void init(FilterConfig filterConfig) {
051                    super.init(filterConfig);
052    
053                    _filterConfig = filterConfig;
054                    _dateFormat = FastDateFormatFactoryUtil.getSimpleDateFormat(
055                            _DATE_FORMAT, Locale.US, TimeZoneUtil.getTimeZone(_TIME_ZONE));
056            }
057    
058            protected long getLastModified(HttpServletRequest request) {
059                    long lasModified = -1;
060    
061                    Map<String, String[]> parameterMap = HttpUtil.getParameterMap(
062                            request.getQueryString());
063    
064                    String[] value = parameterMap.get("t");
065    
066                    if ((value != null) && (value.length > 0)) {
067                            lasModified = GetterUtil.getLong(value[0]);
068                    }
069    
070                    return lasModified;
071            }
072    
073            @Override
074            protected void processFilter(
075                            HttpServletRequest request, HttpServletResponse response,
076                            FilterChain filterChain)
077                    throws Exception {
078    
079                    Enumeration<String> enu = _filterConfig.getInitParameterNames();
080    
081                    while (enu.hasMoreElements()) {
082                            String name = enu.nextElement();
083    
084                            if (name.equals(_URL_REGEX_PATTERN)) {
085                                    continue;
086                            }
087    
088                            String value = _filterConfig.getInitParameter(name);
089    
090                            if (name.equals(_EXPIRES) && Validator.isNumber(value)) {
091                                    int seconds = GetterUtil.getInteger(value);
092    
093                                    Calendar cal = new GregorianCalendar();
094    
095                                    cal.add(Calendar.SECOND, seconds);
096    
097                                    value = _dateFormat.format(cal.getTime());
098                            }
099    
100                            // LEP-5895
101    
102                            boolean addHeader = true;
103    
104                            if (PropsValues.WEB_SERVER_PROXY_LEGACY_MODE) {
105                                    String contextPath = request.getContextPath();
106    
107                                    if (name.equalsIgnoreCase(HttpHeaders.CACHE_CONTROL) &&
108                                            contextPath.equals(PortalUtil.getPathContext())) {
109    
110                                            HttpSession session = request.getSession(false);
111    
112                                            if ((session == null) || session.isNew()) {
113                                                    addHeader = false;
114                                            }
115                                    }
116                            }
117    
118                            if (addHeader) {
119                                    response.addHeader(name, value);
120                            }
121                    }
122    
123                    long lastModified = getLastModified(request);
124                    long ifModifiedSince = request.getDateHeader(
125                            HttpHeaders.IF_MODIFIED_SINCE);
126    
127                    if (lastModified > 0) {
128                            response.setDateHeader(HttpHeaders.LAST_MODIFIED, lastModified);
129    
130                            if (lastModified <= ifModifiedSince) {
131                                    response.setDateHeader(
132                                            HttpHeaders.LAST_MODIFIED, ifModifiedSince);
133                                    response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
134    
135                                    return;
136                            }
137                    }
138    
139                    processFilter(HeaderFilter.class, request, response, filterChain);
140            }
141    
142            private static final String _DATE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss z";
143    
144            private static final String _EXPIRES = "Expires";
145    
146            private static final String _TIME_ZONE = StringPool.UTC;
147    
148            private static final String _URL_REGEX_PATTERN = "url-regex-pattern";
149    
150            private Format _dateFormat;
151            private FilterConfig _filterConfig;
152    
153    }