Refactor to use SpEL in function properties
* Clean up per review
This commit is contained in:
@@ -1,86 +0,0 @@
|
||||
/*
|
||||
* Copyright 2020-2020 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://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.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.stream.app.processor.http.request;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
|
||||
import static org.springframework.cloud.fn.http.request.HttpRequestFunctionConfiguration.HttpRequestFunction;
|
||||
|
||||
@Configuration
|
||||
@EnableConfigurationProperties({ HttpRequestProcessorProperties.class })
|
||||
public class HttpRequestProcessorConfiguration {
|
||||
|
||||
private static Log log = LogFactory.getLog(HttpRequestProcessorConfiguration.class);
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(WebClient.class)
|
||||
public WebClient webClient() {
|
||||
return WebClient.builder()
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
HttpRequestFunctionFactory httpRequestFunctionFactory(WebClient webClient,
|
||||
HttpRequestProcessorProperties properties) {
|
||||
return new HttpRequestFunctionFactory(webClient, properties);
|
||||
}
|
||||
|
||||
@Bean
|
||||
Function<Message<?>, ?> httpRequestProcessor(HttpRequestFunctionFactory httpRequestFunctionFactory,
|
||||
HttpRequestProcessorProperties properties) {
|
||||
|
||||
return message -> Mono.from(httpRequestFunctionFactory.getHttpRequestFunction(message).apply(Flux.just(message))
|
||||
.map(responseEntity -> properties.getReplyExpression().getValue(responseEntity)))
|
||||
.doOnError(e -> log.error(e.getMessage(), e))
|
||||
.block();
|
||||
}
|
||||
|
||||
static class HttpRequestFunctionFactory {
|
||||
|
||||
private final WebClient webClient;
|
||||
|
||||
private final HttpRequestProcessorProperties properties;
|
||||
|
||||
private final HttpRequestFunction instance;
|
||||
|
||||
HttpRequestFunctionFactory(WebClient webClient, HttpRequestProcessorProperties properties) {
|
||||
this.properties = properties;
|
||||
this.webClient = webClient;
|
||||
this.instance = properties.usesRequestExpressions() ? null
|
||||
: new HttpRequestFunction(webClient, properties);
|
||||
}
|
||||
|
||||
HttpRequestFunction getHttpRequestFunction(Message<?> message) {
|
||||
if (instance != null) {
|
||||
return instance;
|
||||
}
|
||||
return new HttpRequestFunction(webClient, properties.evaluateFunctionProperties(message));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,254 +0,0 @@
|
||||
/*
|
||||
* Copyright 2020-2020 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://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.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.stream.app.processor.http.request;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import javax.validation.constraints.AssertTrue;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.cloud.fn.http.request.HttpRequestFunctionProperties;
|
||||
import org.springframework.cloud.fn.http.request.HttpRequestProperties;
|
||||
import org.springframework.expression.Expression;
|
||||
import org.springframework.expression.spel.standard.SpelExpressionParser;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
/**
|
||||
* Configuration properties for the Http Request Processor module.
|
||||
*
|
||||
* @author Waldemar Hummer
|
||||
* @author Mark Fisher
|
||||
* @author Christian Tzolov
|
||||
* @author Artem Bilan
|
||||
* @author David Turanski
|
||||
*/
|
||||
|
||||
@Validated
|
||||
@ConfigurationProperties("http.request.processor")
|
||||
public class HttpRequestProcessorProperties implements HttpRequestProperties {
|
||||
|
||||
private static final HttpMethod DEFAULT_HTTP_METHOD = HttpMethod.GET;
|
||||
|
||||
private static final Class<?> DEFAULT_RESPONSE_TYPE = String.class;
|
||||
|
||||
/**
|
||||
* The URL to issue an http request to, as a static value.
|
||||
*/
|
||||
private String url;
|
||||
|
||||
/**
|
||||
* The (static) request body; if neither this nor bodyExpression is provided, the payload
|
||||
* will be used.
|
||||
*/
|
||||
private Object body;
|
||||
|
||||
/**
|
||||
* The kind of http method to use.
|
||||
*/
|
||||
private HttpMethod httpMethod = DEFAULT_HTTP_METHOD;
|
||||
|
||||
/**
|
||||
* The type used to interpret the response.
|
||||
*/
|
||||
private Class<?> expectedResponseType = DEFAULT_RESPONSE_TYPE;
|
||||
|
||||
/**
|
||||
* Request timeout in milliseconds.
|
||||
*/
|
||||
private long timeout = 30_000;
|
||||
|
||||
/**
|
||||
* A Map of HTTP request headers.
|
||||
*/
|
||||
private HttpHeaders headers = new HttpHeaders();
|
||||
|
||||
/**
|
||||
* A SpEL expression against incoming message to determine the URL to use.
|
||||
*/
|
||||
private Expression urlExpression;
|
||||
|
||||
/**
|
||||
* A SpEL expression to derive the request method from the incoming message.
|
||||
*/
|
||||
private Expression httpMethodExpression;
|
||||
|
||||
/**
|
||||
* A SpEL expression to derive the request body from the incoming message.
|
||||
*/
|
||||
private Expression bodyExpression;
|
||||
|
||||
/**
|
||||
* A SpEL expression used to derive the http headers map to use.
|
||||
*/
|
||||
private Expression headersExpression;
|
||||
|
||||
/**
|
||||
* A SpEL expression used to compute the final result, applied against the whole http
|
||||
* {@link org.springframework.http.ResponseEntity}.
|
||||
*/
|
||||
private Expression replyExpression = new SpelExpressionParser().parseExpression("body");
|
||||
|
||||
public Expression getUrlExpression() {
|
||||
return urlExpression;
|
||||
}
|
||||
|
||||
public void setUrlExpression(Expression urlExpression) {
|
||||
this.urlExpression = urlExpression;
|
||||
}
|
||||
|
||||
public Expression getHttpMethodExpression() {
|
||||
return httpMethodExpression;
|
||||
}
|
||||
|
||||
public void setHttpMethodExpression(Expression httpMethodExpression) {
|
||||
this.setHttpMethod(null);
|
||||
this.httpMethodExpression = httpMethodExpression;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUrl() {
|
||||
return this.url;
|
||||
}
|
||||
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpMethod getHttpMethod() {
|
||||
return httpMethod;
|
||||
}
|
||||
|
||||
public void setHttpMethod(HttpMethod httpMethod) {
|
||||
this.httpMethod = httpMethod;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Class<?> getExpectedResponseType() {
|
||||
return expectedResponseType;
|
||||
}
|
||||
|
||||
public void setExpectedResponseType(Class<?> expectedResponseType) {
|
||||
this.expectedResponseType = expectedResponseType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getBody() {
|
||||
return body;
|
||||
}
|
||||
|
||||
public void setBody(Object body) {
|
||||
this.body = body;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getTimeout() {
|
||||
return this.timeout;
|
||||
}
|
||||
|
||||
public void setTimeout(long timeout) {
|
||||
this.timeout = timeout;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpHeaders getHeaders() {
|
||||
return headers;
|
||||
}
|
||||
|
||||
public void setHeaders(HttpHeaders headers) {
|
||||
this.headers = headers;
|
||||
}
|
||||
|
||||
public Expression getBodyExpression() {
|
||||
return bodyExpression;
|
||||
}
|
||||
|
||||
public void setBodyExpression(Expression bodyExpression) {
|
||||
this.bodyExpression = bodyExpression;
|
||||
}
|
||||
|
||||
public Expression getHeadersExpression() {
|
||||
return headersExpression;
|
||||
}
|
||||
|
||||
public void setHeadersExpression(Expression headersExpression) {
|
||||
this.headersExpression = headersExpression;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Expression getReplyExpression() {
|
||||
return replyExpression;
|
||||
}
|
||||
|
||||
public void setReplyExpression(Expression replyExpression) {
|
||||
this.replyExpression = replyExpression;
|
||||
}
|
||||
|
||||
@AssertTrue(message = "Exactly one of 'url' or 'urlExpression' is required")
|
||||
public boolean isExactlyOneUrl() {
|
||||
return getUrl() == null ^ urlExpression == null;
|
||||
}
|
||||
|
||||
@AssertTrue(message = "At most one of 'body' or 'bodyExpression' is allowed")
|
||||
public boolean isAtMostOneBody() {
|
||||
return getBody() == null || bodyExpression == null;
|
||||
}
|
||||
|
||||
@AssertTrue(message = "At most one of 'httpMethod' or 'httpMethodExpression' is allowed")
|
||||
public boolean isAtMostOneHttpMethod() {
|
||||
return getHttpMethod() == null || httpMethodExpression == null;
|
||||
}
|
||||
|
||||
public boolean usesRequestExpressions() {
|
||||
return headersExpression != null ||
|
||||
bodyExpression != null ||
|
||||
httpMethodExpression != null ||
|
||||
urlExpression != null;
|
||||
}
|
||||
|
||||
HttpRequestFunctionProperties evaluateFunctionProperties(Message<?> message) {
|
||||
HttpRequestFunctionProperties properties = new HttpRequestFunctionProperties();
|
||||
properties.setUrl(urlExpression != null ? urlExpression.getValue(message, String.class) : getUrl());
|
||||
properties.setBody(bodyExpression != null ? bodyExpression.getValue(message) : getBody());
|
||||
properties.setHttpMethod(httpMethodExpression != null ? httpMethodExpression.getValue(message, HttpMethod.class)
|
||||
: getHttpMethod());
|
||||
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.addAll(getHeaders());
|
||||
if (headersExpression != null) {
|
||||
Map<?, ?> headersMap = headersExpression.getValue(message, Map.class);
|
||||
for (Map.Entry<?, ?> header : headersMap.entrySet()) {
|
||||
if (header.getKey() != null && header.getValue() != null) {
|
||||
headers.add(header.getKey().toString(),
|
||||
header.getValue().toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
properties.setHeaders(headers);
|
||||
|
||||
properties.setTimeout(getTimeout());
|
||||
|
||||
properties.setExpectedResponseType(getExpectedResponseType());
|
||||
|
||||
return properties;
|
||||
}
|
||||
}
|
||||
@@ -1 +1,2 @@
|
||||
configuration-properties.classes=org.springframework.cloud.stream.app.processor.http.request.HttpRequestProcessorProperties
|
||||
configuration-properties.classes=org.springframework.cloud.fn.http.request.HttpRequestFunctionProperties
|
||||
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
configuration-properties.classes=org.springframework.cloud.stream.app.processor.http.request.HttpRequestProcessorProperties
|
||||
configuration-properties.classes=org.springframework.cloud.fn.http.request.HttpRequestFunctionProperties
|
||||
|
||||
|
||||
@@ -32,9 +32,11 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.cloud.fn.http.request.HttpRequestFunctionConfiguration;
|
||||
import org.springframework.cloud.stream.binder.test.InputDestination;
|
||||
import org.springframework.cloud.stream.binder.test.OutputDestination;
|
||||
import org.springframework.cloud.stream.binder.test.TestChannelBinderConfiguration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.messaging.Message;
|
||||
@@ -42,7 +44,6 @@ import org.springframework.messaging.MessageHeaders;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
||||
|
||||
public class HttpRequestProcessorTests {
|
||||
|
||||
@@ -79,14 +80,14 @@ public class HttpRequestProcessorTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
void requestUsingExpressions() throws IOException {
|
||||
void requestUsingExpressions() {
|
||||
applicationContextRunner
|
||||
.withPropertyValues(
|
||||
"http.request.processor.url-expression=headers['url']",
|
||||
"http.request.processor.http-method-expression=headers['method']",
|
||||
"http.request.processor.body-expression=headers['body']",
|
||||
"http.request.processor.headers-expression={Accept:'application/json'}",
|
||||
"http.request.processor.reply-expression=#root")
|
||||
"http.request.reply-expression=#root",
|
||||
"http.request.url-expression=headers['url']",
|
||||
"http.request.http-method-expression=headers['method']",
|
||||
"http.request.body-expression=headers['body']"
|
||||
)
|
||||
.run(context -> {
|
||||
Message<?> message = MessageBuilder.withPayload("")
|
||||
.setHeader("url", url())
|
||||
@@ -98,13 +99,11 @@ public class HttpRequestProcessorTests {
|
||||
ObjectMapper objectMapper = context.getBean(ObjectMapper.class);
|
||||
|
||||
inputDestination.send(message);
|
||||
Message<byte[]> reply = outputDestination.receive(100);
|
||||
Message<byte[]> reply = outputDestination.receive(10000);
|
||||
|
||||
// Cannot deserialize ResponseEntity directly.
|
||||
Map responseEntityAsMap = objectMapper.readValue(reply.getPayload(), HashMap.class);
|
||||
|
||||
System.out.println(responseEntityAsMap);
|
||||
|
||||
assertThat(responseEntityAsMap.get("statusCode")).isEqualTo("OK");
|
||||
assertThat(responseEntityAsMap.get("body")).isEqualTo(message.getHeaders().get("body"));
|
||||
assertThat(reply.getHeaders().get(MessageHeaders.CONTENT_TYPE))
|
||||
@@ -116,11 +115,11 @@ public class HttpRequestProcessorTests {
|
||||
void requestUsingReturnType() throws IOException {
|
||||
applicationContextRunner
|
||||
.withPropertyValues(
|
||||
"http.request.processor.url=" + url(),
|
||||
"http.request.processor.httpMethod=POST",
|
||||
"http.request.processor.headers[Accept]=application/octet-stream",
|
||||
"http.request.processor.expectedResponseType=byte[]",
|
||||
"spring.cloud.stream.bindings.httpRequestProcessor-out-0.contentType=application/octet-stream")
|
||||
"http.request.url-expression='" + url() + "'",
|
||||
"http.request.http-method-expression='POST'",
|
||||
"http.request.headers-expression={Accept:'application/octet-stream'}",
|
||||
"http.request.expected-response-type=byte[]",
|
||||
"spring.cloud.stream.bindings.httpRequestFunction-out-0.contentType=application/octet-stream")
|
||||
.run(context -> {
|
||||
Message<?> message = MessageBuilder.withPayload("hello")
|
||||
.build();
|
||||
@@ -128,7 +127,7 @@ public class HttpRequestProcessorTests {
|
||||
OutputDestination outputDestination = context.getBean(OutputDestination.class);
|
||||
|
||||
inputDestination.send(message);
|
||||
Message<byte[]> reply = outputDestination.receive(100);
|
||||
Message<byte[]> reply = outputDestination.receive(10000);
|
||||
assertThat(new String(reply.getPayload())).isEqualTo(message.getPayload());
|
||||
assertThat(reply.getHeaders().get(MessageHeaders.CONTENT_TYPE))
|
||||
.isEqualTo(MediaType.APPLICATION_OCTET_STREAM);
|
||||
@@ -139,8 +138,8 @@ public class HttpRequestProcessorTests {
|
||||
void requestUsingJsonPathMethodExpression() throws IOException {
|
||||
applicationContextRunner
|
||||
.withPropertyValues(
|
||||
"http.request.processor.url=" + url(),
|
||||
"http.request.processor.httpMethodExpression=#jsonPath(payload,'$.myMethod')")
|
||||
"http.request.url-expression='" + url() + "'",
|
||||
"http.request.http-method-expression=#jsonPath(payload,'$.myMethod')")
|
||||
.run(context -> {
|
||||
Message<?> message = MessageBuilder
|
||||
.withPayload("{\"name\":\"Fred\",\"age\":41, \"myMethod\":\"POST\"}")
|
||||
@@ -149,48 +148,13 @@ public class HttpRequestProcessorTests {
|
||||
OutputDestination outputDestination = context.getBean(OutputDestination.class);
|
||||
|
||||
inputDestination.send(message);
|
||||
Message<byte[]> reply = outputDestination.receive(100);
|
||||
Message<byte[]> reply = outputDestination.receive(10000);
|
||||
assertThat(new String(reply.getPayload())).isEqualTo(message.getPayload());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void cannotSpecifyBothUrlandUrlExpression() {
|
||||
applicationContextRunner
|
||||
.withPropertyValues("http.request.processor.url=http://example.com",
|
||||
"http.request.processor.url-expression=headers['url']")
|
||||
.run(context -> {
|
||||
assertThatIllegalStateException().isThrownBy(() -> {
|
||||
context.start();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void cannotSpecifyBothHttpMethosdandHttpMethodExpression() {
|
||||
applicationContextRunner
|
||||
.withPropertyValues("http.request.processor.http-method=POST",
|
||||
"http.request.processor.http-method-expression=headers['method']")
|
||||
.run(context -> {
|
||||
assertThatIllegalStateException().isThrownBy(() -> {
|
||||
context.start();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void cannotSpecifyBothHeadersAndHeadersExpression() {
|
||||
applicationContextRunner
|
||||
.withPropertyValues("http.request.processor.headers[Content-Type]=application/json",
|
||||
"http.request.processor.headers-expression={'Content-Type': headers['content']}")
|
||||
.run(context -> {
|
||||
assertThatIllegalStateException().isThrownBy(() -> {
|
||||
context.start();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@SpringBootApplication
|
||||
@Import(HttpRequestFunctionConfiguration.class)
|
||||
static class HttpRequestProcessorApp {
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user