diff --git a/pom.xml b/pom.xml index 7c49ba8df72a..9b5fd00cf176 100644 --- a/pom.xml +++ b/pom.xml @@ -5,12 +5,8 @@ --> 4.0.0 - - com.microsoft.azure - 1.0.0-SNAPSHOT - azure-bom - - + com.microsoft.azure + 1.0.0-SNAPSHOT azure-parent pom @@ -47,28 +43,8 @@ - azureoss-snapshots-pr - Azure Private Snapshots - http://azureoss.westus.cloudapp.azure.com:8080/nexus/content/repositories/snapshots-pr/ - default - - true - always - - - - adx-snapshots - Azure ADX Snapshots - http://adxsnapshots.azurewebsites.net - default - - true - always - - - - oss-snapshots - Open Source Snapshots + ossrh + Sonatype Snapshots https://oss.sonatype.org/content/repositories/snapshots/ default @@ -80,12 +56,17 @@ - adx-snapshots - Azure ADX Snapshots - ftp://waws-prod-bay-005.ftp.azurewebsites.windows.net/site/wwwroot + ossrh + Maven Central Snapshots + https://oss.sonatype.org/content/repositories/snapshots true default + + ossrh + Maven Central + https://oss.sonatype.org/service/local/staging/deploy/maven2/ + diff --git a/runtimes/README.md b/runtimes/README.md index dfa96fbf28cb..4356da5475d0 100644 --- a/runtimes/README.md +++ b/runtimes/README.md @@ -1,22 +1,2 @@ -[![Build Status](https://travis-ci.org/Azure/autorest-clientruntime-for-java.svg?branch=javavnext)](https://travis-ci.org/Azure/autorest-clientruntime-for-java) - -# AutoRest Client Runtimes for Java +# autorest-clientruntime-for-java The runtime libraries for AutoRest generated Java clients. - -## Repository structure - -### client-runtime -This is the generic runtime. You will need this for AutoRest generated library using Java code generator. - -### azure-client-runtime -This is the runtime with Azure specific customizations. You will need this for AutoRest generated library using Azure.Java code generator. - -### azure-client-authentication -This package provides access to Active Directory authentication on JDK using OrgId or application ID / secret combinations. Multi-factor-auth is currently not supported. - -### azure-android-client-authentication -This package provides access to Active Directory authentication on Android. You can login with Microsoft accounts, OrgId, with or without multi-factor-auth. - -## Build -To build this repository, you will need maven 2.0+ and gradle 1.6+. -Maven is used for [Java SDK](https://github.com/Azure/azure-sdk-for-java) when it's used as a submodule in there. Gradle is used for [AutoRest](https://github.com/Azure/autorest) when it's used as a submodule in there. diff --git a/runtimes/azure-android-client-authentication/build.gradle b/runtimes/azure-android-client-authentication/build.gradle index df85a81cc546..753fd4b7114e 100644 --- a/runtimes/azure-android-client-authentication/build.gradle +++ b/runtimes/azure-android-client-authentication/build.gradle @@ -95,6 +95,16 @@ 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' @@ -106,7 +116,7 @@ task javadoc(type: Javadoc) { options.encoding = 'UTF-8' } -task javadocJar(type: Jar, dependsOn: [javadoc]) { +task javadocJar(type: Jar, dependsOn: [javadoc, versionInfo]) { classifier = 'javadoc' from javadoc.destinationDir } @@ -114,4 +124,5 @@ task javadocJar(type: Jar, dependsOn: [javadoc]) { artifacts { archives sourcesJar archives javadocJar + archives file: file("${projectDir}/.gitrevision") } \ No newline at end of file diff --git a/runtimes/azure-client-authentication/build.gradle b/runtimes/azure-client-authentication/build.gradle index 5f2010826478..a04020e1fd37 100644 --- a/runtimes/azure-client-authentication/build.gradle +++ b/runtimes/azure-client-authentication/build.gradle @@ -70,6 +70,16 @@ 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' } @@ -79,7 +89,7 @@ task sourcesJar(type: Jar, dependsOn:classes) { from sourceSets.main.allSource } -task javadocJar(type: Jar, dependsOn: [javadoc]) { +task javadocJar(type: Jar, dependsOn: [javadoc, versionInfo]) { classifier = 'javadoc' from javadoc.destinationDir } @@ -87,6 +97,7 @@ task javadocJar(type: Jar, dependsOn: [javadoc]) { artifacts { archives sourcesJar archives javadocJar + archives file: file("${projectDir}/.gitrevision") } test { diff --git a/runtimes/azure-client-runtime/build.gradle b/runtimes/azure-client-runtime/build.gradle index e8a50ca11242..3d90bc184cff 100644 --- a/runtimes/azure-client-runtime/build.gradle +++ b/runtimes/azure-client-runtime/build.gradle @@ -20,7 +20,7 @@ checkstyle { } dependencies { - compile 'com.microsoft.rest:client-runtime:1.0.0-SNAPSHOT' + compile 'com.microsoft.rest:client-runtime:1.0.0-beta1' testCompile 'junit:junit:4.12' testCompile 'junit:junit-dep:4.11' deployerJars "org.apache.maven.wagon:wagon-ftp:2.10" @@ -69,6 +69,16 @@ 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' } @@ -78,7 +88,7 @@ task sourcesJar(type: Jar, dependsOn:classes) { from sourceSets.main.allSource } -task javadocJar(type: Jar, dependsOn: [javadoc]) { +task javadocJar(type: Jar, dependsOn: [javadoc, versionInfo]) { classifier = 'javadoc' from javadoc.destinationDir } @@ -86,6 +96,7 @@ task javadocJar(type: Jar, dependsOn: [javadoc]) { artifacts { archives sourcesJar archives javadocJar + archives file: file("${projectDir}/.gitrevision") } test { diff --git a/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/AzureClient.java b/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/AzureClient.java index d8389869810f..2f3569fc51c5 100644 --- a/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/AzureClient.java +++ b/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/AzureClient.java @@ -7,13 +7,14 @@ package com.microsoft.azure; -import com.microsoft.rest.RestClient; import com.microsoft.rest.ServiceCall; import com.microsoft.rest.ServiceCallback; import com.microsoft.rest.ServiceException; import com.microsoft.rest.ServiceResponse; import com.microsoft.rest.ServiceResponseCallback; import com.microsoft.rest.ServiceResponseWithHeaders; +import com.microsoft.rest.credentials.ServiceClientCredentials; +import com.microsoft.rest.serializer.JacksonMapperAdapter; import java.io.IOException; import java.lang.reflect.Type; @@ -23,9 +24,11 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; +import okhttp3.OkHttpClient; import okhttp3.ResponseBody; import retrofit2.Call; import retrofit2.Response; +import retrofit2.Retrofit; import retrofit2.http.GET; import retrofit2.http.Url; @@ -39,18 +42,32 @@ public class AzureClient extends AzureServiceClient { * used if null. */ private Integer longRunningOperationRetryTimeout; + /** + * The credentials to use for authentication for long running operations. + */ + private ServiceClientCredentials credentials; /** * The executor for asynchronous requests. */ private ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); + /** + * Initializes an instance of this class. + */ + public AzureClient() { + super(); + } + /** * Initializes an instance of this class with customized client metadata. * - * @param restClient the REST client to connect to Azure + * @param clientBuilder customized http client. + * @param retrofitBuilder customized retrofit builder + * @param mapperAdapter the adapter for the Jackson object mapper */ - public AzureClient(RestClient restClient) { - super(restClient); + public AzureClient(OkHttpClient.Builder clientBuilder, Retrofit.Builder retrofitBuilder, JacksonMapperAdapter mapperAdapter) { + super(clientBuilder, retrofitBuilder); + this.mapperAdapter = mapperAdapter; } /** @@ -81,13 +98,13 @@ public ServiceResponse getPutOrPatchResult(Response respons CloudException exception = new CloudException(statusCode + " is not a valid polling status code"); exception.setResponse(response); if (responseBody != null) { - exception.setBody((CloudError) restClient().mapperAdapter().deserialize(responseBody.string(), CloudError.class)); + exception.setBody((CloudError) mapperAdapter.deserialize(responseBody.string(), CloudError.class)); responseBody.close(); } throw exception; } - PollingState pollingState = new PollingState<>(response, this.getLongRunningOperationRetryTimeout(), resourceType, restClient().mapperAdapter()); + PollingState pollingState = new PollingState<>(response, this.getLongRunningOperationRetryTimeout(), resourceType, mapperAdapter); String url = response.raw().request().url().toString(); // Check provisioning state @@ -134,7 +151,7 @@ public ServiceResponseWithHeaders getPutOrPatchResultWi ServiceResponse bodyResponse = getPutOrPatchResult(response, resourceType); return new ServiceResponseWithHeaders<>( bodyResponse.getBody(), - restClient().mapperAdapter().deserialize(restClient().mapperAdapter().serialize(bodyResponse.getResponse().headers()), headerType), + mapperAdapter.deserialize(mapperAdapter.serialize(bodyResponse.getResponse().headers()), headerType), bodyResponse.getResponse() ); } @@ -169,7 +186,7 @@ public AsyncPollingTask getPutOrPatchResultAsync(Response r exception.setResponse(response); try { if (responseBody != null) { - exception.setBody((CloudError) restClient().mapperAdapter().deserialize(responseBody.string(), CloudError.class)); + exception.setBody((CloudError) mapperAdapter.deserialize(responseBody.string(), CloudError.class)); responseBody.close(); } } catch (Exception e) { /* ignore serialization errors on top of service errors */ } @@ -179,7 +196,7 @@ public AsyncPollingTask getPutOrPatchResultAsync(Response r PollingState pollingState; try { - pollingState = new PollingState<>(response, this.getLongRunningOperationRetryTimeout(), resourceType, restClient().mapperAdapter()); + pollingState = new PollingState<>(response, this.getLongRunningOperationRetryTimeout(), resourceType, mapperAdapter); } catch (IOException e) { callback.failure(e); return null; @@ -218,7 +235,7 @@ public void success(ServiceResponse result) { try { callback.success(new ServiceResponseWithHeaders<>( result.getBody(), - restClient().mapperAdapter().deserialize(restClient().mapperAdapter().serialize(result.getResponse().headers()), headerType), + mapperAdapter.deserialize(mapperAdapter.serialize(result.getResponse().headers()), headerType), result.getResponse() )); } catch (IOException e) { @@ -256,13 +273,13 @@ public ServiceResponse getPostOrDeleteResult(Response respo CloudException exception = new CloudException(statusCode + " is not a valid polling status code"); exception.setResponse(response); if (responseBody != null) { - exception.setBody((CloudError) restClient().mapperAdapter().deserialize(responseBody.string(), CloudError.class)); + exception.setBody((CloudError) mapperAdapter.deserialize(responseBody.string(), CloudError.class)); responseBody.close(); } throw exception; } - PollingState pollingState = new PollingState<>(response, this.getLongRunningOperationRetryTimeout(), resourceType, restClient().mapperAdapter()); + PollingState pollingState = new PollingState<>(response, this.getLongRunningOperationRetryTimeout(), resourceType, mapperAdapter); // Check provisioning state while (!AzureAsyncOperation.getTerminalStatuses().contains(pollingState.getStatus())) { @@ -308,7 +325,7 @@ public ServiceResponseWithHeaders getPostOrDeleteResult ServiceResponse bodyResponse = getPostOrDeleteResult(response, resourceType); return new ServiceResponseWithHeaders<>( bodyResponse.getBody(), - restClient().mapperAdapter().deserialize(restClient().mapperAdapter().serialize(bodyResponse.getResponse().headers()), headerType), + mapperAdapter.deserialize(mapperAdapter.serialize(bodyResponse.getResponse().headers()), headerType), bodyResponse.getResponse() ); } @@ -343,7 +360,7 @@ public AsyncPollingTask getPostOrDeleteResultAsync(Response exception.setResponse(response); try { if (responseBody != null) { - exception.setBody((CloudError) restClient().mapperAdapter().deserialize(responseBody.string(), CloudError.class)); + exception.setBody((CloudError) mapperAdapter.deserialize(responseBody.string(), CloudError.class)); responseBody.close(); } } catch (Exception e) { /* ignore serialization errors on top of service errors */ } @@ -353,7 +370,7 @@ public AsyncPollingTask getPostOrDeleteResultAsync(Response PollingState pollingState; try { - pollingState = new PollingState<>(response, this.getLongRunningOperationRetryTimeout(), resourceType, restClient().mapperAdapter()); + pollingState = new PollingState<>(response, this.getLongRunningOperationRetryTimeout(), resourceType, mapperAdapter); } catch (IOException e) { callback.failure(e); return null; @@ -391,7 +408,7 @@ public void success(ServiceResponse result) { try { callback.success(new ServiceResponseWithHeaders<>( result.getBody(), - restClient().mapperAdapter().deserialize(restClient().mapperAdapter().serialize(result.getResponse().headers()), headerType), + mapperAdapter.deserialize(mapperAdapter.serialize(result.getResponse().headers()), headerType), result.getResponse() )); } catch (IOException e) { @@ -567,7 +584,7 @@ private void updateStateFromAzureAsyncOperationHeader(PollingState pollin AzureAsyncOperation body = null; if (response.body() != null) { - body = restClient().mapperAdapter().deserialize(response.body().string(), AzureAsyncOperation.class); + body = mapperAdapter.deserialize(response.body().string(), AzureAsyncOperation.class); response.body().close(); } @@ -575,7 +592,7 @@ private void updateStateFromAzureAsyncOperationHeader(PollingState pollin CloudException exception = new CloudException("no body"); exception.setResponse(response); if (response.errorBody() != null) { - exception.setBody((CloudError) restClient().mapperAdapter().deserialize(response.errorBody().string(), CloudError.class)); + exception.setBody((CloudError) mapperAdapter.deserialize(response.errorBody().string(), CloudError.class)); response.errorBody().close(); } throw exception; @@ -607,14 +624,14 @@ public void success(ServiceResponse result) { try { AzureAsyncOperation body = null; if (result.getBody() != null) { - body = restClient().mapperAdapter().deserialize(result.getBody().string(), AzureAsyncOperation.class); + body = mapperAdapter.deserialize(result.getBody().string(), AzureAsyncOperation.class); result.getBody().close(); } if (body == null || body.getStatus() == null) { CloudException exception = new CloudException("no body"); exception.setResponse(result.getResponse()); if (result.getResponse().errorBody() != null) { - exception.setBody((CloudError) restClient().mapperAdapter().deserialize(result.getResponse().errorBody().string(), CloudError.class)); + exception.setBody((CloudError) mapperAdapter.deserialize(result.getResponse().errorBody().string(), CloudError.class)); result.getResponse().errorBody().close(); } failure(exception); @@ -646,17 +663,18 @@ private Response poll(String url) throws CloudException, IOExcepti if (port == -1) { port = endpoint.getDefaultPort(); } - AsyncService service = restClient().retrofit().create(AsyncService.class); + AsyncService service = this.retrofitBuilder + .baseUrl(endpoint.getProtocol() + "://" + endpoint.getHost() + ":" + port).build().create(AsyncService.class); Response response = service.get(endpoint.getFile()).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"); exception.setResponse(response); if (response.body() != null) { - exception.setBody((CloudError) restClient().mapperAdapter().deserialize(response.body().string(), CloudError.class)); + exception.setBody((CloudError) mapperAdapter.deserialize(response.body().string(), CloudError.class)); response.body().close(); } else if (response.errorBody() != null) { - exception.setBody((CloudError) restClient().mapperAdapter().deserialize(response.errorBody().string(), CloudError.class)); + exception.setBody((CloudError) mapperAdapter.deserialize(response.errorBody().string(), CloudError.class)); response.errorBody().close(); } throw exception; @@ -683,7 +701,8 @@ private Call pollAsync(String url, final ServiceCallback call = service.get(endpoint.getFile()); call.enqueue(new ServiceResponseCallback(callback) { @Override @@ -694,10 +713,10 @@ public void onResponse(Call call, Response response) CloudException exception = new CloudException(statusCode + " is not a valid polling status code"); exception.setResponse(response); if (response.body() != null) { - exception.setBody((CloudError) restClient().mapperAdapter().deserialize(response.body().string(), CloudError.class)); + exception.setBody((CloudError) mapperAdapter.deserialize(response.body().string(), CloudError.class)); response.body().close(); } else if (response.errorBody() != null) { - exception.setBody((CloudError) restClient().mapperAdapter().deserialize(response.errorBody().string(), CloudError.class)); + exception.setBody((CloudError) mapperAdapter.deserialize(response.errorBody().string(), CloudError.class)); response.errorBody().close(); } callback.failure(exception); @@ -730,6 +749,24 @@ public void setLongRunningOperationRetryTimeout(Integer longRunningOperationRetr this.longRunningOperationRetryTimeout = longRunningOperationRetryTimeout; } + /** + * Gets the credentials used for authentication. + * + * @return the credentials. + */ + public ServiceClientCredentials getCredentials() { + return credentials; + } + + /** + * Sets the credentials used for authentication. + * + * @param credentials the credentials. + */ + public void setCredentials(ServiceClientCredentials credentials) { + this.credentials = credentials; + } + /** * The Retrofit service used for polling. */ diff --git a/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/AzureServiceClient.java b/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/AzureServiceClient.java index 94142029cfc6..5feea13fa74d 100644 --- a/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/AzureServiceClient.java +++ b/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/AzureServiceClient.java @@ -8,24 +8,57 @@ package com.microsoft.azure; import com.microsoft.azure.serializer.AzureJacksonMapperAdapter; -import com.microsoft.rest.RestClient; import com.microsoft.rest.ServiceClient; +import com.microsoft.rest.UserAgentInterceptor; +import com.microsoft.rest.retry.RetryHandler; + +import java.net.CookieManager; +import java.net.CookiePolicy; + +import okhttp3.JavaNetCookieJar; +import okhttp3.OkHttpClient; +import retrofit2.Retrofit; /** * ServiceClient is the abstraction for accessing REST operations and their payload data types. */ public abstract class AzureServiceClient extends ServiceClient { - protected AzureServiceClient(String baseUrl) { - this(new RestClient.Builder(baseUrl) - .withMapperAdapter(new AzureJacksonMapperAdapter()).build()); + /** + * Initializes a new instance of the ServiceClient class. + */ + protected AzureServiceClient() { + super(); } /** * Initializes a new instance of the ServiceClient class. * - * @param restClient the REST client + * @param clientBuilder the builder to build up an OkHttp client + * @param retrofitBuilder the builder to build up a rest adapter */ - protected AzureServiceClient(RestClient restClient) { - super(restClient); + protected AzureServiceClient(OkHttpClient.Builder clientBuilder, Retrofit.Builder retrofitBuilder) { + super(clientBuilder, retrofitBuilder); + } + + /** + * This method initializes the builders for Http client and Retrofit with common + * behaviors for all service clients. + */ + @Override + protected void initialize() { + // Add retry handler + CookieManager cookieManager = new CookieManager(); + cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); + + // Set up OkHttp client + this.clientBuilder = clientBuilder + .cookieJar(new JavaNetCookieJar(cookieManager)) + .addInterceptor(new RetryHandler()) + .addInterceptor(new UserAgentInterceptor()); + // Set up rest adapter + this.mapperAdapter = new AzureJacksonMapperAdapter(); + this.retrofitBuilder = retrofitBuilder + .client(clientBuilder.build()) + .addConverterFactory(mapperAdapter.getConverterFactory()); } } diff --git a/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/AzureServiceResponseBuilder.java b/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/AzureServiceResponseBuilder.java index 7ad369ecc568..f553fd575cc0 100644 --- a/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/AzureServiceResponseBuilder.java +++ b/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/AzureServiceResponseBuilder.java @@ -8,7 +8,7 @@ package com.microsoft.azure; import com.google.common.reflect.TypeToken; -import com.microsoft.rest.RestException; +import com.microsoft.rest.AutoRestException; import com.microsoft.rest.ServiceResponse; import com.microsoft.rest.ServiceResponseBuilder; import com.microsoft.rest.serializer.JacksonMapperAdapter; @@ -27,7 +27,7 @@ * @param the return type from caller. * @param the exception to throw in case of error. */ -public class AzureServiceResponseBuilder extends ServiceResponseBuilder { +public class AzureServiceResponseBuilder extends ServiceResponseBuilder { /** * Create a ServiceResponseBuilder instance. * diff --git a/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/CloudException.java b/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/CloudException.java index 2e0a9db74783..00969b08e656 100644 --- a/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/CloudException.java +++ b/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/CloudException.java @@ -7,13 +7,13 @@ package com.microsoft.azure; -import com.microsoft.rest.RestException; +import com.microsoft.rest.AutoRestException; import retrofit2.Response; /** * Exception thrown for an invalid response with custom error information. */ -public class CloudException extends RestException { +public class CloudException extends AutoRestException { /** * Information about the associated HTTP response. */ diff --git a/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/PagedList.java b/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/PagedList.java index f501163ff03e..f7cc4549504e 100644 --- a/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/PagedList.java +++ b/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/PagedList.java @@ -7,10 +7,9 @@ package com.microsoft.azure; -import com.microsoft.rest.RestException; +import com.microsoft.rest.AutoRestException; import java.io.IOException; -import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.List; @@ -31,15 +30,6 @@ public abstract class PagedList implements List { private List items; /** Stores the link to get the next page of items. */ private String nextPageLink; - /** Stores the latest page fetched. */ - private Page currentPage; - - /** - * Creates an instance of Pagedlist. - */ - public PagedList() { - items = new ArrayList<>(); - } /** * Creates an instance of PagedList from a {@link Page} response. @@ -50,7 +40,6 @@ public PagedList(Page page) { this(); items.addAll(page.getItems()); nextPageLink = page.getNextPageLink(); - currentPage = page; } /** @@ -58,10 +47,10 @@ public PagedList(Page page) { * * @param nextPageLink the link to get the next page of items. * @return the {@link Page} object storing a page of items and a link to the next page. - * @throws RestException thrown if an error is raised from Azure. + * @throws AutoRestException thrown if an error is raised from Azure. * @throws IOException thrown if there's any failure in deserialization. */ - public abstract Page nextPage(String nextPageLink) throws RestException, IOException; + public abstract Page nextPage(String nextPageLink) throws AutoRestException, IOException; /** * If there are more pages available. @@ -81,12 +70,12 @@ public void loadNextPage() { Page nextPage = nextPage(this.nextPageLink); this.nextPageLink = nextPage.getNextPageLink(); this.items.addAll(nextPage.getItems()); - this.currentPage = nextPage; - } catch (RestException e) { + } catch (AutoRestException e) { throw new WebServiceException(e.toString(), e); } catch (IOException e) { throw new DataBindingException(e.getMessage(), e); } + } /** @@ -98,24 +87,6 @@ public void loadAll() { } } - /** - * Gets the latest page fetched. - * - * @return the latest page. - */ - public Page currentPage() { - return currentPage; - } - - /** - * Gets the next page's link. - * - * @return the next page link. - */ - public String nextPageLink() { - return nextPageLink; - } - /** * The implementation of {@link ListIterator} for PagedList. */ @@ -134,7 +105,7 @@ public ListItr(int index) { @Override public boolean hasNext() { - return itemsListItr.hasNext() || hasNextPage(); + return itemsListItr.hasNext() || nextPageLink != null; } @Override @@ -195,7 +166,7 @@ public int size() { @Override public boolean isEmpty() { - return items.isEmpty() && !hasNextPage(); + return items.isEmpty() && nextPageLink == null; } @Override diff --git a/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/Resource.java b/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/Resource.java index f108f9edbc93..732035538ef6 100644 --- a/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/Resource.java +++ b/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/Resource.java @@ -49,7 +49,7 @@ public class Resource { * * @return the id value */ - public String id() { + public String getId() { return this.id; } @@ -58,7 +58,7 @@ public String id() { * * @return the name value */ - public String name() { + public String getName() { return this.name; } @@ -67,7 +67,7 @@ public String name() { * * @return the type value */ - public String type() { + public String getType() { return this.type; } @@ -76,7 +76,7 @@ public String type() { * * @return the location value */ - public String location() { + public String getLocation() { return this.location; } diff --git a/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/SubResource.java b/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/SubResource.java index 4f89c0afb8be..36ca18ae3665 100644 --- a/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/SubResource.java +++ b/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/SubResource.java @@ -21,7 +21,7 @@ public class SubResource { * * @return the id value */ - public String id() { + public String getId() { return this.id; } diff --git a/runtimes/client-runtime/build.gradle b/runtimes/client-runtime/build.gradle index 2558fc5055f9..b734d33608ea 100644 --- a/runtimes/client-runtime/build.gradle +++ b/runtimes/client-runtime/build.gradle @@ -80,6 +80,16 @@ 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' } @@ -89,7 +99,7 @@ task sourcesJar(type: Jar, dependsOn:classes) { from sourceSets.main.allSource } -task javadocJar(type: Jar, dependsOn: [javadoc]) { +task javadocJar(type: Jar, dependsOn: [javadoc, versionInfo]) { classifier = 'javadoc' from javadoc.destinationDir } @@ -97,6 +107,7 @@ task javadocJar(type: Jar, dependsOn: [javadoc]) { artifacts { archives sourcesJar archives javadocJar + archives file: file("${projectDir}/.gitrevision") } test { diff --git a/runtimes/client-runtime/src/main/java/com/microsoft/rest/RestException.java b/runtimes/client-runtime/src/main/java/com/microsoft/rest/AutoRestException.java similarity index 79% rename from runtimes/client-runtime/src/main/java/com/microsoft/rest/RestException.java rename to runtimes/client-runtime/src/main/java/com/microsoft/rest/AutoRestException.java index cf5dff556aea..72db4288ab3a 100644 --- a/runtimes/client-runtime/src/main/java/com/microsoft/rest/RestException.java +++ b/runtimes/client-runtime/src/main/java/com/microsoft/rest/AutoRestException.java @@ -10,18 +10,18 @@ /** * Exception thrown for an invalid response with custom error information. */ -public abstract class RestException extends Exception { +public abstract class AutoRestException extends Exception { /** * Initializes a new instance of the AutoRestException class. */ - public RestException() { } + public AutoRestException() { } /** * Initializes a new instance of the AutoRestException class. * * @param message The exception message. */ - public RestException(String message) { + public AutoRestException(String message) { super(message); } @@ -30,7 +30,7 @@ public RestException(String message) { * * @param cause exception that caused this exception to occur */ - public RestException(Throwable cause) { + public AutoRestException(Throwable cause) { super(cause); } @@ -40,7 +40,7 @@ public RestException(Throwable cause) { * @param message the exception message * @param cause exception that caused this exception to occur */ - public RestException(String message, Throwable cause) { + public AutoRestException(String message, Throwable cause) { super(message, cause); } } diff --git a/runtimes/client-runtime/src/main/java/com/microsoft/rest/BaseUrlHandler.java b/runtimes/client-runtime/src/main/java/com/microsoft/rest/BaseUrlHandler.java deleted file mode 100644 index f04cbc17edd8..000000000000 --- a/runtimes/client-runtime/src/main/java/com/microsoft/rest/BaseUrlHandler.java +++ /dev/null @@ -1,83 +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.io.IOException; - -import okhttp3.HttpUrl; -import okhttp3.Interceptor; -import okhttp3.Request; -import okhttp3.Response; - -/** - * An instance of class handles dynamic base URLs in the HTTP pipeline. - */ -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) { - 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) - .build(); - } - return chain.proceed(request); - } -} diff --git a/runtimes/client-runtime/src/main/java/com/microsoft/rest/CustomHeadersInterceptor.java b/runtimes/client-runtime/src/main/java/com/microsoft/rest/CustomHeadersInterceptor.java deleted file mode 100644 index 00903b93a9e0..000000000000 --- a/runtimes/client-runtime/src/main/java/com/microsoft/rest/CustomHeadersInterceptor.java +++ /dev/null @@ -1,136 +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 okhttp3.Headers; -import okhttp3.Interceptor; -import okhttp3.Request; -import okhttp3.Response; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * An instance of this class enables adding custom headers in client requests - * when added to the {@link okhttp3.OkHttpClient} interceptors. - */ -public class CustomHeadersInterceptor implements Interceptor { - /** - * A mapping of custom headers. - */ - private Map> headers; - - /** - * Initialize an instance of {@link CustomHeadersInterceptor} class. - */ - public CustomHeadersInterceptor() { - headers = new HashMap>(); - } - - /** - * Initialize an instance of {@link CustomHeadersInterceptor} class. - * - * @param key the key for the header - * @param value the value of the header - */ - public CustomHeadersInterceptor(String key, String value) { - this(); - addHeader(key, value); - } - - /** - * Add a single header key-value pair. If one with the name already exists, - * it gets replaced. - * - * @param name the name of the header. - * @param value the value of the header. - * @return the interceptor instance itself. - */ - public CustomHeadersInterceptor replaceHeader(String name, String value) { - this.headers.put(name, new ArrayList()); - this.headers.get(name).add(value); - return this; - } - - /** - * Add a single header key-value pair. If one with the name already exists, - * both stay in the header map. - * - * @param name the name of the header. - * @param value the value of the header. - * @return the interceptor instance itself. - */ - public CustomHeadersInterceptor addHeader(String name, String value) { - if (!this.headers.containsKey(name)) { - this.headers.put(name, new ArrayList()); - } - this.headers.get(name).add(value); - return this; - } - - /** - * Add all headers in a {@link Headers} object. - * - * @param headers an OkHttp {@link Headers} object. - * @return the interceptor instance itself. - */ - public CustomHeadersInterceptor addHeaders(Headers headers) { - this.headers.putAll(headers.toMultimap()); - return this; - } - - /** - * Add all headers in a header map. - * - * @param headers a map of headers. - * @return the interceptor instance itself. - */ - public CustomHeadersInterceptor addHeaderMap(Map headers) { - for (Map.Entry header : headers.entrySet()) { - this.headers.put(header.getKey(), Collections.singletonList(header.getValue())); - } - return this; - } - - /** - * Add all headers in a header multimap. - * - * @param headers a multimap of headers. - * @return the interceptor instance itself. - */ - public CustomHeadersInterceptor addHeaderMultimap(Map> headers) { - this.headers.putAll(headers); - return this; - } - - /** - * Remove a header. - * - * @param name the name of the header to remove. - * @return the interceptor instance itself. - */ - public CustomHeadersInterceptor removeHeader(String name) { - this.headers.remove(name); - return this; - } - - @Override - public Response intercept(Chain chain) throws IOException { - Request.Builder builder = chain.request().newBuilder(); - for (Map.Entry> header : headers.entrySet()) { - for (String value : header.getValue()) { - builder = builder.header(header.getKey(), value); - } - } - return chain.proceed(builder.build()); - } -} diff --git a/runtimes/client-runtime/src/main/java/com/microsoft/rest/RestClient.java b/runtimes/client-runtime/src/main/java/com/microsoft/rest/RestClient.java deleted file mode 100644 index ffc52643040d..000000000000 --- a/runtimes/client-runtime/src/main/java/com/microsoft/rest/RestClient.java +++ /dev/null @@ -1,269 +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 com.microsoft.rest.credentials.ServiceClientCredentials; -import com.microsoft.rest.retry.RetryHandler; -import com.microsoft.rest.serializer.JacksonMapperAdapter; - -import java.net.CookieManager; -import java.net.CookiePolicy; - -import okhttp3.Interceptor; -import okhttp3.JavaNetCookieJar; -import okhttp3.OkHttpClient; -import okhttp3.logging.HttpLoggingInterceptor; -import retrofit2.Retrofit; - -/** - * An instance of this class stores the client information for making REST calls. - */ -public final class RestClient { - /** The {@link okhttp3.OkHttpClient} object. */ - private OkHttpClient httpClient; - /** The {@link retrofit2.Retrofit} object. */ - private Retrofit retrofit; - /** The credentials to authenticate. */ - private ServiceClientCredentials credentials; - /** The interceptor to handle custom headers. */ - private CustomHeadersInterceptor customHeadersInterceptor; - /** The interceptor to handle base URL. */ - private BaseUrlHandler baseUrlHandler; - /** The adapter to a Jackson {@link com.fasterxml.jackson.databind.ObjectMapper}. */ - private JacksonMapperAdapter mapperAdapter; - - private RestClient(OkHttpClient httpClient, - Retrofit retrofit, - ServiceClientCredentials credentials, - CustomHeadersInterceptor customHeadersInterceptor, - BaseUrlHandler baseUrlHandler, - JacksonMapperAdapter mapperAdapter) { - this.httpClient = httpClient; - this.retrofit = retrofit; - this.credentials = credentials; - this.customHeadersInterceptor = customHeadersInterceptor; - this.baseUrlHandler = baseUrlHandler; - this.mapperAdapter = mapperAdapter; - } - - /** - * Get the headers interceptor. - * - * @return the headers interceptor. - */ - public CustomHeadersInterceptor headers() { - return customHeadersInterceptor; - } - - /** - * Get the adapter to {@link com.fasterxml.jackson.databind.ObjectMapper}. - * - * @return the Jackson mapper adapter. - */ - public JacksonMapperAdapter mapperAdapter() { - return mapperAdapter; - } - - /** - * Get the http client. - * - * @return the {@link OkHttpClient} object. - */ - public OkHttpClient httpClient() { - return httpClient; - } - - /** - * Get the retrofit instance. - * - * @return the {@link Retrofit} object. - */ - 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. - * @see {@link RestClient#setBaseUrl(String...)} - */ - 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. - * - * @return the credentials. - */ - public ServiceClientCredentials credentials() { - return this.credentials; - } - - /** - * The builder class for building a REST client. - */ - public static class Builder { - /** The builder to build an {@link OkHttpClient}. */ - private OkHttpClient.Builder httpClientBuilder; - /** The builder to build a {@link Retrofit}. */ - private Retrofit.Builder retrofitBuilder; - /** The credentials to authenticate. */ - private ServiceClientCredentials credentials; - /** The interceptor to handle custom headers. */ - private CustomHeadersInterceptor customHeadersInterceptor; - /** The interceptor to handle base URL. */ - private BaseUrlHandler baseUrlHandler; - /** The adapter to a Jackson {@link com.fasterxml.jackson.databind.ObjectMapper}. */ - private JacksonMapperAdapter mapperAdapter; - - /** - * Creates an instance of the builder with a base URL to the service. - * - * @param baseUrl the dynamic base URL with varialbes wrapped in "{" and "}". - */ - public Builder(String baseUrl) { - this(baseUrl, new OkHttpClient.Builder(), new Retrofit.Builder()); - } - - /** - * Creates an instance of the builder with a base URL and 2 custom builders. - * - * @param baseUrl the dynamic base URL with varialbes wrapped in "{" and "}". - * @param httpClientBuilder the builder to build an {@link OkHttpClient}. - * @param retrofitBuilder the builder to build a {@link Retrofit}. - */ - public Builder(String baseUrl, OkHttpClient.Builder httpClientBuilder, Retrofit.Builder retrofitBuilder) { - if (baseUrl == null) { - throw new IllegalArgumentException("baseUrl == null"); - } - if (httpClientBuilder == null) { - throw new IllegalArgumentException("httpClientBuilder == null"); - } - if (retrofitBuilder == null) { - throw new IllegalArgumentException("retrofitBuilder == null"); - } - CookieManager cookieManager = new CookieManager(); - cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); - customHeadersInterceptor = new CustomHeadersInterceptor(); - baseUrlHandler = new BaseUrlHandler(baseUrl); - // Set up OkHttp client - this.httpClientBuilder = httpClientBuilder - .cookieJar(new JavaNetCookieJar(cookieManager)) - .addInterceptor(new RetryHandler()) - .addInterceptor(new UserAgentInterceptor()); - // Set up rest adapter - this.retrofitBuilder = retrofitBuilder.baseUrl(baseUrl); - } - - /** - * Sets the base URL. - * - * @param baseUrl the dynamic base URL. - * @return the builder itself for chaining. - */ - public Builder withBaseUrl(String baseUrl) { - this.retrofitBuilder.baseUrl(baseUrl); - this.baseUrlHandler = new BaseUrlHandler(baseUrl); - return this; - } - - /** - * Sets the user agent header. - * - * @param userAgent the user agent header. - * @return the builder itself for chaining. - */ - public Builder withUserAgent(String userAgent) { - this.httpClientBuilder.addInterceptor(new UserAgentInterceptor(userAgent)); - return this; - } - - /** - * Sets the mapper adapter. - * - * @param mapperAdapter an adapter to a Jackson mapper. - * @return the builder itself for chaining. - */ - public Builder withMapperAdapter(JacksonMapperAdapter mapperAdapter) { - if (mapperAdapter != null) { - this.mapperAdapter = mapperAdapter; - this.retrofitBuilder = retrofitBuilder.addConverterFactory(mapperAdapter.getConverterFactory()); - } - return this; - } - - /** - * Sets the credentials. - * - * @param credentials the credentials object. - * @return the builder itself for chaining. - */ - public Builder withCredentials(ServiceClientCredentials credentials) { - this.credentials = credentials; - if (credentials != null) { - credentials.applyCredentialsFilter(httpClientBuilder); - } - return this; - } - - /** - * Sets the log level. - * - * @param logLevel the {@link okhttp3.logging.HttpLoggingInterceptor.Level} enum. - * @return the builder itself for chaining. - */ - public Builder withLogLevel(HttpLoggingInterceptor.Level logLevel) { - this.httpClientBuilder.addInterceptor(new HttpLoggingInterceptor().setLevel(logLevel)); - return this; - } - - /** - * Add an interceptor the Http client pipeline. - * - * @param interceptor the interceptor to add. - * @return the builder itself for chaining. - */ - public Builder withInterceptor(Interceptor interceptor) { - this.httpClientBuilder.addInterceptor(interceptor); - return this; - } - - /** - * Build a RestClient with all the current configurations. - * - * @return a {@link RestClient}. - */ - public RestClient build() { - OkHttpClient httpClient = httpClientBuilder - .addInterceptor(baseUrlHandler) - .addInterceptor(customHeadersInterceptor) - .build(); - return new RestClient(httpClient, - retrofitBuilder.client(httpClient).build(), - credentials, - customHeadersInterceptor, - baseUrlHandler, - mapperAdapter); - } - } -} diff --git a/runtimes/client-runtime/src/main/java/com/microsoft/rest/ServiceClient.java b/runtimes/client-runtime/src/main/java/com/microsoft/rest/ServiceClient.java index ef2f8cae9cfe..a0655bc3ab79 100644 --- a/runtimes/client-runtime/src/main/java/com/microsoft/rest/ServiceClient.java +++ b/runtimes/client-runtime/src/main/java/com/microsoft/rest/ServiceClient.java @@ -7,8 +7,20 @@ package com.microsoft.rest; +import com.microsoft.rest.retry.RetryHandler; import com.microsoft.rest.serializer.JacksonMapperAdapter; +import java.net.CookieManager; +import java.net.CookiePolicy; +import java.util.List; + +import okhttp3.Interceptor; +import okhttp3.JavaNetCookieJar; +import okhttp3.OkHttpClient; +import okhttp3.logging.HttpLoggingInterceptor; +import okhttp3.logging.HttpLoggingInterceptor.Level; +import retrofit2.Retrofit; + /** * ServiceClient is the abstraction for accessing REST operations and their payload data types. */ @@ -16,33 +28,90 @@ public abstract class ServiceClient { /** * The builder for building the OkHttp client. */ - private RestClient restClient; + protected OkHttpClient.Builder clientBuilder; + + /** + * The builder for building Retrofit services. + */ + protected Retrofit.Builder retrofitBuilder; + + /** + * The adapter for {@link com.fasterxml.jackson.databind.ObjectMapper} for serialization + * and deserialization operations. + */ + protected JacksonMapperAdapter mapperAdapter; /** * Initializes a new instance of the ServiceClient class. */ - protected ServiceClient(String baseUrl) { - this(new RestClient.Builder(baseUrl) - .withMapperAdapter(new JacksonMapperAdapter()).build()); + protected ServiceClient() { + this(new OkHttpClient.Builder(), new Retrofit.Builder()); } /** * Initializes a new instance of the ServiceClient class. * - * @param restClient the builder to build up an REST client + * @param clientBuilder the builder to build up an OkHttp client + * @param retrofitBuilder the builder to build up a rest adapter */ - protected ServiceClient(RestClient restClient) { - if (restClient == null) { - throw new IllegalArgumentException("restClient == null"); + protected ServiceClient(OkHttpClient.Builder clientBuilder, Retrofit.Builder retrofitBuilder) { + if (clientBuilder == null) { + throw new IllegalArgumentException("clientBuilder == null"); + } + if (retrofitBuilder == null) { + throw new IllegalArgumentException("retrofitBuilder == null"); } - this.restClient = restClient; + + this.clientBuilder = clientBuilder; + this.retrofitBuilder = retrofitBuilder; } /** * Get the list of interceptors the OkHttp client will execute. * @return the list of interceptors */ - public RestClient restClient() { - return this.restClient; + public List getClientInterceptors() { + return this.clientBuilder.interceptors(); + } + + /** + * Sets the logging level for OkHttp client. + * + * @param logLevel the logging level enum + */ + public void setLogLevel(Level logLevel) { + HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(); + loggingInterceptor.setLevel(logLevel); + this.getClientInterceptors().add(loggingInterceptor); + } + + /** + * Gets the adapter for {@link com.fasterxml.jackson.databind.ObjectMapper} for serialization + * and deserialization operations.. + * + * @return the adapter. + */ + public JacksonMapperAdapter getMapperAdapter() { + return this.mapperAdapter; + } + + /** + * This method initializes the builders for Http client and Retrofit with common + * behaviors for all service clients. + */ + protected void initialize() { + // Add retry handler + CookieManager cookieManager = new CookieManager(); + cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); + + // Set up OkHttp client + this.clientBuilder = clientBuilder + .cookieJar(new JavaNetCookieJar(cookieManager)) + .addInterceptor(new RetryHandler()) + .addInterceptor(new UserAgentInterceptor()); + // Set up rest adapter + this.mapperAdapter = new JacksonMapperAdapter(); + this.retrofitBuilder = retrofitBuilder + .addConverterFactory(mapperAdapter.getConverterFactory()); } } diff --git a/runtimes/client-runtime/src/main/java/com/microsoft/rest/ServiceException.java b/runtimes/client-runtime/src/main/java/com/microsoft/rest/ServiceException.java index c041ae3bda3a..bf187c9d043a 100644 --- a/runtimes/client-runtime/src/main/java/com/microsoft/rest/ServiceException.java +++ b/runtimes/client-runtime/src/main/java/com/microsoft/rest/ServiceException.java @@ -12,7 +12,7 @@ /** * Exception thrown for an invalid response with custom error information. */ -public class ServiceException extends RestException { +public class ServiceException extends AutoRestException { /** * Information about the associated HTTP response. */ diff --git a/runtimes/client-runtime/src/main/java/com/microsoft/rest/ServiceResponseBuilder.java b/runtimes/client-runtime/src/main/java/com/microsoft/rest/ServiceResponseBuilder.java index b6e057a0e607..62e2d156ba61 100644 --- a/runtimes/client-runtime/src/main/java/com/microsoft/rest/ServiceResponseBuilder.java +++ b/runtimes/client-runtime/src/main/java/com/microsoft/rest/ServiceResponseBuilder.java @@ -9,6 +9,8 @@ import com.fasterxml.jackson.core.type.TypeReference; import com.microsoft.rest.serializer.JacksonMapperAdapter; +import okhttp3.ResponseBody; +import retrofit2.Response; import java.io.IOException; import java.io.InputStream; @@ -18,16 +20,13 @@ import java.util.HashMap; import java.util.Map; -import okhttp3.ResponseBody; -import retrofit2.Response; - /** * The builder for building a {@link ServiceResponse}. * * @param The return type the caller expects from the REST response. * @param the exception to throw in case of error. */ -public class ServiceResponseBuilder { +public class ServiceResponseBuilder { /** * A mapping of HTTP status codes and their corresponding return types. */ @@ -36,7 +35,7 @@ public class ServiceResponseBuilder { /** * The exception type to thrown in case of error. */ - protected Class exceptionType; + protected Class exceptionType; /** * The mapperAdapter used for deserializing the response. @@ -83,7 +82,7 @@ public ServiceResponseBuilder register(int statusCode, final Type type) { * @param type the type to deserialize. * @return the same builder instance. */ - public ServiceResponseBuilder registerError(final Class type) { + public ServiceResponseBuilder registerError(final Class type) { this.exceptionType = type; try { Field f = type.getDeclaredField("body"); diff --git a/runtimes/client-runtime/src/main/java/com/microsoft/rest/serializer/JacksonMapperAdapter.java b/runtimes/client-runtime/src/main/java/com/microsoft/rest/serializer/JacksonMapperAdapter.java index a59247207502..ae2af3c5f6ac 100644 --- a/runtimes/client-runtime/src/main/java/com/microsoft/rest/serializer/JacksonMapperAdapter.java +++ b/runtimes/client-runtime/src/main/java/com/microsoft/rest/serializer/JacksonMapperAdapter.java @@ -62,8 +62,7 @@ protected void initializeObjectMapper(ObjectMapper mapper) { mapper.setVisibility(mapper.getSerializationConfig().getDefaultVisibilityChecker() .withFieldVisibility(JsonAutoDetect.Visibility.ANY) .withSetterVisibility(JsonAutoDetect.Visibility.NONE) - .withGetterVisibility(JsonAutoDetect.Visibility.NONE) - .withIsGetterVisibility(JsonAutoDetect.Visibility.NONE)); + .withGetterVisibility(JsonAutoDetect.Visibility.NONE)); } /** diff --git a/runtimes/client-runtime/src/test/java/com/microsoft/rest/CredentialsTests.java b/runtimes/client-runtime/src/test/java/com/microsoft/rest/CredentialsTests.java index 19b7a8ca2a4f..296c7c910095 100644 --- a/runtimes/client-runtime/src/test/java/com/microsoft/rest/CredentialsTests.java +++ b/runtimes/client-runtime/src/test/java/com/microsoft/rest/CredentialsTests.java @@ -9,65 +9,50 @@ import com.microsoft.rest.credentials.BasicAuthenticationCredentials; import com.microsoft.rest.credentials.TokenCredentials; - +import okhttp3.Interceptor; +import okhttp3.Protocol; +import okhttp3.Response; import org.junit.Assert; import org.junit.Test; import java.io.IOException; -import okhttp3.Interceptor; -import okhttp3.OkHttpClient; -import okhttp3.Protocol; -import okhttp3.Request; -import okhttp3.Response; -import retrofit2.Retrofit; - public class CredentialsTests { @Test public void basicCredentialsTest() throws Exception { - OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder(); - Retrofit.Builder retrofitBuilder = new Retrofit.Builder(); + ServiceClient serviceClient = new ServiceClient() { }; BasicAuthenticationCredentials credentials = new BasicAuthenticationCredentials("user", "pass"); - RestClient.Builder restBuilder = new RestClient.Builder("http://localhost", clientBuilder, retrofitBuilder) - .withCredentials(credentials) - .withInterceptor(new Interceptor() { + credentials.applyCredentialsFilter(serviceClient.clientBuilder); + serviceClient.getClientInterceptors().add(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { String header = chain.request().header("Authorization"); Assert.assertEquals("Basic dXNlcjpwYXNz", header); - return new Response.Builder() - .request(chain.request()) - .code(200) - .protocol(Protocol.HTTP_1_1) - .build(); + return new Response.Builder() + .request(chain.request()) + .code(200) + .protocol(Protocol.HTTP_1_1) + .build(); } }); - ServiceClient serviceClient = new ServiceClient(restBuilder.build()) { }; - Response response = serviceClient.restClient().httpClient().newCall(new Request.Builder().url("http://localhost").build()).execute(); - Assert.assertEquals(200, response.code()); } @Test public void tokenCredentialsTest() throws Exception { - OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder(); - Retrofit.Builder retrofitBuilder = new Retrofit.Builder(); + ServiceClient serviceClient = new ServiceClient() { }; TokenCredentials credentials = new TokenCredentials(null, "this_is_a_token"); - RestClient.Builder restBuilder = new RestClient.Builder("http://localhost", clientBuilder, retrofitBuilder) - .withCredentials(credentials) - .withInterceptor(new Interceptor() { - @Override - public Response intercept(Chain chain) throws IOException { - String header = chain.request().header("Authorization"); - Assert.assertEquals("Bearer this_is_a_token", header); - return new Response.Builder() - .request(chain.request()) - .code(200) - .protocol(Protocol.HTTP_1_1) - .build(); - } - }); - ServiceClient serviceClient = new ServiceClient(restBuilder.build()) { }; - Response response = serviceClient.restClient().httpClient().newCall(new Request.Builder().url("http://localhost").build()).execute(); - Assert.assertEquals(200, response.code()); + credentials.applyCredentialsFilter(serviceClient.clientBuilder); + serviceClient.getClientInterceptors().add(new Interceptor() { + @Override + public Response intercept(Chain chain) throws IOException { + String header = chain.request().header("Authorization"); + Assert.assertEquals("Bearer this_is_a_token", header); + return new Response.Builder() + .request(chain.request()) + .code(200) + .protocol(Protocol.HTTP_1_1) + .build(); + } + }); } } diff --git a/runtimes/client-runtime/src/test/java/com/microsoft/rest/RetryHandlerTests.java b/runtimes/client-runtime/src/test/java/com/microsoft/rest/RetryHandlerTests.java index 050e7374aba1..4291450c27d1 100644 --- a/runtimes/client-runtime/src/test/java/com/microsoft/rest/RetryHandlerTests.java +++ b/runtimes/client-runtime/src/test/java/com/microsoft/rest/RetryHandlerTests.java @@ -10,12 +10,9 @@ import com.microsoft.rest.retry.RetryHandler; import okhttp3.Interceptor; -import okhttp3.OkHttpClient; import okhttp3.Protocol; import okhttp3.Request; import okhttp3.Response; -import retrofit2.Retrofit; - import org.junit.Assert; import org.junit.Test; @@ -24,10 +21,9 @@ public class RetryHandlerTests { @Test public void exponentialRetryEndOn501() throws Exception { - OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder(); - Retrofit.Builder retrofitBuilder = new Retrofit.Builder(); - clientBuilder.addInterceptor(new RetryHandler()); - clientBuilder.addInterceptor(new Interceptor() { + ServiceClient serviceClient = new ServiceClient() { }; + serviceClient.getClientInterceptors().add(new RetryHandler()); + serviceClient.getClientInterceptors().add(new Interceptor() { // Send 408, 500, 502, all retried, with a 501 ending private int[] codes = new int[]{408, 500, 502, 501}; private int count = 0; @@ -41,22 +37,18 @@ public Response intercept(Chain chain) throws IOException { .build(); } }); - RestClient.Builder restBuilder = new RestClient.Builder("http://localhost", clientBuilder, retrofitBuilder); - ServiceClient serviceClient = new ServiceClient(restBuilder.build()) { }; - Response response = serviceClient.restClient().httpClient().newCall( + Response response = serviceClient.clientBuilder.build().newCall( new Request.Builder().url("http://localhost").get().build()).execute(); Assert.assertEquals(501, response.code()); } @Test public void exponentialRetryMax() throws Exception { - OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder(); - Retrofit.Builder retrofitBuilder = new Retrofit.Builder(); - clientBuilder.addInterceptor(new RetryHandler()); - clientBuilder.addInterceptor(new Interceptor() { + ServiceClient serviceClient = new ServiceClient() { }; + serviceClient.getClientInterceptors().add(new RetryHandler()); + serviceClient.getClientInterceptors().add(new Interceptor() { // Send 500 until max retry is hit private int count = 0; - @Override public Response intercept(Chain chain) throws IOException { Assert.assertTrue(count++ < 5); @@ -67,9 +59,7 @@ public Response intercept(Chain chain) throws IOException { .build(); } }); - RestClient.Builder restBuilder = new RestClient.Builder("http://localhost", clientBuilder, retrofitBuilder); - ServiceClient serviceClient = new ServiceClient(restBuilder.build()) { }; - Response response = serviceClient.restClient().httpClient().newCall( + Response response = serviceClient.clientBuilder.build().newCall( new Request.Builder().url("http://localhost").get().build()).execute(); Assert.assertEquals(500, response.code()); } diff --git a/runtimes/client-runtime/src/test/java/com/microsoft/rest/ServiceClientTests.java b/runtimes/client-runtime/src/test/java/com/microsoft/rest/ServiceClientTests.java index 5497d607549d..f30127974005 100644 --- a/runtimes/client-runtime/src/test/java/com/microsoft/rest/ServiceClientTests.java +++ b/runtimes/client-runtime/src/test/java/com/microsoft/rest/ServiceClientTests.java @@ -7,41 +7,27 @@ package com.microsoft.rest; +import okhttp3.Interceptor; +import okhttp3.Response; import org.junit.Assert; import org.junit.Test; import java.io.IOException; -import okhttp3.Interceptor; -import okhttp3.OkHttpClient; -import okhttp3.Protocol; -import okhttp3.Request; -import okhttp3.Response; -import retrofit2.Retrofit; - public class ServiceClientTests { @Test public void filterTests() throws Exception { - OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder(); - Retrofit.Builder retrofitBuilder = new Retrofit.Builder(); - clientBuilder.interceptors().add(0, new FirstFilter()); - clientBuilder.interceptors().add(1, new SecondFilter()); - clientBuilder.interceptors().add(new Interceptor() { + ServiceClient serviceClient = new ServiceClient() { }; + serviceClient.getClientInterceptors().add(0, new FirstFilter()); + serviceClient.getClientInterceptors().add(1, new SecondFilter()); + serviceClient.getClientInterceptors().add(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Assert.assertEquals("1", chain.request().header("filter1")); Assert.assertEquals("2", chain.request().header("filter2")); - return new Response.Builder() - .request(chain.request()) - .code(200) - .protocol(Protocol.HTTP_1_1) - .build(); + return chain.proceed(chain.request()); } }); - RestClient.Builder restBuilder = new RestClient.Builder("http://localhost", clientBuilder, retrofitBuilder); - ServiceClient serviceClient = new ServiceClient(restBuilder.build()) { }; - Response response = serviceClient.restClient().httpClient().newCall(new Request.Builder().url("http://localhost").build()).execute(); - Assert.assertEquals(200, response.code()); } public class FirstFilter implements Interceptor { diff --git a/runtimes/client-runtime/src/test/java/com/microsoft/rest/UserAgentTests.java b/runtimes/client-runtime/src/test/java/com/microsoft/rest/UserAgentTests.java index e6b938f93368..ce06a25b8869 100644 --- a/runtimes/client-runtime/src/test/java/com/microsoft/rest/UserAgentTests.java +++ b/runtimes/client-runtime/src/test/java/com/microsoft/rest/UserAgentTests.java @@ -8,11 +8,8 @@ package com.microsoft.rest; import okhttp3.Interceptor; -import okhttp3.OkHttpClient; import okhttp3.Protocol; import okhttp3.Response; -import retrofit2.Retrofit; - import org.junit.Assert; import org.junit.Test; @@ -21,42 +18,36 @@ public class UserAgentTests { @Test public void defaultUserAgentTests() throws Exception { - OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder(); - Retrofit.Builder retrofitBuilder = new Retrofit.Builder(); - clientBuilder.addInterceptor(new Interceptor() { + ServiceClient serviceClient = new ServiceClient() { }; + serviceClient.getClientInterceptors().add(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { String header = chain.request().header("User-Agent"); Assert.assertEquals("AutoRest-Java", header); return new Response.Builder() - .request(chain.request()) - .code(200) - .protocol(Protocol.HTTP_1_1) - .build(); + .request(chain.request()) + .code(200) + .protocol(Protocol.HTTP_1_1) + .build(); } }); - RestClient.Builder restBuilder = new RestClient.Builder("http://localhost", clientBuilder, retrofitBuilder); - ServiceClient serviceClient = new ServiceClient(restBuilder.build()) { }; } @Test public void customUserAgentTests() throws Exception { - OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder(); - Retrofit.Builder retrofitBuilder = new Retrofit.Builder(); - clientBuilder.addInterceptor(new UserAgentInterceptor("Awesome")); - clientBuilder.addInterceptor(new Interceptor() { + ServiceClient serviceClient = new ServiceClient() { }; + serviceClient.getClientInterceptors().add(new UserAgentInterceptor("Awesome")); + serviceClient.getClientInterceptors().add(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { String header = chain.request().header("User-Agent"); Assert.assertEquals("Awesome", header); return new Response.Builder() - .request(chain.request()) - .code(200) - .protocol(Protocol.HTTP_1_1) - .build(); + .request(chain.request()) + .code(200) + .protocol(Protocol.HTTP_1_1) + .build(); } }); - RestClient.Builder restBuilder = new RestClient.Builder("http://localhost", clientBuilder, retrofitBuilder); - ServiceClient serviceClient = new ServiceClient(restBuilder.build()) { }; } } diff --git a/runtimes/pom.xml b/runtimes/pom.xml index 98b38905b457..ae5dc28eaff4 100644 --- a/runtimes/pom.xml +++ b/runtimes/pom.xml @@ -244,6 +244,29 @@ maven-release-plugin 2.5.2 + + + org.codehaus.mojo + build-helper-maven-plugin + 1.10 + + + attach-artifacts + package + + attach-artifact + + + + + target/.gitrevision + gitrevision + + + + + +