Clover Coverage Report - Atlassian Trusted Apps(Aggregated)
Coverage timestamp: Tue Jun 9 2009 19:34:44 CDT
26   76   14   4.33
14   62   0.54   6
6     2.33  
1    
 
 
  DefaultRequestValidator       Line # 10 26 14 97.8% 0.9782609
 
  (14)
 
1    package com.atlassian.security.auth.trustedapps;
2   
3    import java.util.StringTokenizer;
4   
5    import javax.servlet.http.HttpServletRequest;
6   
7    /**
8    * DefaultRequestValidor aggregates IP and URL matchers and throws an exception if they do not match.
9    */
 
10    public class DefaultRequestValidator implements RequestValidator
11    {
12    private final IPMatcher ipMatcher;
13    private final URLMatcher urlMatcher;
14   
 
15  14 toggle public DefaultRequestValidator(IPMatcher ipMatcher, URLMatcher urlMatcher)
16    {
17  14 Null.not("ipMatcher", ipMatcher);
18  12 Null.not("urlMatcher", urlMatcher);
19   
20  10 this.ipMatcher = ipMatcher;
21  10 this.urlMatcher = urlMatcher;
22    }
23   
 
24  7 toggle public void validate(HttpServletRequest request) throws InvalidRequestException
25    {
26  7 validateRemoteRequestIP(request);
27  6 validateXForwardedFor(request);
28  5 validateRequestURL(request);
29    }
30   
 
31  7 toggle private void validateRemoteRequestIP(HttpServletRequest request) throws InvalidIPAddressException
32    {
33  7 final String remoteAddr = request.getRemoteAddr();
34  7 if (!ipMatcher.match(remoteAddr))
35    {
36  1 throw new InvalidRemoteAddressException(remoteAddr);
37    }
38    }
39   
 
40  6 toggle private void validateXForwardedFor(HttpServletRequest request) throws InvalidXForwardedForAddressException
41    {
42  6 String forwardedFor = request.getHeader("X-Forwarded-For");
43  6 if (forwardedFor != null)
44    {
45  3 StringTokenizer tokenizer = new StringTokenizer(forwardedFor, ",");
46  7 while (tokenizer.hasMoreTokens())
47    {
48  5 String token = tokenizer.nextToken();
49  5 if (token.trim().length() > 0)
50    {
51  5 if (!ipMatcher.match(token.trim()))
52    {
53  1 throw new InvalidXForwardedForAddressException(token);
54    }
55    }
56    }
57    }
58    }
59   
 
60  5 toggle private void validateRequestURL(HttpServletRequest request) throws InvalidRequestUrlException
61    {
62  5 final String pathInfo = getPathInfo(request);
63  5 if (!urlMatcher.match(pathInfo))
64    {
65  1 throw new InvalidRequestUrlException(pathInfo);
66    }
67    }
68   
 
69  5 toggle private String getPathInfo(HttpServletRequest request)
70    {
71  5 String context = request.getContextPath();
72  5 String uri = request.getRequestURI();
73  5 if (context != null && context.length() > 0) return uri.substring(context.length());
74  4 else return uri;
75    }
76    }