Clover Coverage Report - Atlassian Core
Coverage timestamp: Sun Nov 30 2008 18:33:35 CST
24   95   8   8
10   64   0.33   3
3     2.67  
1    
 
 
  AbstractNoOpServlet       Line # 23 24 8 0% 0.0
 
No Tests
 
1    package com.atlassian.core.servlet;
2   
3    import org.apache.log4j.Category;
4   
5    import javax.servlet.ServletException;
6    import javax.servlet.ServletRequest;
7    import javax.servlet.ServletResponse;
8    import javax.servlet.http.HttpServlet;
9    import javax.servlet.http.HttpServletRequest;
10    import javax.servlet.http.HttpServletResponse;
11    import java.io.IOException;
12    import java.util.Enumeration;
13   
14    /**
15    * NoOpServlet is a dummy servlet used only to provide a servlet mapping for url patterns that dont have any.
16    * This is necessary as some application servers like WebSphere 6.1.0.5 returns a 404 if there are no mapped servlet before
17    * applying filters to the request which could potentially change the URL mapped to a valid servlet. For example, the
18    * URLRewriter filter does this. Hence this dummy servlet should never handle any requests.
19    * <p>
20    * If this servlet receives a request, it will simply log all relevant information from the request that may be of
21    * help in determining why the request was received, as this would not be the desired result.
22    */
 
23    public abstract class AbstractNoOpServlet extends HttpServlet
24    {
25    private static final Category log = Category.getInstance(AbstractNoOpServlet.class);
26    private static final String RECEIVED_UNEXPECTED_REQUEST = "NoOpServlet received an unexpected request.";
27    private static final String UNABLE_TO_HANDLE_REQUEST = "Unable to handle request. Request is not a HttpServletRequest";
28   
 
29  0 toggle public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException
30    {
31  0 log.warn(RECEIVED_UNEXPECTED_REQUEST);
32   
33  0 if (!(servletRequest instanceof HttpServletRequest && servletResponse instanceof HttpServletResponse))
34    {
35  0 log.error(UNABLE_TO_HANDLE_REQUEST);
36  0 return;
37    }
38  0 HttpServletRequest request = (HttpServletRequest) servletRequest;
39  0 HttpServletResponse response = (HttpServletResponse) servletResponse;
40   
41    //log informations that may help in finding out why the request came here
42  0 logUserInformation(request);
43  0 logRequestInformation(request);
44   
45    //Let the user know that there is some error
46  0 response.sendError(404, RECEIVED_UNEXPECTED_REQUEST + " More information is available in the log file.");
47    }
48   
 
49  0 toggle private void logUserInformation(HttpServletRequest request)
50    {
51  0 String username = getUserName(request);
52  0 if (username != null)
53    {
54  0 log.warn("User: " + username);
55    }
56    else
57    {
58  0 log.warn("User: Anonymous (Not logged in)");
59    }
60    }
61   
62    /**
63    * get the current username from the request
64    * @param request to retrieve the usernaem from
65    * @return current username
66    */
67    protected abstract String getUserName(HttpServletRequest request);
68   
69    /**
70    * Logs relevant information about the request such as the request URL and the query string
71    * @param request request to log information about
72    */
 
73  0 toggle private void logRequestInformation(HttpServletRequest request)
74    {
75  0 try
76    {
77  0 log.warn("Request Information");
78  0 log.warn("- Request URL: " + request.getRequestURL());
79  0 log.warn("- Query String: " + (request.getQueryString() == null ? "" : request.getQueryString()));
80   
81  0 log.warn("Request Attributes");
82  0 Enumeration attributeNames = request.getAttributeNames();
83  0 while (attributeNames.hasMoreElements())
84    {
85  0 String name = (String) attributeNames.nextElement();
86  0 Object attribute = request.getAttribute(name);
87  0 log.warn("- " + name + ": " + (attribute == null ? "null" : attribute.toString()));
88    }
89    }
90    catch (Throwable t)
91    {
92  0 log.error("Error rendering logging information" + t);
93    }
94    }
95    }