001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.main;
029
030import java.io.IOException;
031import java.time.Duration;
032import java.time.temporal.ChronoUnit;
033
034import javax.servlet.Filter;
035import javax.servlet.FilterChain;
036import javax.servlet.FilterConfig;
037import javax.servlet.ServletException;
038import javax.servlet.ServletRequest;
039import javax.servlet.ServletResponse;
040import javax.servlet.http.HttpServletResponse;
041import javax.servlet.http.HttpServletResponseWrapper;
042
043import com.google.common.net.HttpHeaders;
044
045/**
046 * Simple filter for setting caching headers for exported resources.
047 *
048 * <p>CSS and Javascript are assumed to be cacheable for 24 hours, everything else is assumed to be cacheable for 365 days.
049 */
050public class CmsExportExpiresFilter implements Filter {
051
052    /**
053     * Response wrapper which sets caching headers based on content type.
054     */
055    static class ResponseWrapper extends HttpServletResponseWrapper {
056
057        /**
058         * Creates a new instance.
059         * @param response the response to wrap
060         */
061        public ResponseWrapper(HttpServletResponse response) {
062
063            super(response);
064        }
065
066        /**
067         * @see javax.servlet.ServletResponseWrapper#setContentType(java.lang.String)
068         */
069        @Override
070        public void setContentType(String type) {
071
072            super.setContentType(type);
073            Duration day = Duration.of(1, ChronoUnit.DAYS);
074            Duration year = Duration.of(365, ChronoUnit.DAYS);
075            Duration duration = null;
076            if (type != null) {
077                if (type.contains("application/javascript") || type.contains("text/css") || type.contains("text/javascript")) {
078                    duration = day;
079                } else {
080                    duration = year;
081                }
082                setDateHeader(HttpHeaders.EXPIRES, System.currentTimeMillis() + duration.toMillis());
083                setHeader(HttpHeaders.CACHE_CONTROL, "max-age=" + duration.getSeconds());
084            }
085        }
086    }
087
088    /**
089     * @see javax.servlet.Filter#destroy()
090     */
091    public void destroy() {
092
093        // do nothing
094    }
095
096    /**
097     * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
098     */
099    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
100    throws IOException, ServletException {
101
102        ResponseWrapper wrappedResponse = new ResponseWrapper((HttpServletResponse)response);
103        chain.doFilter(request, wrappedResponse);
104    }
105
106    /**
107     * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
108     */
109    public void init(FilterConfig filterConfig) {
110        // does nothing
111    }
112
113}