@Override public AmazonWebServiceResponse<T> handle(HttpResponse response) throws Exception { AmazonWebServiceResponse<T> awsResponse = super.handle(response); T result = awsResponse.getResult(); if (result != null) { for (HeaderHandler<T> handler : headerHandlers) { handler.handle(result, response); } } return awsResponse; } }
@SuppressWarnings("unchecked") private <T> T fillInResponseMetadata(AmazonWebServiceResponse<T> awsResponse, HttpResponse httpResponse) { final T result = awsResponse.getResult(); if (result instanceof AmazonWebServiceResult<?>) { ((AmazonWebServiceResult) result) .setSdkResponseMetadata(awsResponse.getResponseMetadata()) .setSdkHttpMetadata(SdkHttpMetadata.from(httpResponse)); } return result; }
@Override public T handle(HttpResponse httpResponse) throws Exception { T result = delegate.handle(httpResponse).getResult(); result.sdkResponseMetadata(new SdkResponseMetadata(SdkHttpMetadata.from(httpResponse))); return result; }
/** * Confirms a subscription by visiting the provided URL. * * @param httpClient Client to use to visit URL. * @param subscribeUrl Confirmation URL. * @return Result of subscription confirmation. */ static ConfirmSubscriptionResult confirmSubscription(HttpClient httpClient, String subscribeUrl) { try { HttpGet request = new HttpGet(subscribeUrl); HttpResponse response = httpClient.execute(request); if (ApacheUtils.isRequestSuccessful(response)) { return new StaxResponseHandler<ConfirmSubscriptionResult>(ConfirmSubscriptionResultStaxUnmarshaller.getInstance()) .handle(ApacheUtils.createResponse(null, request, response, null)).getResult(); } else { throw new HttpException("Could not confirm subscription", response); } } catch (Exception e) { throw new SdkClientException(e); } } }
@Override public AmazonWebServiceResponse<T> handle(HttpResponse response) throws Exception { AmazonWebServiceResponse<T> awsResponse = super.handle(response); T result = awsResponse.getResult(); if (result != null) { for (HeaderHandler<T> handler : headerHandlers) { handler.handle(result, response); } } return awsResponse; } }
@SuppressWarnings("unchecked") private <T> T fillInResponseMetadata(AmazonWebServiceResponse<T> awsResponse, HttpResponse httpResponse) { final T result = awsResponse.getResult(); if (result instanceof AmazonWebServiceResult<?>) { ((AmazonWebServiceResult) result) .setSdkResponseMetadata(awsResponse.getResponseMetadata()) .setSdkHttpMetadata(SdkHttpMetadata.from(httpResponse)); } return result; }
return awsResponse.getResult(); } catch (final CRC32MismatchException e) { throw e;
@Test public void testHandle() throws Exception { S3StringResponseHandler handler = new S3StringResponseHandler(); ByteArrayInputStream bais = new ByteArrayInputStream("content".getBytes(StringUtils.UTF8)); HttpResponse response = new HttpResponse.Builder().content(bais).build(); AmazonWebServiceResponse<String> awsr = handler.handle(response); assertEquals(awsr.getResult(), "content"); } }
@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"); }
@Test public void testHandle() throws Exception { ByteArrayInputStream bais = new ByteArrayInputStream("content".getBytes(StringUtils.UTF8)); HttpResponse response = new HttpResponse.Builder().content(bais) .header(Headers.REDIRECT_LOCATION, "redirect") .header(Headers.REQUESTER_CHARGED_HEADER, "true").build(); S3ObjectResponseHandler handler = new S3ObjectResponseHandler(); AmazonWebServiceResponse<S3Object> object = handler.handle(response); S3Object content = object.getResult(); assertEquals(content.getRedirectLocation(), "redirect"); assertTrue(content.isRequesterCharged()); S3ObjectInputStream is = content.getObjectContent(); int curr = -1; ByteArrayOutputStream baos = new ByteArrayOutputStream(); while ((curr = is.read()) != -1) { baos.write(curr); } assertArrayEquals(baos.toByteArray(), "content".getBytes(StringUtils.UTF8)); }
@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); } }
@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"); }
@Test public void testHandleWithContent() throws Exception { final ByteArrayInputStream bais = new ByteArrayInputStream( ("<data>Content</data>").getBytes(StringUtils.UTF8)); final HttpResponse response = new HttpResponse.Builder().header("testKey", "testValue") .header("x-amzn-RequestId", "99") .content(bais).build(); Unmarshaller<String, StaxUnmarshallerContext> unmarshaller = new Unmarshaller<String, StaxUnmarshallerContext>() { @Override public String unmarshall(StaxUnmarshallerContext in) throws Exception { in.nextEvent(); String content = in.readText(); assertEquals(content, "Content"); assertEquals(in.getHeader("testKey"), "testValue"); return content; } }; StaxResponseHandler<String> handler = new StaxResponseHandler<String>(unmarshaller); AmazonWebServiceResponse<String> awsr = handler.handle(response); assertEquals(awsr.getResponseMetadata().getRequestId(), "99"); assertEquals(awsr.getResult(), "Content"); }
@Test public void testHandleWithNullContent() throws Exception { final HttpResponse response = new HttpResponse.Builder().header("testKey", "testValue") .content(null).build(); Unmarshaller<String, StaxUnmarshallerContext> unmarshaller = new Unmarshaller<String, StaxUnmarshallerContext>() { @Override public String unmarshall(StaxUnmarshallerContext in) throws Exception { assertEquals(in.getHeader("testKey"), "testValue"); return "Test"; } }; StaxResponseHandler<String> handler = new StaxResponseHandler<String>(unmarshaller); assertEquals(handler.handle(response).getResult(), "Test"); } }
@Test public void testHandleWithNullContent() throws Exception { HttpResponse response = new HttpResponse.Builder().statusText("testResponse") .statusCode(200).header("testKey", "testValue").content(null).build(); Unmarshaller<String, JsonUnmarshallerContext> unmarshaller = new Unmarshaller<String, JsonUnmarshallerContext>() { @Override public String unmarshall(JsonUnmarshallerContext in) throws Exception { in.getReader().beginObject(); assertFalse(in.getReader().hasNext()); return "NullContent"; } }; JsonResponseHandler<String> toTest = new JsonResponseHandler<String>(unmarshaller); AmazonWebServiceResponse<String> awsResponse = toTest.handle(response); assertEquals(awsResponse.getResult(), "NullContent"); }
@Override public AmazonWebServiceResponse<T> handle(HttpResponse response) throws Exception { AmazonWebServiceResponse<T> awsResponse = super.handle(response); T result = awsResponse.getResult(); if (result != null) { for (HeaderHandler<T> handler : headerHandlers) { handler.handle(result, response); } } return awsResponse; } }
@Override public AmazonWebServiceResponse<T> handle(HttpResponse response) throws Exception { AmazonWebServiceResponse<T> awsResponse = super.handle(response); T result = awsResponse.getResult(); if (result != null) { for (HeaderHandler<T> handler : headerHandlers) { handler.handle(result, response); } } return awsResponse; } }
@SuppressWarnings("unchecked") private <T> T fillInResponseMetadata(AmazonWebServiceResponse<T> awsResponse, HttpResponse httpResponse) { final T result = awsResponse.getResult(); if (result instanceof AmazonWebServiceResult<?>) { ((AmazonWebServiceResult) result) .setSdkResponseMetadata(awsResponse.getResponseMetadata()) .setSdkHttpMetadata(SdkHttpMetadata.from(httpResponse)); } return result; }
assertEquals(((S3ResponseMetadata) awsr.getResponseMetadata()).getHostId(), "extendedId"); ObjectMetadata om = awsr.getResult(); assertEquals(om.getRawMetadataValue(Headers.REQUEST_ID), null); assertEquals(om.getRawMetadataValue(Headers.EXTENDED_REQUEST_ID), null);
return awsResponse.getResult(); } catch (final CRC32MismatchException e) { throw e;