Skip to content

Commit 86e28b0

Browse files
committed
add client response attrs
Signed-off-by: jiangyuan <[email protected]>
1 parent 5af2c68 commit 86e28b0

File tree

4 files changed

+13
-7
lines changed

4 files changed

+13
-7
lines changed

Diff for: spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/common/MvcUtils.java

+5
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ public abstract class MvcUtils {
6363
*/
6464
public static final String CLIENT_RESPONSE_INPUT_STREAM_ATTR = qualify("cachedClientResponseBody");
6565

66+
/**
67+
* Client response key.
68+
*/
69+
public static final String CLIENT_RESPONSE_ATTR = qualify("cachedClientResponse");
70+
6671
/**
6772
* CircuitBreaker execution exception attribute name.
6873
*/

Diff for: spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/RetryFilterFunctions.java

+6-7
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.springframework.cloud.gateway.server.mvc.filter;
1818

1919
import java.io.IOException;
20-
import java.io.InputStream;
2120
import java.util.Arrays;
2221
import java.util.HashMap;
2322
import java.util.HashSet;
@@ -34,6 +33,7 @@
3433
import org.springframework.http.HttpMethod;
3534
import org.springframework.http.HttpStatus;
3635
import org.springframework.http.HttpStatusCode;
36+
import org.springframework.http.client.ClientHttpResponse;
3737
import org.springframework.retry.RetryContext;
3838
import org.springframework.retry.RetryPolicy;
3939
import org.springframework.retry.policy.CompositeRetryPolicy;
@@ -76,8 +76,7 @@ public static HandlerFilterFunction<ServerResponse, ServerResponse> retry(RetryC
7676
if (config.isCacheBody()) {
7777
MvcUtils.getOrCacheBody(request);
7878
}
79-
// test ci build
80-
// reset(request);
79+
reset(request);
8180
ServerResponse serverResponse = next.handle(request);
8281

8382
if (isRetryableStatusCode(serverResponse.statusCode(), config)
@@ -90,10 +89,10 @@ && isRetryableMethod(request.method(), config)) {
9089
}
9190

9291
private static void reset(ServerRequest request) throws IOException {
93-
InputStream inputStream = MvcUtils.getAttribute(request, MvcUtils.CLIENT_RESPONSE_INPUT_STREAM_ATTR);
94-
if (inputStream != null) {
95-
inputStream.close();
96-
MvcUtils.putAttribute(request, MvcUtils.CLIENT_RESPONSE_INPUT_STREAM_ATTR, null);
92+
ClientHttpResponse clientHttpResponse = MvcUtils.getAttribute(request, MvcUtils.CLIENT_RESPONSE_ATTR);
93+
if (clientHttpResponse != null) {
94+
clientHttpResponse.close();
95+
MvcUtils.putAttribute(request, MvcUtils.CLIENT_RESPONSE_ATTR, null);
9796
}
9897
}
9998

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

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public ServerResponse exchange(Request request) {
5656
InputStream body = clientHttpResponse.getBody();
5757
// put the body input stream in a request attribute so filters can read it.
5858
MvcUtils.putAttribute(request.getServerRequest(), MvcUtils.CLIENT_RESPONSE_INPUT_STREAM_ATTR, body);
59+
MvcUtils.putAttribute(request.getServerRequest(), MvcUtils.CLIENT_RESPONSE_ATTR, clientHttpResponse);
5960
ServerResponse serverResponse = GatewayServerResponse.status(clientHttpResponse.getStatusCode())
6061
.build((req, httpServletResponse) -> {
6162
try (clientHttpResponse) {

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

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ private ServerResponse doExchange(Request request, ClientHttpResponse clientResp
7272
InputStream body = clientResponse.getBody();
7373
// put the body input stream in a request attribute so filters can read it.
7474
MvcUtils.putAttribute(request.getServerRequest(), MvcUtils.CLIENT_RESPONSE_INPUT_STREAM_ATTR, body);
75+
MvcUtils.putAttribute(request.getServerRequest(), MvcUtils.CLIENT_RESPONSE_ATTR, clientResponse);
7576
ServerResponse serverResponse = GatewayServerResponse.status(clientResponse.getStatusCode())
7677
.build((req, httpServletResponse) -> {
7778
try (clientResponse) {

0 commit comments

Comments
 (0)