3

In my application, I have 2 filters:

1) First, which throws an exception and I call response.sendError(UNAUTHORIZED.getStatusCode(), "My message");

2) Second for logging request and response to the database.

I have a column "error_message" and I want to save an error message from the filter 1 in the filter 2. How can I do this on weblogic? I have javax.servlet.http.HttpServletResponse and I can only take status: response.getStatus()

Using Spring I can call ServletRequestContext.current().getErrorMessage() but when I use weblogic it does not work.

Patres
  • 147
  • 2
  • 20
  • Did you check this post - https://stackoverflow.com/questions/2964250/jsp-getservletcontext-error ?? – Sachin Jun 20 '19 at 19:13
  • @Sachin Thanks, yes. But ServletContext does not have the getErrorMessage() function – Patres Jun 20 '19 at 19:33

1 Answers1

0

Instead of creating Custom HttpServletResponseWrapper.You can use ContentCachingResponseWrapper as it provide method getContentAsByteArray().

More here - How to read and copy the HTTP servlet response output stream content for logging

public void doFilterInternal(HttpServletRequest servletRequest, HttpServletResponse servletResponse,
        FilterChain filterChain) throws IOException, ServletException {

    HttpServletRequest request = servletRequest;
    HttpServletResponse response = servletResponse;
    ContentCachingRequestWrapper requestWrapper = new ContentCachingRequestWrapper(request);
    ContentCachingResponseWrapper responseWrapper =new ContentCachingResponseWrapper(response);
    try {
        super.doFilterInternal(requestWrapper, responseWrapper, filterChain);

    } finally {

        byte[] responseArray=responseWrapper.getContentAsByteArray();
        String responseStr=new String(responseArray,responseWrapper.getCharacterEncoding());
        System.out.println("string"+responseStr);       
        /*It is important to copy cached reponse body back to response stream
        to see response */
        responseWrapper.copyBodyToResponse();

    }

}