WebHttpHandlerBuilder.java 11.4 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;

/**
45
 * This builder has two purposes.
46
 *
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}'s, then
 * further decorated with a set of {@link WebExceptionHandler}'s.
50
 *
51 52 53 54 55 56 57 58
 * <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}.
 *
 * <p>The processing chain can be assembled manually via builder methods, or
 * detected from Spring configuration via
 * {@link #applicationContext(ApplicationContext)}, or a mix of both.
59 60
 *
 * @author Rossen Stoyanchev
61
 * @author Sebastien Deleuze
62
 * @since 5.0
63
 * @see HttpWebHandlerAdapter
64
 */
P
Phillip Webb 已提交
65
public final class WebHttpHandlerBuilder {
66

67 68 69 70 71 72
	/** 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";

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

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

79

R
Rossen Stoyanchev 已提交
80
	private final WebHandler webHandler;
81

82 83 84
	@Nullable
	private final ApplicationContext applicationContext;

85 86 87 88
	private final List<WebFilter> filters = new ArrayList<>();

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

89
	@Nullable
90 91
	private WebSessionManager sessionManager;

92
	@Nullable
93 94
	private ServerCodecConfigurer codecConfigurer;

95
	@Nullable
96 97
	private LocaleContextResolver localeContextResolver;

98

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

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

121 122

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

131
	/**
R
Rossen Stoyanchev 已提交
132 133
	 * Static factory method to create a new builder instance by detecting beans
	 * in an {@link ApplicationContext}. The following are detected:
134
	 * <ul>
135 136 137 138 139 140 141 142 143 144 145 146
	 * <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}.
147 148 149 150 151 152
	 * </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(
153
				context.getBean(WEB_HANDLER_BEAN_NAME, WebHandler.class), context);
154

R
Rossen Stoyanchev 已提交
155
		// Autowire lists for @Bean + @Order
156

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

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

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

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

186 187 188
		return builder;
	}

189

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

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

210 211 212 213 214 215 216 217 218 219 220
	/**
	 * 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;
	}

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

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

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

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

261 262 263 264 265 266 267 268 269 270 271

	/**
	 * Whether a {@code ServerCodecConfigurer} is configured or not, either
	 * detected from an {@code ApplicationContext} or explicitly configured via
	 * {@link #codecConfigurer(ServerCodecConfigurer)}.
	 * @since 5.0.9
	 */
	public boolean hasCodecConfigurer() {
		return this.codecConfigurer != null;
	}

272 273 274 275 276 277 278 279 280 281
	/**
	 * 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;
	}

282 283 284 285 286 287 288 289 290 291
	/**
	 * Whether a {@code LocaleContextResolver} is configured or not, either
	 * detected from an {@code ApplicationContext} or explicitly configured via
	 * {@link #localeContextResolver(LocaleContextResolver)}.
	 * @since 5.0.9
	 */
	public boolean hasLocaleContextResolver() {
		return this.localeContextResolver != null;
	}

R
Rossen Stoyanchev 已提交
292

293 294 295 296
	/**
	 * Build the {@link HttpHandler}.
	 */
	public HttpHandler build() {
297
		WebHandler decorated = new FilteringWebHandler(this.webHandler, this.filters);
R
Rossen Stoyanchev 已提交
298 299 300
		decorated = new ExceptionHandlingWebHandler(decorated,  this.exceptionHandlers);

		HttpWebHandlerAdapter adapted = new HttpWebHandlerAdapter(decorated);
301
		if (this.sessionManager != null) {
R
Rossen Stoyanchev 已提交
302
			adapted.setSessionManager(this.sessionManager);
303
		}
304 305 306
		if (this.codecConfigurer != null) {
			adapted.setCodecConfigurer(this.codecConfigurer);
		}
307 308 309
		if (this.localeContextResolver != null) {
			adapted.setLocaleContextResolver(this.localeContextResolver);
		}
310 311 312
		if (this.applicationContext != null) {
			adapted.setApplicationContext(this.applicationContext);
		}
313 314
		adapted.afterPropertiesSet();

R
Rossen Stoyanchev 已提交
315
		return adapted;
316 317
	}

R
Rossen Stoyanchev 已提交
318 319 320 321
	/**
	 * Clone this {@link WebHttpHandlerBuilder}.
	 * @return the cloned builder instance
	 */
R
Polish  
Rossen Stoyanchev 已提交
322 323
	@Override
	public WebHttpHandlerBuilder clone() {
R
Rossen Stoyanchev 已提交
324 325 326
		return new WebHttpHandlerBuilder(this);
	}

327

R
Rossen Stoyanchev 已提交
328
	private static class SortedBeanContainer {
329

330
		private List<WebFilter> filters = Collections.emptyList();
331

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

334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
		@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;
		}
	}

353
}