"dispatchOptionsRequest" only sets the default 'Allow' header if actually needed (SPR-7837); "dispatchTraceRequest" only generates default response body if actually needed

This commit is contained in:
Juergen Hoeller
2011-12-12 15:30:04 +00:00
parent 450a3d7eee
commit d507c2b927

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2010 the original author or authors. * Copyright 2002-2011 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.
@@ -143,9 +143,9 @@ public abstract class FrameworkServlet extends HttpServletBean {
/** /**
* Any number of these characters are considered delimiters between * Any number of these characters are considered delimiters between
* multiple values in a single init-param String value. * multiple values in a single init-param String value.
* @see #initializeWebApplicationContext
*/ */
private String INIT_PARAM_DELIMITERS = ",; \t\n"; private static final String INIT_PARAM_DELIMITERS = ",; \t\n";
/** ServletContext attribute to find the WebApplicationContext in */ /** ServletContext attribute to find the WebApplicationContext in */
private String contextAttribute; private String contextAttribute;
@@ -188,7 +188,7 @@ public abstract class FrameworkServlet extends HttpServletBean {
/** Actual ApplicationContextInitializer instances to apply to the context */ /** Actual ApplicationContextInitializer instances to apply to the context */
private ArrayList<ApplicationContextInitializer<ConfigurableApplicationContext>> contextInitializers = private ArrayList<ApplicationContextInitializer<ConfigurableApplicationContext>> contextInitializers =
new ArrayList<ApplicationContextInitializer<ConfigurableApplicationContext>>(); new ArrayList<ApplicationContextInitializer<ConfigurableApplicationContext>>();
/** /**
@@ -408,8 +408,8 @@ public abstract class FrameworkServlet extends HttpServletBean {
* means that your controllers will receive those requests; make sure * means that your controllers will receive those requests; make sure
* that those endpoints are actually able to handle an OPTIONS request. * that those endpoints are actually able to handle an OPTIONS request.
* <p>Note that HttpServlet's default OPTIONS processing will be applied * <p>Note that HttpServlet's default OPTIONS processing will be applied
* in any case. Your controllers are simply available to override the * in any case if your controllers happen to not set the 'Allow' header
* default headers and optionally generate a response body. * (as required for an OPTIONS response).
*/ */
public void setDispatchOptionsRequest(boolean dispatchOptionsRequest) { public void setDispatchOptionsRequest(boolean dispatchOptionsRequest) {
this.dispatchOptionsRequest = dispatchOptionsRequest; this.dispatchOptionsRequest = dispatchOptionsRequest;
@@ -425,9 +425,8 @@ public abstract class FrameworkServlet extends HttpServletBean {
* means that your controllers will receive those requests; make sure * means that your controllers will receive those requests; make sure
* that those endpoints are actually able to handle a TRACE request. * that those endpoints are actually able to handle a TRACE request.
* <p>Note that HttpServlet's default TRACE processing will be applied * <p>Note that HttpServlet's default TRACE processing will be applied
* in any case. Your controllers are simply available to override the * in any case if your controllers happen to not generate a response
* default headers and the default body, calling <code>response.reset()</code> * of content type 'message/http' (as required for a TRACE response).
* if necessary.
*/ */
public void setDispatchTraceRequest(boolean dispatchTraceRequest) { public void setDispatchTraceRequest(boolean dispatchTraceRequest) {
this.dispatchTraceRequest = dispatchTraceRequest; this.dispatchTraceRequest = dispatchTraceRequest;
@@ -661,13 +660,15 @@ public abstract class FrameworkServlet extends HttpServletBean {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
protected void applyInitializers(ConfigurableApplicationContext wac) { protected void applyInitializers(ConfigurableApplicationContext wac) {
if (this.contextInitializerClasses != null) { if (this.contextInitializerClasses != null) {
String[] initializerClassNames = StringUtils.tokenizeToStringArray(this.contextInitializerClasses, INIT_PARAM_DELIMITERS); String[] initializerClassNames =
for(String initializerClassName : initializerClassNames) { StringUtils.tokenizeToStringArray(this.contextInitializerClasses, INIT_PARAM_DELIMITERS);
ApplicationContextInitializer<ConfigurableApplicationContext> initializer = null; for (String initializerClassName : initializerClassNames) {
ApplicationContextInitializer<ConfigurableApplicationContext> initializer;
try { try {
Class<?> initializerClass = ClassUtils.forName(initializerClassName, wac.getClassLoader()); Class<?> initializerClass = ClassUtils.forName(initializerClassName, wac.getClassLoader());
initializer = BeanUtils.instantiateClass(initializerClass, ApplicationContextInitializer.class); initializer = BeanUtils.instantiateClass(initializerClass, ApplicationContextInitializer.class);
} catch (Exception ex) { }
catch (Exception ex) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
String.format("Could not instantiate class [%s] specified via " + String.format("Could not instantiate class [%s] specified via " +
"'contextInitializerClasses' init-param", initializerClassName), ex); "'contextInitializerClasses' init-param", initializerClassName), ex);
@@ -675,9 +676,7 @@ public abstract class FrameworkServlet extends HttpServletBean {
this.contextInitializers.add(initializer); this.contextInitializers.add(initializer);
} }
} }
Collections.sort(this.contextInitializers, new AnnotationAwareOrderComparator()); Collections.sort(this.contextInitializers, new AnnotationAwareOrderComparator());
for (ApplicationContextInitializer<ConfigurableApplicationContext> initializer : this.contextInitializers) { for (ApplicationContextInitializer<ConfigurableApplicationContext> initializer : this.contextInitializers) {
initializer.initialize(wac); initializer.initialize(wac);
} }
@@ -814,32 +813,41 @@ public abstract class FrameworkServlet extends HttpServletBean {
/** /**
* Delegate OPTIONS requests to {@link #processRequest}, if desired. * Delegate OPTIONS requests to {@link #processRequest}, if desired.
* <p>Applies HttpServlet's standard OPTIONS processing first. * <p>Applies HttpServlet's standard OPTIONS processing otherwise,
* and also if there is still no 'Allow' header set after dispatching.
* @see #doService * @see #doService
*/ */
@Override @Override
protected void doOptions(HttpServletRequest request, HttpServletResponse response) protected void doOptions(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { throws ServletException, IOException {
super.doOptions(request, response);
if (this.dispatchOptionsRequest) { if (this.dispatchOptionsRequest) {
processRequest(request, response); processRequest(request, response);
if (response.containsHeader("Allow")) {
// Proper OPTIONS response coming from a handler - we're done.
return;
}
} }
super.doOptions(request, response);
} }
/** /**
* Delegate TRACE requests to {@link #processRequest}, if desired. * Delegate TRACE requests to {@link #processRequest}, if desired.
* <p>Applies HttpServlet's standard TRACE processing first. * <p>Applies HttpServlet's standard TRACE processing otherwise.
* @see #doService * @see #doService
*/ */
@Override @Override
protected void doTrace(HttpServletRequest request, HttpServletResponse response) protected void doTrace(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { throws ServletException, IOException {
super.doTrace(request, response);
if (this.dispatchTraceRequest) { if (this.dispatchTraceRequest) {
processRequest(request, response); processRequest(request, response);
if ("message/http".equals(response.getContentType())) {
// Proper TRACE response coming from a handler - we're done.
return;
}
} }
super.doTrace(request, response);
} }