-
Notifications
You must be signed in to change notification settings - Fork 1.1k
GH 3936 : Request attribute for webclient in webflux integration has been added #3950
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,10 +23,12 @@ | |
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
|
||
import org.springframework.beans.factory.BeanFactory; | ||
import org.springframework.core.ParameterizedTypeReference; | ||
import org.springframework.core.io.Resource; | ||
import org.springframework.expression.Expression; | ||
import org.springframework.expression.common.LiteralExpression; | ||
import org.springframework.expression.spel.support.StandardEvaluationContext; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.HttpStatusCode; | ||
|
@@ -35,6 +37,7 @@ | |
import org.springframework.http.ResponseEntity; | ||
import org.springframework.http.client.reactive.ClientHttpRequest; | ||
import org.springframework.http.client.reactive.ClientHttpResponse; | ||
import org.springframework.integration.expression.ExpressionUtils; | ||
import org.springframework.integration.expression.ValueExpression; | ||
import org.springframework.integration.http.outbound.AbstractHttpRequestExecutingMessageHandler; | ||
import org.springframework.lang.Nullable; | ||
|
@@ -57,6 +60,7 @@ | |
* @author Artem Bilan | ||
* @author Gary Russell | ||
* @author David Graff | ||
* @author Jatin Saxena | ||
* | ||
* @since 5.0 | ||
* | ||
|
@@ -75,6 +79,11 @@ public class WebFluxRequestExecutingMessageHandler extends AbstractHttpRequestEx | |
|
||
private Expression publisherElementTypeExpression; | ||
|
||
private Expression attributeVariablesExpression; | ||
|
||
private StandardEvaluationContext evaluationContext; | ||
|
||
|
||
/** | ||
* Create a handler that will send requests to the provided URI. | ||
* @param uri The URI. | ||
|
@@ -193,11 +202,22 @@ public void setPublisherElementTypeExpression(Expression publisherElementTypeExp | |
this.publisherElementTypeExpression = publisherElementTypeExpression; | ||
} | ||
|
||
public void setAttributeVariablesExpression(Expression attributeVariablesExpression) { | ||
artembilan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Assert.notNull(attributeVariablesExpression, "'attributeVariablesExpression' must not be null"); | ||
this.attributeVariablesExpression = attributeVariablesExpression; | ||
} | ||
|
||
@Override | ||
public String getComponentType() { | ||
return (isExpectReply() ? "webflux:outbound-gateway" : "webflux:outbound-channel-adapter"); | ||
} | ||
|
||
@Override | ||
protected final void doInit() { | ||
BeanFactory beanFactory = getBeanFactory(); | ||
this.evaluationContext = ExpressionUtils.createStandardEvaluationContext(beanFactory); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can just have a single line here: doesn't look like there is a reason in extracting There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. beanFactory variable has been removed and code has been updated to single line |
||
} | ||
|
||
@Override | ||
@Nullable | ||
protected Object exchange(Object uri, HttpMethod httpMethod, HttpEntity<?> httpRequest, | ||
|
@@ -230,6 +250,12 @@ private WebClient.RequestBodySpec createRequestBodySpec(Object uri, HttpMethod h | |
} | ||
|
||
requestSpec = requestSpec.headers(headers -> headers.putAll(httpRequest.getHeaders())); | ||
|
||
if(attributeVariablesExpression != null) { | ||
Map<String, Object> attributeMap = evaluateAttributeVariables(requestMessage); | ||
requestSpec = requestSpec.attributes(map -> map.putAll(attributeMap)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's do some optimization not calling this method if |
||
} | ||
|
||
BodyInserter<?, ? super ClientHttpRequest> inserter = buildBodyInserterForRequest(requestMessage, httpRequest); | ||
if (inserter != null) { | ||
requestSpec.body(inserter); | ||
|
@@ -369,4 +395,7 @@ private Object createReplyFromResponse(Mono<ResponseEntity<Flux<Object>>> respon | |
.map(this::getReply); | ||
} | ||
|
||
private Map<String,Object> evaluateAttributeVariables(Message<?> requestMessage){ | ||
return this.attributeVariablesExpression.getValue(this.evaluationContext, requestMessage, Map.class); | ||
artembilan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
artembilan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,17 +16,19 @@ | |
|
||
package org.springframework.integration.webflux.outbound; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.time.Duration; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
import reactor.test.StepVerifier; | ||
|
||
import org.springframework.beans.factory.BeanFactory; | ||
import org.springframework.core.io.buffer.DataBuffer; | ||
import org.springframework.core.io.buffer.DataBufferFactory; | ||
import org.springframework.core.io.buffer.DataBufferLimitException; | ||
import org.springframework.expression.spel.standard.SpelExpressionParser; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.client.reactive.ClientHttpConnector; | ||
|
@@ -44,13 +46,15 @@ | |
import org.springframework.web.reactive.function.client.WebClient; | ||
import org.springframework.web.reactive.function.client.WebClientResponseException; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
import reactor.test.StepVerifier; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is wrong imports re-order according our new code style. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Import sequence has been updated to follow code style |
||
|
||
/** | ||
* @author Shiliang Li | ||
* @author Artem Bilan | ||
* @author David Graff | ||
* | ||
* @author Jatin Saxena | ||
* @since 5.0 | ||
*/ | ||
class WebFluxRequestExecutingMessageHandlerTests { | ||
|
@@ -214,6 +218,10 @@ void testFluxReply() { | |
reactiveHandler.setExpectedResponseType(String.class); | ||
reactiveHandler.setReplyPayloadToFlux(true); | ||
|
||
reactiveHandler.setAttributeVariablesExpression(new SpelExpressionParser().parseExpression("{name:{first:'Nikola',last:'Tesla'},dob:{day:10,month:'July',year:1856}}")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We prefer no more than There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, doesn't look like you do some verification for those attributes to propagate. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. New test case has been added to check the propagation of attributes. Also code has been changes to keep less than 120 char in one line. |
||
setBeanFactory(reactiveHandler); | ||
reactiveHandler.afterPropertiesSet(); | ||
|
||
reactiveHandler.handleMessage(MessageBuilder.withPayload(Mono.just("hello, world")).build()); | ||
|
||
Message<?> receive = replyChannel.receive(10_000); | ||
|
@@ -376,4 +384,8 @@ void testMaxInMemorySizeExceeded() { | |
.isEqualTo("Exceeded limit on max bytes to buffer : 1"); | ||
} | ||
|
||
private void setBeanFactory(WebFluxRequestExecutingMessageHandler handler) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure why we need this method, if you simply can do that directly in a line of code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Code has been updated accordingly. |
||
handler.setBeanFactory(mock(BeanFactory.class)); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See
IntegrationNamespaceUtils.createExpressionDefIfAttributeDefined()
instead for convenience.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code has been updated to use IntegrationNamespaceUtils.createExpressionDefIfAttributeDefined()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@saxenaj ,
you know, there is no reason to add this kind of comments if you agree with us: we just simply will see that in the next commit you add to the PR.
Otherwise I got at a bout 10 emails from you instead of just one about a new commit. 😄