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

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

The newest version!
/*
 * Copyright 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.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.core.SdkField;
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.traits.RequiredTrait;
import software.amazon.awssdk.core.traits.TraitType;
import software.amazon.awssdk.protocols.core.ValueToStringConverter;

@SdkInternalApi
public final class QueryParamMarshaller {

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

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

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

    public static final XmlMarshaller SHORT = new SimpleQueryParamMarshaller<>(ValueToStringConverter.FROM_SHORT);

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

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

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

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

    public static final XmlMarshaller> LIST = (list, context, paramName, sdkField) -> {
        if (list == null || list.isEmpty()) {
            return;
        }

        for (Object member : list) {
            context.marshall(MarshallLocation.QUERY_PARAM, member, paramName, null);
        }
    };

    public static final XmlMarshaller> MAP = (map, context, paramName, sdkField) -> {
        if (map == null || map.isEmpty()) {
            return;
        }

        MapTrait mapTrait = sdkField.getRequiredTrait(MapTrait.class, TraitType.MAP_TRAIT);
        SdkField valueField = mapTrait.valueFieldInfo();

        for (Map.Entry entry : map.entrySet()) {
            if (valueField.containsTrait(ListTrait.class, TraitType.LIST_TRAIT)) {
                ((List) entry.getValue()).forEach(val -> {
                    context.marshallerRegistry().getMarshaller(MarshallLocation.QUERY_PARAM, val)
                           .marshall(val, context, entry.getKey(), null);
                });
            } else {
                SimpleQueryParamMarshaller valueMarshaller = (SimpleQueryParamMarshaller)
                    context.marshallerRegistry().getMarshaller(MarshallLocation.QUERY_PARAM, entry.getValue());

                context.request().putRawQueryParameter(entry.getKey(), valueMarshaller.convert(entry.getValue(), null));
            }
        }
    };

    public static final XmlMarshaller NULL = (val, context, paramName, sdkField) -> {
        if (Objects.nonNull(sdkField) && sdkField.containsTrait(RequiredTrait.class, TraitType.REQUIRED_TRAIT)) {
            throw new IllegalArgumentException(String.format("Parameter '%s' must not be null", paramName));
        }
    };

    private QueryParamMarshaller() {
    }

    private static class SimpleQueryParamMarshaller implements XmlMarshaller {

        private final ValueToStringConverter.ValueToString converter;

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

        @Override
        public void marshall(T val, XmlMarshallerContext context, String paramName, SdkField sdkField) {
            context.request().appendRawQueryParameter(paramName, converter.convert(val, sdkField));
        }

        public String convert(T val, SdkField sdkField) {
            return converter.convert(val, sdkField);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy