@Override public <T> Mono<T> bodyToMono(ParameterizedTypeReference<T> typeReference) { return this.delegate.bodyToMono(typeReference); }
@Override public <T> Mono<T> bodyToMono(Class<? extends T> elementClass) { return this.delegate.bodyToMono(elementClass); }
@Override public <T> Mono<T> bodyToMono(Class<? extends T> elementClass) { return delegate.bodyToMono(elementClass); }
@Override public <T> Mono<T> bodyToMono(ParameterizedTypeReference<T> typeReference) { return delegate.bodyToMono(typeReference); }
@Override public <T> Mono<T> bodyToMono(Class<T> bodyType) { return this.responseMono.flatMap(response -> handleBody(response, response.bodyToMono(bodyType), mono -> mono.flatMap(Mono::error))); }
@Override public <T> Mono<T> bodyToMono(ParameterizedTypeReference<T> bodyType) { return this.responseMono.flatMap(response -> handleBody(response, response.bodyToMono(bodyType), mono -> mono.flatMap(Mono::error))); }
@Override public <B> BodySpec<B, ?> expectBody(ParameterizedTypeReference<B> bodyType) { B body = this.response.bodyToMono(bodyType).block(this.timeout); EntityExchangeResult<B> entityResult = new EntityExchangeResult<>(this.exchangeResult, body); return new DefaultBodySpec<>(entityResult); }
@Override public <B> BodySpec<B, ?> expectBody(Class<B> bodyType) { B body = this.response.bodyToMono(bodyType).block(this.timeout); EntityExchangeResult<B> entityResult = new EntityExchangeResult<>(this.exchangeResult, body); return new DefaultBodySpec<>(entityResult); }
@SuppressWarnings("unchecked") private <T> Mono<T> drainBody(ClientResponse response, Throwable ex) { // Ensure the body is drained, even if the StatusHandler didn't consume it, // but ignore exception, in case the handler did consume. return (Mono<T>) response.bodyToMono(Void.class) .onErrorResume(ex2 -> Mono.empty()).thenReturn(ex); }
@Override public BodyContentSpec expectBody() { ByteArrayResource resource = this.response.bodyToMono(ByteArrayResource.class).block(this.timeout); byte[] body = (resource != null ? resource.getByteArray() : null); EntityExchangeResult<byte[]> entityResult = new EntityExchangeResult<>(this.exchangeResult, body); return new DefaultBodyContentSpec(entityResult); }
private Function<ClientResponse, Mono<DetectedEndpoint>> convert(EndpointDefinition endpointDefinition, URI uri) { return response -> { Mono<DetectedEndpoint> endpoint = Mono.empty(); if (response.statusCode().is2xxSuccessful()) { endpoint = Mono.just(DetectedEndpoint.of(endpointDefinition, uri.toString())); } return response.bodyToMono(Void.class).then(endpoint); }; }
protected Mono<Info> convertInfo(Instance instance, ClientResponse response) { if (response.statusCode().is2xxSuccessful() && response.headers() .contentType() .map(mt -> mt.isCompatibleWith(MediaType.APPLICATION_JSON) || mt.isCompatibleWith(ACTUATOR_V2_MEDIATYPE)) .orElse(false)) { return response.bodyToMono(RESPONSE_TYPE).map(Info::from).defaultIfEmpty(Info.empty()); } log.info("Couldn't retrieve info for {}: {}", instance, response.statusCode()); return response.bodyToMono(Void.class).then(Mono.just(Info.empty())); }
@Test public void bodyToMonoClass() { Mono<String> result = Mono.just("foo"); when(mockResponse.bodyToMono(String.class)).thenReturn(result); assertSame(result, wrapper.bodyToMono(String.class)); }
@Test public void bodyToMonoParameterizedTypeReference() { Mono<String> result = Mono.just("foo"); ParameterizedTypeReference<String> reference = new ParameterizedTypeReference<String>() {}; when(mockResponse.bodyToMono(reference)).thenReturn(result); assertSame(result, wrapper.bodyToMono(reference)); }
protected Mono<StatusInfo> convertStatusInfo(ClientResponse response) { Boolean hasCompatibleContentType = response.headers() .contentType() .map(mt -> mt.isCompatibleWith(MediaType.APPLICATION_JSON) || mt.isCompatibleWith(ACTUATOR_V2_MEDIATYPE)) .orElse(false); StatusInfo statusInfoFromStatus = this.getStatusInfoFromStatus(response.statusCode(), emptyMap()); if (hasCompatibleContentType) { return response.bodyToMono(RESPONSE_TYPE).map(body -> { if (body.get("status") instanceof String) { return StatusInfo.from(body); } return getStatusInfoFromStatus(response.statusCode(), body); }).defaultIfEmpty(statusInfoFromStatus); } return response.bodyToMono(Void.class).then(Mono.just(statusInfoFromStatus)); }
@Test public void onStatusWithBodyConsumed() { RuntimeException ex = new RuntimeException("response error"); testOnStatus(ex, response -> response.bodyToMono(Void.class).thenReturn(ex)); }
@Override public Mono<Endpoints> detectEndpoints(Instance instance) { Registration registration = instance.getRegistration(); String managementUrl = registration.getManagementUrl(); if (managementUrl == null || Objects.equals(registration.getServiceUrl(), managementUrl)) { return Mono.empty(); } return instanceWebClient.instance(instance) .get() .uri(managementUrl) .exchange() .flatMap(response -> { if (response.statusCode().is2xxSuccessful() && response.headers() .contentType() .map(actuatorMediaType::isCompatibleWith) .orElse(false)) { return response.bodyToMono(Response.class); } else { return response.bodyToMono(Void.class).then(Mono.empty()); } }) .flatMap(this::convert); }
@Test public void onStatusWithMonoErrorAndBodyConsumed() { RuntimeException ex = new RuntimeException("response error"); testOnStatus(ex, response -> response.bodyToMono(Void.class).then(Mono.error(ex))); }
private static Mono<UserInfoErrorResponse> parse(ClientResponse httpResponse) { String wwwAuth = httpResponse.headers().asHttpHeaders().getFirst(HttpHeaders.WWW_AUTHENTICATE); if (!StringUtils.isEmpty(wwwAuth)) { // Bearer token error? return Mono.fromCallable(() -> UserInfoErrorResponse.parse(wwwAuth)); } ParameterizedTypeReference<Map<String, String>> typeReference = new ParameterizedTypeReference<Map<String, String>>() {}; // Other error? return httpResponse .bodyToMono(typeReference) .map(body -> new UserInfoErrorResponse(ErrorObject.parse(new JSONObject(body)))); } }
@Test public void value() { Mono<Msg> result = this.webClient.get() .uri("/message") .exchange() .doOnNext(response -> { Assert.assertFalse(response.headers().contentType().get().getParameters().containsKey("delimited")); Assert.assertEquals("sample.proto", response.headers().header("X-Protobuf-Schema").get(0)); Assert.assertEquals("Msg", response.headers().header("X-Protobuf-Message").get(0)); }) .flatMap(response -> response.bodyToMono(Msg.class)); StepVerifier.create(result) .expectNext(TEST_MSG) .verifyComplete(); }