use of com.amazonaws.transform.StaxUnmarshallerContext in project aws-sdk-android by aws-amplify.
the class StaxResponseHandlerTest method testHandleWithContent.
@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");
}
use of com.amazonaws.transform.StaxUnmarshallerContext in project aws-sdk-android by aws-amplify.
the class StaxResponseHandler 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 XML");
InputStream content = response.getContent();
if (content == null)
content = new ByteArrayInputStream("<eof/>".getBytes(StringUtils.UTF8));
XmlPullParser xpp = XML_PULL_PARSER_FACTORY.newPullParser();
xpp.setInput(content, null);
AmazonWebServiceResponse<T> awsResponse = new AmazonWebServiceResponse<T>();
StaxUnmarshallerContext unmarshallerContext = new StaxUnmarshallerContext(xpp, 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-RequestId") != null) {
metadata.put(ResponseMetadata.AWS_REQUEST_ID, responseHeaders.get("x-amzn-RequestId"));
}
}
awsResponse.setResponseMetadata(new ResponseMetadata(metadata));
log.trace("Done parsing service response");
return awsResponse;
}
use of com.amazonaws.transform.StaxUnmarshallerContext in project aws-sdk-android by aws-amplify.
the class BucketNotificationConfigurationStaxUnmarshaller method unmarshall.
@Override
public BucketNotificationConfiguration unmarshall(InputStream inputStream) throws Exception {
final XmlPullParser xpp = xmlPullParserFactory.newPullParser();
xpp.setInput(inputStream, null);
final StaxUnmarshallerContext context = new StaxUnmarshallerContext(xpp, null);
final int originalDepth = context.getCurrentDepth();
int targetDepth = originalDepth + 1;
if (context.isStartOfDocument()) {
targetDepth += 1;
}
final BucketNotificationConfiguration config = new BucketNotificationConfiguration();
while (true) {
final int xmlEvent = context.nextEvent();
if (xmlEvent == XmlPullParser.END_DOCUMENT) {
break;
} else if (xmlEvent == XmlPullParser.START_TAG) {
if (context.testExpression("TopicConfiguration", targetDepth)) {
final Entry<String, NotificationConfiguration> entry = TopicConfigurationStaxUnmarshaller.getInstance().unmarshall(context);
config.addConfiguration(entry.getKey(), entry.getValue());
} else if (context.testExpression("QueueConfiguration", targetDepth)) {
final Entry<String, NotificationConfiguration> entry = QueueConfigurationStaxUnmarshaller.getInstance().unmarshall(context);
config.addConfiguration(entry.getKey(), entry.getValue());
} else if (context.testExpression("CloudFunctionConfiguration", targetDepth)) {
final Entry<String, NotificationConfiguration> entry = LambdaConfigurationStaxUnmarshaller.getInstance().unmarshall(context);
config.addConfiguration(entry.getKey(), entry.getValue());
}
} else if (xmlEvent == XmlPullParser.END_TAG) {
if (context.getCurrentDepth() < originalDepth) {
return config;
}
}
}
return config;
}
Aggregations