Skip to content

Commit b5ec98f

Browse files
artembilangaryrussell
authored andcommitted
WebFlux improvements
* add `BodyExtractor` support for Outbound part * add `ClientHttpResponseBodyExtractor` as identity function * add XML configuration for the Inbound part * document `BodyExtractor` and Inbound XML support Rename `replyToFlux` property to the `replyPayloadToFlux` Doc Polishing
1 parent 1e1346d commit b5ec98f

16 files changed

+960
-19
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.integration.webflux.config;
18+
19+
import org.w3c.dom.Element;
20+
21+
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
22+
import org.springframework.beans.factory.xml.ParserContext;
23+
import org.springframework.integration.config.xml.IntegrationNamespaceUtils;
24+
import org.springframework.integration.http.config.HttpInboundEndpointParser;
25+
import org.springframework.integration.webflux.inbound.WebFluxInboundEndpoint;
26+
27+
/**
28+
* @author Artem Bilan
29+
*
30+
* @since 5.0.1
31+
*/
32+
public class WebFluxInboundEndpointParser extends HttpInboundEndpointParser {
33+
34+
public WebFluxInboundEndpointParser(boolean expectReply) {
35+
super(expectReply);
36+
}
37+
38+
@Override
39+
protected Class<?> getBeanClass(Element element) {
40+
return WebFluxInboundEndpoint.class;
41+
}
42+
43+
@Override
44+
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
45+
super.doParse(element, parserContext, builder);
46+
47+
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "codec-configurer");
48+
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "requested-content-type-resolver");
49+
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "reactive-adapter-registry");
50+
}
51+
52+
}

spring-integration-webflux/src/main/java/org/springframework/integration/webflux/config/WebFluxNamespaceHandler.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2017-2018 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.
@@ -28,6 +28,8 @@
2828
public class WebFluxNamespaceHandler extends AbstractIntegrationNamespaceHandler {
2929

3030
public void init() {
31+
registerBeanDefinitionParser("inbound-channel-adapter", new WebFluxInboundEndpointParser(false));
32+
registerBeanDefinitionParser("inbound-gateway", new WebFluxInboundEndpointParser(true));
3133
registerBeanDefinitionParser("outbound-channel-adapter", new WebFluxOutboundChannelAdapterParser());
3234
registerBeanDefinitionParser("outbound-gateway", new WebFluxOutboundGatewayParser());
3335
}

spring-integration-webflux/src/main/java/org/springframework/integration/webflux/config/WebFluxOutboundGatewayParser.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2017-2018 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.
@@ -47,7 +47,8 @@ protected BeanDefinitionBuilder getBuilder(Element element, ParserContext parser
4747
.addIndexedArgumentValue(1, new RuntimeBeanReference(webClientRef));
4848
}
4949

50-
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "reply-to-flux");
50+
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "reply-payload-to-flux");
51+
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "body-extractor");
5152
return builder;
5253
}
5354

spring-integration-webflux/src/main/java/org/springframework/integration/webflux/dsl/WebFluxMessageHandlerSpec.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 the original author or authors.
2+
* Copyright 2017-2018 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.
@@ -20,11 +20,16 @@
2020

2121
import org.springframework.expression.Expression;
2222
import org.springframework.expression.common.LiteralExpression;
23+
import org.springframework.http.client.reactive.ClientHttpResponse;
2324
import org.springframework.integration.expression.ValueExpression;
2425
import org.springframework.integration.http.dsl.BaseHttpMessageHandlerSpec;
2526
import org.springframework.integration.webflux.outbound.WebFluxRequestExecutingMessageHandler;
27+
import org.springframework.web.reactive.function.BodyExtractor;
2628
import org.springframework.web.reactive.function.client.WebClient;
2729

30+
import reactor.core.publisher.Flux;
31+
import reactor.core.publisher.Mono;
32+
2833
/**
2934
* The {@link BaseHttpMessageHandlerSpec} implementation for the {@link WebFluxRequestExecutingMessageHandler}.
3035
*
@@ -53,6 +58,30 @@ public class WebFluxMessageHandlerSpec
5358
this.webClient = webClient;
5459
}
5560

61+
/**
62+
* The boolean flag to identify if the reply payload should be as a {@link Flux} from the response body
63+
* or as resolved value from the {@link Mono} of the response body.
64+
* Defaults to {@code false} - simple value is pushed downstream.
65+
* Makes sense when {@code expectedResponseType} is configured.
66+
* @param replyPayloadToFlux represent reply payload as a {@link Flux} or as a value from the {@link Mono}.
67+
* @since 5.0.1
68+
* @see WebFluxRequestExecutingMessageHandler#setReplyPayloadToFlux(boolean)
69+
*/
70+
public void replyPayloadToFlux(boolean replyPayloadToFlux) {
71+
this.target.setReplyPayloadToFlux(replyPayloadToFlux);
72+
}
73+
74+
/**
75+
* Specify a {@link BodyExtractor} as an alternative to the {@code expectedResponseType}
76+
* to allow to get low-level access to the received {@link ClientHttpResponse}.
77+
* @param bodyExtractor the {@link BodyExtractor} to use.
78+
* @since 5.0.1
79+
* @see WebFluxRequestExecutingMessageHandler#setBodyExtractor(BodyExtractor)
80+
*/
81+
public void bodyExtractor(BodyExtractor<?, ClientHttpResponse> bodyExtractor) {
82+
this.target.setBodyExtractor(bodyExtractor);
83+
}
84+
5685
@Override
5786
protected boolean isClientSet() {
5887
return this.webClient != null;

spring-integration-webflux/src/main/java/org/springframework/integration/webflux/inbound/WebFluxInboundEndpoint.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 the original author or authors.
2+
* Copyright 2017-2018 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.
@@ -126,7 +126,7 @@ public void setReactiveAdapterRegistry(ReactiveAdapterRegistry adapterRegistry)
126126

127127
@Override
128128
public String getComponentType() {
129-
return super.getComponentType().replaceFirst("(http:)", "$1webflux-");
129+
return super.getComponentType().replaceFirst("http", "webflux");
130130
}
131131

132132
@Override

spring-integration-webflux/src/main/java/org/springframework/integration/webflux/outbound/WebFluxRequestExecutingMessageHandler.java

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 the original author or authors.
2+
* Copyright 2017-2018 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.
@@ -31,6 +31,7 @@
3131
import org.springframework.http.HttpStatus;
3232
import org.springframework.http.ReactiveHttpInputMessage;
3333
import org.springframework.http.ResponseEntity;
34+
import org.springframework.http.client.reactive.ClientHttpResponse;
3435
import org.springframework.integration.expression.ValueExpression;
3536
import org.springframework.integration.http.outbound.AbstractHttpRequestExecutingMessageHandler;
3637
import org.springframework.messaging.Message;
@@ -62,7 +63,9 @@ public class WebFluxRequestExecutingMessageHandler extends AbstractHttpRequestEx
6263

6364
private final WebClient webClient;
6465

65-
private boolean replyToFlux;
66+
private boolean replyPayloadToFlux;
67+
68+
private BodyExtractor<?, ClientHttpResponse> bodyExtractor;
6669

6770
/**
6871
* Create a handler that will send requests to the provided URI.
@@ -120,13 +123,25 @@ public WebFluxRequestExecutingMessageHandler(Expression uriExpression, WebClient
120123
* or as resolved value from the {@link Mono} of the response body.
121124
* Defaults to {@code false} - simple value is pushed downstream.
122125
* Makes sense when {@code expectedResponseType} is configured.
123-
* @param replyToFlux represent reply payload as a {@link Flux} or as a value from the {@link Mono}.
126+
* @param replyPayloadToFlux represent reply payload as a {@link Flux} or as a value from the {@link Mono}.
127+
* @since 5.0.1
128+
* @see #setExpectedResponseType(Class)
129+
* @see #setExpectedResponseTypeExpression(Expression)
130+
*/
131+
public void setReplyPayloadToFlux(boolean replyPayloadToFlux) {
132+
this.replyPayloadToFlux = replyPayloadToFlux;
133+
}
134+
135+
/**
136+
* Specify a {@link BodyExtractor} as an alternative to the {@code expectedResponseType}
137+
* to allow to get low-level access to the received {@link ClientHttpResponse}.
138+
* @param bodyExtractor the {@link BodyExtractor} to use.
124139
* @since 5.0.1
125140
* @see #setExpectedResponseType(Class)
126141
* @see #setExpectedResponseTypeExpression(Expression)
127142
*/
128-
public void setReplyToFlux(boolean replyToFlux) {
129-
this.replyToFlux = replyToFlux;
143+
public void setBodyExtractor(BodyExtractor<?, ClientHttpResponse> bodyExtractor) {
144+
this.bodyExtractor = bodyExtractor;
130145
}
131146

132147
@Override
@@ -191,7 +206,7 @@ protected Object exchange(Supplier<URI> uriSupplier, HttpMethod httpMethod, Http
191206
Mono<?> bodyMono;
192207

193208
if (expectedResponseType != null) {
194-
if (this.replyToFlux) {
209+
if (this.replyPayloadToFlux) {
195210
BodyExtractor<? extends Flux<?>, ReactiveHttpInputMessage> extractor;
196211
if (expectedResponseType instanceof ParameterizedTypeReference<?>) {
197212
extractor = BodyExtractors.toFlux(
@@ -215,6 +230,15 @@ protected Object exchange(Supplier<URI> uriSupplier, HttpMethod httpMethod, Http
215230
bodyMono = response.body(extractor);
216231
}
217232
}
233+
else if (this.bodyExtractor != null) {
234+
Object body = response.body(this.bodyExtractor);
235+
if (body instanceof Mono) {
236+
bodyMono = (Mono<?>) body;
237+
}
238+
else {
239+
bodyMono = Mono.just(body);
240+
}
241+
}
218242
else {
219243
bodyMono = Mono.empty();
220244
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.integration.webflux.support;
18+
19+
import org.springframework.http.client.reactive.ClientHttpResponse;
20+
import org.springframework.web.reactive.function.BodyExtractor;
21+
22+
/**
23+
* The {@link BodyExtractor} identity function implementation
24+
* which just returns the provided {@link ClientHttpResponse}.
25+
*
26+
* @author Artem Bilan
27+
*
28+
* @since 5.0.1
29+
*/
30+
public class ClientHttpResponseBodyExtractor implements BodyExtractor<ClientHttpResponse, ClientHttpResponse> {
31+
32+
@Override
33+
public ClientHttpResponse extract(ClientHttpResponse response, Context context) {
34+
return response;
35+
}
36+
37+
}

0 commit comments

Comments
 (0)