- Check when renaming file that name doesn't already exist.
[pithos] / src / gr / ebs / gss / server / CacheFilter.java
1 /*
2  * Copyright 2009 Electronic Business Systems Ltd.
3  *
4  * This file is part of GSS.
5  *
6  * GSS is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * GSS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 package gr.ebs.gss.server;
20
21 import java.io.IOException;
22 import java.util.Date;
23
24 import javax.servlet.Filter;
25 import javax.servlet.FilterChain;
26 import javax.servlet.FilterConfig;
27 import javax.servlet.ServletException;
28 import javax.servlet.ServletRequest;
29 import javax.servlet.ServletResponse;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
32
33 /**
34  * A cache filter that properly caches GWT-generated files.
35  *
36  * @author past
37  *
38  */
39 public class CacheFilter implements Filter {
40         public void doFilter( ServletRequest request, ServletResponse response,
41                         FilterChain filterChain) throws IOException, ServletException {
42                 HttpServletRequest httpRequest = (HttpServletRequest)request;
43                 String requestURI = httpRequest.getRequestURI();
44                 String contextPath = httpRequest.getContextPath();
45                 if (requestURI.contains(".nocache.")) {
46                         HttpServletResponse httpResponse = (HttpServletResponse)response;
47                         httpResponse.setHeader("Expires", "Fri, 01 Jan 1990 00:00:00 GMT");
48                         httpResponse.setHeader("Pragma", "no-cache");
49                         httpResponse.setHeader("Cache-control", "no-cache, must-revalidate");
50                 } else if (requestURI.contains(".cache.")) {
51                         long today = new Date().getTime();
52                         HttpServletResponse httpResponse = (HttpServletResponse)response;
53                         httpResponse.setDateHeader("Expires", today+31536000000L);
54                 } else if (!requestURI.startsWith(contextPath + "/nonce") &&
55                                         !requestURI.startsWith(contextPath + "/token") &&
56                                         !requestURI.startsWith(contextPath + "/login") &&
57                                         !requestURI.startsWith(contextPath + "/rest") &&
58                                         !requestURI.startsWith(contextPath + "/policy")) {
59                         long today = new Date().getTime();
60                         HttpServletResponse httpResponse = (HttpServletResponse)response;
61                         httpResponse.setDateHeader("Expires", today+3456000000L);
62                 }
63                 filterChain.doFilter(request, response);
64         }
65
66         @Override
67         public void destroy() {
68         }
69
70         @Override
71         public void init(FilterConfig filterConfig) {
72         }
73 }