From 4a7b25e722c3b8f539ef057b05fb73ea45297a63 Mon Sep 17 00:00:00 2001 From: Jianghao Lu Date: Wed, 15 Jun 2016 22:43:25 -0700 Subject: [PATCH 1/5] Remove required mapper adapter --- .../com/microsoft/azure/AzureEnvironment.java | 7 +- .../com/microsoft/azure/AzureRestClient.java | 94 +++++++++++++++++++ .../microsoft/azure/AzureServiceClient.java | 6 +- .../RequestIdHeaderInterceptorTests.java | 20 ++-- .../java/com/microsoft/rest/RestClient.java | 87 ++++++++++------- .../com/microsoft/rest/ServiceClient.java | 5 +- .../com/microsoft/rest/CredentialsTests.java | 12 +-- .../com/microsoft/rest/RetryHandlerTests.java | 8 +- .../microsoft/rest/ServiceClientTests.java | 4 +- .../com/microsoft/rest/UserAgentTests.java | 12 +-- 10 files changed, 182 insertions(+), 73 deletions(-) create mode 100644 azure-client-runtime/src/main/java/com/microsoft/azure/AzureRestClient.java diff --git a/azure-client-runtime/src/main/java/com/microsoft/azure/AzureEnvironment.java b/azure-client-runtime/src/main/java/com/microsoft/azure/AzureEnvironment.java index 1721e2ff97bb8..6bf28394ca158 100644 --- a/azure-client-runtime/src/main/java/com/microsoft/azure/AzureEnvironment.java +++ b/azure-client-runtime/src/main/java/com/microsoft/azure/AzureEnvironment.java @@ -7,7 +7,6 @@ package com.microsoft.azure; -import com.microsoft.azure.serializer.AzureJacksonMapperAdapter; import com.microsoft.rest.RestClient; /** @@ -88,9 +87,9 @@ public String getBaseUrl() { * @return a builder for the rest client. */ public RestClient.Builder newRestClientBuilder() { - return new RestClient.Builder(baseURL) - .withInterceptor(new RequestIdHeaderInterceptor()) - .withMapperAdapter(new AzureJacksonMapperAdapter()); + return new AzureRestClient.Builder() + .withDefaultBaseUrl(this) + .withInterceptor(new RequestIdHeaderInterceptor()); } /** diff --git a/azure-client-runtime/src/main/java/com/microsoft/azure/AzureRestClient.java b/azure-client-runtime/src/main/java/com/microsoft/azure/AzureRestClient.java new file mode 100644 index 0000000000000..af9679c5c5584 --- /dev/null +++ b/azure-client-runtime/src/main/java/com/microsoft/azure/AzureRestClient.java @@ -0,0 +1,94 @@ +/** + * + * 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.azure; + +import com.microsoft.azure.serializer.AzureJacksonMapperAdapter; +import com.microsoft.rest.BaseUrlHandler; +import com.microsoft.rest.CustomHeadersInterceptor; +import com.microsoft.rest.RestClient; +import com.microsoft.rest.UserAgentInterceptor; +import com.microsoft.rest.credentials.ServiceClientCredentials; +import com.microsoft.rest.retry.RetryHandler; +import com.microsoft.rest.serializer.JacksonMapperAdapter; + +import okhttp3.OkHttpClient; +import retrofit2.Retrofit; + +/** + * An instance of this class stores the client information for making REST calls to Azure. + */ +public final class AzureRestClient extends RestClient { + private AzureRestClient(OkHttpClient httpClient, + Retrofit retrofit, + ServiceClientCredentials credentials, + CustomHeadersInterceptor customHeadersInterceptor, + UserAgentInterceptor userAgentInterceptor, + BaseUrlHandler baseUrlHandler, + JacksonMapperAdapter mapperAdapter) { + super(httpClient, retrofit, credentials, customHeadersInterceptor, + userAgentInterceptor, baseUrlHandler, mapperAdapter); + } + + /** + * The builder class for building a REST client. + */ + public static class Builder extends RestClient.Builder { + /** + * Creates an instance of the builder with a base URL to the service. + */ + public Builder() { + super(); + } + + /** + * Creates an instance of the builder with a base URL and 2 custom builders. + * + * @param httpClientBuilder the builder to build an {@link OkHttpClient}. + * @param retrofitBuilder the builder to build a {@link Retrofit}. + */ + public Builder(OkHttpClient.Builder httpClientBuilder, Retrofit.Builder retrofitBuilder) { + super(httpClientBuilder, retrofitBuilder); + } + + /** + * Sets the base URL with the default from the Azure Environment. + * + * @param environment the environment the application is running in + * @return the builder itself for chaining + */ + public Builder withDefaultBaseUrl(AzureEnvironment environment) { + withBaseUrl(environment.getBaseUrl()); + return this; + } + + /** + * Build an AzureRestClient with all the current configurations. + * + * @return an {@link AzureRestClient}. + */ + public AzureRestClient build() { + AzureJacksonMapperAdapter mapperAdapter = new AzureJacksonMapperAdapter(); + OkHttpClient httpClient = httpClientBuilder + .addInterceptor(baseUrlHandler) + .addInterceptor(customHeadersInterceptor) + .addInterceptor(new RetryHandler()) + .build(); + return new AzureRestClient(httpClient, + retrofitBuilder + .baseUrl(baseUrl) + .client(httpClient) + .addConverterFactory(mapperAdapter.getConverterFactory()) + .build(), + credentials, + customHeadersInterceptor, + userAgentInterceptor, + baseUrlHandler, + mapperAdapter); + } + } +} diff --git a/azure-client-runtime/src/main/java/com/microsoft/azure/AzureServiceClient.java b/azure-client-runtime/src/main/java/com/microsoft/azure/AzureServiceClient.java index 08094b67d5071..5edc6a662e62c 100644 --- a/azure-client-runtime/src/main/java/com/microsoft/azure/AzureServiceClient.java +++ b/azure-client-runtime/src/main/java/com/microsoft/azure/AzureServiceClient.java @@ -7,7 +7,6 @@ package com.microsoft.azure; -import com.microsoft.azure.serializer.AzureJacksonMapperAdapter; import com.microsoft.rest.RestClient; import com.microsoft.rest.ServiceClient; @@ -16,9 +15,8 @@ */ public abstract class AzureServiceClient extends ServiceClient { protected AzureServiceClient(String baseUrl) { - this(new RestClient.Builder(baseUrl) - .withInterceptor(new RequestIdHeaderInterceptor()) - .withMapperAdapter(new AzureJacksonMapperAdapter()).build()); + this(new RestClient.Builder().withBaseUrl(baseUrl) + .withInterceptor(new RequestIdHeaderInterceptor()).build()); } /** diff --git a/azure-client-runtime/src/test/java/com/microsoft/azure/RequestIdHeaderInterceptorTests.java b/azure-client-runtime/src/test/java/com/microsoft/azure/RequestIdHeaderInterceptorTests.java index e8cce598b05f4..24ed68d3dd273 100644 --- a/azure-client-runtime/src/test/java/com/microsoft/azure/RequestIdHeaderInterceptorTests.java +++ b/azure-client-runtime/src/test/java/com/microsoft/azure/RequestIdHeaderInterceptorTests.java @@ -7,24 +7,26 @@ package com.microsoft.azure; -import com.microsoft.azure.serializer.AzureJacksonMapperAdapter; import com.microsoft.rest.RestClient; import com.microsoft.rest.retry.RetryHandler; -import okhttp3.Interceptor; -import okhttp3.Protocol; -import okhttp3.Request; -import okhttp3.Response; + import org.junit.Assert; import org.junit.Test; import java.io.IOException; +import okhttp3.Interceptor; +import okhttp3.Protocol; +import okhttp3.Request; +import okhttp3.Response; + public class RequestIdHeaderInterceptorTests { private static final String REQUEST_ID_HEADER = "x-ms-client-request-id"; @Test public void newRequestIdForEachCall() throws Exception { - RestClient restClient = new RestClient.Builder("http://localhost") + RestClient restClient = new AzureRestClient.Builder() + .withBaseUrl("http://localhost") .withInterceptor(new RequestIdHeaderInterceptor()) .withInterceptor(new Interceptor() { private String firstRequestId = null; @@ -45,7 +47,6 @@ public Response intercept(Chain chain) throws IOException { .protocol(Protocol.HTTP_1_1).build(); } }) - .withMapperAdapter(new AzureJacksonMapperAdapter()) .build(); AzureServiceClient serviceClient = new AzureServiceClient(restClient) { }; Response response = serviceClient.restClient().httpClient() @@ -58,11 +59,13 @@ public Response intercept(Chain chain) throws IOException { @Test public void sameRequestIdForRetry() throws Exception { - RestClient restClient = new RestClient.Builder("http://localhost") + RestClient restClient = new AzureRestClient.Builder() + .withBaseUrl("http://localhost") .withInterceptor(new RequestIdHeaderInterceptor()) .withInterceptor(new RetryHandler()) .withInterceptor(new Interceptor() { private String firstRequestId = null; + @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); @@ -80,7 +83,6 @@ public Response intercept(Chain chain) throws IOException { .protocol(Protocol.HTTP_1_1).build(); } }) - .withMapperAdapter(new AzureJacksonMapperAdapter()) .build(); AzureServiceClient serviceClient = new AzureServiceClient(restClient) { }; Response response = serviceClient.restClient().httpClient() 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 254639642345a..fcfc32092c884 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/RestClient.java +++ b/client-runtime/src/main/java/com/microsoft/rest/RestClient.java @@ -11,6 +11,7 @@ import com.microsoft.rest.retry.RetryHandler; import com.microsoft.rest.serializer.JacksonMapperAdapter; +import java.lang.reflect.Field; import java.net.CookieManager; import java.net.CookiePolicy; @@ -23,7 +24,7 @@ /** * An instance of this class stores the client information for making REST calls. */ -public final class RestClient { +public class RestClient { /** The {@link okhttp3.OkHttpClient} object. */ private OkHttpClient httpClient; /** The {@link retrofit2.Retrofit} object. */ @@ -39,7 +40,7 @@ public final class RestClient { /** The interceptor to set 'User-Agent' header. */ private UserAgentInterceptor userAgentInterceptor; - private RestClient(OkHttpClient httpClient, + protected RestClient(OkHttpClient httpClient, Retrofit retrofit, ServiceClientCredentials credentials, CustomHeadersInterceptor customHeadersInterceptor, @@ -73,6 +74,17 @@ public JacksonMapperAdapter mapperAdapter() { return mapperAdapter; } + /** + * Sets the mapper adapter. + * + * @param mapperAdapter an adapter to a Jackson mapper. + * @return the builder itself for chaining. + */ + public RestClient withMapperAdapater(JacksonMapperAdapter mapperAdapter) { + this.mapperAdapter = mapperAdapter; + return this; + } + /** * Get the http client. * @@ -104,41 +116,35 @@ public ServiceClientCredentials credentials() { * The builder class for building a REST client. */ public static class Builder { + /** The dynamic base URL with variables wrapped in "{" and "}". */ + protected String baseUrl; /** The builder to build an {@link OkHttpClient}. */ - private OkHttpClient.Builder httpClientBuilder; + protected OkHttpClient.Builder httpClientBuilder; /** The builder to build a {@link Retrofit}. */ - private Retrofit.Builder retrofitBuilder; + protected Retrofit.Builder retrofitBuilder; /** The credentials to authenticate. */ - private ServiceClientCredentials credentials; + protected ServiceClientCredentials credentials; /** The interceptor to handle custom headers. */ - private CustomHeadersInterceptor customHeadersInterceptor; + protected CustomHeadersInterceptor customHeadersInterceptor; /** The interceptor to handle base URL. */ - private BaseUrlHandler baseUrlHandler; - /** The adapter to a Jackson {@link com.fasterxml.jackson.databind.ObjectMapper}. */ - private JacksonMapperAdapter mapperAdapter; + protected BaseUrlHandler baseUrlHandler; /** The interceptor to set 'User-Agent' header. */ - private UserAgentInterceptor userAgentInterceptor; + protected UserAgentInterceptor userAgentInterceptor; /** * Creates an instance of the builder with a base URL to the service. - * - * @param baseUrl the dynamic base URL with variables wrapped in "{" and "}". */ - public Builder(String baseUrl) { - this(baseUrl, new OkHttpClient.Builder(), new Retrofit.Builder()); + public Builder() { + this(new OkHttpClient.Builder(), new Retrofit.Builder()); } /** * Creates an instance of the builder with a base URL and 2 custom builders. * - * @param baseUrl the dynamic base URL with variables wrapped in "{" and "}". * @param httpClientBuilder the builder to build an {@link OkHttpClient}. * @param retrofitBuilder the builder to build a {@link Retrofit}. */ - public Builder(String baseUrl, OkHttpClient.Builder httpClientBuilder, Retrofit.Builder retrofitBuilder) { - if (baseUrl == null) { - throw new IllegalArgumentException("baseUrl == null"); - } + public Builder(OkHttpClient.Builder httpClientBuilder, Retrofit.Builder retrofitBuilder) { if (httpClientBuilder == null) { throw new IllegalArgumentException("httpClientBuilder == null"); } @@ -154,29 +160,47 @@ public Builder(String baseUrl, OkHttpClient.Builder httpClientBuilder, Retrofit. this.httpClientBuilder = httpClientBuilder .cookieJar(new JavaNetCookieJar(cookieManager)) .addInterceptor(userAgentInterceptor); - // Set up rest adapter - this.retrofitBuilder = retrofitBuilder.baseUrl(baseUrl); + this.retrofitBuilder = retrofitBuilder; } /** - * Sets the user agent header. + * Sets the dynamic base URL. * - * @param userAgent the user agent header. + * @param baseUrl the base URL to use. * @return the builder itself for chaining. */ - public Builder withUserAgent(String userAgent) { - this.userAgentInterceptor.setUserAgent(userAgent); + public Builder withBaseUrl(String baseUrl) { + this.baseUrl = baseUrl; return this; } /** - * Sets the mapper adapter. + * Sets the base URL with the default from the service client. * - * @param mapperAdapter an adapter to a Jackson mapper. + * @param serviceClientClass the service client class containing a default base URL. * @return the builder itself for chaining. */ - public Builder withMapperAdapter(JacksonMapperAdapter mapperAdapter) { - this.mapperAdapter = mapperAdapter; + public Builder withDefaultBaseUrl(Class serviceClientClass) { + try { + Field field = serviceClientClass.getDeclaredField("DEFAULT_BASE_URL"); + field.setAccessible(true); + baseUrl = (String) field.get(null); + } catch (NoSuchFieldException e) { + throw new UnsupportedOperationException("Cannot read static field DEFAULT_BASE_URL", e); + } catch (IllegalAccessException e) { + throw new UnsupportedOperationException("Cannot read static field DEFAULT_BASE_URL", e); + } + return this; + } + + /** + * Sets the user agent header. + * + * @param userAgent the user agent header. + * @return the builder itself for chaining. + */ + public Builder withUserAgent(String userAgent) { + this.userAgentInterceptor.setUserAgent(userAgent); return this; } @@ -222,9 +246,7 @@ public Builder withInterceptor(Interceptor interceptor) { * @return a {@link RestClient}. */ public RestClient build() { - if (mapperAdapter == null) { - throw new IllegalArgumentException("Please set mapper adapter."); - } + JacksonMapperAdapter mapperAdapter = new JacksonMapperAdapter(); OkHttpClient httpClient = httpClientBuilder .addInterceptor(baseUrlHandler) .addInterceptor(customHeadersInterceptor) @@ -232,6 +254,7 @@ public RestClient build() { .build(); return new RestClient(httpClient, retrofitBuilder + .baseUrl(baseUrl) .client(httpClient) .addConverterFactory(mapperAdapter.getConverterFactory()) .build(), diff --git a/client-runtime/src/main/java/com/microsoft/rest/ServiceClient.java b/client-runtime/src/main/java/com/microsoft/rest/ServiceClient.java index 674b6a1bc6253..e6d2f4a8d595d 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/ServiceClient.java +++ b/client-runtime/src/main/java/com/microsoft/rest/ServiceClient.java @@ -7,8 +7,6 @@ package com.microsoft.rest; -import com.microsoft.rest.serializer.JacksonMapperAdapter; - /** * ServiceClient is the abstraction for accessing REST operations and their payload data types. */ @@ -24,8 +22,7 @@ public abstract class ServiceClient { * @param baseUrl the service endpoint */ protected ServiceClient(String baseUrl) { - this(new RestClient.Builder(baseUrl) - .withMapperAdapter(new JacksonMapperAdapter()).build()); + this(new RestClient.Builder().withBaseUrl(baseUrl).build()); } /** diff --git a/client-runtime/src/test/java/com/microsoft/rest/CredentialsTests.java b/client-runtime/src/test/java/com/microsoft/rest/CredentialsTests.java index 3e05c5d723fdb..797654a98cbdc 100644 --- a/client-runtime/src/test/java/com/microsoft/rest/CredentialsTests.java +++ b/client-runtime/src/test/java/com/microsoft/rest/CredentialsTests.java @@ -26,11 +26,9 @@ public class CredentialsTests { @Test public void basicCredentialsTest() throws Exception { - OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder(); - Retrofit.Builder retrofitBuilder = new Retrofit.Builder(); BasicAuthenticationCredentials credentials = new BasicAuthenticationCredentials("user", "pass"); - RestClient.Builder restBuilder = new RestClient.Builder("http://localhost", clientBuilder, retrofitBuilder) - .withMapperAdapter(new JacksonMapperAdapter()) + RestClient.Builder restBuilder = new RestClient.Builder() + .withBaseUrl("http://localhost") .withCredentials(credentials) .withInterceptor(new Interceptor() { @Override @@ -51,11 +49,9 @@ public Response intercept(Chain chain) throws IOException { @Test public void tokenCredentialsTest() throws Exception { - OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder(); - Retrofit.Builder retrofitBuilder = new Retrofit.Builder(); TokenCredentials credentials = new TokenCredentials(null, "this_is_a_token"); - RestClient.Builder restBuilder = new RestClient.Builder("http://localhost", clientBuilder, retrofitBuilder) - .withMapperAdapter(new JacksonMapperAdapter()) + RestClient.Builder restBuilder = new RestClient.Builder() + .withBaseUrl("http://localhost") .withCredentials(credentials) .withInterceptor(new Interceptor() { @Override diff --git a/client-runtime/src/test/java/com/microsoft/rest/RetryHandlerTests.java b/client-runtime/src/test/java/com/microsoft/rest/RetryHandlerTests.java index 97c46f6fe4fc3..31d8e3a15a0c0 100644 --- a/client-runtime/src/test/java/com/microsoft/rest/RetryHandlerTests.java +++ b/client-runtime/src/test/java/com/microsoft/rest/RetryHandlerTests.java @@ -42,8 +42,8 @@ public Response intercept(Chain chain) throws IOException { .build(); } }); - RestClient.Builder restBuilder = new RestClient.Builder("http://localhost", clientBuilder, retrofitBuilder) - .withMapperAdapter(new JacksonMapperAdapter()); + RestClient.Builder restBuilder = new RestClient.Builder( clientBuilder, retrofitBuilder) + .withBaseUrl("http://localhost"); ServiceClient serviceClient = new ServiceClient(restBuilder.build()) { }; Response response = serviceClient.restClient().httpClient().newCall( new Request.Builder().url("http://localhost").get().build()).execute(); @@ -69,8 +69,8 @@ public Response intercept(Chain chain) throws IOException { .build(); } }); - RestClient.Builder restBuilder = new RestClient.Builder("http://localhost", clientBuilder, retrofitBuilder) - .withMapperAdapter(new JacksonMapperAdapter()); + RestClient.Builder restBuilder = new RestClient.Builder(clientBuilder, retrofitBuilder) + .withBaseUrl("http://localhost"); ServiceClient serviceClient = new ServiceClient(restBuilder.build()) { }; Response response = serviceClient.restClient().httpClient().newCall( new Request.Builder().url("http://localhost").get().build()).execute(); diff --git a/client-runtime/src/test/java/com/microsoft/rest/ServiceClientTests.java b/client-runtime/src/test/java/com/microsoft/rest/ServiceClientTests.java index 6423ef23d8081..61f5a118b58f0 100644 --- a/client-runtime/src/test/java/com/microsoft/rest/ServiceClientTests.java +++ b/client-runtime/src/test/java/com/microsoft/rest/ServiceClientTests.java @@ -39,8 +39,8 @@ public Response intercept(Chain chain) throws IOException { .build(); } }); - RestClient.Builder restBuilder = new RestClient.Builder("http://localhost", clientBuilder, retrofitBuilder) - .withMapperAdapter(new JacksonMapperAdapter()); + RestClient.Builder restBuilder = new RestClient.Builder(clientBuilder, retrofitBuilder) + .withBaseUrl("http://localhost"); ServiceClient serviceClient = new ServiceClient(restBuilder.build()) { }; Response response = serviceClient.restClient().httpClient().newCall(new Request.Builder().url("http://localhost").build()).execute(); Assert.assertEquals(200, response.code()); diff --git a/client-runtime/src/test/java/com/microsoft/rest/UserAgentTests.java b/client-runtime/src/test/java/com/microsoft/rest/UserAgentTests.java index 35970714e3a83..1b9d343048acc 100644 --- a/client-runtime/src/test/java/com/microsoft/rest/UserAgentTests.java +++ b/client-runtime/src/test/java/com/microsoft/rest/UserAgentTests.java @@ -22,7 +22,8 @@ public class UserAgentTests { @Test public void defaultUserAgentTests() throws Exception { - RestClient.Builder restBuilder = new RestClient.Builder("http://localhost") + RestClient.Builder restBuilder = new RestClient.Builder() + .withBaseUrl("http://localhost") .withInterceptor(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { @@ -34,8 +35,7 @@ public Response intercept(Chain chain) throws IOException { .protocol(Protocol.HTTP_1_1) .build(); } - }) - .withMapperAdapter(new JacksonMapperAdapter()); + }); ServiceClient serviceClient = new ServiceClient(restBuilder.build()) { }; Response response = serviceClient.restClient().httpClient() .newCall(new Request.Builder().get().url("http://localhost").build()).execute(); @@ -45,7 +45,8 @@ public Response intercept(Chain chain) throws IOException { @Test public void customUserAgentTests() throws Exception { - RestClient.Builder restBuilder = new RestClient.Builder("http://localhost") + RestClient.Builder restBuilder = new RestClient.Builder() + .withBaseUrl("http://localhost") .withUserAgent("Awesome") .withInterceptor(new Interceptor() { @Override @@ -58,8 +59,7 @@ public Response intercept(Chain chain) throws IOException { .protocol(Protocol.HTTP_1_1) .build(); } - }) - .withMapperAdapter(new JacksonMapperAdapter()); + }); ServiceClient serviceClient = new ServiceClient(restBuilder.build()) { }; Response response = serviceClient.restClient().httpClient() .newCall(new Request.Builder().get().url("http://localhost").build()).execute(); From 1773d49c87b9a23a0f4338005c4e6184ae0012d8 Mon Sep 17 00:00:00 2001 From: Jianghao Lu Date: Wed, 15 Jun 2016 22:55:27 -0700 Subject: [PATCH 2/5] Add stage for base url --- .../com/microsoft/azure/AzureRestClient.java | 54 ++++--- .../java/com/microsoft/rest/RestClient.java | 143 ++++++++++-------- .../com/microsoft/rest/CredentialsTests.java | 12 +- .../com/microsoft/rest/RetryHandlerTests.java | 12 +- .../microsoft/rest/ServiceClientTests.java | 6 +- .../com/microsoft/rest/UserAgentTests.java | 14 +- 6 files changed, 126 insertions(+), 115 deletions(-) diff --git a/azure-client-runtime/src/main/java/com/microsoft/azure/AzureRestClient.java b/azure-client-runtime/src/main/java/com/microsoft/azure/AzureRestClient.java index af9679c5c5584..373d09268d013 100644 --- a/azure-client-runtime/src/main/java/com/microsoft/azure/AzureRestClient.java +++ b/azure-client-runtime/src/main/java/com/microsoft/azure/AzureRestClient.java @@ -53,6 +53,7 @@ public Builder() { */ public Builder(OkHttpClient.Builder httpClientBuilder, Retrofit.Builder retrofitBuilder) { super(httpClientBuilder, retrofitBuilder); + buildable = new Buildable(); } /** @@ -61,34 +62,37 @@ public Builder(OkHttpClient.Builder httpClientBuilder, Retrofit.Builder retrofit * @param environment the environment the application is running in * @return the builder itself for chaining */ - public Builder withDefaultBaseUrl(AzureEnvironment environment) { + public RestClient.Builder.Buildable withDefaultBaseUrl(AzureEnvironment environment) { withBaseUrl(environment.getBaseUrl()); - return this; + return buildable; } - /** - * Build an AzureRestClient with all the current configurations. - * - * @return an {@link AzureRestClient}. - */ - public AzureRestClient build() { - AzureJacksonMapperAdapter mapperAdapter = new AzureJacksonMapperAdapter(); - OkHttpClient httpClient = httpClientBuilder - .addInterceptor(baseUrlHandler) - .addInterceptor(customHeadersInterceptor) - .addInterceptor(new RetryHandler()) - .build(); - return new AzureRestClient(httpClient, - retrofitBuilder - .baseUrl(baseUrl) - .client(httpClient) - .addConverterFactory(mapperAdapter.getConverterFactory()) - .build(), - credentials, - customHeadersInterceptor, - userAgentInterceptor, - baseUrlHandler, - mapperAdapter); + public class Buildable extends RestClient.Builder.Buildable { + /** + * Build an AzureRestClient with all the current configurations. + * + * @return an {@link AzureRestClient}. + */ + @Override + public AzureRestClient build() { + AzureJacksonMapperAdapter mapperAdapter = new AzureJacksonMapperAdapter(); + OkHttpClient httpClient = httpClientBuilder + .addInterceptor(baseUrlHandler) + .addInterceptor(customHeadersInterceptor) + .addInterceptor(new RetryHandler()) + .build(); + return new AzureRestClient(httpClient, + retrofitBuilder + .baseUrl(baseUrl) + .client(httpClient) + .addConverterFactory(mapperAdapter.getConverterFactory()) + .build(), + credentials, + customHeadersInterceptor, + userAgentInterceptor, + baseUrlHandler, + mapperAdapter); + } } } } 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 fcfc32092c884..0bc2d1bbdeaa4 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/RestClient.java +++ b/client-runtime/src/main/java/com/microsoft/rest/RestClient.java @@ -130,6 +130,8 @@ public static class Builder { protected BaseUrlHandler baseUrlHandler; /** The interceptor to set 'User-Agent' header. */ protected UserAgentInterceptor userAgentInterceptor; + /** The inner Builder instance. */ + protected Buildable buildable; /** * Creates an instance of the builder with a base URL to the service. @@ -161,6 +163,7 @@ public Builder(OkHttpClient.Builder httpClientBuilder, Retrofit.Builder retrofit .cookieJar(new JavaNetCookieJar(cookieManager)) .addInterceptor(userAgentInterceptor); this.retrofitBuilder = retrofitBuilder; + this.buildable = new Buildable(); } /** @@ -169,9 +172,9 @@ public Builder(OkHttpClient.Builder httpClientBuilder, Retrofit.Builder retrofit * @param baseUrl the base URL to use. * @return the builder itself for chaining. */ - public Builder withBaseUrl(String baseUrl) { + public Buildable withBaseUrl(String baseUrl) { this.baseUrl = baseUrl; - return this; + return buildable; } /** @@ -180,7 +183,7 @@ public Builder withBaseUrl(String baseUrl) { * @param serviceClientClass the service client class containing a default base URL. * @return the builder itself for chaining. */ - public Builder withDefaultBaseUrl(Class serviceClientClass) { + public Buildable withDefaultBaseUrl(Class serviceClientClass) { try { Field field = serviceClientClass.getDeclaredField("DEFAULT_BASE_URL"); field.setAccessible(true); @@ -190,79 +193,85 @@ public Builder withDefaultBaseUrl(Class serviceClientClass) { } catch (IllegalAccessException e) { throw new UnsupportedOperationException("Cannot read static field DEFAULT_BASE_URL", e); } - return this; + return buildable; } /** - * Sets the user agent header. - * - * @param userAgent the user agent header. - * @return the builder itself for chaining. + * The inner class from which a Rest Client can be built. */ - public Builder withUserAgent(String userAgent) { - this.userAgentInterceptor.setUserAgent(userAgent); - return this; - } + public class Buildable { + /** + * Sets the user agent header. + * + * @param userAgent the user agent header. + * @return the builder itself for chaining. + */ + public Buildable withUserAgent(String userAgent) { + userAgentInterceptor.setUserAgent(userAgent); + return this; + } - /** - * Sets the credentials. - * - * @param credentials the credentials object. - * @return the builder itself for chaining. - */ - public Builder withCredentials(ServiceClientCredentials credentials) { - this.credentials = credentials; - if (credentials != null) { - credentials.applyCredentialsFilter(httpClientBuilder); + /** + * Sets the credentials. + * + * @param credentials the credentials object. + * @return the builder itself for chaining. + */ + public Buildable withCredentials(ServiceClientCredentials credentials) { + Builder.this.credentials = credentials; + if (credentials != null) { + credentials.applyCredentialsFilter(httpClientBuilder); + } + return this; } - return this; - } - /** - * Sets the log level. - * - * @param logLevel the {@link okhttp3.logging.HttpLoggingInterceptor.Level} enum. - * @return the builder itself for chaining. - */ - public Builder withLogLevel(HttpLoggingInterceptor.Level logLevel) { - this.httpClientBuilder.addInterceptor(new HttpLoggingInterceptor().setLevel(logLevel)); - return this; - } + /** + * Sets the log level. + * + * @param logLevel the {@link okhttp3.logging.HttpLoggingInterceptor.Level} enum. + * @return the builder itself for chaining. + */ + public Buildable withLogLevel(HttpLoggingInterceptor.Level logLevel) { + httpClientBuilder.addInterceptor(new HttpLoggingInterceptor().setLevel(logLevel)); + return this; + } - /** - * Add an interceptor the Http client pipeline. - * - * @param interceptor the interceptor to add. - * @return the builder itself for chaining. - */ - public Builder withInterceptor(Interceptor interceptor) { - this.httpClientBuilder.addInterceptor(interceptor); - return this; - } + /** + * Add an interceptor the Http client pipeline. + * + * @param interceptor the interceptor to add. + * @return the builder itself for chaining. + */ + public Buildable withInterceptor(Interceptor interceptor) { + httpClientBuilder.addInterceptor(interceptor); + return this; + } + + /** + * Build a RestClient with all the current configurations. + * + * @return a {@link RestClient}. + */ + public RestClient build() { + JacksonMapperAdapter mapperAdapter = new JacksonMapperAdapter(); + OkHttpClient httpClient = httpClientBuilder + .addInterceptor(baseUrlHandler) + .addInterceptor(customHeadersInterceptor) + .addInterceptor(new RetryHandler()) + .build(); + return new RestClient(httpClient, + retrofitBuilder + .baseUrl(baseUrl) + .client(httpClient) + .addConverterFactory(mapperAdapter.getConverterFactory()) + .build(), + credentials, + customHeadersInterceptor, + userAgentInterceptor, + baseUrlHandler, + mapperAdapter); + } - /** - * Build a RestClient with all the current configurations. - * - * @return a {@link RestClient}. - */ - public RestClient build() { - JacksonMapperAdapter mapperAdapter = new JacksonMapperAdapter(); - OkHttpClient httpClient = httpClientBuilder - .addInterceptor(baseUrlHandler) - .addInterceptor(customHeadersInterceptor) - .addInterceptor(new RetryHandler()) - .build(); - return new RestClient(httpClient, - retrofitBuilder - .baseUrl(baseUrl) - .client(httpClient) - .addConverterFactory(mapperAdapter.getConverterFactory()) - .build(), - credentials, - customHeadersInterceptor, - userAgentInterceptor, - baseUrlHandler, - mapperAdapter); } } } diff --git a/client-runtime/src/test/java/com/microsoft/rest/CredentialsTests.java b/client-runtime/src/test/java/com/microsoft/rest/CredentialsTests.java index 797654a98cbdc..fbd3e15f52c9f 100644 --- a/client-runtime/src/test/java/com/microsoft/rest/CredentialsTests.java +++ b/client-runtime/src/test/java/com/microsoft/rest/CredentialsTests.java @@ -27,7 +27,7 @@ public class CredentialsTests { @Test public void basicCredentialsTest() throws Exception { BasicAuthenticationCredentials credentials = new BasicAuthenticationCredentials("user", "pass"); - RestClient.Builder restBuilder = new RestClient.Builder() + RestClient restClient = new RestClient.Builder() .withBaseUrl("http://localhost") .withCredentials(credentials) .withInterceptor(new Interceptor() { @@ -41,8 +41,8 @@ public Response intercept(Chain chain) throws IOException { .protocol(Protocol.HTTP_1_1) .build(); } - }); - ServiceClient serviceClient = new ServiceClient(restBuilder.build()) { }; + }).build(); + ServiceClient serviceClient = new ServiceClient(restClient) { }; Response response = serviceClient.restClient().httpClient().newCall(new Request.Builder().url("http://localhost").build()).execute(); Assert.assertEquals(200, response.code()); } @@ -50,7 +50,7 @@ public Response intercept(Chain chain) throws IOException { @Test public void tokenCredentialsTest() throws Exception { TokenCredentials credentials = new TokenCredentials(null, "this_is_a_token"); - RestClient.Builder restBuilder = new RestClient.Builder() + RestClient restClient = new RestClient.Builder() .withBaseUrl("http://localhost") .withCredentials(credentials) .withInterceptor(new Interceptor() { @@ -64,8 +64,8 @@ public Response intercept(Chain chain) throws IOException { .protocol(Protocol.HTTP_1_1) .build(); } - }); - ServiceClient serviceClient = new ServiceClient(restBuilder.build()) { }; + }).build(); + ServiceClient serviceClient = new ServiceClient(restClient) { }; Response response = serviceClient.restClient().httpClient().newCall(new Request.Builder().url("http://localhost").build()).execute(); Assert.assertEquals(200, response.code()); } diff --git a/client-runtime/src/test/java/com/microsoft/rest/RetryHandlerTests.java b/client-runtime/src/test/java/com/microsoft/rest/RetryHandlerTests.java index 31d8e3a15a0c0..bceb2835068af 100644 --- a/client-runtime/src/test/java/com/microsoft/rest/RetryHandlerTests.java +++ b/client-runtime/src/test/java/com/microsoft/rest/RetryHandlerTests.java @@ -42,9 +42,9 @@ public Response intercept(Chain chain) throws IOException { .build(); } }); - RestClient.Builder restBuilder = new RestClient.Builder( clientBuilder, retrofitBuilder) - .withBaseUrl("http://localhost"); - ServiceClient serviceClient = new ServiceClient(restBuilder.build()) { }; + RestClient restClient = new RestClient.Builder( clientBuilder, retrofitBuilder) + .withBaseUrl("http://localhost").build(); + ServiceClient serviceClient = new ServiceClient(restClient) { }; Response response = serviceClient.restClient().httpClient().newCall( new Request.Builder().url("http://localhost").get().build()).execute(); Assert.assertEquals(501, response.code()); @@ -69,9 +69,9 @@ public Response intercept(Chain chain) throws IOException { .build(); } }); - RestClient.Builder restBuilder = new RestClient.Builder(clientBuilder, retrofitBuilder) - .withBaseUrl("http://localhost"); - ServiceClient serviceClient = new ServiceClient(restBuilder.build()) { }; + RestClient restClient = new RestClient.Builder(clientBuilder, retrofitBuilder) + .withBaseUrl("http://localhost").build(); + ServiceClient serviceClient = new ServiceClient(restClient) { }; Response response = serviceClient.restClient().httpClient().newCall( new Request.Builder().url("http://localhost").get().build()).execute(); Assert.assertEquals(500, response.code()); diff --git a/client-runtime/src/test/java/com/microsoft/rest/ServiceClientTests.java b/client-runtime/src/test/java/com/microsoft/rest/ServiceClientTests.java index 61f5a118b58f0..c4309e729bcf8 100644 --- a/client-runtime/src/test/java/com/microsoft/rest/ServiceClientTests.java +++ b/client-runtime/src/test/java/com/microsoft/rest/ServiceClientTests.java @@ -39,9 +39,9 @@ public Response intercept(Chain chain) throws IOException { .build(); } }); - RestClient.Builder restBuilder = new RestClient.Builder(clientBuilder, retrofitBuilder) - .withBaseUrl("http://localhost"); - ServiceClient serviceClient = new ServiceClient(restBuilder.build()) { }; + RestClient restClient = new RestClient.Builder(clientBuilder, retrofitBuilder) + .withBaseUrl("http://localhost").build(); + ServiceClient serviceClient = new ServiceClient(restClient) { }; Response response = serviceClient.restClient().httpClient().newCall(new Request.Builder().url("http://localhost").build()).execute(); Assert.assertEquals(200, response.code()); } diff --git a/client-runtime/src/test/java/com/microsoft/rest/UserAgentTests.java b/client-runtime/src/test/java/com/microsoft/rest/UserAgentTests.java index 1b9d343048acc..245ceaeae8dd6 100644 --- a/client-runtime/src/test/java/com/microsoft/rest/UserAgentTests.java +++ b/client-runtime/src/test/java/com/microsoft/rest/UserAgentTests.java @@ -7,8 +7,6 @@ package com.microsoft.rest; -import com.microsoft.rest.serializer.JacksonMapperAdapter; - import org.junit.Assert; import org.junit.Test; @@ -22,7 +20,7 @@ public class UserAgentTests { @Test public void defaultUserAgentTests() throws Exception { - RestClient.Builder restBuilder = new RestClient.Builder() + RestClient restClient = new RestClient.Builder() .withBaseUrl("http://localhost") .withInterceptor(new Interceptor() { @Override @@ -35,8 +33,8 @@ public Response intercept(Chain chain) throws IOException { .protocol(Protocol.HTTP_1_1) .build(); } - }); - ServiceClient serviceClient = new ServiceClient(restBuilder.build()) { }; + }).build(); + ServiceClient serviceClient = new ServiceClient(restClient) { }; Response response = serviceClient.restClient().httpClient() .newCall(new Request.Builder().get().url("http://localhost").build()).execute(); Assert.assertEquals(200, response.code()); @@ -45,7 +43,7 @@ public Response intercept(Chain chain) throws IOException { @Test public void customUserAgentTests() throws Exception { - RestClient.Builder restBuilder = new RestClient.Builder() + RestClient restClient = new RestClient.Builder() .withBaseUrl("http://localhost") .withUserAgent("Awesome") .withInterceptor(new Interceptor() { @@ -59,8 +57,8 @@ public Response intercept(Chain chain) throws IOException { .protocol(Protocol.HTTP_1_1) .build(); } - }); - ServiceClient serviceClient = new ServiceClient(restBuilder.build()) { }; + }).build(); + ServiceClient serviceClient = new ServiceClient(restClient) { }; Response response = serviceClient.restClient().httpClient() .newCall(new Request.Builder().get().url("http://localhost").build()).execute(); Assert.assertEquals(200, response.code()); From 05105b477b12dd6e00aecea0116e6e8b5de1cf45 Mon Sep 17 00:00:00 2001 From: Jianghao Lu Date: Thu, 16 Jun 2016 13:39:32 -0700 Subject: [PATCH 3/5] Remove rest client from generic codegen --- .../microsoft/azure/AzureServiceClient.java | 6 +- .../com/microsoft/rest/ServiceClient.java | 63 ++++++++++++++----- .../com/microsoft/rest/CredentialsTests.java | 48 +++++++------- .../com/microsoft/rest/RetryHandlerTests.java | 12 ++-- .../microsoft/rest/ServiceClientTests.java | 6 +- .../com/microsoft/rest/UserAgentTests.java | 28 ++++----- 6 files changed, 95 insertions(+), 68 deletions(-) diff --git a/azure-client-runtime/src/main/java/com/microsoft/azure/AzureServiceClient.java b/azure-client-runtime/src/main/java/com/microsoft/azure/AzureServiceClient.java index 5edc6a662e62c..6c68c9fcd071a 100644 --- a/azure-client-runtime/src/main/java/com/microsoft/azure/AzureServiceClient.java +++ b/azure-client-runtime/src/main/java/com/microsoft/azure/AzureServiceClient.java @@ -13,7 +13,9 @@ /** * ServiceClient is the abstraction for accessing REST operations and their payload data types. */ -public abstract class AzureServiceClient extends ServiceClient { +public abstract class AzureServiceClient { + RestClient restClient; + protected AzureServiceClient(String baseUrl) { this(new RestClient.Builder().withBaseUrl(baseUrl) .withInterceptor(new RequestIdHeaderInterceptor()).build()); @@ -25,7 +27,7 @@ protected AzureServiceClient(String baseUrl) { * @param restClient the REST client */ protected AzureServiceClient(RestClient restClient) { - super(restClient); + this.restClient = restClient; } /** diff --git a/client-runtime/src/main/java/com/microsoft/rest/ServiceClient.java b/client-runtime/src/main/java/com/microsoft/rest/ServiceClient.java index e6d2f4a8d595d..8896f1968f90f 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/ServiceClient.java +++ b/client-runtime/src/main/java/com/microsoft/rest/ServiceClient.java @@ -7,14 +7,25 @@ package com.microsoft.rest; +import com.microsoft.rest.retry.RetryHandler; +import com.microsoft.rest.serializer.JacksonMapperAdapter; + +import java.net.CookieManager; +import java.net.CookiePolicy; + +import okhttp3.JavaNetCookieJar; +import okhttp3.OkHttpClient; +import retrofit2.Retrofit; + /** * ServiceClient is the abstraction for accessing REST operations and their payload data types. */ public abstract class ServiceClient { - /** - * The builder for building the OkHttp client. - */ - private RestClient restClient; + protected OkHttpClient httpClient; + + protected Retrofit retrofit; + + protected JacksonMapperAdapter mapperAdapter; /** * Initializes a new instance of the ServiceClient class. @@ -22,26 +33,46 @@ public abstract class ServiceClient { * @param baseUrl the service endpoint */ protected ServiceClient(String baseUrl) { - this(new RestClient.Builder().withBaseUrl(baseUrl).build()); + this(baseUrl, new OkHttpClient.Builder(), new Retrofit.Builder()); } /** * Initializes a new instance of the ServiceClient class. * - * @param restClient the builder to build up an REST client */ - protected ServiceClient(RestClient restClient) { - if (restClient == null) { - throw new IllegalArgumentException("restClient == null"); + protected ServiceClient(String baseUrl, OkHttpClient.Builder clientBuilder, Retrofit.Builder restBuilder) { + if (clientBuilder == null) { + throw new IllegalArgumentException("clientBuilder == null"); } - this.restClient = restClient; + if (restBuilder == null) { + throw new IllegalArgumentException("restBuilder == null"); + } + this.mapperAdapter = new JacksonMapperAdapter(); + CookieManager cookieManager = new CookieManager(); + cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); + this.httpClient = clientBuilder + .cookieJar(new JavaNetCookieJar(cookieManager)) + .addInterceptor(new UserAgentInterceptor()) + .addInterceptor(new BaseUrlHandler()) + .addInterceptor(new CustomHeadersInterceptor()) + .addInterceptor(new RetryHandler()) + .build(); + this.retrofit = restBuilder + .baseUrl(baseUrl) + .client(httpClient) + .addConverterFactory(mapperAdapter.getConverterFactory()) + .build(); } - /** - * Get the list of interceptors the OkHttp client will execute. - * @return the list of interceptors - */ - public RestClient restClient() { - return this.restClient; + public Retrofit retrofit() { + return this.retrofit; + } + + public OkHttpClient httpClient() { + return this.httpClient; + } + + public JacksonMapperAdapter mapperAdapter() { + return this.mapperAdapter; } } diff --git a/client-runtime/src/test/java/com/microsoft/rest/CredentialsTests.java b/client-runtime/src/test/java/com/microsoft/rest/CredentialsTests.java index fbd3e15f52c9f..31b7daf4b7e25 100644 --- a/client-runtime/src/test/java/com/microsoft/rest/CredentialsTests.java +++ b/client-runtime/src/test/java/com/microsoft/rest/CredentialsTests.java @@ -27,33 +27,33 @@ public class CredentialsTests { @Test public void basicCredentialsTest() throws Exception { BasicAuthenticationCredentials credentials = new BasicAuthenticationCredentials("user", "pass"); - RestClient restClient = new RestClient.Builder() - .withBaseUrl("http://localhost") - .withCredentials(credentials) - .withInterceptor(new Interceptor() { - @Override - public Response intercept(Chain chain) throws IOException { - String header = chain.request().header("Authorization"); - Assert.assertEquals("Basic dXNlcjpwYXNz", header); - return new Response.Builder() - .request(chain.request()) - .code(200) - .protocol(Protocol.HTTP_1_1) - .build(); - } - }).build(); - ServiceClient serviceClient = new ServiceClient(restClient) { }; - Response response = serviceClient.restClient().httpClient().newCall(new Request.Builder().url("http://localhost").build()).execute(); + OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder(); + credentials.applyCredentialsFilter(clientBuilder); + clientBuilder.addInterceptor( + new Interceptor() { + @Override + public Response intercept(Chain chain) throws IOException { + String header = chain.request().header("Authorization"); + Assert.assertEquals("Basic dXNlcjpwYXNz", header); + return new Response.Builder() + .request(chain.request()) + .code(200) + .protocol(Protocol.HTTP_1_1) + .build(); + } + }); + ServiceClient serviceClient = new ServiceClient("http://localhost", clientBuilder, new Retrofit.Builder()) { }; + Response response = serviceClient.httpClient().newCall(new Request.Builder().url("http://localhost").build()).execute(); Assert.assertEquals(200, response.code()); } @Test public void tokenCredentialsTest() throws Exception { TokenCredentials credentials = new TokenCredentials(null, "this_is_a_token"); - RestClient restClient = new RestClient.Builder() - .withBaseUrl("http://localhost") - .withCredentials(credentials) - .withInterceptor(new Interceptor() { + OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder(); + credentials.applyCredentialsFilter(clientBuilder); + clientBuilder.addInterceptor( + new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { String header = chain.request().header("Authorization"); @@ -64,9 +64,9 @@ public Response intercept(Chain chain) throws IOException { .protocol(Protocol.HTTP_1_1) .build(); } - }).build(); - ServiceClient serviceClient = new ServiceClient(restClient) { }; - Response response = serviceClient.restClient().httpClient().newCall(new Request.Builder().url("http://localhost").build()).execute(); + }); + ServiceClient serviceClient = new ServiceClient("http://localhost", clientBuilder, new Retrofit.Builder()) { }; + Response response = serviceClient.httpClient().newCall(new Request.Builder().url("http://localhost").build()).execute(); Assert.assertEquals(200, response.code()); } } diff --git a/client-runtime/src/test/java/com/microsoft/rest/RetryHandlerTests.java b/client-runtime/src/test/java/com/microsoft/rest/RetryHandlerTests.java index bceb2835068af..a086eb39f0d3f 100644 --- a/client-runtime/src/test/java/com/microsoft/rest/RetryHandlerTests.java +++ b/client-runtime/src/test/java/com/microsoft/rest/RetryHandlerTests.java @@ -42,10 +42,8 @@ public Response intercept(Chain chain) throws IOException { .build(); } }); - RestClient restClient = new RestClient.Builder( clientBuilder, retrofitBuilder) - .withBaseUrl("http://localhost").build(); - ServiceClient serviceClient = new ServiceClient(restClient) { }; - Response response = serviceClient.restClient().httpClient().newCall( + ServiceClient serviceClient = new ServiceClient("http://localhost", clientBuilder, retrofitBuilder) { }; + Response response = serviceClient.httpClient().newCall( new Request.Builder().url("http://localhost").get().build()).execute(); Assert.assertEquals(501, response.code()); } @@ -69,10 +67,8 @@ public Response intercept(Chain chain) throws IOException { .build(); } }); - RestClient restClient = new RestClient.Builder(clientBuilder, retrofitBuilder) - .withBaseUrl("http://localhost").build(); - ServiceClient serviceClient = new ServiceClient(restClient) { }; - Response response = serviceClient.restClient().httpClient().newCall( + ServiceClient serviceClient = new ServiceClient("http://localhost", clientBuilder, retrofitBuilder) { }; + Response response = serviceClient.httpClient().newCall( new Request.Builder().url("http://localhost").get().build()).execute(); Assert.assertEquals(500, response.code()); } diff --git a/client-runtime/src/test/java/com/microsoft/rest/ServiceClientTests.java b/client-runtime/src/test/java/com/microsoft/rest/ServiceClientTests.java index c4309e729bcf8..a5815aa0ae7dc 100644 --- a/client-runtime/src/test/java/com/microsoft/rest/ServiceClientTests.java +++ b/client-runtime/src/test/java/com/microsoft/rest/ServiceClientTests.java @@ -39,10 +39,8 @@ public Response intercept(Chain chain) throws IOException { .build(); } }); - RestClient restClient = new RestClient.Builder(clientBuilder, retrofitBuilder) - .withBaseUrl("http://localhost").build(); - ServiceClient serviceClient = new ServiceClient(restClient) { }; - Response response = serviceClient.restClient().httpClient().newCall(new Request.Builder().url("http://localhost").build()).execute(); + ServiceClient serviceClient = new ServiceClient("http://localhost", clientBuilder, retrofitBuilder) { }; + Response response = serviceClient.httpClient().newCall(new Request.Builder().url("http://localhost").build()).execute(); Assert.assertEquals(200, response.code()); } diff --git a/client-runtime/src/test/java/com/microsoft/rest/UserAgentTests.java b/client-runtime/src/test/java/com/microsoft/rest/UserAgentTests.java index 245ceaeae8dd6..f6a3f9491ffa3 100644 --- a/client-runtime/src/test/java/com/microsoft/rest/UserAgentTests.java +++ b/client-runtime/src/test/java/com/microsoft/rest/UserAgentTests.java @@ -13,16 +13,18 @@ import java.io.IOException; import okhttp3.Interceptor; +import okhttp3.OkHttpClient; import okhttp3.Protocol; import okhttp3.Request; import okhttp3.Response; +import retrofit2.Retrofit; public class UserAgentTests { @Test public void defaultUserAgentTests() throws Exception { - RestClient restClient = new RestClient.Builder() - .withBaseUrl("http://localhost") - .withInterceptor(new Interceptor() { + OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder() + .addInterceptor(new UserAgentInterceptor()) + .addInterceptor(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { String header = chain.request().header("User-Agent"); @@ -33,20 +35,18 @@ public Response intercept(Chain chain) throws IOException { .protocol(Protocol.HTTP_1_1) .build(); } - }).build(); - ServiceClient serviceClient = new ServiceClient(restClient) { }; - Response response = serviceClient.restClient().httpClient() + }); + ServiceClient serviceClient = new ServiceClient("http://localhost", clientBuilder, new Retrofit.Builder()) { }; + Response response = serviceClient.httpClient() .newCall(new Request.Builder().get().url("http://localhost").build()).execute(); Assert.assertEquals(200, response.code()); } @Test public void customUserAgentTests() throws Exception { - - RestClient restClient = new RestClient.Builder() - .withBaseUrl("http://localhost") - .withUserAgent("Awesome") - .withInterceptor(new Interceptor() { + OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder() + .addInterceptor(new UserAgentInterceptor()) + .addInterceptor(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { String header = chain.request().header("User-Agent"); @@ -57,9 +57,9 @@ public Response intercept(Chain chain) throws IOException { .protocol(Protocol.HTTP_1_1) .build(); } - }).build(); - ServiceClient serviceClient = new ServiceClient(restClient) { }; - Response response = serviceClient.restClient().httpClient() + }); + ServiceClient serviceClient = new ServiceClient("http://localhost", clientBuilder, new Retrofit.Builder()) { }; + Response response = serviceClient.httpClient() .newCall(new Request.Builder().get().url("http://localhost").build()).execute(); Assert.assertEquals(200, response.code()); } From fecda5c1b3d1620a7f245ea986340079119daecb Mon Sep 17 00:00:00 2001 From: Jianghao Lu Date: Thu, 16 Jun 2016 14:30:42 -0700 Subject: [PATCH 4/5] Move RestClient into azure runtime --- .../com/microsoft/azure/AzureEnvironment.java | 6 +- .../com/microsoft/azure/AzureRestClient.java | 98 ------------------- .../microsoft/azure/AzureServiceClient.java | 15 ++- .../java/com/microsoft/azure}/RestClient.java | 25 +++-- .../RequestIdHeaderInterceptorTests.java | 5 +- .../com/microsoft/rest/ServiceClient.java | 20 +++- .../microsoft/rest/UserAgentInterceptor.java | 8 +- .../com/microsoft/rest/CredentialsTests.java | 1 - .../com/microsoft/rest/RetryHandlerTests.java | 11 +-- .../microsoft/rest/ServiceClientTests.java | 3 +- .../com/microsoft/rest/UserAgentTests.java | 2 +- 11 files changed, 62 insertions(+), 132 deletions(-) delete mode 100644 azure-client-runtime/src/main/java/com/microsoft/azure/AzureRestClient.java rename {client-runtime/src/main/java/com/microsoft/rest => azure-client-runtime/src/main/java/com/microsoft/azure}/RestClient.java (91%) diff --git a/azure-client-runtime/src/main/java/com/microsoft/azure/AzureEnvironment.java b/azure-client-runtime/src/main/java/com/microsoft/azure/AzureEnvironment.java index 6bf28394ca158..517a2c3205ac1 100644 --- a/azure-client-runtime/src/main/java/com/microsoft/azure/AzureEnvironment.java +++ b/azure-client-runtime/src/main/java/com/microsoft/azure/AzureEnvironment.java @@ -7,8 +7,6 @@ package com.microsoft.azure; -import com.microsoft.rest.RestClient; - /** * An instance of this class describes an environment in Azure. */ @@ -86,8 +84,8 @@ public String getBaseUrl() { * * @return a builder for the rest client. */ - public RestClient.Builder newRestClientBuilder() { - return new AzureRestClient.Builder() + public RestClient.Builder.Buildable newRestClientBuilder() { + return new RestClient.Builder() .withDefaultBaseUrl(this) .withInterceptor(new RequestIdHeaderInterceptor()); } diff --git a/azure-client-runtime/src/main/java/com/microsoft/azure/AzureRestClient.java b/azure-client-runtime/src/main/java/com/microsoft/azure/AzureRestClient.java deleted file mode 100644 index 373d09268d013..0000000000000 --- a/azure-client-runtime/src/main/java/com/microsoft/azure/AzureRestClient.java +++ /dev/null @@ -1,98 +0,0 @@ -/** - * - * 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.azure; - -import com.microsoft.azure.serializer.AzureJacksonMapperAdapter; -import com.microsoft.rest.BaseUrlHandler; -import com.microsoft.rest.CustomHeadersInterceptor; -import com.microsoft.rest.RestClient; -import com.microsoft.rest.UserAgentInterceptor; -import com.microsoft.rest.credentials.ServiceClientCredentials; -import com.microsoft.rest.retry.RetryHandler; -import com.microsoft.rest.serializer.JacksonMapperAdapter; - -import okhttp3.OkHttpClient; -import retrofit2.Retrofit; - -/** - * An instance of this class stores the client information for making REST calls to Azure. - */ -public final class AzureRestClient extends RestClient { - private AzureRestClient(OkHttpClient httpClient, - Retrofit retrofit, - ServiceClientCredentials credentials, - CustomHeadersInterceptor customHeadersInterceptor, - UserAgentInterceptor userAgentInterceptor, - BaseUrlHandler baseUrlHandler, - JacksonMapperAdapter mapperAdapter) { - super(httpClient, retrofit, credentials, customHeadersInterceptor, - userAgentInterceptor, baseUrlHandler, mapperAdapter); - } - - /** - * The builder class for building a REST client. - */ - public static class Builder extends RestClient.Builder { - /** - * Creates an instance of the builder with a base URL to the service. - */ - public Builder() { - super(); - } - - /** - * Creates an instance of the builder with a base URL and 2 custom builders. - * - * @param httpClientBuilder the builder to build an {@link OkHttpClient}. - * @param retrofitBuilder the builder to build a {@link Retrofit}. - */ - public Builder(OkHttpClient.Builder httpClientBuilder, Retrofit.Builder retrofitBuilder) { - super(httpClientBuilder, retrofitBuilder); - buildable = new Buildable(); - } - - /** - * Sets the base URL with the default from the Azure Environment. - * - * @param environment the environment the application is running in - * @return the builder itself for chaining - */ - public RestClient.Builder.Buildable withDefaultBaseUrl(AzureEnvironment environment) { - withBaseUrl(environment.getBaseUrl()); - return buildable; - } - - public class Buildable extends RestClient.Builder.Buildable { - /** - * Build an AzureRestClient with all the current configurations. - * - * @return an {@link AzureRestClient}. - */ - @Override - public AzureRestClient build() { - AzureJacksonMapperAdapter mapperAdapter = new AzureJacksonMapperAdapter(); - OkHttpClient httpClient = httpClientBuilder - .addInterceptor(baseUrlHandler) - .addInterceptor(customHeadersInterceptor) - .addInterceptor(new RetryHandler()) - .build(); - return new AzureRestClient(httpClient, - retrofitBuilder - .baseUrl(baseUrl) - .client(httpClient) - .addConverterFactory(mapperAdapter.getConverterFactory()) - .build(), - credentials, - customHeadersInterceptor, - userAgentInterceptor, - baseUrlHandler, - mapperAdapter); - } - } - } -} diff --git a/azure-client-runtime/src/main/java/com/microsoft/azure/AzureServiceClient.java b/azure-client-runtime/src/main/java/com/microsoft/azure/AzureServiceClient.java index 6c68c9fcd071a..658103230dd01 100644 --- a/azure-client-runtime/src/main/java/com/microsoft/azure/AzureServiceClient.java +++ b/azure-client-runtime/src/main/java/com/microsoft/azure/AzureServiceClient.java @@ -7,14 +7,14 @@ package com.microsoft.azure; -import com.microsoft.rest.RestClient; -import com.microsoft.rest.ServiceClient; - /** * ServiceClient is the abstraction for accessing REST operations and their payload data types. */ public abstract class AzureServiceClient { - RestClient restClient; + /** + * The RestClient instance storing all information needed for making REST calls. + */ + private RestClient restClient; protected AzureServiceClient(String baseUrl) { this(new RestClient.Builder().withBaseUrl(baseUrl) @@ -38,4 +38,11 @@ protected AzureServiceClient(RestClient restClient) { public String userAgent() { return "Azure-SDK-For-Java/" + getClass().getPackage().getImplementationVersion(); } + + /** + * @return the {@link RestClient} instance. + */ + public RestClient restClient() { + return restClient; + } } diff --git a/client-runtime/src/main/java/com/microsoft/rest/RestClient.java b/azure-client-runtime/src/main/java/com/microsoft/azure/RestClient.java similarity index 91% rename from client-runtime/src/main/java/com/microsoft/rest/RestClient.java rename to azure-client-runtime/src/main/java/com/microsoft/azure/RestClient.java index 0bc2d1bbdeaa4..2517f890a52ca 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/RestClient.java +++ b/azure-client-runtime/src/main/java/com/microsoft/azure/RestClient.java @@ -5,8 +5,12 @@ * */ -package com.microsoft.rest; +package com.microsoft.azure; +import com.microsoft.azure.serializer.AzureJacksonMapperAdapter; +import com.microsoft.rest.BaseUrlHandler; +import com.microsoft.rest.CustomHeadersInterceptor; +import com.microsoft.rest.UserAgentInterceptor; import com.microsoft.rest.credentials.ServiceClientCredentials; import com.microsoft.rest.retry.RetryHandler; import com.microsoft.rest.serializer.JacksonMapperAdapter; @@ -188,14 +192,23 @@ public Buildable withDefaultBaseUrl(Class serviceClientClass) { Field field = serviceClientClass.getDeclaredField("DEFAULT_BASE_URL"); field.setAccessible(true); baseUrl = (String) field.get(null); - } catch (NoSuchFieldException e) { - throw new UnsupportedOperationException("Cannot read static field DEFAULT_BASE_URL", e); - } catch (IllegalAccessException e) { + } catch (NoSuchFieldException | IllegalAccessException e) { throw new UnsupportedOperationException("Cannot read static field DEFAULT_BASE_URL", e); } return buildable; } + /** + * Sets the base URL with the default from the Azure Environment. + * + * @param environment the environment the application is running in + * @return the builder itself for chaining + */ + public RestClient.Builder.Buildable withDefaultBaseUrl(AzureEnvironment environment) { + withBaseUrl(environment.getBaseUrl()); + return buildable; + } + /** * The inner class from which a Rest Client can be built. */ @@ -207,7 +220,7 @@ public class Buildable { * @return the builder itself for chaining. */ public Buildable withUserAgent(String userAgent) { - userAgentInterceptor.setUserAgent(userAgent); + userAgentInterceptor.withUserAgent(userAgent); return this; } @@ -253,7 +266,7 @@ public Buildable withInterceptor(Interceptor interceptor) { * @return a {@link RestClient}. */ public RestClient build() { - JacksonMapperAdapter mapperAdapter = new JacksonMapperAdapter(); + AzureJacksonMapperAdapter mapperAdapter = new AzureJacksonMapperAdapter(); OkHttpClient httpClient = httpClientBuilder .addInterceptor(baseUrlHandler) .addInterceptor(customHeadersInterceptor) diff --git a/azure-client-runtime/src/test/java/com/microsoft/azure/RequestIdHeaderInterceptorTests.java b/azure-client-runtime/src/test/java/com/microsoft/azure/RequestIdHeaderInterceptorTests.java index 24ed68d3dd273..060336cd23d29 100644 --- a/azure-client-runtime/src/test/java/com/microsoft/azure/RequestIdHeaderInterceptorTests.java +++ b/azure-client-runtime/src/test/java/com/microsoft/azure/RequestIdHeaderInterceptorTests.java @@ -7,7 +7,6 @@ package com.microsoft.azure; -import com.microsoft.rest.RestClient; import com.microsoft.rest.retry.RetryHandler; import org.junit.Assert; @@ -25,7 +24,7 @@ public class RequestIdHeaderInterceptorTests { @Test public void newRequestIdForEachCall() throws Exception { - RestClient restClient = new AzureRestClient.Builder() + RestClient restClient = new RestClient.Builder() .withBaseUrl("http://localhost") .withInterceptor(new RequestIdHeaderInterceptor()) .withInterceptor(new Interceptor() { @@ -59,7 +58,7 @@ public Response intercept(Chain chain) throws IOException { @Test public void sameRequestIdForRetry() throws Exception { - RestClient restClient = new AzureRestClient.Builder() + RestClient restClient = new RestClient.Builder() .withBaseUrl("http://localhost") .withInterceptor(new RequestIdHeaderInterceptor()) .withInterceptor(new RetryHandler()) diff --git a/client-runtime/src/main/java/com/microsoft/rest/ServiceClient.java b/client-runtime/src/main/java/com/microsoft/rest/ServiceClient.java index 8896f1968f90f..8311a243de139 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/ServiceClient.java +++ b/client-runtime/src/main/java/com/microsoft/rest/ServiceClient.java @@ -21,11 +21,12 @@ * ServiceClient is the abstraction for accessing REST operations and their payload data types. */ public abstract class ServiceClient { - protected OkHttpClient httpClient; - - protected Retrofit retrofit; - - protected JacksonMapperAdapter mapperAdapter; + /** The HTTP client. */ + private OkHttpClient httpClient; + /** The Retrofit instance. */ + private Retrofit retrofit; + /** The adapter to a Jackson {@link com.fasterxml.jackson.databind.ObjectMapper}. */ + private JacksonMapperAdapter mapperAdapter; /** * Initializes a new instance of the ServiceClient class. @@ -64,14 +65,23 @@ protected ServiceClient(String baseUrl, OkHttpClient.Builder clientBuilder, Retr .build(); } + /** + * @return the Retrofit instance. + */ public Retrofit retrofit() { return this.retrofit; } + /** + * @return the HTTP client. + */ public OkHttpClient httpClient() { return this.httpClient; } + /** + * @return the adapter to a Jackson {@link com.fasterxml.jackson.databind.ObjectMapper}. + */ public JacksonMapperAdapter mapperAdapter() { return this.mapperAdapter; } diff --git a/client-runtime/src/main/java/com/microsoft/rest/UserAgentInterceptor.java b/client-runtime/src/main/java/com/microsoft/rest/UserAgentInterceptor.java index ac4a5ec5a73c4..7b822078d79ab 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/UserAgentInterceptor.java +++ b/client-runtime/src/main/java/com/microsoft/rest/UserAgentInterceptor.java @@ -39,18 +39,22 @@ public UserAgentInterceptor() { * Overwrite the User-Agent header. * * @param userAgent the new user agent value. + * @return the user agent interceptor itself */ - public void setUserAgent(String userAgent) { + public UserAgentInterceptor withUserAgent(String userAgent) { this.userAgent = userAgent; + return this; } /** * Append a text to the User-Agent header. * * @param userAgent the user agent value to append. + * @return the user agent interceptor itself */ - public void appendUserAgent(String userAgent) { + public UserAgentInterceptor appendUserAgent(String userAgent) { this.userAgent += " " + userAgent; + return this; } @Override diff --git a/client-runtime/src/test/java/com/microsoft/rest/CredentialsTests.java b/client-runtime/src/test/java/com/microsoft/rest/CredentialsTests.java index 31b7daf4b7e25..d21de3ef9bd1e 100644 --- a/client-runtime/src/test/java/com/microsoft/rest/CredentialsTests.java +++ b/client-runtime/src/test/java/com/microsoft/rest/CredentialsTests.java @@ -9,7 +9,6 @@ import com.microsoft.rest.credentials.BasicAuthenticationCredentials; import com.microsoft.rest.credentials.TokenCredentials; -import com.microsoft.rest.serializer.JacksonMapperAdapter; import org.junit.Assert; import org.junit.Test; diff --git a/client-runtime/src/test/java/com/microsoft/rest/RetryHandlerTests.java b/client-runtime/src/test/java/com/microsoft/rest/RetryHandlerTests.java index a086eb39f0d3f..068e57b214323 100644 --- a/client-runtime/src/test/java/com/microsoft/rest/RetryHandlerTests.java +++ b/client-runtime/src/test/java/com/microsoft/rest/RetryHandlerTests.java @@ -9,7 +9,11 @@ import com.microsoft.rest.retry.RetryHandler; -import com.microsoft.rest.serializer.JacksonMapperAdapter; +import org.junit.Assert; +import org.junit.Test; + +import java.io.IOException; + import okhttp3.Interceptor; import okhttp3.OkHttpClient; import okhttp3.Protocol; @@ -17,11 +21,6 @@ import okhttp3.Response; import retrofit2.Retrofit; -import org.junit.Assert; -import org.junit.Test; - -import java.io.IOException; - public class RetryHandlerTests { @Test public void exponentialRetryEndOn501() throws Exception { diff --git a/client-runtime/src/test/java/com/microsoft/rest/ServiceClientTests.java b/client-runtime/src/test/java/com/microsoft/rest/ServiceClientTests.java index a5815aa0ae7dc..9d76292d20a03 100644 --- a/client-runtime/src/test/java/com/microsoft/rest/ServiceClientTests.java +++ b/client-runtime/src/test/java/com/microsoft/rest/ServiceClientTests.java @@ -7,10 +7,8 @@ package com.microsoft.rest; -import com.microsoft.rest.serializer.JacksonMapperAdapter; import org.junit.Assert; import org.junit.Test; -import retrofit2.Retrofit; import java.io.IOException; @@ -19,6 +17,7 @@ import okhttp3.Protocol; import okhttp3.Request; import okhttp3.Response; +import retrofit2.Retrofit; public class ServiceClientTests { @Test diff --git a/client-runtime/src/test/java/com/microsoft/rest/UserAgentTests.java b/client-runtime/src/test/java/com/microsoft/rest/UserAgentTests.java index f6a3f9491ffa3..9aa60e5b26313 100644 --- a/client-runtime/src/test/java/com/microsoft/rest/UserAgentTests.java +++ b/client-runtime/src/test/java/com/microsoft/rest/UserAgentTests.java @@ -45,7 +45,7 @@ public Response intercept(Chain chain) throws IOException { @Test public void customUserAgentTests() throws Exception { OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder() - .addInterceptor(new UserAgentInterceptor()) + .addInterceptor(new UserAgentInterceptor().withUserAgent("Awesome")) .addInterceptor(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { From 04af9766d691bd19eb137f9f6cc27fab9c572cc0 Mon Sep 17 00:00:00 2001 From: Jianghao Lu Date: Thu, 16 Jun 2016 15:33:20 -0700 Subject: [PATCH 5/5] Regenerate azure & azure.fluent --- .../microsoft/azure/AzureServiceClient.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/azure-client-runtime/src/main/java/com/microsoft/azure/AzureServiceClient.java b/azure-client-runtime/src/main/java/com/microsoft/azure/AzureServiceClient.java index 658103230dd01..09c8c28fbd536 100644 --- a/azure-client-runtime/src/main/java/com/microsoft/azure/AzureServiceClient.java +++ b/azure-client-runtime/src/main/java/com/microsoft/azure/AzureServiceClient.java @@ -7,6 +7,11 @@ package com.microsoft.azure; +import com.microsoft.rest.serializer.JacksonMapperAdapter; + +import okhttp3.OkHttpClient; +import retrofit2.Retrofit; + /** * ServiceClient is the abstraction for accessing REST operations and their payload data types. */ @@ -45,4 +50,25 @@ public String userAgent() { public RestClient restClient() { return restClient; } + + /** + * @return the Retrofit instance. + */ + public Retrofit retrofit() { + return restClient().retrofit(); + } + + /** + * @return the HTTP client. + */ + public OkHttpClient httpClient() { + return restClient().httpClient(); + } + + /** + * @return the adapter to a Jackson {@link com.fasterxml.jackson.databind.ObjectMapper}. + */ + public JacksonMapperAdapter mapperAdapter() { + return restClient().mapperAdapter(); + } }