Search in sources :

Example 1 with StaxUnmarshallerContext

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");
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) StaxUnmarshallerContext(com.amazonaws.transform.StaxUnmarshallerContext) Unmarshaller(com.amazonaws.transform.Unmarshaller) Test(org.junit.Test)

Example 2 with StaxUnmarshallerContext

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;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) StaxUnmarshallerContext(com.amazonaws.transform.StaxUnmarshallerContext) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) XmlPullParser(org.xmlpull.v1.XmlPullParser) AmazonWebServiceResponse(com.amazonaws.AmazonWebServiceResponse) ResponseMetadata(com.amazonaws.ResponseMetadata)

Example 3 with StaxUnmarshallerContext

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;
}
Also used : Entry(java.util.Map.Entry) StaxUnmarshallerContext(com.amazonaws.transform.StaxUnmarshallerContext) XmlPullParser(org.xmlpull.v1.XmlPullParser) NotificationConfiguration(com.amazonaws.services.s3.model.NotificationConfiguration) BucketNotificationConfiguration(com.amazonaws.services.s3.model.BucketNotificationConfiguration) BucketNotificationConfiguration(com.amazonaws.services.s3.model.BucketNotificationConfiguration)

Aggregations

StaxUnmarshallerContext (com.amazonaws.transform.StaxUnmarshallerContext)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 XmlPullParser (org.xmlpull.v1.XmlPullParser)2 AmazonWebServiceResponse (com.amazonaws.AmazonWebServiceResponse)1 ResponseMetadata (com.amazonaws.ResponseMetadata)1 BucketNotificationConfiguration (com.amazonaws.services.s3.model.BucketNotificationConfiguration)1 NotificationConfiguration (com.amazonaws.services.s3.model.NotificationConfiguration)1 Unmarshaller (com.amazonaws.transform.Unmarshaller)1 InputStream (java.io.InputStream)1 Entry (java.util.Map.Entry)1 Test (org.junit.Test)1