diff --git a/azure-client-runtime/src/main/java/com/microsoft/azure/v2/AzureProxy.java b/azure-client-runtime/src/main/java/com/microsoft/azure/v2/AzureProxy.java index f39072556579..413c58ea33e6 100644 --- a/azure-client-runtime/src/main/java/com/microsoft/azure/v2/AzureProxy.java +++ b/azure-client-runtime/src/main/java/com/microsoft/azure/v2/AzureProxy.java @@ -13,11 +13,12 @@ import com.microsoft.rest.v2.credentials.ServiceClientCredentials; import com.microsoft.rest.v2.http.HttpClient; import com.microsoft.rest.v2.http.HttpPipeline; +import com.microsoft.rest.v2.http.HttpPipelineBuilder; import com.microsoft.rest.v2.http.NettyClient; import com.microsoft.rest.v2.policy.AddCookiesPolicy; import com.microsoft.rest.v2.policy.CredentialsPolicy; import com.microsoft.rest.v2.policy.LoggingPolicy; -import com.microsoft.rest.v2.policy.RequestPolicy; +import com.microsoft.rest.v2.policy.RequestPolicyFactory; import com.microsoft.rest.v2.policy.RetryPolicy; import com.microsoft.rest.v2.protocol.SerializerAdapter; import com.microsoft.rest.v2.InvalidReturnTypeException; @@ -143,7 +144,7 @@ private static String getDefaultUserAgentString(Class swaggerInterface) { * @return the default HttpPipeline. */ public static HttpPipeline defaultPipeline(Class swaggerInterface) { - return defaultPipeline(swaggerInterface, (RequestPolicy.Factory) null); + return defaultPipeline(swaggerInterface, (RequestPolicyFactory) null); } /** @@ -165,9 +166,9 @@ public static HttpPipeline defaultPipeline(Class swaggerInterface, ServiceCli * pipeline. * @return the default HttpPipeline. */ - public static HttpPipeline defaultPipeline(Class swaggerInterface, RequestPolicy.Factory credentialsPolicy) { + public static HttpPipeline defaultPipeline(Class swaggerInterface, RequestPolicyFactory credentialsPolicy) { final HttpClient httpClient = new NettyClient.Factory().create(null); - final HttpPipeline.Builder builder = new HttpPipeline.Builder().withHttpClient(httpClient); + final HttpPipelineBuilder builder = new HttpPipelineBuilder().withHttpClient(httpClient); builder.withUserAgent(getDefaultUserAgentString(swaggerInterface)); builder.withRequestPolicy(new RetryPolicy.Factory()); builder.withRequestPolicy(new AddCookiesPolicy.Factory()); diff --git a/client-runtime/src/main/java/com/microsoft/rest/v2/RestProxy.java b/client-runtime/src/main/java/com/microsoft/rest/v2/RestProxy.java index 585a3658d53c..29b8d670fdf8 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/v2/RestProxy.java +++ b/client-runtime/src/main/java/com/microsoft/rest/v2/RestProxy.java @@ -15,12 +15,13 @@ import com.microsoft.rest.v2.http.HttpHeader; import com.microsoft.rest.v2.http.HttpHeaders; import com.microsoft.rest.v2.http.HttpPipeline; +import com.microsoft.rest.v2.http.HttpPipelineBuilder; import com.microsoft.rest.v2.http.HttpRequest; import com.microsoft.rest.v2.http.HttpResponse; import com.microsoft.rest.v2.http.UrlBuilder; import com.microsoft.rest.v2.policy.AddCookiesPolicy; import com.microsoft.rest.v2.policy.CredentialsPolicy; -import com.microsoft.rest.v2.policy.RequestPolicy; +import com.microsoft.rest.v2.policy.RequestPolicyFactory; import com.microsoft.rest.v2.policy.RetryPolicy; import com.microsoft.rest.v2.policy.UserAgentPolicy; import com.microsoft.rest.v2.protocol.SerializerAdapter; @@ -32,8 +33,8 @@ import io.reactivex.Maybe; import io.reactivex.Observable; import io.reactivex.Single; -import org.joda.time.DateTime; import io.reactivex.functions.Function; +import org.joda.time.DateTime; import java.io.IOException; import java.io.InputStream; @@ -591,7 +592,7 @@ public static SerializerAdapter createDefaultSerializer() { * @return the default HttpPipeline. */ public static HttpPipeline createDefaultPipeline() { - return createDefaultPipeline((RequestPolicy.Factory) null); + return createDefaultPipeline((RequestPolicyFactory) null); } /** @@ -609,8 +610,8 @@ public static HttpPipeline createDefaultPipeline(ServiceClientCredentials creden * pipeline. * @return the default HttpPipeline. */ - public static HttpPipeline createDefaultPipeline(RequestPolicy.Factory credentialsPolicy) { - final HttpPipeline.Builder builder = new HttpPipeline.Builder(); + public static HttpPipeline createDefaultPipeline(RequestPolicyFactory credentialsPolicy) { + final HttpPipelineBuilder builder = new HttpPipelineBuilder(); builder.withRequestPolicy(new UserAgentPolicy.Factory()); builder.withRequestPolicy(new RetryPolicy.Factory()); builder.withRequestPolicy(new AddCookiesPolicy.Factory()); diff --git a/client-runtime/src/main/java/com/microsoft/rest/v2/http/AbstractHttpPipelineLogger.java b/client-runtime/src/main/java/com/microsoft/rest/v2/http/AbstractHttpPipelineLogger.java new file mode 100644 index 000000000000..37970e5a31fe --- /dev/null +++ b/client-runtime/src/main/java/com/microsoft/rest/v2/http/AbstractHttpPipelineLogger.java @@ -0,0 +1,38 @@ +/** + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for + * license information. + */ + +package com.microsoft.rest.v2.http; + +/** + * An abstract Logger for HttpPipeline RequestPolicies that contains functionality that is + * common to Loggers. + */ +public abstract class AbstractHttpPipelineLogger implements HttpPipelineLogger { + private HttpPipelineLogLevel minimumLogLevel = HttpPipelineLogLevel.INFO; + + /** + * Set the minimum log level that this logger should log. Anything with a higher log level + * should be ignored. + * @param minimumLogLevel The minimum log level to set. + * @return This Logger. + */ + public AbstractHttpPipelineLogger withMinimumLogLevel(HttpPipelineLogLevel minimumLogLevel) { + this.minimumLogLevel = minimumLogLevel; + return this; + } + + @Override + public HttpPipelineLogLevel minimumLogLevel() { + return minimumLogLevel; + } + + protected static String format(String message, Object... formattedMessageArguments) { + if (formattedMessageArguments != null && formattedMessageArguments.length >= 1) { + message = String.format(message, formattedMessageArguments); + } + return message; + } +} \ No newline at end of file diff --git a/client-runtime/src/main/java/com/microsoft/rest/v2/http/HttpPipeline.java b/client-runtime/src/main/java/com/microsoft/rest/v2/http/HttpPipeline.java index 276735a339dd..2e9933805a05 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/v2/http/HttpPipeline.java +++ b/client-runtime/src/main/java/com/microsoft/rest/v2/http/HttpPipeline.java @@ -8,12 +8,10 @@ import com.microsoft.rest.v2.policy.RequestPolicy; import com.microsoft.rest.v2.policy.HttpClientRequestPolicyAdapter; -import com.microsoft.rest.v2.policy.UserAgentPolicy; +import com.microsoft.rest.v2.policy.RequestPolicyFactory; +import com.microsoft.rest.v2.policy.RequestPolicyOptions; import io.reactivex.Single; -import java.util.ArrayList; -import java.util.List; - /** * A collection of RequestPolicies that will be applied to a HTTP request before it is sent and will * be applied to a HTTP response when it is received. @@ -24,7 +22,7 @@ public final class HttpPipeline { * The factories appear in this list in the order that they will be applied to outgoing * requests. */ - private final RequestPolicy.Factory[] requestPolicyFactories; + private final RequestPolicyFactory[] requestPolicyFactories; /** * The HttpClient that will be used to send requests unless the sendRequestAsync() method is @@ -35,7 +33,7 @@ public final class HttpPipeline { /** * The optional properties that will be passed to each RequestPolicy as it is being created. */ - private final RequestPolicy.Options requestPolicyOptions; + private final RequestPolicyOptions requestPolicyOptions; /** * Create a new HttpPipeline with the provided RequestPolicy factories. @@ -43,30 +41,14 @@ public final class HttpPipeline { * responses that pass through this HttpPipeline. * @param options The optional properties that will be set on this HTTP pipelines. */ - private HttpPipeline(RequestPolicy.Factory[] requestPolicyFactories, Options options) { + HttpPipeline(RequestPolicyFactory[] requestPolicyFactories, HttpPipelineOptions options) { this.requestPolicyFactories = requestPolicyFactories; final HttpClient httpClient = (options != null && options.httpClient() != null ? options.httpClient() : HttpClient.createDefault()); this.httpClientRequestPolicyAdapter = new HttpClientRequestPolicyAdapter(httpClient); - final Logger logger = (options != null ? options.logger() : null); - this.requestPolicyOptions = new RequestPolicy.Options(logger); - } - - /** - * Get the HttpClient. - * @return the HttpClient. - */ - HttpClient httpClient() { - return httpClientRequestPolicyAdapter.httpClient(); - } - - /** - * Get the RequestPolicy factories that this pipeline contains. - * @return the RequestPolicy factories that this pipeline contains. - */ - RequestPolicy.Factory[] requestPolicyFactories() { - return requestPolicyFactories; + final HttpPipelineLogger logger = (options != null ? options.logger() : null); + this.requestPolicyOptions = new RequestPolicyOptions(logger); } /** @@ -77,7 +59,7 @@ RequestPolicy.Factory[] requestPolicyFactories() { */ public Single sendRequestAsync(HttpRequest httpRequest) { RequestPolicy requestPolicy = httpClientRequestPolicyAdapter; - for (final RequestPolicy.Factory requestPolicyFactory : requestPolicyFactories) { + for (final RequestPolicyFactory requestPolicyFactory : requestPolicyFactories) { requestPolicy = requestPolicyFactory.create(requestPolicy, requestPolicyOptions); } return requestPolicy.sendAsync(httpRequest); @@ -88,8 +70,8 @@ public Single sendRequestAsync(HttpRequest httpRequest) { * @param requestPolicyFactories The RequestPolicy factories to use. * @return The built HttpPipeline. */ - public static HttpPipeline build(RequestPolicy.Factory... requestPolicyFactories) { - return build((Options) null, requestPolicyFactories); + public static HttpPipeline build(RequestPolicyFactory... requestPolicyFactories) { + return build((HttpPipelineOptions) null, requestPolicyFactories); } /** @@ -98,8 +80,8 @@ public static HttpPipeline build(RequestPolicy.Factory... requestPolicyFactories * @param requestPolicyFactories The RequestPolicy factories to use. * @return The built HttpPipeline. */ - public static HttpPipeline build(HttpClient httpClient, RequestPolicy.Factory... requestPolicyFactories) { - return build(new Options().withHttpClient(httpClient), requestPolicyFactories); + public static HttpPipeline build(HttpClient httpClient, RequestPolicyFactory... requestPolicyFactories) { + return build(new HttpPipelineOptions().withHttpClient(httpClient), requestPolicyFactories); } /** @@ -108,339 +90,13 @@ public static HttpPipeline build(HttpClient httpClient, RequestPolicy.Factory... * @param requestPolicyFactories The RequestPolicy factories to use. * @return The built HttpPipeline. */ - public static HttpPipeline build(Options pipelineOptions, RequestPolicy.Factory... requestPolicyFactories) { - final HttpPipeline.Builder builder = new HttpPipeline.Builder(pipelineOptions); + public static HttpPipeline build(HttpPipelineOptions pipelineOptions, RequestPolicyFactory... requestPolicyFactories) { + final HttpPipelineBuilder builder = new HttpPipelineBuilder(pipelineOptions); if (requestPolicyFactories != null) { - for (final RequestPolicy.Factory requestPolicyFactory : requestPolicyFactories) { + for (final RequestPolicyFactory requestPolicyFactory : requestPolicyFactories) { builder.withRequestPolicy(requestPolicyFactory); } } return builder.build(); } - - /** - * A builder class that can be used to create a HttpPipeline. - */ - public static class Builder { - /** - * The optional properties that will be set on the created HTTP pipelines. - */ - private Options options; - - /** - * The list of RequestPolicy factories that will be applied to HTTP requests and responses. - * The factories appear in this list in the reverse order that they will be applied to - * outgoing requests. - */ - private final List requestPolicyFactories; - - /** - * Create a new HttpPipeline builder. - */ - public Builder() { - this(null); - } - - /** - * Create a new HttpPipeline builder. - * - * @param options The optional properties that will be set on the created HTTP pipelines. - */ - public Builder(Options options) { - this.options = options; - this.requestPolicyFactories = new ArrayList<>(); - } - - /** - * Get the RequestPolicy factories in this HttpPipeline builder. - * @return the RequestPolicy factories in this HttpPipeline builder. - */ - List requestPolicyFactories() { - return requestPolicyFactories; - } - - /** - * Get the options for this HttpPipeline builder. - * @return the options for this HttpPipeline builder. - */ - Options options() { - return options; - } - - /** - * Set the HttpClient that will be used by HttpPipelines that are created by this Builder. - * @param httpClient The HttpClient to use. - * @return This HttpPipeline builder. - */ - public Builder withHttpClient(HttpClient httpClient) { - if (options == null) { - options = new Options(); - } - options.withHttpClient(httpClient); - return this; - } - - /** - * Set the Logger that will be used for each RequestPolicy within the created HttpPipeline. - * @param logger The Logger to provide to each RequestPolicy. - * @return This HttpPipeline options object. - */ - public Builder withLogger(Logger logger) { - if (options == null) { - options = new Options(); - } - options.withLogger(logger); - return this; - } - - /** - * Add the provided RequestPolicy factory to this HttpPipeline builder. - * @param requestPolicyFactory The RequestPolicy factory to add to this HttpPipeline builder. - * @return This HttpPipeline builder. - */ - public Builder withRequestPolicy(RequestPolicy.Factory requestPolicyFactory) { - return withRequestPolicy(requestPolicyFactories.size(), requestPolicyFactory); - } - - /** - * Add the provided RequestPolicy factory to this HttpPipeline builder - * at the provided index in the pipeline. - * @param index The index to insert the provided RequestPolicy factory. - * @param requestPolicyFactory The RequestPolicy factory to add to this - * HttpPipeline builder. - * @return This HttpPipeline builder. - */ - public Builder withRequestPolicy(int index, RequestPolicy.Factory requestPolicyFactory) { - // The requestPolicyFactories list is in reverse order that the - // policies will be in. The caller of this method should be - // providing the index based on the policy list, not the factory - // list. - final int insertIndex = requestPolicyFactories.size() - index; - requestPolicyFactories.add(insertIndex, requestPolicyFactory); - return this; - } - - /** - * Add the provided RequestPolicy factories to this HttpPipeline builder. - * @param requestPolicyFactories The RequestPolicy factories to add to this - * HttpPipeline builder. - * @return This HttpPipeline builder. - */ - public Builder withRequestPolicies(RequestPolicy.Factory... requestPolicyFactories) { - for (RequestPolicy.Factory factory : requestPolicyFactories) { - withRequestPolicy(factory); - } - return this; - } - - /** - * Add the provided RequestPolicy factory to this HttpPipeline builder - * directly before the first instance of the provided RequestPolicy - * factory type. If the provided RequestPolicy factory type is not - * found, then the RequestPolicy factory will be added to the end of the - * pipeline. - * @param requestPolicyFactoryType The RequestPolicy factory type to - * search for. - * @param requestPolicyFactory The RequestPolicy factory to add. - * @return This HttpPipeline builder. - */ - public Builder withRequestPolicyBefore(Class requestPolicyFactoryType, RequestPolicy.Factory requestPolicyFactory) { - int searchIndex = 0; - for (final RequestPolicy.Factory factory : requestPolicyFactories) { - if (requestPolicyFactoryType.equals(factory.getClass())) { - break; - } - else { - ++searchIndex; - } - } - final int factoryCount = requestPolicyFactories.size(); - - if (searchIndex == factoryCount) { - withRequestPolicy(requestPolicyFactory); - } else { - final int insertIndex = searchIndex + 1; - requestPolicyFactories.add(insertIndex, requestPolicyFactory); - } - - return this; - } - - /** - * Add the provided RequestPolicy factory to this HttpPipeline builder - * directly after the first instance of the provided RequestPolicy - * factory type. If the provided RequestPolicy factory type is not - * found, then the RequestPolicy factory will be added to the end of the - * pipeline. - * @param requestPolicyFactoryType The RequestPolicy factory type to - * search for. - * @param requestPolicyFactory The RequestPolicy factory to add. - * @return This HttpPipeline builder. - */ - public Builder withRequestPolicyAfter(Class requestPolicyFactoryType, RequestPolicy.Factory requestPolicyFactory) { - int searchIndex = 0; - for (final RequestPolicy.Factory factory : requestPolicyFactories) { - if (requestPolicyFactoryType.equals(factory.getClass())) { - break; - } - else { - ++searchIndex; - } - } - final int factoryCount = requestPolicyFactories.size(); - - if (searchIndex == factoryCount) { - withRequestPolicy(requestPolicyFactory); - } else { - requestPolicyFactories.add(searchIndex, requestPolicyFactory); - } - - return this; - } - - /** - * Add a RequestPolicy that will add the providedd UserAgent header to each outgoing - * HttpRequest. - * @param userAgent The userAgent header value to add to each outgoing HttpRequest. - * @return This HttpPipeline builder. - */ - public Builder withUserAgent(String userAgent) { - return withRequestPolicy(new UserAgentPolicy.Factory(userAgent)); - } - - /** - * Create a new HttpPipeline from the RequestPolicy factories that have been added to this - * HttpPipeline builder. - * @return The created HttpPipeline. - */ - public HttpPipeline build() { - final int requestPolicyCount = requestPolicyFactories.size(); - final RequestPolicy.Factory[] requestPolicyFactoryArray = new RequestPolicy.Factory[requestPolicyCount]; - return new HttpPipeline(requestPolicyFactories.toArray(requestPolicyFactoryArray), options); - } - } - - /** - * A Logger that can be added to an HttpPipeline. This enables each RequestPolicy to log - * messages that can be used for debugging purposes. - */ - public interface Logger { - /** - * The log level threshold for what logs will be logged. - * @return The log level threshold for what logs will be logged. - */ - LogLevel minimumLogLevel(); - - /** - * Log the provided message. - * @param logLevel The LogLevel associated with this message. - * @param message The message to log. - * @param formattedArguments A variadic list of arguments that should be formatted into the - * provided message. - */ - void log(LogLevel logLevel, String message, Object... formattedArguments); - } - - /** - * An abstract Logger for HttpPipeline RequestPolicies that contains functionality that is - * common to Loggers. - */ - public abstract static class AbstractLogger implements Logger { - private HttpPipeline.LogLevel minimumLogLevel = HttpPipeline.LogLevel.INFO; - - /** - * Set the minimum log level that this logger should log. Anything with a higher log level - * should be ignored. - * @param minimumLogLevel The minimum log level to set. - * @return This Logger. - */ - public AbstractLogger withMinimumLogLevel(HttpPipeline.LogLevel minimumLogLevel) { - this.minimumLogLevel = minimumLogLevel; - return this; - } - - @Override - public HttpPipeline.LogLevel minimumLogLevel() { - return minimumLogLevel; - } - - protected static String format(String message, Object... formattedMessageArguments) { - if (formattedMessageArguments != null && formattedMessageArguments.length >= 1) { - message = String.format(message, formattedMessageArguments); - } - return message; - } - } - - /** - * The different levels of logs from HttpPipeline's RequestPolicies. - */ - public enum LogLevel { - /** - * A log level that indicates that no logs will be logged. - */ - OFF, - - /** - * An error log. - */ - ERROR, - - /** - * A warning log. - */ - WARNING, - - /** - * An information log. - */ - INFO - } - - /** - * The optional properties that can be set on an HttpPipeline. - */ - public static class Options { - private HttpClient httpClient; - private Logger logger; - - /** - * Configure the HttpClient that will be used for the created HttpPipeline. If no HttpClient - * is set (or if null is set), then a default HttpClient will be created for the - * HttpPipeline. - * @param httpClient the HttpClient to use for the created HttpPipeline. - * @return This HttpPipeline options object. - */ - public Options withHttpClient(HttpClient httpClient) { - this.httpClient = httpClient; - return this; - } - - /** - * Get the HttpClient that was set. - * @return The HttpClient that was set. - */ - HttpClient httpClient() { - return httpClient; - } - - /** - * Configure the Logger that will be used for each RequestPolicy within the created - * HttpPipeline. - * @param logger The Logger to provide to each RequestPolicy. - * @return This HttpPipeline options object. - */ - public Options withLogger(Logger logger) { - this.logger = logger; - return this; - } - - /** - * Get the Logger that was set. - * @return The Logger that was set. - */ - Logger logger() { - return logger; - } - } } diff --git a/client-runtime/src/main/java/com/microsoft/rest/v2/http/HttpPipelineBuilder.java b/client-runtime/src/main/java/com/microsoft/rest/v2/http/HttpPipelineBuilder.java new file mode 100644 index 000000000000..fdc7c9787096 --- /dev/null +++ b/client-runtime/src/main/java/com/microsoft/rest/v2/http/HttpPipelineBuilder.java @@ -0,0 +1,215 @@ +/** + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for + * license information. + */ + +package com.microsoft.rest.v2.http; + +import com.microsoft.rest.v2.policy.RequestPolicyFactory; +import com.microsoft.rest.v2.policy.UserAgentPolicy; + +import java.util.ArrayList; +import java.util.List; + +/** + * A builder class that can be used to create a HttpPipeline. + */ +public final class HttpPipelineBuilder { + /** + * The optional properties that will be set on the created HTTP pipelines. + */ + private HttpPipelineOptions options; + + /** + * The list of RequestPolicy factories that will be applied to HTTP requests and responses. + * The factories appear in this list in the reverse order that they will be applied to + * outgoing requests. + */ + private final List requestPolicyFactories; + + /** + * Create a new HttpPipeline builder. + */ + public HttpPipelineBuilder() { + this(null); + } + + /** + * Create a new HttpPipeline builder. + * + * @param options The optional properties that will be set on the created HTTP pipelines. + */ + public HttpPipelineBuilder(HttpPipelineOptions options) { + this.options = options; + this.requestPolicyFactories = new ArrayList<>(); + } + + /** + * Get the RequestPolicy factories in this HttpPipeline builder. + * @return the RequestPolicy factories in this HttpPipeline builder. + */ + List requestPolicyFactories() { + return requestPolicyFactories; + } + + /** + * Get the options for this HttpPipeline builder. + * @return the options for this HttpPipeline builder. + */ + HttpPipelineOptions options() { + return options; + } + + /** + * Set the HttpClient that will be used by HttpPipelines that are created by this Builder. + * @param httpClient The HttpClient to use. + * @return This HttpPipeline builder. + */ + public HttpPipelineBuilder withHttpClient(HttpClient httpClient) { + if (options == null) { + options = new HttpPipelineOptions(); + } + options.withHttpClient(httpClient); + return this; + } + + /** + * Set the Logger that will be used for each RequestPolicy within the created HttpPipeline. + * @param logger The Logger to provide to each RequestPolicy. + * @return This HttpPipeline options object. + */ + public HttpPipelineBuilder withLogger(HttpPipelineLogger logger) { + if (options == null) { + options = new HttpPipelineOptions(); + } + options.withLogger(logger); + return this; + } + + /** + * Add the provided RequestPolicy factory to this HttpPipeline builder. + * @param requestPolicyFactory The RequestPolicy factory to add to this HttpPipeline builder. + * @return This HttpPipeline builder. + */ + public HttpPipelineBuilder withRequestPolicy(RequestPolicyFactory requestPolicyFactory) { + return withRequestPolicy(requestPolicyFactories.size(), requestPolicyFactory); + } + + /** + * Add the provided RequestPolicy factory to this HttpPipeline builder + * at the provided index in the pipeline. + * @param index The index to insert the provided RequestPolicy factory. + * @param requestPolicyFactory The RequestPolicy factory to add to this + * HttpPipeline builder. + * @return This HttpPipeline builder. + */ + public HttpPipelineBuilder withRequestPolicy(int index, RequestPolicyFactory requestPolicyFactory) { + // The requestPolicyFactories list is in reverse order that the + // policies will be in. The caller of this method should be + // providing the index based on the policy list, not the factory + // list. + final int insertIndex = requestPolicyFactories.size() - index; + requestPolicyFactories.add(insertIndex, requestPolicyFactory); + return this; + } + + /** + * Add the provided RequestPolicy factories to this HttpPipeline builder. + * @param requestPolicyFactories The RequestPolicy factories to add to this + * HttpPipeline builder. + * @return This HttpPipeline builder. + */ + public HttpPipelineBuilder withRequestPolicies(RequestPolicyFactory... requestPolicyFactories) { + for (RequestPolicyFactory factory : requestPolicyFactories) { + withRequestPolicy(factory); + } + return this; + } + + /** + * Add the provided RequestPolicy factory to this HttpPipeline builder + * directly before the first instance of the provided RequestPolicy + * factory type. If the provided RequestPolicy factory type is not + * found, then the RequestPolicy factory will be added to the end of the + * pipeline. + * @param requestPolicyFactoryType The RequestPolicy factory type to + * search for. + * @param requestPolicyFactory The RequestPolicy factory to add. + * @return This HttpPipeline builder. + */ + public HttpPipelineBuilder withRequestPolicyBefore(Class requestPolicyFactoryType, RequestPolicyFactory requestPolicyFactory) { + int searchIndex = 0; + for (final RequestPolicyFactory factory : requestPolicyFactories) { + if (requestPolicyFactoryType.equals(factory.getClass())) { + break; + } + else { + ++searchIndex; + } + } + final int factoryCount = requestPolicyFactories.size(); + + if (searchIndex == factoryCount) { + withRequestPolicy(requestPolicyFactory); + } else { + final int insertIndex = searchIndex + 1; + requestPolicyFactories.add(insertIndex, requestPolicyFactory); + } + + return this; + } + + /** + * Add the provided RequestPolicy factory to this HttpPipeline builder + * directly after the first instance of the provided RequestPolicy + * factory type. If the provided RequestPolicy factory type is not + * found, then the RequestPolicy factory will be added to the end of the + * pipeline. + * @param requestPolicyFactoryType The RequestPolicy factory type to + * search for. + * @param requestPolicyFactory The RequestPolicy factory to add. + * @return This HttpPipeline builder. + */ + public HttpPipelineBuilder withRequestPolicyAfter(Class requestPolicyFactoryType, RequestPolicyFactory requestPolicyFactory) { + int searchIndex = 0; + for (final RequestPolicyFactory factory : requestPolicyFactories) { + if (requestPolicyFactoryType.equals(factory.getClass())) { + break; + } + else { + ++searchIndex; + } + } + final int factoryCount = requestPolicyFactories.size(); + + if (searchIndex == factoryCount) { + withRequestPolicy(requestPolicyFactory); + } else { + requestPolicyFactories.add(searchIndex, requestPolicyFactory); + } + + return this; + } + + /** + * Add a RequestPolicy that will add the providedd UserAgent header to each outgoing + * HttpRequest. + * @param userAgent The userAgent header value to add to each outgoing HttpRequest. + * @return This HttpPipeline builder. + */ + public HttpPipelineBuilder withUserAgent(String userAgent) { + return withRequestPolicy(new UserAgentPolicy.Factory(userAgent)); + } + + /** + * Create a new HttpPipeline from the RequestPolicy factories that have been added to this + * HttpPipeline builder. + * @return The created HttpPipeline. + */ + public HttpPipeline build() { + final int requestPolicyCount = requestPolicyFactories.size(); + final RequestPolicyFactory[] requestPolicyFactoryArray = new RequestPolicyFactory[requestPolicyCount]; + return new HttpPipeline(requestPolicyFactories.toArray(requestPolicyFactoryArray), options); + } +} \ No newline at end of file diff --git a/client-runtime/src/main/java/com/microsoft/rest/v2/http/HttpPipelineLogLevel.java b/client-runtime/src/main/java/com/microsoft/rest/v2/http/HttpPipelineLogLevel.java new file mode 100644 index 000000000000..523bbd4a800d --- /dev/null +++ b/client-runtime/src/main/java/com/microsoft/rest/v2/http/HttpPipelineLogLevel.java @@ -0,0 +1,32 @@ +/** + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for + * license information. + */ + +package com.microsoft.rest.v2.http; + +/** + * The different levels of logs from HttpPipeline's RequestPolicies. + */ +public enum HttpPipelineLogLevel { + /** + * A log level that indicates that no logs will be logged. + */ + OFF, + + /** + * An error log. + */ + ERROR, + + /** + * A warning log. + */ + WARNING, + + /** + * An information log. + */ + INFO +} \ No newline at end of file diff --git a/client-runtime/src/main/java/com/microsoft/rest/v2/http/HttpPipelineLogger.java b/client-runtime/src/main/java/com/microsoft/rest/v2/http/HttpPipelineLogger.java new file mode 100644 index 000000000000..bcb13d93bc16 --- /dev/null +++ b/client-runtime/src/main/java/com/microsoft/rest/v2/http/HttpPipelineLogger.java @@ -0,0 +1,29 @@ +/** + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for + * license information. + */ + +package com.microsoft.rest.v2.http; + +/** + * A Logger that can be added to an HttpPipeline. This enables each RequestPolicy to log + * messages that can be used for debugging purposes. + */ +public interface HttpPipelineLogger { + /** + * The log level threshold for what logs will be logged. + * @return The log level threshold for what logs will be logged. + */ + HttpPipelineLogLevel minimumLogLevel(); + + /** + * Log the provided message. + * @param logLevel The LogLevel associated with this message. + * @param message The message to log. + * @param formattedArguments A variadic list of arguments that should be formatted into the + * provided message. + */ + void log(HttpPipelineLogLevel logLevel, String message, Object... formattedArguments); +} + diff --git a/client-runtime/src/main/java/com/microsoft/rest/v2/http/HttpPipelineOptions.java b/client-runtime/src/main/java/com/microsoft/rest/v2/http/HttpPipelineOptions.java new file mode 100644 index 000000000000..1ca13801c257 --- /dev/null +++ b/client-runtime/src/main/java/com/microsoft/rest/v2/http/HttpPipelineOptions.java @@ -0,0 +1,54 @@ +/** + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for + * license information. + */ + +package com.microsoft.rest.v2.http; + +/** + * The optional properties that can be set on an HttpPipeline. + */ +public class HttpPipelineOptions { + private HttpClient httpClient; + private HttpPipelineLogger logger; + + /** + * Configure the HttpClient that will be used for the created HttpPipeline. If no HttpClient + * is set (or if null is set), then a default HttpClient will be created for the + * HttpPipeline. + * @param httpClient the HttpClient to use for the created HttpPipeline. + * @return This HttpPipeline options object. + */ + public HttpPipelineOptions withHttpClient(HttpClient httpClient) { + this.httpClient = httpClient; + return this; + } + + /** + * Get the HttpClient that was set. + * @return The HttpClient that was set. + */ + HttpClient httpClient() { + return httpClient; + } + + /** + * Configure the Logger that will be used for each RequestPolicy within the created + * HttpPipeline. + * @param logger The Logger to provide to each RequestPolicy. + * @return This HttpPipeline options object. + */ + public HttpPipelineOptions withLogger(HttpPipelineLogger logger) { + this.logger = logger; + return this; + } + + /** + * Get the Logger that was set. + * @return The Logger that was set. + */ + HttpPipelineLogger logger() { + return logger; + } +} \ No newline at end of file diff --git a/client-runtime/src/main/java/com/microsoft/rest/v2/http/Slf4jLogger.java b/client-runtime/src/main/java/com/microsoft/rest/v2/http/Slf4jLogger.java index 50c0fb75e09f..b539c8ced86c 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/v2/http/Slf4jLogger.java +++ b/client-runtime/src/main/java/com/microsoft/rest/v2/http/Slf4jLogger.java @@ -9,7 +9,7 @@ /** * An adapter that connects an HttpPipeline.Logger to an slf4j.Logger. */ -public class Slf4jLogger extends HttpPipeline.AbstractLogger { +public class Slf4jLogger extends AbstractHttpPipelineLogger { private final org.slf4j.Logger slf4jLogger; /** @@ -21,7 +21,7 @@ public Slf4jLogger(org.slf4j.Logger slf4jLogger) { } @Override - public void log(HttpPipeline.LogLevel logLevel, String message, Object... formattedArguments) { + public void log(HttpPipelineLogLevel logLevel, String message, Object... formattedArguments) { message = format(message, formattedArguments); switch (logLevel) { case ERROR: diff --git a/client-runtime/src/main/java/com/microsoft/rest/v2/http/SystemOutLogger.java b/client-runtime/src/main/java/com/microsoft/rest/v2/http/SystemOutLogger.java index 61c31ea675e6..b93095bac089 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/v2/http/SystemOutLogger.java +++ b/client-runtime/src/main/java/com/microsoft/rest/v2/http/SystemOutLogger.java @@ -9,9 +9,9 @@ /** * A HttpPipeline RequestPolicy logger that logs to the StdOut/System.out stream. */ -public class SystemOutLogger extends HttpPipeline.AbstractLogger { +public class SystemOutLogger extends AbstractHttpPipelineLogger { @Override - public void log(HttpPipeline.LogLevel logLevel, String message, Object... formattedArguments) { + public void log(HttpPipelineLogLevel logLevel, String message, Object... formattedArguments) { System.out.println(logLevel + ") " + format(message, formattedArguments)); } } diff --git a/client-runtime/src/main/java/com/microsoft/rest/v2/http/UrlBuilder.java b/client-runtime/src/main/java/com/microsoft/rest/v2/http/UrlBuilder.java index 59796024a8b1..1471f6a18f22 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/v2/http/UrlBuilder.java +++ b/client-runtime/src/main/java/com/microsoft/rest/v2/http/UrlBuilder.java @@ -22,7 +22,7 @@ public class UrlBuilder { * @return This UrlBuilder so that multiple setters can be chained together. */ public UrlBuilder withScheme(String scheme) { - return with(scheme, UrlTokenizer.State.SCHEME); + return with(scheme, UrlTokenizerState.SCHEME); } /** @@ -39,7 +39,7 @@ public String scheme() { * @return This UrlBuilder so that multiple setters can be chained together. */ public UrlBuilder withHost(String host) { - return with(host, UrlTokenizer.State.SCHEME_OR_HOST); + return with(host, UrlTokenizerState.SCHEME_OR_HOST); } /** @@ -56,7 +56,7 @@ public String host() { * @return This UrlBuilder so that multiple setters can be chained together. */ public UrlBuilder withPort(String port) { - return with(port, UrlTokenizer.State.PORT); + return with(port, UrlTokenizerState.PORT); } /** @@ -82,7 +82,7 @@ public Integer port() { * @return This UrlBuilder so that multiple setters can be chained together. */ public UrlBuilder withPath(String path) { - return with(path, UrlTokenizer.State.PATH); + return with(path, UrlTokenizerState.PATH); } /** @@ -117,7 +117,7 @@ public UrlBuilder addQueryParameter(String queryParameterName, String queryParam * @return This UrlBuilder so that multiple setters can be chained together. */ public UrlBuilder withQuery(String query) { - return with(query, UrlTokenizer.State.QUERY); + return with(query, UrlTokenizerState.QUERY); } /** @@ -128,12 +128,12 @@ public String query() { return query; } - private UrlBuilder with(String text, UrlTokenizer.State startState) { + private UrlBuilder with(String text, UrlTokenizerState startState) { final UrlTokenizer tokenizer = new UrlTokenizer(text, startState); while (tokenizer.next()) { final UrlToken token = tokenizer.current(); final String tokenText = token.text(); - final UrlToken.Type tokenType = token.type(); + final UrlTokenType tokenType = token.type(); switch (tokenType) { case SCHEME: scheme = emptyToNull(tokenText); @@ -213,7 +213,7 @@ public String toString() { */ public static UrlBuilder parse(String url) { final UrlBuilder result = new UrlBuilder(); - result.with(url, UrlTokenizer.State.SCHEME_OR_HOST); + result.with(url, UrlTokenizerState.SCHEME_OR_HOST); return result; } diff --git a/client-runtime/src/main/java/com/microsoft/rest/v2/http/UrlToken.java b/client-runtime/src/main/java/com/microsoft/rest/v2/http/UrlToken.java index 8cb7dbf9d604..3c3d3e7550a9 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/v2/http/UrlToken.java +++ b/client-runtime/src/main/java/com/microsoft/rest/v2/http/UrlToken.java @@ -8,9 +8,9 @@ class UrlToken { private final String text; - private final Type type; + private final UrlTokenType type; - UrlToken(String text, Type type) { + UrlToken(String text, UrlTokenType type) { this.text = text; this.type = type; } @@ -19,7 +19,7 @@ String text() { return text; } - Type type() { + UrlTokenType type() { return type; } @@ -43,34 +43,22 @@ public int hashCode() { } static UrlToken scheme(String text) { - return new UrlToken(text, Type.SCHEME); + return new UrlToken(text, UrlTokenType.SCHEME); } static UrlToken host(String text) { - return new UrlToken(text, Type.HOST); + return new UrlToken(text, UrlTokenType.HOST); } static UrlToken port(String text) { - return new UrlToken(text, Type.PORT); + return new UrlToken(text, UrlTokenType.PORT); } static UrlToken path(String text) { - return new UrlToken(text, Type.PATH); + return new UrlToken(text, UrlTokenType.PATH); } static UrlToken query(String text) { - return new UrlToken(text, Type.QUERY); - } - - enum Type { - SCHEME, - - HOST, - - PORT, - - PATH, - - QUERY, + return new UrlToken(text, UrlTokenType.QUERY); } } diff --git a/client-runtime/src/main/java/com/microsoft/rest/v2/http/UrlTokenType.java b/client-runtime/src/main/java/com/microsoft/rest/v2/http/UrlTokenType.java new file mode 100644 index 000000000000..7654b11173e7 --- /dev/null +++ b/client-runtime/src/main/java/com/microsoft/rest/v2/http/UrlTokenType.java @@ -0,0 +1,19 @@ +/** + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for + * license information. + */ + +package com.microsoft.rest.v2.http; + +enum UrlTokenType { + SCHEME, + + HOST, + + PORT, + + PATH, + + QUERY, +} \ No newline at end of file diff --git a/client-runtime/src/main/java/com/microsoft/rest/v2/http/UrlTokenizer.java b/client-runtime/src/main/java/com/microsoft/rest/v2/http/UrlTokenizer.java index 591224c076b8..488ad5335807 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/v2/http/UrlTokenizer.java +++ b/client-runtime/src/main/java/com/microsoft/rest/v2/http/UrlTokenizer.java @@ -9,15 +9,15 @@ class UrlTokenizer { private final String text; private final int textLength; - private State state; + private UrlTokenizerState state; private int currentIndex; private UrlToken currentToken; UrlTokenizer(String text) { - this(text, State.SCHEME_OR_HOST); + this(text, UrlTokenizerState.SCHEME_OR_HOST); } - UrlTokenizer(String text, State state) { + UrlTokenizer(String text, UrlTokenizerState state) { this.text = text; this.textLength = (text == null ? 0 : text.length()); this.state = state; @@ -65,10 +65,10 @@ boolean next() { final String scheme = readUntilNotLetterOrDigit(); currentToken = UrlToken.scheme(scheme); if (!hasCurrentCharacter()) { - state = State.DONE; + state = UrlTokenizerState.DONE; } else { - state = State.HOST; + state = UrlTokenizerState.HOST; } break; @@ -76,25 +76,25 @@ boolean next() { final String schemeOrHost = readUntilCharacter(':', '/', '?'); if (!hasCurrentCharacter()) { currentToken = UrlToken.host(schemeOrHost); - state = State.DONE; + state = UrlTokenizerState.DONE; } else if (currentCharacter() == ':') { if (peekCharacters(3).equals("://")) { currentToken = UrlToken.scheme(schemeOrHost); - state = State.HOST; + state = UrlTokenizerState.HOST; } else { currentToken = UrlToken.host(schemeOrHost); - state = State.PORT; + state = UrlTokenizerState.PORT; } } else if (currentCharacter() == '/') { currentToken = UrlToken.host(schemeOrHost); - state = State.PATH; + state = UrlTokenizerState.PATH; } else if (currentCharacter() == '?') { currentToken = UrlToken.host(schemeOrHost); - state = State.QUERY; + state = UrlTokenizerState.QUERY; } break; @@ -107,16 +107,16 @@ else if (currentCharacter() == '?') { currentToken = UrlToken.host(host); if (!hasCurrentCharacter()) { - state = State.DONE; + state = UrlTokenizerState.DONE; } else if (currentCharacter() == ':') { - state = State.PORT; + state = UrlTokenizerState.PORT; } else if (currentCharacter() == '/') { - state = State.PATH; + state = UrlTokenizerState.PATH; } else { - state = State.QUERY; + state = UrlTokenizerState.QUERY; } break; @@ -129,13 +129,13 @@ else if (currentCharacter() == '/') { currentToken = UrlToken.port(port); if (!hasCurrentCharacter()) { - state = State.DONE; + state = UrlTokenizerState.DONE; } else if (currentCharacter() == '/') { - state = State.PATH; + state = UrlTokenizerState.PATH; } else { - state = State.QUERY; + state = UrlTokenizerState.QUERY; } break; @@ -144,10 +144,10 @@ else if (currentCharacter() == '/') { currentToken = UrlToken.path(path); if (!hasCurrentCharacter()) { - state = State.DONE; + state = UrlTokenizerState.DONE; } else { - state = State.QUERY; + state = UrlTokenizerState.QUERY; } break; @@ -158,7 +158,7 @@ else if (currentCharacter() == '/') { final String query = readRemaining(); currentToken = UrlToken.query(query); - state = State.DONE; + state = UrlTokenizerState.DONE; break; default: @@ -228,20 +228,4 @@ private String readRemaining() { } return result; } - - enum State { - SCHEME, - - SCHEME_OR_HOST, - - HOST, - - PORT, - - PATH, - - QUERY, - - DONE - } } diff --git a/client-runtime/src/main/java/com/microsoft/rest/v2/http/UrlTokenizerState.java b/client-runtime/src/main/java/com/microsoft/rest/v2/http/UrlTokenizerState.java new file mode 100644 index 000000000000..c4f34ddbb653 --- /dev/null +++ b/client-runtime/src/main/java/com/microsoft/rest/v2/http/UrlTokenizerState.java @@ -0,0 +1,23 @@ +/** + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for + * license information. + */ + +package com.microsoft.rest.v2.http; + +enum UrlTokenizerState { + SCHEME, + + SCHEME_OR_HOST, + + HOST, + + PORT, + + PATH, + + QUERY, + + DONE +} \ No newline at end of file diff --git a/client-runtime/src/main/java/com/microsoft/rest/v2/policy/AbstractRequestPolicy.java b/client-runtime/src/main/java/com/microsoft/rest/v2/policy/AbstractRequestPolicy.java index 94746519eee7..487491fc355b 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/v2/policy/AbstractRequestPolicy.java +++ b/client-runtime/src/main/java/com/microsoft/rest/v2/policy/AbstractRequestPolicy.java @@ -6,21 +6,21 @@ package com.microsoft.rest.v2.policy; -import com.microsoft.rest.v2.http.HttpPipeline; +import com.microsoft.rest.v2.http.HttpPipelineLogLevel; /** * An abstract RequestPolicy base-class. */ public abstract class AbstractRequestPolicy implements RequestPolicy { private final RequestPolicy nextPolicy; - private final Options options; + private final RequestPolicyOptions options; /** * Initialize the fields for this AbstractRequestPolicy. * @param nextPolicy The next RequestPolicy in the policy chain. * @param options The options for this RequestPolicy. */ - protected AbstractRequestPolicy(RequestPolicy nextPolicy, Options options) { + protected AbstractRequestPolicy(RequestPolicy nextPolicy, RequestPolicyOptions options) { this.nextPolicy = nextPolicy; this.options = options; } @@ -37,7 +37,7 @@ protected RequestPolicy nextPolicy() { * Get the options that were provided to this AbstractRequestPolicy. * @return The options that were provided to this AbstractRequestPolicy. */ - protected Options options() { + protected RequestPolicyOptions options() { return options; } @@ -46,7 +46,7 @@ protected Options options() { * @param logLevel The log level of the log that will be logged. * @return Whether or not a log with the provided log level should be logged. */ - public boolean shouldLog(HttpPipeline.LogLevel logLevel) { + public boolean shouldLog(HttpPipelineLogLevel logLevel) { return options != null && options.shouldLog(logLevel); } @@ -57,7 +57,7 @@ public boolean shouldLog(HttpPipeline.LogLevel logLevel) { * @param message The message of this log. * @param formattedMessageArguments The formatted arguments to apply to the message. */ - protected void log(HttpPipeline.LogLevel logLevel, String message, Object... formattedMessageArguments) { + protected void log(HttpPipelineLogLevel logLevel, String message, Object... formattedMessageArguments) { if (options != null) { options.log(logLevel, message, formattedMessageArguments); } diff --git a/client-runtime/src/main/java/com/microsoft/rest/v2/policy/AddCookiesPolicy.java b/client-runtime/src/main/java/com/microsoft/rest/v2/policy/AddCookiesPolicy.java index 71b91f53e29b..85c7be146325 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/v2/policy/AddCookiesPolicy.java +++ b/client-runtime/src/main/java/com/microsoft/rest/v2/policy/AddCookiesPolicy.java @@ -72,11 +72,11 @@ public HttpResponse apply(HttpResponse httpResponse) throws Exception { /** * Factory for creating AddCookiesPolicy. */ - public static final class Factory implements RequestPolicy.Factory { + public static final class Factory implements RequestPolicyFactory { private final CookieHandler cookies = new CookieManager(); @Override - public RequestPolicy create(RequestPolicy next, RequestPolicy.Options options) { + public RequestPolicy create(RequestPolicy next, RequestPolicyOptions options) { return new AddCookiesPolicy(cookies, next); } } diff --git a/client-runtime/src/main/java/com/microsoft/rest/v2/policy/AddHeadersPolicy.java b/client-runtime/src/main/java/com/microsoft/rest/v2/policy/AddHeadersPolicy.java index ee9bb54e1f5c..468f3b126adc 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/v2/policy/AddHeadersPolicy.java +++ b/client-runtime/src/main/java/com/microsoft/rest/v2/policy/AddHeadersPolicy.java @@ -32,7 +32,7 @@ private AddHeadersPolicy(HttpHeaders headers, RequestPolicy next) { /** * Factory to create AddHeadersPolicy. */ - public static class Factory implements RequestPolicy.Factory { + public static class Factory implements RequestPolicyFactory { private final HttpHeaders headers; /** @@ -44,7 +44,7 @@ public Factory(HttpHeaders headers) { } @Override - public RequestPolicy create(RequestPolicy next, RequestPolicy.Options options) { + public RequestPolicy create(RequestPolicy next, RequestPolicyOptions options) { return new AddHeadersPolicy(headers, next); } } diff --git a/client-runtime/src/main/java/com/microsoft/rest/v2/policy/CredentialsPolicy.java b/client-runtime/src/main/java/com/microsoft/rest/v2/policy/CredentialsPolicy.java index 716706a24e8e..0c8d1f1eb39d 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/v2/policy/CredentialsPolicy.java +++ b/client-runtime/src/main/java/com/microsoft/rest/v2/policy/CredentialsPolicy.java @@ -20,7 +20,7 @@ public final class CredentialsPolicy implements RequestPolicy { /** * Factory which instantiates CredentialsPolicy. */ - public static class Factory implements RequestPolicy.Factory { + public static class Factory implements RequestPolicyFactory { private final ServiceClientCredentials credentials; /** @@ -32,7 +32,7 @@ public Factory(ServiceClientCredentials credentials) { } @Override - public RequestPolicy create(RequestPolicy next, RequestPolicy.Options options) { + public RequestPolicy create(RequestPolicy next, RequestPolicyOptions options) { return new CredentialsPolicy(credentials, next); } } diff --git a/client-runtime/src/main/java/com/microsoft/rest/v2/policy/HostPolicy.java b/client-runtime/src/main/java/com/microsoft/rest/v2/policy/HostPolicy.java index a2a808b25663..d45c96cb7e84 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/v2/policy/HostPolicy.java +++ b/client-runtime/src/main/java/com/microsoft/rest/v2/policy/HostPolicy.java @@ -6,8 +6,7 @@ package com.microsoft.rest.v2.policy; - -import com.microsoft.rest.v2.http.HttpPipeline; +import com.microsoft.rest.v2.http.HttpPipelineLogLevel; import com.microsoft.rest.v2.http.HttpRequest; import com.microsoft.rest.v2.http.HttpResponse; import com.microsoft.rest.v2.http.UrlBuilder; @@ -19,15 +18,15 @@ public class HostPolicy extends AbstractRequestPolicy { private final String host; - HostPolicy(RequestPolicy nextPolicy, Options options, String host) { + HostPolicy(RequestPolicy nextPolicy, RequestPolicyOptions options, String host) { super(nextPolicy, options); this.host = host; } @Override public Single sendAsync(HttpRequest request) { - if (shouldLog(HttpPipeline.LogLevel.INFO)) { - log(HttpPipeline.LogLevel.INFO, "Setting host to {0}", host); + if (shouldLog(HttpPipelineLogLevel.INFO)) { + log(HttpPipelineLogLevel.INFO, "Setting host to {0}", host); } final UrlBuilder urlBuilder = UrlBuilder.parse(request.url()); request.withUrl(urlBuilder.withHost(host).toString()); @@ -37,7 +36,7 @@ public Single sendAsync(HttpRequest request) { /** * A RequestPolicy.Factory class that creates HostPolicy objects. */ - public static class Factory implements RequestPolicy.Factory { + public static class Factory implements RequestPolicyFactory { private final String host; /** @@ -49,7 +48,7 @@ public Factory(String host) { } @Override - public HostPolicy create(RequestPolicy next, Options options) { + public HostPolicy create(RequestPolicy next, RequestPolicyOptions options) { return new HostPolicy(next, options, host); } } diff --git a/client-runtime/src/main/java/com/microsoft/rest/v2/policy/LoggingPolicy.java b/client-runtime/src/main/java/com/microsoft/rest/v2/policy/LoggingPolicy.java index ee7fe38e0077..f15e510d179d 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/v2/policy/LoggingPolicy.java +++ b/client-runtime/src/main/java/com/microsoft/rest/v2/policy/LoggingPolicy.java @@ -28,7 +28,7 @@ public final class LoggingPolicy implements RequestPolicy { /** * Factory for creating LoggingPolicy instances in a chain. */ - public static class Factory implements RequestPolicy.Factory { + public static class Factory implements RequestPolicyFactory { private final LogLevel logLevel; /** @@ -40,7 +40,7 @@ public Factory(LogLevel logLevel) { } @Override - public RequestPolicy create(RequestPolicy next, RequestPolicy.Options options) { + public RequestPolicy create(RequestPolicy next, RequestPolicyOptions options) { return new LoggingPolicy(logLevel, next); } } diff --git a/client-runtime/src/main/java/com/microsoft/rest/v2/policy/PortPolicy.java b/client-runtime/src/main/java/com/microsoft/rest/v2/policy/PortPolicy.java index 917479855d52..15a0cebeb51c 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/v2/policy/PortPolicy.java +++ b/client-runtime/src/main/java/com/microsoft/rest/v2/policy/PortPolicy.java @@ -6,8 +6,7 @@ package com.microsoft.rest.v2.policy; - -import com.microsoft.rest.v2.http.HttpPipeline; +import com.microsoft.rest.v2.http.HttpPipelineLogLevel; import com.microsoft.rest.v2.http.HttpRequest; import com.microsoft.rest.v2.http.HttpResponse; import com.microsoft.rest.v2.http.UrlBuilder; @@ -20,7 +19,7 @@ public class PortPolicy extends AbstractRequestPolicy { private final int port; private final boolean overwrite; - PortPolicy(RequestPolicy nextPolicy, Options options, int port, boolean overwrite) { + PortPolicy(RequestPolicy nextPolicy, RequestPolicyOptions options, int port, boolean overwrite) { super(nextPolicy, options); this.port = port; this.overwrite = overwrite; @@ -30,8 +29,8 @@ public class PortPolicy extends AbstractRequestPolicy { public Single sendAsync(HttpRequest request) { final UrlBuilder urlBuilder = UrlBuilder.parse(request.url()); if (overwrite || urlBuilder.port() == null) { - if (shouldLog(HttpPipeline.LogLevel.INFO)) { - log(HttpPipeline.LogLevel.INFO, "Changing port to {0}", port); + if (shouldLog(HttpPipelineLogLevel.INFO)) { + log(HttpPipelineLogLevel.INFO, "Changing port to {0}", port); } request.withUrl(urlBuilder.withPort(port).toString()); } @@ -41,7 +40,7 @@ public Single sendAsync(HttpRequest request) { /** * A RequestPolicy.Factory class that creates PortPolicy objects. */ - public static class Factory implements RequestPolicy.Factory { + public static class Factory implements RequestPolicyFactory { private final int port; private final boolean overwrite; @@ -64,7 +63,7 @@ public Factory(int port, boolean overwrite) { } @Override - public RequestPolicy create(RequestPolicy next, Options options) { + public RequestPolicy create(RequestPolicy next, RequestPolicyOptions options) { return new PortPolicy(next, options, port, overwrite); } } diff --git a/client-runtime/src/main/java/com/microsoft/rest/v2/policy/ProtocolPolicy.java b/client-runtime/src/main/java/com/microsoft/rest/v2/policy/ProtocolPolicy.java index 5de418fafc0e..41ac990fcbeb 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/v2/policy/ProtocolPolicy.java +++ b/client-runtime/src/main/java/com/microsoft/rest/v2/policy/ProtocolPolicy.java @@ -6,8 +6,7 @@ package com.microsoft.rest.v2.policy; - -import com.microsoft.rest.v2.http.HttpPipeline; +import com.microsoft.rest.v2.http.HttpPipelineLogLevel; import com.microsoft.rest.v2.http.HttpRequest; import com.microsoft.rest.v2.http.HttpResponse; import com.microsoft.rest.v2.http.UrlBuilder; @@ -20,7 +19,7 @@ public class ProtocolPolicy extends AbstractRequestPolicy { private final String protocol; private final boolean overwrite; - ProtocolPolicy(RequestPolicy nextPolicy, Options options, String protocol, boolean overwrite) { + ProtocolPolicy(RequestPolicy nextPolicy, RequestPolicyOptions options, String protocol, boolean overwrite) { super(nextPolicy, options); this.protocol = protocol; this.overwrite = overwrite; @@ -30,8 +29,8 @@ public class ProtocolPolicy extends AbstractRequestPolicy { public Single sendAsync(HttpRequest request) { final UrlBuilder urlBuilder = UrlBuilder.parse(request.url()); if (overwrite || urlBuilder.scheme() == null) { - if (shouldLog(HttpPipeline.LogLevel.INFO)) { - log(HttpPipeline.LogLevel.INFO, "Setting protocol to {0}", protocol); + if (shouldLog(HttpPipelineLogLevel.INFO)) { + log(HttpPipelineLogLevel.INFO, "Setting protocol to {0}", protocol); } request.withUrl(urlBuilder.withScheme(protocol).toString()); } @@ -41,7 +40,7 @@ public Single sendAsync(HttpRequest request) { /** * A RequestPolicy.Factory class that creates ProtocolPolicy objects. */ - public static class Factory implements RequestPolicy.Factory { + public static class Factory implements RequestPolicyFactory { private final String protocol; private final boolean overwrite; @@ -64,7 +63,7 @@ public Factory(String protocol, boolean overwrite) { } @Override - public ProtocolPolicy create(RequestPolicy next, Options options) { + public ProtocolPolicy create(RequestPolicy next, RequestPolicyOptions options) { return new ProtocolPolicy(next, options, protocol, overwrite); } } diff --git a/client-runtime/src/main/java/com/microsoft/rest/v2/policy/ProxyAuthenticationPolicy.java b/client-runtime/src/main/java/com/microsoft/rest/v2/policy/ProxyAuthenticationPolicy.java index c28d43ea584a..9723e33f8791 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/v2/policy/ProxyAuthenticationPolicy.java +++ b/client-runtime/src/main/java/com/microsoft/rest/v2/policy/ProxyAuthenticationPolicy.java @@ -37,7 +37,7 @@ public Single sendAsync(HttpRequest request) { /** * Factory for creating ProxyAuthenticationPolicy. */ - public static class Factory implements RequestPolicy.Factory { + public static class Factory implements RequestPolicyFactory { private final String username; private final String password; @@ -52,7 +52,7 @@ public Factory(String username, String password) { } @Override - public RequestPolicy create(RequestPolicy next, RequestPolicy.Options options) { + public RequestPolicy create(RequestPolicy next, RequestPolicyOptions options) { return new ProxyAuthenticationPolicy(username, password, next); } } diff --git a/client-runtime/src/main/java/com/microsoft/rest/v2/policy/RequestIdPolicy.java b/client-runtime/src/main/java/com/microsoft/rest/v2/policy/RequestIdPolicy.java index ffa6d5b650a2..11ea91ad856e 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/v2/policy/RequestIdPolicy.java +++ b/client-runtime/src/main/java/com/microsoft/rest/v2/policy/RequestIdPolicy.java @@ -22,9 +22,9 @@ public final class RequestIdPolicy implements RequestPolicy { /** * Factory which instantiates RequestIdPolicy. */ - public static class Factory implements RequestPolicy.Factory { + public static class Factory implements RequestPolicyFactory { @Override - public RequestPolicy create(RequestPolicy next, RequestPolicy.Options options) { + public RequestPolicy create(RequestPolicy next, RequestPolicyOptions options) { return new RequestIdPolicy(next); } } diff --git a/client-runtime/src/main/java/com/microsoft/rest/v2/policy/RequestPolicy.java b/client-runtime/src/main/java/com/microsoft/rest/v2/policy/RequestPolicy.java index b451dd6459c3..6d48c09481cc 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/v2/policy/RequestPolicy.java +++ b/client-runtime/src/main/java/com/microsoft/rest/v2/policy/RequestPolicy.java @@ -6,7 +6,6 @@ package com.microsoft.rest.v2.policy; -import com.microsoft.rest.v2.http.HttpPipeline; import com.microsoft.rest.v2.http.HttpRequest; import com.microsoft.rest.v2.http.HttpResponse; import io.reactivex.Single; @@ -24,68 +23,4 @@ public interface RequestPolicy { * @return The io.reactivex.Single instance representing the asynchronous operation. */ Single sendAsync(HttpRequest request); - - /** - * Factory to create a RequestPolicy. RequestPolicies are instantiated per-request - * so that they can contain instance state specific to that request/response exchange, - * for example, the number of retries attempted so far in a counter. - */ - interface Factory { - /** - * Creates RequestPolicy. - * - * @param next the next RequestPolicy in the request-response pipeline. - * @return the RequestPolicy - */ - RequestPolicy create(RequestPolicy next, Options options); - } - - /** - * Optional properties that can be used when creating a RequestPolicy. - */ - final class Options { - /** - * The Logger that has been assigned to the HttpPipeline. - */ - private final HttpPipeline.Logger logger; - - /** - * Create a new RequestPolicy.Options object. - * @param logger The logger that has been assigned to the HttpPipeline. - */ - public Options(HttpPipeline.Logger logger) { - this.logger = logger; - } - - /** - * Get whether or not a log with the provided log level should be logged. - * @param logLevel The log level of the log that will be logged. - * @return Whether or not a log with the provided log level should be logged. - */ - public boolean shouldLog(HttpPipeline.LogLevel logLevel) { - boolean result = false; - - if (logger != null && logLevel != null && logLevel != HttpPipeline.LogLevel.OFF) { - final HttpPipeline.LogLevel minimumLogLevel = logger.minimumLogLevel(); - if (minimumLogLevel != null) { - result = logLevel.ordinal() <= minimumLogLevel.ordinal(); - } - } - - return result; - } - - /** - * Attempt to log the provided message to the provided logger. If no logger was provided or if - * the log level does not meat the logger's threshold, then nothing will be logged. - * @param logLevel The log level of this log. - * @param message The message of this log. - * @param formattedMessageArguments The formatted arguments to apply to the message. - */ - public void log(HttpPipeline.LogLevel logLevel, String message, Object... formattedMessageArguments) { - if (logger != null) { - logger.log(logLevel, message, formattedMessageArguments); - } - } - } } diff --git a/client-runtime/src/main/java/com/microsoft/rest/v2/policy/RequestPolicyFactory.java b/client-runtime/src/main/java/com/microsoft/rest/v2/policy/RequestPolicyFactory.java new file mode 100644 index 000000000000..feeb8f11270e --- /dev/null +++ b/client-runtime/src/main/java/com/microsoft/rest/v2/policy/RequestPolicyFactory.java @@ -0,0 +1,23 @@ +/** + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for + * license information. + */ + +package com.microsoft.rest.v2.policy; + +/** + * Factory to create a RequestPolicy. RequestPolicies are instantiated per-request + * so that they can contain instance state specific to that request/response exchange, + * for example, the number of retries attempted so far in a counter. + */ +public interface RequestPolicyFactory { + /** + * Creates RequestPolicy. + * + * @param next the next RequestPolicy in the request-response pipeline. + * @param options The optional arguments that can be passed to a RequestPolicy. + * @return the created RequestPolicy + */ + RequestPolicy create(RequestPolicy next, RequestPolicyOptions options); +} \ No newline at end of file diff --git a/client-runtime/src/main/java/com/microsoft/rest/v2/policy/RequestPolicyOptions.java b/client-runtime/src/main/java/com/microsoft/rest/v2/policy/RequestPolicyOptions.java new file mode 100644 index 000000000000..bd0420e6266f --- /dev/null +++ b/client-runtime/src/main/java/com/microsoft/rest/v2/policy/RequestPolicyOptions.java @@ -0,0 +1,59 @@ +/** + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for + * license information. + */ + +package com.microsoft.rest.v2.policy; + +import com.microsoft.rest.v2.http.HttpPipelineLogLevel; +import com.microsoft.rest.v2.http.HttpPipelineLogger; + +/** + * Optional properties that can be used when creating a RequestPolicy. + */ +public final class RequestPolicyOptions { + /** + * The Logger that has been assigned to the HttpPipeline. + */ + private final HttpPipelineLogger logger; + + /** + * Create a new RequestPolicy.Options object. + * @param logger The logger that has been assigned to the HttpPipeline. + */ + public RequestPolicyOptions(HttpPipelineLogger logger) { + this.logger = logger; + } + + /** + * Get whether or not a log with the provided log level should be logged. + * @param logLevel The log level of the log that will be logged. + * @return Whether or not a log with the provided log level should be logged. + */ + public boolean shouldLog(HttpPipelineLogLevel logLevel) { + boolean result = false; + + if (logger != null && logLevel != null && logLevel != HttpPipelineLogLevel.OFF) { + final HttpPipelineLogLevel minimumLogLevel = logger.minimumLogLevel(); + if (minimumLogLevel != null) { + result = logLevel.ordinal() <= minimumLogLevel.ordinal(); + } + } + + return result; + } + + /** + * Attempt to log the provided message to the provided logger. If no logger was provided or if + * the log level does not meat the logger's threshold, then nothing will be logged. + * @param logLevel The log level of this log. + * @param message The message of this log. + * @param formattedMessageArguments The formatted arguments to apply to the message. + */ + public void log(HttpPipelineLogLevel logLevel, String message, Object... formattedMessageArguments) { + if (logger != null) { + logger.log(logLevel, message, formattedMessageArguments); + } + } +} \ No newline at end of file diff --git a/client-runtime/src/main/java/com/microsoft/rest/v2/policy/RetryPolicy.java b/client-runtime/src/main/java/com/microsoft/rest/v2/policy/RetryPolicy.java index 2e64a55d6844..9a7db1b563c9 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/v2/policy/RetryPolicy.java +++ b/client-runtime/src/main/java/com/microsoft/rest/v2/policy/RetryPolicy.java @@ -21,7 +21,7 @@ public final class RetryPolicy implements RequestPolicy { /** * Factory which instantiates RetryPolicy. */ - public static class Factory implements RequestPolicy.Factory { + public static class Factory implements RequestPolicyFactory { private static final int DEFAULT_NUMBER_OF_ATTEMPTS = 3; final int maxRetries; @@ -41,7 +41,7 @@ public Factory(int maxRetries) { } @Override - public RequestPolicy create(RequestPolicy next, RequestPolicy.Options options) { + public RequestPolicy create(RequestPolicy next, RequestPolicyOptions options) { return new RetryPolicy(maxRetries, next); } } diff --git a/client-runtime/src/main/java/com/microsoft/rest/v2/policy/UseOtherHostPolicy.java b/client-runtime/src/main/java/com/microsoft/rest/v2/policy/UseOtherHostPolicy.java index 1295c3389fda..182544f08fb9 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/v2/policy/UseOtherHostPolicy.java +++ b/client-runtime/src/main/java/com/microsoft/rest/v2/policy/UseOtherHostPolicy.java @@ -20,9 +20,9 @@ public final class UseOtherHostPolicy implements RequestPolicy { /** * Factory to create UseOtherHostPolicy. */ - public static class Factory implements RequestPolicy.Factory { + public static class Factory implements RequestPolicyFactory { @Override - public RequestPolicy create(RequestPolicy next, RequestPolicy.Options options) { + public RequestPolicy create(RequestPolicy next, RequestPolicyOptions options) { return new UseOtherHostPolicy(next); } } diff --git a/client-runtime/src/main/java/com/microsoft/rest/v2/policy/UserAgentPolicy.java b/client-runtime/src/main/java/com/microsoft/rest/v2/policy/UserAgentPolicy.java index d821294b7473..2ee69e380713 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/v2/policy/UserAgentPolicy.java +++ b/client-runtime/src/main/java/com/microsoft/rest/v2/policy/UserAgentPolicy.java @@ -19,7 +19,7 @@ public final class UserAgentPolicy implements RequestPolicy { /** * Creates {@link UserAgentPolicy}. */ - public static class Factory implements RequestPolicy.Factory { + public static class Factory implements RequestPolicyFactory { private final String userAgent; /** @@ -42,7 +42,7 @@ public Factory() { } @Override - public RequestPolicy create(RequestPolicy next, RequestPolicy.Options options) { + public RequestPolicy create(RequestPolicy next, RequestPolicyOptions options) { return new UserAgentPolicy(userAgent, next); } } diff --git a/client-runtime/src/test/java/RestProxyStressTests.java b/client-runtime/src/test/java/RestProxyStressTests.java index ac3801eac4da..d29ffe0fc536 100644 --- a/client-runtime/src/test/java/RestProxyStressTests.java +++ b/client-runtime/src/test/java/RestProxyStressTests.java @@ -7,6 +7,8 @@ import com.microsoft.rest.v2.policy.LoggingPolicy; import com.microsoft.rest.v2.policy.LoggingPolicy.LogLevel; import com.microsoft.rest.v2.policy.RequestPolicy; +import com.microsoft.rest.v2.policy.RequestPolicyFactory; +import com.microsoft.rest.v2.policy.RequestPolicyOptions; import io.reactivex.Flowable; import io.reactivex.Single; import io.reactivex.functions.Consumer; @@ -47,9 +49,9 @@ public Single sendAsync(HttpRequest request) { return next.sendAsync(request); } - static class Factory implements RequestPolicy.Factory { + static class Factory implements RequestPolicyFactory { @Override - public RequestPolicy create(RequestPolicy next, Options options) { + public RequestPolicy create(RequestPolicy next, RequestPolicyOptions options) { return new AddDatePolicy(next); } } diff --git a/client-runtime/src/test/java/com/microsoft/rest/v2/CredentialsTests.java b/client-runtime/src/test/java/com/microsoft/rest/v2/CredentialsTests.java index a59dce250126..1cc6baab6b9a 100644 --- a/client-runtime/src/test/java/com/microsoft/rest/v2/CredentialsTests.java +++ b/client-runtime/src/test/java/com/microsoft/rest/v2/CredentialsTests.java @@ -15,6 +15,8 @@ import com.microsoft.rest.v2.http.HttpResponse; import com.microsoft.rest.v2.http.MockHttpClient; import com.microsoft.rest.v2.policy.RequestPolicy; +import com.microsoft.rest.v2.policy.RequestPolicyFactory; +import com.microsoft.rest.v2.policy.RequestPolicyOptions; import org.junit.Assert; import org.junit.Test; import io.reactivex.Single; @@ -25,9 +27,9 @@ public class CredentialsTests { public void basicCredentialsTest() throws Exception { BasicAuthenticationCredentials credentials = new BasicAuthenticationCredentials("user", "pass"); - RequestPolicy.Factory auditorFactory = new RequestPolicy.Factory() { + RequestPolicyFactory auditorFactory = new RequestPolicyFactory() { @Override - public RequestPolicy create(final RequestPolicy next, RequestPolicy.Options options) { + public RequestPolicy create(final RequestPolicy next, RequestPolicyOptions options) { return new RequestPolicy() { @Override public Single sendAsync(HttpRequest request) { @@ -52,9 +54,9 @@ public Single sendAsync(HttpRequest request) { public void tokenCredentialsTest() throws Exception { TokenCredentials credentials = new TokenCredentials(null, "this_is_a_token"); - RequestPolicy.Factory auditorFactory = new RequestPolicy.Factory() { + RequestPolicyFactory auditorFactory = new RequestPolicyFactory() { @Override - public RequestPolicy create(final RequestPolicy next, RequestPolicy.Options options) { + public RequestPolicy create(final RequestPolicy next, RequestPolicyOptions options) { return new RequestPolicy() { @Override public Single sendAsync(HttpRequest request) { diff --git a/client-runtime/src/test/java/com/microsoft/rest/v2/RestProxyWithNettyTests.java b/client-runtime/src/test/java/com/microsoft/rest/v2/RestProxyWithNettyTests.java index 3c4948a5fd2f..49c8b5e663a7 100644 --- a/client-runtime/src/test/java/com/microsoft/rest/v2/RestProxyWithNettyTests.java +++ b/client-runtime/src/test/java/com/microsoft/rest/v2/RestProxyWithNettyTests.java @@ -3,9 +3,6 @@ import com.microsoft.rest.v2.http.HttpClient; import com.microsoft.rest.v2.http.HttpClient.Configuration; import com.microsoft.rest.v2.http.NettyClient; -import com.microsoft.rest.v2.policy.RequestPolicy.Factory; - -import java.util.Collections; public class RestProxyWithNettyTests extends RestProxyTests { private static NettyClient.Factory nettyClientFactory = new NettyClient.Factory(); diff --git a/client-runtime/src/test/java/com/microsoft/rest/v2/http/HttpPipelineBuilderTests.java b/client-runtime/src/test/java/com/microsoft/rest/v2/http/HttpPipelineBuilderTests.java index 392f5d3a2c9d..7f3c7daff7a3 100644 --- a/client-runtime/src/test/java/com/microsoft/rest/v2/http/HttpPipelineBuilderTests.java +++ b/client-runtime/src/test/java/com/microsoft/rest/v2/http/HttpPipelineBuilderTests.java @@ -12,14 +12,14 @@ public class HttpPipelineBuilderTests { @Test public void constructorWithNoArguments() { - final HttpPipeline.Builder builder = new HttpPipeline.Builder(); + final HttpPipelineBuilder builder = new HttpPipelineBuilder(); assertEquals(0, builder.requestPolicyFactories().size()); assertNull(builder.options()); } @Test public void withRequestPolicy() { - final HttpPipeline.Builder builder = new HttpPipeline.Builder(); + final HttpPipelineBuilder builder = new HttpPipelineBuilder(); builder.withRequestPolicy(new PortPolicy.Factory(80)); assertEquals(1, builder.requestPolicyFactories().size()); @@ -39,7 +39,7 @@ public void withRequestPolicy() { @Test public void withRequestPolicyWithIndex() { - final HttpPipeline.Builder builder = new HttpPipeline.Builder(); + final HttpPipelineBuilder builder = new HttpPipelineBuilder(); builder.withRequestPolicy(0, new PortPolicy.Factory(80)); assertEquals(1, builder.requestPolicyFactories().size()); @@ -59,7 +59,7 @@ public void withRequestPolicyWithIndex() { @Test public void withRequestPolicyBefore() { - final HttpPipeline.Builder builder = new HttpPipeline.Builder(); + final HttpPipelineBuilder builder = new HttpPipelineBuilder(); builder.withRequestPolicyBefore(RetryPolicy.Factory.class, new PortPolicy.Factory(80)); assertEquals(1, builder.requestPolicyFactories().size()); @@ -86,7 +86,7 @@ public void withRequestPolicyBefore() { @Test public void withRequestPolicyAfter() { - final HttpPipeline.Builder builder = new HttpPipeline.Builder(); + final HttpPipelineBuilder builder = new HttpPipelineBuilder(); builder.withRequestPolicyAfter(RetryPolicy.Factory.class, new PortPolicy.Factory(80)); assertEquals(1, builder.requestPolicyFactories().size()); @@ -113,7 +113,7 @@ public void withRequestPolicyAfter() { @Test public void withRequestPolicyArray() { - final HttpPipeline.Builder builder = new HttpPipeline.Builder(); + final HttpPipelineBuilder builder = new HttpPipelineBuilder(); builder.withRequestPolicies( new ProtocolPolicy.Factory("http"), @@ -128,7 +128,7 @@ public void withRequestPolicyArray() { @Test public void appendingRequestPolicyArray() { - final HttpPipeline.Builder builder = new HttpPipeline.Builder(); + final HttpPipelineBuilder builder = new HttpPipelineBuilder(); builder.withRequestPolicy(new RetryPolicy.Factory()); builder.withRequestPolicies( diff --git a/client-runtime/src/test/java/com/microsoft/rest/v2/http/HttpPipelineTests.java b/client-runtime/src/test/java/com/microsoft/rest/v2/http/HttpPipelineTests.java index 3ab1460f7047..1823019a44f6 100644 --- a/client-runtime/src/test/java/com/microsoft/rest/v2/http/HttpPipelineTests.java +++ b/client-runtime/src/test/java/com/microsoft/rest/v2/http/HttpPipelineTests.java @@ -46,7 +46,7 @@ public Single sendRequestAsync(HttpRequest request) { return Single.just(new MockHttpResponse(200)); } }; - final HttpPipeline httpPipeline = new HttpPipeline.Builder() + final HttpPipeline httpPipeline = new HttpPipelineBuilder() .withHttpClient(httpClient) .withUserAgent(expectedUserAgent) .build(); diff --git a/client-runtime/src/test/java/com/microsoft/rest/v2/http/MockHttpPipelineLogger.java b/client-runtime/src/test/java/com/microsoft/rest/v2/http/MockHttpPipelineLogger.java index 407f0e7635d0..7e9fed1afd65 100644 --- a/client-runtime/src/test/java/com/microsoft/rest/v2/http/MockHttpPipelineLogger.java +++ b/client-runtime/src/test/java/com/microsoft/rest/v2/http/MockHttpPipelineLogger.java @@ -3,22 +3,22 @@ import java.util.ArrayList; import java.util.List; -public class MockHttpPipelineLogger implements HttpPipeline.Logger { - private final HttpPipeline.LogLevel minimumLogLevel; +public class MockHttpPipelineLogger implements HttpPipelineLogger { + private final HttpPipelineLogLevel minimumLogLevel; private final List logs; - public MockHttpPipelineLogger(HttpPipeline.LogLevel minimumLogLevel) { + public MockHttpPipelineLogger(HttpPipelineLogLevel minimumLogLevel) { this.minimumLogLevel = minimumLogLevel; this.logs = new ArrayList<>(); } @Override - public HttpPipeline.LogLevel minimumLogLevel() { + public HttpPipelineLogLevel minimumLogLevel() { return minimumLogLevel; } @Override - public void log(HttpPipeline.LogLevel logLevel, String message, Object... formattedArguments) { + public void log(HttpPipelineLogLevel logLevel, String message, Object... formattedArguments) { this.logs.add(logLevel + ") " + String.format(message, formattedArguments)); } diff --git a/client-runtime/src/test/java/com/microsoft/rest/v2/policy/ProxyAuthenticationPolicyTests.java b/client-runtime/src/test/java/com/microsoft/rest/v2/policy/ProxyAuthenticationPolicyTests.java index faea1d398435..c6ee1a771145 100644 --- a/client-runtime/src/test/java/com/microsoft/rest/v2/policy/ProxyAuthenticationPolicyTests.java +++ b/client-runtime/src/test/java/com/microsoft/rest/v2/policy/ProxyAuthenticationPolicyTests.java @@ -24,9 +24,9 @@ public void test() { final HttpPipeline pipeline = HttpPipeline.build( new MockHttpClient(), new ProxyAuthenticationPolicy.Factory(username, password), - new RequestPolicy.Factory() { + new RequestPolicyFactory() { @Override - public RequestPolicy create(final RequestPolicy next, RequestPolicy.Options options) { + public RequestPolicy create(final RequestPolicy next, RequestPolicyOptions options) { return new RequestPolicy() { @Override public Single sendAsync(HttpRequest request) { diff --git a/client-runtime/src/test/java/com/microsoft/rest/v2/policy/RequestIdPolicyTests.java b/client-runtime/src/test/java/com/microsoft/rest/v2/policy/RequestIdPolicyTests.java index 57544a1b604f..18f262191372 100644 --- a/client-runtime/src/test/java/com/microsoft/rest/v2/policy/RequestIdPolicyTests.java +++ b/client-runtime/src/test/java/com/microsoft/rest/v2/policy/RequestIdPolicyTests.java @@ -9,6 +9,7 @@ import com.microsoft.rest.v2.http.HttpClient; import com.microsoft.rest.v2.http.HttpHeaders; import com.microsoft.rest.v2.http.HttpPipeline; +import com.microsoft.rest.v2.http.HttpPipelineBuilder; import com.microsoft.rest.v2.http.HttpRequest; import com.microsoft.rest.v2.http.HttpResponse; import com.microsoft.rest.v2.http.MockHttpClient; @@ -71,7 +72,7 @@ public Single bodyAsStringAsync() { @Test public void newRequestIdForEachCall() throws Exception { - HttpPipeline pipeline = new HttpPipeline.Builder() + HttpPipeline pipeline = new HttpPipelineBuilder() .withRequestPolicy(new RequestIdPolicy.Factory()) .withHttpClient(new MockHttpClient() { String firstRequestId = null; diff --git a/client-runtime/src/test/java/com/microsoft/rest/v2/policy/RequestPolicyOptionsTests.java b/client-runtime/src/test/java/com/microsoft/rest/v2/policy/RequestPolicyOptionsTests.java index dfe7305092e3..d04a3e335944 100644 --- a/client-runtime/src/test/java/com/microsoft/rest/v2/policy/RequestPolicyOptionsTests.java +++ b/client-runtime/src/test/java/com/microsoft/rest/v2/policy/RequestPolicyOptionsTests.java @@ -1,6 +1,6 @@ package com.microsoft.rest.v2.policy; -import com.microsoft.rest.v2.http.HttpPipeline; +import com.microsoft.rest.v2.http.HttpPipelineLogLevel; import com.microsoft.rest.v2.http.MockHttpPipelineLogger; import org.junit.Test; @@ -9,43 +9,43 @@ public class RequestPolicyOptionsTests { @Test public void shouldLogWithNullLogger() { - final RequestPolicy.Options options = new RequestPolicy.Options(null); - assertFalse(options.shouldLog(HttpPipeline.LogLevel.INFO)); + final RequestPolicyOptions options = new RequestPolicyOptions(null); + assertFalse(options.shouldLog(HttpPipelineLogLevel.INFO)); } @Test public void shouldLogWithNullLogLevel() { - final RequestPolicy.Options options = new RequestPolicy.Options(new MockHttpPipelineLogger(HttpPipeline.LogLevel.INFO)); + final RequestPolicyOptions options = new RequestPolicyOptions(new MockHttpPipelineLogger(HttpPipelineLogLevel.INFO)); assertFalse(options.shouldLog(null)); } @Test public void shouldLogWithOFFLogLevelAndOFFMinimum() { - final RequestPolicy.Options options = new RequestPolicy.Options(new MockHttpPipelineLogger(HttpPipeline.LogLevel.OFF)); - assertFalse(options.shouldLog(HttpPipeline.LogLevel.OFF)); + final RequestPolicyOptions options = new RequestPolicyOptions(new MockHttpPipelineLogger(HttpPipelineLogLevel.OFF)); + assertFalse(options.shouldLog(HttpPipelineLogLevel.OFF)); } @Test public void shouldLogWithOFFLogLevelAndINFOMinimum() { - final RequestPolicy.Options options = new RequestPolicy.Options(new MockHttpPipelineLogger(HttpPipeline.LogLevel.INFO)); - assertFalse(options.shouldLog(HttpPipeline.LogLevel.OFF)); + final RequestPolicyOptions options = new RequestPolicyOptions(new MockHttpPipelineLogger(HttpPipelineLogLevel.INFO)); + assertFalse(options.shouldLog(HttpPipelineLogLevel.OFF)); } @Test public void shouldLogWithINFOLogLevelAndINFOMinimum() { - final RequestPolicy.Options options = new RequestPolicy.Options(new MockHttpPipelineLogger(HttpPipeline.LogLevel.INFO)); - assertTrue(options.shouldLog(HttpPipeline.LogLevel.INFO)); + final RequestPolicyOptions options = new RequestPolicyOptions(new MockHttpPipelineLogger(HttpPipelineLogLevel.INFO)); + assertTrue(options.shouldLog(HttpPipelineLogLevel.INFO)); } @Test public void shouldLogWithINFOLogLevelAndWARNINGMinimum() { - final RequestPolicy.Options options = new RequestPolicy.Options(new MockHttpPipelineLogger(HttpPipeline.LogLevel.WARNING)); - assertFalse(options.shouldLog(HttpPipeline.LogLevel.INFO)); + final RequestPolicyOptions options = new RequestPolicyOptions(new MockHttpPipelineLogger(HttpPipelineLogLevel.WARNING)); + assertFalse(options.shouldLog(HttpPipelineLogLevel.INFO)); } @Test public void logWithNullLogger() { - final RequestPolicy.Options options = new RequestPolicy.Options(null); - options.log(HttpPipeline.LogLevel.INFO, "Test Log"); + final RequestPolicyOptions options = new RequestPolicyOptions(null); + options.log(HttpPipelineLogLevel.INFO, "Test Log"); } }