add client response attrs

Signed-off-by: jiangyuan <joe469391363@gmail.com>

test ci build
This commit is contained in:
jiangyuan
2025-03-31 13:09:37 +08:00
parent a261d17b4e
commit 7b79e181f2
4 changed files with 12 additions and 5 deletions

View File

@@ -63,6 +63,11 @@ public abstract class MvcUtils {
*/
public static final String CLIENT_RESPONSE_INPUT_STREAM_ATTR = qualify("cachedClientResponseBody");
/**
* Client response key.
*/
public static final String CLIENT_RESPONSE_ATTR = qualify("cachedClientResponse");
/**
* CircuitBreaker execution exception attribute name.
*/

View File

@@ -17,7 +17,6 @@
package org.springframework.cloud.gateway.server.mvc.filter;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
@@ -34,6 +33,7 @@ import org.springframework.core.NestedRuntimeException;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.retry.RetryContext;
import org.springframework.retry.RetryPolicy;
import org.springframework.retry.policy.CompositeRetryPolicy;
@@ -89,10 +89,10 @@ public abstract class RetryFilterFunctions {
}
private static void reset(ServerRequest request) throws IOException {
InputStream inputStream = MvcUtils.getAttribute(request, MvcUtils.CLIENT_RESPONSE_INPUT_STREAM_ATTR);
if (inputStream != null) {
inputStream.close();
MvcUtils.putAttribute(request, MvcUtils.CLIENT_RESPONSE_INPUT_STREAM_ATTR, null);
ClientHttpResponse clientHttpResponse = MvcUtils.getAttribute(request, MvcUtils.CLIENT_RESPONSE_ATTR);
if (clientHttpResponse != null) {
clientHttpResponse.close();
MvcUtils.putAttribute(request, MvcUtils.CLIENT_RESPONSE_ATTR, null);
}
}

View File

@@ -56,6 +56,7 @@ public class ClientHttpRequestFactoryProxyExchange extends AbstractProxyExchange
InputStream body = clientHttpResponse.getBody();
// put the body input stream in a request attribute so filters can read it.
MvcUtils.putAttribute(request.getServerRequest(), MvcUtils.CLIENT_RESPONSE_INPUT_STREAM_ATTR, body);
MvcUtils.putAttribute(request.getServerRequest(), MvcUtils.CLIENT_RESPONSE_ATTR, clientHttpResponse);
ServerResponse serverResponse = GatewayServerResponse.status(clientHttpResponse.getStatusCode())
.build((req, httpServletResponse) -> {
try (clientHttpResponse) {

View File

@@ -72,6 +72,7 @@ public class RestClientProxyExchange extends AbstractProxyExchange {
InputStream body = clientResponse.getBody();
// put the body input stream in a request attribute so filters can read it.
MvcUtils.putAttribute(request.getServerRequest(), MvcUtils.CLIENT_RESPONSE_INPUT_STREAM_ATTR, body);
MvcUtils.putAttribute(request.getServerRequest(), MvcUtils.CLIENT_RESPONSE_ATTR, clientResponse);
ServerResponse serverResponse = GatewayServerResponse.status(clientResponse.getStatusCode())
.build((req, httpServletResponse) -> {
try (clientResponse) {