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

software.amazon.awssdk.protocols.json.internal.unmarshall.HeaderUnmarshaller Maven / Gradle / Ivy

/*
 * 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.json.internal.unmarshall;

import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.List;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.core.SdkField;
import software.amazon.awssdk.core.traits.JsonValueTrait;
import software.amazon.awssdk.core.traits.TraitType;
import software.amazon.awssdk.protocols.core.StringToValueConverter;
import software.amazon.awssdk.protocols.jsoncore.JsonNode;
import software.amazon.awssdk.utils.BinaryUtils;

/**
 * Header unmarshallers for all the simple types we support.
 */
@SdkInternalApi
final class HeaderUnmarshaller {

    public static final JsonUnmarshaller STRING =
        new SimpleHeaderUnmarshaller<>(HeaderUnmarshaller::unmarshallStringHeader);
    public static final JsonUnmarshaller INTEGER = new SimpleHeaderUnmarshaller<>(StringToValueConverter.TO_INTEGER);
    public static final JsonUnmarshaller LONG = new SimpleHeaderUnmarshaller<>(StringToValueConverter.TO_LONG);
    public static final JsonUnmarshaller SHORT = new SimpleHeaderUnmarshaller<>(StringToValueConverter.TO_SHORT);
    public static final JsonUnmarshaller DOUBLE = new SimpleHeaderUnmarshaller<>(StringToValueConverter.TO_DOUBLE);
    public static final JsonUnmarshaller BOOLEAN = new SimpleHeaderUnmarshaller<>(StringToValueConverter.TO_BOOLEAN);
    public static final JsonUnmarshaller FLOAT = new SimpleHeaderUnmarshaller<>(StringToValueConverter.TO_FLOAT);

    // Only supports string value type
    public static final JsonUnmarshaller> LIST =
        (context, jsonContent, field) -> context.response().matchingHeaders(field.locationName());

    private HeaderUnmarshaller() {
    }

    /**
     * Unmarshalls a string header, taking into account whether it's a Base 64 encoded JSON value.
     * 

* Note: This code does no attempt to validate whether the unmarshalled string does, in fact, represent valid * JSON values. The string value is returned as-is, and it's up to the user to validate the results. * * @param value Value to unmarshall * @param field {@link SdkField} containing metadata about member being unmarshalled. * @return Unmarshalled value. */ private static String unmarshallStringHeader(String value, SdkField field) { return field.containsTrait(JsonValueTrait.class, TraitType.JSON_VALUE_TRAIT) ? new String(BinaryUtils.fromBase64(value), StandardCharsets.UTF_8) : value; } public static JsonUnmarshaller createInstantHeaderUnmarshaller( StringToValueConverter.StringToValue instantStringToValue) { return new SimpleHeaderUnmarshaller<>(instantStringToValue); } /** * Simple unmarshaller implementation that calls a {@link StringToValueConverter} with the header value if it's present. * * @param Type to unmarshall into. */ private static class SimpleHeaderUnmarshaller implements JsonUnmarshaller { private final StringToValueConverter.StringToValue stringToValue; private SimpleHeaderUnmarshaller(StringToValueConverter.StringToValue stringToValue) { this.stringToValue = stringToValue; } @Override public T unmarshall(JsonUnmarshallerContext context, JsonNode jsonContent, SdkField field) { return context.response().firstMatchingHeader(field.locationName()) .map(s -> stringToValue.convert(s, field)) .orElse(null); } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy