Skip to content

Commit

Permalink
Squashed 'ClientRuntimes/Java/' changes from 87afa33..6196ce0
Browse files Browse the repository at this point in the history
6196ce0 Merge pull request Azure#20 from jianghaolu/master
06f30cf Checkstyle passes everywhere
2bf6b8b Merge pull request Azure#783 from jianghaolu/javadocs
b14cc72 Merge pull request Azure#779 from jianghaolu/paramhostfix
546e068 Network passes checkstyle
14b071c Merge commit '4aa3dd4b847e747387475e9249c4aba97b6ef8ac' into paramhostfix
1ae4191 Merge pull request Azure#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
  • Loading branch information
jianghaolu committed Jun 15, 2016
1 parent ca79538 commit 785a59e
Show file tree
Hide file tree
Showing 11 changed files with 184 additions and 176 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public <T> ServiceResponse<T> getPutOrPatchResult(Response<ResponseBody> respons

int statusCode = response.code();
ResponseBody responseBody;
if (response.isSuccess()) {
if (response.isSuccessful()) {
responseBody = response.body();
} else {
responseBody = response.errorBody();
Expand Down Expand Up @@ -164,7 +164,7 @@ public <T> AsyncPollingTask<T> getPutOrPatchResultAsync(Response<ResponseBody> r

int statusCode = response.code();
ResponseBody responseBody;
if (response.isSuccess()) {
if (response.isSuccessful()) {
responseBody = response.body();
} else {
responseBody = response.errorBody();
Expand Down Expand Up @@ -252,7 +252,7 @@ public <T> ServiceResponse<T> getPostOrDeleteResult(Response<ResponseBody> respo

int statusCode = response.code();
ResponseBody responseBody;
if (response.isSuccess()) {
if (response.isSuccessful()) {
responseBody = response.body();
} else {
responseBody = response.errorBody();
Expand Down Expand Up @@ -337,7 +337,7 @@ public <T> AsyncPollingTask<T> getPostOrDeleteResultAsync(Response<ResponseBody>

int statusCode = response.code();
ResponseBody responseBody;
if (response.isSuccess()) {
if (response.isSuccessful()) {
responseBody = response.body();
} else {
responseBody = response.errorBody();
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> nodeA = new DAGNode<>("A", "dataA");
DAGNode<String> nodeI = new DAGNode<>("I", "dataI");

DAGNode<String> nodeB = new DAGNode<>("B", "dataB");
nodeB.addDependency(nodeA.key());

DAGNode<String> nodeC = new DAGNode<>("C", "dataC");
nodeC.addDependency(nodeA.key());

DAGNode<String> nodeH = new DAGNode<>("H", "dataH");
nodeH.addDependency(nodeI.key());

DAGNode<String> nodeG = new DAGNode<>("G", "dataG");
nodeG.addDependency(nodeC.key());

DAGNode<String> nodeE = new DAGNode<>("E", "dataE");
nodeE.addDependency(nodeB.key());
nodeE.addDependency(nodeG.key());

DAGNode<String> nodeD = new DAGNode<>("D", "dataD");
nodeD.addDependency(nodeB.key());


DAGNode<String> nodeF = new DAGNode<>("F", "dataF");
nodeF.addDependency(nodeD.key());
nodeF.addDependency(nodeE.key());
nodeF.addDependency(nodeH.key());

DAGraph<String, DAGNode<String>> 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<String> 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<String> 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<String, DAGNode<String>> graphA = createGraph("A");
DAGraph<String, DAGNode<String>> graphI = createGraph("I");

DAGraph<String, DAGNode<String>> graphB = createGraph("B");
graphA.merge(graphB);

DAGraph<String, DAGNode<String>> graphC = createGraph("C");
graphA.merge(graphC);

DAGraph<String, DAGNode<String>> graphH = createGraph("H");
graphI.merge(graphH);

DAGraph<String, DAGNode<String>> graphG = createGraph("G");
graphC.merge(graphG);

DAGraph<String, DAGNode<String>> graphE = createGraph("E");
graphB.merge(graphE);
graphG.merge(graphE);

DAGraph<String, DAGNode<String>> graphD = createGraph("D");
graphB.merge(graphD);

DAGraph<String, DAGNode<String>> graphF = createGraph("F");
graphD.merge(graphF);
graphE.merge(graphF);
graphH.merge(graphF);

DAGraph<String, DAGNode<String>> dag = graphF;
dag.prepare();

DAGNode<String> 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<String, DAGNode<String>> createGraph(String resourceName) {
DAGNode<String> node = new DAGNode<>(resourceName, "data" + resourceName);
DAGraph<String, DAGNode<String>> graph = new DAGraph<>(node);
return graph;
}
}
1 change: 0 additions & 1 deletion build-tools/src/main/resources/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@
<module name="FinalParameters"/>
-->

<module name="TodoComment"/>
<module name="UpperEll"/>

</module>
Expand Down
4 changes: 3 additions & 1 deletion build-tools/src/main/resources/suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
-->

<suppressions>
<suppress checks="Javadoc" files=".*Tests\.java"/>
<suppress checks="Javadoc" files="Test[s]?.*\.java"/>
<suppress checks="Javadoc" files=".*Test[s]?\.java"/>
<suppress checks="Javadoc" files=".*Test[s]?Base\.java"/>
<suppress checks="Javadoc" files=".*CoverageReporter\.java"/>
</suppressions>
14 changes: 6 additions & 8 deletions client-runtime/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
8 changes: 0 additions & 8 deletions client-runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
</dependency>
<dependency>
<groupId>com.squareup.okio</groupId>
<artifactId>okio</artifactId>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>logging-interceptor</artifactId>
Expand All @@ -75,10 +71,6 @@
<groupId>com.squareup.retrofit2</groupId>
<artifactId>converter-jackson</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-joda</artifactId>
Expand Down

This file was deleted.

70 changes: 16 additions & 54 deletions client-runtime/src/main/java/com/microsoft/rest/BaseUrlHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading

0 comments on commit 785a59e

Please sign in to comment.