From dab76262be04a0cdc4c774220e1577136e6f2c65 Mon Sep 17 00:00:00 2001 From: Jianghao Lu Date: Wed, 11 May 2016 11:56:08 -0700 Subject: [PATCH 01/10] Address eclipse build errors --- .../java/com/microsoft/rest/package-info.java | 4 ---- pom.xml | 17 ----------------- 2 files changed, 21 deletions(-) delete mode 100644 client-runtime/src/test/java/com/microsoft/rest/package-info.java diff --git a/client-runtime/src/test/java/com/microsoft/rest/package-info.java b/client-runtime/src/test/java/com/microsoft/rest/package-info.java deleted file mode 100644 index 137d82e99154..000000000000 --- a/client-runtime/src/test/java/com/microsoft/rest/package-info.java +++ /dev/null @@ -1,4 +0,0 @@ -/** - * The package contains the tests for the client runtime. - */ -package com.microsoft.rest; \ No newline at end of file diff --git a/pom.xml b/pom.xml index a6f30c1d67dd..b6bb0bb29b04 100644 --- a/pom.xml +++ b/pom.xml @@ -143,23 +143,6 @@ - - org.apache.maven.plugins - maven-help-plugin - 2.1.1 - - - validate - - evaluate - - - legal - - - - - org.apache.maven.plugins maven-compiler-plugin From 788f625579edcb0614414ba01cf3354dbe51b6f9 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 11 May 2016 12:32:58 -0700 Subject: [PATCH 02/10] support for Azure.authenticate(File propertiesFile) --- .../ApplicationTokenCredentials.java | 75 +++++++++++++++++++ .../azure/credentials/AzureEnvironment.java | 28 ++++++- 2 files changed, 99 insertions(+), 4 deletions(-) diff --git a/azure-client-authentication/src/main/java/com/microsoft/azure/credentials/ApplicationTokenCredentials.java b/azure-client-authentication/src/main/java/com/microsoft/azure/credentials/ApplicationTokenCredentials.java index 3e995fb06a08..1305f2c2c12d 100644 --- a/azure-client-authentication/src/main/java/com/microsoft/azure/credentials/ApplicationTokenCredentials.java +++ b/azure-client-authentication/src/main/java/com/microsoft/azure/credentials/ApplicationTokenCredentials.java @@ -12,7 +12,10 @@ import com.microsoft.aad.adal4j.ClientCredential; import com.microsoft.rest.credentials.TokenCredentials; +import java.io.File; +import java.io.FileInputStream; import java.io.IOException; +import java.util.Properties; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -52,6 +55,78 @@ public ApplicationTokenCredentials(String clientId, String domain, String secret } } + + /** + * Contains the keys of the settings in a Properties file to read credentials from + */ + private enum CredentialSettings { + SUBSCRIPTION_ID("subscription"), + TENANT_ID("tenant"), + CLIENT_ID("client"), + CLIENT_KEY("key"), + MANAGEMENT_URI("managementURI"), + BASE_URL("baseURL"), + AUTH_URL("authURL"); + + private final String name; + + private CredentialSettings(String name) { + this.name = name; + } + + @Override + public String toString() { + return this.name; + } + } + + + /** + * Initializes the credentials based on the provided credentials file + * @param credentialsFile A file with credentials, using the standard Java properties format + * and the following keys: + * subscription= + * tenant= + * client= + * key= + * managementURI= + * baseURL= + * authURL= + * @return The credentials based on the file. + * @throws IOException + */ + public static ApplicationTokenCredentials fromFile(File credentialsFile) throws IOException { + // Set defaults + Properties authSettings = new Properties(); + authSettings.put(CredentialSettings.AUTH_URL.toString(), AzureEnvironment.AZURE.getAuthenticationEndpoint()); + authSettings.put(CredentialSettings.BASE_URL.toString(), AzureEnvironment.AZURE.getBaseUrl()); + authSettings.put(CredentialSettings.MANAGEMENT_URI.toString(), AzureEnvironment.AZURE.getTokenAudience()); + + // Load the credentials from the file + FileInputStream credentialsFileStream = new FileInputStream(credentialsFile); + authSettings.load(credentialsFileStream); + credentialsFileStream.close(); + + final String clientId = authSettings.getProperty(CredentialSettings.CLIENT_ID.toString()); + final String tenantId = authSettings.getProperty(CredentialSettings.TENANT_ID.toString()); + final String clientKey = authSettings.getProperty(CredentialSettings.CLIENT_KEY.toString()); + final String mgmtUri = authSettings.getProperty(CredentialSettings.MANAGEMENT_URI.toString()); + final String authUrl = authSettings.getProperty(CredentialSettings.AUTH_URL.toString()); + final String baseUrl = authSettings.getProperty(CredentialSettings.BASE_URL.toString()); + + return new ApplicationTokenCredentials( + clientId, + tenantId, + clientKey, + new AzureEnvironment( + authUrl, + mgmtUri, + true, + baseUrl) + ); + } + + /** * Gets the active directory application client id. * diff --git a/azure-client-authentication/src/main/java/com/microsoft/azure/credentials/AzureEnvironment.java b/azure-client-authentication/src/main/java/com/microsoft/azure/credentials/AzureEnvironment.java index dd8a1a2dc9de..fe02d2133037 100644 --- a/azure-client-authentication/src/main/java/com/microsoft/azure/credentials/AzureEnvironment.java +++ b/azure-client-authentication/src/main/java/com/microsoft/azure/credentials/AzureEnvironment.java @@ -11,7 +11,12 @@ * An instance of this class describes an environment in Azure. */ public final class AzureEnvironment { - /** + /** + * Base URL for calls to Azure management API. + */ + private final String baseURL; + + /** * ActiveDirectory Endpoint for the Azure Environment. */ private String authenticationEndpoint; @@ -33,10 +38,15 @@ public final class AzureEnvironment { * @param validateAuthority whether the authentication endpoint should * be validated with Azure AD. */ - public AzureEnvironment(String authenticationEndpoint, String tokenAudience, boolean validateAuthority) { + public AzureEnvironment( + String authenticationEndpoint, + String tokenAudience, + boolean validateAuthority, + String baseUrl) { this.authenticationEndpoint = authenticationEndpoint; this.tokenAudience = tokenAudience; this.validateAuthority = validateAuthority; + this.baseURL = baseUrl; } /** @@ -45,7 +55,8 @@ public AzureEnvironment(String authenticationEndpoint, String tokenAudience, boo public static final AzureEnvironment AZURE = new AzureEnvironment( "https://login.windows.net/", "https://management.core.windows.net/", - true); + true, + "https://management.azure.com/"); /** * Provides the settings for authentication with Azure China. @@ -53,8 +64,17 @@ public AzureEnvironment(String authenticationEndpoint, String tokenAudience, boo public static final AzureEnvironment AZURE_CHINA = new AzureEnvironment( "https://login.chinacloudapi.cn/", "https://management.core.chinacloudapi.cn/", - true); + true, + "https://management.chinacloudapi.cn"); //TODO: Should be confirmed... + /** + * Gets the base URL of the management service + * @return the Base URL for the management service + */ + public String getBaseUrl() { + return this.baseURL; + } + /** * Gets the ActiveDirectory Endpoint for the Azure Environment. * From 5e2de1182a392d95fb382aa7e76c9cdba68336be Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 12 May 2016 14:05:48 -0700 Subject: [PATCH 03/10] enabling Azure.authenticate(File).withDefaultSubscription() based on the subscription provided in the file --- .../ApplicationTokenCredentials.java | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/azure-client-authentication/src/main/java/com/microsoft/azure/credentials/ApplicationTokenCredentials.java b/azure-client-authentication/src/main/java/com/microsoft/azure/credentials/ApplicationTokenCredentials.java index 1305f2c2c12d..c9eb75f58209 100644 --- a/azure-client-authentication/src/main/java/com/microsoft/azure/credentials/ApplicationTokenCredentials.java +++ b/azure-client-authentication/src/main/java/com/microsoft/azure/credentials/ApplicationTokenCredentials.java @@ -33,7 +33,9 @@ public class ApplicationTokenCredentials extends TokenCredentials { private AzureEnvironment environment; /** The current authentication result. */ private AuthenticationResult authenticationResult; - + /** The default subscription to use, if any */ + private String defaultSubscription; + /** * Initializes a new instance of the UserTokenCredentials. * @@ -80,7 +82,18 @@ public String toString() { } } - + /** + * @return The default subscription ID, if any + */ + public String defaultSubscriptionId() { + return defaultSubscription; + } + + ApplicationTokenCredentials withDefaultSubscriptionId(String subscriptionId) { + this.defaultSubscription = subscriptionId; + return this; + } + /** * Initializes the credentials based on the provided credentials file * @param credentialsFile A file with credentials, using the standard Java properties format @@ -113,7 +126,8 @@ public static ApplicationTokenCredentials fromFile(File credentialsFile) throws final String mgmtUri = authSettings.getProperty(CredentialSettings.MANAGEMENT_URI.toString()); final String authUrl = authSettings.getProperty(CredentialSettings.AUTH_URL.toString()); final String baseUrl = authSettings.getProperty(CredentialSettings.BASE_URL.toString()); - + final String defaultSubscriptionId = authSettings.getProperty(CredentialSettings.SUBSCRIPTION_ID.toString()); + return new ApplicationTokenCredentials( clientId, tenantId, @@ -123,7 +137,7 @@ public static ApplicationTokenCredentials fromFile(File credentialsFile) throws mgmtUri, true, baseUrl) - ); + ).withDefaultSubscriptionId(defaultSubscriptionId); } @@ -153,7 +167,7 @@ public String getDomain() { public String getSecret() { return secret; } - + /** * Gets the Azure environment to authenticate with. * From 41224f8b48ebb30ad3a786a9a4ae7821aa211928 Mon Sep 17 00:00:00 2001 From: Jianghao Lu Date: Wed, 11 May 2016 16:27:20 -0700 Subject: [PATCH 04/10] ComputeManager, rename region->location, etc --- azure-client-authentication/build.gradle | 2 +- azure-client-authentication/pom.xml | 6 +++--- .../ApplicationTokenCredentials.java | 1 + .../azure/credentials/UserTokenCredentials.java | 1 + .../credentials/UserTokenCredentialsTests.java | 3 +-- .../com/microsoft/azure}/AzureEnvironment.java | 14 +++++++++++++- .../java/com/microsoft/rest/RestClient.java | 17 ++++++++++------- 7 files changed, 30 insertions(+), 14 deletions(-) rename {azure-client-authentication/src/main/java/com/microsoft/azure/credentials => azure-client-runtime/src/main/java/com/microsoft/azure}/AzureEnvironment.java (88%) diff --git a/azure-client-authentication/build.gradle b/azure-client-authentication/build.gradle index 5f2010826478..a2cd81b3f0c2 100644 --- a/azure-client-authentication/build.gradle +++ b/azure-client-authentication/build.gradle @@ -20,7 +20,7 @@ checkstyle { dependencies { compile 'com.microsoft.azure:adal4j:1.1.2' - compile 'com.microsoft.rest:client-runtime:1.0.0-beta1' + compile 'com.microsoft.azure:azure-client-runtime:1.0.0-beta1' testCompile 'junit:junit:4.12' testCompile 'junit:junit-dep:4.11' deployerJars "org.apache.maven.wagon:wagon-ftp:2.10" diff --git a/azure-client-authentication/pom.xml b/azure-client-authentication/pom.xml index 4dc317b73c13..47582ca1eaa8 100644 --- a/azure-client-authentication/pom.xml +++ b/azure-client-authentication/pom.xml @@ -47,9 +47,9 @@ - com.microsoft.rest - client-runtime - ${parent.version} + com.microsoft.azure + azure-client-runtime + 1.0.0-SNAPSHOT com.microsoft.azure diff --git a/azure-client-authentication/src/main/java/com/microsoft/azure/credentials/ApplicationTokenCredentials.java b/azure-client-authentication/src/main/java/com/microsoft/azure/credentials/ApplicationTokenCredentials.java index c9eb75f58209..f5914f24362f 100644 --- a/azure-client-authentication/src/main/java/com/microsoft/azure/credentials/ApplicationTokenCredentials.java +++ b/azure-client-authentication/src/main/java/com/microsoft/azure/credentials/ApplicationTokenCredentials.java @@ -10,6 +10,7 @@ import com.microsoft.aad.adal4j.AuthenticationContext; import com.microsoft.aad.adal4j.AuthenticationResult; import com.microsoft.aad.adal4j.ClientCredential; +import com.microsoft.azure.AzureEnvironment; import com.microsoft.rest.credentials.TokenCredentials; import java.io.File; diff --git a/azure-client-authentication/src/main/java/com/microsoft/azure/credentials/UserTokenCredentials.java b/azure-client-authentication/src/main/java/com/microsoft/azure/credentials/UserTokenCredentials.java index af02c07507b7..60d6bdb3c0a5 100644 --- a/azure-client-authentication/src/main/java/com/microsoft/azure/credentials/UserTokenCredentials.java +++ b/azure-client-authentication/src/main/java/com/microsoft/azure/credentials/UserTokenCredentials.java @@ -9,6 +9,7 @@ import com.microsoft.aad.adal4j.AuthenticationContext; import com.microsoft.aad.adal4j.AuthenticationResult; +import com.microsoft.azure.AzureEnvironment; import com.microsoft.rest.credentials.TokenCredentials; import java.io.IOException; diff --git a/azure-client-authentication/src/test/java/com/microsoft/azure/credentials/UserTokenCredentialsTests.java b/azure-client-authentication/src/test/java/com/microsoft/azure/credentials/UserTokenCredentialsTests.java index afce04b56ecd..7905ba7e7b21 100644 --- a/azure-client-authentication/src/test/java/com/microsoft/azure/credentials/UserTokenCredentialsTests.java +++ b/azure-client-authentication/src/test/java/com/microsoft/azure/credentials/UserTokenCredentialsTests.java @@ -8,9 +8,8 @@ package com.microsoft.azure.credentials; import com.microsoft.aad.adal4j.AuthenticationResult; - +import com.microsoft.azure.AzureEnvironment; import org.junit.Assert; - import org.junit.Test; import java.io.IOException; diff --git a/azure-client-authentication/src/main/java/com/microsoft/azure/credentials/AzureEnvironment.java b/azure-client-runtime/src/main/java/com/microsoft/azure/AzureEnvironment.java similarity index 88% rename from azure-client-authentication/src/main/java/com/microsoft/azure/credentials/AzureEnvironment.java rename to azure-client-runtime/src/main/java/com/microsoft/azure/AzureEnvironment.java index fe02d2133037..a17c920a0515 100644 --- a/azure-client-authentication/src/main/java/com/microsoft/azure/credentials/AzureEnvironment.java +++ b/azure-client-runtime/src/main/java/com/microsoft/azure/AzureEnvironment.java @@ -5,7 +5,10 @@ * */ -package com.microsoft.azure.credentials; +package com.microsoft.azure; + +import com.microsoft.azure.serializer.AzureJacksonMapperAdapter; +import com.microsoft.rest.RestClient; /** * An instance of this class describes an environment in Azure. @@ -74,6 +77,15 @@ public AzureEnvironment( public String getBaseUrl() { return this.baseURL; } + + /** + * Gets a builder for {@link RestClient}. + * + * @return a builder for the rest client. + */ + public RestClient.Builder newRestClientBuilder() { + return new RestClient.Builder(baseURL).withMapperAdapter(new AzureJacksonMapperAdapter()); + } /** * Gets the ActiveDirectory Endpoint for the Azure Environment. 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 e1a94ee6b7b4..151b6d30d353 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/RestClient.java +++ b/client-runtime/src/main/java/com/microsoft/rest/RestClient.java @@ -138,7 +138,7 @@ public static class Builder { /** * Creates an instance of the builder with a base URL to the service. * - * @param baseUrl the dynamic base URL with varialbes wrapped in "{" and "}". + * @param baseUrl the dynamic base URL with variables wrapped in "{" and "}". */ public Builder(String baseUrl) { this(baseUrl, new OkHttpClient.Builder(), new Retrofit.Builder()); @@ -147,7 +147,7 @@ public Builder(String baseUrl) { /** * Creates an instance of the builder with a base URL and 2 custom builders. * - * @param baseUrl the dynamic base URL with varialbes wrapped in "{" and "}". + * @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}. */ @@ -204,10 +204,7 @@ public Builder withUserAgent(String userAgent) { * @return the builder itself for chaining. */ public Builder withMapperAdapter(JacksonMapperAdapter mapperAdapter) { - if (mapperAdapter != null) { - this.mapperAdapter = mapperAdapter; - this.retrofitBuilder = retrofitBuilder.addConverterFactory(mapperAdapter.getConverterFactory()); - } + this.mapperAdapter = mapperAdapter; return this; } @@ -253,12 +250,18 @@ public Builder withInterceptor(Interceptor interceptor) { * @return a {@link RestClient}. */ public RestClient build() { + if (mapperAdapter == null) { + throw new IllegalArgumentException("Please set mapper adapter."); + } OkHttpClient httpClient = httpClientBuilder .addInterceptor(baseUrlHandler) .addInterceptor(customHeadersInterceptor) .build(); return new RestClient(httpClient, - retrofitBuilder.client(httpClient).build(), + retrofitBuilder + .client(httpClient) + .addConverterFactory(mapperAdapter.getConverterFactory()) + .build(), credentials, customHeadersInterceptor, baseUrlHandler, From 29d8857dfdf54ddc494bd2f0103cb59d4bfc290d Mon Sep 17 00:00:00 2001 From: Jianghao Lu Date: Tue, 17 May 2016 14:40:26 -0700 Subject: [PATCH 05/10] Fix request id to be generated each time --- .../com/microsoft/azure/AzureEnvironment.java | 4 ++- .../microsoft/azure/AzureServiceClient.java | 1 + .../azure/RequestIdHeaderInterceptor.java | 29 +++++++++++++++++++ .../java/com/microsoft/rest/RestClient.java | 2 +- 4 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 azure-client-runtime/src/main/java/com/microsoft/azure/RequestIdHeaderInterceptor.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 a17c920a0515..5b91e595b4c9 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 @@ -84,7 +84,9 @@ public String getBaseUrl() { * @return a builder for the rest client. */ public RestClient.Builder newRestClientBuilder() { - return new RestClient.Builder(baseURL).withMapperAdapter(new AzureJacksonMapperAdapter()); + return new RestClient.Builder(baseURL) + .withInterceptor(new RequestIdHeaderInterceptor()) + .withMapperAdapter(new AzureJacksonMapperAdapter()); } /** 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 94142029cfc6..a3a47dc32b4a 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 @@ -17,6 +17,7 @@ public abstract class AzureServiceClient extends ServiceClient { protected AzureServiceClient(String baseUrl) { this(new RestClient.Builder(baseUrl) + .withInterceptor(new RequestIdHeaderInterceptor()) .withMapperAdapter(new AzureJacksonMapperAdapter()).build()); } diff --git a/azure-client-runtime/src/main/java/com/microsoft/azure/RequestIdHeaderInterceptor.java b/azure-client-runtime/src/main/java/com/microsoft/azure/RequestIdHeaderInterceptor.java new file mode 100644 index 000000000000..ddbe0db30ec0 --- /dev/null +++ b/azure-client-runtime/src/main/java/com/microsoft/azure/RequestIdHeaderInterceptor.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.azure; + +import okhttp3.Interceptor; +import okhttp3.Request; +import okhttp3.Response; + +import java.io.IOException; +import java.util.UUID; + +/** + * An instance of this class puts an UUID in the request header. Azure uses + * the request id as the unique identifier for + */ +public class RequestIdHeaderInterceptor implements Interceptor { + @Override + public Response intercept(Chain chain) throws IOException { + Request request = chain.request().newBuilder() + .header("x-ms-client-request-id", UUID.randomUUID().toString()) + .build(); + return chain.proceed(request); + } +} 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 151b6d30d353..529cdb8de549 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/RestClient.java +++ b/client-runtime/src/main/java/com/microsoft/rest/RestClient.java @@ -168,7 +168,6 @@ public Builder(String baseUrl, OkHttpClient.Builder httpClientBuilder, Retrofit. // Set up OkHttp client this.httpClientBuilder = httpClientBuilder .cookieJar(new JavaNetCookieJar(cookieManager)) - .addInterceptor(new RetryHandler()) .addInterceptor(new UserAgentInterceptor()); // Set up rest adapter this.retrofitBuilder = retrofitBuilder.baseUrl(baseUrl); @@ -256,6 +255,7 @@ public RestClient build() { OkHttpClient httpClient = httpClientBuilder .addInterceptor(baseUrlHandler) .addInterceptor(customHeadersInterceptor) + .addInterceptor(new RetryHandler()) .build(); return new RestClient(httpClient, retrofitBuilder From 9d481365610781fa02a92b9ae9f99f70e32c090c Mon Sep 17 00:00:00 2001 From: Jianghao Lu Date: Tue, 17 May 2016 15:12:38 -0700 Subject: [PATCH 06/10] Add a test for request id header --- .../RequestIdHeaderInterceptorTests.java | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 azure-client-runtime/src/test/java/com/microsoft/azure/RequestIdHeaderInterceptorTests.java 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 new file mode 100644 index 000000000000..e8cce598b05f --- /dev/null +++ b/azure-client-runtime/src/test/java/com/microsoft/azure/RequestIdHeaderInterceptorTests.java @@ -0,0 +1,90 @@ +/** + * + * 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.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; + +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") + .withInterceptor(new RequestIdHeaderInterceptor()) + .withInterceptor(new Interceptor() { + private String firstRequestId = null; + @Override + public Response intercept(Chain chain) throws IOException { + Request request = chain.request(); + if (request.header(REQUEST_ID_HEADER) != null) { + if (firstRequestId == null) { + firstRequestId = request.header(REQUEST_ID_HEADER); + return new Response.Builder().code(200).request(request) + .protocol(Protocol.HTTP_1_1).build(); + } else if (!request.header(REQUEST_ID_HEADER).equals(firstRequestId)) { + return new Response.Builder().code(200).request(request) + .protocol(Protocol.HTTP_1_1).build(); + } + } + return new Response.Builder().code(400).request(request) + .protocol(Protocol.HTTP_1_1).build(); + } + }) + .withMapperAdapter(new AzureJacksonMapperAdapter()) + .build(); + AzureServiceClient serviceClient = new AzureServiceClient(restClient) { }; + Response response = serviceClient.restClient().httpClient() + .newCall(new Request.Builder().get().url("http://localhost").build()).execute(); + Assert.assertEquals(200, response.code()); + response = serviceClient.restClient().httpClient() + .newCall(new Request.Builder().get().url("http://localhost").build()).execute(); + Assert.assertEquals(200, response.code()); + } + + @Test + public void sameRequestIdForRetry() throws Exception { + RestClient restClient = new RestClient.Builder("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(); + if (request.header(REQUEST_ID_HEADER) != null) { + if (firstRequestId == null) { + firstRequestId = request.header(REQUEST_ID_HEADER); + return new Response.Builder().code(500).request(request) + .protocol(Protocol.HTTP_1_1).build(); + } else if (request.header(REQUEST_ID_HEADER).equals(firstRequestId)) { + return new Response.Builder().code(200).request(request) + .protocol(Protocol.HTTP_1_1).build(); + } + } + return new Response.Builder().code(400).request(request) + .protocol(Protocol.HTTP_1_1).build(); + } + }) + .withMapperAdapter(new AzureJacksonMapperAdapter()) + .build(); + AzureServiceClient serviceClient = new AzureServiceClient(restClient) { }; + Response response = serviceClient.restClient().httpClient() + .newCall(new Request.Builder().get().url("http://localhost").build()).execute(); + Assert.assertEquals(200, response.code()); + } +} From b887e80e5a7e5936fc3523f67a1ead68348787e4 Mon Sep 17 00:00:00 2001 From: Jianghao Lu Date: Wed, 18 May 2016 14:00:02 -0700 Subject: [PATCH 07/10] Add user agent to every method --- .../microsoft/azure/AzureServiceClient.java | 1 + .../java/com/microsoft/rest/RestClient.java | 34 +++++---- .../microsoft/rest/UserAgentInterceptor.java | 45 ++++++++---- .../com/microsoft/rest/UserAgentTests.java | 71 ++++++++++--------- 4 files changed, 88 insertions(+), 63 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 a3a47dc32b4a..c2e8efead624 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 @@ -28,5 +28,6 @@ protected AzureServiceClient(String baseUrl) { */ protected AzureServiceClient(RestClient restClient) { super(restClient); + restClient.userAgent().setUserAgent("Azure-SDK-For-Java", getClass().getPackage().getImplementationVersion(), null); } } 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 529cdb8de549..79f25fedc334 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/RestClient.java +++ b/client-runtime/src/main/java/com/microsoft/rest/RestClient.java @@ -35,17 +35,21 @@ public final class RestClient { private BaseUrlHandler baseUrlHandler; /** The adapter to a Jackson {@link com.fasterxml.jackson.databind.ObjectMapper}. */ private JacksonMapperAdapter mapperAdapter; + /** The interceptor to set 'User-Agent' header. */ + private UserAgentInterceptor userAgentInterceptor; private RestClient(OkHttpClient httpClient, Retrofit retrofit, ServiceClientCredentials credentials, CustomHeadersInterceptor customHeadersInterceptor, + UserAgentInterceptor userAgentInterceptor, BaseUrlHandler baseUrlHandler, JacksonMapperAdapter mapperAdapter) { this.httpClient = httpClient; this.retrofit = retrofit; this.credentials = credentials; this.customHeadersInterceptor = customHeadersInterceptor; + this.userAgentInterceptor = userAgentInterceptor; this.baseUrlHandler = baseUrlHandler; this.mapperAdapter = mapperAdapter; } @@ -86,6 +90,16 @@ public Retrofit retrofit() { return retrofit; } + /** + * Get the user agent interceptor where custom user agents can be set or + * appended. + * + * @return the user agent interceptor. + */ + public UserAgentInterceptor userAgent() { + return this.userAgentInterceptor; + } + /** * Get the base URL currently set. If it's a customizable URL, the updated * URL instead of the raw one might be returned. @@ -134,6 +148,8 @@ public static class Builder { private BaseUrlHandler baseUrlHandler; /** The adapter to a Jackson {@link com.fasterxml.jackson.databind.ObjectMapper}. */ private JacksonMapperAdapter mapperAdapter; + /** The interceptor to set 'User-Agent' header. */ + private UserAgentInterceptor userAgentInterceptor; /** * Creates an instance of the builder with a base URL to the service. @@ -165,26 +181,15 @@ public Builder(String baseUrl, OkHttpClient.Builder httpClientBuilder, Retrofit. cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); customHeadersInterceptor = new CustomHeadersInterceptor(); baseUrlHandler = new BaseUrlHandler(baseUrl); + userAgentInterceptor = new UserAgentInterceptor(); // Set up OkHttp client this.httpClientBuilder = httpClientBuilder .cookieJar(new JavaNetCookieJar(cookieManager)) - .addInterceptor(new UserAgentInterceptor()); + .addInterceptor(userAgentInterceptor); // Set up rest adapter this.retrofitBuilder = retrofitBuilder.baseUrl(baseUrl); } - /** - * Sets the base URL. - * - * @param baseUrl the dynamic base URL. - * @return the builder itself for chaining. - */ - public Builder withBaseUrl(String baseUrl) { - this.retrofitBuilder.baseUrl(baseUrl); - this.baseUrlHandler = new BaseUrlHandler(baseUrl); - return this; - } - /** * Sets the user agent header. * @@ -192,7 +197,7 @@ public Builder withBaseUrl(String baseUrl) { * @return the builder itself for chaining. */ public Builder withUserAgent(String userAgent) { - this.httpClientBuilder.addInterceptor(new UserAgentInterceptor(userAgent)); + this.userAgentInterceptor.setUserAgent(userAgent); return this; } @@ -264,6 +269,7 @@ public RestClient build() { .build(), credentials, customHeadersInterceptor, + userAgentInterceptor, baseUrlHandler, 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 4c6c4f6ca5df..548facdda46c 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/UserAgentInterceptor.java +++ b/client-runtime/src/main/java/com/microsoft/rest/UserAgentInterceptor.java @@ -10,7 +10,6 @@ import okhttp3.Interceptor; import okhttp3.Request; import okhttp3.Response; -import okhttp3.internal.Version; import java.io.IOException; @@ -33,28 +32,44 @@ public class UserAgentInterceptor implements Interceptor { * 'User-Agent' header. */ public UserAgentInterceptor() { - this(DEFAULT_USER_AGENT_HEADER); + setUserAgent(DEFAULT_USER_AGENT_HEADER, null, null); } - /** - * Initialize an instance of {@link UserAgentInterceptor} class with the specified - * 'User-Agent' header. - * - * @param userAgent the 'User-Agent' header value. - */ - public UserAgentInterceptor(String userAgent) { - this.userAgent = userAgent; + public void setUserAgent(String userAgent) { + setUserAgent(userAgent, null, null); + } + + public void setUserAgent(String product, String version, String extras) { + this.userAgent = product; + if (version != null) { + this.userAgent += "/" + version; + } + if (extras != null) { + this.userAgent += String.format(" (%s)", extras); + } + } + + public void appendUserAgent(String userAgent) { + appendUserAgent(userAgent, null, null); + } + + public void appendUserAgent(String product, String version, String extras) { + this.userAgent += " " + product; + if (version != null) { + this.userAgent += "/" + version; + } + if (extras != null) { + this.userAgent += String.format(" (%s)", extras); + } } @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); String header = request.header("User-Agent"); - if (header == null || header.equals(Version.userAgent()) || header.equals(DEFAULT_USER_AGENT_HEADER)) { - request = chain.request().newBuilder() - .header("User-Agent", userAgent) - .build(); - } + request = chain.request().newBuilder() + .header("User-Agent", userAgent + " " + header) + .build(); return chain.proceed(request); } } 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 e6b938f93368..af1e35617e83 100644 --- a/client-runtime/src/test/java/com/microsoft/rest/UserAgentTests.java +++ b/client-runtime/src/test/java/com/microsoft/rest/UserAgentTests.java @@ -7,12 +7,11 @@ package com.microsoft.rest; +import com.microsoft.rest.serializer.JacksonMapperAdapter; import okhttp3.Interceptor; -import okhttp3.OkHttpClient; import okhttp3.Protocol; +import okhttp3.Request; import okhttp3.Response; -import retrofit2.Retrofit; - import org.junit.Assert; import org.junit.Test; @@ -21,42 +20,46 @@ public class UserAgentTests { @Test public void defaultUserAgentTests() throws Exception { - OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder(); - Retrofit.Builder retrofitBuilder = new Retrofit.Builder(); - clientBuilder.addInterceptor(new Interceptor() { - @Override - public Response intercept(Chain chain) throws IOException { - String header = chain.request().header("User-Agent"); - Assert.assertEquals("AutoRest-Java", header); - return new Response.Builder() - .request(chain.request()) - .code(200) - .protocol(Protocol.HTTP_1_1) - .build(); - } - }); - RestClient.Builder restBuilder = new RestClient.Builder("http://localhost", clientBuilder, retrofitBuilder); + RestClient.Builder restBuilder = new RestClient.Builder("http://localhost") + .withInterceptor(new Interceptor() { + @Override + public Response intercept(Chain chain) throws IOException { + String header = chain.request().header("User-Agent"); + Assert.assertEquals("AutoRest-Java", header); + return new Response.Builder() + .request(chain.request()) + .code(200) + .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(); + Assert.assertEquals(200, response.code()); } @Test public void customUserAgentTests() throws Exception { - OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder(); - Retrofit.Builder retrofitBuilder = new Retrofit.Builder(); - clientBuilder.addInterceptor(new UserAgentInterceptor("Awesome")); - clientBuilder.addInterceptor(new Interceptor() { - @Override - public Response intercept(Chain chain) throws IOException { - String header = chain.request().header("User-Agent"); - Assert.assertEquals("Awesome", header); - return new Response.Builder() - .request(chain.request()) - .code(200) - .protocol(Protocol.HTTP_1_1) - .build(); - } - }); - RestClient.Builder restBuilder = new RestClient.Builder("http://localhost", clientBuilder, retrofitBuilder); + RestClient.Builder restBuilder = new RestClient.Builder("http://localhost") + .withUserAgent("Awesome") + .withInterceptor(new Interceptor() { + @Override + public Response intercept(Chain chain) throws IOException { + String header = chain.request().header("User-Agent"); + Assert.assertEquals("Awesome", header); + return new Response.Builder() + .request(chain.request()) + .code(200) + .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(); + Assert.assertEquals(200, response.code()); } } From 603745129299229df5c008463be04ce57e266d60 Mon Sep 17 00:00:00 2001 From: Jianghao Lu Date: Wed, 18 May 2016 16:22:12 -0700 Subject: [PATCH 08/10] Send client specific user agent --- .../java/com/microsoft/azure/AzureClient.java | 17 ++++++---- .../microsoft/azure/AzureServiceClient.java | 5 ++- .../java/com/microsoft/rest/RestClient.java | 10 ------ .../microsoft/rest/UserAgentInterceptor.java | 34 +++++-------------- 4 files changed, 22 insertions(+), 44 deletions(-) diff --git a/azure-client-runtime/src/main/java/com/microsoft/azure/AzureClient.java b/azure-client-runtime/src/main/java/com/microsoft/azure/AzureClient.java index d8389869810f..9c442977b2d4 100644 --- a/azure-client-runtime/src/main/java/com/microsoft/azure/AzureClient.java +++ b/azure-client-runtime/src/main/java/com/microsoft/azure/AzureClient.java @@ -7,7 +7,6 @@ package com.microsoft.azure; -import com.microsoft.rest.RestClient; import com.microsoft.rest.ServiceCall; import com.microsoft.rest.ServiceCallback; import com.microsoft.rest.ServiceException; @@ -27,6 +26,7 @@ import retrofit2.Call; import retrofit2.Response; import retrofit2.http.GET; +import retrofit2.http.Header; import retrofit2.http.Url; /** @@ -44,13 +44,16 @@ public class AzureClient extends AzureServiceClient { */ private ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); + private final String serviceClientUserAgent; + /** * Initializes an instance of this class with customized client metadata. * - * @param restClient the REST client to connect to Azure + * @param serviceClient the caller client that initiates the asynchronous request. */ - public AzureClient(RestClient restClient) { - super(restClient); + public AzureClient(AzureServiceClient serviceClient) { + super(serviceClient.restClient()); + this.serviceClientUserAgent = serviceClient.userAgent(); } /** @@ -647,7 +650,7 @@ private Response poll(String url) throws CloudException, IOExcepti port = endpoint.getDefaultPort(); } AsyncService service = restClient().retrofit().create(AsyncService.class); - Response response = service.get(endpoint.getFile()).execute(); + Response response = service.get(endpoint.getFile(), serviceClientUserAgent).execute(); int statusCode = response.code(); if (statusCode != 200 && statusCode != 201 && statusCode != 202 && statusCode != 204) { CloudException exception = new CloudException(statusCode + " is not a valid polling status code"); @@ -684,7 +687,7 @@ private Call pollAsync(String url, final ServiceCallback call = service.get(endpoint.getFile()); + Call call = service.get(endpoint.getFile(), serviceClientUserAgent); call.enqueue(new ServiceResponseCallback(callback) { @Override public void onResponse(Call call, Response response) { @@ -735,7 +738,7 @@ public void setLongRunningOperationRetryTimeout(Integer longRunningOperationRetr */ private interface AsyncService { @GET - Call get(@Url String url); + Call get(@Url String url, @Header("User-Agent") String userAgent); } /** 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 c2e8efead624..ae76abb9d561 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 @@ -28,6 +28,9 @@ protected AzureServiceClient(String baseUrl) { */ protected AzureServiceClient(RestClient restClient) { super(restClient); - restClient.userAgent().setUserAgent("Azure-SDK-For-Java", getClass().getPackage().getImplementationVersion(), null); + } + + public String userAgent() { + return "Azure-SDK-For-Java/" + getClass().getPackage().getImplementationVersion(); } } 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 79f25fedc334..afd69a02e50f 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/RestClient.java +++ b/client-runtime/src/main/java/com/microsoft/rest/RestClient.java @@ -90,16 +90,6 @@ public Retrofit retrofit() { return retrofit; } - /** - * Get the user agent interceptor where custom user agents can be set or - * appended. - * - * @return the user agent interceptor. - */ - public UserAgentInterceptor userAgent() { - return this.userAgentInterceptor; - } - /** * Get the base URL currently set. If it's a customizable URL, the updated * URL instead of the raw one might be returned. 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 548facdda46c..bb15c0f4305d 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/UserAgentInterceptor.java +++ b/client-runtime/src/main/java/com/microsoft/rest/UserAgentInterceptor.java @@ -32,44 +32,26 @@ public class UserAgentInterceptor implements Interceptor { * 'User-Agent' header. */ public UserAgentInterceptor() { - setUserAgent(DEFAULT_USER_AGENT_HEADER, null, null); + this.userAgent = DEFAULT_USER_AGENT_HEADER; } public void setUserAgent(String userAgent) { - setUserAgent(userAgent, null, null); - } - - public void setUserAgent(String product, String version, String extras) { - this.userAgent = product; - if (version != null) { - this.userAgent += "/" + version; - } - if (extras != null) { - this.userAgent += String.format(" (%s)", extras); - } + this.userAgent = userAgent; } public void appendUserAgent(String userAgent) { - appendUserAgent(userAgent, null, null); - } - - public void appendUserAgent(String product, String version, String extras) { - this.userAgent += " " + product; - if (version != null) { - this.userAgent += "/" + version; - } - if (extras != null) { - this.userAgent += String.format(" (%s)", extras); - } + this.userAgent += " " + userAgent; } @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); String header = request.header("User-Agent"); - request = chain.request().newBuilder() - .header("User-Agent", userAgent + " " + header) - .build(); + if (header == null || !userAgent.equals(DEFAULT_USER_AGENT_HEADER)) { + request = chain.request().newBuilder() + .header("User-Agent", userAgent + " " + header) + .build(); + } return chain.proceed(request); } } From 63803e0b0697b5acee723870f0b46dcd1ec6a7fc Mon Sep 17 00:00:00 2001 From: Jianghao Lu Date: Wed, 18 May 2016 18:08:21 -0700 Subject: [PATCH 09/10] Fix runtime tests --- .../com/microsoft/rest/UserAgentInterceptor.java | 16 ++++++++++++---- .../com/microsoft/rest/CredentialsTests.java | 13 +++++-------- .../com/microsoft/rest/RetryHandlerTests.java | 7 +++++-- .../com/microsoft/rest/ServiceClientTests.java | 13 +++++-------- 4 files changed, 27 insertions(+), 22 deletions(-) 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 bb15c0f4305d..7705592ba9f7 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/UserAgentInterceptor.java +++ b/client-runtime/src/main/java/com/microsoft/rest/UserAgentInterceptor.java @@ -47,11 +47,19 @@ public void appendUserAgent(String userAgent) { public Response intercept(Chain chain) throws IOException { Request request = chain.request(); String header = request.header("User-Agent"); - if (header == null || !userAgent.equals(DEFAULT_USER_AGENT_HEADER)) { - request = chain.request().newBuilder() - .header("User-Agent", userAgent + " " + header) - .build(); + if (header == null) { + header = DEFAULT_USER_AGENT_HEADER; } + if (!userAgent.equals(DEFAULT_USER_AGENT_HEADER)) { + if (header.equals(DEFAULT_USER_AGENT_HEADER)) { + header = userAgent; + } else { + header = userAgent + " " + header; + } + } + request = chain.request().newBuilder() + .header("User-Agent", header) + .build(); return chain.proceed(request); } } 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 19b7a8ca2a4f..9071c9ae9dc4 100644 --- a/client-runtime/src/test/java/com/microsoft/rest/CredentialsTests.java +++ b/client-runtime/src/test/java/com/microsoft/rest/CredentialsTests.java @@ -9,19 +9,14 @@ import com.microsoft.rest.credentials.BasicAuthenticationCredentials; import com.microsoft.rest.credentials.TokenCredentials; - +import com.microsoft.rest.serializer.JacksonMapperAdapter; +import okhttp3.*; import org.junit.Assert; import org.junit.Test; +import retrofit2.Retrofit; import java.io.IOException; -import okhttp3.Interceptor; -import okhttp3.OkHttpClient; -import okhttp3.Protocol; -import okhttp3.Request; -import okhttp3.Response; -import retrofit2.Retrofit; - public class CredentialsTests { @Test public void basicCredentialsTest() throws Exception { @@ -29,6 +24,7 @@ public void basicCredentialsTest() throws Exception { 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()) .withCredentials(credentials) .withInterceptor(new Interceptor() { @Override @@ -53,6 +49,7 @@ public void tokenCredentialsTest() throws Exception { 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()) .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 050e7374aba1..97c46f6fe4fc 100644 --- a/client-runtime/src/test/java/com/microsoft/rest/RetryHandlerTests.java +++ b/client-runtime/src/test/java/com/microsoft/rest/RetryHandlerTests.java @@ -9,6 +9,7 @@ import com.microsoft.rest.retry.RetryHandler; +import com.microsoft.rest.serializer.JacksonMapperAdapter; import okhttp3.Interceptor; import okhttp3.OkHttpClient; import okhttp3.Protocol; @@ -41,7 +42,8 @@ public Response intercept(Chain chain) throws IOException { .build(); } }); - RestClient.Builder restBuilder = new RestClient.Builder("http://localhost", clientBuilder, retrofitBuilder); + RestClient.Builder restBuilder = new RestClient.Builder("http://localhost", clientBuilder, retrofitBuilder) + .withMapperAdapter(new JacksonMapperAdapter()); ServiceClient serviceClient = new ServiceClient(restBuilder.build()) { }; Response response = serviceClient.restClient().httpClient().newCall( new Request.Builder().url("http://localhost").get().build()).execute(); @@ -67,7 +69,8 @@ public Response intercept(Chain chain) throws IOException { .build(); } }); - RestClient.Builder restBuilder = new RestClient.Builder("http://localhost", clientBuilder, retrofitBuilder); + RestClient.Builder restBuilder = new RestClient.Builder("http://localhost", clientBuilder, retrofitBuilder) + .withMapperAdapter(new JacksonMapperAdapter()); 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 5497d607549d..de57b15dc397 100644 --- a/client-runtime/src/test/java/com/microsoft/rest/ServiceClientTests.java +++ b/client-runtime/src/test/java/com/microsoft/rest/ServiceClientTests.java @@ -7,18 +7,14 @@ package com.microsoft.rest; +import com.microsoft.rest.serializer.JacksonMapperAdapter; +import okhttp3.*; import org.junit.Assert; import org.junit.Test; +import retrofit2.Retrofit; import java.io.IOException; -import okhttp3.Interceptor; -import okhttp3.OkHttpClient; -import okhttp3.Protocol; -import okhttp3.Request; -import okhttp3.Response; -import retrofit2.Retrofit; - public class ServiceClientTests { @Test public void filterTests() throws Exception { @@ -38,7 +34,8 @@ public Response intercept(Chain chain) throws IOException { .build(); } }); - RestClient.Builder restBuilder = new RestClient.Builder("http://localhost", clientBuilder, retrofitBuilder); + RestClient.Builder restBuilder = new RestClient.Builder("http://localhost", clientBuilder, retrofitBuilder) + .withMapperAdapter(new JacksonMapperAdapter()); 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()); From b443c0f07a67e281a96c2d8332a447240716f46b Mon Sep 17 00:00:00 2001 From: Jianghao Lu Date: Wed, 18 May 2016 20:51:16 -0700 Subject: [PATCH 10/10] Fix styles and tests --- .../java/com/microsoft/rest/UserAgentInterceptor.java | 10 ++++++++++ .../test/java/com/microsoft/rest/UserAgentTests.java | 11 +++++------ 2 files changed, 15 insertions(+), 6 deletions(-) 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 7705592ba9f7..ac4a5ec5a73c 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/UserAgentInterceptor.java +++ b/client-runtime/src/main/java/com/microsoft/rest/UserAgentInterceptor.java @@ -35,10 +35,20 @@ public UserAgentInterceptor() { this.userAgent = DEFAULT_USER_AGENT_HEADER; } + /** + * Overwrite the User-Agent header. + * + * @param userAgent the new user agent value. + */ public void setUserAgent(String userAgent) { this.userAgent = userAgent; } + /** + * Append a text to the User-Agent header. + * + * @param userAgent the user agent value to append. + */ public void appendUserAgent(String userAgent) { this.userAgent += " " + userAgent; } 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 7949ad843365..35970714e3a8 100644 --- a/client-runtime/src/test/java/com/microsoft/rest/UserAgentTests.java +++ b/client-runtime/src/test/java/com/microsoft/rest/UserAgentTests.java @@ -8,18 +8,17 @@ package com.microsoft.rest; import com.microsoft.rest.serializer.JacksonMapperAdapter; -import okhttp3.Interceptor; -import okhttp3.OkHttpClient; -import okhttp3.Protocol; -import okhttp3.Request; -import okhttp3.Response; -import retrofit2.Retrofit; 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 UserAgentTests { @Test public void defaultUserAgentTests() throws Exception {