mirror of https://github.com/apache/jclouds.git
Merge pull request #641 from aledsage/Issue-647-FixForMalformedExpiresHeader
Issue 647: fix handling of malformed 'Expires' header
This commit is contained in:
commit
49956b5d15
|
@ -17,8 +17,10 @@ import org.jclouds.date.DateCodec;
|
||||||
import org.jclouds.date.DateCodecFactory;
|
import org.jclouds.date.DateCodecFactory;
|
||||||
import org.jclouds.io.ContentMetadataCodec.DefaultContentMetadataCodec;
|
import org.jclouds.io.ContentMetadataCodec.DefaultContentMetadataCodec;
|
||||||
import org.jclouds.logging.Logger;
|
import org.jclouds.logging.Logger;
|
||||||
|
import org.jclouds.util.Throwables2;
|
||||||
|
|
||||||
import com.google.common.base.Predicate;
|
import com.google.common.base.Predicate;
|
||||||
|
import com.google.common.base.Throwables;
|
||||||
import com.google.common.collect.ImmutableMultimap;
|
import com.google.common.collect.ImmutableMultimap;
|
||||||
import com.google.common.collect.ImmutableMultimap.Builder;
|
import com.google.common.collect.ImmutableMultimap.Builder;
|
||||||
import com.google.common.collect.Multimap;
|
import com.google.common.collect.Multimap;
|
||||||
|
@ -115,9 +117,13 @@ public interface ContentMetadataCodec {
|
||||||
public Date parseExpires(String expires) {
|
public Date parseExpires(String expires) {
|
||||||
try {
|
try {
|
||||||
return (expires != null) ? getExpiresDateCodec().toDate(expires) : null;
|
return (expires != null) ? getExpiresDateCodec().toDate(expires) : null;
|
||||||
} catch (ParseException e) {
|
} catch (Exception e) {
|
||||||
logger.warn(e, "Invalid Expires header (%s); should be in RFC-1123 format; treating as already expired", expires);
|
if (Throwables2.getFirstThrowableOfType(e, ParseException.class) != null) {
|
||||||
|
logger.debug("Invalid Expires header (%s); should be in RFC-1123 format; treating as already expired: %s", expires, e.getMessage());
|
||||||
return new Date(0);
|
return new Date(0);
|
||||||
|
} else {
|
||||||
|
throw Throwables.propagate(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,44 @@
|
||||||
|
package org.jclouds.date.internal;
|
||||||
|
|
||||||
|
import static org.testng.Assert.assertEquals;
|
||||||
|
import static org.testng.Assert.fail;
|
||||||
|
|
||||||
|
import java.text.ParseException;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
import org.jclouds.date.DateCodec;
|
||||||
|
import org.jclouds.util.Throwables2;
|
||||||
|
import org.testng.annotations.BeforeMethod;
|
||||||
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
|
public class SimpleDateCodecFactoryTest {
|
||||||
|
|
||||||
|
private SimpleDateCodecFactory simpleDateCodecFactory;
|
||||||
|
private DateCodec rfc1123Codec;
|
||||||
|
|
||||||
|
@BeforeMethod
|
||||||
|
public void setUp() throws Exception {
|
||||||
|
simpleDateCodecFactory = new SimpleDateCodecFactory(new SimpleDateFormatDateService());
|
||||||
|
rfc1123Codec = simpleDateCodecFactory.rfc1123();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCodecForRfc1123() throws Exception {
|
||||||
|
Date date = new Date(1000);
|
||||||
|
assertEquals(rfc1123Codec.toDate(rfc1123Codec.toString(date)), date);
|
||||||
|
|
||||||
|
assertEquals(rfc1123Codec.toDate("Thu, 01 Dec 1994 16:00:00 GMT"), new Date(786297600000L));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCodecForRfc1123ThrowsParseExceptionWhenMalformed() throws Exception {
|
||||||
|
try {
|
||||||
|
rfc1123Codec.toDate("wrong");
|
||||||
|
fail();
|
||||||
|
} catch (Exception e) {
|
||||||
|
if (Throwables2.getFirstThrowableOfType(e, ParseException.class) == null) {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue