From 09992f44dd542729e698763cc07c05c42c8e2ee4 Mon Sep 17 00:00:00 2001 From: buczekp <buczekp> Date: Tue, 20 Apr 2010 07:39:44 +0000 Subject: [PATCH] [LMS-1492] configured caching SVN: 15523 --- openbis/resource/server/web.xml | 9 +++ .../openbis/generic/server/CacheFilter.java | 78 +++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 openbis/source/java/ch/systemsx/cisd/openbis/generic/server/CacheFilter.java diff --git a/openbis/resource/server/web.xml b/openbis/resource/server/web.xml index 2700ba66b8b..75a1fce8d2f 100644 --- a/openbis/resource/server/web.xml +++ b/openbis/resource/server/web.xml @@ -36,6 +36,15 @@ ch.systemsx.cisd.openbis.generic.server.GenericHttpSessionListener </listener-class> </listener> + + <filter> + <filter-name>CacheFilter</filter-name> + <filter-class>ch.systemsx.cisd.openbis.generic.server.CacheFilter</filter-class> + </filter> + <filter-mapping> + <filter-name>CacheFilter</filter-name> + <url-pattern>/*</url-pattern> + </filter-mapping> <!-- Trying kind of extension (i.e., '*.do') here as 'url-pattern' does not work. --> <servlet-mapping> diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/CacheFilter.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/CacheFilter.java new file mode 100644 index 00000000000..2221aec0a04 --- /dev/null +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/CacheFilter.java @@ -0,0 +1,78 @@ +/* + * Copyright 2008 ETH Zuerich, CISD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.systemsx.cisd.openbis.generic.server; + +import java.io.IOException; + +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; + +/** + * Implements {@link Filter} and sets the cache to store data for resources that don't have + * '.nocache.' in their name. Static resources such as images should stay in cache for a long time. + * + * @author Piotr Buczek + */ +public class CacheFilter implements Filter +{ + @SuppressWarnings("unused") + private FilterConfig filterConfig; + + private static final int DAY_IN_SECONDS = 24 * 60 * 60; + + private static final int YEAR_IN_SECONDS = 365 * DAY_IN_SECONDS; + + private static final int SPRINT_IN_SECONDS = 14 * DAY_IN_SECONDS; + + public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) + throws IOException, ServletException + { + + HttpServletRequest httpRequest = (HttpServletRequest) request; + + String requestURI = httpRequest.getRequestURI(); + if (requestURI.contains(".nocache.") == false) + { + HttpServletResponse httpResponse = (HttpServletResponse) response; + httpResponse.addHeader("Cache-Control", "max-age=" + + (isPicture(requestURI) ? YEAR_IN_SECONDS : SPRINT_IN_SECONDS)); + } + filterChain.doFilter(request, response); + + } + + private boolean isPicture(String requestURI) + { + return requestURI.endsWith(".gif") || requestURI.endsWith(".png"); + } + + public void init(FilterConfig config) throws ServletException + { + this.filterConfig = config; + } + + public void destroy() + { + this.filterConfig = null; + } +} -- GitLab