Skip to content

Commit

Permalink
Merge pull request Azure#19 from jianghaolu/updates
Browse files Browse the repository at this point in the history
Update dependencies & Fix parameterized host
  • Loading branch information
jianghaolu committed Jun 8, 2016
2 parents 87afa33 + 8b43962 commit 1ae4191
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 174 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
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
29 changes: 1 addition & 28 deletions client-runtime/src/main/java/com/microsoft/rest/RestClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,15 @@ public ServiceResponse<T> build(Response<ResponseBody> response) throws E, IOExc

int statusCode = response.code();
ResponseBody responseBody;
if (response.isSuccess()) {
if (response.isSuccessful()) {
responseBody = response.body();
} else {
responseBody = response.errorBody();
}

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 {
Expand Down Expand Up @@ -175,7 +175,7 @@ public ServiceResponse<T> buildEmpty(Response<Void> 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 {
Expand Down
22 changes: 6 additions & 16 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,42 +79,32 @@
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>retrofit</artifactId>
<version>2.0.0-beta4</version>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>com.squareup.okio</groupId>
<artifactId>okio</artifactId>
<version>1.7.0</version>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>logging-interceptor</artifactId>
<version>3.1.1</version>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp-urlconnection</artifactId>
<version>3.1.1</version>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>converter-jackson</artifactId>
<version>2.0.0-beta4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.1</version>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-joda</artifactId>
<version>2.7.1</version>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
Expand Down

0 comments on commit 1ae4191

Please sign in to comment.