Browse Source

InstantFormatter accepts RFC-1123 values as well

Issue: SPR-14201
pull/1047/head
Juergen Hoeller 9 years ago
parent
commit
9412f7a094
  1. 18
      spring-context/src/main/java/org/springframework/format/datetime/standard/InstantFormatter.java
  2. 52
      spring-web/src/test/java/org/springframework/web/method/annotation/RequestHeaderMethodArgumentResolverTests.java

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -18,6 +18,7 @@ package org.springframework.format.datetime.standard;
import java.text.ParseException; import java.text.ParseException;
import java.time.Instant; import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.util.Locale; import java.util.Locale;
import org.springframework.format.Formatter; import org.springframework.format.Formatter;
@ -26,18 +27,29 @@ import org.springframework.lang.UsesJava8;
/** /**
* {@link Formatter} implementation for a JSR-310 {@link java.time.Instant}, * {@link Formatter} implementation for a JSR-310 {@link java.time.Instant},
* following JSR-310's parsing rules for an Instant (that is, not using a * following JSR-310's parsing rules for an Instant (that is, not using a
* configurable {@link java.time.format.DateTimeFormatter}). * configurable {@link java.time.format.DateTimeFormatter}): accepting the
* default {@code ISO_INSTANT} format as well as {@code RFC_1123_DATE_TIME}
* (which is commonly used for HTTP date header values), as of Spring 4.3.
* *
* @author Juergen Hoeller * @author Juergen Hoeller
* @since 4.0 * @since 4.0
* @see java.time.Instant#parse * @see java.time.Instant#parse
* @see java.time.format.DateTimeFormatter#ISO_INSTANT
* @see java.time.format.DateTimeFormatter#RFC_1123_DATE_TIME
*/ */
@UsesJava8 @UsesJava8
public class InstantFormatter implements Formatter<Instant> { public class InstantFormatter implements Formatter<Instant> {
@Override @Override
public Instant parse(String text, Locale locale) throws ParseException { public Instant parse(String text, Locale locale) throws ParseException {
return Instant.parse(text); if (text.length() > 0 && Character.isDigit(text.charAt(0))) {
// assuming UTC instant a la "2007-12-03T10:15:30.00Z"
return Instant.parse(text);
}
else {
// assuming RFC-1123 value a la "Tue, 3 Jun 2008 11:05:30 GMT"
return Instant.from(DateTimeFormatter.RFC_1123_DATE_TIME.parse(text));
}
} }
@Override @Override

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

@ -17,6 +17,9 @@
package org.springframework.web.method.annotation; package org.springframework.web.method.annotation;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Map; import java.util.Map;
import org.junit.After; import org.junit.After;
@ -25,11 +28,14 @@ import org.junit.Test;
import org.springframework.core.MethodParameter; import org.springframework.core.MethodParameter;
import org.springframework.core.annotation.SynthesizingMethodParameter; import org.springframework.core.annotation.SynthesizingMethodParameter;
import org.springframework.format.support.DefaultFormattingConversionService;
import org.springframework.mock.web.test.MockHttpServletRequest; import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.mock.web.test.MockHttpServletResponse; import org.springframework.mock.web.test.MockHttpServletResponse;
import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils;
import org.springframework.web.bind.ServletRequestBindingException; import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.bind.support.DefaultDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest; import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletWebRequest; import org.springframework.web.context.request.ServletWebRequest;
@ -54,6 +60,8 @@ public class RequestHeaderMethodArgumentResolverTests {
private MethodParameter paramResolvedNameWithExpression; private MethodParameter paramResolvedNameWithExpression;
private MethodParameter paramResolvedNameWithPlaceholder; private MethodParameter paramResolvedNameWithPlaceholder;
private MethodParameter paramNamedValueMap; private MethodParameter paramNamedValueMap;
private MethodParameter paramDate;
private MethodParameter paramInstant;
private MockHttpServletRequest servletRequest; private MockHttpServletRequest servletRequest;
@ -75,6 +83,8 @@ public class RequestHeaderMethodArgumentResolverTests {
paramResolvedNameWithExpression = new SynthesizingMethodParameter(method, 4); paramResolvedNameWithExpression = new SynthesizingMethodParameter(method, 4);
paramResolvedNameWithPlaceholder = new SynthesizingMethodParameter(method, 5); paramResolvedNameWithPlaceholder = new SynthesizingMethodParameter(method, 5);
paramNamedValueMap = new SynthesizingMethodParameter(method, 6); paramNamedValueMap = new SynthesizingMethodParameter(method, 6);
paramDate = new SynthesizingMethodParameter(method, 7);
paramInstant = new SynthesizingMethodParameter(method, 8);
servletRequest = new MockHttpServletRequest(); servletRequest = new MockHttpServletRequest();
webRequest = new ServletWebRequest(servletRequest, new MockHttpServletResponse()); webRequest = new ServletWebRequest(servletRequest, new MockHttpServletResponse());
@ -103,7 +113,7 @@ public class RequestHeaderMethodArgumentResolverTests {
Object result = resolver.resolveArgument(paramNamedDefaultValueStringHeader, null, webRequest, null); Object result = resolver.resolveArgument(paramNamedDefaultValueStringHeader, null, webRequest, null);
assertTrue(result instanceof String); assertTrue(result instanceof String);
assertEquals("Invalid result", expected, result); assertEquals(expected, result);
} }
@Test @Test
@ -113,14 +123,14 @@ public class RequestHeaderMethodArgumentResolverTests {
Object result = resolver.resolveArgument(paramNamedValueStringArray, null, webRequest, null); Object result = resolver.resolveArgument(paramNamedValueStringArray, null, webRequest, null);
assertTrue(result instanceof String[]); assertTrue(result instanceof String[]);
assertArrayEquals("Invalid result", expected, (String[]) result); assertArrayEquals(expected, (String[]) result);
} }
@Test @Test
public void resolveDefaultValue() throws Exception { public void resolveDefaultValue() throws Exception {
Object result = resolver.resolveArgument(paramNamedDefaultValueStringHeader, null, webRequest, null); Object result = resolver.resolveArgument(paramNamedDefaultValueStringHeader, null, webRequest, null);
assertTrue(result instanceof String); assertTrue(result instanceof String);
assertEquals("Invalid result", "bar", result); assertEquals("bar", result);
} }
@Test @Test
@ -145,7 +155,7 @@ public class RequestHeaderMethodArgumentResolverTests {
try { try {
Object result = resolver.resolveArgument(paramResolvedNameWithExpression, null, webRequest, null); Object result = resolver.resolveArgument(paramResolvedNameWithExpression, null, webRequest, null);
assertTrue(result instanceof String); assertTrue(result instanceof String);
assertEquals("Invalid result", expected, result); assertEquals(expected, result);
} }
finally { finally {
System.clearProperty("systemProperty"); System.clearProperty("systemProperty");
@ -161,7 +171,7 @@ public class RequestHeaderMethodArgumentResolverTests {
try { try {
Object result = resolver.resolveArgument(paramResolvedNameWithPlaceholder, null, webRequest, null); Object result = resolver.resolveArgument(paramResolvedNameWithPlaceholder, null, webRequest, null);
assertTrue(result instanceof String); assertTrue(result instanceof String);
assertEquals("Invalid result", expected, result); assertEquals(expected, result);
} }
finally { finally {
System.clearProperty("systemProperty"); System.clearProperty("systemProperty");
@ -182,6 +192,34 @@ public class RequestHeaderMethodArgumentResolverTests {
resolver.resolveArgument(paramNamedValueStringArray, null, webRequest, null); resolver.resolveArgument(paramNamedValueStringArray, null, webRequest, null);
} }
@Test
public void dateConversion() throws Exception {
String rfc1123val = "Thu, 21 Apr 2016 17:11:08 +0100";
servletRequest.addHeader("name", rfc1123val);
ConfigurableWebBindingInitializer bindingInitializer = new ConfigurableWebBindingInitializer();
bindingInitializer.setConversionService(new DefaultFormattingConversionService());
Object result = resolver.resolveArgument(paramDate, null, webRequest,
new DefaultDataBinderFactory(bindingInitializer));
assertTrue(result instanceof Date);
assertEquals(new Date(rfc1123val), result);
}
@Test
public void instantConversion() throws Exception {
String rfc1123val = "Thu, 21 Apr 2016 17:11:08 +0100";
servletRequest.addHeader("name", rfc1123val);
ConfigurableWebBindingInitializer bindingInitializer = new ConfigurableWebBindingInitializer();
bindingInitializer.setConversionService(new DefaultFormattingConversionService());
Object result = resolver.resolveArgument(paramInstant, null, webRequest,
new DefaultDataBinderFactory(bindingInitializer));
assertTrue(result instanceof Instant);
assertEquals(Instant.from(DateTimeFormatter.RFC_1123_DATE_TIME.parse(rfc1123val)), result);
}
public void params( public void params(
@RequestHeader(name = "name", defaultValue = "bar") String param1, @RequestHeader(name = "name", defaultValue = "bar") String param1,
@ -190,7 +228,9 @@ public class RequestHeaderMethodArgumentResolverTests {
@RequestHeader(name = "name", defaultValue="#{request.contextPath}") String param4, @RequestHeader(name = "name", defaultValue="#{request.contextPath}") String param4,
@RequestHeader("#{systemProperties.systemProperty}") String param5, @RequestHeader("#{systemProperties.systemProperty}") String param5,
@RequestHeader("${systemProperty}") String param6, @RequestHeader("${systemProperty}") String param6,
@RequestHeader("name") Map<?, ?> unsupported) { @RequestHeader("name") Map<?, ?> unsupported,
@RequestHeader("name") Date dateParam,
@RequestHeader("name") Instant instantParam) {
} }
} }

Loading…
Cancel
Save