Java tutorial
/** * * Copyright (c) Microsoft and contributors. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * * See the License for the specific language governing permissions and * limitations under the License. * */ // Warning: This code was generated by a tool. // // Changes to this file may cause incorrect behavior and will be lost if the // code is regenerated. package com.microsoft.windowsazure.management.servicebus; import com.microsoft.windowsazure.core.OperationStatus; import com.microsoft.windowsazure.core.OperationStatusResponse; import com.microsoft.windowsazure.core.ServiceClient; import com.microsoft.windowsazure.core.utils.BOMInputStream; import com.microsoft.windowsazure.core.utils.XmlUtility; import com.microsoft.windowsazure.credentials.SubscriptionCloudCredentials; import com.microsoft.windowsazure.exception.ServiceException; import com.microsoft.windowsazure.management.configuration.ManagementConfiguration; import com.microsoft.windowsazure.management.servicebus.models.ServiceBusLocation; import com.microsoft.windowsazure.management.servicebus.models.ServiceBusRegionsResponse; import com.microsoft.windowsazure.tracing.CloudTracing; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.HttpClientBuilder; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.xml.sax.SAXException; import javax.inject.Inject; import javax.inject.Named; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import java.io.IOException; import java.io.InputStream; import java.net.URI; import java.net.URISyntaxException; import java.net.URLEncoder; import java.util.HashMap; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; /** * The Service Bus Management API is a REST API for managing Service Bus queues, * topics, rules and subscriptions. (see * http://msdn.microsoft.com/en-us/library/windowsazure/hh780776.aspx for more * information) */ public class ServiceBusManagementClientImpl extends ServiceClient<ServiceBusManagementClient> implements ServiceBusManagementClient { private String apiVersion; /** * Gets the API version. * @return The ApiVersion value. */ public String getApiVersion() { return this.apiVersion; } private URI baseUri; /** * Gets the URI used as the base for all cloud service requests. * @return The BaseUri value. */ public URI getBaseUri() { return this.baseUri; } private SubscriptionCloudCredentials credentials; /** * Gets subscription credentials which uniquely identify Microsoft Azure * subscription. The subscription ID forms part of the URI for every * service call. * @return The Credentials value. */ public SubscriptionCloudCredentials getCredentials() { return this.credentials; } private int longRunningOperationInitialTimeout; /** * Gets or sets the initial timeout for Long Running Operations. * @return The LongRunningOperationInitialTimeout value. */ public int getLongRunningOperationInitialTimeout() { return this.longRunningOperationInitialTimeout; } /** * Gets or sets the initial timeout for Long Running Operations. * @param longRunningOperationInitialTimeoutValue The * LongRunningOperationInitialTimeout value. */ public void setLongRunningOperationInitialTimeout(final int longRunningOperationInitialTimeoutValue) { this.longRunningOperationInitialTimeout = longRunningOperationInitialTimeoutValue; } private int longRunningOperationRetryTimeout; /** * Gets or sets the retry timeout for Long Running Operations. * @return The LongRunningOperationRetryTimeout value. */ public int getLongRunningOperationRetryTimeout() { return this.longRunningOperationRetryTimeout; } /** * Gets or sets the retry timeout for Long Running Operations. * @param longRunningOperationRetryTimeoutValue The * LongRunningOperationRetryTimeout value. */ public void setLongRunningOperationRetryTimeout(final int longRunningOperationRetryTimeoutValue) { this.longRunningOperationRetryTimeout = longRunningOperationRetryTimeoutValue; } private NamespaceOperations namespaces; /** * The Service Bus Management API includes operations for managing Service * Bus namespaces. * @return The NamespacesOperations value. */ public NamespaceOperations getNamespacesOperations() { return this.namespaces; } private NotificationHubOperations notificationHubs; /** * The Service Bus Management API includes operations for managing Service * Bus notification hubs. * @return The NotificationHubsOperations value. */ public NotificationHubOperations getNotificationHubsOperations() { return this.notificationHubs; } private QueueOperations queues; /** * The Service Bus Management API includes operations for managing Service * Bus queues. * @return The QueuesOperations value. */ public QueueOperations getQueuesOperations() { return this.queues; } private RelayOperations relays; /** * The Service Bus Management API includes operations for managing Service * Bus relays. * @return The RelaysOperations value. */ public RelayOperations getRelaysOperations() { return this.relays; } private TopicOperations topics; /** * The Service Bus Management API includes operations for managing Service * Bus topics for a namespace. * @return The TopicsOperations value. */ public TopicOperations getTopicsOperations() { return this.topics; } /** * Initializes a new instance of the ServiceBusManagementClientImpl class. * * @param httpBuilder The HTTP client builder. * @param executorService The executor service. */ public ServiceBusManagementClientImpl(HttpClientBuilder httpBuilder, ExecutorService executorService) { super(httpBuilder, executorService); this.namespaces = new NamespaceOperationsImpl(this); this.notificationHubs = new NotificationHubOperationsImpl(this); this.queues = new QueueOperationsImpl(this); this.relays = new RelayOperationsImpl(this); this.topics = new TopicOperationsImpl(this); this.apiVersion = "2013-08-01"; this.longRunningOperationInitialTimeout = -1; this.longRunningOperationRetryTimeout = -1; } /** * Initializes a new instance of the ServiceBusManagementClientImpl class. * * @param httpBuilder The HTTP client builder. * @param executorService The executor service. * @param credentials Required. Gets subscription credentials which uniquely * identify Microsoft Azure subscription. The subscription ID forms part of * the URI for every service call. * @param baseUri Optional. Gets the URI used as the base for all cloud * service requests. */ @Inject public ServiceBusManagementClientImpl(HttpClientBuilder httpBuilder, ExecutorService executorService, @Named(ManagementConfiguration.SUBSCRIPTION_CLOUD_CREDENTIALS) SubscriptionCloudCredentials credentials, @Named(ManagementConfiguration.URI) URI baseUri) { this(httpBuilder, executorService); if (credentials == null) { throw new NullPointerException("credentials"); } else { this.credentials = credentials; } if (baseUri == null) { try { this.baseUri = new URI("https://management.core.windows.net"); } catch (URISyntaxException ex) { } } else { this.baseUri = baseUri; } } /** * Initializes a new instance of the ServiceBusManagementClientImpl class. * * @param httpBuilder The HTTP client builder. * @param executorService The executor service. * @param credentials Required. Gets subscription credentials which uniquely * identify Microsoft Azure subscription. The subscription ID forms part of * the URI for every service call. * @throws URISyntaxException Thrown if there was an error parsing a URI in * the response. */ public ServiceBusManagementClientImpl(HttpClientBuilder httpBuilder, ExecutorService executorService, SubscriptionCloudCredentials credentials) throws URISyntaxException { this(httpBuilder, executorService); if (credentials == null) { throw new NullPointerException("credentials"); } this.credentials = credentials; this.baseUri = new URI("https://management.core.windows.net"); } /** * Initializes a new instance of the ServiceBusManagementClientImpl class. * * @param httpBuilder The HTTP client builder. * @param executorService The executor service. * @param credentials Required. Gets subscription credentials which uniquely * identify Microsoft Azure subscription. The subscription ID forms part of * the URI for every service call. * @param baseUri Optional. Gets the URI used as the base for all cloud * service requests. * @param apiVersion Optional. Gets the API version. * @param longRunningOperationInitialTimeout Required. Gets or sets the * initial timeout for Long Running Operations. * @param longRunningOperationRetryTimeout Required. Gets or sets the retry * timeout for Long Running Operations. */ public ServiceBusManagementClientImpl(HttpClientBuilder httpBuilder, ExecutorService executorService, SubscriptionCloudCredentials credentials, URI baseUri, String apiVersion, int longRunningOperationInitialTimeout, int longRunningOperationRetryTimeout) { this(httpBuilder, executorService); this.credentials = credentials; this.baseUri = baseUri; this.apiVersion = apiVersion; this.longRunningOperationInitialTimeout = longRunningOperationInitialTimeout; this.longRunningOperationRetryTimeout = longRunningOperationRetryTimeout; } /** * Initializes a new instance of the ServiceBusManagementClientImpl class. * * @param httpBuilder The HTTP client builder. * @param executorService The executor service. */ protected ServiceBusManagementClientImpl newInstance(HttpClientBuilder httpBuilder, ExecutorService executorService) { return new ServiceBusManagementClientImpl(httpBuilder, executorService, this.getCredentials(), this.getBaseUri(), this.getApiVersion(), this.getLongRunningOperationInitialTimeout(), this.getLongRunningOperationRetryTimeout()); } /** * The Get Operation Status operation returns the status of thespecified * operation. After calling an asynchronous operation, you can call Get * Operation Status to determine whether the operation has succeeded, * failed, or is still in progress. (see * http://msdn.microsoft.com/en-us/library/windowsazure/ee460783.aspx for * more information) * * @param requestId Required. The request ID for the request you wish to * track. The request ID is returned in the x-ms-request-id response header * for every request. * @return The response body contains the status of the specified * asynchronous operation, indicating whether it has succeeded, is * inprogress, or has failed. Note that this status is distinct from the * HTTP status code returned for the Get Operation Status operation itself. * If the asynchronous operation succeeded, the response body includes the * HTTP status code for the successful request. If the asynchronous * operation failed, the response body includes the HTTP status code for * the failed request, and also includes error information regarding the * failure. */ @Override public Future<OperationStatusResponse> getOperationStatusAsync(final String requestId) { return this.getExecutorService().submit(new Callable<OperationStatusResponse>() { @Override public OperationStatusResponse call() throws Exception { return getOperationStatus(requestId); } }); } /** * The Get Operation Status operation returns the status of thespecified * operation. After calling an asynchronous operation, you can call Get * Operation Status to determine whether the operation has succeeded, * failed, or is still in progress. (see * http://msdn.microsoft.com/en-us/library/windowsazure/ee460783.aspx for * more information) * * @param requestId Required. The request ID for the request you wish to * track. The request ID is returned in the x-ms-request-id response header * for every request. * @throws IOException Signals that an I/O exception of some sort has * occurred. This class is the general class of exceptions produced by * failed or interrupted I/O operations. * @throws ServiceException Thrown if an unexpected response is found. * @throws ParserConfigurationException Thrown if there was a serious * configuration error with the document parser. * @throws SAXException Thrown if there was an error parsing the XML * response. * @return The response body contains the status of the specified * asynchronous operation, indicating whether it has succeeded, is * inprogress, or has failed. Note that this status is distinct from the * HTTP status code returned for the Get Operation Status operation itself. * If the asynchronous operation succeeded, the response body includes the * HTTP status code for the successful request. If the asynchronous * operation failed, the response body includes the HTTP status code for * the failed request, and also includes error information regarding the * failure. */ @Override public OperationStatusResponse getOperationStatus(String requestId) throws IOException, ServiceException, ParserConfigurationException, SAXException { // Validate if (requestId == null) { throw new NullPointerException("requestId"); } // Tracing boolean shouldTrace = CloudTracing.getIsEnabled(); String invocationId = null; if (shouldTrace) { invocationId = Long.toString(CloudTracing.getNextInvocationId()); HashMap<String, Object> tracingParameters = new HashMap<String, Object>(); tracingParameters.put("requestId", requestId); CloudTracing.enter(invocationId, this, "getOperationStatusAsync", tracingParameters); } // Construct URL String url = ""; url = url + "/"; if (this.getCredentials().getSubscriptionId() != null) { url = url + URLEncoder.encode(this.getCredentials().getSubscriptionId(), "UTF-8"); } url = url + "/operations/"; url = url + URLEncoder.encode(requestId, "UTF-8"); String baseUrl = this.getBaseUri().toString(); // Trim '/' character from the end of baseUrl and beginning of url. if (baseUrl.charAt(baseUrl.length() - 1) == '/') { baseUrl = baseUrl.substring(0, (baseUrl.length() - 1) + 0); } if (url.charAt(0) == '/') { url = url.substring(1); } url = baseUrl + "/" + url; url = url.replace(" ", "%20"); // Create HTTP transport objects HttpGet httpRequest = new HttpGet(url); // Set Headers httpRequest.setHeader("x-ms-version", "2013-08-01"); // Send Request HttpResponse httpResponse = null; try { if (shouldTrace) { CloudTracing.sendRequest(invocationId, httpRequest); } httpResponse = this.getHttpClient().execute(httpRequest); if (shouldTrace) { CloudTracing.receiveResponse(invocationId, httpResponse); } int statusCode = httpResponse.getStatusLine().getStatusCode(); if (statusCode != HttpStatus.SC_OK) { ServiceException ex = ServiceException.createFromXml(httpRequest, null, httpResponse, httpResponse.getEntity()); if (shouldTrace) { CloudTracing.error(invocationId, ex); } throw ex; } // Create Result OperationStatusResponse result = null; // Deserialize Response if (statusCode == HttpStatus.SC_OK) { InputStream responseContent = httpResponse.getEntity().getContent(); result = new OperationStatusResponse(); DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(true); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document responseDoc = documentBuilder.parse(new BOMInputStream(responseContent)); Element operationElement = XmlUtility.getElementByTagNameNS(responseDoc, "http://schemas.microsoft.com/windowsazure", "Operation"); if (operationElement != null) { Element idElement = XmlUtility.getElementByTagNameNS(operationElement, "http://schemas.microsoft.com/windowsazure", "ID"); if (idElement != null) { String idInstance; idInstance = idElement.getTextContent(); result.setId(idInstance); } Element statusElement = XmlUtility.getElementByTagNameNS(operationElement, "http://schemas.microsoft.com/windowsazure", "Status"); if (statusElement != null && statusElement.getTextContent() != null && !statusElement.getTextContent().isEmpty()) { OperationStatus statusInstance; statusInstance = OperationStatus.valueOf(statusElement.getTextContent()); result.setStatus(statusInstance); } Element httpStatusCodeElement = XmlUtility.getElementByTagNameNS(operationElement, "http://schemas.microsoft.com/windowsazure", "HttpStatusCode"); if (httpStatusCodeElement != null && httpStatusCodeElement.getTextContent() != null && !httpStatusCodeElement.getTextContent().isEmpty()) { Integer httpStatusCodeInstance; httpStatusCodeInstance = Integer.valueOf(httpStatusCodeElement.getTextContent()); result.setHttpStatusCode(httpStatusCodeInstance); } Element errorElement = XmlUtility.getElementByTagNameNS(operationElement, "http://schemas.microsoft.com/windowsazure", "Error"); if (errorElement != null) { OperationStatusResponse.ErrorDetails errorInstance = new OperationStatusResponse.ErrorDetails(); result.setError(errorInstance); Element codeElement = XmlUtility.getElementByTagNameNS(errorElement, "http://schemas.microsoft.com/windowsazure", "Code"); if (codeElement != null) { String codeInstance; codeInstance = codeElement.getTextContent(); errorInstance.setCode(codeInstance); } Element messageElement = XmlUtility.getElementByTagNameNS(errorElement, "http://schemas.microsoft.com/windowsazure", "Message"); if (messageElement != null) { String messageInstance; messageInstance = messageElement.getTextContent(); errorInstance.setMessage(messageInstance); } } } } result.setStatusCode(statusCode); if (httpResponse.getHeaders("x-ms-request-id").length > 0) { result.setRequestId(httpResponse.getFirstHeader("x-ms-request-id").getValue()); } if (shouldTrace) { CloudTracing.exit(invocationId, result); } return result; } finally { if (httpResponse != null && httpResponse.getEntity() != null) { httpResponse.getEntity().getContent().close(); } } } /** * Retrieves the list of regions that support the creation and management of * Service Bus service namespaces. (see * http://msdn.microsoft.com/en-us/library/windowsazure/jj860465.aspx for * more information) * * @return A response to a request for a list of regions. */ @Override public Future<ServiceBusRegionsResponse> getServiceBusRegionsAsync() { return this.getExecutorService().submit(new Callable<ServiceBusRegionsResponse>() { @Override public ServiceBusRegionsResponse call() throws Exception { return getServiceBusRegions(); } }); } /** * Retrieves the list of regions that support the creation and management of * Service Bus service namespaces. (see * http://msdn.microsoft.com/en-us/library/windowsazure/jj860465.aspx for * more information) * * @throws IOException Signals that an I/O exception of some sort has * occurred. This class is the general class of exceptions produced by * failed or interrupted I/O operations. * @throws ServiceException Thrown if an unexpected response is found. * @throws ParserConfigurationException Thrown if there was a serious * configuration error with the document parser. * @throws SAXException Thrown if there was an error parsing the XML * response. * @return A response to a request for a list of regions. */ @Override public ServiceBusRegionsResponse getServiceBusRegions() throws IOException, ServiceException, ParserConfigurationException, SAXException { // Validate // Tracing boolean shouldTrace = CloudTracing.getIsEnabled(); String invocationId = null; if (shouldTrace) { invocationId = Long.toString(CloudTracing.getNextInvocationId()); HashMap<String, Object> tracingParameters = new HashMap<String, Object>(); CloudTracing.enter(invocationId, this, "getServiceBusRegionsAsync", tracingParameters); } // Construct URL String url = ""; url = url + "/"; if (this.getCredentials().getSubscriptionId() != null) { url = url + URLEncoder.encode(this.getCredentials().getSubscriptionId(), "UTF-8"); } url = url + "/services/servicebus/regions"; String baseUrl = this.getBaseUri().toString(); // Trim '/' character from the end of baseUrl and beginning of url. if (baseUrl.charAt(baseUrl.length() - 1) == '/') { baseUrl = baseUrl.substring(0, (baseUrl.length() - 1) + 0); } if (url.charAt(0) == '/') { url = url.substring(1); } url = baseUrl + "/" + url; url = url.replace(" ", "%20"); // Create HTTP transport objects HttpGet httpRequest = new HttpGet(url); // Set Headers httpRequest.setHeader("Accept", "application/atom+xml"); httpRequest.setHeader("Content-Type", "application/atom+xml"); httpRequest.setHeader("x-ms-version", "2013-08-01"); // Send Request HttpResponse httpResponse = null; try { if (shouldTrace) { CloudTracing.sendRequest(invocationId, httpRequest); } httpResponse = this.getHttpClient().execute(httpRequest); if (shouldTrace) { CloudTracing.receiveResponse(invocationId, httpResponse); } int statusCode = httpResponse.getStatusLine().getStatusCode(); if (statusCode != HttpStatus.SC_OK) { ServiceException ex = ServiceException.createFromXml(httpRequest, null, httpResponse, httpResponse.getEntity()); if (shouldTrace) { CloudTracing.error(invocationId, ex); } throw ex; } // Create Result ServiceBusRegionsResponse result = null; // Deserialize Response if (statusCode == HttpStatus.SC_OK) { InputStream responseContent = httpResponse.getEntity().getContent(); result = new ServiceBusRegionsResponse(); DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(true); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document responseDoc = documentBuilder.parse(new BOMInputStream(responseContent)); Element feedElement = XmlUtility.getElementByTagNameNS(responseDoc, "http://www.w3.org/2005/Atom", "feed"); if (feedElement != null) { if (feedElement != null) { for (int i1 = 0; i1 < com.microsoft.windowsazure.core.utils.XmlUtility .getElementsByTagNameNS(feedElement, "http://www.w3.org/2005/Atom", "entry") .size(); i1 = i1 + 1) { org.w3c.dom.Element entriesElement = ((org.w3c.dom.Element) com.microsoft.windowsazure.core.utils.XmlUtility .getElementsByTagNameNS(feedElement, "http://www.w3.org/2005/Atom", "entry") .get(i1)); ServiceBusLocation entryInstance = new ServiceBusLocation(); result.getRegions().add(entryInstance); Element contentElement = XmlUtility.getElementByTagNameNS(entriesElement, "http://www.w3.org/2005/Atom", "content"); if (contentElement != null) { Element regionCodeDescriptionElement = XmlUtility.getElementByTagNameNS( contentElement, "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect", "RegionCodeDescription"); if (regionCodeDescriptionElement != null) { Element codeElement = XmlUtility.getElementByTagNameNS( regionCodeDescriptionElement, "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect", "Code"); if (codeElement != null) { String codeInstance; codeInstance = codeElement.getTextContent(); entryInstance.setCode(codeInstance); } Element fullNameElement = XmlUtility.getElementByTagNameNS( regionCodeDescriptionElement, "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect", "FullName"); if (fullNameElement != null) { String fullNameInstance; fullNameInstance = fullNameElement.getTextContent(); entryInstance.setFullName(fullNameInstance); } } } } } } } result.setStatusCode(statusCode); if (httpResponse.getHeaders("x-ms-request-id").length > 0) { result.setRequestId(httpResponse.getFirstHeader("x-ms-request-id").getValue()); } if (shouldTrace) { CloudTracing.exit(invocationId, result); } return result; } finally { if (httpResponse != null && httpResponse.getEntity() != null) { httpResponse.getEntity().getContent().close(); } } } }