Skip to content

Commit 9412f7a

Browse files
committed
InstantFormatter accepts RFC-1123 values as well
Issue: SPR-14201
1 parent 777767c commit 9412f7a

File tree

2 files changed

+61
-9
lines changed

2 files changed

+61
-9
lines changed

spring-context/src/main/java/org/springframework/format/datetime/standard/InstantFormatter.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818

1919
import java.text.ParseException;
2020
import java.time.Instant;
21+
import java.time.format.DateTimeFormatter;
2122
import java.util.Locale;
2223

2324
import org.springframework.format.Formatter;
@@ -26,18 +27,29 @@
2627
/**
2728
* {@link Formatter} implementation for a JSR-310 {@link java.time.Instant},
2829
* following JSR-310's parsing rules for an Instant (that is, not using a
29-
* configurable {@link java.time.format.DateTimeFormatter}).
30+
* configurable {@link java.time.format.DateTimeFormatter}): accepting the
31+
* default {@code ISO_INSTANT} format as well as {@code RFC_1123_DATE_TIME}
32+
* (which is commonly used for HTTP date header values), as of Spring 4.3.
3033
*
3134
* @author Juergen Hoeller
3235
* @since 4.0
3336
* @see java.time.Instant#parse
37+
* @see java.time.format.DateTimeFormatter#ISO_INSTANT
38+
* @see java.time.format.DateTimeFormatter#RFC_1123_DATE_TIME
3439
*/
3540
@UsesJava8
3641
public class InstantFormatter implements Formatter<Instant> {
3742

3843
@Override
3944
public Instant parse(String text, Locale locale) throws ParseException {
40-
return Instant.parse(text);
45+
if (text.length() > 0 && Character.isDigit(text.charAt(0))) {
46+
// assuming UTC instant a la "2007-12-03T10:15:30.00Z"
47+
return Instant.parse(text);
48+
}
49+
else {
50+
// assuming RFC-1123 value a la "Tue, 3 Jun 2008 11:05:30 GMT"
51+
return Instant.from(DateTimeFormatter.RFC_1123_DATE_TIME.parse(text));
52+
}
4153
}
4254

4355
@Override

spring-web/src/test/java/org/springframework/web/method/annotation/RequestHeaderMethodArgumentResolverTests.java

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
package org.springframework.web.method.annotation;
1818

1919
import java.lang.reflect.Method;
20+
import java.time.Instant;
21+
import java.time.format.DateTimeFormatter;
22+
import java.util.Date;
2023
import java.util.Map;
2124

2225
import org.junit.After;
@@ -25,11 +28,14 @@
2528

2629
import org.springframework.core.MethodParameter;
2730
import org.springframework.core.annotation.SynthesizingMethodParameter;
31+
import org.springframework.format.support.DefaultFormattingConversionService;
2832
import org.springframework.mock.web.test.MockHttpServletRequest;
2933
import org.springframework.mock.web.test.MockHttpServletResponse;
3034
import org.springframework.util.ReflectionUtils;
3135
import org.springframework.web.bind.ServletRequestBindingException;
3236
import org.springframework.web.bind.annotation.RequestHeader;
37+
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
38+
import org.springframework.web.bind.support.DefaultDataBinderFactory;
3339
import org.springframework.web.context.request.NativeWebRequest;
3440
import org.springframework.web.context.request.RequestContextHolder;
3541
import org.springframework.web.context.request.ServletWebRequest;
@@ -54,6 +60,8 @@ public class RequestHeaderMethodArgumentResolverTests {
5460
private MethodParameter paramResolvedNameWithExpression;
5561
private MethodParameter paramResolvedNameWithPlaceholder;
5662
private MethodParameter paramNamedValueMap;
63+
private MethodParameter paramDate;
64+
private MethodParameter paramInstant;
5765

5866
private MockHttpServletRequest servletRequest;
5967

@@ -75,6 +83,8 @@ public void setUp() throws Exception {
7583
paramResolvedNameWithExpression = new SynthesizingMethodParameter(method, 4);
7684
paramResolvedNameWithPlaceholder = new SynthesizingMethodParameter(method, 5);
7785
paramNamedValueMap = new SynthesizingMethodParameter(method, 6);
86+
paramDate = new SynthesizingMethodParameter(method, 7);
87+
paramInstant = new SynthesizingMethodParameter(method, 8);
7888

7989
servletRequest = new MockHttpServletRequest();
8090
webRequest = new ServletWebRequest(servletRequest, new MockHttpServletResponse());
@@ -103,7 +113,7 @@ public void resolveStringArgument() throws Exception {
103113

104114
Object result = resolver.resolveArgument(paramNamedDefaultValueStringHeader, null, webRequest, null);
105115
assertTrue(result instanceof String);
106-
assertEquals("Invalid result", expected, result);
116+
assertEquals(expected, result);
107117
}
108118

109119
@Test
@@ -113,14 +123,14 @@ public void resolveStringArrayArgument() throws Exception {
113123

114124
Object result = resolver.resolveArgument(paramNamedValueStringArray, null, webRequest, null);
115125
assertTrue(result instanceof String[]);
116-
assertArrayEquals("Invalid result", expected, (String[]) result);
126+
assertArrayEquals(expected, (String[]) result);
117127
}
118128

119129
@Test
120130
public void resolveDefaultValue() throws Exception {
121131
Object result = resolver.resolveArgument(paramNamedDefaultValueStringHeader, null, webRequest, null);
122132
assertTrue(result instanceof String);
123-
assertEquals("Invalid result", "bar", result);
133+
assertEquals("bar", result);
124134
}
125135

126136
@Test
@@ -145,7 +155,7 @@ public void resolveNameFromSystemPropertyThroughExpression() throws Exception {
145155
try {
146156
Object result = resolver.resolveArgument(paramResolvedNameWithExpression, null, webRequest, null);
147157
assertTrue(result instanceof String);
148-
assertEquals("Invalid result", expected, result);
158+
assertEquals(expected, result);
149159
}
150160
finally {
151161
System.clearProperty("systemProperty");
@@ -161,7 +171,7 @@ public void resolveNameFromSystemPropertyThroughPlaceholder() throws Exception {
161171
try {
162172
Object result = resolver.resolveArgument(paramResolvedNameWithPlaceholder, null, webRequest, null);
163173
assertTrue(result instanceof String);
164-
assertEquals("Invalid result", expected, result);
174+
assertEquals(expected, result);
165175
}
166176
finally {
167177
System.clearProperty("systemProperty");
@@ -182,6 +192,34 @@ public void notFound() throws Exception {
182192
resolver.resolveArgument(paramNamedValueStringArray, null, webRequest, null);
183193
}
184194

195+
@Test
196+
public void dateConversion() throws Exception {
197+
String rfc1123val = "Thu, 21 Apr 2016 17:11:08 +0100";
198+
servletRequest.addHeader("name", rfc1123val);
199+
200+
ConfigurableWebBindingInitializer bindingInitializer = new ConfigurableWebBindingInitializer();
201+
bindingInitializer.setConversionService(new DefaultFormattingConversionService());
202+
Object result = resolver.resolveArgument(paramDate, null, webRequest,
203+
new DefaultDataBinderFactory(bindingInitializer));
204+
205+
assertTrue(result instanceof Date);
206+
assertEquals(new Date(rfc1123val), result);
207+
}
208+
209+
@Test
210+
public void instantConversion() throws Exception {
211+
String rfc1123val = "Thu, 21 Apr 2016 17:11:08 +0100";
212+
servletRequest.addHeader("name", rfc1123val);
213+
214+
ConfigurableWebBindingInitializer bindingInitializer = new ConfigurableWebBindingInitializer();
215+
bindingInitializer.setConversionService(new DefaultFormattingConversionService());
216+
Object result = resolver.resolveArgument(paramInstant, null, webRequest,
217+
new DefaultDataBinderFactory(bindingInitializer));
218+
219+
assertTrue(result instanceof Instant);
220+
assertEquals(Instant.from(DateTimeFormatter.RFC_1123_DATE_TIME.parse(rfc1123val)), result);
221+
}
222+
185223

186224
public void params(
187225
@RequestHeader(name = "name", defaultValue = "bar") String param1,
@@ -190,7 +228,9 @@ public void params(
190228
@RequestHeader(name = "name", defaultValue="#{request.contextPath}") String param4,
191229
@RequestHeader("#{systemProperties.systemProperty}") String param5,
192230
@RequestHeader("${systemProperty}") String param6,
193-
@RequestHeader("name") Map<?, ?> unsupported) {
231+
@RequestHeader("name") Map<?, ?> unsupported,
232+
@RequestHeader("name") Date dateParam,
233+
@RequestHeader("name") Instant instantParam) {
194234
}
195235

196236
}

0 commit comments

Comments
 (0)