@Override //TODO: This date formatting is coupled to AWS's format. Should generalize it public StructuredJsonGenerator writeValue(Instant instant) { try { generator.writeNumber(DateUtils.formatUnixTimestampInstant(instant)); } catch (IOException e) { throw new JsonGenerationException(e); } return this; }
private Optional<Instant> getExpiration(JsonNode expirationNode) { return Optional.ofNullable(expirationNode).map(node -> { // Convert the expirationNode string to ISO-8601 format. String expirationValue = node.asText().replaceAll("\\+0000$", "Z"); try { return DateUtils.parseIso8601Date(expirationValue); } catch (RuntimeException e) { throw new IllegalStateException("Unable to parse credentials expiration date from metadata service.", e); } }); }
/** * Parses the specified date string as an RFC 1123 date and returns the Date * object. * * @param dateString * The date string to parse. * * @return The parsed Date object. */ public static Instant parseRfc1123Date(String dateString) { if (dateString == null) { return null; } return parseInstant(dateString, RFC_1123_DATE_TIME); }
@Override public String convert(Instant val, SdkField<Instant> sdkField) { if (val == null) { return null; } TimestampFormatTrait.Format format = sdkField.getOptionalTrait(TimestampFormatTrait.class) .map(TimestampFormatTrait::format) .orElseGet(() -> getDefaultTimestampFormat(sdkField.location(), defaultFormats)); switch (format) { case ISO_8601: return DateUtils.formatIso8601Date(val); case RFC_822: return DateUtils.formatRfc1123Date(val); case UNIX_TIMESTAMP: return DateUtils.formatUnixTimestampInstant(val); default: throw SdkClientException.create("Unsupported timestamp format - " + format); } }
/** * Adds the specified date as text to the current position of the * in-progress XML document. * * @param date * The date to add to the XML document. * * @return This XMLWriter so that additional method calls can be chained * together. */ public XmlWriter value(Date date) { append(escapeXmlEntities(DateUtils.formatIso8601Date(date.toInstant()))); return this; }
@Override public Instant convert(String value, SdkField<Instant> field) { if (value == null) { return null; } TimestampFormatTrait.Format format = resolveTimestampFormat(field); switch (format) { case ISO_8601: return DateUtils.parseIso8601Date(value); case UNIX_TIMESTAMP: return safeParseDate(DateUtils::parseUnixTimestampInstant).apply(value); case UNIX_TIMESTAMP_MILLIS: return safeParseDate(DateUtils::parseUnixTimestampMillisInstant).apply(value); case RFC_822: return DateUtils.parseRfc1123Date(value); default: throw SdkClientException.create("Unrecognized timestamp format - " + format); } }
/** * Step 3 of the AWS Signature version 4 calculation. It involves deriving * the signing key and computing the signature. Refer to * http://docs.aws.amazon * .com/general/latest/gr/sigv4-calculate-signature.html */ protected byte[] deriveSigningKey(AwsCredentials credentials, Aws4SignerRequestParams signerRequestParams) { String cacheKey = computeSigningCacheKeyName(credentials, signerRequestParams); long daysSinceEpochSigningDate = numberOfDaysSinceEpoch(signerRequestParams.getSigningDateTimeMilli()); SignerKey signerKey = SIGNER_CACHE.get(cacheKey); if (signerKey != null && daysSinceEpochSigningDate == signerKey.getNumberOfDaysSinceEpoch()) { return signerKey.getSigningKey(); } LOG.trace(() -> "Generating a new signing key as the signing key not available in the cache for the date: " + TimeUnit.DAYS.toMillis(daysSinceEpochSigningDate)); byte[] signingKey = newSigningKey(credentials, signerRequestParams.getFormattedSigningDate(), signerRequestParams.getRegionName(), signerRequestParams.getServiceSigningName()); SIGNER_CACHE.add(cacheKey, new SignerKey(daysSinceEpochSigningDate, signingKey)); return signingKey; }
@Override public String convert(Instant val, SdkField<Instant> sdkField) { if (val == null) { return null; } TimestampFormatTrait.Format format = sdkField.getOptionalTrait(TimestampFormatTrait.class) .map(TimestampFormatTrait::format) .orElseGet(() -> getDefaultTimestampFormat(sdkField.location(), defaultFormats)); switch (format) { case ISO_8601: return DateUtils.formatIso8601Date(val); case RFC_822: return DateUtils.formatRfc1123Date(val); case UNIX_TIMESTAMP: return DateUtils.formatUnixTimestampInstant(val); default: throw SdkClientException.create("Unsupported timestamp format - " + format); } }
/** * Adds the specified date as text to the current position of the * in-progress XML document. * * @param date * The date to add to the XML document. * * @return This XMLWriter so that additional method calls can be chained * together. */ public XmlWriter value(Date date) { append(escapeXmlEntities(DateUtils.formatIso8601Date(date.toInstant()))); return this; }
@Override public Instant convert(String value, SdkField<Instant> field) { if (value == null) { return null; } TimestampFormatTrait.Format format = resolveTimestampFormat(field); switch (format) { case ISO_8601: return DateUtils.parseIso8601Date(value); case UNIX_TIMESTAMP: return safeParseDate(DateUtils::parseUnixTimestampInstant).apply(value); case UNIX_TIMESTAMP_MILLIS: return safeParseDate(DateUtils::parseUnixTimestampMillisInstant).apply(value); case RFC_822: return DateUtils.parseRfc1123Date(value); default: throw SdkClientException.create("Unrecognized timestamp format - " + format); } }
/** * Step 3 of the AWS Signature version 4 calculation. It involves deriving * the signing key and computing the signature. Refer to * http://docs.aws.amazon * .com/general/latest/gr/sigv4-calculate-signature.html */ protected byte[] deriveSigningKey(AwsCredentials credentials, Aws4SignerRequestParams signerRequestParams) { String cacheKey = computeSigningCacheKeyName(credentials, signerRequestParams); long daysSinceEpochSigningDate = numberOfDaysSinceEpoch(signerRequestParams.getSigningDateTimeMilli()); SignerKey signerKey = SIGNER_CACHE.get(cacheKey); if (signerKey != null && daysSinceEpochSigningDate == signerKey.getNumberOfDaysSinceEpoch()) { return signerKey.getSigningKey(); } LOG.trace(() -> "Generating a new signing key as the signing key not available in the cache for the date: " + TimeUnit.DAYS.toMillis(daysSinceEpochSigningDate)); byte[] signingKey = newSigningKey(credentials, signerRequestParams.getFormattedSigningDate(), signerRequestParams.getRegionName(), signerRequestParams.getServiceSigningName()); SIGNER_CACHE.add(cacheKey, new SignerKey(daysSinceEpochSigningDate, signingKey)); return signingKey; }
@Override public void marshall(Instant val, JsonMarshallerContext context, String paramName, SdkField<Instant> sdkField) { StructuredJsonGenerator jsonGenerator = context.jsonGenerator(); if (paramName != null) { jsonGenerator.writeFieldName(paramName); } TimestampFormatTrait trait = sdkField.getTrait(TimestampFormatTrait.class); if (trait != null) { switch (trait.format()) { case UNIX_TIMESTAMP: jsonGenerator.writeNumber(DateUtils.formatUnixTimestampInstant(val)); break; case RFC_822: jsonGenerator.writeValue(DateUtils.formatRfc1123Date(val)); break; case ISO_8601: jsonGenerator.writeValue(DateUtils.formatIso8601Date(val)); break; default: throw SdkClientException.create("Unrecognized timestamp format - " + trait.format()); } } else { // Important to fallback to the jsonGenerator implementation as that may differ per wire format, // irrespective of protocol. I.E. CBOR would default to unix timestamp as milliseconds while JSON // will default to unix timestamp as seconds with millisecond decimal precision. jsonGenerator.writeValue(val); } } };
private Optional<Instant> getExpiration(JsonNode expirationNode) { return Optional.ofNullable(expirationNode).map(node -> { // Convert the expirationNode string to ISO-8601 format. String expirationValue = node.asText().replaceAll("\\+0000$", "Z"); try { return DateUtils.parseIso8601Date(expirationValue); } catch (RuntimeException e) { throw new IllegalStateException("Unable to parse credentials expiration date from metadata service.", e); } }); }
@Override //TODO: This date formatting is coupled to AWS's format. Should generalize it public StructuredJsonGenerator writeValue(Instant instant) { try { generator.writeNumber(DateUtils.formatUnixTimestampInstant(instant)); } catch (IOException e) { throw new JsonGenerationException(e); } return this; }
/** * Parses the specified date string as an RFC 1123 date and returns the Date * object. * * @param dateString * The date string to parse. * * @return The parsed Date object. */ public static Instant parseRfc1123Date(String dateString) { if (dateString == null) { return null; } return parseInstant(dateString, RFC_1123_DATE_TIME); }
@Override public void marshall(Instant val, JsonMarshallerContext context, String paramName, SdkField<Instant> sdkField) { StructuredJsonGenerator jsonGenerator = context.jsonGenerator(); if (paramName != null) { jsonGenerator.writeFieldName(paramName); } TimestampFormatTrait trait = sdkField.getTrait(TimestampFormatTrait.class); if (trait != null) { switch (trait.format()) { case UNIX_TIMESTAMP: jsonGenerator.writeNumber(DateUtils.formatUnixTimestampInstant(val)); break; case RFC_822: jsonGenerator.writeValue(DateUtils.formatRfc1123Date(val)); break; case ISO_8601: jsonGenerator.writeValue(DateUtils.formatIso8601Date(val)); break; default: throw SdkClientException.create("Unrecognized timestamp format - " + trait.format()); } } else { // Important to fallback to the jsonGenerator implementation as that may differ per wire format, // irrespective of protocol. I.E. CBOR would default to unix timestamp as milliseconds while JSON // will default to unix timestamp as seconds with millisecond decimal precision. jsonGenerator.writeValue(val); } } };
/** * Parses the specified date string as an ISO 8601 date (yyyy-MM-dd'T'HH:mm:ss.SSSZZ) * and returns the {@link Instant} object. * * @param dateString * The date string to parse. * * @return The parsed Instant object. */ public static Instant parseIso8601Date(String dateString) { // For EC2 Spot Fleet. if (dateString.endsWith("+0000")) { dateString = dateString .substring(0, dateString.length() - 5) .concat("Z"); } try { return parseInstant(dateString, ISO_INSTANT); } catch (DateTimeParseException e) { return parseInstant(dateString, ALTERNATE_ISO_8601_DATE_FORMAT); } }
/** * Parses the specified date string as an ISO 8601 date (yyyy-MM-dd'T'HH:mm:ss.SSSZZ) * and returns the {@link Instant} object. * * @param dateString * The date string to parse. * * @return The parsed Instant object. */ public static Instant parseIso8601Date(String dateString) { // For EC2 Spot Fleet. if (dateString.endsWith("+0000")) { dateString = dateString .substring(0, dateString.length() - 5) .concat("Z"); } try { return parseInstant(dateString, ISO_INSTANT); } catch (DateTimeParseException e) { return parseInstant(dateString, ALTERNATE_ISO_8601_DATE_FORMAT); } }