1 /*
2  * Copyright 2010-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  *  http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */

15 package com.amazonaws.services.s3.internal;
16
17 import java.io.InputStream;
18 import java.util.Map;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22
23 import com.amazonaws.AmazonWebServiceResponse;
24 import com.amazonaws.http.HttpResponse;
25 import com.amazonaws.transform.Unmarshaller;
26
27 /**
28  * S3 Implementation of HttpResponseHandler. Relies on a SAX unmarshaller for
29  * handling the response.
30  */

31 public class S3XmlResponseHandler<T> extends AbstractS3ResponseHandler<T> {
32
33     /** The SAX unmarshaller to use when handling the response from S3 */
34     private Unmarshaller<T, InputStream> responseUnmarshaller;
35
36     /** Shared logger for profiling information */
37     private static final Log log = LogFactory.getLog("com.amazonaws.request");
38
39     /** Response headers from the processed response */
40     private Map<String, String> responseHeaders;
41
42     /**
43      * Constructs a new S3 response handler that will use the specified SAX
44      * unmarshaller to turn the response into an object.
45      *
46      * @param responseUnmarshaller
47      *            The SAX unmarshaller to use on the response from S3.
48      */

49     public S3XmlResponseHandler(Unmarshaller<T, InputStream> responseUnmarshaller) {
50         this.responseUnmarshaller = responseUnmarshaller;
51     }
52
53     /**
54      * @see com.amazonaws.http.HttpResponseHandler#handle(com.amazonaws.http.HttpResponse)
55      */

56     public AmazonWebServiceResponse<T> handle(HttpResponse response) throws Exception {
57         AmazonWebServiceResponse<T> awsResponse = parseResponseMetadata(response);
58         responseHeaders = response.getHeaders();
59
60         if (responseUnmarshaller != null) {
61             log.trace("Beginning to parse service response XML");
62             T result = responseUnmarshaller.unmarshall(response.getContent());
63             log.trace("Done parsing service response XML");
64             awsResponse.setResult(result);
65         }
66
67         return awsResponse;
68     }
69
70     /**
71      * Returns the headers from the processed response. Will return null until a
72      * response has been handled.
73      *
74      * @return the headers from the processed response.
75      */

76     public Map<String, String> getResponseHeaders() {
77         return responseHeaders;
78     }
79
80 }
81