Skip to content

Commit

Permalink
Polishing contribution
Browse files Browse the repository at this point in the history
Closes gh-30092
  • Loading branch information
rstoyanchev committed Mar 14, 2023
1 parent cd8955f commit c3ce847
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 60 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -370,36 +370,35 @@ public ResponseSpec exchange() {

private ClientRequest.Builder initRequestBuilder() {
ClientRequest.Builder builder = ClientRequest.create(this.httpMethod, initUri())
.headers(this::initHeaders)
.cookies(this::initCookies)
.headers(headersToUse -> {
if (!CollectionUtils.isEmpty(defaultHeaders)) {
headersToUse.putAll(defaultHeaders);
}
if (!CollectionUtils.isEmpty(this.headers)) {
headersToUse.putAll(this.headers);
}
})
.cookies(cookiesToUse -> {
if (!CollectionUtils.isEmpty(defaultCookies)) {
cookiesToUse.putAll(defaultCookies);
}
if (!CollectionUtils.isEmpty(this.cookies)) {
cookiesToUse.putAll(this.cookies);
}
})
.attributes(attributes -> attributes.putAll(this.attributes));

if (this.httpRequestConsumer != null) {
builder.httpRequest(this.httpRequestConsumer);
}

return builder;
}

private URI initUri() {
return (this.uri != null ? this.uri : uriBuilderFactory.expand(""));
}

private void initHeaders(HttpHeaders out) {
if (!CollectionUtils.isEmpty(defaultHeaders)) {
out.putAll(defaultHeaders);
}
if (!CollectionUtils.isEmpty(this.headers)) {
out.putAll(this.headers);
}
}

private void initCookies(MultiValueMap<String, String> out) {
if (!CollectionUtils.isEmpty(defaultCookies)) {
out.putAll(defaultCookies);
}
if (!CollectionUtils.isEmpty(this.cookies)) {
out.putAll(this.cookies);
}
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.util.function.Function;
import java.util.function.IntPredicate;
import java.util.function.Predicate;
import java.util.function.Supplier;

import io.micrometer.observation.Observation;
import io.micrometer.observation.ObservationRegistry;
Expand All @@ -43,7 +42,6 @@
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpRequest;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -394,30 +392,7 @@ public RequestHeadersSpec<?> syncBody(Object body) {
@Override
public ResponseSpec retrieve() {
return new DefaultResponseSpec(
exchange(), this::createRequest, DefaultWebClient.this.defaultStatusHandlers);
}

private HttpRequest createRequest() {
return new HttpRequest() {
private final URI uri = initUri();

@Override
public HttpMethod getMethod() {
return httpMethod;
}

@Override
public URI getURI() {
return this.uri;
}

@Override
public HttpHeaders getHeaders() {
HttpHeaders headers = new HttpHeaders();
initHeaders(headers);
return headers;
}
};
this.httpMethod, initUri(), exchange(), DefaultWebClient.this.defaultStatusHandlers);
}

@Override
Expand Down Expand Up @@ -528,21 +503,25 @@ private static class DefaultResponseSpec implements ResponseSpec {
private static final StatusHandler DEFAULT_STATUS_HANDLER =
new StatusHandler(STATUS_CODE_ERROR, ClientResponse::createException);

private final Mono<ClientResponse> responseMono;

private final Supplier<HttpRequest> requestSupplier;
private final HttpMethod httpMethod;

private final URI uri;

private final Mono<ClientResponse> responseMono;

private final List<StatusHandler> statusHandlers = new ArrayList<>(1);

private final int defaultStatusHandlerCount;


DefaultResponseSpec(
Mono<ClientResponse> responseMono, Supplier<HttpRequest> requestSupplier,
HttpMethod httpMethod, URI uri, Mono<ClientResponse> responseMono,
List<StatusHandler> defaultStatusHandlers) {

this.httpMethod = httpMethod;
this.uri = uri;
this.responseMono = responseMono;
this.requestSupplier = requestSupplier;
this.statusHandlers.addAll(defaultStatusHandlers);
this.statusHandlers.add(DEFAULT_STATUS_HANDLER);
this.defaultStatusHandlerCount = this.statusHandlers.size();
Expand Down Expand Up @@ -696,21 +675,14 @@ private <T> Mono<T> applyStatusHandlers(ClientResponse response) {
exMono = releaseIfNotConsumed(response, ex2);
}
Mono<T> result = exMono.flatMap(Mono::error);
HttpRequest request = this.requestSupplier.get();
return insertCheckpoint(result, statusCode, request);
return result.checkpoint(statusCode + " from " +
this.httpMethod + " " + getUriToLog(this.uri) + " [DefaultWebClient]");
}
}
return null;
}

private <T> Mono<T> insertCheckpoint(Mono<T> result, HttpStatusCode statusCode, HttpRequest request) {
HttpMethod method = request.getMethod();
URI uri = getUriToLog(request);
return result.checkpoint(statusCode + " from " + method + " " + uri + " [DefaultWebClient]");
}

private static URI getUriToLog(HttpRequest request) {
URI uri = request.getURI();
private static URI getUriToLog(URI uri) {
if (StringUtils.hasText(uri.getQuery())) {
try {
uri = new URI(uri.getScheme(), uri.getHost(), uri.getPath(), null);
Expand Down

0 comments on commit c3ce847

Please sign in to comment.