Skip to content

Commit eb520d4

Browse files
committed
Merge branch '4.1.x'
2 parents 3a2d820 + 5b73fdb commit eb520d4

File tree

6 files changed

+238
-7
lines changed

6 files changed

+238
-7
lines changed

spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/GatewayServerMvcAutoConfiguration.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,9 @@ public RestClientCustomizer gatewayRestClientCustomizer(ClientHttpRequestFactory
8888

8989
@Bean
9090
@ConditionalOnMissingBean(ProxyExchange.class)
91-
public RestClientProxyExchange restClientProxyExchange(RestClient.Builder restClientBuilder) {
92-
return new RestClientProxyExchange(restClientBuilder.build());
91+
public RestClientProxyExchange restClientProxyExchange(RestClient.Builder restClientBuilder,
92+
GatewayMvcProperties properties) {
93+
return new RestClientProxyExchange(restClientBuilder.build(), properties);
9394
}
9495

9596
@Bean
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2013-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.gateway.server.mvc.common;
18+
19+
import java.io.IOException;
20+
import java.io.InputStream;
21+
import java.io.OutputStream;
22+
23+
import org.springframework.cloud.gateway.server.mvc.config.GatewayMvcProperties;
24+
import org.springframework.cloud.gateway.server.mvc.handler.ProxyExchange;
25+
import org.springframework.http.client.ClientHttpResponse;
26+
import org.springframework.util.Assert;
27+
import org.springframework.util.StreamUtils;
28+
29+
public abstract class AbstractProxyExchange implements ProxyExchange {
30+
31+
private final GatewayMvcProperties properties;
32+
33+
protected AbstractProxyExchange(GatewayMvcProperties properties) {
34+
this.properties = properties;
35+
}
36+
37+
protected int copyResponseBody(ClientHttpResponse clientResponse, InputStream inputStream,
38+
OutputStream outputStream) throws IOException {
39+
Assert.notNull(clientResponse, "No ClientResponse specified");
40+
Assert.notNull(inputStream, "No InputStream specified");
41+
Assert.notNull(outputStream, "No OutputStream specified");
42+
43+
int transferredBytes;
44+
45+
if (properties.getStreamingMediaTypes().contains(clientResponse.getHeaders().getContentType())) {
46+
transferredBytes = copyResponseBodyWithFlushing(inputStream, outputStream);
47+
}
48+
else {
49+
transferredBytes = StreamUtils.copy(inputStream, outputStream);
50+
}
51+
52+
return transferredBytes;
53+
}
54+
55+
private int copyResponseBodyWithFlushing(InputStream inputStream, OutputStream outputStream) throws IOException {
56+
int readBytes;
57+
var totalReadBytes = 0;
58+
var buffer = new byte[properties.getStreamingBufferSize()];
59+
60+
while ((readBytes = inputStream.read(buffer)) != -1) {
61+
outputStream.write(buffer, 0, readBytes);
62+
outputStream.flush();
63+
if (totalReadBytes < Integer.MAX_VALUE) {
64+
try {
65+
totalReadBytes = Math.addExact(totalReadBytes, readBytes);
66+
}
67+
catch (ArithmeticException e) {
68+
totalReadBytes = Integer.MAX_VALUE;
69+
}
70+
}
71+
}
72+
73+
outputStream.flush();
74+
75+
return totalReadBytes;
76+
}
77+
78+
}

spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/config/GatewayMvcProperties.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.time.Duration;
2020
import java.util.ArrayList;
21+
import java.util.Arrays;
2122
import java.util.LinkedHashMap;
2223
import java.util.List;
2324

@@ -26,6 +27,7 @@
2627

2728
import org.springframework.boot.context.properties.ConfigurationProperties;
2829
import org.springframework.core.style.ToStringCreator;
30+
import org.springframework.http.MediaType;
2931

3032
@ConfigurationProperties(GatewayMvcProperties.PREFIX)
3133
public class GatewayMvcProperties {
@@ -51,6 +53,18 @@ public class GatewayMvcProperties {
5153

5254
private HttpClient httpClient = new HttpClient();
5355

56+
/**
57+
* Mime-types that are streaming.
58+
*/
59+
private List<MediaType> streamingMediaTypes = Arrays.asList(MediaType.TEXT_EVENT_STREAM,
60+
new MediaType("application", "stream+json"), new MediaType("application", "grpc"),
61+
new MediaType("application", "grpc+protobuf"), new MediaType("application", "grpc+json"));
62+
63+
/**
64+
* Buffer size for streaming media mime-types.
65+
*/
66+
private int streamingBufferSize = 16384;
67+
5468
public List<RouteProperties> getRoutes() {
5569
return routes;
5670
}
@@ -71,11 +85,29 @@ public HttpClient getHttpClient() {
7185
return httpClient;
7286
}
7387

88+
public List<MediaType> getStreamingMediaTypes() {
89+
return streamingMediaTypes;
90+
}
91+
92+
public void setStreamingMediaTypes(List<MediaType> streamingMediaTypes) {
93+
this.streamingMediaTypes = streamingMediaTypes;
94+
}
95+
96+
public int getStreamingBufferSize() {
97+
return streamingBufferSize;
98+
}
99+
100+
public void setStreamingBufferSize(int streamingBufferSize) {
101+
this.streamingBufferSize = streamingBufferSize;
102+
}
103+
74104
@Override
75105
public String toString() {
76106
return new ToStringCreator(this).append("httpClient", httpClient)
77107
.append("routes", routes)
78108
.append("routesMap", routesMap)
109+
.append("streamingMediaTypes", streamingMediaTypes)
110+
.append("streamingBufferSize", streamingBufferSize)
79111
.toString();
80112
}
81113

spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/handler/ClientHttpRequestFactoryProxyExchange.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,28 @@
2020
import java.io.InputStream;
2121
import java.io.UncheckedIOException;
2222

23+
import org.springframework.cloud.gateway.server.mvc.common.AbstractProxyExchange;
2324
import org.springframework.cloud.gateway.server.mvc.common.MvcUtils;
25+
import org.springframework.cloud.gateway.server.mvc.config.GatewayMvcProperties;
2426
import org.springframework.http.client.ClientHttpRequest;
2527
import org.springframework.http.client.ClientHttpRequestFactory;
2628
import org.springframework.http.client.ClientHttpResponse;
2729
import org.springframework.util.StreamUtils;
2830
import org.springframework.web.servlet.function.ServerResponse;
2931

30-
public class ClientHttpRequestFactoryProxyExchange implements ProxyExchange {
32+
public class ClientHttpRequestFactoryProxyExchange extends AbstractProxyExchange {
3133

3234
private final ClientHttpRequestFactory requestFactory;
3335

36+
@Deprecated
3437
public ClientHttpRequestFactoryProxyExchange(ClientHttpRequestFactory requestFactory) {
38+
super(new GatewayMvcProperties());
39+
this.requestFactory = requestFactory;
40+
}
41+
42+
public ClientHttpRequestFactoryProxyExchange(ClientHttpRequestFactory requestFactory,
43+
GatewayMvcProperties properties) {
44+
super(properties);
3545
this.requestFactory = requestFactory;
3646
}
3747

@@ -54,7 +64,8 @@ public ServerResponse exchange(Request request) {
5464
InputStream inputStream = MvcUtils.getAttribute(request.getServerRequest(),
5565
MvcUtils.CLIENT_RESPONSE_INPUT_STREAM_ATTR);
5666
// copy body from request to clientHttpRequest
57-
StreamUtils.copy(inputStream, httpServletResponse.getOutputStream());
67+
ClientHttpRequestFactoryProxyExchange.this.copyResponseBody(clientHttpResponse, inputStream,
68+
httpServletResponse.getOutputStream());
5869
}
5970
return null;
6071
});

spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/handler/RestClientProxyExchange.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,26 @@
2121
import java.io.OutputStream;
2222
import java.io.UncheckedIOException;
2323

24+
import org.springframework.cloud.gateway.server.mvc.common.AbstractProxyExchange;
2425
import org.springframework.cloud.gateway.server.mvc.common.MvcUtils;
26+
import org.springframework.cloud.gateway.server.mvc.config.GatewayMvcProperties;
2527
import org.springframework.http.client.ClientHttpResponse;
2628
import org.springframework.util.StreamUtils;
2729
import org.springframework.web.client.RestClient;
2830
import org.springframework.web.servlet.function.ServerResponse;
2931

30-
public class RestClientProxyExchange implements ProxyExchange {
32+
public class RestClientProxyExchange extends AbstractProxyExchange {
3133

3234
private final RestClient restClient;
3335

36+
@Deprecated
3437
public RestClientProxyExchange(RestClient restClient) {
38+
super(new GatewayMvcProperties());
39+
this.restClient = restClient;
40+
}
41+
42+
public RestClientProxyExchange(RestClient restClient, GatewayMvcProperties properties) {
43+
super(properties);
3544
this.restClient = restClient;
3645
}
3746

@@ -59,7 +68,7 @@ private static int copyBody(Request request, OutputStream outputStream) throws I
5968
return StreamUtils.copy(request.getServerRequest().servletRequest().getInputStream(), outputStream);
6069
}
6170

62-
private static ServerResponse doExchange(Request request, ClientHttpResponse clientResponse) throws IOException {
71+
private ServerResponse doExchange(Request request, ClientHttpResponse clientResponse) throws IOException {
6372
InputStream body = clientResponse.getBody();
6473
// put the body input stream in a request attribute so filters can read it.
6574
MvcUtils.putAttribute(request.getServerRequest(), MvcUtils.CLIENT_RESPONSE_INPUT_STREAM_ATTR, body);
@@ -71,7 +80,8 @@ private static ServerResponse doExchange(Request request, ClientHttpResponse cli
7180
InputStream inputStream = MvcUtils.getAttribute(request.getServerRequest(),
7281
MvcUtils.CLIENT_RESPONSE_INPUT_STREAM_ATTR);
7382
// copy body from request to clientHttpRequest
74-
StreamUtils.copy(inputStream, httpServletResponse.getOutputStream());
83+
RestClientProxyExchange.this.copyResponseBody(clientResponse, inputStream,
84+
httpServletResponse.getOutputStream());
7585
}
7686
return null;
7787
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright 2013-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.gateway.server.mvc.common;
18+
19+
import java.io.IOException;
20+
import java.io.InputStream;
21+
import java.io.OutputStream;
22+
23+
import org.junit.jupiter.api.Test;
24+
25+
import org.springframework.cloud.gateway.server.mvc.config.GatewayMvcProperties;
26+
import org.springframework.http.MediaType;
27+
import org.springframework.mock.http.client.MockClientHttpResponse;
28+
import org.springframework.web.servlet.function.ServerResponse;
29+
30+
import static org.assertj.core.api.Assertions.assertThat;
31+
import static org.mockito.ArgumentMatchers.any;
32+
import static org.mockito.Mockito.mock;
33+
import static org.mockito.Mockito.times;
34+
import static org.mockito.Mockito.verify;
35+
import static org.mockito.Mockito.when;
36+
37+
/**
38+
* @author Jens Mallien
39+
*/
40+
public class AbstractProxyExchangeTests {
41+
42+
@Test
43+
public void copyResponseBodyForJson() throws IOException {
44+
MockClientHttpResponse mockResponse = new MockClientHttpResponse(new byte[0], 200);
45+
mockResponse.getHeaders().setContentType(MediaType.APPLICATION_JSON);
46+
47+
InputStream inputStream = mock(InputStream.class);
48+
when(inputStream.transferTo(any())).thenReturn(3L);
49+
OutputStream outputStream = mock(OutputStream.class);
50+
51+
int result = new TestProxyExchange().copyResponseBody(mockResponse, inputStream, outputStream);
52+
53+
assertThat(result).isEqualTo(3);
54+
verify(outputStream, times(1)).flush();
55+
}
56+
57+
@Test
58+
public void copyResponseBodyForTextEventStream() throws IOException {
59+
MockClientHttpResponse mockResponse = new MockClientHttpResponse(new byte[0], 200);
60+
mockResponse.getHeaders().setContentType(MediaType.TEXT_EVENT_STREAM);
61+
62+
InputStream inputStream = mock(InputStream.class);
63+
when(inputStream.read(any())).thenReturn(1).thenReturn(1).thenReturn(1).thenReturn(-1);
64+
OutputStream outputStream = mock(OutputStream.class);
65+
66+
int result = new TestProxyExchange().copyResponseBody(mockResponse, inputStream, outputStream);
67+
68+
assertThat(result).isEqualTo(3);
69+
verify(outputStream, times(4)).flush();
70+
}
71+
72+
@Test
73+
public void copyResponseBodyWithoutContentType() throws IOException {
74+
MockClientHttpResponse mockResponse = new MockClientHttpResponse(new byte[0], 200);
75+
76+
InputStream inputStream = mock(InputStream.class);
77+
when(inputStream.transferTo(any())).thenReturn(3L);
78+
OutputStream outputStream = mock(OutputStream.class);
79+
80+
int result = new TestProxyExchange().copyResponseBody(mockResponse, inputStream, outputStream);
81+
82+
assertThat(result).isEqualTo(3);
83+
verify(outputStream, times(1)).flush();
84+
}
85+
86+
class TestProxyExchange extends AbstractProxyExchange {
87+
88+
protected TestProxyExchange() {
89+
super(new GatewayMvcProperties());
90+
}
91+
92+
@Override
93+
public ServerResponse exchange(Request request) {
94+
return ServerResponse.ok().build();
95+
}
96+
97+
}
98+
99+
}

0 commit comments

Comments
 (0)