From 785a59ebb1f1249aa40468db84edb644fb82e0a9 Mon Sep 17 00:00:00 2001 From: Jianghao Lu Date: Wed, 15 Jun 2016 15:54:23 -0700 Subject: [PATCH] Squashed 'ClientRuntimes/Java/' changes from 87afa33..6196ce0 6196ce0 Merge pull request #20 from jianghaolu/master 06f30cf Checkstyle passes everywhere 2bf6b8b Merge pull request #783 from jianghaolu/javadocs b14cc72 Merge pull request #779 from jianghaolu/paramhostfix 546e068 Network passes checkstyle 14b071c Merge commit '4aa3dd4b847e747387475e9249c4aba97b6ef8ac' into paramhostfix 1ae4191 Merge pull request #19 from jianghaolu/updates 8b43962 Remove header after use 3e644d9 Fix parameterized host concurrency issue 95fa032 Done storage usages 7ad2207 Update dependencies to official releases git-subtree-dir: ClientRuntimes/Java git-subtree-split: 6196ce04c6741ceefc26f23f96c9e80a1dd78249 --- .../java/com/microsoft/azure/AzureClient.java | 8 +- .../java/com/microsoft/azure/DAGraphTest.java | 145 ++++++++++++++++++ build-tools/src/main/resources/checkstyle.xml | 1 - .../src/main/resources/suppressions.xml | 4 +- client-runtime/build.gradle | 14 +- client-runtime/pom.xml | 8 - .../com/microsoft/rest/AutoRestBaseUrl.java | 53 ------- .../com/microsoft/rest/BaseUrlHandler.java | 70 ++------- .../java/com/microsoft/rest/RestClient.java | 29 +--- .../rest/ServiceResponseBuilder.java | 6 +- pom.xml | 22 +-- 11 files changed, 184 insertions(+), 176 deletions(-) create mode 100644 azure-client-runtime/src/test/java/com/microsoft/azure/DAGraphTest.java delete mode 100644 client-runtime/src/main/java/com/microsoft/rest/AutoRestBaseUrl.java 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 ce28cf8f55..62a760acb4 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 @@ -77,7 +77,7 @@ public ServiceResponse getPutOrPatchResult(Response respons int statusCode = response.code(); ResponseBody responseBody; - if (response.isSuccess()) { + if (response.isSuccessful()) { responseBody = response.body(); } else { responseBody = response.errorBody(); @@ -164,7 +164,7 @@ public AsyncPollingTask getPutOrPatchResultAsync(Response r int statusCode = response.code(); ResponseBody responseBody; - if (response.isSuccess()) { + if (response.isSuccessful()) { responseBody = response.body(); } else { responseBody = response.errorBody(); @@ -252,7 +252,7 @@ public ServiceResponse getPostOrDeleteResult(Response respo int statusCode = response.code(); ResponseBody responseBody; - if (response.isSuccess()) { + if (response.isSuccessful()) { responseBody = response.body(); } else { responseBody = response.errorBody(); @@ -337,7 +337,7 @@ public AsyncPollingTask getPostOrDeleteResultAsync(Response int statusCode = response.code(); ResponseBody responseBody; - if (response.isSuccess()) { + if (response.isSuccessful()) { responseBody = response.body(); } else { responseBody = response.errorBody(); diff --git a/azure-client-runtime/src/test/java/com/microsoft/azure/DAGraphTest.java b/azure-client-runtime/src/test/java/com/microsoft/azure/DAGraphTest.java new file mode 100644 index 0000000000..8c1e12dd70 --- /dev/null +++ b/azure-client-runtime/src/test/java/com/microsoft/azure/DAGraphTest.java @@ -0,0 +1,145 @@ +package com.microsoft.azure; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +public class DAGraphTest { + @Test + public void testDAGraphGetNext() { + /** + * |-------->[D]------>[B]-----------[A] + * | ^ ^ + * | | | + * [F]------->[E]-------| | + * | | | + * | |------->[G]----->[C]---- + * | + * |-------->[H]-------------------->[I] + */ + List expectedOrder = new ArrayList<>(); + expectedOrder.add("A"); expectedOrder.add("I"); + expectedOrder.add("B"); expectedOrder.add("C"); expectedOrder.add("H"); + expectedOrder.add("D"); expectedOrder.add("G"); + expectedOrder.add("E"); + expectedOrder.add("F"); + + DAGNode nodeA = new DAGNode<>("A", "dataA"); + DAGNode nodeI = new DAGNode<>("I", "dataI"); + + DAGNode nodeB = new DAGNode<>("B", "dataB"); + nodeB.addDependency(nodeA.key()); + + DAGNode nodeC = new DAGNode<>("C", "dataC"); + nodeC.addDependency(nodeA.key()); + + DAGNode nodeH = new DAGNode<>("H", "dataH"); + nodeH.addDependency(nodeI.key()); + + DAGNode nodeG = new DAGNode<>("G", "dataG"); + nodeG.addDependency(nodeC.key()); + + DAGNode nodeE = new DAGNode<>("E", "dataE"); + nodeE.addDependency(nodeB.key()); + nodeE.addDependency(nodeG.key()); + + DAGNode nodeD = new DAGNode<>("D", "dataD"); + nodeD.addDependency(nodeB.key()); + + + DAGNode nodeF = new DAGNode<>("F", "dataF"); + nodeF.addDependency(nodeD.key()); + nodeF.addDependency(nodeE.key()); + nodeF.addDependency(nodeH.key()); + + DAGraph> dag = new DAGraph<>(nodeF); + dag.addNode(nodeA); + dag.addNode(nodeB); + dag.addNode(nodeC); + dag.addNode(nodeD); + dag.addNode(nodeE); + dag.addNode(nodeG); + dag.addNode(nodeH); + dag.addNode(nodeI); + + dag.populateDependentKeys(); + DAGNode nextNode = dag.getNext(); + int i = 0; + while (nextNode != null) { + Assert.assertEquals(nextNode.key(), expectedOrder.get(i)); + dag.reportedCompleted(nextNode); + nextNode = dag.getNext(); + i++; + } + + System.out.println("done"); + } + + @Test + public void testGraphMerge() { + /** + * |-------->[D]------>[B]-----------[A] + * | ^ ^ + * | | | + * [F]------->[E]-------| | + * | | | + * | |------->[G]----->[C]---- + * | + * |-------->[H]-------------------->[I] + */ + List expectedOrder = new ArrayList<>(); + expectedOrder.add("A"); expectedOrder.add("I"); + expectedOrder.add("B"); expectedOrder.add("C"); expectedOrder.add("H"); + expectedOrder.add("D"); expectedOrder.add("G"); + expectedOrder.add("E"); + expectedOrder.add("F"); + + DAGraph> graphA = createGraph("A"); + DAGraph> graphI = createGraph("I"); + + DAGraph> graphB = createGraph("B"); + graphA.merge(graphB); + + DAGraph> graphC = createGraph("C"); + graphA.merge(graphC); + + DAGraph> graphH = createGraph("H"); + graphI.merge(graphH); + + DAGraph> graphG = createGraph("G"); + graphC.merge(graphG); + + DAGraph> graphE = createGraph("E"); + graphB.merge(graphE); + graphG.merge(graphE); + + DAGraph> graphD = createGraph("D"); + graphB.merge(graphD); + + DAGraph> graphF = createGraph("F"); + graphD.merge(graphF); + graphE.merge(graphF); + graphH.merge(graphF); + + DAGraph> dag = graphF; + dag.prepare(); + + DAGNode nextNode = dag.getNext(); + int i = 0; + while (nextNode != null) { + Assert.assertEquals(expectedOrder.get(i), nextNode.key()); + // Process the node + dag.reportedCompleted(nextNode); + nextNode = dag.getNext(); + i++; + } + } + + private DAGraph> createGraph(String resourceName) { + DAGNode node = new DAGNode<>(resourceName, "data" + resourceName); + DAGraph> graph = new DAGraph<>(node); + return graph; + } +} diff --git a/build-tools/src/main/resources/checkstyle.xml b/build-tools/src/main/resources/checkstyle.xml index d156ff63e6..1875d6f100 100644 --- a/build-tools/src/main/resources/checkstyle.xml +++ b/build-tools/src/main/resources/checkstyle.xml @@ -248,7 +248,6 @@ --> - diff --git a/build-tools/src/main/resources/suppressions.xml b/build-tools/src/main/resources/suppressions.xml index 690bb7a9fc..29e5bd66ee 100644 --- a/build-tools/src/main/resources/suppressions.xml +++ b/build-tools/src/main/resources/suppressions.xml @@ -31,6 +31,8 @@ --> - + + + \ No newline at end of file diff --git a/client-runtime/build.gradle b/client-runtime/build.gradle index a163e9a9f8..73750c0d3d 100644 --- a/client-runtime/build.gradle +++ b/client-runtime/build.gradle @@ -23,14 +23,12 @@ checkstyle { dependencies { compile 'com.google.guava:guava:18.0' - compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4' - compile 'com.squareup.okhttp3:okhttp:3.2.0' - compile 'com.squareup.okio:okio:1.7.0' - compile 'com.squareup.okhttp3:logging-interceptor:3.1.1' - compile 'com.squareup.okhttp3:okhttp-urlconnection:3.1.1' - compile 'com.squareup.retrofit2:converter-jackson:2.0.0-beta4' - compile 'com.fasterxml.jackson.core:jackson-databind:2.7.1' - compile 'com.fasterxml.jackson.datatype:jackson-datatype-joda:2.7.1' + compile 'com.squareup.retrofit2:retrofit:2.0.2' + compile 'com.squareup.okhttp3:okhttp:3.3.1' + compile 'com.squareup.okhttp3:logging-interceptor:3.3.1' + compile 'com.squareup.okhttp3:okhttp-urlconnection:3.3.1' + compile 'com.squareup.retrofit2:converter-jackson:2.0.2' + compile 'com.fasterxml.jackson.datatype:jackson-datatype-joda:2.7.2' compile 'org.apache.commons:commons-lang3:3.4' testCompile 'junit:junit:4.12' testCompile 'junit:junit-dep:4.11' diff --git a/client-runtime/pom.xml b/client-runtime/pom.xml index a7c0b7441b..e63a421434 100644 --- a/client-runtime/pom.xml +++ b/client-runtime/pom.xml @@ -59,10 +59,6 @@ com.squareup.okhttp3 okhttp - - com.squareup.okio - okio - com.squareup.okhttp3 logging-interceptor @@ -75,10 +71,6 @@ com.squareup.retrofit2 converter-jackson - - com.fasterxml.jackson.core - jackson-databind - com.fasterxml.jackson.datatype jackson-datatype-joda diff --git a/client-runtime/src/main/java/com/microsoft/rest/AutoRestBaseUrl.java b/client-runtime/src/main/java/com/microsoft/rest/AutoRestBaseUrl.java deleted file mode 100644 index d89aa6852f..0000000000 --- a/client-runtime/src/main/java/com/microsoft/rest/AutoRestBaseUrl.java +++ /dev/null @@ -1,53 +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.rest; - -import java.util.HashMap; -import java.util.Map; - -import okhttp3.HttpUrl; -import retrofit2.BaseUrl; - -/** - * An instance of this class stores information of the host of a service. - */ -public class AutoRestBaseUrl implements BaseUrl { - /** A template based URL with variables wrapped in {}s. */ - private String template; - /** a mapping from {} wrapped variables in the template and their actual values. */ - private Map mappings; - - @Override - public HttpUrl url() { - String url = template; - for (Map.Entry entry : mappings.entrySet()) { - url = url.replace(entry.getKey(), entry.getValue()); - } - mappings.clear(); - return HttpUrl.parse(url); - } - - /** - * Creates an instance of a template based URL. - * - * @param url the template based URL to use. - */ - public AutoRestBaseUrl(String url) { - this.template = url; - this.mappings = new HashMap<>(); - } - - /** - * Sets the value for the {} wrapped variables in the template URL. - * @param matcher the {} wrapped variable to replace. - * @param value the value to set for the variable. - */ - public void set(CharSequence matcher, String value) { - this.mappings.put(matcher, value); - } -} diff --git a/client-runtime/src/main/java/com/microsoft/rest/BaseUrlHandler.java b/client-runtime/src/main/java/com/microsoft/rest/BaseUrlHandler.java index f04cbc17ed..46eacbd472 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/BaseUrlHandler.java +++ b/client-runtime/src/main/java/com/microsoft/rest/BaseUrlHandler.java @@ -15,67 +15,29 @@ import okhttp3.Response; /** - * An instance of class handles dynamic base URLs in the HTTP pipeline. + * Handles dynamic replacements on base URL. The arguments must be in pairs + * with the string in raw URL to replace as replacements[i] and the dynamic + * part as replacements[i+1]. E.g. {subdomain}.microsoft.com can be set + * dynamically by setting header x-ms-parameterized-host: "{subdomain}, azure" */ public class BaseUrlHandler implements Interceptor { - /** The URL template for the dynamic URL. */ - private final String rawUrl; - /** The base URL after applying the variable replacements. */ - private String baseUrl; - - /** - * Creates an instance of this class with a URL template. - * - * @param rawUrl the URL template with variables wrapped in "{" and "}". - */ - public BaseUrlHandler(String rawUrl) { - this.rawUrl = rawUrl; - this.baseUrl = null; - } - - /** - * Gets the base URL. - * - * @return the URL template if it's not a dynamic URL or variables in a - * dynamic URL haven't been set. The compiled URL otherwise. - */ - public String baseUrl() { - if (this.baseUrl == null) { - return rawUrl; - } - return this.baseUrl; - } - - /** - * Handles dynamic replacements on base URL. The arguments must be in pairs - * with the string in raw URL to replace as replacements[i] and the dynamic - * part as replacements[i+1]. E.g. {subdomain}.microsoft.com can be set - * dynamically by calling setBaseUrl("{subdomain}", "azure"). - * - * @param replacements the string replacements in pairs. - */ - public void setBaseUrl(String... replacements) { - if (replacements.length % 2 != 0) { - throw new IllegalArgumentException("Must provide a replacement value for each pattern"); - } - baseUrl = rawUrl; - for (int i = 0; i < replacements.length; i += 2) { - baseUrl = baseUrl.replace(replacements[i], replacements[i + 1]); - } - } - @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); - if (baseUrl != null) { + String parameters = request.header("x-ms-parameterized-host"); + if (parameters != null && !parameters.isEmpty()) { + String[] replacements = parameters.split(", "); + if (replacements.length % 2 != 0) { + throw new IllegalArgumentException("Must provide a replacement value for each pattern"); + } + String baseUrl = request.url().toString(); + for (int i = 0; i < replacements.length; i += 2) { + baseUrl = baseUrl.replaceAll("(?i)\\Q" + replacements[i] + "\\E", replacements[i + 1]); + } HttpUrl baseHttpUrl = HttpUrl.parse(baseUrl); - HttpUrl newUrl = request.url().newBuilder() - .host(baseHttpUrl.host()) - .scheme(baseHttpUrl.scheme()) - .port(baseHttpUrl.port()) - .build(); request = request.newBuilder() - .url(newUrl) + .url(baseHttpUrl) + .removeHeader("x-ms-parameterized-host") .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 0d5436b4fa..2546396423 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/RestClient.java +++ b/client-runtime/src/main/java/com/microsoft/rest/RestClient.java @@ -91,33 +91,6 @@ public Retrofit retrofit() { return retrofit; } - /** - * Get the base URL currently set. If it's a customizable URL, the updated - * URL instead of the raw one might be returned. - * - * @return the base URL. -<<<<<<< HEAD - * @see {@link RestClient#setBaseUrl(String...)} -======= - * @see RestClient#setBaseUrl(String...) ->>>>>>> fddca6a8917951772a65a3e5b47c5e72c1f42fb5 - */ - public String baseUrl() { - return baseUrlHandler.baseUrl(); - } - - /** - * Handles dynamic replacements on base URL. The arguments must be in pairs - * with the string in raw URL to replace as replacements[i] and the dynamic - * part as replacements[i+1]. E.g. {subdomain}.microsoft.com can be set - * dynamically by calling setBaseUrl("{subdomain}", "azure"). - * - * @param replacements the string replacements in pairs. - */ - public void setBaseUrl(String... replacements) { - baseUrlHandler.setBaseUrl(replacements); - } - /** * Get the credentials attached to this REST client. * @@ -175,7 +148,7 @@ public Builder(String baseUrl, OkHttpClient.Builder httpClientBuilder, Retrofit. CookieManager cookieManager = new CookieManager(); cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); customHeadersInterceptor = new CustomHeadersInterceptor(); - baseUrlHandler = new BaseUrlHandler(baseUrl); + baseUrlHandler = new BaseUrlHandler(); userAgentInterceptor = new UserAgentInterceptor(); // Set up OkHttp client this.httpClientBuilder = httpClientBuilder diff --git a/client-runtime/src/main/java/com/microsoft/rest/ServiceResponseBuilder.java b/client-runtime/src/main/java/com/microsoft/rest/ServiceResponseBuilder.java index b6e057a0e6..3ad57cb938 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/ServiceResponseBuilder.java +++ b/client-runtime/src/main/java/com/microsoft/rest/ServiceResponseBuilder.java @@ -132,7 +132,7 @@ public ServiceResponse build(Response response) throws E, IOExc int statusCode = response.code(); ResponseBody responseBody; - if (response.isSuccess()) { + if (response.isSuccessful()) { responseBody = response.body(); } else { responseBody = response.errorBody(); @@ -140,7 +140,7 @@ public ServiceResponse build(Response response) throws E, IOExc if (responseTypes.containsKey(statusCode)) { return new ServiceResponse<>((T) buildBody(statusCode, responseBody), response); - } else if (response.isSuccess() && responseTypes.size() == 1) { + } else if (response.isSuccessful() && responseTypes.size() == 1) { return new ServiceResponse<>((T) buildBody(statusCode, responseBody), response); } else { try { @@ -175,7 +175,7 @@ public ServiceResponse buildEmpty(Response response) throws E, IOExcept int statusCode = response.code(); if (responseTypes.containsKey(statusCode)) { return new ServiceResponse<>(response); - } else if (response.isSuccess() && responseTypes.size() == 1) { + } else if (response.isSuccessful() && responseTypes.size() == 1) { return new ServiceResponse<>(response); } else { try { diff --git a/pom.xml b/pom.xml index 51a8ab8ffd..aa77260c58 100644 --- a/pom.xml +++ b/pom.xml @@ -79,42 +79,32 @@ com.squareup.retrofit2 retrofit - 2.0.0-beta4 + 2.0.2 com.squareup.okhttp3 okhttp - 3.2.0 - - - com.squareup.okio - okio - 1.7.0 + 3.3.1 com.squareup.okhttp3 logging-interceptor - 3.1.1 + 3.3.1 com.squareup.okhttp3 okhttp-urlconnection - 3.1.1 + 3.3.1 com.squareup.retrofit2 converter-jackson - 2.0.0-beta4 - - - com.fasterxml.jackson.core - jackson-databind - 2.7.1 + 2.0.2 com.fasterxml.jackson.datatype jackson-datatype-joda - 2.7.1 + 2.7.2 org.apache.commons