WebHttpHandlerBuilder.java 10.1 KB
Newer Older
1
/*
R
Rossen Stoyanchev 已提交
2
 * Copyright 2002-2017 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 65 66
 */
public class WebHttpHandlerBuilder {

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 85

	private final List<WebFilter> filters = new ArrayList<>();

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

86
	@Nullable
87 88
	private WebSessionManager sessionManager;

89
	@Nullable
90 91
	private ServerCodecConfigurer codecConfigurer;

92
	@Nullable
93 94
	private LocaleContextResolver localeContextResolver;

95 96 97 98

	/**
	 * Private constructor.
	 */
R
Rossen Stoyanchev 已提交
99 100 101
	private WebHttpHandlerBuilder(WebHandler webHandler) {
		Assert.notNull(webHandler, "WebHandler must not be null");
		this.webHandler = webHandler;
102 103
	}

R
Rossen Stoyanchev 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116
	/**
	 * Copy constructor.
	 */
	private WebHttpHandlerBuilder(WebHttpHandlerBuilder other) {

		this.webHandler = other.webHandler;
		this.filters.addAll(other.filters);
		this.exceptionHandlers.addAll(other.exceptionHandlers);
		this.sessionManager = other.sessionManager;
		this.codecConfigurer = other.codecConfigurer;
		this.localeContextResolver = other.localeContextResolver;
	}

117 118

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

127
	/**
R
Rossen Stoyanchev 已提交
128 129
	 * Static factory method to create a new builder instance by detecting beans
	 * in an {@link ApplicationContext}. The following are detected:
130 131 132 133 134 135 136 137 138
	 * <ul>
	 *	<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}.
139 140
	 *  <li>{@link ServerCodecConfigurer} [0..1] -- looked up by the name
	 *	{@link #SERVER_CODEC_CONFIGURER_BEAN_NAME}.
141 142
	 *<li>{@link LocaleContextResolver} [0..1] -- looked up by the name
	 *	{@link #LOCALE_CONTEXT_RESOLVER_BEAN_NAME}.
143 144 145 146 147 148 149 150 151
	 * </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(
				context.getBean(WEB_HANDLER_BEAN_NAME, WebHandler.class));

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

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

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

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

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

183 184 185
		return builder;
	}

186

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

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

207 208 209 210 211 212 213 214 215 216 217
	/**
	 * 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;
	}

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

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

239 240 241 242 243 244 245 246 247 248
	/**
	 * Configure the {@link ServerCodecConfigurer} to set on the
	 * {@link ServerWebExchange WebServerExchange}.
	 * @param codecConfigurer the codec configurer
	 */
	public WebHttpHandlerBuilder codecConfigurer(ServerCodecConfigurer codecConfigurer) {
		this.codecConfigurer = codecConfigurer;
		return this;
	}

249 250 251 252 253 254 255 256 257 258
	/**
	 * 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;
	}

R
Rossen Stoyanchev 已提交
259

260 261 262 263
	/**
	 * Build the {@link HttpHandler}.
	 */
	public HttpHandler build() {
R
Rossen Stoyanchev 已提交
264 265 266 267 268 269 270

		WebHandler decorated;

		decorated = new FilteringWebHandler(this.webHandler, this.filters);
		decorated = new ExceptionHandlingWebHandler(decorated,  this.exceptionHandlers);

		HttpWebHandlerAdapter adapted = new HttpWebHandlerAdapter(decorated);
271
		if (this.sessionManager != null) {
R
Rossen Stoyanchev 已提交
272
			adapted.setSessionManager(this.sessionManager);
273
		}
274 275 276
		if (this.codecConfigurer != null) {
			adapted.setCodecConfigurer(this.codecConfigurer);
		}
277 278 279
		if (this.localeContextResolver != null) {
			adapted.setLocaleContextResolver(this.localeContextResolver);
		}
R
Rossen Stoyanchev 已提交
280 281

		return adapted;
282 283
	}

R
Rossen Stoyanchev 已提交
284 285 286 287
	/**
	 * Clone this {@link WebHttpHandlerBuilder}.
	 * @return the cloned builder instance
	 */
R
Polish  
Rossen Stoyanchev 已提交
288 289
	@Override
	public WebHttpHandlerBuilder clone() {
R
Rossen Stoyanchev 已提交
290 291 292
		return new WebHttpHandlerBuilder(this);
	}

293

R
Rossen Stoyanchev 已提交
294
	private static class SortedBeanContainer {
295

296
		private List<WebFilter> filters = Collections.emptyList();
297

298
		private List<WebExceptionHandler> exceptionHandlers = Collections.emptyList();
R
Rossen Stoyanchev 已提交
299 300


301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
		@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;
		}
	}

320
}