mirror of https://github.com/apache/jclouds.git
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.
This commit is contained in:
parent
9677ed18eb
commit
d57bbebe7e
|
@ -51,7 +51,8 @@ public class CopyObjectHandler extends ParseSax.HandlerWithResult<ObjectMetadata
|
|||
if (qName.equals("ETag")) {
|
||||
this.currentETag = currentOrNull(currentText);
|
||||
} else if (qName.equals("LastModified")) {
|
||||
this.currentLastModified = dateParser.iso8601DateParse(currentOrNull(currentText));
|
||||
this.currentLastModified = dateParser
|
||||
.iso8601DateParseWithOptionalTZ(currentOrNull(currentText));
|
||||
} else if (qName.equals("CopyObjectResult")) {
|
||||
metadata = new CopyObjectResult(currentLastModified, currentETag);
|
||||
}
|
||||
|
|
|
@ -68,7 +68,8 @@ public class ListAllMyBucketsHandler extends ParseSax.HandlerWithResult<Set<Buck
|
|||
} else if (qName.equals("Name")) {
|
||||
currentName = currentOrNull(currentText);
|
||||
} else if (qName.equals("CreationDate")) {
|
||||
currentCreationDate = dateParser.iso8601DateParse(currentOrNull(currentText));
|
||||
currentCreationDate = dateParser
|
||||
.iso8601DateParseWithOptionalTZ(currentOrNull(currentText));
|
||||
}
|
||||
currentText = new StringBuilder();
|
||||
}
|
||||
|
|
|
@ -96,7 +96,8 @@ public class ListBucketHandler extends ParseSax.HandlerWithResult<ListBucketResp
|
|||
builder.key(currentKey);
|
||||
builder.uri(uriBuilder(getRequest().getEndpoint()).clearQuery().appendPath(currentKey).build());
|
||||
} else if (qName.equals("LastModified")) {
|
||||
builder.lastModified(dateParser.iso8601DateParse(currentOrNull(currentText)));
|
||||
builder.lastModified(dateParser
|
||||
.iso8601DateParseWithOptionalTZ(currentOrNull(currentText)));
|
||||
} else if (qName.equals("ETag")) {
|
||||
String currentETag = currentOrNull(currentText);
|
||||
builder.eTag(currentETag);
|
||||
|
|
|
@ -25,6 +25,7 @@ import org.jclouds.date.internal.SimpleDateFormatDateService;
|
|||
import org.jclouds.http.functions.BaseHandlerTest;
|
||||
import org.jclouds.s3.domain.ObjectMetadata;
|
||||
import org.jclouds.s3.domain.internal.CopyObjectResult;
|
||||
import org.jclouds.util.Strings2;
|
||||
import org.testng.annotations.BeforeTest;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
|
@ -37,6 +38,8 @@ public class CopyObjectHandlerTest extends BaseHandlerTest {
|
|||
|
||||
private DateService dateService;
|
||||
|
||||
private final String copyObjectXML = "<CopyObjectResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"><LastModified>2014-07-23T20:53:17+0000</LastModified><ETag>\"92836a3ea45a6984d1b4d23a747d46bb\"</ETag></CopyObjectResult>";
|
||||
|
||||
@BeforeTest
|
||||
@Override
|
||||
protected void setUpInjector() {
|
||||
|
@ -57,4 +60,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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
@ -46,6 +47,7 @@ import com.google.common.collect.ImmutableList;
|
|||
@Test(groups = "unit", testName = "ListBucketHandlerTest")
|
||||
public class ListBucketHandlerTest extends BaseHandlerTest {
|
||||
public static final String listBucketWithPrefixAppsSlash = "<ListBucketResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"><Name>adriancole.org.jclouds.s3.amazons3testdelimiter</Name><Prefix>apps/</Prefix><Marker></Marker><MaxKeys>1000</MaxKeys><IsTruncated>false</IsTruncated><Contents><Key>apps/0</Key><LastModified>2009-05-07T18:27:08.000Z</LastModified><ETag>"c82e6a0025c31c5de5947fda62ac51ab"</ETag><Size>8</Size><Owner><ID>e1a5f66a480ca99a4fdfe8e318c3020446c9989d7004e7778029fbcc5d990fa0</ID><DisplayName>ferncam</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents><Contents><Key>apps/1</Key><LastModified>2009-05-07T18:27:09.000Z</LastModified><ETag>"944fab2c5a9a6bacf07db5e688310d7a"</ETag><Size>8</Size><Owner><ID>e1a5f66a480ca99a4fdfe8e318c3020446c9989d7004e7778029fbcc5d990fa0</ID><DisplayName>ferncam</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents><Contents><Key>apps/2</Key><LastModified>2009-05-07T18:27:09.000Z</LastModified><ETag>"a227b8888045c8fd159fb495214000f0"</ETag><Size>8</Size><Owner><ID>e1a5f66a480ca99a4fdfe8e318c3020446c9989d7004e7778029fbcc5d990fa0</ID><DisplayName>ferncam</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents><Contents><Key>apps/3</Key><LastModified>2009-05-07T18:27:09.000Z</LastModified><ETag>"c9caa76c3dec53e2a192608ce73eef03"</ETag><Size>8</Size><Owner><ID>e1a5f66a480ca99a4fdfe8e318c3020446c9989d7004e7778029fbcc5d990fa0</ID><DisplayName>ferncam</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents><Contents><Key>apps/4</Key><LastModified>2009-05-07T18:27:09.000Z</LastModified><ETag>"1ce5d0dcc6154a647ea90c7bdf82a224"</ETag><Size>8</Size><Owner><ID>e1a5f66a480ca99a4fdfe8e318c3020446c9989d7004e7778029fbcc5d990fa0</ID><DisplayName>ferncam</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents><Contents><Key>apps/5</Key><LastModified>2009-05-07T18:27:09.000Z</LastModified><ETag>"79433524d87462ee05708a8ef894ed55"</ETag><Size>8</Size><Owner><ID>e1a5f66a480ca99a4fdfe8e318c3020446c9989d7004e7778029fbcc5d990fa0</ID><DisplayName>ferncam</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents><Contents><Key>apps/6</Key><LastModified>2009-05-07T18:27:10.000Z</LastModified><ETag>"dd00a060b28ddca8bc5a21a49e306f67"</ETag><Size>8</Size><Owner><ID>e1a5f66a480ca99a4fdfe8e318c3020446c9989d7004e7778029fbcc5d990fa0</ID><DisplayName>ferncam</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents><Contents><Key>apps/7</Key><LastModified>2009-05-07T18:27:10.000Z</LastModified><ETag>"8cd06eca6e819a927b07a285d750b100"</ETag><Size>8</Size><Owner><ID>e1a5f66a480ca99a4fdfe8e318c3020446c9989d7004e7778029fbcc5d990fa0</ID><DisplayName>ferncam</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents><Contents><Key>apps/8</Key><LastModified>2009-05-07T18:27:10.000Z</LastModified><ETag>"174495094d0633b92cbe46603eee6bad"</ETag><Size>8</Size><Owner><ID>e1a5f66a480ca99a4fdfe8e318c3020446c9989d7004e7778029fbcc5d990fa0</ID><DisplayName>ferncam</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents><Contents><Key>apps/9</Key><LastModified>2009-05-07T18:27:10.000Z</LastModified><ETag>"cd8a19b26fea8a827276df0ad11c580d"</ETag><Size>8</Size><Owner><ID>e1a5f66a480ca99a4fdfe8e318c3020446c9989d7004e7778029fbcc5d990fa0</ID><DisplayName>ferncam</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents></ListBucketResult>";
|
||||
public static final String listBucketWithTSTimeZone = "<ListBucketResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"><Name>adriancole.org.jclouds.s3.amazons3testdelimiter</Name><Prefix>apps/</Prefix><Marker></Marker><MaxKeys>1000</MaxKeys><IsTruncated>false</IsTruncated><Contents><Key>apps/9</Key><LastModified>2014-07-23T20:53:17+0000</LastModified><ETag>"cd8a19b26fea8a827276df0ad11c580d"</ETag><Size>8</Size><Owner><ID>e1a5f66a480ca99a4fdfe8e318c3020446c9989d7004e7778029fbcc5d990fa0</ID><DisplayName>ferncam</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents></ListBucketResult>";
|
||||
public static final String listBucketWithSlashDelimiterAndCommonPrefixApps = "<ListBucketResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"> <Delimiter>/</Delimiter> <CommonPrefixes><Prefix>apps/</Prefix></CommonPrefixes></ListBucketResult>";
|
||||
private DateService dateService = new SimpleDateFormatDateService();
|
||||
|
||||
|
@ -130,7 +132,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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -73,6 +73,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();
|
||||
|
|
|
@ -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;
|
||||
|
@ -149,7 +150,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);
|
||||
|
@ -201,4 +203,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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -106,6 +106,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);
|
||||
|
|
|
@ -141,4 +141,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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue