Search in sources :

Example 1 with JsonUnmarshallerContext

use of com.amazonaws.transform.JsonUnmarshallerContext 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 JsonUnmarshallerContext

use of com.amazonaws.transform.JsonUnmarshallerContext in project aws-sdk-android by aws-amplify.

the class JsonResponseHandlerTest method testHandleWithNoCRC32.

@Test
public void testHandleWithNoCRC32() throws Exception {
    ByteArrayInputStream bais = new ByteArrayInputStream("{\"key\" :\"Content\"}".getBytes(StringUtils.UTF8));
    HttpResponse response = new HttpResponse.Builder().statusText("testResponse").statusCode(200).header("testKey", "testValue").content(bais).build();
    Unmarshaller<String, JsonUnmarshallerContext> unmarshaller = new Unmarshaller<String, JsonUnmarshallerContext>() {

        @Override
        public String unmarshall(JsonUnmarshallerContext in) throws Exception {
            in.getReader().beginObject();
            in.getReader().nextName();
            return in.getReader().nextString();
        }
    };
    JsonResponseHandler<String> toTest = new JsonResponseHandler<String>(unmarshaller);
    AmazonWebServiceResponse<String> awsResponse = toTest.handle(response);
    assertEquals(awsResponse.getResult(), "Content");
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Unmarshaller(com.amazonaws.transform.Unmarshaller) JsonUnmarshallerContext(com.amazonaws.transform.JsonUnmarshallerContext) Test(org.junit.Test)

Example 3 with JsonUnmarshallerContext

use of com.amazonaws.transform.JsonUnmarshallerContext in project aws-sdk-android by aws-amplify.

the class JsonResponseHandlerTest method testHandleNeedsConnectionLeftOpen.

@Test
public void testHandleNeedsConnectionLeftOpen() throws Exception {
    ByteArrayInputStream bais = new ByteArrayInputStream("{\"key\" :\"Content\"}".getBytes(StringUtils.UTF8));
    CRC32 crc32 = new CRC32();
    crc32.update("{\"key\" :\"Content\"}".getBytes(StringUtils.UTF8));
    HttpResponse response = new HttpResponse.Builder().statusText("testResponse").statusCode(200).header("testKey", "testValue").content(bais).build();
    final List<InputStream> capture = new ArrayList<InputStream>();
    Unmarshaller<String, JsonUnmarshallerContext> unmarshaller = new Unmarshaller<String, JsonUnmarshallerContext>() {

        @Override
        public String unmarshall(JsonUnmarshallerContext in) throws Exception {
            capture.add(in.getHttpResponse().getContent());
            return "OpenConnection";
        }
    };
    JsonResponseHandler<String> toTest = new JsonResponseHandler<String>(unmarshaller);
    toTest.needsConnectionLeftOpen = true;
    assertTrue(toTest.needsConnectionLeftOpen());
    AmazonWebServiceResponse<String> awsResponse = toTest.handle(response);
    assertEquals(awsResponse.getResult(), "OpenConnection");
    assertSame(capture.get(0), bais);
}
Also used : CRC32(java.util.zip.CRC32) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) ByteArrayInputStream(java.io.ByteArrayInputStream) Unmarshaller(com.amazonaws.transform.Unmarshaller) JsonUnmarshallerContext(com.amazonaws.transform.JsonUnmarshallerContext) Test(org.junit.Test)

Example 4 with JsonUnmarshallerContext

use of com.amazonaws.transform.JsonUnmarshallerContext in project aws-sdk-android by aws-amplify.

the class JsonResponseHandlerTest method testHandleWithCRC32.

@Test
public void testHandleWithCRC32() throws Exception {
    ByteArrayInputStream bais = new ByteArrayInputStream("{\"key\" :\"Content\"}".getBytes(StringUtils.UTF8));
    CRC32 crc32 = new CRC32();
    crc32.update("{\"key\" :\"Content\"}".getBytes(StringUtils.UTF8));
    HttpResponse response = new HttpResponse.Builder().statusText("testResponse").statusCode(200).header("testKey", "testValue").header("x-amz-crc32", String.valueOf(crc32.getValue())).content(bais).build();
    Unmarshaller<String, JsonUnmarshallerContext> unmarshaller = new Unmarshaller<String, JsonUnmarshallerContext>() {

        @Override
        public String unmarshall(JsonUnmarshallerContext in) throws Exception {
            in.getReader().beginObject();
            in.getReader().nextName();
            return in.getReader().nextString();
        }
    };
    JsonResponseHandler<String> toTest = new JsonResponseHandler<String>(unmarshaller);
    // Is a no-op. just adding for code coverage of the no-op
    toTest.registerAdditionalMetadataExpressions(null);
    AmazonWebServiceResponse<String> awsResponse = toTest.handle(response);
    assertEquals(awsResponse.getResult(), "Content");
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) CRC32(java.util.zip.CRC32) Unmarshaller(com.amazonaws.transform.Unmarshaller) JsonUnmarshallerContext(com.amazonaws.transform.JsonUnmarshallerContext) Test(org.junit.Test)

Example 5 with JsonUnmarshallerContext

use of com.amazonaws.transform.JsonUnmarshallerContext in project aws-sdk-android by aws-amplify.

the class JsonUnmarshallerTest method setupUnmarshaller.

private JsonUnmarshallerContext setupUnmarshaller(String snippet) throws Exception {
    AwsJsonReader jsonReader = JsonUtils.getJsonReader(new StringReader(snippet));
    JsonUnmarshallerContext unmarshallerContext = new JsonUnmarshallerContext(jsonReader);
    return unmarshallerContext;
}
Also used : StringReader(java.io.StringReader) JsonUnmarshallerContext(com.amazonaws.transform.JsonUnmarshallerContext) AwsJsonReader(com.amazonaws.util.json.AwsJsonReader)

Aggregations

JsonUnmarshallerContext (com.amazonaws.transform.JsonUnmarshallerContext)11 Test (org.junit.Test)6 AwsJsonReader (com.amazonaws.util.json.AwsJsonReader)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 StringReader (java.io.StringReader)4 ListUnmarshaller (com.amazonaws.transform.ListUnmarshaller)3 Unmarshaller (com.amazonaws.transform.Unmarshaller)3 MapUnmarshaller (com.amazonaws.transform.MapUnmarshaller)2 InputStream (java.io.InputStream)2 CRC32 (java.util.zip.CRC32)2 AmazonWebServiceResponse (com.amazonaws.AmazonWebServiceResponse)1 ResponseMetadata (com.amazonaws.ResponseMetadata)1 HttpResponse (com.amazonaws.http.HttpResponse)1 CRC32MismatchException (com.amazonaws.internal.CRC32MismatchException)1 InvalidContactFlowException (com.amazonaws.services.connect.model.InvalidContactFlowException)1 InvalidContactFlowModuleException (com.amazonaws.services.connect.model.InvalidContactFlowModuleException)1 ValidationException (com.amazonaws.services.geo.model.ValidationException)1 InvokeResult (com.amazonaws.services.lambda.model.InvokeResult)1 CRC32ChecksumCalculatingInputStream (com.amazonaws.util.CRC32ChecksumCalculatingInputStream)1 IOException (java.io.IOException)1