From 8ce9f79fde4497d1131f4996c1a0f1006c59d6e8 Mon Sep 17 00:00:00 2001 From: Shri Javadekar Date: Fri, 25 Jul 2014 16:59:18 -0700 Subject: [PATCH] Try iso8601SecondsDateParse if iso8601DateParse fails. S3 compatible blobStores sometimes return date in the format: "2014-07-23T20:53:17+0000" instead of the more common "2014-07-23T18:09:39.944Z". This caused jclouds to barf with an IllegalArgumentException. This commit tries to parse both the formats for S3. The exception is thrown if both fail. Added unit tests for the same. --- .../org/jclouds/s3/xml/CopyObjectHandler.java | 3 ++- .../s3/xml/ListAllMyBucketsHandler.java | 3 ++- .../org/jclouds/s3/xml/ListBucketHandler.java | 3 ++- .../jclouds/s3/xml/CopyObjectHandlerTest.java | 19 ++++++++++++++++++ .../jclouds/s3/xml/ListBucketHandlerTest.java | 20 ++++++++++++++++++- .../java/org/jclouds/date/DateService.java | 15 ++++++++++++++ .../internal/SimpleDateFormatDateService.java | 18 ++++++++++++++++- .../org/jclouds/date/DateServiceTest.java | 9 +++++++++ .../jclouds/date/joda/JodaDateService.java | 14 +++++++++++++ 9 files changed, 99 insertions(+), 5 deletions(-) diff --git a/apis/s3/src/main/java/org/jclouds/s3/xml/CopyObjectHandler.java b/apis/s3/src/main/java/org/jclouds/s3/xml/CopyObjectHandler.java index 3be7618b2e..c9bcf31cc6 100644 --- a/apis/s3/src/main/java/org/jclouds/s3/xml/CopyObjectHandler.java +++ b/apis/s3/src/main/java/org/jclouds/s3/xml/CopyObjectHandler.java @@ -52,7 +52,8 @@ public class CopyObjectHandler extends ParseSax.HandlerWithResult2014-07-23T20:53:17+0000\"92836a3ea45a6984d1b4d23a747d46bb\""; + @BeforeTest @Override protected void setUpInjector() { @@ -59,4 +62,20 @@ public class CopyObjectHandlerTest extends BaseHandlerTest { assertEquals(result, expected); } + /** + * Verifies that the parser doesn't barf if the timestamp in the copy object + * xml has time zone designators. + */ + public void testTimeStampWithTZ() { + InputStream is = Strings2.toInputStream(copyObjectXML); + ObjectMetadata expected = new CopyObjectResult( + new SimpleDateFormatDateService() + .iso8601SecondsDateParse("2014-07-23T20:53:17+0000"), + "\"92836a3ea45a6984d1b4d23a747d46bb\""); + + ObjectMetadata result = factory.create( + injector.getInstance(CopyObjectHandler.class)).parse(is); + + assertEquals(result, expected); + } } diff --git a/apis/s3/src/test/java/org/jclouds/s3/xml/ListBucketHandlerTest.java b/apis/s3/src/test/java/org/jclouds/s3/xml/ListBucketHandlerTest.java index 21c87f40ea..7b77e28d65 100644 --- a/apis/s3/src/test/java/org/jclouds/s3/xml/ListBucketHandlerTest.java +++ b/apis/s3/src/test/java/org/jclouds/s3/xml/ListBucketHandlerTest.java @@ -33,6 +33,7 @@ import org.jclouds.s3.domain.CanonicalUser; import org.jclouds.s3.domain.ListBucketResponse; import org.jclouds.s3.domain.ObjectMetadata; import org.jclouds.s3.domain.ObjectMetadataBuilder; +import org.jclouds.s3.domain.internal.CopyObjectResult; import org.jclouds.s3.domain.internal.ListBucketResponseImpl; import org.jclouds.util.Strings2; import org.testng.annotations.Test; @@ -48,6 +49,7 @@ import com.google.common.collect.ImmutableList; @Test(groups = "unit", testName = "ListBucketHandlerTest") public class ListBucketHandlerTest extends BaseHandlerTest { public static final String listBucketWithPrefixAppsSlash = "adriancole.org.jclouds.s3.amazons3testdelimiterapps/1000falseapps/02009-05-07T18:27:08.000Z"c82e6a0025c31c5de5947fda62ac51ab"8e1a5f66a480ca99a4fdfe8e318c3020446c9989d7004e7778029fbcc5d990fa0ferncamSTANDARDapps/12009-05-07T18:27:09.000Z"944fab2c5a9a6bacf07db5e688310d7a"8e1a5f66a480ca99a4fdfe8e318c3020446c9989d7004e7778029fbcc5d990fa0ferncamSTANDARDapps/22009-05-07T18:27:09.000Z"a227b8888045c8fd159fb495214000f0"8e1a5f66a480ca99a4fdfe8e318c3020446c9989d7004e7778029fbcc5d990fa0ferncamSTANDARDapps/32009-05-07T18:27:09.000Z"c9caa76c3dec53e2a192608ce73eef03"8e1a5f66a480ca99a4fdfe8e318c3020446c9989d7004e7778029fbcc5d990fa0ferncamSTANDARDapps/42009-05-07T18:27:09.000Z"1ce5d0dcc6154a647ea90c7bdf82a224"8e1a5f66a480ca99a4fdfe8e318c3020446c9989d7004e7778029fbcc5d990fa0ferncamSTANDARDapps/52009-05-07T18:27:09.000Z"79433524d87462ee05708a8ef894ed55"8e1a5f66a480ca99a4fdfe8e318c3020446c9989d7004e7778029fbcc5d990fa0ferncamSTANDARDapps/62009-05-07T18:27:10.000Z"dd00a060b28ddca8bc5a21a49e306f67"8e1a5f66a480ca99a4fdfe8e318c3020446c9989d7004e7778029fbcc5d990fa0ferncamSTANDARDapps/72009-05-07T18:27:10.000Z"8cd06eca6e819a927b07a285d750b100"8e1a5f66a480ca99a4fdfe8e318c3020446c9989d7004e7778029fbcc5d990fa0ferncamSTANDARDapps/82009-05-07T18:27:10.000Z"174495094d0633b92cbe46603eee6bad"8e1a5f66a480ca99a4fdfe8e318c3020446c9989d7004e7778029fbcc5d990fa0ferncamSTANDARDapps/92009-05-07T18:27:10.000Z"cd8a19b26fea8a827276df0ad11c580d"8e1a5f66a480ca99a4fdfe8e318c3020446c9989d7004e7778029fbcc5d990fa0ferncamSTANDARD"; + public static final String listBucketWithTSTimeZone = "adriancole.org.jclouds.s3.amazons3testdelimiterapps/1000falseapps/92014-07-23T20:53:17+0000"cd8a19b26fea8a827276df0ad11c580d"8e1a5f66a480ca99a4fdfe8e318c3020446c9989d7004e7778029fbcc5d990fa0ferncamSTANDARD"; public static final String listBucketWithSlashDelimiterAndCommonPrefixApps = " / apps/"; private DateService dateService = new SimpleDateFormatDateService(); @@ -132,7 +134,23 @@ public class ListBucketHandlerTest extends BaseHandlerTest { assertEquals(bucket.getPrefix(), "apps/"); assertEquals(bucket.getMaxKeys(), 1000); assert bucket.getMarker() == null; - } + /** + * Verifies that the parser doesn't barf if the timestamp returned in the + * list bucket response has time zone designators in it. + */ + @Test + public void testListMyBucketsWithTZ() { + ListBucketResponse bucket = createParser().parse( + Strings2.toInputStream(listBucketWithTSTimeZone)); + ObjectMetadata expected = new CopyObjectResult( + new SimpleDateFormatDateService() + .iso8601SecondsDateParse("2014-07-23T20:53:17+0000"), + "\"92836a3ea45a6984d1b4d23a747d46bb\""); + + // Verify that the date was parsed successfully. + ObjectMetadata metadata = bucket.iterator().next(); + assertEquals(metadata.getLastModified(), expected.getLastModified()); + } } diff --git a/core/src/main/java/org/jclouds/date/DateService.java b/core/src/main/java/org/jclouds/date/DateService.java index 53652e6c8b..7d5b3bd6d3 100644 --- a/core/src/main/java/org/jclouds/date/DateService.java +++ b/core/src/main/java/org/jclouds/date/DateService.java @@ -76,6 +76,21 @@ public interface DateService { */ Date iso8601SecondsDateParse(String toParse) throws IllegalArgumentException; + /** + * Parse a given date in either of two iso8601 formats: + * "yyyy-MM-dd'T'HH:mm:ssZ" or "yyyy-MM-dd'T'HH:mm:ss.SSSZ". The latter one + * has the timezone designator, e.g. 2014-07-23T20:53:17+0000. At least one + * S3 compatible blobstore uses both these formats when returning + * container/object metadata. + * + * @param toParse + * The string to parse. + * @return the Date object of the parsed string. + * @throws IllegalArgumentException + */ + Date iso8601DateParseWithOptionalTZ(String toParse) + throws IllegalArgumentException; + String rfc1123DateFormat(Date date); String rfc1123DateFormat(); diff --git a/core/src/main/java/org/jclouds/date/internal/SimpleDateFormatDateService.java b/core/src/main/java/org/jclouds/date/internal/SimpleDateFormatDateService.java index d1611ac802..e3ce9130d8 100644 --- a/core/src/main/java/org/jclouds/date/internal/SimpleDateFormatDateService.java +++ b/core/src/main/java/org/jclouds/date/internal/SimpleDateFormatDateService.java @@ -18,6 +18,7 @@ package org.jclouds.date.internal; import static org.jclouds.date.internal.DateUtils.findTZ; import static org.jclouds.date.internal.DateUtils.trimTZ; import static org.jclouds.date.internal.DateUtils.trimToMillis; +import static org.jclouds.util.SaxUtils.currentOrNull; import java.text.ParseException; import java.text.SimpleDateFormat; @@ -152,7 +153,8 @@ public class SimpleDateFormatDateService implements DateService { } @Override - public final Date iso8601SecondsDateParse(String toParse) { + public final Date iso8601SecondsDateParse(String toParse) + throws IllegalArgumentException { if (toParse.length() < 10) throw new IllegalArgumentException("incorrect date format " + toParse); String tz = findTZ(toParse); @@ -204,4 +206,18 @@ public class SimpleDateFormatDateService implements DateService { } } } + + @Override + public Date iso8601DateParseWithOptionalTZ(String toParse) + throws IllegalArgumentException { + try { + return iso8601DateParse(toParse); + } catch (IllegalArgumentException orig) { + try { + return iso8601SecondsDateParse(toParse); + } catch (IllegalArgumentException ie) { + throw orig; + } + } + } } diff --git a/core/src/test/java/org/jclouds/date/DateServiceTest.java b/core/src/test/java/org/jclouds/date/DateServiceTest.java index b945275d23..e8c61a3441 100644 --- a/core/src/test/java/org/jclouds/date/DateServiceTest.java +++ b/core/src/test/java/org/jclouds/date/DateServiceTest.java @@ -109,6 +109,15 @@ public class DateServiceTest extends PerformanceTest { assertEquals(dsDate, testData[0].date); } + @Test + public void testIso8601OptionalTZDateParse() { + Date dsDate = dateService + .iso8601DateParseWithOptionalTZ(testData[0].iso8601SecondsDateString); + Date secondsDate = dateService + .iso8601SecondsDateParse(testData[0].iso8601SecondsDateString); + assertEquals(dsDate, secondsDate); + } + @Test public void testIso8601SecondsDateParse() { Date dsDate = dateService.iso8601SecondsDateParse(testData[0].iso8601SecondsDateString); diff --git a/drivers/joda/src/main/java/org/jclouds/date/joda/JodaDateService.java b/drivers/joda/src/main/java/org/jclouds/date/joda/JodaDateService.java index 1658fed680..7bb151987f 100644 --- a/drivers/joda/src/main/java/org/jclouds/date/joda/JodaDateService.java +++ b/drivers/joda/src/main/java/org/jclouds/date/joda/JodaDateService.java @@ -146,4 +146,18 @@ public class JodaDateService implements DateService { public final Date rfc1123DateParse(String toParse) { return rfc1123DateFormat.parseDateTime(toParse).toDate(); } + + @Override + public Date iso8601DateParseWithOptionalTZ(String toParse) + throws IllegalArgumentException { + try { + return iso8601DateParse(toParse); + } catch (IllegalArgumentException orig) { + try { + return iso8601SecondsDateParse(toParse); + } catch (IllegalArgumentException ie) { + throw orig; + } + } + } }