Skip to content

Commit

Permalink
fix: list found httpclient implementations and output selected one
Browse files Browse the repository at this point in the history
Fixes #4935
  • Loading branch information
metacosm committed Mar 1, 2023
1 parent e1a316f commit 2291af4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
* Fix #4863: default HttpClient retry logic to 100ms interval
* Fix #4863: default HttpClient retry logic to 10 attempts
* Fix #4865: (java-generator) performance improvements
* Fix #4935: improve HTTP client implementation selection messages

#### Dependency Upgrade
* Fix #4655: Upgrade Fabric8 Kubernetes Model to Kubernetes v1.26.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Base64;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -73,7 +74,6 @@ public void before(BasicBuilder builder, HttpRequest request, RequestTags tags)
private static final Pattern IPV4_PATTERN = Pattern.compile(
"(http://|https://)?(?<ipAddressOrSubnet>(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])(\\/([1-2]\\d|3[0-2]|\\d))?)(\\D+|$)");
private static final Pattern INVALID_HOST_PATTERN = Pattern.compile("[^\\da-zA-Z.\\-/:]+");
private static final AtomicBoolean MULTIPLE_HTTP_CLIENT_WARNING_LOGGED = new AtomicBoolean();

private HttpClientUtils() {
}
Expand Down Expand Up @@ -145,22 +145,27 @@ public static HttpClient.Factory getHttpClientFactory() {
"No httpclient implementations found on the context classloader, please ensure your classpath includes an implementation jar");
}
}
LOGGER.info("Using httpclient {} factory", factory.getClass().getName());
return factory;
}

private static HttpClient.Factory getFactory(ServiceLoader<HttpClient.Factory> loader) {
HttpClient.Factory factory = null;
List<String> detectedFactories = new ArrayList<>(7);
for (HttpClient.Factory possible : loader) {
if (factory != null && MULTIPLE_HTTP_CLIENT_WARNING_LOGGED.compareAndSet(false, true)) {
LOGGER.warn("There are multiple httpclient implementation in the classpath, "
+ "choosing the first non-default implementation. "
+ "You should exclude dependencies that aren't needed or use an explicit association of the HttpClient.Factory.");
}
detectedFactories.add(possible.getClass().getName());
if (factory == null || (factory.isDefault() && !possible.isDefault())
|| (!factory.isDefault()) && factory.priority() < possible.priority()) {
factory = possible;
}
}

if (detectedFactories.size() > 1) {
LOGGER.warn("The following httpclient factories were detected on your classpath: {}, "
+ "choosing the first non-default implementation. "
+ "You should exclude dependencies that aren't needed or use an explicit association of the HttpClient.Factory.",
detectedFactories);
}
return factory;
}

Expand Down

0 comments on commit 2291af4

Please sign in to comment.