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.internal.marshall;
17
18 import software.amazon.awssdk.annotations.SdkInternalApi;
19 import software.amazon.awssdk.core.protocol.MarshallLocation;
20 import software.amazon.awssdk.core.protocol.MarshallingType;
21 import software.amazon.awssdk.protocols.core.AbstractMarshallingRegistry;
22
23 @SdkInternalApi
24 public final class XmlMarshallerRegistry extends AbstractMarshallingRegistry {
25
26     private XmlMarshallerRegistry(Builder builder) {
27         super(builder);
28     }
29
30     @SuppressWarnings("unchecked")
31     public <T> XmlMarshaller<T> getMarshaller(MarshallLocation marshallLocation, T val) {
32         return (XmlMarshaller<T>) get(marshallLocation, toMarshallingType(val));
33     }
34
35     @SuppressWarnings("unchecked")
36     public <T> XmlMarshaller<Object> getMarshaller(MarshallLocation marshallLocation,
37                                                    MarshallingType<T> marshallingType,
38                                                    Object val) {
39         return (XmlMarshaller<Object>) get(marshallLocation,
40                                            val == null ? MarshallingType.NULL : marshallingType);
41     }
42
43     /**
44      * @return Builder instance to construct a {@link XmlMarshallerRegistry}.
45      */

46     public static Builder builder() {
47         return new Builder();
48     }
49
50     /**
51      * Builder for a {@link XmlMarshallerRegistry}.
52      */

53     public static final class Builder extends AbstractMarshallingRegistry.Builder {
54         private Builder() {
55         }
56
57         public <T> Builder payloadMarshaller(MarshallingType<T> marshallingType,
58                                              XmlMarshaller<T> marshaller) {
59             register(MarshallLocation.PAYLOAD, marshallingType, marshaller);
60             return this;
61         }
62
63         public <T> Builder headerMarshaller(MarshallingType<T> marshallingType,
64                                             XmlMarshaller<T> marshaller) {
65             register(MarshallLocation.HEADER, marshallingType, marshaller);
66             return this;
67         }
68
69         public <T> Builder queryParamMarshaller(MarshallingType<T> marshallingType,
70                                                 XmlMarshaller<T> marshaller) {
71             register(MarshallLocation.QUERY_PARAM, marshallingType, marshaller);
72             return this;
73         }
74
75         public <T> Builder pathParamMarshaller(MarshallingType<T> marshallingType,
76                                                XmlMarshaller<T> marshaller) {
77             register(MarshallLocation.PATH, marshallingType, marshaller);
78             return this;
79         }
80
81         public <T> Builder greedyPathParamMarshaller(MarshallingType<T> marshallingType,
82                                                      XmlMarshaller<T> marshaller) {
83             register(MarshallLocation.GREEDY_PATH, marshallingType, marshaller);
84             return this;
85         }
86
87         /**
88          * @return An immutable {@link XmlMarshallerRegistry} object.
89          */

90         public XmlMarshallerRegistry build() {
91             return new XmlMarshallerRegistry(this);
92         }
93     }
94 }
95