Skip to content

Commit

Permalink
Fix parameterized host concurrency issue
Browse files Browse the repository at this point in the history
  • Loading branch information
jianghaolu committed Jun 8, 2016
1 parent 7ad2207 commit 3e644d9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 79 deletions.
63 changes: 12 additions & 51 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,28 @@
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) {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
String parameters = request.header("x-ms-parameterized-host");
String[] replacements = parameters.split(", ");
if (replacements.length % 2 != 0) {
throw new IllegalArgumentException("Must provide a replacement value for each pattern");
}
baseUrl = rawUrl;
String baseUrl = request.url().toString();
for (int i = 0; i < replacements.length; i += 2) {
baseUrl = baseUrl.replace(replacements[i], replacements[i + 1]);
baseUrl = baseUrl.replaceAll("(?i)\\Q" + replacements[i] + "\\E", replacements[i + 1]);
}
}

@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
if (baseUrl != null) {
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)
.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

0 comments on commit 3e644d9

Please sign in to comment.