30
30
31
31
import org .springframework .core .ParameterizedTypeReference ;
32
32
import org .springframework .core .codec .Hints ;
33
+ import org .springframework .core .io .buffer .DataBuffer ;
33
34
import org .springframework .core .io .buffer .DataBufferUtils ;
34
35
import org .springframework .http .HttpHeaders ;
35
36
import org .springframework .http .HttpRequest ;
@@ -66,6 +67,8 @@ class DefaultClientResponse implements ClientResponse {
66
67
67
68
private final Supplier <HttpRequest > requestSupplier ;
68
69
70
+ private final BodyExtractor .Context bodyExtractorContext ;
71
+
69
72
70
73
public DefaultClientResponse (ClientHttpResponse response , ExchangeStrategies strategies ,
71
74
String logPrefix , String requestDescription , Supplier <HttpRequest > requestSupplier ) {
@@ -76,6 +79,22 @@ public DefaultClientResponse(ClientHttpResponse response, ExchangeStrategies str
76
79
this .logPrefix = logPrefix ;
77
80
this .requestDescription = requestDescription ;
78
81
this .requestSupplier = requestSupplier ;
82
+ this .bodyExtractorContext = new BodyExtractor .Context () {
83
+ @ Override
84
+ public List <HttpMessageReader <?>> messageReaders () {
85
+ return strategies .messageReaders ();
86
+ }
87
+
88
+ @ Override
89
+ public Optional <ServerHttpResponse > serverResponse () {
90
+ return Optional .empty ();
91
+ }
92
+
93
+ @ Override
94
+ public Map <String , Object > hints () {
95
+ return Hints .from (Hints .LOG_PREFIX_HINT , logPrefix );
96
+ }
97
+ };
79
98
}
80
99
81
100
@@ -107,22 +126,7 @@ public MultiValueMap<String, ResponseCookie> cookies() {
107
126
@ SuppressWarnings ("unchecked" )
108
127
@ Override
109
128
public <T > T body (BodyExtractor <T , ? super ClientHttpResponse > extractor ) {
110
- T result = extractor .extract (this .response , new BodyExtractor .Context () {
111
- @ Override
112
- public List <HttpMessageReader <?>> messageReaders () {
113
- return strategies .messageReaders ();
114
- }
115
-
116
- @ Override
117
- public Optional <ServerHttpResponse > serverResponse () {
118
- return Optional .empty ();
119
- }
120
-
121
- @ Override
122
- public Map <String , Object > hints () {
123
- return Hints .from (Hints .LOG_PREFIX_HINT , logPrefix );
124
- }
125
- });
129
+ T result = extractor .extract (this .response , this .bodyExtractorContext );
126
130
String description = "Body from " + this .requestDescription + " [DefaultClientResponse]" ;
127
131
if (result instanceof Mono ) {
128
132
return (T ) ((Mono <?>) result ).checkpoint (description );
@@ -146,8 +150,10 @@ public <T> Mono<T> bodyToMono(ParameterizedTypeReference<T> elementTypeRef) {
146
150
}
147
151
148
152
@ Override
153
+ @ SuppressWarnings ("unchecked" )
149
154
public <T > Flux <T > bodyToFlux (Class <? extends T > elementClass ) {
150
- return body (BodyExtractors .toFlux (elementClass ));
155
+ return elementClass .equals (DataBuffer .class ) ?
156
+ (Flux <T >) body (BodyExtractors .toDataBuffers ()) : body (BodyExtractors .toFlux (elementClass ));
151
157
}
152
158
153
159
@ Override
0 commit comments