How to transform the servlet response content

Introduction

In the previous post How to modify the servlet request we explained how to change the servlet request on the fly, using the servlet filter mechanism and the class HttpServletResponseWrapper. In the following paragraphs we are going to show how to change the response content just before it is sent back to the client.

How to tranform the http response content

We can tranform the servlet http response content just before it is sent back to the client, using the servlet filter mechanism and the class HttpServletResponseWrapper. The wrapper is needed because the output stream of the original response is handled and closed by the servlet engine. What we need is to take the original content generated by the servlet, transform it and writing it again in the response output stream, but we cannot do this in the normal request-response flow because as soon as the content is written to the response outputstream the latter gets closed and it not more possible to write anything in it. The solution is to wrap the response in an extension of the class HttpServletResponseWrapper and provide it with a custom outputstream. The wrapper is passed then to the chain execution of the filter instead of the original response and the servlet engine will write its content on the custom outputstream. Then the content will be taken, tranformed and written to the original response outputstream. In the following paragraph a simple example is shown.

Http response transformation example

The following is a simple example of how the all thing works. First of all we define a Wrapper that extends the HttpServletResponseWrapper:

public class CustomResponseWrapper extends
   HttpServletResponseWrapper {

   private CharArrayWriter output;

   public CharResponseWrapper(HttpServletResponse response){
      super(response);
      output = new CharArrayWriter();
   }

   public String getResponseContent() {
      return output.toString();
   }

   public PrintWriter getWriter(){
      return new PrintWriter(output);
   }
}

Then in our servlet filter’s doFilter method we implement the response transformation logic:

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

   PrintWriter out = response.getWriter();
   CustomResponseWrapper wrapper = new CustomResponseWrapper(
         (HttpServletResponse)response);

   chain.doFilter(request, wrapper);

   CharArrayWriter writer = new CharArrayWriter();
   String originalContent = wrapper.getResponseContent();
   writer.write("<h1>Added Title</h1>"); 
   writer.write(originalContent.substring(originalContent.indexOf("") 
   + "".length + 1));
   response.setContentLength(writer.toString().length()); 
   out.write(writer.toString()); 
   out.close(); 
}

Here the doFilter in the chain object is executed with the wrapper instead of the original response. When the chain’s doFilter execution completes the content is taken from the wrapper , gets manipulated using a charArrayWriter and a HTML h1 title is added to it. Finally the changed content is written to the original response outputstream and the latter is closed.

How to transform the servlet response content last modified: 2015-04-20T22:38:30+00:00 by Mario Casari

Leave a Reply

Your email address will not be published. Required fields are marked *