fixed date parser when there's a wrong date format specified

This commit is contained in:
Adrian Cole 2011-01-26 19:23:22 -08:00
parent a14cfac03c
commit 02fffda058
2 changed files with 16 additions and 18 deletions

View File

@ -22,7 +22,6 @@ package org.jclouds.blobstore.functions;
import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState; import static com.google.common.base.Preconditions.checkState;
import static org.jclouds.Constants.PROPERTY_API_VERSION;
import static org.jclouds.blobstore.reference.BlobStoreConstants.PROPERTY_USER_METADATA_PREFIX; import static org.jclouds.blobstore.reference.BlobStoreConstants.PROPERTY_USER_METADATA_PREFIX;
import static org.jclouds.blobstore.util.BlobStoreUtils.getNameFor; import static org.jclouds.blobstore.util.BlobStoreUtils.getNameFor;
@ -53,17 +52,15 @@ public class ParseSystemAndUserMetadataFromHeaders implements Function<HttpRespo
private final String metadataPrefix; private final String metadataPrefix;
private final DateService dateParser; private final DateService dateParser;
private final Provider<MutableBlobMetadata> metadataFactory; private final Provider<MutableBlobMetadata> metadataFactory;
private final String apiVersion;
private String key; private String key;
@Inject @Inject
public ParseSystemAndUserMetadataFromHeaders(Provider<MutableBlobMetadata> metadataFactory, DateService dateParser, public ParseSystemAndUserMetadataFromHeaders(Provider<MutableBlobMetadata> metadataFactory, DateService dateParser,
@Named(PROPERTY_USER_METADATA_PREFIX) String metadataPrefix, @Named(PROPERTY_API_VERSION) String apiVersion) { @Named(PROPERTY_USER_METADATA_PREFIX) String metadataPrefix) {
this.metadataFactory = checkNotNull(metadataFactory, "metadataFactory"); this.metadataFactory = checkNotNull(metadataFactory, "metadataFactory");
this.dateParser = checkNotNull(dateParser, "dateParser"); this.dateParser = checkNotNull(dateParser, "dateParser");
this.metadataPrefix = checkNotNull(metadataPrefix, "metadataPrefix"); this.metadataPrefix = checkNotNull(metadataPrefix, "metadataPrefix");
this.apiVersion = checkNotNull(metadataPrefix, "metadataPrefix");
} }
public MutableBlobMetadata apply(HttpResponse from) { public MutableBlobMetadata apply(HttpResponse from) {
@ -97,9 +94,10 @@ public class ParseSystemAndUserMetadataFromHeaders implements Function<HttpRespo
throw new HttpException(HttpHeaders.LAST_MODIFIED + " header not present in response: " throw new HttpException(HttpHeaders.LAST_MODIFIED + " header not present in response: "
+ from.getStatusLine()); + from.getStatusLine());
} }
// Eucalyptus 1.6 returns iso8601 dates
if (apiVersion.indexOf("Walrus-1.6") != -1) { // Walrus
metadata.setLastModified(dateParser.iso8601DateParse(lastModified.replace("+0000", "Z"))); if (lastModified.startsWith("20")) {
metadata.setLastModified(dateParser.iso8601DateParse(lastModified));
} else { } else {
metadata.setLastModified(dateParser.rfc822DateParse(lastModified)); metadata.setLastModified(dateParser.rfc822DateParse(lastModified));
} }

View File

@ -55,7 +55,7 @@ public class ParseSystemAndUserMetadataFromHeadersTest {
@BeforeTest @BeforeTest
void setUp() { void setUp() {
parser = new ParseSystemAndUserMetadataFromHeaders(blobMetadataProvider, new SimpleDateFormatDateService(), parser = new ParseSystemAndUserMetadataFromHeaders(blobMetadataProvider, new SimpleDateFormatDateService(),
"prefix", "default"); "prefix");
parser.setName("key"); parser.setName("key");
} }
@ -82,8 +82,8 @@ public class ParseSystemAndUserMetadataFromHeadersTest {
HttpHeaders.LAST_MODIFIED, "Wed, 09 Sep 2009 19:50:23 GMT")); HttpHeaders.LAST_MODIFIED, "Wed, 09 Sep 2009 19:50:23 GMT"));
MutableBlobMetadata metadata = blobMetadataProvider.get(); MutableBlobMetadata metadata = blobMetadataProvider.get();
parser.parseLastModifiedOrThrowException(from, metadata); parser.parseLastModifiedOrThrowException(from, metadata);
assertEquals(metadata.getLastModified(), assertEquals(metadata.getLastModified(), new SimpleDateFormatDateService()
new SimpleDateFormatDateService().rfc822DateParse("Wed, 09 Sep 2009 19:50:23 GMT")); .rfc822DateParse("Wed, 09 Sep 2009 19:50:23 GMT"));
} }
@Test(expectedExceptions = HttpException.class) @Test(expectedExceptions = HttpException.class)