From 2b339db53b0fe3e1bef92c8b164fbe6b5d7d4fe7 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sat, 13 Jun 2015 17:09:58 +0200 Subject: [PATCH] Polish @CrossOrigin - origin --> origins - method --> methods - constants are now actually constant (i.e., static final) --- .../web/bind/annotation/CrossOrigin.java | 16 ++++++++-------- .../annotation/RequestMappingHandlerMapping.java | 7 ++----- .../mvc/method/annotation/CrossOriginTests.java | 12 ++++++------ 3 files changed, 16 insertions(+), 19 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/bind/annotation/CrossOrigin.java b/spring-web/src/main/java/org/springframework/web/bind/annotation/CrossOrigin.java index 4b2ad6dbe8..360b0db220 100644 --- a/spring-web/src/main/java/org/springframework/web/bind/annotation/CrossOrigin.java +++ b/spring-web/src/main/java/org/springframework/web/bind/annotation/CrossOrigin.java @@ -39,19 +39,19 @@ import org.springframework.core.annotation.AliasFor; @Documented public @interface CrossOrigin { - String[] DEFAULT_ORIGIN = { "*" }; + static final String[] DEFAULT_ORIGIN = { "*" }; - String[] DEFAULT_ALLOWED_HEADERS = { "*" }; + static final String[] DEFAULT_ALLOWED_HEADERS = { "*" }; - boolean DEFAULT_ALLOW_CREDENTIALS = true; + static final boolean DEFAULT_ALLOW_CREDENTIALS = true; - long DEFAULT_MAX_AGE = 1800; + static final long DEFAULT_MAX_AGE = 1800; /** - * Alias for {@link #origin}. + * Alias for {@link #origins}. */ - @AliasFor(attribute = "origin") + @AliasFor(attribute = "origins") String[] value() default {}; /** @@ -63,7 +63,7 @@ public @interface CrossOrigin { * @see #value */ @AliasFor(attribute = "value") - String[] origin() default {}; + String[] origins() default {}; /** * List of request headers that can be used during the actual request. @@ -88,7 +88,7 @@ public @interface CrossOrigin { *

If undefined, methods defined by {@link RequestMapping} annotation * are used. */ - RequestMethod[] method() default {}; + RequestMethod[] methods() default {}; /** * Whether the browser should include any cookies associated with the diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMapping.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMapping.java index fbfd99df40..706e86796d 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMapping.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMapping.java @@ -36,7 +36,6 @@ import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.mvc.condition.AbstractRequestCondition; import org.springframework.web.servlet.mvc.condition.CompositeRequestCondition; -import org.springframework.web.servlet.mvc.condition.NameValueExpression; import org.springframework.web.servlet.mvc.condition.RequestCondition; import org.springframework.web.servlet.mvc.method.RequestMappingInfo; import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping; @@ -228,7 +227,6 @@ public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMappi * @param handlerType the handler type for which to create the condition * @return the condition, or {@code null} */ - @SuppressWarnings("unused") protected RequestCondition getCustomTypeCondition(Class handlerType) { return null; } @@ -244,7 +242,6 @@ public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMappi * @param method the handler method for which to create the condition * @return the condition, or {@code null} */ - @SuppressWarnings("unused") protected RequestCondition getCustomMethodCondition(Method method) { return null; } @@ -326,10 +323,10 @@ public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMappi if (annotation == null) { return; } - for (String origin : annotation.origin()) { + for (String origin : annotation.origins()) { config.addAllowedOrigin(origin); } - for (RequestMethod method : annotation.method()) { + for (RequestMethod method : annotation.methods()) { config.addAllowedMethod(method.name()); } for (String header : annotation.allowedHeaders()) { diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/CrossOriginTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/CrossOriginTests.java index 4f58b36cf8..690a743e6e 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/CrossOriginTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/CrossOriginTests.java @@ -56,8 +56,9 @@ import static org.junit.Assert.*; */ public class CrossOriginTests { - private TestRequestMappingInfoHandlerMapping handlerMapping; - private MockHttpServletRequest request; + private final TestRequestMappingInfoHandlerMapping handlerMapping = new TestRequestMappingInfoHandlerMapping(); + + private final MockHttpServletRequest request = new MockHttpServletRequest(); @Rule public ExpectedException exception = ExpectedException.none(); @@ -65,11 +66,10 @@ public class CrossOriginTests { @Before public void setUp() { - this.handlerMapping = new TestRequestMappingInfoHandlerMapping(); this.handlerMapping.setRemoveSemicolonContent(false); this.handlerMapping.setApplicationContext(new StaticWebApplicationContext()); this.handlerMapping.afterPropertiesSet(); - this.request = new MockHttpServletRequest(); + this.request.setMethod("GET"); this.request.addHeader(HttpHeaders.ORIGIN, "http://domain.com/"); } @@ -304,8 +304,8 @@ public class CrossOriginTests { return "{}"; } - @CrossOrigin(origin = { "http://site1.com", "http://site2.com" }, allowedHeaders = { "header1", "header2" }, - exposedHeaders = { "header3", "header4" }, method = RequestMethod.DELETE, maxAge = 123, allowCredentials = "false") + @CrossOrigin(origins = { "http://site1.com", "http://site2.com" }, allowedHeaders = { "header1", "header2" }, + exposedHeaders = { "header3", "header4" }, methods = RequestMethod.DELETE, maxAge = 123, allowCredentials = "false") @RequestMapping(path = "/customized", method = { RequestMethod.GET, RequestMethod.POST }) public void customized() { }