private HttpResponse putRequest(
     byte[] bytes,
     int position,
     int length,
     String fileType,
     HttpRequestFactory requestFactory,
     long size,
     String location)
     throws HttpResponseException {
   try {
     InputStreamContent inputStreamContent =
         new InputStreamContent(fileType, new ByteArrayInputStream(bytes));
     HttpRequest chunkRequest =
         requestFactory.buildPutRequest(new DocsUrl(location), inputStreamContent);
     chunkRequest.getHeaders().setContentRange(range(position, length, size));
     chunkRequest.getHeaders().setContentLength((long) length);
     return requestExecutor.executeNonSensitive(chunkRequest);
   } catch (HttpResponseException e) {
     throw e;
   } catch (Exception e) {
     String message = "unable to upload chunk number " + position + ", " + e.getMessage();
     logger.error(message);
     throw new CoreException(message, e);
   }
 }
  @Override
  public GameState call() throws Exception {
    HttpContent content;
    HttpRequest request;
    HttpResponse response;
    GameState gameState = null;
    AdvancedGameState advancedGameState;

    try {
      // Initial request
      logger.info("Sending initial request...");
      content = new UrlEncodedContent(apiKey);
      request = REQUEST_FACTORY.buildPostRequest(gameUrl, content);
      request.setReadTimeout(0); // Wait forever to be assigned to a game
      response = request.execute();
      gameState = response.parseAs(GameState.class);
      logger.info("Game URL: {}", gameState.getViewUrl());

      advancedGameState = new AdvancedGameState(gameState);

      // Game loop
      while (!gameState.getGame().isFinished() && !gameState.getHero().isCrashed()) {
        logger.info("Taking turn " + gameState.getGame().getTurn());
        BotMove direction = bot.move(advancedGameState);
        Move move = new Move(apiKey.getKey(), direction.toString());

        HttpContent turn = new UrlEncodedContent(move);
        HttpRequest turnRequest =
            REQUEST_FACTORY.buildPostRequest(new GenericUrl(gameState.getPlayUrl()), turn);
        HttpResponse turnResponse = turnRequest.execute();

        gameState = turnResponse.parseAs(GameState.class);
        advancedGameState = new AdvancedGameState(advancedGameState, gameState);
      }

    } catch (Exception e) {
      logger.error("Error during game play", e);
    }

    logger.info("Game over");
    return gameState;
  }
Пример #3
0
  @Test
  public void test() throws IOException {

    HttpTransport transport = injector.getInstance(HttpTransport.class);
    JsonFactory jsonFactory = injector.getInstance(JsonFactory.class);

    DriveManager drive =
        new DriveManager(
            transport, jsonFactory, GoogleOAuth2.getGlobalCredential(transport, jsonFactory));
    Drive service = drive.getClient();
    File file = service.files().get("1HyyFJfqms_ZII3kNHr9smIXFadMYNRQvXsOhLOsUhLg").execute();
    //		IOUtils.toString(downloadFile(service, file));
    String downloadUrl = file.getExportLinks().get("text/html");
    System.out.println(downloadUrl);

    HttpRequestFactory factory = transport.createRequestFactory();
    HttpRequest request = factory.buildGetRequest(new GenericUrl(downloadUrl));
    HttpResponse response = request.execute();
    System.out.println(IOUtils.toString(response.getContent()));
  }
  private String initUpload(
      HttpRequestFactory requestFactory,
      DocsUrl url,
      long size,
      String fileType,
      AtomContent fileContent)
      throws IOException {
    HttpRequest request = requestFactory.buildPostRequest(url, fileContent);
    request.getHeaders().set("X-Upload-Content-Type", fileType);
    request.getHeaders().set("X-Upload-Content-Length", size);
    HttpResponse response = requestExecutor.executeNonSensitive(request);

    if (!response.isSuccessStatusCode()) {
      throw new CoreException(
          "unable to initiate upload, "
              + response.getStatusCode()
              + ": "
              + response.getStatusMessage());
    }
    response.ignore();
    return response.getHeaders().getLocation();
  }
  /**
   * Internal method to execute the HTTP method given.
   *
   * @see AmazonHttpClient#execute(Request, HttpResponseHandler, HttpResponseHandler)
   * @see AmazonHttpClient#execute(Request, HttpResponseHandler, HttpResponseHandler,
   *     ExecutionContext)
   */
  private <T extends Object> T executeHelper(
      Request<?> request,
      HttpResponseHandler<AmazonWebServiceResponse<T>> responseHandler,
      HttpResponseHandler<AmazonServiceException> errorResponseHandler,
      ExecutionContext executionContext)
      throws AmazonClientException, AmazonServiceException {

    /*
     * Depending on which response handler we end up choosing to handle the
     * HTTP response, it might require us to leave the underlying HTTP
     * connection open, depending on whether or not it reads the complete
     * HTTP response stream from the HTTP connection, or if delays reading
     * any of the content until after a response is returned to the caller.
     */
    boolean leaveHttpConnectionOpen = false;

    // When we release connections, the connection manager leaves them
    // open so they can be reused.  We want to close out any idle
    // connections so that they don't sit around in CLOSE_WAIT.
    httpClient.getConnectionManager().closeIdleConnections(30, TimeUnit.SECONDS);

    if (requestLog.isDebugEnabled()) {
      requestLog.debug("Sending Request: " + request.toString());
    }

    // Apply whatever request options we know how to handle, such as user-agent.
    applyRequestData(request);

    int retryCount = 0;
    URI redirectedURI = null;
    HttpEntity entity = null;
    AmazonServiceException exception = null;

    // Make a copy of the original request params and headers so that we can
    // permute it in this loop and start over with the original every time.
    Map<String, String> originalParameters = new HashMap<String, String>();
    originalParameters.putAll(request.getParameters());
    Map<String, String> originalHeaders = new HashMap<String, String>();
    originalHeaders.putAll(request.getHeaders());

    while (true) {
      if (retryCount > 0) {
        request.setParameters(originalParameters);
        request.setHeaders(originalHeaders);
      }

      // Sign the request if a signer was provided
      if (executionContext.getSigner() != null && executionContext.getCredentials() != null) {
        executionContext.getSigner().sign(request, executionContext.getCredentials());
      }

      HttpRequestBase httpRequest =
          httpRequestFactory.createHttpRequest(request, config, entity, executionContext);

      if (httpRequest instanceof HttpEntityEnclosingRequest) {
        entity = ((HttpEntityEnclosingRequest) httpRequest).getEntity();
      }

      if (redirectedURI != null) {
        httpRequest.setURI(redirectedURI);
      }

      org.apache.http.HttpResponse response = null;
      try {
        if (retryCount > 0) {
          pauseExponentially(retryCount, exception, executionContext.getCustomBackoffStrategy());
          if (entity != null) {
            InputStream content = entity.getContent();
            if (content.markSupported()) {
              content.reset();
            }
          }
        }

        exception = null;
        retryCount++;

        response = httpClient.execute(httpRequest);
        if (isRequestSuccessful(response)) {
          /*
           * If we get back any 2xx status code, then we know we should
           * treat the service call as successful.
           */
          leaveHttpConnectionOpen = responseHandler.needsConnectionLeftOpen();
          return handleResponse(request, responseHandler, httpRequest, response, executionContext);
        } else if (isTemporaryRedirect(response)) {
          /*
           * S3 sends 307 Temporary Redirects if you try to delete an
           * EU bucket from the US endpoint. If we get a 307, we'll
           * point the HTTP method to the redirected location, and let
           * the next retry deliver the request to the right location.
           */
          Header[] locationHeaders = response.getHeaders("location");
          String redirectedLocation = locationHeaders[0].getValue();
          log.debug("Redirecting to: " + redirectedLocation);
          redirectedURI = URI.create(redirectedLocation);
          httpRequest.setURI(redirectedURI);
        } else {
          leaveHttpConnectionOpen = errorResponseHandler.needsConnectionLeftOpen();
          exception = handleErrorResponse(request, errorResponseHandler, httpRequest, response);

          if (!shouldRetry(httpRequest, exception, retryCount)) {
            throw exception;
          }
        }
      } catch (IOException ioe) {
        log.warn("Unable to execute HTTP request: " + ioe.getMessage());

        if (!shouldRetry(httpRequest, ioe, retryCount)) {
          throw new AmazonClientException(
              "Unable to execute HTTP request: " + ioe.getMessage(), ioe);
        }
      } finally {
        /*
         * Some response handlers need to manually manage the HTTP
         * connection and will take care of releasing the connection on
         * their own, but if this response handler doesn't need the
         * connection left open, we go ahead and release the it to free
         * up resources.
         */
        if (!leaveHttpConnectionOpen) {
          try {
            response.getEntity().getContent().close();
          } catch (Throwable t) {
          }
        }
      }
    }
  }
Пример #6
0
  /**
   * Internal method to execute the HTTP method given.
   *
   * @see AmazonHttpClient#execute(Request, HttpResponseHandler, HttpResponseHandler)
   * @see AmazonHttpClient#execute(Request, HttpResponseHandler, HttpResponseHandler,
   *     ExecutionContext)
   */
  private <T> Response<T> executeHelper(
      Request<?> request,
      HttpResponseHandler<AmazonWebServiceResponse<T>> responseHandler,
      HttpResponseHandler<AmazonServiceException> errorResponseHandler,
      ExecutionContext executionContext)
      throws AmazonClientException, AmazonServiceException {
    /*
     * Depending on which response handler we end up choosing to handle the
     * HTTP response, it might require us to leave the underlying HTTP
     * connection open, depending on whether or not it reads the complete
     * HTTP response stream from the HTTP connection, or if delays reading
     * any of the content until after a response is returned to the caller.
     */
    boolean leaveHttpConnectionOpen = false;
    AWSRequestMetrics awsRequestMetrics = executionContext.getAwsRequestMetrics();
    /* add the service endpoint to the logs. You can infer service name from service endpoint */
    awsRequestMetrics.addProperty(Field.ServiceName, request.getServiceName());
    awsRequestMetrics.addProperty(Field.ServiceEndpoint, request.getEndpoint());
    // Apply whatever request options we know how to handle, such as user-agent.
    setUserAgent(request);
    int requestCount = 0;
    URI redirectedURI = null;
    HttpEntity entity = null;
    AmazonClientException retriedException = null;

    // Make a copy of the original request params and headers so that we can
    // permute it in this loop and start over with the original every time.
    Map<String, String> originalParameters = new LinkedHashMap<String, String>();
    originalParameters.putAll(request.getParameters());
    Map<String, String> originalHeaders = new HashMap<String, String>();
    originalHeaders.putAll(request.getHeaders());
    final AWSCredentials credentials = executionContext.getCredentials();
    Signer signer = null;

    while (true) {
      ++requestCount;
      awsRequestMetrics.setCounter(Field.RequestCount, requestCount);
      if (requestCount > 1) { // retry
        request.setParameters(originalParameters);
        request.setHeaders(originalHeaders);
      }
      HttpRequestBase httpRequest = null;
      org.apache.http.HttpResponse apacheResponse = null;

      try {
        // Sign the request if a signer was provided
        if (signer == null) signer = executionContext.getSignerByURI(request.getEndpoint());
        if (signer != null && credentials != null) {
          awsRequestMetrics.startEvent(Field.RequestSigningTime);
          try {
            signer.sign(request, credentials);
          } finally {
            awsRequestMetrics.endEvent(Field.RequestSigningTime);
          }
        }

        if (requestLog.isDebugEnabled()) {
          requestLog.debug("Sending Request: " + request.toString());
        }

        httpRequest = httpRequestFactory.createHttpRequest(request, config, executionContext);

        if (httpRequest instanceof HttpEntityEnclosingRequest) {
          entity = ((HttpEntityEnclosingRequest) httpRequest).getEntity();
        }

        if (redirectedURI != null) {
          httpRequest.setURI(redirectedURI);
        }

        if (requestCount > 1) { // retry
          awsRequestMetrics.startEvent(Field.RetryPauseTime);
          try {
            pauseBeforeNextRetry(
                request.getOriginalRequest(),
                retriedException,
                requestCount,
                config.getRetryPolicy());
          } finally {
            awsRequestMetrics.endEvent(Field.RetryPauseTime);
          }
        }

        if (entity != null) {
          InputStream content = entity.getContent();
          if (requestCount > 1) { // retry
            if (content.markSupported()) {
              content.reset();
              content.mark(-1);
            }
          } else {
            if (content.markSupported()) {
              content.mark(-1);
            }
          }
        }

        captureConnectionPoolMetrics(httpClient.getConnectionManager(), awsRequestMetrics);
        HttpContext httpContext = new BasicHttpContext();
        httpContext.setAttribute(AWSRequestMetrics.class.getSimpleName(), awsRequestMetrics);
        retriedException = null;
        awsRequestMetrics.startEvent(Field.HttpRequestTime);
        try {
          apacheResponse = httpClient.execute(httpRequest, httpContext);
        } finally {
          awsRequestMetrics.endEvent(Field.HttpRequestTime);
        }

        if (isRequestSuccessful(apacheResponse)) {
          awsRequestMetrics.addProperty(
              Field.StatusCode, apacheResponse.getStatusLine().getStatusCode());
          /*
           * If we get back any 2xx status code, then we know we should
           * treat the service call as successful.
           */
          leaveHttpConnectionOpen = responseHandler.needsConnectionLeftOpen();
          HttpResponse httpResponse = createResponse(httpRequest, request, apacheResponse);
          T response =
              handleResponse(
                  request,
                  responseHandler,
                  httpRequest,
                  httpResponse,
                  apacheResponse,
                  executionContext);
          return new Response<T>(response, httpResponse);
        } else if (isTemporaryRedirect(apacheResponse)) {
          /*
           * S3 sends 307 Temporary Redirects if you try to delete an
           * EU bucket from the US endpoint. If we get a 307, we'll
           * point the HTTP method to the redirected location, and let
           * the next retry deliver the request to the right location.
           */
          Header[] locationHeaders = apacheResponse.getHeaders("location");
          String redirectedLocation = locationHeaders[0].getValue();
          log.debug("Redirecting to: " + redirectedLocation);
          redirectedURI = URI.create(redirectedLocation);
          httpRequest.setURI(redirectedURI);
          awsRequestMetrics.addProperty(
              Field.StatusCode, apacheResponse.getStatusLine().getStatusCode());
          awsRequestMetrics.addProperty(Field.RedirectLocation, redirectedLocation);
          awsRequestMetrics.addProperty(Field.AWSRequestID, null);

        } else {
          leaveHttpConnectionOpen = errorResponseHandler.needsConnectionLeftOpen();
          AmazonServiceException ase =
              handleErrorResponse(request, errorResponseHandler, httpRequest, apacheResponse);
          awsRequestMetrics.addProperty(Field.AWSRequestID, ase.getRequestId());
          awsRequestMetrics.addProperty(Field.AWSErrorCode, ase.getErrorCode());
          awsRequestMetrics.addProperty(Field.StatusCode, ase.getStatusCode());

          if (!shouldRetry(
              request.getOriginalRequest(),
              httpRequest,
              ase,
              requestCount,
              config.getRetryPolicy())) {
            throw ase;
          }

          // Cache the retryable exception
          retriedException = ase;
          /*
           * Checking for clock skew error again because we don't want to set the
           * global time offset for every service exception.
           */
          if (RetryUtils.isClockSkewError(ase)) {
            int timeOffset = parseClockSkewOffset(apacheResponse, ase);
            SDKGlobalConfiguration.setGlobalTimeOffset(timeOffset);
          }
          resetRequestAfterError(request, ase);
        }
      } catch (IOException ioe) {
        if (log.isInfoEnabled()) {
          log.info("Unable to execute HTTP request: " + ioe.getMessage(), ioe);
        }
        awsRequestMetrics.incrementCounter(Field.Exception);
        awsRequestMetrics.addProperty(Field.Exception, ioe);
        awsRequestMetrics.addProperty(Field.AWSRequestID, null);

        AmazonClientException ace =
            new AmazonClientException("Unable to execute HTTP request: " + ioe.getMessage(), ioe);
        if (!shouldRetry(
            request.getOriginalRequest(),
            httpRequest,
            ace,
            requestCount,
            config.getRetryPolicy())) {
          throw ace;
        }

        // Cache the retryable exception
        retriedException = ace;
        resetRequestAfterError(request, ioe);
      } catch (RuntimeException e) {
        throw handleUnexpectedFailure(e, awsRequestMetrics);
      } catch (Error e) {
        throw handleUnexpectedFailure(e, awsRequestMetrics);
      } finally {
        /*
         * Some response handlers need to manually manage the HTTP
         * connection and will take care of releasing the connection on
         * their own, but if this response handler doesn't need the
         * connection left open, we go ahead and release the it to free
         * up resources.
         */
        if (!leaveHttpConnectionOpen) {
          try {
            if (apacheResponse != null
                && apacheResponse.getEntity() != null
                && apacheResponse.getEntity().getContent() != null) {
              apacheResponse.getEntity().getContent().close();
            }
          } catch (IOException e) {
            log.warn("Cannot close the response content.", e);
          }
        }
      }
    } /* end while (true) */
  }