tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 
 Tools and Libs > Commons Http Client > How to add Response Interceptor

How to add Response Interceptor 

Commons Http Client is a HTTP agent implementation in java supporting client-side authentication, HTTP state management and HTTP connection management. This requires the libraries httpclient-4.1.2.jar, httpcore-4.1.2.jar, httpmime-4.1.2.jar, httpclient-cache-4.1.2.jar, commons-codec.jar, commons-io-1.3.2.jar and commons-logging-1.1.1.jar to be in classpath. The following example shows how to create a response Interceptor.

File Name  :  
com/bethecoder/tutorials/commons_httpclient/ResponseInterceptorTest.java 
Author  :  Sudhakar KV
Email  :  kvenkatasudhakar@gmail.com
   
package com.bethecoder.tutorials.commons_httpclient;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpResponse;
import org.apache.http.HttpResponseInterceptor;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.HttpEntityWrapper;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HttpContext;

public class ResponseInterceptorTest {

  /**
   @param args
   @throws IOException 
   @throws ClientProtocolException 
   */
  public static void main(String[] argsthrows ClientProtocolException, IOException {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet("http://localhost:8080/HTTP_TEST_APP/print_headers.jsp");
    System.out.println("Requesting : " + httpget.getURI());

    try {
      //Add response Interceptor
      httpclient.addResponseInterceptor(new HttpResponseInterceptor() {
        
        @Override
        public void process(HttpResponse response, HttpContext context)
            throws HttpException, IOException {
          
          //Add header
          response.addHeader("Tampered-By""UppercaseEntity Response Interceptor");
          
          //Set a new entity wrapper
          response.setEntity(new UppercaseEntity(response.getEntity()));
        }
      });
      
      ResponseHandler<String> responseHandler = new BasicResponseHandler();
            String responseBody = httpclient.execute(httpget, responseHandler);
            
      System.out.println("responseBody : " + responseBody);
    finally {
       httpclient.getConnectionManager().shutdown();
    }

  }

}

class UppercaseEntity extends HttpEntityWrapper {

    public UppercaseEntity(final HttpEntity entity) {
        super(entity);
    }

    public InputStream getContent() throws IOException, IllegalStateException {
      InputStream responseStream = wrappedEntity.getContent();
    String resposeString = IOUtils.toString(responseStream);
    
    //Get response in upper case
        return new ByteArrayInputStream(resposeString.toUpperCase().getBytes());
    }
}
   

A simple Web Application which prints the request headers through JSP.

File Name  :  
/HTTP_TEST_APP/WebContent/print_headers.jsp 

It gives the following output,
File Name  :  OUTPUT


                    Print

   



Books Recommendation

 
 
bl  br