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.checkNotNull;
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.util.BlobStoreUtils.getNameFor;
@ -49,21 +48,19 @@ import com.google.common.base.Function;
* @author Adrian Cole
*/
public class ParseSystemAndUserMetadataFromHeaders implements Function<HttpResponse, MutableBlobMetadata>,
InvocationContext<ParseSystemAndUserMetadataFromHeaders> {
InvocationContext<ParseSystemAndUserMetadataFromHeaders> {
private final String metadataPrefix;
private final DateService dateParser;
private final Provider<MutableBlobMetadata> metadataFactory;
private final String apiVersion;
private String key;
@Inject
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.dateParser = checkNotNull(dateParser, "dateParser");
this.metadataPrefix = checkNotNull(metadataPrefix, "metadataPrefix");
this.apiVersion = checkNotNull(metadataPrefix, "metadataPrefix");
}
public MutableBlobMetadata apply(HttpResponse from) {
@ -84,7 +81,7 @@ public class ParseSystemAndUserMetadataFromHeaders implements Function<HttpRespo
for (Entry<String, String> header : from.getHeaders().entries()) {
if (header.getKey() != null && header.getKey().startsWith(metadataPrefix))
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
if ((lastModified = from.getFirstHeaderOrNull("Last-modified")) == null)
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) {
metadata.setLastModified(dateParser.iso8601DateParse(lastModified.replace("+0000", "Z")));
// Walrus
if (lastModified.startsWith("20")) {
metadata.setLastModified(dateParser.iso8601DateParse(lastModified));
} else {
metadata.setLastModified(dateParser.rfc822DateParse(lastModified));
}

View File

@ -55,14 +55,14 @@ public class ParseSystemAndUserMetadataFromHeadersTest {
@BeforeTest
void setUp() {
parser = new ParseSystemAndUserMetadataFromHeaders(blobMetadataProvider, new SimpleDateFormatDateService(),
"prefix", "default");
"prefix");
parser.setName("key");
}
@Test
public void testApplySetsName() {
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().setContentLength(100l);
BlobMetadata metadata = parser.apply(from);
@ -72,18 +72,18 @@ public class ParseSystemAndUserMetadataFromHeadersTest {
@Test
public void testNoContentOn204IsOk() {
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);
}
@Test
public void testSetLastModified() {
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();
parser.parseLastModifiedOrThrowException(from, metadata);
assertEquals(metadata.getLastModified(),
new SimpleDateFormatDateService().rfc822DateParse("Wed, 09 Sep 2009 19:50:23 GMT"));
assertEquals(metadata.getLastModified(), new SimpleDateFormatDateService()
.rfc822DateParse("Wed, 09 Sep 2009 19:50:23 GMT"));
}
@Test(expectedExceptions = HttpException.class)
@ -96,7 +96,7 @@ public class ParseSystemAndUserMetadataFromHeadersTest {
@Test
public void testAddETagTo() {
HttpResponse from = new HttpResponse(200, "ok", Payloads.newStringPayload(""), ImmutableMultimap.of(
HttpHeaders.ETAG, "0xfeb"));
HttpHeaders.ETAG, "0xfeb"));
MutableBlobMetadata metadata = blobMetadataProvider.get();
parser.addETagTo(from, metadata);
assertEquals(metadata.getETag(), "0xfeb");
@ -105,7 +105,7 @@ public class ParseSystemAndUserMetadataFromHeadersTest {
@Test
public void testAddUserMetadataTo() {
HttpResponse from = new HttpResponse(200, "ok", Payloads.newStringPayload(""), ImmutableMultimap.of("prefix"
+ "key", "value"));
+ "key", "value"));
MutableBlobMetadata metadata = blobMetadataProvider.get();
parser.addUserMetadataTo(from, metadata);
assertEquals(metadata.getUserMetadata().get("key"), "value");