Consistent assertions for template method result vs servlet registration
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2018 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.
|
||||
@@ -54,10 +54,10 @@ public abstract class AbstractReactiveWebInitializer implements WebApplicationIn
|
||||
@Override
|
||||
public void onStartup(ServletContext servletContext) throws ServletException {
|
||||
String servletName = getServletName();
|
||||
Assert.hasLength(servletName, "getServletName() must not return empty or null");
|
||||
Assert.hasLength(servletName, "getServletName() must not return null or empty");
|
||||
|
||||
ApplicationContext applicationContext = createApplicationContext();
|
||||
Assert.notNull(applicationContext, "createApplicationContext() must not return null.");
|
||||
Assert.notNull(applicationContext, "createApplicationContext() must not return null");
|
||||
|
||||
refreshApplicationContext(applicationContext);
|
||||
registerCloseListener(servletContext, applicationContext);
|
||||
@@ -66,7 +66,10 @@ public abstract class AbstractReactiveWebInitializer implements WebApplicationIn
|
||||
ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler);
|
||||
|
||||
ServletRegistration.Dynamic registration = servletContext.addServlet(servletName, servlet);
|
||||
Assert.notNull(registration, "Failed to register servlet '" + servletName + "'.");
|
||||
if (registration == null) {
|
||||
throw new IllegalStateException("Failed to register servlet with name '" + servletName + "'. " +
|
||||
"Check if there is another servlet registered under the same name.");
|
||||
}
|
||||
|
||||
registration.setLoadOnStartup(1);
|
||||
registration.addMapping(getServletMapping());
|
||||
@@ -88,7 +91,7 @@ public abstract class AbstractReactiveWebInitializer implements WebApplicationIn
|
||||
protected ApplicationContext createApplicationContext() {
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
Class<?>[] configClasses = getConfigClasses();
|
||||
Assert.notEmpty(configClasses, "No Spring configuration provided.");
|
||||
Assert.notEmpty(configClasses, "No Spring configuration provided through getConfigClasses()");
|
||||
context.register(configClasses);
|
||||
return context;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2018 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.
|
||||
@@ -39,9 +39,8 @@ import org.springframework.web.server.adapter.HttpWebHandlerAdapter;
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @since 5.0
|
||||
* @deprecated in favor of
|
||||
* {@link org.springframework.web.server.adapter.AbstractReactiveWebInitializer
|
||||
* AbstractReactiveWebInitializer}
|
||||
* @deprecated as of 5.0.2, in favor of
|
||||
* {@link org.springframework.web.server.adapter.AbstractReactiveWebInitializer}
|
||||
*/
|
||||
@Deprecated
|
||||
public abstract class AbstractDispatcherHandlerInitializer implements WebApplicationInitializer {
|
||||
@@ -76,28 +75,25 @@ public abstract class AbstractDispatcherHandlerInitializer implements WebApplica
|
||||
*/
|
||||
protected void registerDispatcherHandler(ServletContext servletContext) {
|
||||
String servletName = getServletName();
|
||||
Assert.hasLength(servletName, "getServletName() must not return empty or null");
|
||||
Assert.hasLength(servletName, "getServletName() must not return null or empty");
|
||||
|
||||
ApplicationContext applicationContext = createApplicationContext();
|
||||
Assert.notNull(applicationContext,
|
||||
"createApplicationContext() did not return an application " +
|
||||
"context for servlet [" + servletName + "]");
|
||||
Assert.notNull(applicationContext, "createApplicationContext() must not return null");
|
||||
|
||||
refreshApplicationContext(applicationContext);
|
||||
registerCloseListener(servletContext, applicationContext);
|
||||
|
||||
WebHandler dispatcherHandler = createDispatcherHandler(applicationContext);
|
||||
Assert.notNull(dispatcherHandler,
|
||||
"createDispatcherHandler() did not return a WebHandler for servlet [" + servletName + "]");
|
||||
Assert.notNull(dispatcherHandler, "createDispatcherHandler(ApplicationContext) must not return null");
|
||||
|
||||
ServletHttpHandlerAdapter handlerAdapter = createHandlerAdapter(dispatcherHandler);
|
||||
Assert.notNull(handlerAdapter,
|
||||
"createHttpHandler() did not return a ServletHttpHandlerAdapter for servlet [" + servletName + "]");
|
||||
Assert.notNull(handlerAdapter, "createHandlerAdapter(WebHandler) must not return null");
|
||||
|
||||
ServletRegistration.Dynamic registration = servletContext.addServlet(servletName, handlerAdapter);
|
||||
Assert.notNull(registration,
|
||||
"Failed to register servlet with name '" + servletName + "'." +
|
||||
"Check if there is another servlet registered under the same name.");
|
||||
if (registration == null) {
|
||||
throw new IllegalStateException("Failed to register servlet with name '" + servletName + "'. " +
|
||||
"Check if there is another servlet registered under the same name.");
|
||||
}
|
||||
|
||||
registration.setLoadOnStartup(1);
|
||||
registration.addMapping(getServletMapping());
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2018 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.
|
||||
@@ -53,30 +53,29 @@ public abstract class AbstractServletHttpHandlerAdapterInitializer implements We
|
||||
/**
|
||||
* Register a {@link ServletHttpHandlerAdapter} against the given servlet context.
|
||||
* <p>This method will create a {@code HttpHandler} using {@link #createHttpHandler()},
|
||||
* and use it to create a {@code ServletHttpHandlerAdapter} with the name returned by
|
||||
* {@link #getServletName()}, and mapping it to the patterns
|
||||
* returned from {@link #getServletMappings()}.
|
||||
* <p>Further customization can be achieved by overriding {@link
|
||||
* #customizeRegistration(ServletRegistration.Dynamic)} or
|
||||
* and use it to create a {@code ServletHttpHandlerAdapter} with the name returned
|
||||
* by {@link #getServletName()}, and mapping it to the patterns returned from
|
||||
* {@link #getServletMappings()}.
|
||||
* <p>Further customization can be achieved by overriding
|
||||
* {@link #customizeRegistration(ServletRegistration.Dynamic)} or
|
||||
* {@link #createServlet(HttpHandler)}.
|
||||
* @param servletContext the context to register the servlet against
|
||||
* @param servletContext the context to register the servlet with
|
||||
*/
|
||||
protected void registerHandlerAdapter(ServletContext servletContext) {
|
||||
String servletName = getServletName();
|
||||
Assert.hasLength(servletName, "getServletName() must not return empty or null");
|
||||
Assert.hasLength(servletName, "getServletName() must not return null or empty");
|
||||
|
||||
HttpHandler httpHandler = createHttpHandler();
|
||||
Assert.notNull(httpHandler, "createHttpHandler() did not return a HttpHandler" +
|
||||
"for servlet [" + servletName + "]");
|
||||
Assert.notNull(httpHandler, "createHttpHandler() must not return null");
|
||||
|
||||
ServletHttpHandlerAdapter servlet = createServlet(httpHandler);
|
||||
Assert.notNull(servlet, "createHttpHandler() did not return a ServletHttpHandlerAdapter " +
|
||||
"for servlet [" + servletName + "]");
|
||||
Assert.notNull(servlet, "createServlet(HttpHandler) must not return null");
|
||||
|
||||
ServletRegistration.Dynamic registration = servletContext.addServlet(servletName, servlet);
|
||||
Assert.notNull(registration,
|
||||
"Failed to register servlet with name '" + servletName + "'." +
|
||||
"Check if there is another servlet registered under the same name.");
|
||||
if (registration == null) {
|
||||
throw new IllegalStateException("Failed to register servlet with name '" + servletName + "'. " +
|
||||
"Check if there is another servlet registered under the same name.");
|
||||
}
|
||||
|
||||
registration.setLoadOnStartup(1);
|
||||
registration.addMapping(getServletMappings());
|
||||
@@ -101,7 +100,7 @@ public abstract class AbstractServletHttpHandlerAdapterInitializer implements We
|
||||
|
||||
/**
|
||||
* Create a {@link ServletHttpHandlerAdapter} with the specified .
|
||||
* <p>Default implementation returns a {@code ServletHttpHandlerAdapter}
|
||||
* <p>The default implementation returns a {@code ServletHttpHandlerAdapter}
|
||||
* with the provided {@code httpHandler}.
|
||||
*/
|
||||
protected ServletHttpHandlerAdapter createServlet(HttpHandler httpHandler) {
|
||||
@@ -109,7 +108,7 @@ public abstract class AbstractServletHttpHandlerAdapterInitializer implements We
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify the servlet mapping(s) for the {@code ServletHttpHandlerAdapter} —
|
||||
* Specify the servlet mapping(s) for the {@code ServletHttpHandlerAdapter}:
|
||||
* for example {@code "/"}, {@code "/app"}, etc.
|
||||
* @see #registerHandlerAdapter(ServletContext)
|
||||
*/
|
||||
@@ -123,4 +122,5 @@ public abstract class AbstractServletHttpHandlerAdapterInitializer implements We
|
||||
*/
|
||||
protected void customizeRegistration(ServletRegistration.Dynamic registration) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2018 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.
|
||||
@@ -17,7 +17,6 @@
|
||||
package org.springframework.web.servlet.support;
|
||||
|
||||
import java.util.EnumSet;
|
||||
|
||||
import javax.servlet.DispatcherType;
|
||||
import javax.servlet.Filter;
|
||||
import javax.servlet.FilterRegistration;
|
||||
@@ -40,7 +39,7 @@ import org.springframework.web.servlet.FrameworkServlet;
|
||||
* Base class for {@link org.springframework.web.WebApplicationInitializer}
|
||||
* implementations that register a {@link DispatcherServlet} in the servlet context.
|
||||
*
|
||||
* <p>Most applications should consider extending the Spring Java config, sub-class
|
||||
* <p>Most applications should consider extending the Spring Java config subclass
|
||||
* {@link AbstractAnnotationConfigDispatcherServletInitializer}.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
@@ -77,20 +76,20 @@ public abstract class AbstractDispatcherServletInitializer extends AbstractConte
|
||||
*/
|
||||
protected void registerDispatcherServlet(ServletContext servletContext) {
|
||||
String servletName = getServletName();
|
||||
Assert.hasLength(servletName, "getServletName() must not return empty or null");
|
||||
Assert.hasLength(servletName, "getServletName() must not return null or empty");
|
||||
|
||||
WebApplicationContext servletAppContext = createServletApplicationContext();
|
||||
Assert.notNull(servletAppContext,
|
||||
"createServletApplicationContext() did not return an application " +
|
||||
"context for servlet [" + servletName + "]");
|
||||
Assert.notNull(servletAppContext, "createServletApplicationContext() must not return null");
|
||||
|
||||
FrameworkServlet dispatcherServlet = createDispatcherServlet(servletAppContext);
|
||||
Assert.notNull(dispatcherServlet, "createDispatcherServlet(WebApplicationContext) must not return null");
|
||||
dispatcherServlet.setContextInitializers(getServletApplicationContextInitializers());
|
||||
|
||||
ServletRegistration.Dynamic registration = servletContext.addServlet(servletName, dispatcherServlet);
|
||||
Assert.notNull(registration,
|
||||
"Failed to register servlet with name '" + servletName + "'." +
|
||||
"Check if there is another servlet registered under the same name.");
|
||||
if (registration == null) {
|
||||
throw new IllegalStateException("Failed to register servlet with name '" + servletName + "'. " +
|
||||
"Check if there is another servlet registered under the same name.");
|
||||
}
|
||||
|
||||
registration.setLoadOnStartup(1);
|
||||
registration.addMapping(getServletMappings());
|
||||
@@ -185,16 +184,19 @@ public abstract class AbstractDispatcherServletInitializer extends AbstractConte
|
||||
protected FilterRegistration.Dynamic registerServletFilter(ServletContext servletContext, Filter filter) {
|
||||
String filterName = Conventions.getVariableName(filter);
|
||||
Dynamic registration = servletContext.addFilter(filterName, filter);
|
||||
|
||||
if (registration == null) {
|
||||
int counter = -1;
|
||||
while (counter == -1 || registration == null) {
|
||||
counter++;
|
||||
int counter = 0;
|
||||
while (registration == null) {
|
||||
if (counter == 100) {
|
||||
throw new IllegalStateException("Failed to register filter with name '" + filterName + "'. " +
|
||||
"Check if there is another filter registered under the same name.");
|
||||
}
|
||||
registration = servletContext.addFilter(filterName + "#" + counter, filter);
|
||||
Assert.isTrue(counter < 100,
|
||||
"Failed to register filter '" + filter + "'." +
|
||||
"Could the same Filter instance have been registered already?");
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
|
||||
registration.setAsyncSupported(isAsyncSupported());
|
||||
registration.addMappingForServletNames(getDispatcherTypes(), false, getServletName());
|
||||
return registration;
|
||||
|
||||
Reference in New Issue
Block a user