Java Code Examples for com.google.api.client.http.HttpRequest#getResponseInterceptor()

The following examples show how to use com.google.api.client.http.HttpRequest#getResponseInterceptor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ChainingHttpRequestInitializer.java    From hadoop-connectors with Apache License 2.0 5 votes vote down vote up
@Override
public void initialize(HttpRequest request) throws IOException {
  List<HttpIOExceptionHandler> ioExceptionHandlers = new ArrayList<>();
  List<HttpUnsuccessfulResponseHandler> unsuccessfulResponseHandlers = new ArrayList<>();
  List<HttpExecuteInterceptor> interceptors = new ArrayList<>();
  List<HttpResponseInterceptor> responseInterceptors = new ArrayList<>();
  for (HttpRequestInitializer initializer : initializers) {
    initializer.initialize(request);
    if (request.getIOExceptionHandler() != null) {
      ioExceptionHandlers.add(request.getIOExceptionHandler());
      request.setIOExceptionHandler(null);
    }
    if (request.getUnsuccessfulResponseHandler() != null) {
      unsuccessfulResponseHandlers.add(request.getUnsuccessfulResponseHandler());
      request.setUnsuccessfulResponseHandler(null);
    }
    if (request.getInterceptor() != null) {
      interceptors.add(request.getInterceptor());
      request.setInterceptor(null);
    }
    if (request.getResponseInterceptor() != null) {
      responseInterceptors.add(request.getResponseInterceptor());
      request.setResponseInterceptor(null);
    }
  }
  request.setIOExceptionHandler(
      makeIoExceptionHandler(ioExceptionHandlers));
  request.setUnsuccessfulResponseHandler(
      makeUnsuccessfulResponseHandler(unsuccessfulResponseHandlers));
  request.setInterceptor(
      makeInterceptor(interceptors));
  request.setResponseInterceptor(
      makeResponseInterceptor(responseInterceptors));
}
 
Example 2
Source File: AbstractGoogleClientRequest.java    From google-api-java-client with Apache License 2.0 5 votes vote down vote up
/** 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());
  }
  httpRequest.setResponseReturnRawInputStream(returnRawInputStream);
  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;
}