@Override public HttpResponse execute(HttpRequest request) throws IOException { HttpUriRequest httpRequest = createHttpRequest(request); org.apache.http.HttpResponse httpResponse = httpClient.execute(httpRequest); String statusText = httpResponse.getStatusLine().getReasonPhrase(); int statusCode = httpResponse.getStatusLine().getStatusCode(); InputStream content = null; if (httpResponse.getEntity() != null) { content = httpResponse.getEntity().getContent(); } HttpResponse.Builder builder = HttpResponse.builder() .statusCode(statusCode) .statusText(statusText) .content(content); for (Header header : httpResponse.getAllHeaders()) { builder.header(header.getName(), header.getValue()); } return builder.build(); }
@Test public void testJsonErrorResponseReturnsNullIfExceptionIsNull() throws Exception { List<JsonErrorUnmarshaller> exceptionUnmarshallers = new ArrayList<JsonErrorUnmarshaller>(); exceptionUnmarshallers.add(new JsonErrorUnmarshaller() { @Override public AmazonServiceException unmarshall(JsonErrorResponse error) throws Exception { return null; } }); handler = new JsonErrorResponseHandler(exceptionUnmarshallers); response = HttpResponse.builder() .statusCode(403) .content(new ByteArrayInputStream("{}".getBytes(StringUtils.UTF8))) .build(); assertNull(handler.handle(response)); }
@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 testJsonErrorResponseReturnsXAmzRequestId() throws Exception { List<JsonErrorUnmarshaller> exceptionUnmarshallers = new ArrayList<JsonErrorUnmarshaller>(); exceptionUnmarshallers.add(new JsonErrorUnmarshaller() { @Override public AmazonServiceException unmarshall(JsonErrorResponse error) throws Exception { return new AmazonServiceException("TestException"); } }); handler = new JsonErrorResponseHandler(exceptionUnmarshallers); response = HttpResponse.builder() .statusCode(500) .header("X-Amzn-RequestId", "55") .content(new ByteArrayInputStream("{}".getBytes(StringUtils.UTF8))) .build(); AmazonServiceException returnedException = handler.handle(response); assertEquals(returnedException.getRequestId(), "55"); } }
@Test public void testHandleErrorResponse() throws IOException { String response = "<Error>" + "<Message>testError</Message>" + "<Code>testCode</Code>" + "<RequestId>testId</RequestId>" + "<HostId>testHost</HostId>" + "</Error>"; ByteArrayInputStream content = new ByteArrayInputStream(response.getBytes(StringUtils.UTF8)); HttpResponse errorResponse = new HttpResponse.Builder() .statusCode(200) .content(content) .statusText("testError").build(); S3ErrorResponseHandler handler = new S3ErrorResponseHandler(); AmazonS3Exception ase = (AmazonS3Exception) handler.handle(errorResponse); assertEquals(ase.getErrorMessage(), "testError"); assertEquals(ase.getErrorType(), ErrorType.Client); assertEquals(ase.getRequestId(), "testId"); assertEquals(ase.getExtendedRequestId(), "testHost"); assertEquals(ase.getStatusCode(), 200); assertEquals(ase.getErrorCode(), "testCode"); }
@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 testJsonErrorResponseReturnsServiceErrorTypeIfErrorStatus5XX() throws Exception { List<JsonErrorUnmarshaller> exceptionUnmarshallers = new ArrayList<JsonErrorUnmarshaller>(); exceptionUnmarshallers.add(new JsonErrorUnmarshaller() { @Override public AmazonServiceException unmarshall(JsonErrorResponse error) throws Exception { return new AmazonServiceException("TestException"); } }); handler = new JsonErrorResponseHandler(exceptionUnmarshallers); response = HttpResponse.builder() .statusCode(500) .content(new ByteArrayInputStream("{}".getBytes(StringUtils.UTF8))) .build(); AmazonServiceException returnedException = handler.handle(response); assertEquals(returnedException.getErrorType(), ErrorType.Service); }
@Test public void testJsonExceptionUnmarshallerWithAdditionalFields() throws Exception { HttpResponse response = HttpResponse.builder() .content(new ByteArrayInputStream(errorResponse.getBytes("UTF-8"))) .build(); JsonErrorResponse error = JsonErrorResponse.fromResponse(response); AmazonServiceException ase = new InternalServerErrorExceptionUnmarshaller() .unmarshall(error); assertTrue(ase instanceof InternalServerErrorException); assertEquals("value1", ((InternalServerErrorException) ase).getField1()); assertEquals("value2", ((InternalServerErrorException) ase).getField2()); assertEquals("InternalServerError", ase.getErrorCode()); assertEquals("Requested resource not found " + "(Service: null; " + "Status Code: 0; " + "Error Code: InternalServerError; " + "Request ID: null)", ase.getMessage()); }
@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 testErrorResponseFromUnparsableXML() throws IOException { String response = "<Error>" + "<Message>testError"; ByteArrayInputStream content = new ByteArrayInputStream(response.getBytes(StringUtils.UTF8)); HttpResponse errorResponse = new HttpResponse.Builder() .statusCode(200) .content(content) .statusText("testError").build(); S3ErrorResponseHandler handler = new S3ErrorResponseHandler(); AmazonS3Exception ase = (AmazonS3Exception) handler.handle(errorResponse); assertEquals(ase.getStatusCode(), 200); assertEquals(ase.getErrorType(), ErrorType.Client); assertEquals(ase.getErrorCode(), "200 testError"); }
@Test public void testContentEncodingGZIPRaw() throws Exception { String str = "content to be zipped"; InputStream zippedContent = getGzippedInputStream(str); builder = HttpResponse.builder() .header("Content-Encoding", "gzip") .content(zippedContent); response = builder.build(); InputStream rawContent = response.getRawContent(); assertFalse("Not gzip", rawContent instanceof GZIPInputStream); GZIPInputStream gis = new GZIPInputStream(rawContent); String result = IOUtils.toString(gis); assertEquals("unzip correctly", str, result); }
@Test public void testHandleErrorResponseWithNoBody() throws IOException { HttpResponse errorResponse = new HttpResponse.Builder() .statusCode(500) .content(null) .statusText("testError") .header(Headers.REQUEST_ID, "testId") .header(Headers.EXTENDED_REQUEST_ID, "extendedId").build(); S3ErrorResponseHandler handler = new S3ErrorResponseHandler(); AmazonS3Exception ase = (AmazonS3Exception) handler.handle(errorResponse); assertEquals(ase.getErrorMessage(), "testError"); assertEquals(ase.getErrorType(), ErrorType.Service); assertEquals(ase.getRequestId(), "testId"); assertEquals(ase.getExtendedRequestId(), "extendedId"); assertEquals(ase.getStatusCode(), 500); }
@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"); }
@Test public void testContentEncodingGZIP() throws Exception { String str = "content to be zipped"; InputStream zippedContent = getGzippedInputStream(str); builder = HttpResponse.builder() .header("Content-Encoding", "gzip") .content(zippedContent); response = builder.build(); InputStream unzippedContent = response.getContent(); assertTrue(unzippedContent instanceof GZIPInputStream); String result = IOUtils.toString(unzippedContent); assertEquals("unzip correctly", str, result); }
@Test public void testBuilder() throws Exception { builder = HttpResponse.builder() .statusCode(statusCode) .statusText(statusText) .content(content); for (int i = 0; i < 10; i++) { builder.header("key" + i, "value" + i); } response = builder.build(); assertEquals("status text", statusText, response.getStatusText()); assertTrue("status code", statusCode == response.getStatusCode()); assertTrue("has headers", 10 == response.getHeaders().size()); assertEquals("content", content, response.getContent()); }
@Test public void testJsonErrorResponse() throws Exception { String body = "{\"__type\":\"InvalidSignatureException\",\"message\":\"Clock is skewed\"}"; ByteArrayInputStream content = new ByteArrayInputStream(body.getBytes(StringUtils.UTF8)); response = HttpResponse.builder() .content(content) .statusCode(403) .build(); AmazonServiceException ase = handler.handle(response); assertEquals(ase.getErrorCode(), "InvalidSignatureException"); }
@Test public void testContentEncodingIdentity() throws Exception { builder = HttpResponse.builder() .header("Content-Encoding", "identity") .content(content); response = builder.build(); assertFalse("Not gzip", response.getContent() instanceof GZIPInputStream); assertEquals("same content", "content", IOUtils.toString(response.getContent())); }
@Test(expected = UnsupportedOperationException.class) public void testUnmodifierableHeaders() { builder = HttpResponse.builder() .statusCode(statusCode) .statusText(statusText) .content(content); response = builder.build(); response.getHeaders().put("key", "value"); }
@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 testRestJsonErrorResponse() throws Exception { String body = "{\"message\":\"Clock is skewed\"}"; ByteArrayInputStream content = new ByteArrayInputStream(body.getBytes(StringUtils.UTF8)); response = HttpResponse.builder() .content(content) .statusCode(403) .header("x-amzn-ErrorType", "InvalidSignatureException") .build(); AmazonServiceException ase = handler.handle(response); assertEquals(ase.getErrorCode(), "InvalidSignatureException"); }