Smalltree1989
Smalltree1989

Reputation: 1158

Record Request and Response with Servlet Filter

i'm study to devolop a servlet/filter to use in a web application, the filter have to record every request on the site by all user of the web application. in my head the filter have to work in this way

filter -> request -> save request -> do.chain

public class ServletFilter implements Filter {
  private Application fApp;
 StringWriter ResponseRECORDER; 
 StringWriter RequestRECORDER;
  @Override
  public void init(FilterConfig config) throws ServletException {
    fApp = (Application)config.getServletContext().getAttribute("application");
  }

  @Override
  public void doFilter(ServletRequest request, ServletResponse response,
      FilterChain chain) throws IOException, ServletException {

    chain.doFilter(request,response);
    // Ignore non-http requests.
    if (!(request instanceof HttpServletRequest))
    {
        chain.doFilter(request,response);
        return;
    }

    ((HttpServletRequest)request).getSession();


    // Write the request out to the recording file.
    recordReqResHTTP((HttpServletRequest) request,
            (HttpServletResponse) response); 
    StringBuilder Fixed = new StringBuilder();
    Fixed.append("[Message]");
    Fixed.append("[time]");  
    Fixed.append(System.currentTimeMillis());
    Fixed.append("[/time]");
    Fixed.append("[Request]");
    Fixed.append(RequestRECORDER);
    Fixed.append("[/Request]");
    Fixed.append("[Response]");
    Fixed.append(ResponseRECORDER);
    Fixed.append("[/Response]");
    Fixed.append("[/Message]");

    MessagingService s = (MessagingService)fApp
        .getService("it.interprise.core.workflow.MessagingService");    
    try {
      s.send("recorder", Fixed.toString());
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

  @Override
  public void destroy() {
  }

  public void recordReqResHTTP(HttpServletRequest request,
     HttpServletResponse response)
{
  //HttpSession session = request.getSession();

  //costruisco una stringa per la raccolta dati
  StringWriter ResponseRECORDER = new StringWriter();  
  StringWriter RequestRECORDER = new StringWriter();

  try
  {

    //Registro la Request 

      PrintWriter out = new PrintWriter(RequestRECORDER);
      out.println("<request>");
      out.print("<uri>");
      out.print(request.getRequestURI());
      out.println("</uri>");
      Enumeration e = request.getParameterNames();
      while (e.hasMoreElements())
      {
          String paramName = (String) e.nextElement();
          String[] values = request.getParameterValues(paramName);
          for (int i=0; i < values.length; i++)
          {
              out.print("<param><name>");
              out.print(paramName);
              out.print("</name><value>");
              out.print(values[i]);
              out.println("</value></param>");
          }
      }
      out.println("</request>");
      out.close();
    //Registro la Response

      PrintWriter res = new PrintWriter(ResponseRECORDER);
      res.println("<request>");
      res.print("<uri>");
      res.print(request.getRequestURI());
      res.println("</uri>");
      Enumeration f = request.getParameterNames();
      while (f.hasMoreElements())
      {
          String paramName = (String) f.nextElement();
          String[] values = request.getParameterValues(paramName);
          for (int i=0; i < values.length; i++)
          {
              res.print("<param><name>");
              res.print(paramName);
              res.print("</name><value>");
              res.print(values[i]);
              res.println("</value></param>");
          }
      }
      out.println("</request>");
      out.close();
  }

  catch (Exception exc)
  {

  }
}
}

and the same for the response, have you any idea how i could solve the problem? With this filter the web application stop response...

thank you

Upvotes: 0

Views: 2757

Answers (1)

Michael
Michael

Reputation: 291

The first thing you should do is to log the Exception at the bottom of your example, at least. Maybe there is an Exception raised that cannot be identified by inspecting the code.

A few lines before you close the PrintWriter twice, which could lead to IOException.

Second: You declare instance variables ResponseRECORDER and RequestRECORDER, but later you declare two local variables with same name. Remove the instance variables - the filter must be implemented thread safe.

Once done, I'll guess we will see a hidden NullPointer.

Upvotes: 1

Related Questions