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.core;
17
18 import java.time.Instant;
19 import java.util.Map;
20 import software.amazon.awssdk.annotations.SdkProtectedApi;
21 import software.amazon.awssdk.core.SdkField;
22 import software.amazon.awssdk.core.exception.SdkClientException;
23 import software.amazon.awssdk.core.protocol.MarshallLocation;
24 import software.amazon.awssdk.core.traits.TimestampFormatTrait;
25 import software.amazon.awssdk.protocols.core.ValueToStringConverter.ValueToString;
26 import software.amazon.awssdk.utils.DateUtils;
27
28 /**
29  * Implementation of {@link ValueToString} that converts and {@link Instant} to a string. * Respects the
30  * {@link TimestampFormatTrait} if present.
31  */

32 @SdkProtectedApi
33 public final class InstantToString implements ValueToString<Instant> {
34
35     private final Map<MarshallLocation, TimestampFormatTrait.Format> defaultFormats;
36
37     private InstantToString(Map<MarshallLocation, TimestampFormatTrait.Format> defaultFormats) {
38         this.defaultFormats = defaultFormats;
39     }
40
41     @Override
42     public String convert(Instant val, SdkField<Instant> sdkField) {
43         if (val == null) {
44             return null;
45         }
46         TimestampFormatTrait.Format format =
47             sdkField.getOptionalTrait(TimestampFormatTrait.class)
48                     .map(TimestampFormatTrait::format)
49                     .orElseGet(() -> getDefaultTimestampFormat(sdkField.location(), defaultFormats));
50         switch (format) {
51             case ISO_8601:
52                 return DateUtils.formatIso8601Date(val);
53             case RFC_822:
54                 return DateUtils.formatRfc1123Date(val);
55             case UNIX_TIMESTAMP:
56                 return DateUtils.formatUnixTimestampInstant(val);
57             default:
58                 throw SdkClientException.create("Unsupported timestamp format - " + format);
59         }
60     }
61
62     private TimestampFormatTrait.Format getDefaultTimestampFormat(
63         MarshallLocation location, Map<MarshallLocation, TimestampFormatTrait.Format> defaultFormats) {
64
65         TimestampFormatTrait.Format format = defaultFormats.get(location);
66         if (format == null) {
67             throw SdkClientException.create("No default timestamp marshaller found for location - " + location);
68         }
69         return format;
70     }
71
72     public static InstantToString create(Map<MarshallLocation, TimestampFormatTrait.Format> defaultFormats) {
73         return new InstantToString(defaultFormats);
74     }
75 }
76