Skip to content

Commit 1e49334

Browse files
aaronrosserrstoyanchev
authored andcommitted
Exclude query from URI in WebClient checkpoints
See gh-31992
1 parent 79cc0ec commit 1e49334

File tree

3 files changed

+55
-16
lines changed

3 files changed

+55
-16
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,18 @@ private static <T> Mono<T> releaseIfNotConsumed(ClientResponse response, Throwab
195195
return response.releaseBody().onErrorComplete().then(Mono.error(ex));
196196
}
197197

198+
private static URI getUriToLog(URI uri) {
199+
if (StringUtils.hasText(uri.getQuery())) {
200+
try {
201+
uri = new URI(uri.getScheme(), null, uri.getHost(), uri.getPort(), uri.getPath(), null, null);
202+
}
203+
catch (URISyntaxException ex) {
204+
// ignore
205+
}
206+
}
207+
return uri;
208+
}
209+
198210

199211
private class DefaultRequestBodyUriSpec implements RequestBodyUriSpec {
200212

@@ -457,7 +469,7 @@ public Mono<ClientResponse> exchange() {
457469
observationContext.setRequest(request);
458470
Mono<ClientResponse> responseMono = filterFunction.apply(exchangeFunction)
459471
.exchange(request)
460-
.checkpoint("Request to " + this.httpMethod.name() + " " + this.uri + " [DefaultWebClient]")
472+
.checkpoint("Request to " + this.httpMethod.name() + " " + getUriToLog(request.url()) + " [DefaultWebClient]")
461473
.switchIfEmpty(NO_HTTP_CLIENT_RESPONSE_ERROR);
462474
if (this.contextModifier != null) {
463475
responseMono = responseMono.contextWrite(this.contextModifier);
@@ -698,18 +710,6 @@ private <T> Mono<T> applyStatusHandlers(ClientResponse response) {
698710
return null;
699711
}
700712

701-
private static URI getUriToLog(URI uri) {
702-
if (StringUtils.hasText(uri.getQuery())) {
703-
try {
704-
uri = new URI(uri.getScheme(), null, uri.getHost(), uri.getPort(), uri.getPath(), null, null);
705-
}
706-
catch (URISyntaxException ex) {
707-
// ignore
708-
}
709-
}
710-
return uri;
711-
}
712-
713713

714714
private static class StatusHandler {
715715

spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClientResponseException.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.web.reactive.function.client;
1818

19+
import java.net.URI;
20+
import java.net.URISyntaxException;
1921
import java.nio.charset.Charset;
2022
import java.nio.charset.StandardCharsets;
2123
import java.util.List;
@@ -30,6 +32,7 @@
3032
import org.springframework.http.HttpStatusCode;
3133
import org.springframework.lang.Nullable;
3234
import org.springframework.util.Assert;
35+
import org.springframework.util.StringUtils;
3336

3437
/**
3538
* Exceptions that contain actual HTTP response data.
@@ -99,7 +102,19 @@ public WebClientResponseException(
99102

100103
private static String initMessage(HttpStatusCode status, String reasonPhrase, @Nullable HttpRequest request) {
101104
return status.value() + " " + reasonPhrase +
102-
(request != null ? " from " + request.getMethod() + " " + request.getURI() : "");
105+
(request != null ? " from " + request.getMethod() + " " + getUriToLog(request.getURI()) : "");
106+
}
107+
108+
private static URI getUriToLog(URI uri) {
109+
if (StringUtils.hasText(uri.getQuery())) {
110+
try {
111+
uri = new URI(uri.getScheme(), null, uri.getHost(), uri.getPort(), uri.getPath(), null, null);
112+
}
113+
catch (URISyntaxException ex) {
114+
// ignore
115+
}
116+
}
117+
return uri;
103118
}
104119

105120
/**

spring-webflux/src/test/java/org/springframework/web/reactive/function/client/DefaultClientResponseTests.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.web.reactive.function.client;
1818

1919
import java.net.InetSocketAddress;
20+
import java.net.URI;
2021
import java.nio.charset.StandardCharsets;
2122
import java.util.List;
2223
import java.util.Map;
@@ -34,7 +35,9 @@
3435
import org.springframework.core.io.buffer.DataBuffer;
3536
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
3637
import org.springframework.http.HttpHeaders;
38+
import org.springframework.http.HttpMethod;
3739
import org.springframework.http.HttpRange;
40+
import org.springframework.http.HttpRequest;
3841
import org.springframework.http.HttpStatus;
3942
import org.springframework.http.HttpStatusCode;
4043
import org.springframework.http.MediaType;
@@ -43,6 +46,7 @@
4346
import org.springframework.http.client.reactive.ClientHttpResponse;
4447
import org.springframework.http.codec.DecoderHttpMessageReader;
4548
import org.springframework.http.codec.json.Jackson2JsonDecoder;
49+
import org.springframework.lang.Nullable;
4650
import org.springframework.util.LinkedMultiValueMap;
4751
import org.springframework.util.MultiValueMap;
4852

@@ -67,13 +71,16 @@ class DefaultClientResponseTests {
6771

6872
private final ExchangeStrategies mockExchangeStrategies = mock();
6973

74+
@Nullable
75+
private HttpRequest httpRequest = null;
76+
7077
private DefaultClientResponse defaultClientResponse;
7178

7279

7380
@BeforeEach
7481
void configureMocks() {
7582
given(mockResponse.getHeaders()).willReturn(this.httpHeaders);
76-
defaultClientResponse = new DefaultClientResponse(mockResponse, mockExchangeStrategies, "", "", () -> null);
83+
defaultClientResponse = new DefaultClientResponse(mockResponse, mockExchangeStrategies, "", "", () -> httpRequest);
7784
}
7885

7986

@@ -302,13 +309,30 @@ void createException() {
302309
given(mockResponse.getStatusCode()).willReturn(HttpStatus.NOT_FOUND);
303310
given(mockResponse.getBody()).willReturn(Flux.just(dataBuffer));
304311

312+
httpRequest = new HttpRequest() {
313+
@Override
314+
public HttpMethod getMethod() {
315+
return HttpMethod.valueOf("UNKNOWN");
316+
}
317+
318+
@Override
319+
public URI getURI() {
320+
return URI.create("https://user:[email protected]:9999/app/path?token=secret#fragment");
321+
}
322+
323+
@Override
324+
public HttpHeaders getHeaders() {
325+
return HttpHeaders.EMPTY;
326+
}
327+
};
328+
305329
given(mockExchangeStrategies.messageReaders()).willReturn(
306330
List.of(new DecoderHttpMessageReader<>(new ByteArrayDecoder())));
307331

308332
Mono<WebClientResponseException> resultMono = defaultClientResponse.createException();
309333
WebClientResponseException exception = resultMono.block();
310334
assertThat(exception.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
311-
assertThat(exception.getMessage()).isEqualTo("404 Not Found");
335+
assertThat(exception.getMessage()).isEqualTo("404 Not Found from UNKNOWN https://example.org:9999/app/path");
312336
assertThat(exception.getHeaders()).containsExactly(entry("Content-Type", List.of("text/plain")));
313337
assertThat(exception.getResponseBodyAsByteArray()).isEqualTo(bytes);
314338
}

0 commit comments

Comments
 (0)