AbstractController supports HTTP OPTIONS

Issue: SPR-13130
This commit is contained in:
Rossen Stoyanchev
2016-01-25 08:47:52 -05:00
parent a730e55d92
commit 135738f9bf
5 changed files with 38 additions and 9 deletions

View File

@@ -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");
* 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);

View File

@@ -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

View File

@@ -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;