Clover Coverage Report - Atlassian Core
Coverage timestamp: Sun Nov 30 2008 18:33:35 CST
20   93   10   4
6   61   0.5   5
5     2  
1    
 
 
  GzipFilter       Line # 23 20 10 0% 0.0
 
No Tests
 
1    package com.atlassian.core.filters.gzip;
2   
3    /*
4    * Copyright 2003 Jayson Falkner (jayson@jspinsider.com)
5    * This code is from "Servlets and JavaServer pages; the J2EE Web Tier",
6    * http://www.jspbook.com. You may freely use the code both commercially
7    * and non-commercially. If you like the code, please pick up a copy of
8    * the book and help support the authors, development of more free code,
9    * and the JSP/Servlet/J2EE community.
10   
11    */
12   
13    import com.atlassian.core.filters.AbstractFilter;
14    import org.apache.log4j.Category;
15    import javax.servlet.*;
16    import javax.servlet.http.HttpServletRequest;
17    import javax.servlet.http.HttpServletResponse;
18    import java.io.IOException;
19   
20    /**
21    * @deprecated Please use com.atlassian.gzipfilter.GzipFilter from the 'atlassian-gzipfilter' project instead.
22    */
 
23    public abstract class GzipFilter extends AbstractFilter
24    {
25    private static final Category log = Category.getInstance(GzipFilter.class);
26    private static final String ALREADY_FILTERED = GzipFilter.class.getName() + "_already_filtered";
27   
 
28  0 toggle public void init(FilterConfig filterConfig) throws ServletException
29    {
30  0 super.init(filterConfig);
31    }
32   
 
33  0 toggle public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException
34    {
35  0 if (req.getAttribute(ALREADY_FILTERED) != null)
36    {
37  0 chain.doFilter(req, res);
38  0 return;
39    }
40    else
41    {
42  0 req.setAttribute(ALREADY_FILTERED, Boolean.TRUE);
43    }
44   
45  0 if (req instanceof HttpServletRequest && useGzipForThisRequest((HttpServletRequest) req))
46    {
47  0 HttpServletRequest request = (HttpServletRequest) req;
48  0 HttpServletResponse response = (HttpServletResponse) res;
49  0 String ae = request.getHeader("accept-encoding");
50  0 if (ae != null && ae.indexOf("gzip") != -1 )
51    {
52  0 log.debug("GZIP supported, compressing.");
53   
54  0 GzipResponseWrapper wrappedResponse = new GzipResponseWrapper(response, getEncoding());
55  0 chain.doFilter(req, wrappedResponse);
56  0 wrappedResponse.finishResponse();
57  0 return;
58    }
59  0 chain.doFilter(req, res);
60    }
61    else
62    {
63  0 chain.doFilter(req, res);
64    }
65    }
66   
 
67  0 toggle protected boolean useGzipForThisRequest(HttpServletRequest req)
68    {
69  0 return (useGzip() && !isStyleSheet(req) && isTopLevelRequest(req));
70    }
71   
72    protected abstract boolean useGzip();
73   
 
74  0 toggle protected boolean isTopLevelRequest(HttpServletRequest request)
75    {
76  0 return request.getAttribute("javax.servlet.include.servlet_path") == null;
77    }
78   
79    /**
80    * stylesheets are not to be decorated (internet explorer bug)
81    */
 
82  0 toggle protected boolean isStyleSheet(HttpServletRequest request)
83    {
84  0 return request.getServletPath() != null && request.getServletPath().indexOf("css") != -1;
85    }
86   
87    /**
88    * Application dependent property which will be used to create an output stream for writing
89    * out the response (note - this is different to actually zipping the response, which is handled
90    * @return
91    */
92    protected abstract String getEncoding();
93    }