diff --git a/openbis/resource/server/web.xml b/openbis/resource/server/web.xml index 2700ba66b8bc3825039f624a9c5a5f3113c01bb4..75a1fce8d2f17b8ba5241db6d604449fd948f79e 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 0000000000000000000000000000000000000000..2221aec0a045065c6b991443ac1c17979258001f --- /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; + } +}