INT-4350: Handle ResponseEntity in HTTP Inbounds
JIRA: https://jira.spring.io/browse/INT-4350 Spring MVC and Spring WebFlux handles `ResponseEntity` via appropriate `ReturnValue` handlers. This way all the headers and status code are fully up to end-user. The body is handled by the appropriate converter/writer as before * Add `ResponseEntity` handling to the `HttpRequestHandlingMessagingGateway` and `WebFluxInboundEndpoint`. The logic mostly compy/pasted from the `ResponseEntityResultHandler`
This commit is contained in:
committed by
Gary Russell
parent
008a740fb0
commit
6d96914c1f
@@ -16,7 +16,9 @@
|
||||
|
||||
package org.springframework.integration.webflux.inbound;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.LinkedHashSet;
|
||||
@@ -35,8 +37,10 @@ import org.springframework.expression.Expression;
|
||||
import org.springframework.expression.spel.support.StandardEvaluationContext;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.codec.HttpMessageReader;
|
||||
import org.springframework.http.codec.HttpMessageWriter;
|
||||
import org.springframework.http.codec.ServerCodecConfigurer;
|
||||
@@ -75,6 +79,8 @@ public class WebFluxInboundEndpoint extends BaseHttpInboundEndpoint implements W
|
||||
|
||||
private static final MediaType MEDIA_TYPE_APPLICATION_ALL = new MediaType("application");
|
||||
|
||||
private static final List<HttpMethod> SAFE_METHODS = Arrays.asList(HttpMethod.GET, HttpMethod.HEAD);
|
||||
|
||||
private ServerCodecConfigurer codecConfigurer = ServerCodecConfigurer.create();
|
||||
|
||||
private RequestedContentTypeResolver requestedContentTypeResolver = new HeaderContentTypeResolver();
|
||||
@@ -318,12 +324,44 @@ public class WebFluxInboundEndpoint extends BaseHttpInboundEndpoint implements W
|
||||
return response.setComplete();
|
||||
}
|
||||
else {
|
||||
HttpStatus httpStatus = resolveHttpStatusFromHeaders(replyMessage.getHeaders());
|
||||
final HttpStatus httpStatus = resolveHttpStatusFromHeaders(replyMessage.getHeaders());
|
||||
if (httpStatus != null) {
|
||||
response.setStatusCode(httpStatus);
|
||||
}
|
||||
|
||||
return writeResponseBody(exchange, responseContent);
|
||||
if (responseContent instanceof ResponseEntity) {
|
||||
return Mono.just((ResponseEntity<?>) responseContent)
|
||||
.flatMap(e -> {
|
||||
if (httpStatus == null) {
|
||||
exchange.getResponse().setStatusCode(e.getStatusCode());
|
||||
}
|
||||
|
||||
HttpHeaders entityHeaders = e.getHeaders();
|
||||
HttpHeaders responseHeaders = exchange.getResponse().getHeaders();
|
||||
|
||||
if (!entityHeaders.isEmpty()) {
|
||||
entityHeaders.entrySet().stream()
|
||||
.filter(entry -> !responseHeaders.containsKey(entry.getKey()))
|
||||
.forEach(entry -> responseHeaders.put(entry.getKey(), entry.getValue()));
|
||||
}
|
||||
|
||||
if (e.getBody() == null) {
|
||||
return exchange.getResponse().setComplete();
|
||||
}
|
||||
|
||||
String etag = entityHeaders.getETag();
|
||||
Instant lastModified = Instant.ofEpochMilli(entityHeaders.getLastModified());
|
||||
HttpMethod httpMethod = exchange.getRequest().getMethod();
|
||||
if (SAFE_METHODS.contains(httpMethod) && exchange.checkNotModified(etag, lastModified)) {
|
||||
return exchange.getResponse().setComplete();
|
||||
}
|
||||
|
||||
return writeResponseBody(exchange, e.getBody());
|
||||
});
|
||||
}
|
||||
else {
|
||||
return writeResponseBody(exchange, responseContent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user