AbstractController supports HTTP OPTIONS
Issue: SPR-13130
This commit is contained in:
parent
a730e55d92
commit
135738f9bf
|
|
@ -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.
|
||||||
|
|
@ -55,7 +55,7 @@ public abstract class AbstractMockMvcBuilder<B extends AbstractMockMvcBuilder<B>
|
||||||
|
|
||||||
private final List<ResultHandler> globalResultHandlers = new ArrayList<ResultHandler>();
|
private final List<ResultHandler> globalResultHandlers = new ArrayList<ResultHandler>();
|
||||||
|
|
||||||
private Boolean dispatchOptions = Boolean.FALSE;
|
private Boolean dispatchOptions = Boolean.TRUE;
|
||||||
|
|
||||||
private final List<MockMvcConfigurer> configurers = new ArrayList<MockMvcConfigurer>(4);
|
private final List<MockMvcConfigurer> configurers = new ArrayList<MockMvcConfigurer>(4);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2015 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.
|
||||||
|
|
@ -16,10 +16,15 @@
|
||||||
|
|
||||||
package org.springframework.web.servlet.mvc;
|
package org.springframework.web.servlet.mvc;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import javax.servlet.http.HttpSession;
|
import javax.servlet.http.HttpSession;
|
||||||
|
|
||||||
|
import org.springframework.http.HttpMethod;
|
||||||
|
import org.springframework.util.ObjectUtils;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
import org.springframework.web.servlet.ModelAndView;
|
import org.springframework.web.servlet.ModelAndView;
|
||||||
import org.springframework.web.servlet.support.WebContentGenerator;
|
import org.springframework.web.servlet.support.WebContentGenerator;
|
||||||
import org.springframework.web.util.WebUtils;
|
import org.springframework.web.util.WebUtils;
|
||||||
|
|
@ -87,6 +92,7 @@ import org.springframework.web.util.WebUtils;
|
||||||
*
|
*
|
||||||
* @author Rod Johnson
|
* @author Rod Johnson
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
|
* @author Rossen Stoyanchev
|
||||||
* @see WebContentInterceptor
|
* @see WebContentInterceptor
|
||||||
*/
|
*/
|
||||||
public abstract class AbstractController extends WebContentGenerator implements Controller {
|
public abstract class AbstractController extends WebContentGenerator implements Controller {
|
||||||
|
|
@ -149,6 +155,13 @@ public abstract class AbstractController extends WebContentGenerator implements
|
||||||
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
|
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
|
||||||
throws Exception {
|
throws Exception {
|
||||||
|
|
||||||
|
String[] supportedMethods = getSupportedMethods();
|
||||||
|
if (HttpMethod.OPTIONS.matches(request.getMethod()) && !ObjectUtils.isEmpty(supportedMethods)) {
|
||||||
|
List<String> value = Arrays.asList(supportedMethods);
|
||||||
|
response.setHeader("Allow", StringUtils.collectionToCommaDelimitedString(value));
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
// Delegate to WebContentGenerator for checking and preparing.
|
// Delegate to WebContentGenerator for checking and preparing.
|
||||||
checkRequest(request);
|
checkRequest(request);
|
||||||
prepareResponse(response);
|
prepareResponse(response);
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ package org.springframework.web.servlet.mvc;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
import org.springframework.http.HttpMethod;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.web.servlet.ModelAndView;
|
import org.springframework.web.servlet.ModelAndView;
|
||||||
import org.springframework.web.servlet.View;
|
import org.springframework.web.servlet.View;
|
||||||
|
|
@ -43,6 +44,11 @@ public class ParameterizableViewController extends AbstractController {
|
||||||
private boolean statusOnly;
|
private boolean statusOnly;
|
||||||
|
|
||||||
|
|
||||||
|
public ParameterizableViewController() {
|
||||||
|
super(false);
|
||||||
|
setSupportedMethods(HttpMethod.GET.name(), HttpMethod.HEAD.name());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set a view name for the ModelAndView to return, to be resolved by the
|
* Set a view name for the ModelAndView to return, to be resolved by the
|
||||||
* DispatcherServlet via a ViewResolver. Will override any pre-existing
|
* DispatcherServlet via a ViewResolver. Will override any pre-existing
|
||||||
|
|
|
||||||
|
|
@ -17,10 +17,9 @@
|
||||||
package org.springframework.web.servlet.support;
|
package org.springframework.web.servlet.support;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.HashSet;
|
import java.util.LinkedHashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import javax.servlet.ServletException;
|
import javax.servlet.ServletException;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
@ -111,7 +110,7 @@ public abstract class WebContentGenerator extends WebApplicationObjectSupport {
|
||||||
*/
|
*/
|
||||||
public WebContentGenerator(boolean restrictDefaultSupportedMethods) {
|
public WebContentGenerator(boolean restrictDefaultSupportedMethods) {
|
||||||
if (restrictDefaultSupportedMethods) {
|
if (restrictDefaultSupportedMethods) {
|
||||||
this.supportedMethods = new HashSet<String>(4);
|
this.supportedMethods = new LinkedHashSet<String>(4);
|
||||||
this.supportedMethods.add(METHOD_GET);
|
this.supportedMethods.add(METHOD_GET);
|
||||||
this.supportedMethods.add(METHOD_HEAD);
|
this.supportedMethods.add(METHOD_HEAD);
|
||||||
this.supportedMethods.add(METHOD_POST);
|
this.supportedMethods.add(METHOD_POST);
|
||||||
|
|
@ -123,7 +122,7 @@ public abstract class WebContentGenerator extends WebApplicationObjectSupport {
|
||||||
* @param supportedMethods the supported HTTP methods for this content generator
|
* @param supportedMethods the supported HTTP methods for this content generator
|
||||||
*/
|
*/
|
||||||
public WebContentGenerator(String... supportedMethods) {
|
public WebContentGenerator(String... supportedMethods) {
|
||||||
this.supportedMethods = new HashSet<String>(Arrays.asList(supportedMethods));
|
this.supportedMethods = new LinkedHashSet<String>(Arrays.asList(supportedMethods));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -134,7 +133,7 @@ public abstract class WebContentGenerator extends WebApplicationObjectSupport {
|
||||||
*/
|
*/
|
||||||
public final void setSupportedMethods(String... methods) {
|
public final void setSupportedMethods(String... methods) {
|
||||||
if (methods != null) {
|
if (methods != null) {
|
||||||
this.supportedMethods = new HashSet<String>(Arrays.asList(methods));
|
this.supportedMethods = new LinkedHashSet<String>(Arrays.asList(methods));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
this.supportedMethods = null;
|
this.supportedMethods = null;
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2012 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.
|
||||||
|
|
@ -19,6 +19,7 @@ package org.springframework.web.servlet.mvc;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import org.springframework.http.HttpMethod;
|
||||||
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.ui.ModelMap;
|
import org.springframework.ui.ModelMap;
|
||||||
|
|
@ -69,4 +70,14 @@ public class ParameterizableViewControllerTests {
|
||||||
assertEquals("value", mav.getModel().get("name"));
|
assertEquals("value", mav.getModel().get("name"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void handleRequestHttpOptions() throws Exception {
|
||||||
|
this.request.setMethod(HttpMethod.OPTIONS.name());
|
||||||
|
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||||
|
ModelAndView mav = this.controller.handleRequest(this.request, response);
|
||||||
|
|
||||||
|
assertNull(mav);
|
||||||
|
assertEquals("GET,HEAD", response.getHeader("Allow"));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue