Skip to content

Commit

Permalink
Merge commit 'fddca6a8917951772a65a3e5b47c5e72c1f42fb5' into ua
Browse files Browse the repository at this point in the history
  • Loading branch information
jianghaolu committed May 19, 2016
2 parents 9295380 + 63803e0 commit 99cd7ee
Show file tree
Hide file tree
Showing 24 changed files with 405 additions and 218 deletions.
15 changes: 2 additions & 13 deletions azure-android-client-authentication/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.microsoft.aad:adal:1.1.11'
compile 'com.microsoft.rest:client-runtime:1.0.0-beta1'
compile 'com.microsoft.azure:azure-client-runtime:1.0.0-SNAPSHOT'
testCompile 'junit:junit:4.12'
testCompile 'junit:junit-dep:4.11'
deployerJars "org.apache.maven.wagon:wagon-ftp:2.10"
Expand Down Expand Up @@ -95,16 +95,6 @@ uploadArchives {
}
}

task versionInfo(type:Exec){
commandLine 'git rev-parse HEAD'.split()
ext.versionfile = new File("${projectDir}/.gitrevision")
standardOutput = new ByteArrayOutputStream()

doLast {
versionfile.text = "Azure/autorest#" + standardOutput.toString()
}
}

task sourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier = 'sources'
Expand All @@ -116,13 +106,12 @@ task javadoc(type: Javadoc) {
options.encoding = 'UTF-8'
}

task javadocJar(type: Jar, dependsOn: [javadoc, versionInfo]) {
task javadocJar(type: Jar, dependsOn: [javadoc]) {
classifier = 'javadoc'
from javadoc.destinationDir
}

artifacts {
archives sourcesJar
archives javadocJar
archives file: file("${projectDir}/.gitrevision")
}
15 changes: 2 additions & 13 deletions azure-client-authentication/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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-SNAPSHOT'
testCompile 'junit:junit:4.12'
testCompile 'junit:junit-dep:4.11'
deployerJars "org.apache.maven.wagon:wagon-ftp:2.10"
Expand Down Expand Up @@ -70,16 +70,6 @@ test {
}
}

task versionInfo(type:Exec){
commandLine 'git rev-parse HEAD'.split()
ext.versionfile = new File("${projectDir}/.gitrevision")
standardOutput = new ByteArrayOutputStream()

doLast {
versionfile.text = "Azure/autorest#" + standardOutput.toString()
}
}

javadoc {
options.encoding = 'UTF-8'
}
Expand All @@ -89,15 +79,14 @@ task sourcesJar(type: Jar, dependsOn:classes) {
from sourceSets.main.allSource
}

task javadocJar(type: Jar, dependsOn: [javadoc, versionInfo]) {
task javadocJar(type: Jar, dependsOn: [javadoc]) {
classifier = 'javadoc'
from javadoc.destinationDir
}

artifacts {
archives sourcesJar
archives javadocJar
archives file: file("${projectDir}/.gitrevision")
}

test {
Expand Down
6 changes: 3 additions & 3 deletions azure-client-authentication/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@

<dependencies>
<dependency>
<groupId>com.microsoft.rest</groupId>
<artifactId>client-runtime</artifactId>
<version>${parent.version}</version>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-client-runtime</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@
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;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

Expand All @@ -30,6 +34,8 @@ 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.
Expand All @@ -52,6 +58,103 @@ 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 {
/** The subscription GUID. */
SUBSCRIPTION_ID("subscription"),
/** The tenant GUID or domain. */
TENANT_ID("tenant"),
/** The client id for the client application. */
CLIENT_ID("client"),
/** The client secret for the service principal. */
CLIENT_KEY("key"),
/** The management endpoint. */
MANAGEMENT_URI("managementURI"),
/** The base URL to the current Azure environment. */
BASE_URL("baseURL"),
/** The URL to Active Directory authentication. */
AUTH_URL("authURL");

/** The name of the key in the properties file. */
private final String name;

CredentialSettings(String name) {
this.name = name;
}

@Override
public String toString() {
return this.name;
}
}

/**
* @return The default subscription ID, if any
*/
public String defaultSubscriptionId() {
return defaultSubscription;
}

/**
* Set default subscription ID.
*
* @param subscriptionId the default subscription ID.
* @return the credentials object itself.
*/
public 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.
* and the following keys:
* subscription=<subscription-id>
* tenant=<tenant-id>
* client=<client-id>
* key=<client-key>
* managementURI=<management-URI>
* baseURL=<base-URL>
* authURL=<authentication-URL>
* @return The credentials based on the file.
* @throws IOException exception thrown from file access errors.
*/
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());
final String defaultSubscriptionId = authSettings.getProperty(CredentialSettings.SUBSCRIPTION_ID.toString());

return new ApplicationTokenCredentials(
clientId,
tenantId,
clientKey,
new AzureEnvironment(
authUrl,
mgmtUri,
true,
baseUrl)
).withDefaultSubscriptionId(defaultSubscriptionId);
}

/**
* Gets the active directory application client id.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 1 addition & 12 deletions azure-client-runtime/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,6 @@ test {
}
}

task versionInfo(type:Exec){
commandLine 'git rev-parse HEAD'.split()
ext.versionfile = new File("${projectDir}/.gitrevision")
standardOutput = new ByteArrayOutputStream()

doLast {
versionfile.text = "Azure/autorest#" + standardOutput.toString()
}
}

javadoc {
options.encoding = 'UTF-8'
}
Expand All @@ -88,15 +78,14 @@ task sourcesJar(type: Jar, dependsOn:classes) {
from sourceSets.main.allSource
}

task javadocJar(type: Jar, dependsOn: [javadoc, versionInfo]) {
task javadocJar(type: Jar, dependsOn: [javadoc]) {
classifier = 'javadoc'
from javadoc.destinationDir
}

artifacts {
archives sourcesJar
archives javadocJar
archives file: file("${projectDir}/.gitrevision")
}

test {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,6 +26,7 @@
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.http.GET;
import retrofit2.http.Header;
import retrofit2.http.Url;

/**
Expand All @@ -44,13 +44,19 @@ public class AzureClient extends AzureServiceClient {
*/
private ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();

/**
* The user agent from the service client that owns this Azure Client.
*/
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();
}

/**
Expand Down Expand Up @@ -647,7 +653,7 @@ private Response<ResponseBody> poll(String url) throws CloudException, IOExcepti
port = endpoint.getDefaultPort();
}
AsyncService service = restClient().retrofit().create(AsyncService.class);
Response<ResponseBody> response = service.get(endpoint.getFile()).execute();
Response<ResponseBody> 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");
Expand Down Expand Up @@ -684,7 +690,7 @@ private Call<ResponseBody> pollAsync(String url, final ServiceCallback<ResponseB
port = endpoint.getDefaultPort();
}
AsyncService service = restClient().retrofit().create(AsyncService.class);
Call<ResponseBody> call = service.get(endpoint.getFile());
Call<ResponseBody> call = service.get(endpoint.getFile(), serviceClientUserAgent);
call.enqueue(new ServiceResponseCallback<ResponseBody>(callback) {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
Expand Down Expand Up @@ -735,7 +741,7 @@ public void setLongRunningOperationRetryTimeout(Integer longRunningOperationRetr
*/
private interface AsyncService {
@GET
Call<ResponseBody> get(@Url String url);
Call<ResponseBody> get(@Url String url, @Header("User-Agent") String userAgent);
}

/**
Expand Down
Loading

0 comments on commit 99cd7ee

Please sign in to comment.