Skip to content
Snippets Groups Projects
Commit 09992f44 authored by buczekp's avatar buczekp
Browse files

[LMS-1492] configured caching

SVN: 15523
parent cc5d92b5
No related branches found
No related tags found
No related merge requests found
......@@ -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>
......
/*
* 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;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment