Bumping versions
This commit is contained in:
@@ -144,32 +144,39 @@ public abstract class BodyFilterFunctions {
|
||||
}).orElse(request);
|
||||
}
|
||||
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
public static <T, R> BiFunction<ServerRequest, ServerResponse, ServerResponse> modifyResponseBody(Class<T> inClass, Class<R> outClass,
|
||||
String newContentType, RewriteResponseFunction<T, R> rewriteFunction) {
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
public static <T, R> BiFunction<ServerRequest, ServerResponse, ServerResponse> modifyResponseBody(Class<T> inClass,
|
||||
Class<R> outClass, String newContentType, RewriteResponseFunction<T, R> rewriteFunction) {
|
||||
return (request, response) -> {
|
||||
Object o = request.attributes().get(MvcUtils.CLIENT_RESPONSE_INPUT_STREAM_ATTR);
|
||||
if (o instanceof InputStream inputStream) {
|
||||
try {
|
||||
List<HttpMessageConverter<?>> converters = request.messageConverters();
|
||||
Optional<HttpMessageConverter<?>> inConverter = converters.stream().filter(c -> c.canRead(inClass, response.headers().getContentType())).findFirst();
|
||||
Optional<HttpMessageConverter<?>> inConverter = converters.stream()
|
||||
.filter(c -> c.canRead(inClass, response.headers().getContentType()))
|
||||
.findFirst();
|
||||
if (inConverter.isEmpty()) {
|
||||
//TODO: throw exception?
|
||||
// TODO: throw exception?
|
||||
return response;
|
||||
}
|
||||
HttpMessageConverter<?> inputConverter = inConverter.get();
|
||||
T input = (T) inputConverter.read((Class)inClass, new SimpleInputMessage(inputStream, response.headers()));
|
||||
T input = (T) inputConverter.read((Class) inClass,
|
||||
new SimpleInputMessage(inputStream, response.headers()));
|
||||
R output = rewriteFunction.apply(request, response, input);
|
||||
|
||||
Optional<HttpMessageConverter<?>> outConverter = converters.stream().filter(c -> c.canWrite(outClass, null)).findFirst();
|
||||
Optional<HttpMessageConverter<?>> outConverter = converters.stream()
|
||||
.filter(c -> c.canWrite(outClass, null))
|
||||
.findFirst();
|
||||
if (outConverter.isEmpty()) {
|
||||
//TODO: throw exception?
|
||||
// TODO: throw exception?
|
||||
return response;
|
||||
}
|
||||
HttpMessageConverter<R> byteConverter = (HttpMessageConverter<R>) outConverter.get();
|
||||
ByteArrayHttpOutputMessage outputMessage = new ByteArrayHttpOutputMessage(response.headers());
|
||||
byteConverter.write(output, null, outputMessage);
|
||||
request.attributes().put(MvcUtils.CLIENT_RESPONSE_INPUT_STREAM_ATTR, new ByteArrayInputStream(outputMessage.body.toByteArray()));
|
||||
request.attributes()
|
||||
.put(MvcUtils.CLIENT_RESPONSE_INPUT_STREAM_ATTR,
|
||||
new ByteArrayInputStream(outputMessage.body.toByteArray()));
|
||||
if (StringUtils.hasText(newContentType)) {
|
||||
response.headers().setContentType(MediaType.parseMediaType(newContentType));
|
||||
}
|
||||
@@ -184,7 +191,9 @@ public abstract class BodyFilterFunctions {
|
||||
}
|
||||
|
||||
private final static class SimpleInputMessage implements HttpInputMessage {
|
||||
|
||||
private final InputStream inputStream;
|
||||
|
||||
private final HttpHeaders headers;
|
||||
|
||||
private SimpleInputMessage(InputStream inputStream, HttpHeaders headers) {
|
||||
@@ -201,6 +210,7 @@ public abstract class BodyFilterFunctions {
|
||||
public HttpHeaders getHeaders() {
|
||||
return this.headers;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private final static class ByteArrayHttpOutputMessage implements HttpOutputMessage {
|
||||
@@ -235,7 +245,9 @@ public abstract class BodyFilterFunctions {
|
||||
}
|
||||
|
||||
public interface RewriteResponseFunction<T, R> {
|
||||
|
||||
R apply(ServerRequest request, ServerResponse response, T t);
|
||||
|
||||
}
|
||||
|
||||
private static class ByteArrayServletInputStream extends ServletInputStream {
|
||||
|
||||
@@ -52,38 +52,40 @@ public class BodyFilterFunctionsTests {
|
||||
@Test
|
||||
public void modifyResponseBodySimple() {
|
||||
restClient.get()
|
||||
.uri("/anything/modifyresponsebodysimple")
|
||||
.header("X-Foo", "fooval")
|
||||
.exchange()
|
||||
.expectStatus()
|
||||
.isOk()
|
||||
.expectBody(Map.class)
|
||||
.consumeWith(res -> {
|
||||
Map<String, Object> headers = getMap(res.getResponseBody(), "headers");
|
||||
assertThat(headers).containsEntry("X-Foo", "FOOVAL");
|
||||
});
|
||||
.uri("/anything/modifyresponsebodysimple")
|
||||
.header("X-Foo", "fooval")
|
||||
.exchange()
|
||||
.expectStatus()
|
||||
.isOk()
|
||||
.expectBody(Map.class)
|
||||
.consumeWith(res -> {
|
||||
Map<String, Object> headers = getMap(res.getResponseBody(), "headers");
|
||||
assertThat(headers).containsEntry("X-Foo", "FOOVAL");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void modifyResponseBodyComplex() {
|
||||
restClient.get()
|
||||
.uri("/deny")
|
||||
.header("X-Foo", "fooval")
|
||||
.exchange()
|
||||
.expectStatus()
|
||||
.isOk()
|
||||
// deny returns text/plain
|
||||
.expectHeader().contentType(MediaType.APPLICATION_JSON)
|
||||
.expectBody(Message.class)
|
||||
.consumeWith(res -> {
|
||||
assertThat(res.getResponseBody().message()).isNotEmpty();
|
||||
});
|
||||
.uri("/deny")
|
||||
.header("X-Foo", "fooval")
|
||||
.exchange()
|
||||
.expectStatus()
|
||||
.isOk()
|
||||
// deny returns text/plain
|
||||
.expectHeader()
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.expectBody(Message.class)
|
||||
.consumeWith(res -> {
|
||||
assertThat(res.getResponseBody().message()).isNotEmpty();
|
||||
});
|
||||
}
|
||||
|
||||
@SpringBootConfiguration
|
||||
@EnableAutoConfiguration
|
||||
@LoadBalancerClient(name = "httpbin", configuration = TestLoadBalancerConfig.Httpbin.class)
|
||||
protected static class TestConfiguration {
|
||||
|
||||
@Bean
|
||||
public RouterFunction<ServerResponse> gatewayRouterFunctionsModifyResponseBodySimple() {
|
||||
// @formatter:off
|
||||
@@ -107,9 +109,11 @@ public class BodyFilterFunctionsTests {
|
||||
.build();
|
||||
// @formatter:on
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
record Message(String message) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user