/***************************************************************
* This file is part of the [fleXive](R) framework.
*
* Copyright (c) 1999-2010
* UCS - unique computing solutions gmbh (http://www.ucs.at)
* All rights reserved
*
* The [fleXive](R) project is free software; you can redistribute
* it and/or modify it under the terms of the GNU Lesser General Public
* License version 2.1 or higher as published by the Free Software Foundation.
*
* The GNU Lesser General Public License can be found at
* http://www.gnu.org/licenses/lgpl.html.
* A copy is found in the textfile LGPL.txt and important notices to the
* license from the author are found in LICENSE.txt distributed with
* these libraries.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* For further information about UCS - unique computing solutions gmbh,
* please see the company website: http://www.ucs.at
*
* For further information about [fleXive](R), please see the
* project website: http://www.flexive.org
*
*
* This copyright notice MUST APPEAR in all copies of the file!
***************************************************************/
package com.flexive.cms.war.filters;
import com.flexive.cms.war.CmsRequestUtils;
import com.flexive.cms.war.facelets.CmsResourceResolver;
import java.io.IOException;
import java.util.regex.Pattern;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Set cache headers of XHTML files served by the CMS (except edit mode pages).
*
*
Init parameters
*
*
* - excludePattern: a regexp for URLs that should never have cache headers
*
*
* @author Daniel Lichtenberger
* @version $Rev: 58 $
*/
public class CmsPageHeadersFilter implements Filter {
private static final Log LOG = LogFactory.getLog(CmsPageHeadersFilter.class);
/**
* The maximum age of a static page served by the CMS in minutes
*
* To prevent problem with JSF forms, this is set to the same
* value as the session timeout in web.xml.
*/
private static final long MAXAGE_STATIC_PAGE = 30;
private Pattern excludePattern;
public void destroy() {
// nop
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
//noinspection PointlessBooleanExpression
if (request instanceof HttpServletRequest && MAXAGE_STATIC_PAGE > 0
&& (excludePattern == null || !excludePattern.matcher(((HttpServletRequest) request).getRequestURI()).find())) {
final HttpServletRequest httpRequest = (HttpServletRequest) request;
final String uri = httpRequest.getRequestURI().replace(httpRequest.getContextPath(), "");
final HttpServletResponse httpResponse = (HttpServletResponse) response;
if (!CmsRequestUtils.isWebsiteRequest(request)
|| CmsRequestUtils.getRequestedURI(request).startsWith(CmsResourceResolver.ADMIN_PATH)) {
// disable cache for edit mode, /admin
httpResponse.setDateHeader("Expires", 0);
httpResponse.setHeader("Pragma", "no-cache");
} else {
// public page - cache for MAXAGE_STATIC_PAGE minutes
// set one hour expiry in edit mode
httpResponse.setDateHeader(
"Expires",
System.currentTimeMillis() + MAXAGE_STATIC_PAGE * 60 * 1000
);
// play safe
httpResponse.setHeader("Cache-Control", "private");
httpResponse.setHeader("Pragma", "cache");
}
}
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
final String patternParam = filterConfig.getInitParameter("excludePattern");
if (StringUtils.isNotBlank(patternParam)) {
try {
this.excludePattern = Pattern.compile(patternParam);
if (LOG.isInfoEnabled()) {
LOG.info("Set exclude path for CMS caching filter: " + patternParam);
}
} catch (Exception e) {
if (LOG.isWarnEnabled()) {
LOG.warn("Failed to parse excludePattern: " + e.getMessage());
}
}
}
}
}