diff --git a/azure-client-authentication/src/main/java/com/microsoft/azure/credentials/RefreshTokenClient.java b/azure-client-authentication/src/main/java/com/microsoft/azure/credentials/RefreshTokenClient.java index e31a8d98b07c..7f7887760635 100644 --- a/azure-client-authentication/src/main/java/com/microsoft/azure/credentials/RefreshTokenClient.java +++ b/azure-client-authentication/src/main/java/com/microsoft/azure/credentials/RefreshTokenClient.java @@ -18,12 +18,15 @@ import com.microsoft.rest.annotations.POST; import com.microsoft.rest.annotations.PathParam; import com.microsoft.rest.http.HttpClient; +import com.microsoft.rest.http.HttpClient.Configuration; import com.microsoft.rest.http.RxNettyAdapter; +import com.microsoft.rest.policy.RequestPolicy; import com.microsoft.rest.protocol.SerializerAdapter; import rx.Single; import rx.functions.Func1; import java.net.Proxy; +import java.util.Collections; import java.util.Date; /** @@ -43,9 +46,8 @@ final class RefreshTokenClient { } private static HttpClient createHttpClient(Proxy proxy) { - return new RxNettyAdapter.Builder() - .withProxy(proxy) - .build(); + return new RxNettyAdapter.Factory() + .create(new Configuration(Collections.emptyList(), proxy)); } AuthenticationResult refreshToken(String tenant, String clientId, String resource, String refreshToken, boolean isMultipleResoureRefreshToken) { diff --git a/client-runtime/src/main/java/com/microsoft/rest/RestClient.java b/client-runtime/src/main/java/com/microsoft/rest/RestClient.java index 9b72e6082ffc..8d7c97b3b15d 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/RestClient.java +++ b/client-runtime/src/main/java/com/microsoft/rest/RestClient.java @@ -30,6 +30,7 @@ * An instance of this class stores configuration for setting up specific service clients. */ public final class RestClient { + private final HttpClient.Factory httpClientFactory; private final HttpClient httpClient; private final Proxy proxy; private final String baseURL; @@ -53,21 +54,20 @@ private RestClient(RestClient.Builder builder) { this.logLevel = builder.logLevel; this.customPolicyFactories = builder.customPolicyFactories; - final RxNettyAdapter.Builder httpClientBuilder = new RxNettyAdapter.Builder() - .withRequestPolicy(new UserAgentPolicy.Factory(userAgent)) - .withRequestPolicy(new RetryPolicy.Factory()) - .withRequestPolicy(new AddCookiesPolicy.Factory()); - if (credentials != null) { - httpClientBuilder.withRequestPolicy(new CredentialsPolicy.Factory(credentials)); - } - httpClientBuilder.withRequestPolicies(customPolicyFactories) - .withRequestPolicy(new LoggingPolicy.Factory(logLevel)); + this.httpClientFactory = builder.httpClientFactory; - if (proxy != null) { - httpClientBuilder.withProxy(proxy); + List policyFactories = new ArrayList<>(); + policyFactories.add(new UserAgentPolicy.Factory(userAgent)); + policyFactories.add(new RetryPolicy.Factory()); + policyFactories.add(new AddCookiesPolicy.Factory()); + if (credentials != null) { + policyFactories.add(new CredentialsPolicy.Factory(credentials)); } + policyFactories.addAll(customPolicyFactories); + policyFactories.add(new LoggingPolicy.Factory(logLevel)); - this.httpClient = httpClientBuilder.build(); + HttpClient.Configuration configuration = new HttpClient.Configuration(policyFactories, proxy); + this.httpClient = httpClientFactory.create(configuration); } /** @@ -162,6 +162,7 @@ public static class Builder { private final long defaultReadTimeoutMillis = 10000; private final long defaultConnectionTimeoutMillis = 10000; + private HttpClient.Factory httpClientFactory; private Proxy proxy; /** The dynamic base URL with variables wrapped in "{" and "}". */ private String baseUrl; @@ -180,6 +181,7 @@ public static class Builder { private LogLevel logLevel = LogLevel.NONE; private Builder(final RestClient restClient) { + this.httpClientFactory = restClient.httpClientFactory; this.proxy = restClient.proxy; this.baseUrl = restClient.baseURL; this.userAgent = restClient.userAgent; @@ -194,7 +196,19 @@ private Builder(final RestClient restClient) { /** * Creates an instance of the builder. */ - public Builder() { } + public Builder() { + this.httpClientFactory = new RxNettyAdapter.Factory(); + } + + /** + * Sets the httpClientFactory. + * @param httpClientFactory the httpClientFactory to use. + * @return the builder itself for chaining. + */ + public Builder withHttpClientFactory(HttpClient.Factory httpClientFactory) { + this.httpClientFactory = httpClientFactory; + return this; + } /** * Sets the proxy. diff --git a/client-runtime/src/main/java/com/microsoft/rest/http/BufferedHttpResponse.java b/client-runtime/src/main/java/com/microsoft/rest/http/BufferedHttpResponse.java index 5780b19e4088..0f9c8794489c 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/http/BufferedHttpResponse.java +++ b/client-runtime/src/main/java/com/microsoft/rest/http/BufferedHttpResponse.java @@ -84,4 +84,4 @@ public String call(byte[] bytes) { public BufferedHttpResponse buffer() { return this; } -} \ No newline at end of file +} diff --git a/client-runtime/src/main/java/com/microsoft/rest/http/HttpClient.java b/client-runtime/src/main/java/com/microsoft/rest/http/HttpClient.java index 13971ff10aee..a497dca218a3 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/http/HttpClient.java +++ b/client-runtime/src/main/java/com/microsoft/rest/http/HttpClient.java @@ -9,6 +9,7 @@ import com.microsoft.rest.policy.RequestPolicy; import rx.Single; +import java.net.Proxy; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -57,4 +58,48 @@ public final Single sendRequestAsync(HttpRequest request) { * @return A {@link Single} representing the HTTP response that will arrive asynchronously. */ protected abstract Single sendRequestInternalAsync(HttpRequest request); + + /** + * The set of parameters used to create an HTTP client. + */ + public static final class Configuration { + private final List policyFactories; + private final Proxy proxy; + + /** + * @return The policy factories to use when creating RequestPolicies to intercept requests. + */ + public List policyFactories() { + return policyFactories; + } + + /** + * @return The optional proxy to use. + */ + public Proxy proxy() { + return proxy; + } + + /** + * Creates a Configuration. + * @param policyFactories The policy factories to use when creating RequestPolicies to intercept requests. + * @param proxy The optional proxy to use. + */ + public Configuration(List policyFactories, Proxy proxy) { + this.policyFactories = policyFactories; + this.proxy = proxy; + } + } + + /** + * Creates an HttpClient from a Configuration. + */ + public interface Factory { + /** + * Creates an HttpClient with the given Configuration. + * @param configuration the configuration. + * @return the HttpClient. + */ + HttpClient create(Configuration configuration); + } } diff --git a/client-runtime/src/main/java/com/microsoft/rest/http/HttpResponse.java b/client-runtime/src/main/java/com/microsoft/rest/http/HttpResponse.java index 0e386afe0871..eff1f1d5bc71 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/http/HttpResponse.java +++ b/client-runtime/src/main/java/com/microsoft/rest/http/HttpResponse.java @@ -66,4 +66,4 @@ public abstract class HttpResponse { public HttpResponse buffer() { return new BufferedHttpResponse(this); } -} \ No newline at end of file +} diff --git a/client-runtime/src/main/java/com/microsoft/rest/http/RxNettyAdapter.java b/client-runtime/src/main/java/com/microsoft/rest/http/RxNettyAdapter.java index 710b8a7682c7..94779f067e4e 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/http/RxNettyAdapter.java +++ b/client-runtime/src/main/java/com/microsoft/rest/http/RxNettyAdapter.java @@ -30,9 +30,7 @@ import java.net.URI; import java.net.URISyntaxException; import java.security.NoSuchAlgorithmException; -import java.util.ArrayList; import java.util.Arrays; -import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -174,63 +172,26 @@ protected InputStream next(InputStream state, Observer observer) } /** - * The builder class for building a RxNettyAdapter. + * The factory for creating an RxNettyAdapter. */ - public static class Builder { - private final List requestPolicyFactories = new ArrayList<>(); - private final List channelHandlerConfigs = new ArrayList<>(); - - /** - * Add the provided RequestPolicy.Factory to this Builder's configuration. - * @param requestPolicyFactory The RequestPolicy.Factory to add. - * @return The Builder itself for chaining. - */ - public Builder withRequestPolicy(RequestPolicy.Factory requestPolicyFactory) { - requestPolicyFactories.add(requestPolicyFactory); - return this; - } - - /** - * Add the provided RequestPolicy.Factories to this Builder's configuration. - * @param requestPolicyFactories The RequestPolicy.Factories to add. - * @return The Builder itself for chaining. - */ - public Builder withRequestPolicies(Collection requestPolicyFactories) { - this.requestPolicyFactories.addAll(requestPolicyFactories); - return this; - } - - /** - * Add the provided ChannelHandlerConfig to this Builder's configuration. - * @param channelHandlerConfig The ChannelHandlerConfig to add. - * @return The Builder itself for chaining. - */ - public Builder withChannelHandler(ChannelHandlerConfig channelHandlerConfig) { - channelHandlerConfigs.add(channelHandlerConfig); - return this; - } - - /** - * Add a Proxy to the RxNettyAdapter that will be built from this Builder. - * @param proxy The Proxy to add. - * @return The Builder itself for chaining. - */ - public Builder withProxy(final Proxy proxy) { - return withChannelHandler(new ChannelHandlerConfig(new Func0() { - @Override - public ChannelHandler call() { - return new HttpProxyHandler(proxy.address()); - } - }, - false)); - } + public static class Factory implements HttpClient.Factory { + @Override + public HttpClient create(final Configuration configuration) { + final List channelHandlerConfigs; + final Proxy proxy = configuration.proxy(); + if (proxy != null) { + ChannelHandlerConfig channelHandlerConfig = new ChannelHandlerConfig(new Func0() { + @Override + public ChannelHandler call() { + return new HttpProxyHandler(proxy.address()); + } + }, false); + channelHandlerConfigs = Collections.singletonList(channelHandlerConfig); + } else { + channelHandlerConfigs = Collections.emptyList(); + } - /** - * Build a RxNettyAdapter using this Builder's configuration. - * @return An RxNettyAdapter that uses this Builder's configuration. - */ - public RxNettyAdapter build() { - return new RxNettyAdapter(requestPolicyFactories, channelHandlerConfigs); + return new RxNettyAdapter(configuration.policyFactories(), channelHandlerConfigs); } } }