Search in sources :

Example 1 with RequestBodySpec

use of org.springframework.web.reactive.function.client.WebClient.RequestBodySpec in project spring-data-elasticsearch by spring-projects.

the class DefaultReactiveElasticsearchClient method sendRequest.

private RequestBodySpec sendRequest(WebClient webClient, String logId, Request request, HttpHeaders headers) {
    RequestBodySpec requestBodySpec = // 
    webClient.method(HttpMethod.valueOf(request.getMethod().toUpperCase())).uri(builder -> {
        builder = builder.path(request.getEndpoint());
        if (!ObjectUtils.isEmpty(request.getParameters())) {
            for (Entry<String, String> entry : request.getParameters().entrySet()) {
                builder = builder.queryParam(entry.getKey(), entry.getValue());
            }
        }
        return builder.build();
    }).attribute(ClientRequest.LOG_ID_ATTRIBUTE, // 
    logId).headers(theHeaders -> {
        // add all the headers explicitly set
        theHeaders.addAll(headers);
        // and now those that might be set on the request.
        if (request.getOptions() != null) {
            if (!ObjectUtils.isEmpty(request.getOptions().getHeaders())) {
                request.getOptions().getHeaders().forEach(it -> theHeaders.add(it.getName(), it.getValue()));
            }
        }
    });
    if (request.getEntity() != null) {
        Lazy<String> body = bodyExtractor(request);
        ClientLogger.logRequest(logId, request.getMethod().toUpperCase(), request.getEndpoint(), request.getParameters(), body::get);
        requestBodySpec.contentType(MediaType.valueOf(request.getEntity().getContentType().getValue())).body(Mono.fromSupplier(body), String.class);
    } else {
        ClientLogger.logRequest(logId, request.getMethod().toUpperCase(), request.getEndpoint(), request.getParameters());
    }
    return requestBodySpec;
}
Also used : RequestBodySpec(org.springframework.web.reactive.function.client.WebClient.RequestBodySpec)

Example 2 with RequestBodySpec

use of org.springframework.web.reactive.function.client.WebClient.RequestBodySpec in project spring-cloud-gateway by spring-cloud.

the class ProxyExchange method exchange.

private Mono<ResponseEntity<T>> exchange(RequestEntity<?> requestEntity) {
    Type type = this.responseType;
    RequestBodySpec builder = rest.method(requestEntity.getMethod()).uri(requestEntity.getUrl()).headers(headers -> addHeaders(headers, requestEntity.getHeaders()));
    Mono<ClientResponse> result;
    if (requestEntity.getBody() instanceof Publisher) {
        @SuppressWarnings("unchecked") Publisher<Object> publisher = (Publisher<Object>) requestEntity.getBody();
        result = builder.body(publisher, Object.class).exchange();
    } else if (requestEntity.getBody() != null) {
        result = builder.body(BodyInserters.fromValue(requestEntity.getBody())).exchange();
    } else {
        if (hasBody) {
            result = builder.headers(headers -> addHeaders(headers, exchange.getRequest().getHeaders())).body(exchange.getRequest().getBody(), DataBuffer.class).exchange();
        } else {
            result = builder.headers(headers -> addHeaders(headers, exchange.getRequest().getHeaders())).exchange();
        }
    }
    return result.flatMap(response -> response.toEntity(ParameterizedTypeReference.forType(type)));
}
Also used : ClientResponse(org.springframework.web.reactive.function.client.ClientResponse) Arrays(java.util.Arrays) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) URISyntaxException(java.net.URISyntaxException) WebClient(org.springframework.web.reactive.function.client.WebClient) BindingResult(org.springframework.validation.BindingResult) BindingContext(org.springframework.web.reactive.BindingContext) Function(java.util.function.Function) RequestBodySpec(org.springframework.web.reactive.function.client.WebClient.RequestBodySpec) ServerWebExchange(org.springframework.web.server.ServerWebExchange) HashSet(java.util.HashSet) RequestBody(org.springframework.web.bind.annotation.RequestBody) URI(java.net.URI) RequestEntity(org.springframework.http.RequestEntity) ClientResponse(org.springframework.web.reactive.function.client.ClientResponse) HttpHeaders(org.springframework.http.HttpHeaders) Publisher(org.reactivestreams.Publisher) HttpMethod(org.springframework.http.HttpMethod) Set(java.util.Set) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) Collectors(java.util.stream.Collectors) BodyBuilder(org.springframework.http.RequestEntity.BodyBuilder) Type(java.lang.reflect.Type) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) ResponseEntity(org.springframework.http.ResponseEntity) BodyInserters(org.springframework.web.reactive.function.BodyInserters) Collections(java.util.Collections) StringUtils(org.springframework.util.StringUtils) Type(java.lang.reflect.Type) Publisher(org.reactivestreams.Publisher) RequestBodySpec(org.springframework.web.reactive.function.client.WebClient.RequestBodySpec) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Example 3 with RequestBodySpec

use of org.springframework.web.reactive.function.client.WebClient.RequestBodySpec in project spring-cloud-gateway by spring-cloud.

the class WebClientHttpRoutingFilter method filter.

@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
    URI requestUrl = exchange.getRequiredAttribute(GATEWAY_REQUEST_URL_ATTR);
    String scheme = requestUrl.getScheme();
    if (isAlreadyRouted(exchange) || (!"http".equals(scheme) && !"https".equals(scheme))) {
        return chain.filter(exchange);
    }
    setAlreadyRouted(exchange);
    ServerHttpRequest request = exchange.getRequest();
    HttpMethod method = request.getMethod();
    HttpHeaders filteredHeaders = filterRequest(getHeadersFilters(), exchange);
    boolean preserveHost = exchange.getAttributeOrDefault(PRESERVE_HOST_HEADER_ATTRIBUTE, false);
    RequestBodySpec bodySpec = this.webClient.method(method).uri(requestUrl).headers(httpHeaders -> {
        httpHeaders.addAll(filteredHeaders);
        // TODO: can this support preserviceHostHeader?
        if (!preserveHost) {
            httpHeaders.remove(HttpHeaders.HOST);
        }
    });
    RequestHeadersSpec<?> headersSpec;
    if (requiresBody(method)) {
        headersSpec = bodySpec.body(BodyInserters.fromDataBuffers(request.getBody()));
    } else {
        headersSpec = bodySpec;
    }
    return headersSpec.exchangeToMono(Mono::just).flatMap(res -> {
        ServerHttpResponse response = exchange.getResponse();
        response.getHeaders().putAll(res.headers().asHttpHeaders());
        response.setStatusCode(res.statusCode());
        // Defer committing the response until all route filters have run
        // Put client response as ServerWebExchange attribute and write
        // response later NettyWriteResponseFilter
        exchange.getAttributes().put(CLIENT_RESPONSE_ATTR, res);
        return chain.filter(exchange);
    });
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) RequestBodySpec(org.springframework.web.reactive.function.client.WebClient.RequestBodySpec) URI(java.net.URI) HttpMethod(org.springframework.http.HttpMethod) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse)

Aggregations

RequestBodySpec (org.springframework.web.reactive.function.client.WebClient.RequestBodySpec)3 URI (java.net.URI)2 HttpHeaders (org.springframework.http.HttpHeaders)2 HttpMethod (org.springframework.http.HttpMethod)2 Type (java.lang.reflect.Type)1 URISyntaxException (java.net.URISyntaxException)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 Publisher (org.reactivestreams.Publisher)1 ParameterizedTypeReference (org.springframework.core.ParameterizedTypeReference)1 DataBuffer (org.springframework.core.io.buffer.DataBuffer)1 RequestEntity (org.springframework.http.RequestEntity)1 BodyBuilder (org.springframework.http.RequestEntity.BodyBuilder)1 ResponseEntity (org.springframework.http.ResponseEntity)1 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)1 ServerHttpRequest (org.springframework.http.server.reactive.ServerHttpRequest)1