Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebClient automatic system loader #1903

Merged
merged 1 commit into from
Jun 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.helidon.common.http.MediaType;
import io.helidon.security.SecurityContext;
import io.helidon.webclient.WebClient;
import io.helidon.webclient.security.WebClientSecurity;
import io.helidon.webserver.Routing;
import io.helidon.webserver.ServerRequest;
import io.helidon.webserver.ServerResponse;
Expand All @@ -33,7 +34,9 @@
* Common code for both examples (builder and config based).
*/
final class SignatureExampleUtil {
private static final WebClient CLIENT = WebClient.create();
private static final WebClient CLIENT = WebClient.builder()
.addService(WebClientSecurity.create())
.build();

private static final int START_TIMEOUT_SECONDS = 10;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import io.helidon.config.Config;
import io.helidon.security.SecurityContext;
import io.helidon.webclient.WebClient;
import io.helidon.webclient.security.WebClientSecurity;
import io.helidon.webserver.Routing;
import io.helidon.webserver.ServerRequest;
import io.helidon.webserver.ServerResponse;
Expand Down Expand Up @@ -84,6 +85,7 @@ public void update(Routing.Rules rules) {
private void basicAuthOutbound(ServerRequest serverRequest, ServerResponse response) {
WebClient webClient = WebClient.builder()
.baseUri("http://localhost:" + Main.serverPort + "/greet/secure/basic")
.addService(WebClientSecurity.create())
.build();

webClient.get()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ server:
client:
follow-redirects: true
max-redirects: 5
services:
tracing:
security:

security:
providers:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.concurrent.ExecutionException;

import io.helidon.common.context.Context;
import io.helidon.config.Config;
import io.helidon.webclient.WebClient;
import io.helidon.webclient.WebClientResponse;
import io.helidon.webserver.WebServer;
Expand Down Expand Up @@ -55,6 +56,7 @@ void testTracingSuccess() throws ExecutionException, InterruptedException {
WebClient client = WebClient.builder()
.baseUri(uri)
.context(context)
.config(Config.create().get("client"))
.build();

client.get()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ void testTracingNoServerSuccess() throws ExecutionException, InterruptedExceptio
.baseUri(uri)
.context(context)
.addMediaSupport(JsonpSupport.create())
.config(CONFIG.get("client"))
.build();

WebClientResponse response = client.get()
Expand Down Expand Up @@ -93,6 +94,7 @@ void testTracingNoServerFailure() throws ExecutionException, InterruptedExceptio
.baseUri("http://localhost:" + webServer.port() + "/greet")
.context(context)
.addMediaSupport(JsonpSupport.create())
.config(CONFIG.get("client"))
.build();

WebClientResponse response = client.get()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@
import java.net.URL;
import java.time.Duration;
import java.time.temporal.TemporalUnit;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.ServiceLoader;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import io.helidon.common.HelidonFeatures;
import io.helidon.common.HelidonFlavor;
Expand Down Expand Up @@ -154,6 +153,7 @@ final class Builder implements io.helidon.common.Builder<WebClient>,
private final WebClientConfiguration.Builder<?, ?> configuration = NettyClient.SHARED_CONFIGURATION.get().derive();
private final HelidonServiceLoader.Builder<WebClientServiceProvider> services = HelidonServiceLoader
.builder(ServiceLoader.load(WebClientServiceProvider.class));
private final List<WebClientService> webClientServices = new ArrayList<>();

private Config config = Config.empty();

Expand All @@ -172,17 +172,7 @@ public WebClient build() {
* @return updated builder instance
*/
public Builder addService(WebClientService service) {
services.addService(new WebClientServiceProvider() {
@Override
public String configKey() {
return "ignored";
}

@Override
public WebClientService create(Config config) {
return service;
}
});
webClientServices.add(service);
return this;
}

Expand All @@ -196,18 +186,6 @@ public Builder addService(Supplier<? extends WebClientService> serviceSupplier)
return addService(serviceSupplier.get());
}


/**
* Exclude specific {@link WebClientServiceProvider} provider from being loaded.
*
* @param providerClass excluded provider
* @return updated builder instance
*/
public Builder exclude(Class<? extends WebClientServiceProvider> providerClass) {
services.addExcludedClass(providerClass);
return this;
}

/**
* Sets if Java Service loader should be used to load all {@link WebClientServiceProvider}.
*
Expand Down Expand Up @@ -440,16 +418,17 @@ WebClientConfiguration configuration() {

private List<WebClientService> services() {
Config servicesConfig = config.get("services");
servicesConfig.get("excludes").asList(String.class).orElse(Collections.emptyList())
.forEach(services::addExcludedClassName);

Config serviceConfig = servicesConfig.get("config");

return services.build()
services.build()
.asList()
.stream()
.map(it -> it.create(serviceConfig.get(it.configKey())))
.collect(Collectors.toList());
.forEach(provider -> {
Config providerConfig = servicesConfig.get(provider.configKey());
if (providerConfig.exists()) {
addService(provider.create(providerConfig));
}
});

return webClientServices;
}

}
Expand Down