Interface HttpResponseInterceptor (1.43.2)

public interface HttpResponseInterceptor

HTTP response interceptor to intercept the end of HttpRequest#execute() before returning a successful response or throwing an exception for an unsuccessful response.

For example, this might be used to add a simple timer on requests:

public static class TimerResponseInterceptor implements HttpResponseInterceptor {

private final long startTime = System.nanoTime();

public void interceptResponse(HttpResponse response) { long elapsedNanos = System.nanoTime() - startTime; System.out.println("elapsed seconds: " + TimeUnit.NANOSECONDS.toSeconds(elapsedNanos) + "s"); } }

Sample usage with a request factory:

public static HttpRequestFactory createRequestFactory(HttpTransport transport) { return transport.createRequestFactory(new HttpRequestInitializer() {

@Override public void initialize(HttpRequest request) { request.setResponseInterceptor(new TimerResponseInterceptor()); } }); }

More complex usage example:

public static HttpRequestFactory createRequestFactory2(HttpTransport transport) { final HttpResponseInterceptor responseInterceptor = new TimerResponseInterceptor(); return transport.createRequestFactory(new HttpRequestInitializer() {

public void initialize(HttpRequest request) { request.setResponseInterceptor(new HttpResponseInterceptor() {

public void interceptResponse(HttpResponse response) throws IOException { responseInterceptor.interceptResponse(response); } }); } }); }

Implementations should normally be thread-safe.

Methods

interceptResponse(HttpResponse response)

public abstract void interceptResponse(HttpResponse response)

Invoked at the end of HttpRequest#execute() before returning a successful response or throwing an exception for an unsuccessful response.

Do not read from the content stream unless you intend to throw an exception. Otherwise, it would prevent the caller of HttpRequest#execute() to be able to read the stream from HttpResponse#getContent(). If you intend to throw an exception, you should parse the response, or alternatively pass the response as part of the exception.

Parameter
Name Description
response HttpResponse

HTTP response

Exceptions
Type Description
IOException