提交 135738f9 编写于 作者: R Rossen Stoyanchev

AbstractController supports HTTP OPTIONS

Issue: SPR-13130
上级 a730e55d
/*
* 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");
* 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 Boolean dispatchOptions = Boolean.FALSE;
private Boolean dispatchOptions = Boolean.TRUE;
private final List<MockMvcConfigurer> configurers = new ArrayList<MockMvcConfigurer>(4);
......
/*
* 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");
* you may not use this file except in compliance with the License.
......@@ -16,10 +16,15 @@
package org.springframework.web.servlet.mvc;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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.support.WebContentGenerator;
import org.springframework.web.util.WebUtils;
......@@ -87,6 +92,7 @@ import org.springframework.web.util.WebUtils;
*
* @author Rod Johnson
* @author Juergen Hoeller
* @author Rossen Stoyanchev
* @see WebContentInterceptor
*/
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)
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.
checkRequest(request);
prepareResponse(response);
......
......@@ -19,6 +19,7 @@ package org.springframework.web.servlet.mvc;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;
......@@ -43,6 +44,11 @@ public class ParameterizableViewController extends AbstractController {
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
* DispatcherServlet via a ViewResolver. Will override any pre-existing
......
......@@ -17,10 +17,9 @@
package org.springframework.web.servlet.support;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
......@@ -111,7 +110,7 @@ public abstract class WebContentGenerator extends WebApplicationObjectSupport {
*/
public WebContentGenerator(boolean restrictDefaultSupportedMethods) {
if (restrictDefaultSupportedMethods) {
this.supportedMethods = new HashSet<String>(4);
this.supportedMethods = new LinkedHashSet<String>(4);
this.supportedMethods.add(METHOD_GET);
this.supportedMethods.add(METHOD_HEAD);
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
*/
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) {
if (methods != null) {
this.supportedMethods = new HashSet<String>(Arrays.asList(methods));
this.supportedMethods = new LinkedHashSet<String>(Arrays.asList(methods));
}
else {
this.supportedMethods = null;
......
/*
* 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");
* 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.Test;
import org.springframework.http.HttpMethod;
import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.mock.web.test.MockHttpServletResponse;
import org.springframework.ui.ModelMap;
......@@ -69,4 +70,14 @@ public class ParameterizableViewControllerTests {
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"));
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册