Skip to content

Commit 168c60c

Browse files
committed
Polishing contribution
Closes gh-31992
1 parent 1e49334 commit 168c60c

File tree

4 files changed

+32
-38
lines changed

4 files changed

+32
-38
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -212,7 +212,7 @@ public ClientResponse build() {
212212

213213
return new DefaultClientResponse(httpResponse, this.strategies,
214214
this.originalResponse != null ? this.originalResponse.logPrefix() : "",
215-
this.request.getMethod() + " " + this.request.getURI(),
215+
WebClientUtils.getRequestDescription(this.request.getMethod(), this.request.getURI()),
216216
() -> this.request);
217217
}
218218

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

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,7 +17,6 @@
1717
package org.springframework.web.reactive.function.client;
1818

1919
import java.net.URI;
20-
import java.net.URISyntaxException;
2120
import java.nio.charset.Charset;
2221
import java.time.ZonedDateTime;
2322
import java.util.ArrayList;
@@ -54,7 +53,6 @@
5453
import org.springframework.util.CollectionUtils;
5554
import org.springframework.util.LinkedMultiValueMap;
5655
import org.springframework.util.MultiValueMap;
57-
import org.springframework.util.StringUtils;
5856
import org.springframework.web.reactive.function.BodyExtractor;
5957
import org.springframework.web.reactive.function.BodyInserter;
6058
import org.springframework.web.reactive.function.BodyInserters;
@@ -195,18 +193,6 @@ private static <T> Mono<T> releaseIfNotConsumed(ClientResponse response, Throwab
195193
return response.releaseBody().onErrorComplete().then(Mono.error(ex));
196194
}
197195

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-
210196

211197
private class DefaultRequestBodyUriSpec implements RequestBodyUriSpec {
212198

@@ -469,7 +455,9 @@ public Mono<ClientResponse> exchange() {
469455
observationContext.setRequest(request);
470456
Mono<ClientResponse> responseMono = filterFunction.apply(exchangeFunction)
471457
.exchange(request)
472-
.checkpoint("Request to " + this.httpMethod.name() + " " + getUriToLog(request.url()) + " [DefaultWebClient]")
458+
.checkpoint("Request to " +
459+
WebClientUtils.getRequestDescription(request.method(), request.url()) +
460+
" [DefaultWebClient]")
473461
.switchIfEmpty(NO_HTTP_CLIENT_RESPONSE_ERROR);
474462
if (this.contextModifier != null) {
475463
responseMono = responseMono.contextWrite(this.contextModifier);
@@ -704,7 +692,8 @@ private <T> Mono<T> applyStatusHandlers(ClientResponse response) {
704692
}
705693
Mono<T> result = exMono.flatMap(Mono::error);
706694
return result.checkpoint(statusCode + " from " +
707-
this.httpMethod + " " + getUriToLog(this.uri) + " [DefaultWebClient]");
695+
WebClientUtils.getRequestDescription(this.httpMethod, this.uri) +
696+
" [DefaultWebClient]");
708697
}
709698
}
710699
return null;

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

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,8 +16,6 @@
1616

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

19-
import java.net.URI;
20-
import java.net.URISyntaxException;
2119
import java.nio.charset.Charset;
2220
import java.nio.charset.StandardCharsets;
2321
import java.util.List;
@@ -32,7 +30,6 @@
3230
import org.springframework.http.HttpStatusCode;
3331
import org.springframework.lang.Nullable;
3432
import org.springframework.util.Assert;
35-
import org.springframework.util.StringUtils;
3633

3734
/**
3835
* Exceptions that contain actual HTTP response data.
@@ -101,20 +98,8 @@ public WebClientResponseException(
10198
}
10299

103100
private static String initMessage(HttpStatusCode status, String reasonPhrase, @Nullable HttpRequest request) {
104-
return status.value() + " " + reasonPhrase +
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;
101+
return status.value() + " " + reasonPhrase + (request != null ?
102+
" from " + WebClientUtils.getRequestDescription(request.getMethod(), request.getURI()) : "");
118103
}
119104

120105
/**

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -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.util.List;
2022
import java.util.function.Predicate;
2123

@@ -24,7 +26,9 @@
2426
import reactor.core.publisher.Mono;
2527

2628
import org.springframework.core.codec.CodecException;
29+
import org.springframework.http.HttpMethod;
2730
import org.springframework.http.ResponseEntity;
31+
import org.springframework.util.StringUtils;
2832

2933
/**
3034
* Internal methods shared between {@link DefaultWebClient} and
@@ -64,4 +68,20 @@ public static <T> Mono<ResponseEntity<List<T>>> mapToEntityList(ClientResponse r
6468
new ResponseEntity<>(list, response.headers().asHttpHeaders(), response.statusCode()));
6569
}
6670

71+
/**
72+
* Return a String representation of the request details for logging purposes.
73+
* @since 6.0.16
74+
*/
75+
public static String getRequestDescription(HttpMethod httpMethod, URI uri) {
76+
if (StringUtils.hasText(uri.getQuery())) {
77+
try {
78+
uri = new URI(uri.getScheme(), null, uri.getHost(), uri.getPort(), uri.getPath(), null, null);
79+
}
80+
catch (URISyntaxException ex) {
81+
// ignore
82+
}
83+
}
84+
return httpMethod.name() + " " + uri;
85+
}
86+
6787
}

0 commit comments

Comments
 (0)