Skip to content

Commit

Permalink
Bumping versions
Browse files Browse the repository at this point in the history
  • Loading branch information
spring-builds committed Nov 7, 2024
1 parent 4de8f99 commit d850be3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,32 +144,39 @@ public static <T, R> Function<ServerRequest, ServerRequest> modifyRequestBody(Cl
}).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));
}
Expand All @@ -184,7 +191,9 @@ public static <T, R> BiFunction<ServerRequest, ServerResponse, ServerResponse> m
}

private final static class SimpleInputMessage implements HttpInputMessage {

private final InputStream inputStream;

private final HttpHeaders headers;

private SimpleInputMessage(InputStream inputStream, HttpHeaders headers) {
Expand All @@ -201,6 +210,7 @@ public InputStream getBody() throws IOException {
public HttpHeaders getHeaders() {
return this.headers;
}

}

private final static class ByteArrayHttpOutputMessage implements HttpOutputMessage {
Expand Down Expand Up @@ -235,7 +245,9 @@ public interface RewriteFunction<T, R> extends BiFunction<ServerRequest, T, R> {
}

public interface RewriteResponseFunction<T, R> {

R apply(ServerRequest request, ServerResponse response, T t);

}

private static class ByteArrayServletInputStream extends ServletInputStream {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -107,9 +109,11 @@ public RouterFunction<ServerResponse> gatewayRouterFunctionsModifyResponseBodyCo
.build();
// @formatter:on
}

}

record Message(String message) {

}

}

0 comments on commit d850be3

Please sign in to comment.