From fffa3eb8aff59d82f5d1342bd758da50ed4d7cb1 Mon Sep 17 00:00:00 2001 From: Aled Sage Date: Thu, 17 May 2012 23:37:06 +0100 Subject: [PATCH] Issue 647: fix handling of malformed 'Expires' header --- .../org/jclouds/io/ContentMetadataCodec.java | 12 +++-- .../internal/SimpleDateCodecFactoryTest.java | 44 +++++++++++++++++++ 2 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 core/src/test/java/org/jclouds/date/internal/SimpleDateCodecFactoryTest.java diff --git a/core/src/main/java/org/jclouds/io/ContentMetadataCodec.java b/core/src/main/java/org/jclouds/io/ContentMetadataCodec.java index e10a572870..4d2c801dbe 100644 --- a/core/src/main/java/org/jclouds/io/ContentMetadataCodec.java +++ b/core/src/main/java/org/jclouds/io/ContentMetadataCodec.java @@ -17,8 +17,10 @@ import org.jclouds.date.DateCodec; import org.jclouds.date.DateCodecFactory; import org.jclouds.io.ContentMetadataCodec.DefaultContentMetadataCodec; import org.jclouds.logging.Logger; +import org.jclouds.util.Throwables2; import com.google.common.base.Predicate; +import com.google.common.base.Throwables; import com.google.common.collect.ImmutableMultimap; import com.google.common.collect.ImmutableMultimap.Builder; import com.google.common.collect.Multimap; @@ -115,9 +117,13 @@ public interface ContentMetadataCodec { public Date parseExpires(String expires) { try { return (expires != null) ? getExpiresDateCodec().toDate(expires) : null; - } catch (ParseException e) { - logger.warn(e, "Invalid Expires header (%s); should be in RFC-1123 format; treating as already expired", expires); - return new Date(0); + } catch (Exception e) { + 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); + } else { + throw Throwables.propagate(e); + } } } } diff --git a/core/src/test/java/org/jclouds/date/internal/SimpleDateCodecFactoryTest.java b/core/src/test/java/org/jclouds/date/internal/SimpleDateCodecFactoryTest.java new file mode 100644 index 0000000000..7e03049840 --- /dev/null +++ b/core/src/test/java/org/jclouds/date/internal/SimpleDateCodecFactoryTest.java @@ -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; + } + } + } +}