Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions services-custom/dynamodb-mapper/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@
</build>

<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>dynamodb</artifactId>
<version>${awsjavasdk.version}</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-dynamodb</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package software.amazon.awssdk.mapper.dynamodb;

import software.amazon.awssdk.mapper.dynamodb.DynamoDBMapper.FailedBatch;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
import com.amazonaws.services.dynamodbv2.model.CreateTableRequest;
import com.amazonaws.services.dynamodbv2.model.DeleteTableRequest;
import com.amazonaws.services.s3.model.Region;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
*/
package software.amazon.awssdk.mapper.dynamodb;

import static com.amazonaws.util.Throwables.failure;

/**
* Generic marshaller for enumerations.
*
Expand All @@ -40,8 +38,8 @@ public abstract class AbstractEnumMarshaller<T extends Enum<T>> implements Dynam
public String marshall(final T obj) {
try {
return obj.name();
} catch (final RuntimeException e) {
throw failure(e, "Unable to marshall the instance of " + obj.getClass() + " into a string");
} catch (final Exception e) {
throw MapperExceptions.failure(e, "Unable to marshall the enum " + obj);
}
}

Expand All @@ -52,8 +50,8 @@ public String marshall(final T obj) {
public T unmarshall(final Class<T> clazz, final String obj) {
try {
return Enum.valueOf(clazz, obj);
} catch (final RuntimeException e) {
throw failure(e, "Unable to unmarshall the string " + obj + " into " + clazz);
} catch (final Exception e) {
throw MapperExceptions.failure(e, "Unable to unmarshall the enum value " + obj);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
package software.amazon.awssdk.mapper.dynamodb;

import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;

/**
* Interface to make it possible to cache the expensive type determination
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import java.lang.reflect.Method;
import java.text.ParseException;

import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;

/**
* Unmarshaller interface to make it possible to cache the expensive
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import java.util.Map;

import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;

/**
* A hook allowing a custom transform/untransform of the raw attribute
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import java.util.List;
import java.util.Map;

import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;

/**
* A virtual {@code AttributeTransformer} that transforms and untransforms
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
import software.amazon.awssdk.mapper.dynamodb.unmarshallers.StringUnmarshaller;
import software.amazon.awssdk.mapper.dynamodb.unmarshallers.UUIDSetUnmarshaller;
import software.amazon.awssdk.mapper.dynamodb.unmarshallers.UUIDUnmarshaller;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import software.amazon.awssdk.mapper.dynamodb.StandardAnnotationMaps.TypedMap;
import software.amazon.awssdk.mapper.dynamodb.StandardTypeConverters.Scalar;
import software.amazon.awssdk.mapper.dynamodb.StandardTypeConverters.Vector;
import com.amazonaws.services.dynamodbv2.model.ScalarAttributeType;
import software.amazon.awssdk.services.dynamodb.model.ScalarAttributeType;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import java.util.HashMap;
import java.util.Map;

import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
import com.amazonaws.services.dynamodbv2.model.ConditionalOperator;
import com.amazonaws.services.dynamodbv2.model.DeleteItemRequest;
import com.amazonaws.services.dynamodbv2.model.ExpectedAttributeValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import static com.amazonaws.services.dynamodbv2.model.ComparisonOperator.NOT_CONTAINS;
import static com.amazonaws.services.dynamodbv2.model.ComparisonOperator.NOT_NULL;

import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
import com.amazonaws.services.dynamodbv2.model.ComparisonOperator;
import com.amazonaws.services.dynamodbv2.model.Condition;
import com.amazonaws.services.dynamodbv2.model.KeyType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import static com.amazonaws.services.dynamodbv2.model.KeyType.RANGE;
import static com.amazonaws.services.dynamodbv2.model.ProjectionType.KEYS_ONLY;

import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
import com.amazonaws.services.dynamodbv2.model.GlobalSecondaryIndex;
import com.amazonaws.services.dynamodbv2.model.KeyType;
import com.amazonaws.services.dynamodbv2.model.KeySchemaElement;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import java.util.HashMap;
import java.util.Map;

import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
import com.amazonaws.services.dynamodbv2.model.Condition;
import com.amazonaws.services.dynamodbv2.model.ConditionalOperator;
import com.amazonaws.services.dynamodbv2.model.QueryRequest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import java.util.HashMap;
import java.util.Map;

import com.amazonaws.services.dynamodbv2.model.ConditionalOperator;
import com.amazonaws.services.dynamodbv2.model.ExpectedAttributeValue;
import software.amazon.awssdk.services.dynamodb.model.ConditionalOperator;
import software.amazon.awssdk.services.dynamodb.model.ExpectedAttributeValue;

/**
* Enables adding options to a save operation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
package software.amazon.awssdk.mapper.dynamodb;

import com.amazonaws.services.dynamodbv2.model.ScalarAttributeType;
import software.amazon.awssdk.services.dynamodb.model.ScalarAttributeType;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
Expand All @@ -38,7 +38,7 @@

/**
* The scalar attirbute type.
* @see com.amazonaws.services.dynamodbv2.model.ScalarAttributeType
* @see software.amazon.awssdk.services.dynamodb.model.ScalarAttributeType
*/
ScalarAttributeType type();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import java.util.HashMap;
import java.util.Map;

import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
import com.amazonaws.services.dynamodbv2.model.ComparisonOperator;
import com.amazonaws.services.dynamodbv2.model.Condition;
import com.amazonaws.services.dynamodbv2.model.ConditionalOperator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package software.amazon.awssdk.mapper.dynamodb;

import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;

import java.util.Map;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

/**
* Annotation to convert a date object ({@link java.util.Date}, {@link java.util.Calendar}, {@link org.joda.time.DateTime})
* to a {@link com.amazonaws.services.dynamodbv2.model.ScalarAttributeType#N} stored as epoch time.
* to a {@link software.amazon.awssdk.services.dynamodb.model.ScalarAttributeType#N} stored as epoch time.
*
* <p>Alternately, the {@link DynamoDBTyped} annotation may be used,</p>
* <pre class="brush: java">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import software.amazon.awssdk.mapper.dynamodb.DynamoDBMapper.FailedBatch;
import software.amazon.awssdk.mapper.dynamodb.DynamoDBMapperConfig.PaginationLoadingStrategy;
import software.amazon.awssdk.mapper.dynamodb.DynamoDBMapperConfig.SaveBehavior;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
import com.amazonaws.services.dynamodbv2.model.BatchGetItemRequest;
import com.amazonaws.services.dynamodbv2.model.BatchWriteItemRequest;
import com.amazonaws.services.dynamodbv2.model.CreateTableRequest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import java.lang.reflect.Method;
import java.util.Map;

import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;

/**
* The concrete realization of a strategy for converting between Java objects
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
*/
package software.amazon.awssdk.mapper.dynamodb;

import static com.amazonaws.util.Throwables.failure;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
Expand Down Expand Up @@ -65,10 +63,9 @@ public String marshall(T obj) {

try {
return writer.writeValueAsString(obj);
} catch (JsonProcessingException e) {
throw failure(e,
"Unable to marshall the instance of " + obj.getClass()
+ "into a string");
} catch (Exception e) {
throw MapperExceptions.failure(e,
"Unable to marshall the instance of " + obj.getClass() + "into a string");
}
}

Expand All @@ -77,8 +74,7 @@ public T unmarshall(Class<T> clazz, String json) {
try {
return mapper.readValue(json, (getValueType() == null ? clazz : getValueType()));
} catch (Exception e) {
throw failure(e, "Unable to unmarshall the string " + json
+ "into " + clazz);
throw MapperExceptions.failure(e, "Unable to unmarshall the string " + json + "into " + clazz);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* 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.mapper.dynamodb;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;
import org.joda.time.tz.FixedDateTimeZone;
Comment on lines +21 to +25

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to keep using Joda time for this internal class?


import software.amazon.awssdk.annotations.SdkInternalApi;

/**
* A thin port of only the ISO-8601 parse/format methods of the v1 {@code com.amazonaws.util.DateUtils}
* that the mapper's converter layer uses, kept on the same Joda-Time formatters to preserve byte-for-byte
* compatibility with dates written by the v1 mapper.
*/
@SdkInternalApi
public final class MapperDateUtils {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are there tests for this?


private static final DateTimeZone GMT = new FixedDateTimeZone("GMT", "GMT", 0, 0);
private static final long MILLI_SECONDS_OF_365_DAYS = 365L * 24 * 60 * 60 * 1000;

/** ISO 8601 format. */
private static final DateTimeFormatter ISO8601_DATE_FORMAT =
ISODateTimeFormat.dateTime().withZone(GMT);

/** Alternate ISO 8601 format without fractional seconds. */
private static final DateTimeFormatter ALTERNATE_ISO8601_DATE_FORMAT =
DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss'Z'").withZone(GMT);

/** ISO 8601 format with a UTC offset. */
private static final DateTimeFormatter ISO8601_DATE_FORMAT_WITH_OFFSET =
DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZZ");

private static final List<DateTimeFormatter> ALTERNATE_ISO8601_FORMATTERS = Arrays.asList(
ALTERNATE_ISO8601_DATE_FORMAT, ISO8601_DATE_FORMAT_WITH_OFFSET);

private MapperDateUtils() {
}

/**
* Parses the specified date string as an ISO 8601 date and returns the {@link Date} object.
*
* @param dateStringOrig the date string to parse
* @return the parsed {@link Date} object
*/
public static Date parseISO8601Date(final String dateStringOrig) {
String dateString = dateStringOrig;

// For EC2 Spot Fleet.
if (dateString.endsWith("+0000")) {
dateString = dateString
.substring(0, dateString.length() - 5)
.concat("Z");
}

// https://github.com/aws/aws-sdk-java/issues/233
String temp = tempDateStringForJodaTime(dateString);
try {
if (temp.equals(dateString)) {
// Normal case: nothing special here
return new Date(ISO8601_DATE_FORMAT.parseMillis(dateString));
}
// Handling edge case:
// Joda-time can only handle up to year 292278993 but we are given
// 292278994; So we parse the date string by first adjusting
// the year to 292278993. Then we add 1 year back afterwards.
final long milliLess365Days = ISO8601_DATE_FORMAT.parseMillis(temp);
final long milli = milliLess365Days + MILLI_SECONDS_OF_365_DAYS;
if (milli < 0) { // overflow!
// re-parse the original date string using JodaTime so as to
// throw an exception with a consistent message
return new Date(ISO8601_DATE_FORMAT.parseMillis(dateString));
}
return new Date(milli);
} catch (IllegalArgumentException e) {
for (DateTimeFormatter dateTimeFormatter : ALTERNATE_ISO8601_FORMATTERS) {
try {
// If the first ISO 8601 parser didn't work, try the alternate
// version which doesn't include fractional seconds
return new Date(dateTimeFormatter.parseMillis(dateString));
} catch (Exception oops) {
// ignore
}
}

throw e;
}
}

/**
* Formats the specified date as an ISO 8601 string.
*
* @param date the date to format
* @return the ISO 8601 string representing the specified date
*/
public static String formatISO8601Date(final Date date) {
return ISO8601_DATE_FORMAT.print(date.getTime());
}

/**
* Returns a date string with the prefix temporarily substituted, if applicable, so that
* JodaTime can handle it. Otherwise, if not applicable, the original date string is returned.
*
* <p>See https://github.com/aws/aws-sdk-java/issues/233</p>
*/
private static String tempDateStringForJodaTime(final String dateString) {
final String fromPrefix = "292278994-";
final String toPrefix = "292278993-";
return dateString.startsWith(fromPrefix)
? toPrefix + dateString.substring(fromPrefix.length())
: dateString;
}
}
Loading