001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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.TimeZoneUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.servlet.filters.BasePortalFilter;
024    import com.liferay.portal.util.PortalUtil;
025    import com.liferay.portal.util.PropsValues;
026    
027    import java.text.Format;
028    
029    import java.util.Calendar;
030    import java.util.Enumeration;
031    import java.util.GregorianCalendar;
032    import java.util.Locale;
033    import java.util.Map;
034    
035    import javax.servlet.FilterChain;
036    import javax.servlet.FilterConfig;
037    import javax.servlet.http.HttpServletRequest;
038    import javax.servlet.http.HttpServletResponse;
039    import javax.servlet.http.HttpSession;
040    
041    /**
042     * @author Brian Wing Shun Chan
043     * @author Raymond Augé
044     * @author Eduardo Lundgren
045     */
046    public class HeaderFilter extends BasePortalFilter {
047    
048            @Override
049            public void init(FilterConfig filterConfig) {
050                    super.init(filterConfig);
051    
052                    _filterConfig = filterConfig;
053                    _dateFormat = FastDateFormatFactoryUtil.getSimpleDateFormat(
054                            _DATE_FORMAT, Locale.US, TimeZoneUtil.getTimeZone(_TIME_ZONE));
055            }
056    
057            protected long getLastModified(HttpServletRequest request) {
058                    long lasModified = -1;
059    
060                    Map<String, String[]> parameterMap = HttpUtil.getParameterMap(
061                            request.getQueryString());
062    
063                    String[] value = parameterMap.get("t");
064    
065                    if ((value != null) && (value.length > 0)) {
066                            lasModified = GetterUtil.getLong(value[0]);
067                    }
068    
069                    return lasModified;
070            }
071    
072            @Override
073            protected void processFilter(
074                            HttpServletRequest request, HttpServletResponse response,
075                            FilterChain filterChain)
076                    throws Exception {
077    
078                    Enumeration<String> enu = _filterConfig.getInitParameterNames();
079    
080                    while (enu.hasMoreElements()) {
081                            String name = enu.nextElement();
082    
083                            if (name.equals(_URL_REGEX_PATTERN)) {
084                                    continue;
085                            }
086    
087                            String value = _filterConfig.getInitParameter(name);
088    
089                            if (name.equals(_EXPIRES) && Validator.isNumber(value)) {
090                                    int seconds = GetterUtil.getInteger(value);
091    
092                                    Calendar cal = new GregorianCalendar();
093    
094                                    cal.add(Calendar.SECOND, seconds);
095    
096                                    value = _dateFormat.format(cal.getTime());
097                            }
098    
099                            // LEP-5895 and LPS-15802
100    
101                            boolean addHeader = true;
102    
103                            if (name.equalsIgnoreCase(HttpHeaders.CACHE_CONTROL) ||
104                                    name.equalsIgnoreCase(HttpHeaders.EXPIRES)) {
105    
106                                    boolean newSession = false;
107    
108                                    HttpSession session = request.getSession(false);
109    
110                                    if ((session == null) || session.isNew()) {
111                                            newSession = true;
112                                    }
113    
114                                    String contextPath = request.getContextPath();
115    
116                                    if (name.equalsIgnoreCase(HttpHeaders.EXPIRES) && newSession) {
117                                            addHeader = false;
118                                    }
119                                    else if (PropsValues.WEB_SERVER_PROXY_LEGACY_MODE &&
120                                                     newSession &&
121                                                     contextPath.equals(PortalUtil.getPathContext())) {
122    
123                                            addHeader = false;
124                                    }
125                            }
126    
127                            if (addHeader) {
128                                    response.addHeader(name, value);
129                            }
130                    }
131    
132                    long lastModified = getLastModified(request);
133                    long ifModifiedSince = request.getDateHeader(
134                            HttpHeaders.IF_MODIFIED_SINCE);
135    
136                    if (lastModified > 0) {
137                            response.setDateHeader(HttpHeaders.LAST_MODIFIED, lastModified);
138    
139                            if (lastModified <= ifModifiedSince) {
140                                    response.setDateHeader(
141                                            HttpHeaders.LAST_MODIFIED, ifModifiedSince);
142                                    response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
143    
144                                    return;
145                            }
146                    }
147    
148                    processFilter(HeaderFilter.class, request, response, filterChain);
149            }
150    
151            private static final String _DATE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss z";
152    
153            private static final String _EXPIRES = "Expires";
154    
155            private static final String _TIME_ZONE = "GMT";
156    
157            private static final String _URL_REGEX_PATTERN = "url-regex-pattern";
158    
159            private Format _dateFormat;
160            private FilterConfig _filterConfig;
161    
162    }