From 70ceab379f0d002dd22f1f49d3480a7c2ed6c2de Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Sat, 15 Jun 2013 14:49:18 +0300 Subject: [PATCH] INT-3063: 'Expires' HTTP header workaround According to RFC 2616 HTTP clients should ignore invalid values for date-aware header, but Spring MVC `HttpHeaders` raises `IllegalArgumentException` in this case. Proposal fix to Spring Integration just to `try...catch` call of `HttpHeaders.getExpires()` and `return null`. JIRA: https://jira.springsource.org/browse/INT-3063 --- .../http/support/DefaultHttpHeaderMapper.java | 16 +++++++++++--- ...pHeaderMapperFromMessageOutboundTests.java | 21 ++++++++++++++----- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/spring-integration-http/src/main/java/org/springframework/integration/http/support/DefaultHttpHeaderMapper.java b/spring-integration-http/src/main/java/org/springframework/integration/http/support/DefaultHttpHeaderMapper.java index c926a689d0..12c5b87dd0 100644 --- a/spring-integration-http/src/main/java/org/springframework/integration/http/support/DefaultHttpHeaderMapper.java +++ b/spring-integration-http/src/main/java/org/springframework/integration/http/support/DefaultHttpHeaderMapper.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -58,6 +58,7 @@ import org.springframework.util.StringUtils; * @author Oleg Zhurakousky * @author Gunnar Hillert * @author Gary Russell + * @author Artem Bilan * @since 2.0 */ public class DefaultHttpHeaderMapper implements HeaderMapper, BeanFactoryAware, InitializingBean{ @@ -826,8 +827,17 @@ public class DefaultHttpHeaderMapper implements HeaderMapper, BeanF return (StringUtils.hasText(eTag)) ? eTag : null; } else if (EXPIRES.equalsIgnoreCase(name)) { - long expires = source.getExpires(); - return (expires > -1) ? expires : null; + try { + long expires = source.getExpires(); + return (expires > -1) ? expires : null; + } + catch (Exception e) { + if(logger.isDebugEnabled()) { + logger.debug(e.getMessage()); + } + // According to RFC 2616 + return null; + } } else if (IF_NONE_MATCH.equalsIgnoreCase(name)) { return source.getIfNoneMatch(); diff --git a/spring-integration-http/src/test/java/org/springframework/integration/http/support/DefaultHttpHeaderMapperFromMessageOutboundTests.java b/spring-integration-http/src/test/java/org/springframework/integration/http/support/DefaultHttpHeaderMapperFromMessageOutboundTests.java index 68194f8b78..f4bb00d00a 100644 --- a/spring-integration-http/src/test/java/org/springframework/integration/http/support/DefaultHttpHeaderMapperFromMessageOutboundTests.java +++ b/spring-integration-http/src/test/java/org/springframework/integration/http/support/DefaultHttpHeaderMapperFromMessageOutboundTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,10 +16,10 @@ package org.springframework.integration.http.support; -import static junit.framework.Assert.assertEquals; -import static junit.framework.Assert.assertNotNull; -import static junit.framework.Assert.assertNull; -import static junit.framework.Assert.assertTrue; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; import java.nio.charset.Charset; import java.nio.charset.UnsupportedCharsetException; @@ -32,6 +32,7 @@ import java.util.Locale; import java.util.Map; import org.junit.Test; + import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.integration.MessageHeaders; @@ -41,6 +42,7 @@ import org.springframework.util.CollectionUtils; /** * @author Oleg Zhurakousky * @author Mark Fisher + * @author Artem Bilan * @since 2.0.1 */ public class DefaultHttpHeaderMapperFromMessageOutboundTests { @@ -657,4 +659,13 @@ public class DefaultHttpHeaderMapperFromMessageOutboundTests { mapper.fromHeaders(new MessageHeaders(messageHeaders), headers); assertNull(headers.get("Content-Length")); } + + @Test + public void testInt3063InvalidExpiresHeader() { + HttpHeaders headers = new HttpHeaders(); + headers.set("Expires", "-1"); + Map messageHeaders = DefaultHttpHeaderMapper.outboundMapper().toHeaders(headers); + assertEquals(0, messageHeaders.size()); + } + }