Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -264,12 +264,6 @@ private ClientResponse prepareClientResponse(Publisher<? extends DataBuffer> bod
}

private <T> Mono<T> extractBody(ServerWebExchange exchange, ClientResponse clientResponse, Class<T> inClass) {
// if inClass is byte[] then just return body, otherwise check if
// decoding required
if (byte[].class.isAssignableFrom(inClass)) {
return clientResponse.bodyToMono(inClass);
}

List<String> encodingHeaders = exchange.getResponse().getHeaders().getOrEmpty(HttpHeaders.CONTENT_ENCODING);
for (String encoding : encodingHeaders) {
MessageBodyDecoder decoder = messageBodyDecoders.get(encoding);
Expand All @@ -289,10 +283,6 @@ private <T> Mono<T> extractBody(ServerWebExchange exchange, ClientResponse clien
private Mono<DataBuffer> writeBody(ServerHttpResponse httpResponse, CachedBodyOutputMessage message,
Class<?> outClass) {
Mono<DataBuffer> response = DataBufferUtils.join(message.getBody());
if (byte[].class.isAssignableFrom(outClass)) {
return response;
}

List<String> encodingHeaders = httpResponse.getHeaders().getOrEmpty(HttpHeaders.CONTENT_ENCODING);
for (String encoding : encodingHeaders) {
MessageBodyEncoder encoder = messageBodyEncoders.get(encoding);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.springframework.cloud.gateway.filter.factory.rewrite;

import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -55,6 +56,20 @@ void testModificationOfResponseBody() {
.json("{\"length\":25,\"value\":\"\\\"httpbin compatible home\\\"\"}");
}

@Test
void testModificationOfResponseBodyBytes() {
URI uri = UriComponentsBuilder.fromUriString(this.baseUri + "/gzip-byte").build(true).toUri();

testClient.get()
.uri(uri)
.header("Host", "www.modifyresponsebodyjava.org")
.accept(MediaType.APPLICATION_JSON)
.exchange()
.expectBody()
// Expected: the original data ("httpbin compatible home") should be correctly included as a JSON value
.json("{\"is_byte\":true,\"original\":\"httpbin compatible home\"}");
}

@EnableAutoConfiguration
@SpringBootConfiguration
@Import(DefaultTestConfig.class)
Expand All @@ -76,6 +91,15 @@ RouteLocator testRouteLocator(RouteLocatorBuilder builder) {
return Mono.just(modifiedResponse);
}))
.uri(uri))
.route("modify_response_java_test_gzip_byte", r -> r.path("/gzip-byte")
.and()
.host("www.modifyresponsebodyjava.org")
.filters(f -> f.setPath("/gzip").modifyResponseBody(byte[].class, byte[].class, (webExchange, originalResponse) -> {
String s = new String(originalResponse, StandardCharsets.UTF_8);
String modified = "{\"is_byte\":true,\"original\":" + s + "}";
return Mono.just(modified.getBytes(StandardCharsets.UTF_8));
}))
.uri(uri))
.build();
}

Expand Down
Loading