Search in sources :

Example 1 with HttpResponseInterceptor

use of com.google.api.client.http.HttpResponseInterceptor in project google-api-java-client by google.

the class AbstractGoogleClientRequest method buildHttpRequest.

/**
 * Create a request suitable for use against this service.
 */
private HttpRequest buildHttpRequest(boolean usingHead) throws IOException {
    Preconditions.checkArgument(uploader == null);
    Preconditions.checkArgument(!usingHead || requestMethod.equals(HttpMethods.GET));
    String requestMethodToUse = usingHead ? HttpMethods.HEAD : requestMethod;
    final HttpRequest httpRequest = getAbstractGoogleClient().getRequestFactory().buildRequest(requestMethodToUse, buildHttpRequestUrl(), httpContent);
    new MethodOverride().intercept(httpRequest);
    httpRequest.setParser(getAbstractGoogleClient().getObjectParser());
    // custom methods may use POST with no content but require a Content-Length header
    if (httpContent == null && (requestMethod.equals(HttpMethods.POST) || requestMethod.equals(HttpMethods.PUT) || requestMethod.equals(HttpMethods.PATCH))) {
        httpRequest.setContent(new EmptyContent());
    }
    httpRequest.getHeaders().putAll(requestHeaders);
    if (!disableGZipContent) {
        httpRequest.setEncoding(new GZipEncoding());
    }
    final HttpResponseInterceptor responseInterceptor = httpRequest.getResponseInterceptor();
    httpRequest.setResponseInterceptor(new HttpResponseInterceptor() {

        public void interceptResponse(HttpResponse response) throws IOException {
            if (responseInterceptor != null) {
                responseInterceptor.interceptResponse(response);
            }
            if (!response.isSuccessStatusCode() && httpRequest.getThrowExceptionOnExecuteError()) {
                throw newExceptionOnError(response);
            }
        }
    });
    return httpRequest;
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) GZipEncoding(com.google.api.client.http.GZipEncoding) HttpResponseInterceptor(com.google.api.client.http.HttpResponseInterceptor) HttpResponse(com.google.api.client.http.HttpResponse) IOException(java.io.IOException) MethodOverride(com.google.api.client.googleapis.MethodOverride) EmptyContent(com.google.api.client.http.EmptyContent)

Aggregations

MethodOverride (com.google.api.client.googleapis.MethodOverride)1 EmptyContent (com.google.api.client.http.EmptyContent)1 GZipEncoding (com.google.api.client.http.GZipEncoding)1 HttpRequest (com.google.api.client.http.HttpRequest)1 HttpResponse (com.google.api.client.http.HttpResponse)1 HttpResponseInterceptor (com.google.api.client.http.HttpResponseInterceptor)1 IOException (java.io.IOException)1