All Downloads are FREE. Search and download functionalities are using the official Maven repository.

software.amazon.awssdk.protocols.xml.internal.marshall.XmlPayloadMarshaller Maven / Gradle / Ivy

There is a newer version: 2.28.4
Show newest version
/*
 * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

package software.amazon.awssdk.protocols.xml.internal.marshall;

import java.math.BigDecimal;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.core.SdkBytes;
import software.amazon.awssdk.core.SdkField;
import software.amazon.awssdk.core.SdkPojo;
import software.amazon.awssdk.core.protocol.MarshallLocation;
import software.amazon.awssdk.core.traits.ListTrait;
import software.amazon.awssdk.core.traits.MapTrait;
import software.amazon.awssdk.core.util.SdkAutoConstructList;
import software.amazon.awssdk.core.util.SdkAutoConstructMap;
import software.amazon.awssdk.protocols.core.ValueToStringConverter;

@SdkInternalApi
public class XmlPayloadMarshaller {

    public static final XmlMarshaller STRING = new BasePayloadMarshaller<>(ValueToStringConverter.FROM_STRING);

    public static final XmlMarshaller INTEGER =  new BasePayloadMarshaller<>(ValueToStringConverter.FROM_INTEGER);

    public static final XmlMarshaller LONG = new BasePayloadMarshaller<>(ValueToStringConverter.FROM_LONG);

    public static final XmlMarshaller FLOAT = new BasePayloadMarshaller<>(ValueToStringConverter.FROM_FLOAT);

    public static final XmlMarshaller DOUBLE = new BasePayloadMarshaller<>(ValueToStringConverter.FROM_DOUBLE);

    public static final XmlMarshaller BIG_DECIMAL =
        new BasePayloadMarshaller<>(ValueToStringConverter.FROM_BIG_DECIMAL);

    public static final XmlMarshaller BOOLEAN = new BasePayloadMarshaller<>(ValueToStringConverter.FROM_BOOLEAN);

    public static final XmlMarshaller INSTANT =
        new BasePayloadMarshaller<>(XmlProtocolMarshaller.INSTANT_VALUE_TO_STRING);

    public static final XmlMarshaller SDK_BYTES = new BasePayloadMarshaller<>(ValueToStringConverter.FROM_SDK_BYTES);

    public static final XmlMarshaller SDK_POJO = new BasePayloadMarshaller(null) {
        @Override
        public void marshall(SdkPojo val, XmlMarshallerContext context, String paramName,
                             SdkField sdkField, ValueToStringConverter.ValueToString converter) {
            context.protocolMarshaller().doMarshall(val);
        }
    };

    public static final XmlMarshaller> LIST = new BasePayloadMarshaller>(null) {

        @Override
        public void marshall(List val, XmlMarshallerContext context, String paramName, SdkField> sdkField) {
            if (!shouldEmit(val, paramName)) {
                return;
            }

            marshall(val, context, paramName, sdkField, null);
        }

        @Override
        public void marshall(List list, XmlMarshallerContext context, String paramName,
                             SdkField> sdkField, ValueToStringConverter.ValueToString> converter) {
            ListTrait listTrait = sdkField
                .getOptionalTrait(ListTrait.class)
                .orElseThrow(() -> new IllegalStateException(paramName + " member is missing ListTrait"));

            if (!listTrait.isFlattened()) {
                context.xmlGenerator().startElement(paramName);
            }

            SdkField memberField = listTrait.memberFieldInfo();
            String memberLocationName = listMemberLocationName(listTrait, paramName);

            for (Object listMember : list) {
                context.marshall(MarshallLocation.PAYLOAD, listMember, memberLocationName, memberField);
            }

            if (!listTrait.isFlattened()) {
                context.xmlGenerator().endElement();
            }
        }

        private String listMemberLocationName(ListTrait listTrait, String listLocationName) {
            String locationName = listTrait.memberLocationName();

            if (locationName == null) {
                locationName = listTrait.isFlattened() ? listLocationName : "member";
            }

            return locationName;
        }

        @Override
        protected boolean shouldEmit(List list, String paramName) {
            return super.shouldEmit(list, paramName) &&
                   (!list.isEmpty() || !(list instanceof SdkAutoConstructList));
        }
    };

    // We ignore flattened trait for maps. For rest-xml, none of the services have flattened maps
    public static final XmlMarshaller> MAP = new BasePayloadMarshaller>(null) {

        @Override
        public void marshall(Map map, XmlMarshallerContext context, String paramName,
                             SdkField> sdkField, ValueToStringConverter.ValueToString> converter) {

            MapTrait mapTrait = sdkField.getOptionalTrait(MapTrait.class)
                                        .orElseThrow(() -> new IllegalStateException(paramName + " member is missing MapTrait"));

            for (Map.Entry entry : map.entrySet()) {
                context.xmlGenerator().startElement("entry");
                context.marshall(MarshallLocation.PAYLOAD, entry.getKey(), mapTrait.keyLocationName(), null);
                context.marshall(MarshallLocation.PAYLOAD, entry.getValue(), mapTrait.valueLocationName(),
                                 mapTrait.valueFieldInfo());
                context.xmlGenerator().endElement();
            }
        }

        @Override
        protected boolean shouldEmit(Map map, String paramName) {
            return super.shouldEmit(map, paramName) &&
                   (!map.isEmpty() || !(map instanceof SdkAutoConstructMap));
        }
    };

    private XmlPayloadMarshaller() {
    }

    /**
     * Base payload marshaller for xml protocol. Marshalling happens only when both element name and value are present.
     *
     * Marshalling for simple types is done in the base class.
     * Complex types should override the
     * {@link #marshall(Object, XmlMarshallerContext, String, SdkField, ValueToStringConverter.ValueToString)} method.
     *
     * @param  Type to marshall
     */
    private static class BasePayloadMarshaller implements XmlMarshaller {

        private final ValueToStringConverter.ValueToString converter;

        private BasePayloadMarshaller(ValueToStringConverter.ValueToString converter) {
            this.converter = converter;
        }

        @Override
        public void marshall(T val, XmlMarshallerContext context, String paramName, SdkField sdkField) {
            if (!shouldEmit(val, paramName)) {
                return;
            }

            context.xmlGenerator().startElement(paramName);
            marshall(val, context, paramName, sdkField, converter);
            context.xmlGenerator().endElement();
        }

        void marshall(T val, XmlMarshallerContext context, String paramName, SdkField sdkField,
                      ValueToStringConverter.ValueToString converter) {
            context.xmlGenerator().xmlWriter().value(converter.convert(val, sdkField));
        }

        protected boolean shouldEmit(T val, String paramName) {
            return val != null && paramName != null;
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy