Java Code Examples for org.springframework.web.reactive.function.client.ClientResponse#bodyToMono()

The following examples show how to use org.springframework.web.reactive.function.client.ClientResponse#bodyToMono() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ModifyResponseBodyGatewayFilterFactory.java    From spring-cloud-gateway with Apache License 2.0 6 votes vote down vote up
private <T> Mono<T> extractBody(ServerWebExchange exchange,
		ClientResponse clientResponse, Class<T> inClass) {
	// if inClass is byte[] then just return body, otherwise check if
	// decoding required
	if (byte[].class.isAssignableFrom(inClass)) {
		return clientResponse.bodyToMono(inClass);
	}

	List<String> encodingHeaders = exchange.getResponse().getHeaders()
			.getOrEmpty(HttpHeaders.CONTENT_ENCODING);
	for (String encoding : encodingHeaders) {
		MessageBodyDecoder decoder = messageBodyDecoders.get(encoding);
		if (decoder != null) {
			return clientResponse.bodyToMono(byte[].class)
					.publishOn(Schedulers.parallel()).map(decoder::decode)
					.map(bytes -> exchange.getResponse().bufferFactory()
							.wrap(bytes))
					.map(buffer -> prepareClientResponse(Mono.just(buffer),
							exchange.getResponse().getHeaders()))
					.flatMap(response -> response.bodyToMono(inClass));
		}
	}

	return clientResponse.bodyToMono(inClass);
}
 
Example 2
Source File: CorsTests.java    From spring-cloud-gateway with Apache License 2.0 6 votes vote down vote up
@Test
public void testPreFlightCorsRequest() {
	ClientResponse clientResponse = webClient.options().uri("/abc/123/function")
			.header("Origin", "domain.com")
			.header("Access-Control-Request-Method", "GET").exchange().block();
	HttpHeaders asHttpHeaders = clientResponse.headers().asHttpHeaders();
	Mono<String> bodyToMono = clientResponse.bodyToMono(String.class);
	// pre-flight request shouldn't return the response body
	assertThat(bodyToMono.block()).isNull();
	assertThat(asHttpHeaders.getAccessControlAllowOrigin())
			.as("Missing header value in response: "
					+ HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN)
			.isEqualTo("*");
	assertThat(asHttpHeaders.getAccessControlAllowMethods())
			.as("Missing header value in response: "
					+ HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS)
			.isEqualTo(Arrays.asList(new HttpMethod[] { HttpMethod.GET }));
	assertThat(clientResponse.statusCode()).as("Pre Flight call failed.")
			.isEqualTo(HttpStatus.OK);
}
 
Example 3
Source File: SimpleUrlHandlerCorsTests.java    From spring-cloud-gateway with Apache License 2.0 6 votes vote down vote up
@Test
public void testPreFlightCorsRequestNotHandledByGW() {
	ClientResponse clientResponse = webClient.options().uri("/abc/123/function")
			.header("Origin", "domain.com")
			.header("Access-Control-Request-Method", "GET").exchange().block();
	HttpHeaders asHttpHeaders = clientResponse.headers().asHttpHeaders();
	Mono<String> bodyToMono = clientResponse.bodyToMono(String.class);
	// pre-flight request shouldn't return the response body
	assertThat(bodyToMono.block()).isNull();
	assertThat(asHttpHeaders.getAccessControlAllowOrigin())
			.as("Missing header value in response: "
					+ HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN)
			.isEqualTo("*");
	assertThat(asHttpHeaders.getAccessControlAllowMethods())
			.as("Missing header value in response: "
					+ HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS)
			.isEqualTo(Arrays.asList(new HttpMethod[] { HttpMethod.GET }));
	assertThat(clientResponse.statusCode()).as("Pre Flight call failed.")
			.isEqualTo(HttpStatus.OK);
}
 
Example 4
Source File: CorsTests.java    From spring-cloud-gateway with Apache License 2.0 5 votes vote down vote up
@Test
public void testCorsRequest() {
	ClientResponse clientResponse = webClient.get().uri("/abc/123/function")
			.header("Origin", "domain.com").header(HttpHeaders.HOST, "www.path.org")
			.exchange().block();
	HttpHeaders asHttpHeaders = clientResponse.headers().asHttpHeaders();
	Mono<String> bodyToMono = clientResponse.bodyToMono(String.class);
	assertThat(bodyToMono.block()).isNotNull();
	assertThat(asHttpHeaders.getAccessControlAllowOrigin())
			.as("Missing header value in response: "
					+ HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN)
			.isEqualTo("*");
	assertThat(clientResponse.statusCode()).as("CORS request failed.")
			.isEqualTo(HttpStatus.OK);
}
 
Example 5
Source File: SimpleUrlHandlerCorsTests.java    From spring-cloud-gateway with Apache License 2.0 5 votes vote down vote up
@Test
public void testCorsRequestNotHandledByGW() {
	ClientResponse clientResponse = webClient.get().uri("/abc/123/function")
			.header("Origin", "domain.com").header(HttpHeaders.HOST, "www.path.org")
			.exchange().block();
	HttpHeaders asHttpHeaders = clientResponse.headers().asHttpHeaders();
	Mono<String> bodyToMono = clientResponse.bodyToMono(String.class);
	assertThat(bodyToMono.block()).isNotNull();
	assertThat(asHttpHeaders.getAccessControlAllowOrigin())
			.as("Missing header value in response: "
					+ HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN)
			.isEqualTo("*");
	assertThat(clientResponse.statusCode()).as("CORS request failed.")
			.isEqualTo(HttpStatus.NOT_FOUND);
}
 
Example 6
Source File: WebClientRetrofitTests.java    From spring-cloud-square with Apache License 2.0 5 votes vote down vote up
@Test
public void webClientClientResponse() {
    Mono<ClientResponse> mono = testClient().getClientResponseMono();
    assertThat(mono).isNotNull();
    ClientResponse clientResponse = mono.block();
    Mono<String> body = clientResponse.bodyToMono(String.class);
    assertThat(body.block()).isEqualTo(HELLO);
}
 
Example 7
Source File: HttpResponse.java    From charon-spring-boot-starter with Apache License 2.0 4 votes vote down vote up
HttpResponse(ClientResponse response) {
    body = response.bodyToMono(byte[].class); // Releases connection
    delegate = response;
}