Skip to content

Commit 26000ee

Browse files
committed
Polishing contribution
Closes gh-26502
1 parent 0c2c66b commit 26000ee

File tree

3 files changed

+43
-78
lines changed

3 files changed

+43
-78
lines changed
Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,25 +16,12 @@
1616

1717
package org.springframework.http.server.reactive;
1818

19-
import org.springframework.context.ApplicationContext;
20-
2119
import java.util.function.Function;
22-
import java.util.function.UnaryOperator;
2320

2421
/**
25-
* Allows registering a bean that will decorate the instance of {@link HttpHandler},
26-
* used by {@link org.springframework.web.server.adapter.WebHttpHandlerBuilder#applicationContext(ApplicationContext)};
27-
*
22+
* Contract for applying a decorator to an {@code HttpHandler}.
23+
* @author Christophe Maillard
2824
* @since 5.3.4
2925
*/
30-
public interface HttpHandlerDecoratorFactory extends UnaryOperator<HttpHandler> {
31-
32-
static HttpHandlerDecoratorFactory identity() {
33-
return x -> x;
34-
}
35-
36-
default Function<HttpHandler, HttpHandler> toFunction() {
37-
return this;
38-
}
39-
26+
public interface HttpHandlerDecoratorFactory extends Function<HttpHandler, HttpHandler> {
4027
}

spring-web/src/main/java/org/springframework/web/server/adapter/WebHttpHandlerBuilder.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -176,12 +176,9 @@ public static WebHttpHandlerBuilder applicationContext(ApplicationContext contex
176176
.collect(Collectors.toList());
177177
builder.exceptionHandlers(handlers -> handlers.addAll(exceptionHandlers));
178178

179-
Function<HttpHandler, HttpHandler> httpHandlerDecorator = context
180-
.getBeanProvider(HttpHandlerDecoratorFactory.class)
179+
context.getBeanProvider(HttpHandlerDecoratorFactory.class)
181180
.orderedStream()
182-
.map(HttpHandlerDecoratorFactory::toFunction)
183-
.reduce(Function.identity(), Function::andThen);
184-
builder.httpHandlerDecorator(httpHandlerDecorator);
181+
.forEach(builder::httpHandlerDecorator);
185182

186183
try {
187184
builder.sessionManager(

spring-web/src/test/java/org/springframework/web/server/adapter/WebHttpHandlerBuilderTests.java

Lines changed: 36 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,16 @@
1616

1717
package org.springframework.web.server.adapter;
1818

19+
import java.nio.charset.StandardCharsets;
20+
import java.util.Collections;
21+
import java.util.concurrent.atomic.AtomicBoolean;
22+
import java.util.function.BiFunction;
23+
import java.util.function.Function;
24+
1925
import org.junit.jupiter.api.Test;
26+
import reactor.core.publisher.Flux;
27+
import reactor.core.publisher.Mono;
28+
2029
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
2130
import org.springframework.context.annotation.Bean;
2231
import org.springframework.context.annotation.Configuration;
@@ -33,14 +42,6 @@
3342
import org.springframework.web.server.WebHandler;
3443
import org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest;
3544
import org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse;
36-
import reactor.core.publisher.Flux;
37-
import reactor.core.publisher.Mono;
38-
39-
import java.nio.charset.StandardCharsets;
40-
import java.util.Collections;
41-
import java.util.concurrent.atomic.AtomicBoolean;
42-
import java.util.concurrent.atomic.AtomicLong;
43-
import java.util.function.BiFunction;
4445

4546
import static java.time.Duration.ofMillis;
4647
import static org.assertj.core.api.Assertions.assertThat;
@@ -141,21 +142,16 @@ void httpHandlerDecorator() {
141142
}
142143

143144
@Test
144-
void httpHandlerDecoratorBeans() {
145+
void httpHandlerDecoratorFactoryBeans() {
146+
HttpHandler handler = WebHttpHandlerBuilder.applicationContext(
147+
new AnnotationConfigApplicationContext(HttpHandlerDecoratorFactoryBeansConfig.class)).build();
145148

146-
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
147-
context.register(HttpHandlerDecoratorBeansConfig.class);
148-
context.refresh();
149-
HttpHandler builder = WebHttpHandlerBuilder.applicationContext(context).build();
150-
151-
builder.handle(MockServerHttpRequest.get("/").build(), new MockServerHttpResponse()).block();
152-
153-
AtomicLong decorator1NanoTime = context.getBean("decorator1NanoTime", AtomicLong.class);
154-
AtomicLong decorator2NanoTime = context.getBean("decorator2NanoTime", AtomicLong.class);
155-
AtomicLong decorator3NanoTime = context.getBean("decorator3NanoTime", AtomicLong.class);
156-
assertThat(decorator1NanoTime).hasValueLessThan(decorator3NanoTime.get());
157-
assertThat(decorator3NanoTime).hasValueLessThan(decorator2NanoTime.get());
149+
MockServerHttpResponse response = new MockServerHttpResponse();
150+
handler.handle(MockServerHttpRequest.get("/").build(), response).block();
158151

152+
Function<String, Long> headerValue = name -> Long.valueOf(response.getHeaders().getFirst(name));
153+
assertThat(headerValue.apply("decoratorA")).isLessThan(headerValue.apply("decoratorB"));
154+
assertThat(headerValue.apply("decoratorC")).isLessThan(headerValue.apply("decoratorB"));
159155
}
160156

161157
private static Mono<Void> writeToResponse(ServerWebExchange exchange, String value) {
@@ -164,56 +160,41 @@ private static Mono<Void> writeToResponse(ServerWebExchange exchange, String val
164160
return exchange.getResponse().writeWith(Flux.just(buffer));
165161
}
166162

167-
@Configuration
168-
static class HttpHandlerDecoratorBeansConfig {
169-
170-
@Bean
171-
public WebHandler webHandler() {
172-
return exchange -> Mono.empty();
173-
}
174163

175-
@Bean
176-
public AtomicLong decorator1NanoTime() {
177-
return new AtomicLong();
178-
}
164+
@Configuration
165+
static class HttpHandlerDecoratorFactoryBeansConfig {
179166

180167
@Bean
181168
@Order(1)
182-
public HttpHandlerDecoratorFactory decorator1() {
183-
return handler -> {
184-
decorator1NanoTime().set(System.nanoTime());
185-
return handler;
169+
public HttpHandlerDecoratorFactory decoratorFactoryA() {
170+
return delegate -> (HttpHandler) (request, response) -> {
171+
response.getHeaders().set("decoratorA", String.valueOf(System.nanoTime()));
172+
return delegate.handle(request, response);
186173
};
187174
}
188175

189-
@Bean
190-
public AtomicLong decorator2NanoTime() {
191-
return new AtomicLong();
192-
}
193-
194176
@Bean
195177
@Order(3)
196-
public HttpHandlerDecoratorFactory decorator2() {
197-
return handler -> {
198-
decorator2NanoTime().set(System.nanoTime());
199-
return handler;
178+
public HttpHandlerDecoratorFactory decoratorFactoryB() {
179+
return delegate -> (HttpHandler) (request, response) -> {
180+
response.getHeaders().set("decoratorB", String.valueOf(System.nanoTime()));
181+
return delegate.handle(request, response);
200182
};
201183
}
202184

203-
@Bean
204-
public AtomicLong decorator3NanoTime() {
205-
return new AtomicLong();
206-
}
207-
208185
@Bean
209186
@Order(2)
210-
public HttpHandlerDecoratorFactory decorator3() {
211-
return handler -> {
212-
decorator3NanoTime().set(System.nanoTime());
213-
return handler;
187+
public HttpHandlerDecoratorFactory decoratorFactoryC() {
188+
return delegate -> (HttpHandler) (request, response) -> {
189+
response.getHeaders().set("decoratorC", String.valueOf(System.nanoTime()));
190+
return delegate.handle(request, response);
214191
};
215192
}
216193

194+
@Bean
195+
public WebHandler webHandler() {
196+
return exchange -> Mono.empty();
197+
}
217198
}
218199

219200

0 commit comments

Comments
 (0)