diff options
Diffstat (limited to 'language-web/src/main/java/org/eclipse/viatra/solver/language/web/CacheControlFilter.java')
-rw-r--r-- | language-web/src/main/java/org/eclipse/viatra/solver/language/web/CacheControlFilter.java | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/language-web/src/main/java/org/eclipse/viatra/solver/language/web/CacheControlFilter.java b/language-web/src/main/java/org/eclipse/viatra/solver/language/web/CacheControlFilter.java new file mode 100644 index 00000000..41b8e5bf --- /dev/null +++ b/language-web/src/main/java/org/eclipse/viatra/solver/language/web/CacheControlFilter.java | |||
@@ -0,0 +1,55 @@ | |||
1 | package org.eclipse.viatra.solver.language.web; | ||
2 | |||
3 | import java.io.IOException; | ||
4 | import java.util.regex.Pattern; | ||
5 | |||
6 | import javax.servlet.Filter; | ||
7 | import javax.servlet.FilterChain; | ||
8 | import javax.servlet.FilterConfig; | ||
9 | import javax.servlet.ServletException; | ||
10 | import javax.servlet.ServletRequest; | ||
11 | import javax.servlet.ServletResponse; | ||
12 | import javax.servlet.http.HttpServletRequest; | ||
13 | import javax.servlet.http.HttpServletResponse; | ||
14 | |||
15 | public class CacheControlFilter implements Filter { | ||
16 | |||
17 | private static final String CACHE_CONTROL_HEADER = "Cache-Control"; | ||
18 | |||
19 | private static final String EXPIRES_HEADER = "Expires"; | ||
20 | |||
21 | private static final Pattern CACHE_URI_PATTERN = Pattern.compile(".*\\.(css|gif|js|map|png|svg|woff2)"); | ||
22 | |||
23 | private static final long EXPIRY = 31536000; | ||
24 | |||
25 | private static final String CACHE_CONTROL_CACHE_VALUE = "public, max-age: " + EXPIRY + ", immutable"; | ||
26 | |||
27 | private static final String CACHE_CONTROL_NO_CACHE_VALUE = "no-cache, no-store, max-age: 0, must-revalidate"; | ||
28 | |||
29 | @Override | ||
30 | public void init(FilterConfig filterConfig) throws ServletException { | ||
31 | // Nothing to initialize. | ||
32 | } | ||
33 | |||
34 | @Override | ||
35 | public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) | ||
36 | throws IOException, ServletException { | ||
37 | if (request instanceof HttpServletRequest && response instanceof HttpServletResponse) { | ||
38 | var httpRequest = (HttpServletRequest) request; | ||
39 | var httpResponse = (HttpServletResponse) response; | ||
40 | if (CACHE_URI_PATTERN.matcher(httpRequest.getRequestURI()).matches()) { | ||
41 | httpResponse.setHeader(CACHE_CONTROL_HEADER, CACHE_CONTROL_CACHE_VALUE); | ||
42 | httpResponse.setDateHeader(EXPIRES_HEADER, System.currentTimeMillis() + EXPIRY * 1000L); | ||
43 | } else { | ||
44 | httpResponse.setHeader(CACHE_CONTROL_HEADER, CACHE_CONTROL_NO_CACHE_VALUE); | ||
45 | httpResponse.setDateHeader(EXPIRES_HEADER, 0); | ||
46 | } | ||
47 | } | ||
48 | chain.doFilter(request, response); | ||
49 | } | ||
50 | |||
51 | @Override | ||
52 | public void destroy() { | ||
53 | // Nothing to dispose. | ||
54 | } | ||
55 | } | ||