1 /*
2  * Copyright 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
16 package software.amazon.awssdk.protocols.xml;
17
18 import java.util.Optional;
19 import java.util.function.Supplier;
20 import software.amazon.awssdk.annotations.SdkProtectedApi;
21 import software.amazon.awssdk.awscore.AwsResponse;
22 import software.amazon.awssdk.core.Response;
23 import software.amazon.awssdk.core.SdkPojo;
24 import software.amazon.awssdk.core.http.HttpResponseHandler;
25 import software.amazon.awssdk.protocols.query.unmarshall.XmlElement;
26 import software.amazon.awssdk.protocols.xml.internal.unmarshall.AwsXmlPredicatedResponseHandler;
27 import software.amazon.awssdk.protocols.xml.internal.unmarshall.DecorateErrorFromResponseBodyUnmarshaller;
28
29 /**
30  * Factory to generate the various protocol handlers and generators to be used for communicating with
31  * Amazon S3. S3 has some unique differences from typical REST/XML that warrant a custom protocol factory.
32  */

33 @SdkProtectedApi
34 public final class AwsS3ProtocolFactory extends AwsXmlProtocolFactory {
35     private AwsS3ProtocolFactory(Builder builder) {
36         super(builder);
37     }
38
39     /**
40      * For Amazon S3, the Code, Message, and modeled fields are in the top level document.
41      *
42      * @param document Root XML document.
43      * @return If error root is found than a fulfilled {@link Optional}, otherwise an empty one.
44      */

45     @Override
46     Optional<XmlElement> getErrorRoot(XmlElement document) {
47         return Optional.of(document);
48     }
49
50     public static Builder builder() {
51         return new Builder();
52     }
53
54     /**
55      * Builder for {@link AwsS3ProtocolFactory}.
56      */

57     public static final class Builder extends AwsXmlProtocolFactory.Builder<Builder> {
58
59         private Builder() {
60         }
61
62         public AwsS3ProtocolFactory build() {
63             return new AwsS3ProtocolFactory(this);
64         }
65     }
66
67     @Override
68     public <T extends AwsResponse> HttpResponseHandler<Response<T>> createCombinedResponseHandler(
69         Supplier<SdkPojo> pojoSupplier, XmlOperationMetadata staxOperationMetadata) {
70
71         return createErrorCouldBeInBodyResponseHandler(pojoSupplier, staxOperationMetadata);
72     }
73
74     private <T extends AwsResponse> HttpResponseHandler<Response<T>> createErrorCouldBeInBodyResponseHandler(
75         Supplier<SdkPojo> pojoSupplier, XmlOperationMetadata staxOperationMetadata) {
76
77         return new AwsXmlPredicatedResponseHandler<>(r -> pojoSupplier.get(),
78                                                      createResponseTransformer(pojoSupplier),
79                                                      createErrorTransformer(),
80                                                      DecorateErrorFromResponseBodyUnmarshaller.of(this::getErrorRoot),
81                                                      staxOperationMetadata.isHasStreamingSuccessResponse());
82     }
83 }
84