| .. | .. |
|---|
| 1 | +/* |
|---|
| 2 | + * Copyright @ 2013 CurisTEC, S.A.S. All Rights Reserved. |
|---|
| 3 | + */ |
|---|
| 1 | 4 | package net.curisit.securis; |
|---|
| 2 | 5 | |
|---|
| 3 | 6 | import java.io.IOException; |
|---|
| .. | .. |
|---|
| 18 | 21 | |
|---|
| 19 | 22 | import net.curisit.securis.utils.GZipServletResponseWrapper; |
|---|
| 20 | 23 | |
|---|
| 24 | +/** |
|---|
| 25 | +* GzipFilter |
|---|
| 26 | +* <p> |
|---|
| 27 | +* Servlet filter that compresses <code>*.js</code> responses with GZIP when the client |
|---|
| 28 | +* advertises <code>Accept-Encoding: gzip</code>. |
|---|
| 29 | +*/ |
|---|
| 21 | 30 | @ApplicationScoped |
|---|
| 22 | 31 | @WebFilter(urlPatterns = "*.js") |
|---|
| 23 | 32 | public class GzipFilter implements Filter { |
|---|
| .. | .. |
|---|
| 25 | 34 | @SuppressWarnings("unused") |
|---|
| 26 | 35 | private static final Logger LOG = LogManager.getLogger(GzipFilter.class); |
|---|
| 27 | 36 | |
|---|
| 37 | + /** init<p>Filter init hook (unused). */ |
|---|
| 28 | 38 | @Override |
|---|
| 29 | 39 | public void init(FilterConfig fc) throws ServletException { |
|---|
| 30 | 40 | } |
|---|
| 31 | 41 | |
|---|
| 42 | + /** |
|---|
| 43 | + * doFilter |
|---|
| 44 | + * <p> |
|---|
| 45 | + * Wrap the response with a GZIP-compressing wrapper if supported by the client. |
|---|
| 46 | + */ |
|---|
| 32 | 47 | @Override |
|---|
| 33 | 48 | public void doFilter(ServletRequest sreq, ServletResponse sres, FilterChain chain) throws IOException, ServletException { |
|---|
| 34 | 49 | HttpServletRequest httpRequest = (HttpServletRequest) sreq; |
|---|
| .. | .. |
|---|
| 44 | 59 | } |
|---|
| 45 | 60 | } |
|---|
| 46 | 61 | |
|---|
| 62 | + /** |
|---|
| 63 | + * acceptsGZipEncoding |
|---|
| 64 | + * <p> |
|---|
| 65 | + * @return {@code true} when request header contains "gzip" in <code>Accept-Encoding</code>. |
|---|
| 66 | + */ |
|---|
| 47 | 67 | private boolean acceptsGZipEncoding(HttpServletRequest httpRequest) { |
|---|
| 48 | 68 | String acceptEncoding = httpRequest.getHeader("Accept-Encoding"); |
|---|
| 49 | 69 | |
|---|
| 50 | 70 | return acceptEncoding != null && acceptEncoding.indexOf("gzip") != -1; |
|---|
| 51 | 71 | } |
|---|
| 52 | 72 | |
|---|
| 73 | + /** destroy<p>Filter destroy hook (unused). */ |
|---|
| 53 | 74 | @Override |
|---|
| 54 | 75 | public void destroy() { |
|---|
| 55 | 76 | } |
|---|