Search in sources :

Example 1 with CRC32MismatchException

use of com.amazonaws.internal.CRC32MismatchException in project aws-sdk-android by aws-amplify.

the class JsonResponseHandler method handle.

/**
 * @see com.amazonaws.http.HttpResponseHandler#handle(com.amazonaws.http.HttpResponse)
 */
@Override
public AmazonWebServiceResponse<T> handle(HttpResponse response) throws Exception {
    log.trace("Parsing service response JSON");
    final String crc32Checksum = response.getHeaders().get("x-amz-crc32");
    // Get the raw content input stream to calculate the crc32 checksum on
    // gzipped data.
    InputStream content = response.getRawContent();
    if (content == null) {
        // An empty input stream to avoid NPE
        content = new ByteArrayInputStream("{}".getBytes(StringUtils.UTF8));
    }
    log.debug("CRC32Checksum = " + crc32Checksum);
    log.debug("content encoding = " + response.getHeaders().get("Content-Encoding"));
    boolean isGzipEncoded = "gzip".equals(response.getHeaders().get("Content-Encoding"));
    CRC32ChecksumCalculatingInputStream checksumCalculatingInputStream = null;
    // encoded, no checksum) is already handled: we'll just operate on the raw content stream.
    if (crc32Checksum != null) {
        checksumCalculatingInputStream = new CRC32ChecksumCalculatingInputStream(content);
        content = checksumCalculatingInputStream;
    }
    if (isGzipEncoded) {
        content = new GZIPInputStream(content);
    }
    final AwsJsonReader jsonReader = JsonUtils.getJsonReader(new InputStreamReader(content, StringUtils.UTF8));
    try {
        final AmazonWebServiceResponse<T> awsResponse = new AmazonWebServiceResponse<T>();
        final JsonUnmarshallerContext unmarshallerContext = new JsonUnmarshallerContext(jsonReader, response);
        final T result = responseUnmarshaller.unmarshall(unmarshallerContext);
        if (checksumCalculatingInputStream != null) {
            final long serverSideCRC = Long.parseLong(crc32Checksum);
            final long clientSideCRC = checksumCalculatingInputStream.getCRC32Checksum();
            if (clientSideCRC != serverSideCRC) {
                throw new CRC32MismatchException("Client calculated crc32 checksum didn't match that calculated by server side");
            }
        }
        awsResponse.setResult(result);
        final Map<String, String> metadata = new HashMap<String, String>();
        metadata.put(ResponseMetadata.AWS_REQUEST_ID, response.getHeaders().get("x-amzn-RequestId"));
        awsResponse.setResponseMetadata(new ResponseMetadata(metadata));
        log.trace("Done parsing service response");
        return awsResponse;
    } finally {
        if (!needsConnectionLeftOpen) {
            try {
                jsonReader.close();
            } catch (final IOException e) {
                log.warn("Error closing json parser", e);
            }
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) CRC32ChecksumCalculatingInputStream(com.amazonaws.util.CRC32ChecksumCalculatingInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CRC32ChecksumCalculatingInputStream(com.amazonaws.util.CRC32ChecksumCalculatingInputStream) IOException(java.io.IOException) AmazonWebServiceResponse(com.amazonaws.AmazonWebServiceResponse) AwsJsonReader(com.amazonaws.util.json.AwsJsonReader) CRC32MismatchException(com.amazonaws.internal.CRC32MismatchException) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) JsonUnmarshallerContext(com.amazonaws.transform.JsonUnmarshallerContext) ResponseMetadata(com.amazonaws.ResponseMetadata)

Example 2 with CRC32MismatchException

use of com.amazonaws.internal.CRC32MismatchException in project aws-sdk-android by aws-amplify.

the class AmazonHttpClient method handleResponse.

/**
 * 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 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.
 */
<T> T handleResponse(Request<?> request, HttpResponseHandler<AmazonWebServiceResponse<T>> responseHandler, HttpResponse response, ExecutionContext executionContext) throws IOException {
    try {
        final AWSRequestMetrics awsRequestMetrics = executionContext.getAwsRequestMetrics();
        AmazonWebServiceResponse<? extends T> awsResponse;
        awsRequestMetrics.startEvent(Field.ResponseProcessingTime);
        try {
            awsResponse = responseHandler.handle(response);
        } finally {
            awsRequestMetrics.endEvent(Field.ResponseProcessingTime);
        }
        if (awsResponse == null) {
            throw new RuntimeException("Unable to unmarshall response metadata. Response Code: " + response.getStatusCode() + ", Response Text: " + response.getStatusText());
        }
        if (REQUEST_LOG.isDebugEnabled()) {
            REQUEST_LOG.debug("Received successful response: " + response.getStatusCode() + ", AWS Request ID: " + awsResponse.getRequestId());
        }
        awsRequestMetrics.addProperty(Field.AWSRequestID, awsResponse.getRequestId());
        return awsResponse.getResult();
    } catch (final CRC32MismatchException e) {
        throw e;
    } catch (final IOException e) {
        throw e;
    } catch (final Exception e) {
        final String errorMessage = "Unable to unmarshall response (" + e.getMessage() + "). Response Code: " + response.getStatusCode() + ", Response Text: " + response.getStatusText();
        throw new AmazonClientException(errorMessage, e);
    }
}
Also used : AWSRequestMetrics(com.amazonaws.util.AWSRequestMetrics) AmazonClientException(com.amazonaws.AmazonClientException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) CRC32MismatchException(com.amazonaws.internal.CRC32MismatchException) AmazonServiceException(com.amazonaws.AmazonServiceException) IOException(java.io.IOException) AmazonClientException(com.amazonaws.AmazonClientException) CRC32MismatchException(com.amazonaws.internal.CRC32MismatchException)

Aggregations

CRC32MismatchException (com.amazonaws.internal.CRC32MismatchException)2 IOException (java.io.IOException)2 AmazonClientException (com.amazonaws.AmazonClientException)1 AmazonServiceException (com.amazonaws.AmazonServiceException)1 AmazonWebServiceResponse (com.amazonaws.AmazonWebServiceResponse)1 ResponseMetadata (com.amazonaws.ResponseMetadata)1 JsonUnmarshallerContext (com.amazonaws.transform.JsonUnmarshallerContext)1 AWSRequestMetrics (com.amazonaws.util.AWSRequestMetrics)1 CRC32ChecksumCalculatingInputStream (com.amazonaws.util.CRC32ChecksumCalculatingInputStream)1 AwsJsonReader (com.amazonaws.util.json.AwsJsonReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 URISyntaxException (java.net.URISyntaxException)1 HashMap (java.util.HashMap)1 GZIPInputStream (java.util.zip.GZIPInputStream)1