Search in sources :

Example 1 with BodyExtractor

use of org.springframework.web.reactive.function.BodyExtractor in project spring-integration by spring-projects.

the class WebFluxRequestExecutingMessageHandler method exchange.

@Override
protected Object exchange(Supplier<URI> uriSupplier, HttpMethod httpMethod, HttpEntity<?> httpRequest, Object expectedResponseType, Message<?> requestMessage) {
    WebClient.RequestBodySpec requestSpec = this.webClient.method(httpMethod).uri(b -> uriSupplier.get()).headers(headers -> headers.putAll(httpRequest.getHeaders()));
    if (httpRequest.hasBody()) {
        requestSpec.body(BodyInserters.fromObject(httpRequest.getBody()));
    }
    Mono<ClientResponse> responseMono = requestSpec.exchange().flatMap(response -> {
        HttpStatus httpStatus = response.statusCode();
        if (httpStatus.isError()) {
            return response.body(BodyExtractors.toDataBuffers()).reduce(DataBuffer::write).map(dataBuffer -> {
                byte[] bytes = new byte[dataBuffer.readableByteCount()];
                dataBuffer.read(bytes);
                DataBufferUtils.release(dataBuffer);
                return bytes;
            }).defaultIfEmpty(new byte[0]).map(bodyBytes -> {
                throw new WebClientResponseException("ClientResponse has erroneous status code: " + httpStatus.value() + " " + httpStatus.getReasonPhrase(), httpStatus.value(), httpStatus.getReasonPhrase(), response.headers().asHttpHeaders(), bodyBytes, response.headers().contentType().map(MimeType::getCharset).orElse(StandardCharsets.ISO_8859_1));
            });
        } else {
            return Mono.just(response);
        }
    });
    if (isExpectReply()) {
        return responseMono.flatMap(response -> {
            ResponseEntity.BodyBuilder httpEntityBuilder = ResponseEntity.status(response.statusCode()).headers(response.headers().asHttpHeaders());
            Mono<?> bodyMono;
            if (expectedResponseType != null) {
                if (this.replyPayloadToFlux) {
                    BodyExtractor<? extends Flux<?>, ReactiveHttpInputMessage> extractor;
                    if (expectedResponseType instanceof ParameterizedTypeReference<?>) {
                        extractor = BodyExtractors.toFlux((ParameterizedTypeReference<?>) expectedResponseType);
                    } else {
                        extractor = BodyExtractors.toFlux((Class<?>) expectedResponseType);
                    }
                    Flux<?> flux = response.body(extractor);
                    bodyMono = Mono.just(flux);
                } else {
                    BodyExtractor<? extends Mono<?>, ReactiveHttpInputMessage> extractor;
                    if (expectedResponseType instanceof ParameterizedTypeReference<?>) {
                        extractor = BodyExtractors.toMono((ParameterizedTypeReference<?>) expectedResponseType);
                    } else {
                        extractor = BodyExtractors.toMono((Class<?>) expectedResponseType);
                    }
                    bodyMono = response.body(extractor);
                }
            } else if (this.bodyExtractor != null) {
                Object body = response.body(this.bodyExtractor);
                if (body instanceof Mono) {
                    bodyMono = (Mono<?>) body;
                } else {
                    bodyMono = Mono.just(body);
                }
            } else {
                bodyMono = Mono.empty();
            }
            return bodyMono.map(httpEntityBuilder::body).defaultIfEmpty(httpEntityBuilder.build());
        }).map(this::getReply);
    } else {
        responseMono.subscribe(v -> {
        }, ex -> sendErrorMessage(requestMessage, ex));
        return null;
    }
}
Also used : ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) LiteralExpression(org.springframework.expression.common.LiteralExpression) WebClient(org.springframework.web.reactive.function.client.WebClient) Supplier(java.util.function.Supplier) ClientHttpResponse(org.springframework.http.client.reactive.ClientHttpResponse) MimeType(org.springframework.util.MimeType) ValueExpression(org.springframework.integration.expression.ValueExpression) DataBufferUtils(org.springframework.core.io.buffer.DataBufferUtils) Message(org.springframework.messaging.Message) URI(java.net.URI) ClientResponse(org.springframework.web.reactive.function.client.ClientResponse) HttpMethod(org.springframework.http.HttpMethod) Mono(reactor.core.publisher.Mono) AbstractHttpRequestExecutingMessageHandler(org.springframework.integration.http.outbound.AbstractHttpRequestExecutingMessageHandler) BodyExtractor(org.springframework.web.reactive.function.BodyExtractor) DataBuffer(org.springframework.core.io.buffer.DataBuffer) StandardCharsets(java.nio.charset.StandardCharsets) HttpStatus(org.springframework.http.HttpStatus) Flux(reactor.core.publisher.Flux) HttpEntity(org.springframework.http.HttpEntity) BodyExtractors(org.springframework.web.reactive.function.BodyExtractors) ReactiveHttpInputMessage(org.springframework.http.ReactiveHttpInputMessage) MessageHandler(org.springframework.messaging.MessageHandler) BeanFactory(org.springframework.beans.factory.BeanFactory) Expression(org.springframework.expression.Expression) ResponseEntity(org.springframework.http.ResponseEntity) BodyInserters(org.springframework.web.reactive.function.BodyInserters) WebClientResponseException(org.springframework.web.reactive.function.client.WebClientResponseException) Assert(org.springframework.util.Assert) ClientResponse(org.springframework.web.reactive.function.client.ClientResponse) HttpStatus(org.springframework.http.HttpStatus) Mono(reactor.core.publisher.Mono) Flux(reactor.core.publisher.Flux) WebClient(org.springframework.web.reactive.function.client.WebClient) MimeType(org.springframework.util.MimeType) WebClientResponseException(org.springframework.web.reactive.function.client.WebClientResponseException) BodyExtractor(org.springframework.web.reactive.function.BodyExtractor)

Aggregations

URI (java.net.URI)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Supplier (java.util.function.Supplier)1 BeanFactory (org.springframework.beans.factory.BeanFactory)1 ParameterizedTypeReference (org.springframework.core.ParameterizedTypeReference)1 DataBuffer (org.springframework.core.io.buffer.DataBuffer)1 DataBufferUtils (org.springframework.core.io.buffer.DataBufferUtils)1 Expression (org.springframework.expression.Expression)1 LiteralExpression (org.springframework.expression.common.LiteralExpression)1 HttpEntity (org.springframework.http.HttpEntity)1 HttpMethod (org.springframework.http.HttpMethod)1 HttpStatus (org.springframework.http.HttpStatus)1 ReactiveHttpInputMessage (org.springframework.http.ReactiveHttpInputMessage)1 ResponseEntity (org.springframework.http.ResponseEntity)1 ClientHttpResponse (org.springframework.http.client.reactive.ClientHttpResponse)1 ValueExpression (org.springframework.integration.expression.ValueExpression)1 AbstractHttpRequestExecutingMessageHandler (org.springframework.integration.http.outbound.AbstractHttpRequestExecutingMessageHandler)1 Message (org.springframework.messaging.Message)1 MessageHandler (org.springframework.messaging.MessageHandler)1 Assert (org.springframework.util.Assert)1