|
33 | 33 | import org.reactivestreams.Publisher;
|
34 | 34 | import org.reactivestreams.Subscription;
|
35 | 35 | import reactor.core.CoreSubscriber;
|
| 36 | +import reactor.core.publisher.Flux; |
36 | 37 | import reactor.core.publisher.Mono;
|
37 | 38 | import reactor.util.annotation.Nullable;
|
38 | 39 | import reactor.util.context.Context;
|
|
41 | 42 | import org.springframework.beans.factory.BeanFactory;
|
42 | 43 | import org.springframework.beans.factory.config.BeanPostProcessor;
|
43 | 44 | import org.springframework.cloud.sleuth.instrument.reactor.ReactorSleuth;
|
| 45 | +import org.springframework.core.ParameterizedTypeReference; |
44 | 46 | import org.springframework.core.io.buffer.DataBuffer;
|
| 47 | +import org.springframework.http.HttpStatus; |
| 48 | +import org.springframework.http.ResponseCookie; |
| 49 | +import org.springframework.http.ResponseEntity; |
| 50 | +import org.springframework.http.client.reactive.ClientHttpResponse; |
| 51 | +import org.springframework.util.MultiValueMap; |
45 | 52 | import org.springframework.web.client.RestClientException;
|
| 53 | +import org.springframework.web.reactive.function.BodyExtractor; |
46 | 54 | import org.springframework.web.reactive.function.client.ClientRequest;
|
47 | 55 | import org.springframework.web.reactive.function.client.ClientResponse;
|
48 | 56 | import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
|
49 | 57 | import org.springframework.web.reactive.function.client.ExchangeFunction;
|
| 58 | +import org.springframework.web.reactive.function.client.ExchangeStrategies; |
50 | 59 | import org.springframework.web.reactive.function.client.WebClient;
|
51 | 60 |
|
52 | 61 | /**
|
@@ -290,17 +299,102 @@ public void onNext(ClientResponse response) {
|
290 | 299 | this.done = true;
|
291 | 300 | try {
|
292 | 301 | // decorate response body
|
293 |
| - this.actual |
294 |
| - .onNext(ClientResponse.from(response) |
295 |
| - .body(response.bodyToFlux(DataBuffer.class) |
296 |
| - .transform(this.scopePassingTransformer)) |
297 |
| - .build()); |
| 302 | + this.actual.onNext(wrapped(response)); |
298 | 303 | }
|
299 | 304 | finally {
|
300 | 305 | terminateSpan(response, null);
|
301 | 306 | }
|
302 | 307 | }
|
303 | 308 |
|
| 309 | + // TODO: Remove once fixed |
| 310 | + // https://github.com/spring-projects/spring-framework/issues/23366 |
| 311 | + private ClientResponse wrapped(ClientResponse response) { |
| 312 | + return new ClientResponse() { |
| 313 | + @Override |
| 314 | + public HttpStatus statusCode() { |
| 315 | + try { |
| 316 | + return response.statusCode(); |
| 317 | + } |
| 318 | + catch (IllegalArgumentException ex) { |
| 319 | + return null; |
| 320 | + } |
| 321 | + } |
| 322 | + |
| 323 | + @Override |
| 324 | + public int rawStatusCode() { |
| 325 | + return response.rawStatusCode(); |
| 326 | + } |
| 327 | + |
| 328 | + @Override |
| 329 | + public Headers headers() { |
| 330 | + return response.headers(); |
| 331 | + } |
| 332 | + |
| 333 | + @Override |
| 334 | + public MultiValueMap<String, ResponseCookie> cookies() { |
| 335 | + return response.cookies(); |
| 336 | + } |
| 337 | + |
| 338 | + @Override |
| 339 | + public ExchangeStrategies strategies() { |
| 340 | + return response.strategies(); |
| 341 | + } |
| 342 | + |
| 343 | + @Override |
| 344 | + public <T> T body( |
| 345 | + BodyExtractor<T, ? super ClientHttpResponse> extractor) { |
| 346 | + return response.body(extractor); |
| 347 | + } |
| 348 | + |
| 349 | + @Override |
| 350 | + public <T> Mono<T> bodyToMono(Class<? extends T> elementClass) { |
| 351 | + return response.bodyToMono(elementClass); |
| 352 | + } |
| 353 | + |
| 354 | + @Override |
| 355 | + public <T> Mono<T> bodyToMono( |
| 356 | + ParameterizedTypeReference<T> typeReference) { |
| 357 | + return response.bodyToMono(typeReference); |
| 358 | + } |
| 359 | + |
| 360 | + @Override |
| 361 | + public <T> Flux<T> bodyToFlux(Class<? extends T> elementClass) { |
| 362 | + return (Flux<T>) response.bodyToFlux(DataBuffer.class) |
| 363 | + .transform(scopePassingTransformer); |
| 364 | + } |
| 365 | + |
| 366 | + @Override |
| 367 | + public <T> Flux<T> bodyToFlux( |
| 368 | + ParameterizedTypeReference<T> typeReference) { |
| 369 | + return (Flux<T>) response.bodyToFlux(DataBuffer.class) |
| 370 | + .transform(scopePassingTransformer); |
| 371 | + } |
| 372 | + |
| 373 | + @Override |
| 374 | + public <T> Mono<ResponseEntity<T>> toEntity(Class<T> bodyType) { |
| 375 | + return response.toEntity(bodyType); |
| 376 | + } |
| 377 | + |
| 378 | + @Override |
| 379 | + public <T> Mono<ResponseEntity<T>> toEntity( |
| 380 | + ParameterizedTypeReference<T> typeReference) { |
| 381 | + return response.toEntity(typeReference); |
| 382 | + } |
| 383 | + |
| 384 | + @Override |
| 385 | + public <T> Mono<ResponseEntity<List<T>>> toEntityList( |
| 386 | + Class<T> elementType) { |
| 387 | + return response.toEntityList(elementType); |
| 388 | + } |
| 389 | + |
| 390 | + @Override |
| 391 | + public <T> Mono<ResponseEntity<List<T>>> toEntityList( |
| 392 | + ParameterizedTypeReference<T> typeReference) { |
| 393 | + return response.toEntityList(typeReference); |
| 394 | + } |
| 395 | + }; |
| 396 | + } |
| 397 | + |
304 | 398 | @Override
|
305 | 399 | public void onError(Throwable t) {
|
306 | 400 | try {
|
@@ -346,30 +440,51 @@ void terminateSpanOnCancel() {
|
346 | 440 |
|
347 | 441 | void terminateSpan(@Nullable ClientResponse clientResponse,
|
348 | 442 | @Nullable Throwable throwable) {
|
349 |
| - if (clientResponse == null || clientResponse.statusCode() == null) { |
| 443 | + if (clientResponse == null) { |
350 | 444 | if (log.isDebugEnabled()) {
|
351 | 445 | log.debug("No response was returned. Will close the span ["
|
352 | 446 | + this.span + "]");
|
353 | 447 | }
|
354 | 448 | handleReceive(this.span, this.ws, clientResponse, throwable);
|
355 | 449 | return;
|
356 | 450 | }
|
357 |
| - boolean error = clientResponse.statusCode().is4xxClientError() |
358 |
| - || clientResponse.statusCode().is5xxServerError(); |
| 451 | + int statusCode = statusCodeAsInt(clientResponse); |
| 452 | + boolean error = isError(statusCode); |
359 | 453 | if (error) {
|
360 | 454 | if (log.isDebugEnabled()) {
|
361 | 455 | log.debug(
|
362 | 456 | "Non positive status code was returned from the call. Will close the span ["
|
363 | 457 | + this.span + "]");
|
364 | 458 | }
|
365 | 459 | throwable = new RestClientException("Status code of the response is ["
|
366 |
| - + clientResponse.statusCode().value() |
367 |
| - + "] and the reason is [" |
368 |
| - + clientResponse.statusCode().getReasonPhrase() + "]"); |
| 460 | + + statusCode + "] and the reason is [" |
| 461 | + + reasonPhrase(clientResponse) + "]"); |
369 | 462 | }
|
370 | 463 | handleReceive(this.span, this.ws, clientResponse, throwable);
|
371 | 464 | }
|
372 | 465 |
|
| 466 | + private String reasonPhrase(ClientResponse clientResponse) { |
| 467 | + try { |
| 468 | + return clientResponse.statusCode().getReasonPhrase(); |
| 469 | + } |
| 470 | + catch (IllegalArgumentException ex) { |
| 471 | + return ""; |
| 472 | + } |
| 473 | + } |
| 474 | + |
| 475 | + private boolean isError(int code) { |
| 476 | + return code >= 400; |
| 477 | + } |
| 478 | + |
| 479 | + private int statusCodeAsInt(ClientResponse response) { |
| 480 | + try { |
| 481 | + return response.rawStatusCode(); |
| 482 | + } |
| 483 | + catch (Exception dontCare) { |
| 484 | + return 0; |
| 485 | + } |
| 486 | + } |
| 487 | + |
373 | 488 | }
|
374 | 489 |
|
375 | 490 | }
|
|
0 commit comments