View Javadoc

1   package com.atlassian.cache.servlet.resolver;
2   
3   import uk.ltd.getahead.dwr.AbstractDWRServlet;
4   import uk.ltd.getahead.dwr.DWRServlet;
5   
6   import javax.servlet.ServletConfig;
7   import javax.servlet.ServletException;
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.lang.reflect.InvocationTargetException;
13  import java.lang.reflect.Method;
14  
15  class BogusDwrServlet extends HttpServlet
16  {
17      private DWRServlet dwrServlet = new DWRServlet();
18  
19      public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException
20      {
21          try
22          {
23              Method method = findMethod();
24              if (method == null)
25              {
26                  throw new ServletException("Could not find doGet");
27              }
28              method.invoke(dwrServlet, new Object[] { req, resp });
29          }
30          catch (IllegalAccessException e)
31          {
32              throw new ServletException("Failed to fake DWR servlet", e);
33          }
34          catch (InvocationTargetException e)
35          {
36              throw new ServletException("Failed to fake DWR servlet", e);
37          }
38      }
39  
40      private Method findMethod()
41      {
42          Method method = null;
43  
44          Method[] methods = AbstractDWRServlet.class.getDeclaredMethods();
45          //Method[] methods = DWRServlet.class.getDeclaredMethods(); DWR 1.0
46          for (int i = 0; i < methods.length; i++)
47          {
48              Method aMethod = methods[i];
49              if (aMethod.getName().equals("doGet"))
50              {
51                  aMethod.setAccessible(true);
52                  method = aMethod;
53                  break;
54              }
55          }
56          return method;
57      }
58  
59      public void init(ServletConfig config) throws ServletException
60      {
61          dwrServlet.init(config);
62      }
63  }