/
WebClientUtils.java
87 lines (75 loc) · 2.76 KB
/
WebClientUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.reactive.function.client;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.function.Predicate;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.core.codec.CodecException;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
/**
* Internal methods shared between {@link DefaultWebClient} and
* {@link DefaultClientResponse}.
*
* @author Arjen Poutsma
* @since 5.2
*/
abstract class WebClientUtils {
private static final String VALUE_NONE = "\n\t\t\n\t\t\n\uE000\uE001\uE002\n\t\t\t\t\n";
/**
* Predicate that returns true if an exception should be wrapped.
*/
public static final Predicate<? super Throwable> WRAP_EXCEPTION_PREDICATE =
t -> !(t instanceof WebClientException) && !(t instanceof CodecException);
/**
* Map the given response to a single value {@code ResponseEntity<T>}.
*/
@SuppressWarnings("unchecked")
public static <T> Mono<ResponseEntity<T>> mapToEntity(ClientResponse response, Mono<T> bodyMono) {
return ((Mono<Object>) bodyMono).defaultIfEmpty(VALUE_NONE).map(body ->
new ResponseEntity<>(
body != VALUE_NONE ? (T) body : null,
response.headers().asHttpHeaders(),
response.statusCode()));
}
/**
* Map the given response to a {@code ResponseEntity<List<T>>}.
*/
public static <T> Mono<ResponseEntity<List<T>>> mapToEntityList(ClientResponse response, Publisher<T> body) {
return Flux.from(body).collectList().map(list ->
new ResponseEntity<>(list, response.headers().asHttpHeaders(), response.statusCode()));
}
/**
* Return a String representation of the request details for logging purposes.
* @since 6.0.16
*/
public static String getRequestDescription(HttpMethod httpMethod, URI uri) {
if (StringUtils.hasText(uri.getQuery())) {
try {
uri = new URI(uri.getScheme(), null, uri.getHost(), uri.getPort(), uri.getPath(), null, null);
}
catch (URISyntaxException ex) {
// ignore
}
}
return httpMethod.name() + " " + uri;
}
}