001    /**
002     * Copyright (c) 2000-present 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.LocaleUtil;
022    import com.liferay.portal.kernel.util.PortalUtil;
023    import com.liferay.portal.kernel.util.SetUtil;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.kernel.util.Time;
026    import com.liferay.portal.kernel.util.TimeZoneUtil;
027    import com.liferay.portal.kernel.util.Validator;
028    import com.liferay.portal.servlet.filters.BasePortalFilter;
029    import com.liferay.portal.util.PropsValues;
030    
031    import java.text.Format;
032    
033    import java.util.Enumeration;
034    import java.util.Set;
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            protected long getLastModified(HttpServletRequest request) {
050                    String value = HttpUtil.getParameter(request.getQueryString(), "t");
051    
052                    if (Validator.isNull(value)) {
053                            return -1;
054                    }
055    
056                    return GetterUtil.getLong(value);
057            }
058    
059            @Override
060            protected void processFilter(
061                            HttpServletRequest request, HttpServletResponse response,
062                            FilterChain filterChain)
063                    throws Exception {
064    
065                    FilterConfig filterConfig = getFilterConfig();
066    
067                    Enumeration<String> enu = filterConfig.getInitParameterNames();
068    
069                    while (enu.hasMoreElements()) {
070                            String name = enu.nextElement();
071    
072                            if (!_requestHeaderIgnoreInitParams.contains(name)) {
073                                    _addHeader(
074                                            request, response, name,
075                                            filterConfig.getInitParameter(name));
076                            }
077                    }
078    
079                    long lastModified = getLastModified(request);
080    
081                    if (lastModified > 0) {
082                            long ifModifiedSince = request.getDateHeader(
083                                    HttpHeaders.IF_MODIFIED_SINCE);
084    
085                            response.setDateHeader(HttpHeaders.LAST_MODIFIED, lastModified);
086    
087                            if (lastModified <= ifModifiedSince) {
088                                    response.setDateHeader(
089                                            HttpHeaders.LAST_MODIFIED, ifModifiedSince);
090                                    response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
091    
092                                    return;
093                            }
094                    }
095    
096                    processFilter(
097                            HeaderFilter.class.getName(), request, response, filterChain);
098            }
099    
100            private void _addHeader(
101                    HttpServletRequest request, HttpServletResponse response, String name,
102                    String value) {
103    
104                    // LEP-5895 and LPS-15802
105    
106                    if (StringUtil.equalsIgnoreCase(name, HttpHeaders.CACHE_CONTROL)) {
107                            if (PropsValues.WEB_SERVER_PROXY_LEGACY_MODE) {
108                                    if (_isNewSession(request)) {
109                                            String contextPath = request.getContextPath();
110    
111                                            if (contextPath.equals(PortalUtil.getPathContext())) {
112                                                    return;
113                                            }
114                                    }
115                            }
116                    }
117                    else if (StringUtil.equalsIgnoreCase(name, HttpHeaders.EXPIRES)) {
118                            if (_isNewSession(request)) {
119                                    return;
120                            }
121    
122                            if (Validator.isNumber(value)) {
123                                    int seconds = GetterUtil.getInteger(value);
124    
125                                    value = _dateFormat.format(
126                                            System.currentTimeMillis() + (seconds * Time.SECOND));
127                            }
128                    }
129    
130                    response.addHeader(name, value);
131            }
132    
133            private boolean _isNewSession(HttpServletRequest request) {
134                    HttpSession session = request.getSession(false);
135    
136                    if ((session == null) || session.isNew()) {
137                            return true;
138                    }
139    
140                    return false;
141            }
142    
143            private static final Format _dateFormat =
144                    FastDateFormatFactoryUtil.getSimpleDateFormat(
145                            Time.RFC822_FORMAT, LocaleUtil.US, TimeZoneUtil.GMT);
146            private static final Set<String> _requestHeaderIgnoreInitParams =
147                    SetUtil.fromArray(PropsValues.REQUEST_HEADER_IGNORE_INIT_PARAMS);
148    
149    }