StaxResponseHandler implements HttpResponseHandler<AmazonWebServiceResponse<T>> { public AmazonWebServiceResponse<T> handle(HttpResponse response) throws Exception { log.trace("Parsing service response XML"); InputStream content = response.getContent(); if (content == null) { content = new ByteArrayInputStream("<eof/>".getBytes(StringUtils.UTF8)); } else if (content instanceof SdkFilterInputStream && ((SdkFilterInputStream) content).getDelegateStream() instanceof EmptyInputStream) { content = new ByteArrayInputStream("<eof/>".getBytes(StringUtils.UTF8)); } XMLEventReader eventReader; try { eventReader = XmlUtils.getXmlInputFactory().createXMLEventReader(content); } catch (XMLStreamException e) { throw handleXmlStreamException(e); } try { AmazonWebServiceResponse<T> awsResponse = new AmazonWebServiceResponse<T>(); StaxUnmarshallerContext unmarshallerContext = new StaxUnmarshallerContext(eventReader, response.getHeaders()); unmarshallerContext.registerMetadataExpression("ResponseMetadata/RequestId", 2, ResponseMetadata.AWS_REQUEST_ID); unmarshallerContext.registerMetadataExpression("requestId", 2, ResponseMetadata.AWS_REQUEST_ID); registerAdditionalMetadataExpressions(unmarshallerContext); T result = responseUnmarshaller.unmarshall(unmarshallerContext); awsResponse.setResult(result); Map<String, String> metadata = unmarshallerContext.getMetadata(); Map<String, String> responseHeaders = response.getHeaders(); if (responseHeaders != null) { if (responseHeaders.get(X_AMZN_REQUEST_ID_HEADER) != null) { metadata.put(ResponseMetadata.AWS_REQUEST_ID, responseHeaders.get(X_AMZN_REQUEST_ID_HEADER)); } if (responseHeaders.get(X_AMZN_EXTENDED_REQUEST_ID_HEADER) != null) { metadata.put(ResponseMetadata.AWS_EXTENDED_REQUEST_ID, responseHeaders.get(X_AMZN_EXTENDED_REQUEST_ID_HEADER)); } } awsResponse.setResponseMetadata(getResponseMetadata(metadata)); log.trace("Done parsing service response"); return awsResponse; } catch (XMLStreamException e) { throw handleXmlStreamException(e); } finally { try { eventReader.close(); } catch (XMLStreamException e) { log.warn("Error closing xml parser", e); } } } StaxResponseHandler(Unmarshaller<T, StaxUnmarshallerContext> responseUnmarshaller); AmazonWebServiceResponse<T> handle(HttpResponse response); boolean needsConnectionLeftOpen();  }
@Test(expected = IOException.class) public void socketTimeoutThrownFromInputStream_ThrowsIoException() throws Exception { HttpResponse response = new HttpResponse(null, null); response.setContent(new InputStream() { @Override public int read() throws IOException { throw new SocketTimeoutException("socket timeout"); } }); responseHandler.handle(response); }
@Test(expected = IOException.class) public void socketTimeoutThrownAfternInitialContent_ThrowsIoException() throws Exception { HttpResponse response = new HttpResponse(null, null); response.setContent(new InputStream() { String content = "<OperationNameResponse><OperationNameResult><Str>myname</Str><FooNum>123</FooNum><FalseBool>false</FalseBool><TrueBool>true</TrueBool><Float>1.2</Float><Double>1.3</Double><Long>200</Long><Char>a</Char><Timestamp>2015-01-25T08:00:00Z</Timestamp></OperationNameResult><ResponseMetadata><RequestId>request-id</RequestId></ResponseMetadata></OperationNameResponse>"; int read = 0; @Override public int read() throws IOException { if (read > 50) { throw new SocketTimeoutException("socket timeout"); } return content.charAt(read++); } }); responseHandler.handle(response); }