Polish @CrossOrigin

- origin --> origins
- method --> methods
- constants are now actually constant (i.e., static final)
This commit is contained in:
Sam Brannen 2015-06-13 17:09:58 +02:00
parent 60cdfa535e
commit 2b339db53b
3 changed files with 16 additions and 19 deletions

View File

@ -39,19 +39,19 @@ import org.springframework.core.annotation.AliasFor;
@Documented @Documented
public @interface CrossOrigin { 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 {}; String[] value() default {};
/** /**
@ -63,7 +63,7 @@ public @interface CrossOrigin {
* @see #value * @see #value
*/ */
@AliasFor(attribute = "value") @AliasFor(attribute = "value")
String[] origin() default {}; String[] origins() default {};
/** /**
* List of request headers that can be used during the actual request. * List of request headers that can be used during the actual request.
@ -88,7 +88,7 @@ public @interface CrossOrigin {
* <p>If undefined, methods defined by {@link RequestMapping} annotation * <p>If undefined, methods defined by {@link RequestMapping} annotation
* are used. * are used.
*/ */
RequestMethod[] method() default {}; RequestMethod[] methods() default {};
/** /**
* Whether the browser should include any cookies associated with the * Whether the browser should include any cookies associated with the

View File

@ -36,7 +36,6 @@ import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.method.HandlerMethod; import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.condition.AbstractRequestCondition; import org.springframework.web.servlet.mvc.condition.AbstractRequestCondition;
import org.springframework.web.servlet.mvc.condition.CompositeRequestCondition; 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.condition.RequestCondition;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo; import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping; 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 * @param handlerType the handler type for which to create the condition
* @return the condition, or {@code null} * @return the condition, or {@code null}
*/ */
@SuppressWarnings("unused")
protected RequestCondition<?> getCustomTypeCondition(Class<?> handlerType) { protected RequestCondition<?> getCustomTypeCondition(Class<?> handlerType) {
return null; return null;
} }
@ -244,7 +242,6 @@ public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMappi
* @param method the handler method for which to create the condition * @param method the handler method for which to create the condition
* @return the condition, or {@code null} * @return the condition, or {@code null}
*/ */
@SuppressWarnings("unused")
protected RequestCondition<?> getCustomMethodCondition(Method method) { protected RequestCondition<?> getCustomMethodCondition(Method method) {
return null; return null;
} }
@ -326,10 +323,10 @@ public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMappi
if (annotation == null) { if (annotation == null) {
return; return;
} }
for (String origin : annotation.origin()) { for (String origin : annotation.origins()) {
config.addAllowedOrigin(origin); config.addAllowedOrigin(origin);
} }
for (RequestMethod method : annotation.method()) { for (RequestMethod method : annotation.methods()) {
config.addAllowedMethod(method.name()); config.addAllowedMethod(method.name());
} }
for (String header : annotation.allowedHeaders()) { for (String header : annotation.allowedHeaders()) {

View File

@ -56,8 +56,9 @@ import static org.junit.Assert.*;
*/ */
public class CrossOriginTests { public class CrossOriginTests {
private TestRequestMappingInfoHandlerMapping handlerMapping; private final TestRequestMappingInfoHandlerMapping handlerMapping = new TestRequestMappingInfoHandlerMapping();
private MockHttpServletRequest request;
private final MockHttpServletRequest request = new MockHttpServletRequest();
@Rule @Rule
public ExpectedException exception = ExpectedException.none(); public ExpectedException exception = ExpectedException.none();
@ -65,11 +66,10 @@ public class CrossOriginTests {
@Before @Before
public void setUp() { public void setUp() {
this.handlerMapping = new TestRequestMappingInfoHandlerMapping();
this.handlerMapping.setRemoveSemicolonContent(false); this.handlerMapping.setRemoveSemicolonContent(false);
this.handlerMapping.setApplicationContext(new StaticWebApplicationContext()); this.handlerMapping.setApplicationContext(new StaticWebApplicationContext());
this.handlerMapping.afterPropertiesSet(); this.handlerMapping.afterPropertiesSet();
this.request = new MockHttpServletRequest();
this.request.setMethod("GET"); this.request.setMethod("GET");
this.request.addHeader(HttpHeaders.ORIGIN, "http://domain.com/"); this.request.addHeader(HttpHeaders.ORIGIN, "http://domain.com/");
} }
@ -304,8 +304,8 @@ public class CrossOriginTests {
return "{}"; return "{}";
} }
@CrossOrigin(origin = { "http://site1.com", "http://site2.com" }, allowedHeaders = { "header1", "header2" }, @CrossOrigin(origins = { "http://site1.com", "http://site2.com" }, allowedHeaders = { "header1", "header2" },
exposedHeaders = { "header3", "header4" }, method = RequestMethod.DELETE, maxAge = 123, allowCredentials = "false") exposedHeaders = { "header3", "header4" }, methods = RequestMethod.DELETE, maxAge = 123, allowCredentials = "false")
@RequestMapping(path = "/customized", method = { RequestMethod.GET, RequestMethod.POST }) @RequestMapping(path = "/customized", method = { RequestMethod.GET, RequestMethod.POST })
public void customized() { public void customized() {
} }