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;
@ -49,21 +48,19 @@ import com.google.common.base.Function;
* @author Adrian Cole * @author Adrian Cole
*/ */
public class ParseSystemAndUserMetadataFromHeaders implements Function<HttpResponse, MutableBlobMetadata>, public class ParseSystemAndUserMetadataFromHeaders implements Function<HttpResponse, MutableBlobMetadata>,
InvocationContext<ParseSystemAndUserMetadataFromHeaders> { InvocationContext<ParseSystemAndUserMetadataFromHeaders> {
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) {
@ -84,7 +81,7 @@ public class ParseSystemAndUserMetadataFromHeaders implements Function<HttpRespo
for (Entry<String, String> header : from.getHeaders().entries()) { for (Entry<String, String> header : from.getHeaders().entries()) {
if (header.getKey() != null && header.getKey().startsWith(metadataPrefix)) if (header.getKey() != null && header.getKey().startsWith(metadataPrefix))
metadata.getUserMetadata().put((header.getKey().substring(metadataPrefix.length())).toLowerCase(), metadata.getUserMetadata().put((header.getKey().substring(metadataPrefix.length())).toLowerCase(),
header.getValue()); header.getValue());
} }
} }
@ -95,11 +92,12 @@ public class ParseSystemAndUserMetadataFromHeaders implements Function<HttpRespo
// scaleup-storage uses the wrong case for the last modified header // scaleup-storage uses the wrong case for the last modified header
if ((lastModified = from.getFirstHeaderOrNull("Last-modified")) == null) if ((lastModified = from.getFirstHeaderOrNull("Last-modified")) == null)
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,14 +55,14 @@ 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");
} }
@Test @Test
public void testApplySetsName() { public void testApplySetsName() {
HttpResponse from = new HttpResponse(200, "ok", Payloads.newStringPayload(""), ImmutableMultimap.of( HttpResponse from = new HttpResponse(200, "ok", Payloads.newStringPayload(""), ImmutableMultimap.of(
HttpHeaders.LAST_MODIFIED, "Wed, 09 Sep 2009 19:50:23 GMT")); HttpHeaders.LAST_MODIFIED, "Wed, 09 Sep 2009 19:50:23 GMT"));
from.getPayload().getContentMetadata().setContentType(MediaType.APPLICATION_JSON); from.getPayload().getContentMetadata().setContentType(MediaType.APPLICATION_JSON);
from.getPayload().getContentMetadata().setContentLength(100l); from.getPayload().getContentMetadata().setContentLength(100l);
BlobMetadata metadata = parser.apply(from); BlobMetadata metadata = parser.apply(from);
@ -72,18 +72,18 @@ public class ParseSystemAndUserMetadataFromHeadersTest {
@Test @Test
public void testNoContentOn204IsOk() { public void testNoContentOn204IsOk() {
HttpResponse from = new HttpResponse(204, "ok", Payloads.newStringPayload(""), ImmutableMultimap.of( HttpResponse from = new HttpResponse(204, "ok", Payloads.newStringPayload(""), ImmutableMultimap.of(
HttpHeaders.LAST_MODIFIED, "Wed, 09 Sep 2009 19:50:23 GMT")); HttpHeaders.LAST_MODIFIED, "Wed, 09 Sep 2009 19:50:23 GMT"));
parser.apply(from); parser.apply(from);
} }
@Test @Test
public void testSetLastModified() { public void testSetLastModified() {
HttpResponse from = new HttpResponse(200, "ok", Payloads.newStringPayload(""), ImmutableMultimap.of( HttpResponse from = new HttpResponse(200, "ok", Payloads.newStringPayload(""), ImmutableMultimap.of(
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)
@ -96,7 +96,7 @@ public class ParseSystemAndUserMetadataFromHeadersTest {
@Test @Test
public void testAddETagTo() { public void testAddETagTo() {
HttpResponse from = new HttpResponse(200, "ok", Payloads.newStringPayload(""), ImmutableMultimap.of( HttpResponse from = new HttpResponse(200, "ok", Payloads.newStringPayload(""), ImmutableMultimap.of(
HttpHeaders.ETAG, "0xfeb")); HttpHeaders.ETAG, "0xfeb"));
MutableBlobMetadata metadata = blobMetadataProvider.get(); MutableBlobMetadata metadata = blobMetadataProvider.get();
parser.addETagTo(from, metadata); parser.addETagTo(from, metadata);
assertEquals(metadata.getETag(), "0xfeb"); assertEquals(metadata.getETag(), "0xfeb");
@ -105,7 +105,7 @@ public class ParseSystemAndUserMetadataFromHeadersTest {
@Test @Test
public void testAddUserMetadataTo() { public void testAddUserMetadataTo() {
HttpResponse from = new HttpResponse(200, "ok", Payloads.newStringPayload(""), ImmutableMultimap.of("prefix" HttpResponse from = new HttpResponse(200, "ok", Payloads.newStringPayload(""), ImmutableMultimap.of("prefix"
+ "key", "value")); + "key", "value"));
MutableBlobMetadata metadata = blobMetadataProvider.get(); MutableBlobMetadata metadata = blobMetadataProvider.get();
parser.addUserMetadataTo(from, metadata); parser.addUserMetadataTo(from, metadata);
assertEquals(metadata.getUserMetadata().get("key"), "value"); assertEquals(metadata.getUserMetadata().get("key"), "value");