Clover Coverage Report - Atlassian Core
Coverage timestamp: Sun Nov 30 2008 18:33:35 CST
44   183   30   3.38
22   125   0.68   6.5
13     2.31  
2    
 
 
  AbstractEncodingFilter       Line # 16 23 20 0% 0.0
  AbstractEncodingFilter.WordCurlyQuotesRequestWrapper       Line # 120 21 10 0% 0.0
 
No Tests
 
1    package com.atlassian.core.filters;
2   
3    import com.atlassian.core.util.StringUtils;
4    import com.opensymphony.util.TextUtils;
5   
6    import javax.servlet.*;
7    import javax.servlet.http.HttpServletRequest;
8    import javax.servlet.http.HttpServletResponse;
9    import javax.servlet.http.HttpServletResponseWrapper;
10    import javax.servlet.http.HttpServletRequestWrapper;
11    import java.io.IOException;
12    import java.util.Map;
13    import java.util.HashMap;
14    import java.util.Iterator;
15   
 
16    public abstract class AbstractEncodingFilter extends AbstractFilter
17    {
 
18  0 toggle public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException
19    {
20  0 servletRequest.setCharacterEncoding(getEncoding());
21  0 servletResponse.setContentType(getContentType());
22   
23    // prevent caching of JSPs
24  0 if (servletResponse instanceof HttpServletResponse)
25    {
26  0 HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
27  0 HttpServletRequest req = (HttpServletRequest) servletRequest;
28   
29  0 if (isNonCachableUri(req))
30  0 setNonCachingHeaders(httpServletResponse);
31    }
32   
33  0 filterChain.doFilter(new WordCurlyQuotesRequestWrapper(servletRequest),
34    new HttpServletResponseWrapper((HttpServletResponse) servletResponse)
35    {
 
36  0 toggle public void setContentType(String s)
37    {
38    //JRun passes a null content type sometimes
39  0 if (s != null && s.length() > "text/html".length() && s.charAt(0) == 't' && s.startsWith("text/html"))
40    {
41    //do nothing. This call could be trying to set the charset to another charset.
42    //This is the case with Tomcat & Jetty, whose JSP compiler sets the charset, whether it
43    //is specified in the JSP page or not.
44   
45    //So - if someone else is trying to set the content type to HTML, then they may also want
46    //to override the charset. So - we just don't allow them to override the content-type
47    //if they are just setting to text/html. If they want to overried with 'gif' or 'pdf'
48    //then go straight ahead. Who are we to question the app server. I mean...really....
49   
50    //NB - this can also be accomplished by setting the charset manually in the JSP page & the decorator,
51    //but this approach allows for run-time flexibility of choosing the charsets.
52    }
53    else
54    {
55    // This block is needed here because not all app servers, weblogic to be specific,
56    // are smart enough to keep the original charset when doing a setContentType with only
57    // the content type and not having the "charset=" portion of the string.
58    // Weblogic will actually fall back to ISO-8859-1 in this case. This can be quite
59    // a bad thing if you want to display international characters. SO, we make sure
60    // that we always add the original character set to the content type string so
61    // that in every app server we will never loose the original character encoding.
62  0 if (s != null && s.trim().length() == "text/html".length() && s.charAt(0) == 't' && s.startsWith("text/html"))
63    {
64  0 String currentCharSet = getResponse().getCharacterEncoding();
65  0 s += ";charset=" + currentCharSet;
66    }
67  0 super.setContentType(s);
68    }
69    }
70   
71   
 
72  0 toggle public void setHeader(String string, String string1)
73    {
74    //Opera 7.1+ reads the 'content-location' header and uses it as the href base for
75    //relative URLs.
76    //However, in Orion < 2.0.2 this header did not contain the correct context path
77    //However, even when it is set, it points to the JSP view not the original request URL
78    //So - we just prevent it being set.
79  0 if (!isContentLocationHeader(string))
80  0 super.setHeader(string, string1);
81    }
82   
 
83  0 toggle public void addHeader(String string, String string1)
84    {
85    //See setHeader above
86  0 if (!isContentLocationHeader(string))
87  0 super.addHeader(string, string1);
88    }
89   
 
90  0 toggle private boolean isContentLocationHeader(String headerName)
91    {
92  0 return headerName != null && "content-location".equalsIgnoreCase(headerName);
93    }
94   
95    });
96    }
97   
 
98  0 toggle protected void setNonCachingHeaders(HttpServletResponse httpServletResponse)
99    {
100  0 httpServletResponse.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // http 1.1
101  0 httpServletResponse.setHeader("Pragma", "no-cache"); // http 1.0
102  0 httpServletResponse.setDateHeader("Expires", 0); // prevent proxy caching
103    }
104   
 
105  0 toggle public void init(FilterConfig filterConfig)
106    {
107    }
108   
109    // override this method to prevent caching of other file types
 
110  0 toggle protected boolean isNonCachableUri(HttpServletRequest req)
111    {
112  0 String uri = TextUtils.noNull(req.getRequestURI());
113  0 return uri.indexOf(".jsp") > 0 || uri.indexOf(".jspa") > 0;
114    }
115   
116    protected abstract String getEncoding();
117   
118    protected abstract String getContentType();
119   
 
120    private class WordCurlyQuotesRequestWrapper extends HttpServletRequestWrapper
121    {
122    private Map parameterValueCache = new HashMap();
123    private Map parameterMap = null;
124   
 
125  0 toggle public WordCurlyQuotesRequestWrapper(ServletRequest servletRequest)
126    {
127  0 super((HttpServletRequest) servletRequest);
128    }
129   
 
130  0 toggle public String getParameter(String string)
131    {
132  0 return escapeString(super.getParameter(string));
133    }
134   
 
135  0 toggle protected String escapeString(String string)
136    {
137  0 return StringUtils.escapeCP1252(string, getEncoding());
138    }
139   
140   
 
141  0 toggle public Map getParameterMap()
142    {
143  0 if (parameterMap == null)
144    {
145  0 parameterMap = new HashMap();
146  0 Map originalMap = super.getParameterMap();
147  0 for (Iterator i = originalMap.keySet().iterator(); i.hasNext();)
148    {
149  0 String key = (String) i.next();
150  0 parameterMap.put(key, getParameterValues(key));
151    }
152    }
153  0 return parameterMap;
154    }
155   
156   
 
157  0 toggle public String[] getParameterValues(String string)
158    {
159  0 String[] returnValue = (String[]) parameterValueCache.get(string);
160   
161    //if we haven't yet cached this - look it up.
162  0 if (returnValue == null)
163    {
164  0 String[] parameterValues = super.getParameterValues(string);
165   
166    //values could be null - don't bother converting them
167  0 if (parameterValues == null)
168  0 return null;
169   
170  0 for (int i = 0; i < parameterValues.length; i++)
171    {
172  0 String parameterValue = escapeString(parameterValues[i]);
173  0 parameterValues[i] = parameterValue;
174    }
175   
176  0 parameterValueCache.put(string, parameterValues);
177  0 returnValue = parameterValues;
178   
179    }
180  0 return returnValue;
181    }
182    }
183    }