software.amazon.awssdk.protocols.xml.internal.marshall.XmlPayloadMarshaller Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of aws-xml-protocol Show documentation
Show all versions of aws-xml-protocol Show documentation
The AWS SDK for Java - module holds the classes for AWS Xml protocol
/*
* 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