1 /** 2 * This file Copyright (c) 2008-2015 Magnolia International 3 * Ltd. (http://www.magnolia-cms.com). All rights reserved. 4 * 5 * 6 * This file is dual-licensed under both the Magnolia 7 * Network Agreement and the GNU General Public License. 8 * You may elect to use one or the other of these licenses. 9 * 10 * This file is distributed in the hope that it will be 11 * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the 12 * implied warranty of MERCHANTABILITY or FITNESS FOR A 13 * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT. 14 * Redistribution, except as permitted by whichever of the GPL 15 * or MNA you select, is prohibited. 16 * 17 * 1. For the GPL license (GPL), you can redistribute and/or 18 * modify this file under the terms of the GNU General 19 * Public License, Version 3, as published by the Free Software 20 * Foundation. You should have received a copy of the GNU 21 * General Public License, Version 3 along with this program; 22 * if not, write to the Free Software Foundation, Inc., 51 23 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 24 * 25 * 2. For the Magnolia Network Agreement (MNA), this file 26 * and the accompanying materials are made available under the 27 * terms of the MNA which accompanies this distribution, and 28 * is available at http://www.magnolia-cms.com/mna.html 29 * 30 * Any modifications to this file must keep this entire header 31 * intact. 32 * 33 */ 34 package info.magnolia.module.cache.filter; 35 36 import info.magnolia.cms.cache.CacheConstants; 37 import info.magnolia.cms.filters.AbstractMgnlFilter; 38 39 import java.io.IOException; 40 41 import javax.servlet.FilterChain; 42 import javax.servlet.ServletException; 43 import javax.servlet.http.HttpServletRequest; 44 import javax.servlet.http.HttpServletResponse; 45 46 /** 47 * <p> 48 * Filter that sets cache headers, allowing or dening cache at client-side. By default the filter adds the "Cache-Control: public" and expire directives to resources so that everything can be cached by the browser. Setting the <code>nocache</code> property to <code>true</code> has the opposite effect, forcing browsers to avoid caching. 49 * </p> 50 * <p> 51 * The following example shows how to configure the filter so that static resources (images, css, js) gets cached by the browser, and deny cache for html pages. 52 * </p> 53 * 54 * <pre> 55 * + server 56 * + filters 57 * + ... 58 * + headers-cache 59 * - class info.magnolia.module.cache.filter.CacheHeadersFilter 60 * - expirationMinutes 1440 <em>(default)</em> 61 * + bypasses 62 * + extensions 63 * - class info.magnolia.voting.voters.ExtensionVoter 64 * - allow gif,jpg,png,swf,css,js 65 * - not true 66 * + headers-nocache 67 * - class info.magnolia.module.cache.filter.CacheHeadersFilter 68 * - nocache true 69 * + bypasses 70 * + extensions 71 * - class info.magnolia.voting.voters.ExtensionVoter 72 * - allow html 73 * - not true 74 * </pre> 75 * 76 * @deprecated since 5.4. Use {@link info.magnolia.module.cache.executor.SetExpirationHeaders} instead. 77 */ 78 public class CacheHeadersFilter extends AbstractMgnlFilter { 79 80 /** 81 * Number of minutes this item must be kept in cache. 82 */ 83 private long expirationMinutes = 1440; 84 85 /** 86 * Cache should be avoided for filtered items. 87 */ 88 private boolean nocache; 89 90 /** 91 * Sets the expirationMinutes. 92 * 93 * @param expirationMinutes the expirationMinutes to set 94 */ 95 public void setExpirationMinutes(long expirationMinutes) { 96 this.expirationMinutes = expirationMinutes; 97 } 98 99 /** 100 * Sets the nocache. 101 * 102 * @param nocache the nocache to set 103 */ 104 public void setNocache(boolean nocache) { 105 this.nocache = nocache; 106 } 107 108 @Override 109 public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException { 110 if (nocache) { 111 response.setHeader(CacheConstants.HEADER_PRAGMA, CacheConstants.HEADER_VALUE_NO_CACHE); 112 response.setHeader(CacheConstants.HEADER_CACHE_CONTROL, CacheConstants.HEADER_CACHE_CONTROL_VALUE_DISABLE_CACHE); 113 response.setDateHeader(CacheConstants.HEADER_EXPIRES, 0L); 114 } else { 115 response.setHeader(CacheConstants.HEADER_CACHE_CONTROL, CacheConstants.HEADER_VALUE_MAX_AGE + "=" + expirationMinutes * 60 + ", " + CacheConstants.HEADER_VALUE_PUBLIC); 116 final long expiration = System.currentTimeMillis() + expirationMinutes * 60000; 117 response.setDateHeader(CacheConstants.HEADER_EXPIRES, expiration); 118 } 119 120 chain.doFilter(request, response); 121 } 122 }