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

The following examples show how to use com.google.api.client.http.HttpRequest#execute() . 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: RetryUnsuccessfulResponseHandlerTest.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testDoesNotRetryAfterInterruption() throws IOException {
  MockSleeper sleeper = new MockSleeper() {
    @Override
    public void sleep(long millis) throws InterruptedException {
      super.sleep(millis);
      throw new InterruptedException();
    }
  };
  RetryUnsuccessfulResponseHandler handler = new RetryUnsuccessfulResponseHandler(
      testRetryConfig(sleeper));
  CountingLowLevelHttpRequest failingRequest = CountingLowLevelHttpRequest.fromStatus(503);
  HttpRequest request = TestUtils.createRequest(failingRequest);
  request.setUnsuccessfulResponseHandler(handler);
  request.setNumberOfRetries(MAX_RETRIES);

  try {
    request.execute();
    fail("No exception thrown for HTTP error");
  } catch (HttpResponseException e) {
    assertEquals(503, e.getStatusCode());
  }

  assertEquals(1, sleeper.getCount());
  assertEquals(1, failingRequest.getCount());
}
 
Example 2
Source File: CredentialTest.java    From google-oauth-java-client with Apache License 2.0 6 votes vote down vote up
private void subtestRefreshToken_request(AccessTokenTransport transport, int expectedCalls)
    throws Exception {
  Credential credential =
      new Credential.Builder(BearerToken.queryParameterAccessMethod()).setTransport(transport)
          .setJsonFactory(JSON_FACTORY)
          .setTokenServerUrl(TOKEN_SERVER_URL)
          .setClientAuthentication(new BasicAuthentication(CLIENT_ID, CLIENT_SECRET))
          .build()
          .setRefreshToken(REFRESH_TOKEN)
          .setAccessToken(ACCESS_TOKEN);
  HttpRequestFactory requestFactory = transport.createRequestFactory(credential);
  HttpRequest request = requestFactory.buildDeleteRequest(HttpTesting.SIMPLE_GENERIC_URL);
  request.setThrowExceptionOnExecuteError(false);
  request.execute();

  assertEquals(expectedCalls, transport.calls);
}
 
Example 3
Source File: Auth.java    From datacollector with Apache License 2.0 5 votes vote down vote up
public boolean enable(String path, String type, String description) throws VaultException {
  Map<String, Object> data = new HashMap<>();
  data.put("type", type);

  if (description != null) {
    data.put("description", description);
  }

  HttpContent content = new JsonHttpContent(getJsonFactory(), data);

  try {
    HttpRequest request = getRequestFactory().buildRequest(
        "POST",
        new GenericUrl(getConf().getAddress() + "/v1/sys/auth/" + path),
        content
    );
    HttpResponse response = request.execute();
    if (!response.isSuccessStatusCode()) {
      LOG.error("Request failed status: {} message: {}", response.getStatusCode(), response.getStatusMessage());
    }

    return response.isSuccessStatusCode();
  } catch (IOException e) {
    LOG.error(e.toString(), e);
    throw new VaultException("Failed to authenticate: " + e.toString(), e);
  }
}
 
Example 4
Source File: HttpClient.java    From steem-java-api-wrapper with GNU General Public License v3.0 5 votes vote down vote up
@Override
public JsonRPCResponse invokeAndReadResponse(JsonRPCRequest requestObject, URI endpointUri,
        boolean sslVerificationDisabled) throws SteemCommunicationException {
    try {
        NetHttpTransport.Builder builder = new NetHttpTransport.Builder();
        // Disable SSL verification if needed
        if (sslVerificationDisabled && endpointUri.getScheme().equals("https")) {
            builder.doNotValidateCertificate();
        }

        String requestPayload = requestObject.toJson();
        HttpRequest httpRequest = builder.build().createRequestFactory(new HttpClientRequestInitializer())
                .buildPostRequest(new GenericUrl(endpointUri),
                        ByteArrayContent.fromString("application/json", requestPayload));

        LOGGER.debug("Sending {}.", requestPayload);

        HttpResponse httpResponse = httpRequest.execute();

        int status = httpResponse.getStatusCode();
        String responsePayload = httpResponse.parseAsString();

        if (status >= 200 && status < 300 && responsePayload != null) {
            return new JsonRPCResponse(CommunicationHandler.getObjectMapper().readTree(responsePayload));
        } else {
            throw new ClientProtocolException("Unexpected response status: " + status);
        }

    } catch (GeneralSecurityException | IOException e) {
        throw new SteemCommunicationException("A problem occured while processing the request.", e);
    }
}
 
Example 5
Source File: GoogleJsonResponseExceptionTest.java    From google-api-java-client with Apache License 2.0 5 votes vote down vote up
public void testFrom_withDetails() throws Exception {
  HttpTransport transport = new ErrorTransport();
  HttpRequest request =
      transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
  request.setThrowExceptionOnExecuteError(false);
  HttpResponse response = request.execute();
  GoogleJsonResponseException ge =
      GoogleJsonResponseException.from(GoogleJsonErrorTest.FACTORY, response);
  assertEquals(GoogleJsonErrorTest.ERROR, GoogleJsonErrorTest.FACTORY.toString(ge.getDetails()));
  assertTrue(
      ge.getMessage(), ge.getMessage().startsWith("403" + StringUtils.LINE_SEPARATOR + "{"));
}
 
Example 6
Source File: GoogleJsonResponseExceptionHelper.java    From java-monitoring-client-library with Apache License 2.0 5 votes vote down vote up
public static HttpResponse createHttpResponse(int statusCode, InputStream content)
    throws IOException {
  FakeHttpTransport transport = new FakeHttpTransport(statusCode, content);
  HttpRequestFactory factory = transport.createRequestFactory();
  HttpRequest request =
      factory.buildRequest(
          "foo", new GenericUrl("http://example.com/bar"), new EmptyHttpContent());
  request.setThrowExceptionOnExecuteError(false);
  return request.execute();
}
 
Example 7
Source File: GoogleHttpClient.java    From feign with Apache License 2.0 5 votes vote down vote up
@Override
public final Response execute(final Request inputRequest,
                              final Request.Options options)
    throws IOException {
  final HttpRequest request = convertRequest(inputRequest, options);
  final HttpResponse response = request.execute();
  return convertResponse(inputRequest, response);
}
 
Example 8
Source File: IntegrationTestUtils.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
public ResponseInfo put(String path, String json) throws IOException {
  String url = options.getDatabaseUrl() + path + "?access_token=" + getToken();
  HttpRequest request = requestFactory.buildPutRequest(new GenericUrl(url),
      ByteArrayContent.fromString("application/json", json));
  HttpResponse response = null;
  try {
    response = request.execute();
    return new ResponseInfo(response);
  } finally {
    if (response != null) {
      response.disconnect();
    }
  }
}
 
Example 9
Source File: RetryHttpInitializerTest.java    From hadoop-connectors with Apache License 2.0 5 votes vote down vote up
/** Helper for test cases wanting to test retries kicking in for particular error codes. */
private void testRetriesForErrorCode(int code) throws Exception {
  final String authHeaderValue = "Bearer a1b2c3d4";
  final HttpRequest req = requestFactory.buildGetRequest(new GenericUrl("http://fake-url.com"));
  assertThat(req.getHeaders().getUserAgent()).isEqualTo("foo-user-agent");
  assertThat(req.getInterceptor()).isEqualTo(mockCredential);

  // Simulate the actual behavior of inserting a header for the credential.
  doAnswer(
          unused -> {
            req.getHeaders().setAuthorization(authHeaderValue);
            return null;
          })
      .doAnswer(
          unused -> {
            req.getHeaders().setAuthorization(authHeaderValue);
            return null;
          })
      .when(mockCredential)
      .intercept(eq(req));

  when(mockLowLevelRequest.execute())
      .thenReturn(mockLowLevelResponse)
      .thenReturn(mockLowLevelResponse);
  when(mockLowLevelResponse.getStatusCode())
      .thenReturn(code)
      .thenReturn(200);
  when(mockCredential.handleResponse(eq(req), any(HttpResponse.class), eq(true)))
      .thenReturn(false);

  HttpResponse res = req.execute();
  assertThat(res).isNotNull();

  verify(mockCredential, times(2)).intercept(eq(req));
  verify(mockLowLevelRequest, times(2)).addHeader(eq("Authorization"), eq(authHeaderValue));
  verify(mockLowLevelRequest, times(2)).execute();
  verify(mockLowLevelResponse, times(2)).getStatusCode();
  verify(mockCredential).handleResponse(eq(req), any(HttpResponse.class), eq(true));
  verify(mockSleeper).sleep(anyLong());
}
 
Example 10
Source File: DeezerApi.java    From data-transfer-project with Apache License 2.0 5 votes vote down vote up
private String makePostRequest(String url, Map<String, String> params) throws IOException {
  HttpRequestFactory requestFactory = httpTransport.createRequestFactory();
  StringBuilder extraArgs = new StringBuilder();
  params.entrySet().forEach(entry -> {
    try {
      extraArgs
          .append("&")
          .append(entry.getKey())
          .append("=")
          .append(URLEncoder.encode(entry.getValue(), "UTF8"));
    } catch (UnsupportedEncodingException e) {
      throw new IllegalArgumentException(e);
    }
  });
  HttpRequest getRequest =
      requestFactory.buildGetRequest(
          new GenericUrl(url
              + "?output=json&request_method=post&access_token=" + accessToken
              + extraArgs));
  perUserRateLimiter.acquire();
  HttpResponse response = getRequest.execute();
  int statusCode = response.getStatusCode();
  if (statusCode != 200) {
    throw new IOException(
        "Bad status code: " + statusCode + " error: " + response.getStatusMessage());
  }
  String result =
      CharStreams.toString(new InputStreamReader(response.getContent(), Charsets.UTF_8));
  return result;
}
 
Example 11
Source File: Authorizer.java    From gcp-token-broker with Apache License 2.0 5 votes vote down vote up
private static UserInfo getUserInfo(Credential credential) throws IOException {
    HttpRequest request = HTTP_TRANSPORT
        .createRequestFactory(credential)
        .buildGetRequest(new GenericUrl(USER_INFO_URI));
    request.getHeaders().setContentType("application/json");
    request.setParser(new JsonObjectParser(JSON_FACTORY));
    HttpResponse response = request.execute();
    return response.parseAs(UserInfo.class);
}
 
Example 12
Source File: CredentialTest.java    From google-oauth-java-client with Apache License 2.0 5 votes vote down vote up
private HttpRequest subtestConstructor(Credential credential) throws Exception {
  MockHttpTransport transport = new MockHttpTransport();
  HttpRequestFactory requestFactory = transport.createRequestFactory(credential);
  HttpRequest request = requestFactory.buildDeleteRequest(HttpTesting.SIMPLE_GENERIC_URL);
  request.execute();
  return request;
}
 
Example 13
Source File: TaskQueueNotificationServlet.java    From abelana with Apache License 2.0 5 votes vote down vote up
@Override
public final void doPost(final HttpServletRequest req, final HttpServletResponse resp)
    throws IOException {
  HttpTransport httpTransport;
  try {
    Map<Object, Object> params = new HashMap<>();
    params.putAll(req.getParameterMap());
    params.put("task", req.getHeader("X-AppEngine-TaskName"));

    httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    GoogleCredential credential = GoogleCredential.getApplicationDefault()
        .createScoped(Collections.singleton("https://www.googleapis.com/auth/userinfo.email"));
    HttpRequestFactory requestFactory = httpTransport.createRequestFactory();
    GenericUrl url = new GenericUrl(ConfigurationConstants.IMAGE_RESIZER_URL);

    HttpRequest request = requestFactory.buildPostRequest(url, new UrlEncodedContent(params));
    credential.initialize(request);

    HttpResponse response = request.execute();
    if (!response.isSuccessStatusCode()) {
      log("Call to the imageresizer failed: " + response.getContent().toString());
      resp.setStatus(HttpStatusCodes.STATUS_CODE_SERVER_ERROR);
    } else {
      resp.setStatus(response.getStatusCode());
    }

  } catch (GeneralSecurityException | IOException e) {
    log("Http request error: " + e.getMessage());
    resp.setStatus(HttpStatusCodes.STATUS_CODE_SERVER_ERROR);
  }
}
 
Example 14
Source File: GoogleJsonErrorTest.java    From google-api-java-client with Apache License 2.0 5 votes vote down vote up
public void testParse() throws Exception {
  HttpTransport transport = new ErrorTransport();
  HttpRequest request =
      transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
  request.setThrowExceptionOnExecuteError(false);
  HttpResponse response = request.execute();
  GoogleJsonError errorResponse = GoogleJsonError.parse(FACTORY, response);
  assertEquals(ERROR, FACTORY.toString(errorResponse));
}
 
Example 15
Source File: RetryHttpInitializerTest.java    From hadoop-connectors with Apache License 2.0 5 votes vote down vote up
@Test
public void testBasicOperation() throws IOException {
  final String authHeaderValue = "Bearer a1b2c3d4";
  final HttpRequest req = requestFactory.buildGetRequest(new GenericUrl("http://fake-url.com"));
  assertThat(req.getHeaders().getUserAgent()).isEqualTo("foo-user-agent");
  assertThat(req.getHeaders().get("header-key")).isEqualTo("header=value");
  assertThat(req.getInterceptor()).isEqualTo(mockCredential);

  // Simulate the actual behavior of inserting a header for the credential.
  doAnswer(
          unused -> {
            req.getHeaders().setAuthorization(authHeaderValue);
            return null;
          })
      .when(mockCredential)
      .intercept(eq(req));

  when(mockLowLevelRequest.execute())
      .thenReturn(mockLowLevelResponse);
  when(mockLowLevelResponse.getStatusCode())
      .thenReturn(200);

  HttpResponse res = req.execute();
  assertThat(res).isNotNull();

  verify(mockCredential).intercept(eq(req));
  verify(mockLowLevelRequest).addHeader(eq("Authorization"), eq(authHeaderValue));
  verify(mockLowLevelRequest).execute();
  verify(mockLowLevelResponse).getStatusCode();
}
 
Example 16
Source File: MediaHttpDownloader.java    From google-api-java-client with Apache License 2.0 5 votes vote down vote up
/**
 * Executes the current request.
 *
 * @param currentRequestLastBytePos last byte position for current request
 * @param requestUrl request URL where the download requests will be sent
 * @param requestHeaders request headers or {@code null} to ignore
 * @param outputStream destination output stream
 * @return HTTP response
 */
private HttpResponse executeCurrentRequest(long currentRequestLastBytePos, GenericUrl requestUrl,
    HttpHeaders requestHeaders, OutputStream outputStream) throws IOException {
  // prepare the GET request
  HttpRequest request = requestFactory.buildGetRequest(requestUrl);
  // add request headers
  if (requestHeaders != null) {
    request.getHeaders().putAll(requestHeaders);
  }
  // set Range header (if necessary)
  if (bytesDownloaded != 0 || currentRequestLastBytePos != -1) {
    StringBuilder rangeHeader = new StringBuilder();
    rangeHeader.append("bytes=").append(bytesDownloaded).append("-");
    if (currentRequestLastBytePos != -1) {
      rangeHeader.append(currentRequestLastBytePos);
    }
    request.getHeaders().setRange(rangeHeader.toString());
  }
  // execute the request and copy into the output stream
  HttpResponse response = request.execute();
  try {
    ByteStreams.copy(response.getContent(), outputStream);
  } finally {
    response.disconnect();
  }
  return response;
}
 
Example 17
Source File: MockHttpTransportTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
public void testBuildGetRequest_preservesLoLevelHttpRequest() throws Exception {
  MockHttpTransport httpTransport = new MockHttpTransport();
  GenericUrl url = new GenericUrl("http://example.org");
  HttpRequestFactory requestFactory = httpTransport.createRequestFactory();
  HttpRequest request = requestFactory.buildGetRequest(url);
  request.getHeaders().set("foo", "bar");
  Object unusedOnlyInspectingSideEffects = request.execute();
  MockLowLevelHttpRequest actualRequest = httpTransport.getLowLevelHttpRequest();
  assertThat(actualRequest.getHeaders()).containsKey("foo");
  assertThat(actualRequest.getHeaders().get("foo")).containsExactly("bar");
}
 
Example 18
Source File: BatchRequest.java    From google-api-java-client with Apache License 2.0 4 votes vote down vote up
/**
 * Executes all queued HTTP requests in a single call, parses the responses and invokes callbacks.
 *
 * <p>
 * Calling {@link #execute()} executes and clears the queued requests. This means that the
 * {@link BatchRequest} object can be reused to {@link #queue} and {@link #execute()} requests
 * again.
 * </p>
 */
public void execute() throws IOException {
  boolean retryAllowed;
  Preconditions.checkState(!requestInfos.isEmpty());

  // Log a warning if the user is using the global batch endpoint. In the future, we can turn this
  // into a preconditions check.
  if (GLOBAL_BATCH_ENDPOINT.equals(this.batchUrl.toString())) {
    LOGGER.log(Level.WARNING, GLOBAL_BATCH_ENDPOINT_WARNING);
  }

  HttpRequest batchRequest = requestFactory.buildPostRequest(this.batchUrl, null);
  // NOTE: batch does not support gzip encoding
  HttpExecuteInterceptor originalInterceptor = batchRequest.getInterceptor();
  batchRequest.setInterceptor(new BatchInterceptor(originalInterceptor));
  int retriesRemaining = batchRequest.getNumberOfRetries();

  do {
    retryAllowed = retriesRemaining > 0;
    MultipartContent batchContent = new MultipartContent();
    batchContent.getMediaType().setSubType("mixed");
    int contentId = 1;
    for (RequestInfo<?, ?> requestInfo : requestInfos) {
      batchContent.addPart(new MultipartContent.Part(
          new HttpHeaders().setAcceptEncoding(null).set("Content-ID", contentId++),
          new HttpRequestContent(requestInfo.request)));
    }
    batchRequest.setContent(batchContent);
    HttpResponse response = batchRequest.execute();
    BatchUnparsedResponse batchResponse;
    try {
      // Find the boundary from the Content-Type header.
      String boundary = "--" + response.getMediaType().getParameter("boundary");

      // Parse the content stream.
      InputStream contentStream = response.getContent();
      batchResponse =
          new BatchUnparsedResponse(contentStream, boundary, requestInfos, retryAllowed);

      while (batchResponse.hasNext) {
        batchResponse.parseNextResponse();
      }
    } finally {
      response.disconnect();
    }

    List<RequestInfo<?, ?>> unsuccessfulRequestInfos = batchResponse.unsuccessfulRequestInfos;
    if (!unsuccessfulRequestInfos.isEmpty()) {
      requestInfos = unsuccessfulRequestInfos;
    } else {
      break;
    }
    retriesRemaining--;
  } while (retryAllowed);
  requestInfos.clear();
}
 
Example 19
Source File: SplunkHttpSinkTask.java    From kafka-connect-splunk with Apache License 2.0 4 votes vote down vote up
@Override
public void put(Collection<SinkRecord> collection) {
  if (collection.isEmpty()) {
    log.trace("No records in collection.");
    return;
  }

  try {
    log.trace("Posting {} message(s) to {}", collection.size(), this.eventCollectorUrl);

    SinkRecordContent sinkRecordContent = new SinkRecordContent(collection);

    if (log.isTraceEnabled()) {
      try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
        sinkRecordContent.writeTo(outputStream);
        outputStream.flush();
        byte[] buffer = outputStream.toByteArray();
        log.trace("Posting\n{}", new String(buffer, "UTF-8"));
      } catch (IOException ex) {
        if (log.isTraceEnabled()) {
          log.trace("exception thrown while previewing post", ex);
        }
      }
    }

    HttpRequest httpRequest = this.httpRequestFactory.buildPostRequest(this.eventCollectorUrl, sinkRecordContent);
    HttpResponse httpResponse = httpRequest.execute();

    if (httpResponse.getStatusCode() == 403) {
      throw new ConnectException("Authentication was not successful. Please check the token with Splunk.");
    }

    if (httpResponse.getStatusCode() == 417) {
      log.warn("This exception happens when too much content is pushed to splunk per call. Look at this blog post " +
          "http://blogs.splunk.com/2016/08/12/handling-http-event-collector-hec-content-length-too-large-errors-without-pulling-your-hair-out/" +
          " Setting consumer.max.poll.records to a lower value will decrease the number of message posted to Splunk " +
          "at once.");
      throw new ConnectException("Status 417: Content-Length of XXXXX too large (maximum is 1000000). Verify Splunk config or " +
          " lower the value in consumer.max.poll.records.");
    }

    if (JSON_MEDIA_TYPE.equalsIgnoreParameters(httpResponse.getMediaType())) {
      SplunkStatusMessage statusMessage = httpResponse.parseAs(SplunkStatusMessage.class);

      if (!statusMessage.isSuccessful()) {
        throw new RetriableException(statusMessage.toString());
      }
    } else {
      throw new RetriableException("Media type of " + Json.MEDIA_TYPE + " was not returned.");
    }
  } catch (IOException e) {
    throw new RetriableException(
        String.format("Exception while posting data to %s.", this.eventCollectorUrl),
        e
    );
  }
}
 
Example 20
Source File: HttpEventPublisher.java    From DataflowTemplates with Apache License 2.0 4 votes vote down vote up
/**
 * Executes a POST for the list of {@link SplunkEvent} objects into Splunk's Http Event Collector
 * endpoint.
 *
 * @param events List of {@link SplunkEvent}s
 * @return {@link HttpResponse} for the POST.
 */
public HttpResponse execute(List<SplunkEvent> events) throws IOException {

  HttpContent content = getContent(events);
  HttpRequest request = requestFactory().buildPostRequest(genericUrl(), content);

  HttpBackOffUnsuccessfulResponseHandler responseHandler =
      new HttpBackOffUnsuccessfulResponseHandler(getConfiguredBackOff());

  responseHandler.setBackOffRequired(BackOffRequired.ON_SERVER_ERROR);

  request.setUnsuccessfulResponseHandler(responseHandler);
  setHeaders(request, token());

  return request.execute();
}