WebHttpHandlerBuilder.java 11.3 KB
Newer Older
1
/*
2
 * Copyright 2002-2018 the original author or authors.
3 4 5 6 7 8 9 10 11 12 13 14 15
 *
 * 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.
 */
16

17 18 19
package org.springframework.web.server.adapter;

import java.util.ArrayList;
20
import java.util.Arrays;
21
import java.util.Collections;
22
import java.util.List;
23
import java.util.function.Consumer;
24

25
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
26
import org.springframework.beans.factory.annotation.Autowired;
27 28
import org.springframework.context.ApplicationContext;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
29
import org.springframework.http.codec.ServerCodecConfigurer;
30
import org.springframework.http.server.reactive.HttpHandler;
31
import org.springframework.lang.Nullable;
32 33 34 35 36 37 38 39
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebExceptionHandler;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebHandler;
import org.springframework.web.server.handler.ExceptionHandlingWebHandler;
import org.springframework.web.server.handler.FilteringWebHandler;
40
import org.springframework.web.server.i18n.LocaleContextResolver;
41
import org.springframework.web.server.session.DefaultWebSessionManager;
42 43 44
import org.springframework.web.server.session.WebSessionManager;

/**
J
Juergen Hoeller 已提交
45
 * This builder has two purposes:
46
 *
J
Juergen Hoeller 已提交
47 48 49
 * <p>One is to assemble a processing chain that consists of a target {@link WebHandler},
 * then decorated with a set of {@link WebFilter WebFilters}, then further decorated with
 * a set of {@link WebExceptionHandler WebExceptionHandlers}.
50
 *
J
Juergen Hoeller 已提交
51 52 53
 * <p>The second purpose is to adapt the resulting processing chain to an {@link HttpHandler}:
 * the lowest-level reactive HTTP handling abstraction which can then be used with any of the
 * supported runtimes. The adaptation is done with the help of {@link HttpWebHandlerAdapter}.
54
 *
J
Juergen Hoeller 已提交
55 56
 * <p>The processing chain can be assembled manually via builder methods, or detected from
 * a Spring {@link ApplicationContext} via {@link #applicationContext}, or a mix of both.
57 58
 *
 * @author Rossen Stoyanchev
59
 * @author Sebastien Deleuze
60
 * @since 5.0
61
 * @see HttpWebHandlerAdapter
62
 */
P
Phillip Webb 已提交
63
public final class WebHttpHandlerBuilder {
64

65 66 67 68 69 70
	/** Well-known name for the target WebHandler in the bean factory. */
	public static final String WEB_HANDLER_BEAN_NAME = "webHandler";

	/** Well-known name for the WebSessionManager in the bean factory. */
	public static final String WEB_SESSION_MANAGER_BEAN_NAME = "webSessionManager";

71 72 73
	/** Well-known name for the ServerCodecConfigurer in the bean factory. */
	public static final String SERVER_CODEC_CONFIGURER_BEAN_NAME = "serverCodecConfigurer";

74 75 76
	/** Well-known name for the LocaleContextResolver in the bean factory. */
	public static final String LOCALE_CONTEXT_RESOLVER_BEAN_NAME = "localeContextResolver";

77

R
Rossen Stoyanchev 已提交
78
	private final WebHandler webHandler;
79

80 81 82
	@Nullable
	private final ApplicationContext applicationContext;

83 84 85 86
	private final List<WebFilter> filters = new ArrayList<>();

	private final List<WebExceptionHandler> exceptionHandlers = new ArrayList<>();

87
	@Nullable
88 89
	private WebSessionManager sessionManager;

90
	@Nullable
91 92
	private ServerCodecConfigurer codecConfigurer;

93
	@Nullable
94 95
	private LocaleContextResolver localeContextResolver;

96

97 98 99
	/**
	 * Private constructor to use when initialized from an ApplicationContext.
	 */
100
	private WebHttpHandlerBuilder(WebHandler webHandler, @Nullable ApplicationContext applicationContext) {
101 102 103 104 105
		Assert.notNull(webHandler, "WebHandler must not be null");
		this.webHandler = webHandler;
		this.applicationContext = applicationContext;
	}

R
Rossen Stoyanchev 已提交
106 107 108 109 110
	/**
	 * Copy constructor.
	 */
	private WebHttpHandlerBuilder(WebHttpHandlerBuilder other) {
		this.webHandler = other.webHandler;
111
		this.applicationContext = other.applicationContext;
R
Rossen Stoyanchev 已提交
112 113 114 115 116 117 118
		this.filters.addAll(other.filters);
		this.exceptionHandlers.addAll(other.exceptionHandlers);
		this.sessionManager = other.sessionManager;
		this.codecConfigurer = other.codecConfigurer;
		this.localeContextResolver = other.localeContextResolver;
	}

119 120

	/**
R
Rossen Stoyanchev 已提交
121
	 * Static factory method to create a new builder instance.
122
	 * @param webHandler the target handler for the request
123
	 * @return the prepared builder
124
	 */
125
	public static WebHttpHandlerBuilder webHandler(WebHandler webHandler) {
126
		return new WebHttpHandlerBuilder(webHandler, null);
127 128
	}

129
	/**
R
Rossen Stoyanchev 已提交
130 131
	 * Static factory method to create a new builder instance by detecting beans
	 * in an {@link ApplicationContext}. The following are detected:
132
	 * <ul>
133 134 135 136 137 138 139 140 141 142 143 144
	 * <li>{@link WebHandler} [1] -- looked up by the name
	 * {@link #WEB_HANDLER_BEAN_NAME}.
	 * <li>{@link WebFilter} [0..N] -- detected by type and ordered,
	 * see {@link AnnotationAwareOrderComparator}.
	 * <li>{@link WebExceptionHandler} [0..N] -- detected by type and
	 * ordered.
	 * <li>{@link WebSessionManager} [0..1] -- looked up by the name
	 * {@link #WEB_SESSION_MANAGER_BEAN_NAME}.
	 * <li>{@link ServerCodecConfigurer} [0..1] -- looked up by the name
	 * {@link #SERVER_CODEC_CONFIGURER_BEAN_NAME}.
	 * <li>{@link LocaleContextResolver} [0..1] -- looked up by the name
	 * {@link #LOCALE_CONTEXT_RESOLVER_BEAN_NAME}.
145 146 147 148 149 150
	 * </ul>
	 * @param context the application context to use for the lookup
	 * @return the prepared builder
	 */
	public static WebHttpHandlerBuilder applicationContext(ApplicationContext context) {
		WebHttpHandlerBuilder builder = new WebHttpHandlerBuilder(
151
				context.getBean(WEB_HANDLER_BEAN_NAME, WebHandler.class), context);
152

R
Rossen Stoyanchev 已提交
153
		// Autowire lists for @Bean + @Order
154

R
Rossen Stoyanchev 已提交
155 156
		SortedBeanContainer container = new SortedBeanContainer();
		context.getAutowireCapableBeanFactory().autowireBean(container);
157 158
		builder.filters(filters -> filters.addAll(container.getFilters()));
		builder.exceptionHandlers(handlers -> handlers.addAll(container.getExceptionHandlers()));
159 160 161 162 163 164 165 166 167

		try {
			builder.sessionManager(
					context.getBean(WEB_SESSION_MANAGER_BEAN_NAME, WebSessionManager.class));
		}
		catch (NoSuchBeanDefinitionException ex) {
			// Fall back on default
		}

168 169 170 171 172 173 174 175
		try {
			builder.codecConfigurer(
					context.getBean(SERVER_CODEC_CONFIGURER_BEAN_NAME, ServerCodecConfigurer.class));
		}
		catch (NoSuchBeanDefinitionException ex) {
			// Fall back on default
		}

176 177 178 179 180 181 182 183
		try {
			builder.localeContextResolver(
					context.getBean(LOCALE_CONTEXT_RESOLVER_BEAN_NAME, LocaleContextResolver.class));
		}
		catch (NoSuchBeanDefinitionException ex) {
			// Fall back on default
		}

184 185 186
		return builder;
	}

187

188 189
	/**
	 * Add the given filter(s).
190 191
	 * @param filters the filter(s) to add that's
	 */
192 193 194 195 196 197 198
	public WebHttpHandlerBuilder filter(WebFilter... filters) {
		if (!ObjectUtils.isEmpty(filters)) {
			this.filters.addAll(Arrays.asList(filters));
		}
		return this;
	}

199
	/**
200 201
	 * Manipulate the "live" list of currently configured filters.
	 * @param consumer the consumer to use
202
	 */
203 204
	public WebHttpHandlerBuilder filters(Consumer<List<WebFilter>> consumer) {
		consumer.accept(this.filters);
205 206 207
		return this;
	}

208 209 210 211 212 213 214 215 216 217 218
	/**
	 * Add the given exception handler(s).
	 * @param handlers the exception handler(s)
	 */
	public WebHttpHandlerBuilder exceptionHandler(WebExceptionHandler... handlers) {
		if (!ObjectUtils.isEmpty(handlers)) {
			this.exceptionHandlers.addAll(Arrays.asList(handlers));
		}
		return this;
	}

219
	/**
220 221
	 * Manipulate the "live" list of currently configured exception handlers.
	 * @param consumer the consumer to use
222
	 */
223 224
	public WebHttpHandlerBuilder exceptionHandlers(Consumer<List<WebExceptionHandler>> consumer) {
		consumer.accept(this.exceptionHandlers);
225 226 227
		return this;
	}

228 229
	/**
	 * Configure the {@link WebSessionManager} to set on the
230 231
	 * {@link ServerWebExchange WebServerExchange}.
	 * <p>By default {@link DefaultWebSessionManager} is used.
R
Rossen Stoyanchev 已提交
232
	 * @param manager the session manager
233
	 * @see HttpWebHandlerAdapter#setSessionManager(WebSessionManager)
234
	 */
R
Rossen Stoyanchev 已提交
235 236
	public WebHttpHandlerBuilder sessionManager(WebSessionManager manager) {
		this.sessionManager = manager;
237 238 239
		return this;
	}

240
	/**
J
Juergen Hoeller 已提交
241 242
	 * Whether a {@code WebSessionManager} is configured or not, either detected from an
	 * {@code ApplicationContext} or explicitly configured via {@link #sessionManager}.
243 244 245
	 * @since 5.0.9
	 */
	public boolean hasSessionManager() {
J
Juergen Hoeller 已提交
246
		return (this.sessionManager != null);
247 248 249 250
	}

	/**
	 * Configure the {@link ServerCodecConfigurer} to set on the {@code WebServerExchange}.
251 252 253 254 255 256 257
	 * @param codecConfigurer the codec configurer
	 */
	public WebHttpHandlerBuilder codecConfigurer(ServerCodecConfigurer codecConfigurer) {
		this.codecConfigurer = codecConfigurer;
		return this;
	}

258 259

	/**
J
Juergen Hoeller 已提交
260 261
	 * Whether a {@code ServerCodecConfigurer} is configured or not, either detected from an
	 * {@code ApplicationContext} or explicitly configured via {@link #codecConfigurer}.
262 263 264
	 * @since 5.0.9
	 */
	public boolean hasCodecConfigurer() {
J
Juergen Hoeller 已提交
265
		return (this.codecConfigurer != null);
266 267
	}

268 269 270 271 272 273 274 275 276 277
	/**
	 * Configure the {@link LocaleContextResolver} to set on the
	 * {@link ServerWebExchange WebServerExchange}.
	 * @param localeContextResolver the locale context resolver
	 */
	public WebHttpHandlerBuilder localeContextResolver(LocaleContextResolver localeContextResolver) {
		this.localeContextResolver = localeContextResolver;
		return this;
	}

278
	/**
J
Juergen Hoeller 已提交
279 280
	 * Whether a {@code LocaleContextResolver} is configured or not, either detected from an
	 * {@code ApplicationContext} or explicitly configured via {@link #localeContextResolver}.
281 282 283
	 * @since 5.0.9
	 */
	public boolean hasLocaleContextResolver() {
J
Juergen Hoeller 已提交
284
		return (this.localeContextResolver != null);
285 286
	}

R
Rossen Stoyanchev 已提交
287

288 289 290 291
	/**
	 * Build the {@link HttpHandler}.
	 */
	public HttpHandler build() {
292
		WebHandler decorated = new FilteringWebHandler(this.webHandler, this.filters);
R
Rossen Stoyanchev 已提交
293 294 295
		decorated = new ExceptionHandlingWebHandler(decorated,  this.exceptionHandlers);

		HttpWebHandlerAdapter adapted = new HttpWebHandlerAdapter(decorated);
296
		if (this.sessionManager != null) {
R
Rossen Stoyanchev 已提交
297
			adapted.setSessionManager(this.sessionManager);
298
		}
299 300 301
		if (this.codecConfigurer != null) {
			adapted.setCodecConfigurer(this.codecConfigurer);
		}
302 303 304
		if (this.localeContextResolver != null) {
			adapted.setLocaleContextResolver(this.localeContextResolver);
		}
305 306 307
		if (this.applicationContext != null) {
			adapted.setApplicationContext(this.applicationContext);
		}
308 309
		adapted.afterPropertiesSet();

R
Rossen Stoyanchev 已提交
310
		return adapted;
311 312
	}

R
Rossen Stoyanchev 已提交
313 314 315 316
	/**
	 * Clone this {@link WebHttpHandlerBuilder}.
	 * @return the cloned builder instance
	 */
R
Polish  
Rossen Stoyanchev 已提交
317 318
	@Override
	public WebHttpHandlerBuilder clone() {
R
Rossen Stoyanchev 已提交
319 320 321
		return new WebHttpHandlerBuilder(this);
	}

322

R
Rossen Stoyanchev 已提交
323
	private static class SortedBeanContainer {
324

325
		private List<WebFilter> filters = Collections.emptyList();
326

327
		private List<WebExceptionHandler> exceptionHandlers = Collections.emptyList();
R
Rossen Stoyanchev 已提交
328

329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
		@Autowired(required = false)
		public void setFilters(List<WebFilter> filters) {
			this.filters = filters;
		}

		public List<WebFilter> getFilters() {
			return this.filters;
		}

		@Autowired(required = false)
		public void setExceptionHandlers(List<WebExceptionHandler> exceptionHandlers) {
			this.exceptionHandlers = exceptionHandlers;
		}

		public List<WebExceptionHandler> getExceptionHandlers() {
			return this.exceptionHandlers;
		}
	}

348
}