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;
}
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)));
}
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);
});
}
Aggregations