/
AbstractDispatcherHandlerInitializer.java
226 lines (193 loc) · 8.71 KB
/
AbstractDispatcherHandlerInitializer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.reactive.support;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.http.server.reactive.ServletHttpHandlerAdapter;
import org.springframework.util.Assert;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.multipart.reactive.MultipartResolver;
import org.springframework.web.reactive.DispatcherHandler;
import org.springframework.web.server.WebHandler;
import org.springframework.web.server.adapter.HttpWebHandlerAdapter;
/**
* Base class for {@link org.springframework.web.WebApplicationInitializer}
* implementations that register a {@link DispatcherHandler} in the servlet context, wrapping it in
* a {@link ServletHttpHandlerAdapter}.
*
* <p>Concrete implementations are required to implement
* {@link #createApplicationContext()}, which gets invoked from
* {@link #registerDispatcherHandler(ServletContext)}. Further customization can be achieved by
* overriding {@link #customizeRegistration(ServletRegistration.Dynamic)}.
*
* @author Arjen Poutsma
* @since 5.0
*/
public abstract class AbstractDispatcherHandlerInitializer implements WebApplicationInitializer {
/**
* The default servlet name. Can be customized by overriding {@link #getServletName}.
*/
public static final String DEFAULT_SERVLET_NAME = "dispatcher-handler";
/**
* The default servlet mapping. Can be customized by overriding {@link #getServletMapping()}.
*/
public static final String DEFAULT_SERVLET_MAPPING = "/";
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
registerDispatcherHandler(servletContext);
}
/**
* Register a {@link DispatcherHandler} against the given servlet context.
* <p>This method will create a {@link DispatcherHandler}, initializing it with the application
* context returned from {@link #createApplicationContext()}. The created handler will be
* wrapped in a {@link ServletHttpHandlerAdapter} servlet with the name
* returned by {@link #getServletName()}, mapping it to the pattern
* returned from {@link #getServletMapping()}.
* <p>Further customization can be achieved by overriding {@link
* #customizeRegistration(ServletRegistration.Dynamic)} or
* {@link #createDispatcherHandler(ApplicationContext)}.
* @param servletContext the context to register the servlet against
*/
protected void registerDispatcherHandler(ServletContext servletContext) {
String servletName = getServletName();
Assert.hasLength(servletName, "getServletName() must not return empty or null");
ApplicationContext applicationContext = createApplicationContext();
Assert.notNull(applicationContext,
"createApplicationContext() did not return an application " +
"context for servlet [" + servletName + "]");
refreshApplicationContext(applicationContext);
registerCloseListener(servletContext, applicationContext);
WebHandler dispatcherHandler = createDispatcherHandler(applicationContext);
Assert.notNull(dispatcherHandler,
"createDispatcherHandler() did not return a WebHandler for servlet [" + servletName + "]");
MultipartResolver multipartResolver = createMultipartResolver(applicationContext);
ServletHttpHandlerAdapter handlerAdapter = createHandlerAdapter(dispatcherHandler, multipartResolver);
Assert.notNull(handlerAdapter,
"createHttpHandler() did not return a ServletHttpHandlerAdapter for servlet [" + servletName + "]");
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.");
registration.setLoadOnStartup(1);
registration.addMapping(getServletMapping());
registration.setAsyncSupported(true);
customizeRegistration(registration);
}
/**
* Return the name under which the {@link ServletHttpHandlerAdapter} will be registered.
* Defaults to {@link #DEFAULT_SERVLET_NAME}.
* @see #registerDispatcherHandler(ServletContext)
*/
protected String getServletName() {
return DEFAULT_SERVLET_NAME;
}
/**
* Create an application context to be provided to the {@code DispatcherHandler}.
* <p>The returned context is delegated to Spring's
* {@link DispatcherHandler#DispatcherHandler(ApplicationContext)}. As such,
* it typically contains controllers, view resolvers, and other web-related beans.
* @see #registerDispatcherHandler(ServletContext)
*/
protected abstract ApplicationContext createApplicationContext();
/**
* Refresh the given application context, if necessary.
*/
protected void refreshApplicationContext(ApplicationContext context) {
if (context instanceof ConfigurableApplicationContext) {
ConfigurableApplicationContext cac = (ConfigurableApplicationContext) context;
if (!cac.isActive()) {
cac.refresh();
}
}
}
/**
* Create a {@link DispatcherHandler} (or other kind of {@link WebHandler}-derived
* dispatcher) with the specified {@link ApplicationContext}.
*/
protected WebHandler createDispatcherHandler(ApplicationContext applicationContext) {
return new DispatcherHandler(applicationContext);
}
/**
* Create a {@link MultipartResolver} with the specified {@link ApplicationContext}.
*/
protected MultipartResolver createMultipartResolver(ApplicationContext applicationContext) {
try {
return applicationContext.getBean(MultipartResolver.class);
}
catch (NoSuchBeanDefinitionException nsbe) {
}
return null;
}
/**
* Create a {@link ServletHttpHandlerAdapter}.
* <p>Default implementation returns a {@code ServletHttpHandlerAdapter} with the provided
* {@code webHandler}.
*/
protected ServletHttpHandlerAdapter createHandlerAdapter(WebHandler webHandler, MultipartResolver multipartResolver) {
HttpHandler httpHandler = new HttpWebHandlerAdapter(webHandler);
return new ServletHttpHandlerAdapter(httpHandler, multipartResolver);
}
/**
* Specify the servlet mapping for the {@code ServletHttpHandlerAdapter}.
* <p>Default implementation returns {@code /}.
* @see #registerDispatcherHandler(ServletContext)
*/
protected String getServletMapping() {
return DEFAULT_SERVLET_MAPPING;
}
/**
* Optionally perform further registration customization once
* {@link #registerDispatcherHandler(ServletContext)} has completed.
* @param registration the {@code ServletHttpHandlerAdapter} registration to be customized
* @see #registerDispatcherHandler(ServletContext)
*/
protected void customizeRegistration(ServletRegistration.Dynamic registration) {
}
/**
* Register a {@link ServletContextListener} that closes the given application context
* when the servlet context is destroyed.
* @param servletContext the servlet context to listen to
* @param applicationContext the application context that is to be closed when
* {@code servletContext} is destroyed
*/
protected void registerCloseListener(ServletContext servletContext, ApplicationContext applicationContext) {
if (applicationContext instanceof ConfigurableApplicationContext) {
ConfigurableApplicationContext context = (ConfigurableApplicationContext) applicationContext;
ServletContextDestroyedListener listener = new ServletContextDestroyedListener(context);
servletContext.addListener(listener);
}
}
private static class ServletContextDestroyedListener implements ServletContextListener {
private final ConfigurableApplicationContext applicationContext;
public ServletContextDestroyedListener(ConfigurableApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
@Override
public void contextInitialized(ServletContextEvent sce) {
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
this.applicationContext.close();
}
}
}