Skip to content

Commit

Permalink
WebHttpHandlerBuilder retains ApplicationContext in copy constructor
Browse files Browse the repository at this point in the history
Issue: SPR-16972
  • Loading branch information
jhoeller committed Jun 25, 2018
1 parent 853d30d commit 2a15962
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 31 deletions.
@@ -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.
Expand Down Expand Up @@ -79,6 +79,9 @@ public class WebHttpHandlerBuilder {

private final WebHandler webHandler;

@Nullable
private final ApplicationContext applicationContext;

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

private final List<WebExceptionHandler> exceptionHandlers = new ArrayList<>();
Expand All @@ -92,22 +95,11 @@ public class WebHttpHandlerBuilder {
@Nullable
private LocaleContextResolver localeContextResolver;

@Nullable
private ApplicationContext applicationContext;


/**
* Private constructor.
*/
private WebHttpHandlerBuilder(WebHandler webHandler) {
Assert.notNull(webHandler, "WebHandler must not be null");
this.webHandler = webHandler;
}

/**
* Private constructor to use when initialized from an ApplicationContext.
*/
private WebHttpHandlerBuilder(WebHandler webHandler, ApplicationContext applicationContext) {
private WebHttpHandlerBuilder(WebHandler webHandler, @Nullable ApplicationContext applicationContext) {
Assert.notNull(webHandler, "WebHandler must not be null");
this.webHandler = webHandler;
this.applicationContext = applicationContext;
Expand All @@ -118,6 +110,7 @@ private WebHttpHandlerBuilder(WebHandler webHandler, ApplicationContext applicat
*/
private WebHttpHandlerBuilder(WebHttpHandlerBuilder other) {
this.webHandler = other.webHandler;
this.applicationContext = other.applicationContext;
this.filters.addAll(other.filters);
this.exceptionHandlers.addAll(other.exceptionHandlers);
this.sessionManager = other.sessionManager;
Expand All @@ -132,7 +125,7 @@ private WebHttpHandlerBuilder(WebHttpHandlerBuilder other) {
* @return the prepared builder
*/
public static WebHttpHandlerBuilder webHandler(WebHandler webHandler) {
return new WebHttpHandlerBuilder(webHandler);
return new WebHttpHandlerBuilder(webHandler, null);
}

/**
Expand All @@ -156,7 +149,6 @@ public static WebHttpHandlerBuilder webHandler(WebHandler webHandler) {
* @return the prepared builder
*/
public static WebHttpHandlerBuilder applicationContext(ApplicationContext context) {

WebHttpHandlerBuilder builder = new WebHttpHandlerBuilder(
context.getBean(WEB_HANDLER_BEAN_NAME, WebHandler.class), context);

Expand Down Expand Up @@ -272,13 +264,10 @@ public WebHttpHandlerBuilder localeContextResolver(LocaleContextResolver localeC
* Build the {@link HttpHandler}.
*/
public HttpHandler build() {

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

HttpWebHandlerAdapter adapted = new HttpWebHandlerAdapter(decorated);

if (this.sessionManager != null) {
adapted.setSessionManager(this.sessionManager);
}
Expand All @@ -291,7 +280,6 @@ public HttpHandler build() {
if (this.applicationContext != null) {
adapted.setApplicationContext(this.applicationContext);
}

adapted.afterPropertiesSet();

return adapted;
Expand Down
@@ -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.
Expand Down Expand Up @@ -36,10 +36,8 @@
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebHandler;

import static java.time.Duration.ofMillis;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static java.time.Duration.*;
import static org.junit.Assert.*;

/**
* Unit tests for {@link WebHttpHandlerBuilder}.
Expand All @@ -48,13 +46,12 @@
public class WebHttpHandlerBuilderTests {

@Test // SPR-15074
public void orderedWebFilterBeans() throws Exception {
public void orderedWebFilterBeans() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(OrderedWebFilterBeanConfig.class);
context.refresh();

HttpHandler httpHandler = WebHttpHandlerBuilder.applicationContext(context).build();

assertTrue(httpHandler instanceof HttpWebHandlerAdapter);
assertSame(context, ((HttpWebHandlerAdapter) httpHandler).getApplicationContext());

Expand All @@ -66,13 +63,12 @@ public void orderedWebFilterBeans() throws Exception {
}

@Test // SPR-15074
public void orderedWebExceptionHandlerBeans() throws Exception {
public void orderedWebExceptionHandlerBeans() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(OrderedExceptionHandlerBeanConfig.class);
context.refresh();

HttpHandler httpHandler = WebHttpHandlerBuilder.applicationContext(context).build();

MockServerHttpRequest request = MockServerHttpRequest.get("/").build();
MockServerHttpResponse response = new MockServerHttpResponse();
httpHandler.handle(request, response).block(ofMillis(5000));
Expand All @@ -81,20 +77,30 @@ public void orderedWebExceptionHandlerBeans() throws Exception {
}

@Test
public void configWithoutFilters() throws Exception {
public void configWithoutFilters() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(NoFilterConfig.class);
context.refresh();

HttpHandler httpHandler = WebHttpHandlerBuilder.applicationContext(context).build();

MockServerHttpRequest request = MockServerHttpRequest.get("/").build();
MockServerHttpResponse response = new MockServerHttpResponse();
httpHandler.handle(request, response).block(ofMillis(5000));

assertEquals("handled", response.getBodyAsString().block(ofMillis(5000)));
}

@Test // SPR-16972
public void cloneWithApplicationContext() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(NoFilterConfig.class);
context.refresh();

WebHttpHandlerBuilder builder = WebHttpHandlerBuilder.applicationContext(context);
assertSame(context, ((HttpWebHandlerAdapter) builder.build()).getApplicationContext());
assertSame(context, ((HttpWebHandlerAdapter) builder.clone().build()).getApplicationContext());
}


private static Mono<Void> writeToResponse(ServerWebExchange exchange, String value) {
byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
Expand Down

0 comments on commit 2a15962

Please sign in to comment.