@Override
  public AmazonServiceException handle(HttpResponse response) throws Exception {
    JsonErrorResponse error;
    try {
      error = JsonErrorResponse.fromResponse(response);
    } catch (IOException e) {
      throw new AmazonClientException("Unable to parse error response", e);
    }

    AmazonServiceException ase = runErrorUnmarshallers(error);
    if (ase == null) return null;

    ase.setStatusCode(response.getStatusCode());
    if (response.getStatusCode() < 500) {
      ase.setErrorType(ErrorType.Client);
    } else {
      ase.setErrorType(ErrorType.Service);
    }
    ase.setErrorCode(error.getErrorCode());

    for (Entry<String, String> headerEntry : response.getHeaders().entrySet()) {
      if (headerEntry.getKey().equalsIgnoreCase("X-Amzn-RequestId")) {
        ase.setRequestId(headerEntry.getValue());
      }
    }

    return ase;
  }
    public static JsonErrorResponse fromResponse(HttpResponse response) throws IOException {
      int statusCode = response.getStatusCode();

      // parse error body
      Map<String, String> map =
          JsonUtils.jsonToMap(new BufferedReader(new InputStreamReader(response.getContent())));

      /*
       * Services using AWS JSON 1.1 protocol with HTTP binding send the
       * error type information in the response headers, instead of the
       * content.
       */
      String errorCode = response.getHeaders().get(X_AMZN_ERROR_TYPE);
      if (errorCode != null) {
        int separator = errorCode.indexOf(':');
        if (separator != -1) {
          errorCode = errorCode.substring(0, separator);
        }
      } else if (map.containsKey("__type")) {
        // check body otherwise
        String type = map.get("__type");
        int separator = type.lastIndexOf("#");
        errorCode = type.substring(separator + 1);
      }

      return new JsonErrorResponse(statusCode, errorCode, map);
    }
Exemplo n.º 3
0
  private void triggerResponseHandler(HttpResponse httpResponse) {
    // For more simplify interface to handle the response
    if (responseLiteListener != null) responseLiteListener.onFinished(httpResponse);

    if (responseListener == null) return;

    // Invalid request
    if (httpResponse.isCorrupt()) {
      responseListener.onCorrupt();
      return;
    }

    int status = httpResponse.getStatusCode();
    if (status >= 500) {
      responseListener.onInternalError(httpResponse);
    } else if (status >= 400) {
      responseListener.onFailed(httpResponse);
    } else if (status >= 300) {
      responseListener.onRedirect(httpResponse);
    } else if (status >= 200) {
      responseListener.onSuccess(httpResponse);
    }
  }
Exemplo n.º 4
0
  /**
   * Handles a successful response from a service call by unmarshalling the results using the
   * specified response handler.
   *
   * @param <T> The type of object expected in the response.
   * @param request The original request that generated the response being handled.
   * @param responseHandler The response unmarshaller used to interpret the contents of the
   *     response.
   * @param method The HTTP method that was invoked, and contains the contents of the response.
   * @param executionContext Extra state information about the request currently being executed.
   * @return The contents of the response, unmarshalled using the specified response handler.
   * @throws IOException If any problems were encountered reading the response contents from the
   *     HTTP method object.
   */
  private <T> T handleResponse(
      Request<?> request,
      HttpResponseHandler<AmazonWebServiceResponse<T>> responseHandler,
      HttpRequestBase method,
      HttpResponse httpResponse,
      org.apache.http.HttpResponse apacheHttpResponse,
      ExecutionContext executionContext)
      throws IOException {
    if (responseHandler.needsConnectionLeftOpen() && method instanceof HttpEntityEnclosingRequest) {
      HttpEntityEnclosingRequest httpEntityEnclosingRequest = (HttpEntityEnclosingRequest) method;
      httpResponse.setContent(new HttpMethodReleaseInputStream(httpEntityEnclosingRequest));
    }

    try {
      CountingInputStream countingInputStream = null;
      if (System.getProperty(PROFILING_SYSTEM_PROPERTY) != null) {
        countingInputStream = new CountingInputStream(httpResponse.getContent());
        httpResponse.setContent(countingInputStream);
      }

      AWSRequestMetrics awsRequestMetrics = executionContext.getAwsRequestMetrics();
      AmazonWebServiceResponse<? extends T> awsResponse;
      awsRequestMetrics.startEvent(Field.ResponseProcessingTime);
      try {
        awsResponse = responseHandler.handle(httpResponse);
      } finally {
        awsRequestMetrics.endEvent(Field.ResponseProcessingTime);
      }
      if (countingInputStream != null) {
        awsRequestMetrics.setCounter(Field.BytesProcessed, countingInputStream.getByteCount());
      }

      if (awsResponse == null)
        throw new RuntimeException(
            "Unable to unmarshall response metadata. Response Code: "
                + httpResponse.getStatusCode()
                + ", Response Text: "
                + httpResponse.getStatusText());

      responseMetadataCache.add(request.getOriginalRequest(), awsResponse.getResponseMetadata());

      if (requestLog.isDebugEnabled()) {
        requestLog.debug(
            "Received successful response: "
                + apacheHttpResponse.getStatusLine().getStatusCode()
                + ", AWS Request ID: "
                + awsResponse.getRequestId());
      }
      awsRequestMetrics.addProperty(Field.AWSRequestID, awsResponse.getRequestId());

      return awsResponse.getResult();
    } catch (CRC32MismatchException e) {
      throw e;
    } catch (IOException e) {
      throw e;
    } catch (Exception e) {
      String errorMessage =
          "Unable to unmarshall response ("
              + e.getMessage()
              + "). Response Code: "
              + httpResponse.getStatusCode()
              + ", Response Text: "
              + httpResponse.getStatusText();
      throw new AmazonClientException(errorMessage, e);
    }
  }