Example usage for org.w3c.dom Document createElementNS

List of usage examples for org.w3c.dom Document createElementNS

Introduction

In this page you can find the example usage for org.w3c.dom Document createElementNS.

Prototype

public Element createElementNS(String namespaceURI, String qualifiedName) throws DOMException;

Source Link

Document

Creates an element of the given qualified name and namespace URI.

Usage

From source file:com.microsoft.windowsazure.management.network.GatewayOperationsImpl.java

/**
* The Create Local network Gateway operation creates a new local network
* gateway./*from   w  w  w .  j  a v a  2  s.  co  m*/
*
* @param parameters Required. Parameters supplied to Create Local Network
* Gateway operation.
* @throws ParserConfigurationException Thrown if there was an error
* configuring the parser for the response body.
* @throws SAXException Thrown if there was an error parsing the response
* body.
* @throws TransformerException Thrown if there was an error creating the
* DOM transformer.
* @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.
* @return The Create Local Network Gateway operation response.
*/
@Override
public LocalNetworkGatewayCreateResponse createLocalNetworkGateway(
        LocalNetworkGatewayCreateParameters parameters)
        throws ParserConfigurationException, SAXException, TransformerException, IOException, ServiceException {
    // Validate
    if (parameters == null) {
        throw new NullPointerException("parameters");
    }

    // 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("parameters", parameters);
        CloudTracing.enter(invocationId, this, "createLocalNetworkGatewayAsync", tracingParameters);
    }

    // Construct URL
    String url = "";
    url = url + "/";
    if (this.getClient().getCredentials().getSubscriptionId() != null) {
        url = url + URLEncoder.encode(this.getClient().getCredentials().getSubscriptionId(), "UTF-8");
    }
    url = url + "/services/networking/localnetworkgateways";
    ArrayList<String> queryParameters = new ArrayList<String>();
    queryParameters.add("api-version=" + "2015-04-01");
    if (queryParameters.size() > 0) {
        url = url + "?" + CollectionStringBuilder.join(queryParameters, "&");
    }
    String baseUrl = this.getClient().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
    HttpPost httpRequest = new HttpPost(url);

    // Set Headers
    httpRequest.setHeader("Content-Type", "application/xml");
    httpRequest.setHeader("x-ms-version", "2015-04-01");

    // Serialize Request
    String requestContent = null;
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document requestDoc = documentBuilder.newDocument();

    Element createLocalNetworkGatewayParametersElement = requestDoc.createElementNS(
            "http://schemas.microsoft.com/windowsazure", "CreateLocalNetworkGatewayParameters");
    requestDoc.appendChild(createLocalNetworkGatewayParametersElement);

    if (parameters.getAddressSpace() != null) {
        if (parameters.getAddressSpace() instanceof LazyCollection == false
                || ((LazyCollection) parameters.getAddressSpace()).isInitialized()) {
            Element addressSpaceSequenceElement = requestDoc.createElementNS(
                    "http://schemas.microsoft.com/2003/10/Serialization/Arrays", "AddressSpace");
            for (String addressSpaceItem : parameters.getAddressSpace()) {
                Element addressSpaceItemElement = requestDoc
                        .createElementNS("http://schemas.microsoft.com/2003/10/Serialization/Arrays", "string");
                addressSpaceItemElement.appendChild(requestDoc.createTextNode(addressSpaceItem));
                addressSpaceSequenceElement.appendChild(addressSpaceItemElement);
            }
            createLocalNetworkGatewayParametersElement.appendChild(addressSpaceSequenceElement);
        }
    }

    if (parameters.getGatewayName() != null) {
        Element gatewayNameElement = requestDoc.createElementNS("http://schemas.microsoft.com/windowsazure",
                "GatewayName");
        gatewayNameElement.appendChild(requestDoc.createTextNode(parameters.getGatewayName()));
        createLocalNetworkGatewayParametersElement.appendChild(gatewayNameElement);
    }

    if (parameters.getIpAddress() != null) {
        Element ipAddressElement = requestDoc.createElementNS("http://schemas.microsoft.com/windowsazure",
                "IpAddress");
        ipAddressElement.appendChild(requestDoc.createTextNode(parameters.getIpAddress()));
        createLocalNetworkGatewayParametersElement.appendChild(ipAddressElement);
    }

    DOMSource domSource = new DOMSource(requestDoc);
    StringWriter stringWriter = new StringWriter();
    StreamResult streamResult = new StreamResult(stringWriter);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.transform(domSource, streamResult);
    requestContent = stringWriter.toString();
    StringEntity entity = new StringEntity(requestContent);
    httpRequest.setEntity(entity);
    httpRequest.setHeader("Content-Type", "application/xml");

    // Send Request
    HttpResponse httpResponse = null;
    try {
        if (shouldTrace) {
            CloudTracing.sendRequest(invocationId, httpRequest);
        }
        httpResponse = this.getClient().getHttpClient().execute(httpRequest);
        if (shouldTrace) {
            CloudTracing.receiveResponse(invocationId, httpResponse);
        }
        int statusCode = httpResponse.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
            ServiceException ex = ServiceException.createFromXml(httpRequest, requestContent, httpResponse,
                    httpResponse.getEntity());
            if (shouldTrace) {
                CloudTracing.error(invocationId, ex);
            }
            throw ex;
        }

        // Create Result
        LocalNetworkGatewayCreateResponse result = null;
        // Deserialize Response
        if (statusCode == HttpStatus.SC_OK) {
            InputStream responseContent = httpResponse.getEntity().getContent();
            result = new LocalNetworkGatewayCreateResponse();
            result.setLocalNetworkGatewayId(StreamUtils.toString(responseContent));

        }
        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();
        }
    }
}

From source file:com.microsoft.windowsazure.management.network.GatewayOperationsImpl.java

/**
* Operation to update existing gateway connection.
*
* @param gatewayId Required. The virtual network gateway Id.
* @param connectedentityId Required. The connected entity Id.
* @param parameters Required. Parameters supplied to the Begin Update
* gateway conneciton operation./*from  w w w. j  a v a2  s  .  co  m*/
* @throws ParserConfigurationException Thrown if there was an error
* configuring the parser for the response body.
* @throws SAXException Thrown if there was an error parsing the response
* body.
* @throws TransformerException Thrown if there was an error creating the
* DOM transformer.
* @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.
* @return A standard service response including an HTTP status code and
* request ID.
*/
@Override
public GatewayOperationResponse beginUpdateGatewayConnection(String gatewayId, String connectedentityId,
        UpdateGatewayConnectionParameters parameters)
        throws ParserConfigurationException, SAXException, TransformerException, IOException, ServiceException {
    // Validate
    if (gatewayId == null) {
        throw new NullPointerException("gatewayId");
    }
    if (connectedentityId == null) {
        throw new NullPointerException("connectedentityId");
    }
    if (parameters == null) {
        throw new NullPointerException("parameters");
    }

    // 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("gatewayId", gatewayId);
        tracingParameters.put("connectedentityId", connectedentityId);
        tracingParameters.put("parameters", parameters);
        CloudTracing.enter(invocationId, this, "beginUpdateGatewayConnectionAsync", tracingParameters);
    }

    // Construct URL
    String url = "";
    url = url + "/";
    if (this.getClient().getCredentials().getSubscriptionId() != null) {
        url = url + URLEncoder.encode(this.getClient().getCredentials().getSubscriptionId(), "UTF-8");
    }
    url = url + "/services/networking/gatewayconnections/virtualnetworkgateway/";
    url = url + URLEncoder.encode(gatewayId, "UTF-8");
    url = url + "/connectedentity/";
    url = url + URLEncoder.encode(connectedentityId, "UTF-8");
    ArrayList<String> queryParameters = new ArrayList<String>();
    queryParameters.add("api-version=" + "2015-04-01");
    if (queryParameters.size() > 0) {
        url = url + "?" + CollectionStringBuilder.join(queryParameters, "&");
    }
    String baseUrl = this.getClient().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
    HttpPatch httpRequest = new HttpPatch(url);

    // Set Headers
    httpRequest.setHeader("Content-Type", "application/xml");
    httpRequest.setHeader("x-ms-version", "2015-04-01");

    // Serialize Request
    String requestContent = null;
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document requestDoc = documentBuilder.newDocument();

    Element updateGatewayConnectionParametersElement = requestDoc
            .createElementNS("http://schemas.microsoft.com/windowsazure", "UpdateGatewayConnectionParameters");
    requestDoc.appendChild(updateGatewayConnectionParametersElement);

    Element routingWeightElement = requestDoc.createElementNS("http://schemas.microsoft.com/windowsazure",
            "RoutingWeight");
    routingWeightElement
            .appendChild(requestDoc.createTextNode(Integer.toString(parameters.getRoutingWeight())));
    updateGatewayConnectionParametersElement.appendChild(routingWeightElement);

    if (parameters.getSharedKey() != null) {
        Element sharedKeyElement = requestDoc.createElementNS("http://schemas.microsoft.com/windowsazure",
                "SharedKey");
        sharedKeyElement.appendChild(requestDoc.createTextNode(parameters.getSharedKey()));
        updateGatewayConnectionParametersElement.appendChild(sharedKeyElement);
    }

    DOMSource domSource = new DOMSource(requestDoc);
    StringWriter stringWriter = new StringWriter();
    StreamResult streamResult = new StreamResult(stringWriter);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.transform(domSource, streamResult);
    requestContent = stringWriter.toString();
    StringEntity entity = new StringEntity(requestContent);
    httpRequest.setEntity(entity);
    httpRequest.setHeader("Content-Type", "application/xml");

    // Send Request
    HttpResponse httpResponse = null;
    try {
        if (shouldTrace) {
            CloudTracing.sendRequest(invocationId, httpRequest);
        }
        httpResponse = this.getClient().getHttpClient().execute(httpRequest);
        if (shouldTrace) {
            CloudTracing.receiveResponse(invocationId, httpResponse);
        }
        int statusCode = httpResponse.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_ACCEPTED) {
            ServiceException ex = ServiceException.createFromXml(httpRequest, requestContent, httpResponse,
                    httpResponse.getEntity());
            if (shouldTrace) {
                CloudTracing.error(invocationId, ex);
            }
            throw ex;
        }

        // Create Result
        GatewayOperationResponse result = null;
        // Deserialize Response
        if (statusCode == HttpStatus.SC_ACCEPTED) {
            InputStream responseContent = httpResponse.getEntity().getContent();
            result = new GatewayOperationResponse();
            DocumentBuilderFactory documentBuilderFactory2 = DocumentBuilderFactory.newInstance();
            documentBuilderFactory2.setNamespaceAware(true);
            DocumentBuilder documentBuilder2 = documentBuilderFactory2.newDocumentBuilder();
            Document responseDoc = documentBuilder2.parse(new BOMInputStream(responseContent));

            Element gatewayOperationAsyncResponseElement = XmlUtility.getElementByTagNameNS(responseDoc,
                    "http://schemas.microsoft.com/windowsazure", "GatewayOperationAsyncResponse");
            if (gatewayOperationAsyncResponseElement != null) {
                Element idElement = XmlUtility.getElementByTagNameNS(gatewayOperationAsyncResponseElement,
                        "http://schemas.microsoft.com/windowsazure", "ID");
                if (idElement != null) {
                    String idInstance;
                    idInstance = idElement.getTextContent();
                    result.setOperationId(idInstance);
                }
            }

        }
        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();
        }
    }
}

From source file:com.microsoft.windowsazure.management.network.GatewayOperationsImpl.java

/**
* The Begin Start Diagnostics operation begins an asynchronous operation to
* starta diagnostics session for the specified virtual network gateway in
* Azure.  (see/*ww w.jav  a 2s  . c om*/
* http://msdn.microsoft.com/en-us/library/windowsazure/jj154119.aspx for
* more information)
*
* @param networkName Required. The name of the virtual network for this
* gateway.
* @param parameters Required. Parameters supplied to the Begin Start
* Diagnostics operation.
* @throws ParserConfigurationException Thrown if there was an error
* configuring the parser for the response body.
* @throws SAXException Thrown if there was an error parsing the response
* body.
* @throws TransformerException Thrown if there was an error creating the
* DOM transformer.
* @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.
* @return A standard service response including an HTTP status code and
* request ID.
*/
@Override
public GatewayOperationResponse beginStartDiagnostics(String networkName,
        StartGatewayPublicDiagnosticsParameters parameters)
        throws ParserConfigurationException, SAXException, TransformerException, IOException, ServiceException {
    // Validate
    if (networkName == null) {
        throw new NullPointerException("networkName");
    }
    if (parameters == null) {
        throw new NullPointerException("parameters");
    }

    // 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("networkName", networkName);
        tracingParameters.put("parameters", parameters);
        CloudTracing.enter(invocationId, this, "beginStartDiagnosticsAsync", tracingParameters);
    }

    // Construct URL
    String url = "";
    url = url + "/";
    if (this.getClient().getCredentials().getSubscriptionId() != null) {
        url = url + URLEncoder.encode(this.getClient().getCredentials().getSubscriptionId(), "UTF-8");
    }
    url = url + "/services/networking/";
    url = url + URLEncoder.encode(networkName, "UTF-8");
    url = url + "/gateway/publicdiagnostics";
    String baseUrl = this.getClient().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
    HttpPut httpRequest = new HttpPut(url);

    // Set Headers
    httpRequest.setHeader("Content-Type", "application/xml");
    httpRequest.setHeader("x-ms-version", "2015-04-01");

    // Serialize Request
    String requestContent = null;
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document requestDoc = documentBuilder.newDocument();

    Element updateGatewayPublicDiagnosticsElement = requestDoc
            .createElementNS("http://schemas.microsoft.com/windowsazure", "UpdateGatewayPublicDiagnostics");
    requestDoc.appendChild(updateGatewayPublicDiagnosticsElement);

    if (parameters.getCaptureDurationInSeconds() != null) {
        Element captureDurationInSecondsElement = requestDoc
                .createElementNS("http://schemas.microsoft.com/windowsazure", "CaptureDurationInSeconds");
        captureDurationInSecondsElement
                .appendChild(requestDoc.createTextNode(parameters.getCaptureDurationInSeconds()));
        updateGatewayPublicDiagnosticsElement.appendChild(captureDurationInSecondsElement);
    }

    if (parameters.getContainerName() != null) {
        Element containerNameElement = requestDoc.createElementNS("http://schemas.microsoft.com/windowsazure",
                "ContainerName");
        containerNameElement.appendChild(requestDoc.createTextNode(parameters.getContainerName()));
        updateGatewayPublicDiagnosticsElement.appendChild(containerNameElement);
    }

    if (parameters.getCustomerStorageKey() != null) {
        Element customerStorageKeyElement = requestDoc
                .createElementNS("http://schemas.microsoft.com/windowsazure", "CustomerStorageKey");
        customerStorageKeyElement.appendChild(requestDoc.createTextNode(parameters.getCustomerStorageKey()));
        updateGatewayPublicDiagnosticsElement.appendChild(customerStorageKeyElement);
    }

    if (parameters.getCustomerStorageName() != null) {
        Element customerStorageNameElement = requestDoc
                .createElementNS("http://schemas.microsoft.com/windowsazure", "CustomerStorageName");
        customerStorageNameElement.appendChild(requestDoc.createTextNode(parameters.getCustomerStorageName()));
        updateGatewayPublicDiagnosticsElement.appendChild(customerStorageNameElement);
    }

    if (parameters.getOperation() != null) {
        Element operationElement = requestDoc.createElementNS("http://schemas.microsoft.com/windowsazure",
                "Operation");
        operationElement.appendChild(requestDoc.createTextNode("StartDiagnostics"));
        updateGatewayPublicDiagnosticsElement.appendChild(operationElement);
    }

    DOMSource domSource = new DOMSource(requestDoc);
    StringWriter stringWriter = new StringWriter();
    StreamResult streamResult = new StreamResult(stringWriter);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.transform(domSource, streamResult);
    requestContent = stringWriter.toString();
    StringEntity entity = new StringEntity(requestContent);
    httpRequest.setEntity(entity);
    httpRequest.setHeader("Content-Type", "application/xml");

    // Send Request
    HttpResponse httpResponse = null;
    try {
        if (shouldTrace) {
            CloudTracing.sendRequest(invocationId, httpRequest);
        }
        httpResponse = this.getClient().getHttpClient().execute(httpRequest);
        if (shouldTrace) {
            CloudTracing.receiveResponse(invocationId, httpResponse);
        }
        int statusCode = httpResponse.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_ACCEPTED) {
            ServiceException ex = ServiceException.createFromXml(httpRequest, requestContent, httpResponse,
                    httpResponse.getEntity());
            if (shouldTrace) {
                CloudTracing.error(invocationId, ex);
            }
            throw ex;
        }

        // Create Result
        GatewayOperationResponse result = null;
        // Deserialize Response
        if (statusCode == HttpStatus.SC_ACCEPTED) {
            InputStream responseContent = httpResponse.getEntity().getContent();
            result = new GatewayOperationResponse();
            DocumentBuilderFactory documentBuilderFactory2 = DocumentBuilderFactory.newInstance();
            documentBuilderFactory2.setNamespaceAware(true);
            DocumentBuilder documentBuilder2 = documentBuilderFactory2.newDocumentBuilder();
            Document responseDoc = documentBuilder2.parse(new BOMInputStream(responseContent));

            Element gatewayOperationAsyncResponseElement = XmlUtility.getElementByTagNameNS(responseDoc,
                    "http://schemas.microsoft.com/windowsazure", "GatewayOperationAsyncResponse");
            if (gatewayOperationAsyncResponseElement != null) {
                Element idElement = XmlUtility.getElementByTagNameNS(gatewayOperationAsyncResponseElement,
                        "http://schemas.microsoft.com/windowsazure", "ID");
                if (idElement != null) {
                    String idInstance;
                    idInstance = idElement.getTextContent();
                    result.setOperationId(idInstance);
                }
            }

        }
        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();
        }
    }
}

From source file:com.microsoft.windowsazure.management.network.GatewayOperationsImpl.java

/**
* The Begin Start Diagnostics V2 operation begins an asynchronous operation
* to starta diagnostics session for the specified virtual network gateway.
*
* @param gatewayId Required. The virtual network for this gateway Id.
* @param parameters Required. Parameters supplied to the Begin Start
* Diagnostics V2 operation./*w ww  .ja  v a 2s. c o m*/
* @throws ParserConfigurationException Thrown if there was an error
* configuring the parser for the response body.
* @throws SAXException Thrown if there was an error parsing the response
* body.
* @throws TransformerException Thrown if there was an error creating the
* DOM transformer.
* @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.
* @return A standard service response including an HTTP status code and
* request ID.
*/
@Override
public GatewayOperationResponse beginStartDiagnosticsV2(String gatewayId,
        StartGatewayPublicDiagnosticsParameters parameters)
        throws ParserConfigurationException, SAXException, TransformerException, IOException, ServiceException {
    // Validate
    if (gatewayId == null) {
        throw new NullPointerException("gatewayId");
    }
    if (parameters == null) {
        throw new NullPointerException("parameters");
    }

    // 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("gatewayId", gatewayId);
        tracingParameters.put("parameters", parameters);
        CloudTracing.enter(invocationId, this, "beginStartDiagnosticsV2Async", tracingParameters);
    }

    // Construct URL
    String url = "";
    url = url + "/";
    if (this.getClient().getCredentials().getSubscriptionId() != null) {
        url = url + URLEncoder.encode(this.getClient().getCredentials().getSubscriptionId(), "UTF-8");
    }
    url = url + "/services/networking/virtualnetworkgateways/";
    url = url + URLEncoder.encode(gatewayId, "UTF-8");
    url = url + "/publicdiagnostics";
    String baseUrl = this.getClient().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
    HttpPut httpRequest = new HttpPut(url);

    // Set Headers
    httpRequest.setHeader("Content-Type", "application/xml");
    httpRequest.setHeader("x-ms-version", "2015-04-01");

    // Serialize Request
    String requestContent = null;
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document requestDoc = documentBuilder.newDocument();

    Element updateGatewayPublicDiagnosticsElement = requestDoc
            .createElementNS("http://schemas.microsoft.com/windowsazure", "UpdateGatewayPublicDiagnostics");
    requestDoc.appendChild(updateGatewayPublicDiagnosticsElement);

    if (parameters.getCaptureDurationInSeconds() != null) {
        Element captureDurationInSecondsElement = requestDoc
                .createElementNS("http://schemas.microsoft.com/windowsazure", "CaptureDurationInSeconds");
        captureDurationInSecondsElement
                .appendChild(requestDoc.createTextNode(parameters.getCaptureDurationInSeconds()));
        updateGatewayPublicDiagnosticsElement.appendChild(captureDurationInSecondsElement);
    }

    if (parameters.getContainerName() != null) {
        Element containerNameElement = requestDoc.createElementNS("http://schemas.microsoft.com/windowsazure",
                "ContainerName");
        containerNameElement.appendChild(requestDoc.createTextNode(parameters.getContainerName()));
        updateGatewayPublicDiagnosticsElement.appendChild(containerNameElement);
    }

    if (parameters.getCustomerStorageKey() != null) {
        Element customerStorageKeyElement = requestDoc
                .createElementNS("http://schemas.microsoft.com/windowsazure", "CustomerStorageKey");
        customerStorageKeyElement.appendChild(requestDoc.createTextNode(parameters.getCustomerStorageKey()));
        updateGatewayPublicDiagnosticsElement.appendChild(customerStorageKeyElement);
    }

    if (parameters.getCustomerStorageName() != null) {
        Element customerStorageNameElement = requestDoc
                .createElementNS("http://schemas.microsoft.com/windowsazure", "CustomerStorageName");
        customerStorageNameElement.appendChild(requestDoc.createTextNode(parameters.getCustomerStorageName()));
        updateGatewayPublicDiagnosticsElement.appendChild(customerStorageNameElement);
    }

    if (parameters.getOperation() != null) {
        Element operationElement = requestDoc.createElementNS("http://schemas.microsoft.com/windowsazure",
                "Operation");
        operationElement.appendChild(requestDoc.createTextNode("StartDiagnostics"));
        updateGatewayPublicDiagnosticsElement.appendChild(operationElement);
    }

    DOMSource domSource = new DOMSource(requestDoc);
    StringWriter stringWriter = new StringWriter();
    StreamResult streamResult = new StreamResult(stringWriter);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.transform(domSource, streamResult);
    requestContent = stringWriter.toString();
    StringEntity entity = new StringEntity(requestContent);
    httpRequest.setEntity(entity);
    httpRequest.setHeader("Content-Type", "application/xml");

    // Send Request
    HttpResponse httpResponse = null;
    try {
        if (shouldTrace) {
            CloudTracing.sendRequest(invocationId, httpRequest);
        }
        httpResponse = this.getClient().getHttpClient().execute(httpRequest);
        if (shouldTrace) {
            CloudTracing.receiveResponse(invocationId, httpResponse);
        }
        int statusCode = httpResponse.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_ACCEPTED) {
            ServiceException ex = ServiceException.createFromXml(httpRequest, requestContent, httpResponse,
                    httpResponse.getEntity());
            if (shouldTrace) {
                CloudTracing.error(invocationId, ex);
            }
            throw ex;
        }

        // Create Result
        GatewayOperationResponse result = null;
        // Deserialize Response
        if (statusCode == HttpStatus.SC_ACCEPTED) {
            InputStream responseContent = httpResponse.getEntity().getContent();
            result = new GatewayOperationResponse();
            DocumentBuilderFactory documentBuilderFactory2 = DocumentBuilderFactory.newInstance();
            documentBuilderFactory2.setNamespaceAware(true);
            DocumentBuilder documentBuilder2 = documentBuilderFactory2.newDocumentBuilder();
            Document responseDoc = documentBuilder2.parse(new BOMInputStream(responseContent));

            Element gatewayOperationAsyncResponseElement = XmlUtility.getElementByTagNameNS(responseDoc,
                    "http://schemas.microsoft.com/windowsazure", "GatewayOperationAsyncResponse");
            if (gatewayOperationAsyncResponseElement != null) {
                Element idElement = XmlUtility.getElementByTagNameNS(gatewayOperationAsyncResponseElement,
                        "http://schemas.microsoft.com/windowsazure", "ID");
                if (idElement != null) {
                    String idInstance;
                    idInstance = idElement.getTextContent();
                    result.setOperationId(idInstance);
                }
            }

        }
        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();
        }
    }
}

From source file:com.microsoft.windowsazure.management.network.GatewayOperationsImpl.java

/**
* The Begin Creating Virtual network Gateway operation creates a new
* network gateway.//from  www  .ja  v a 2 s  .  com
*
* @param networkName Required. The name of the virtual network for this
* gateway.
* @param parameters Required. Parameters supplied to the Begin Creating
* Virtual Network Gateway operation.
* @throws ParserConfigurationException Thrown if there was an error
* configuring the parser for the response body.
* @throws SAXException Thrown if there was an error parsing the response
* body.
* @throws TransformerException Thrown if there was an error creating the
* DOM transformer.
* @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.
* @return A standard service response including an HTTP status code and
* request ID.
*/
@Override
public GatewayOperationResponse beginCreatingVirtualNetworkGateway(String networkName,
        VirtualNetworkGatewayCreateParameters parameters)
        throws ParserConfigurationException, SAXException, TransformerException, IOException, ServiceException {
    // Validate
    if (networkName == null) {
        throw new NullPointerException("networkName");
    }
    if (parameters == null) {
        throw new NullPointerException("parameters");
    }

    // 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("networkName", networkName);
        tracingParameters.put("parameters", parameters);
        CloudTracing.enter(invocationId, this, "beginCreatingVirtualNetworkGatewayAsync", tracingParameters);
    }

    // Construct URL
    String url = "";
    url = url + "/";
    if (this.getClient().getCredentials().getSubscriptionId() != null) {
        url = url + URLEncoder.encode(this.getClient().getCredentials().getSubscriptionId(), "UTF-8");
    }
    url = url + "/services/networking/";
    url = url + URLEncoder.encode(networkName, "UTF-8");
    url = url + "/virtualnetworkgateways";
    String baseUrl = this.getClient().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
    HttpPost httpRequest = new HttpPost(url);

    // Set Headers
    httpRequest.setHeader("Content-Type", "application/xml");
    httpRequest.setHeader("x-ms-version", "2015-04-01");

    // Serialize Request
    String requestContent = null;
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document requestDoc = documentBuilder.newDocument();

    Element createVirtualNetworkGatewayParametersElement = requestDoc.createElementNS(
            "http://schemas.microsoft.com/windowsazure", "CreateVirtualNetworkGatewayParameters");
    requestDoc.appendChild(createVirtualNetworkGatewayParametersElement);

    if (parameters.getGatewayName() != null) {
        Element gatewayNameElement = requestDoc.createElementNS("http://schemas.microsoft.com/windowsazure",
                "GatewayName");
        gatewayNameElement.appendChild(requestDoc.createTextNode(parameters.getGatewayName()));
        createVirtualNetworkGatewayParametersElement.appendChild(gatewayNameElement);
    }

    if (parameters.getGatewaySKU() != null) {
        Element gatewaySizeElement = requestDoc.createElementNS("http://schemas.microsoft.com/windowsazure",
                "GatewaySize");
        gatewaySizeElement.appendChild(requestDoc.createTextNode(parameters.getGatewaySKU()));
        createVirtualNetworkGatewayParametersElement.appendChild(gatewaySizeElement);
    }

    if (parameters.getGatewayType() != null) {
        Element gatewayTypeElement = requestDoc.createElementNS("http://schemas.microsoft.com/windowsazure",
                "GatewayType");
        gatewayTypeElement.appendChild(requestDoc.createTextNode(parameters.getGatewayType()));
        createVirtualNetworkGatewayParametersElement.appendChild(gatewayTypeElement);
    }

    if (parameters.getLocation() != null) {
        Element locationElement = requestDoc.createElementNS("http://schemas.microsoft.com/windowsazure",
                "Location");
        locationElement.appendChild(requestDoc.createTextNode(parameters.getLocation()));
        createVirtualNetworkGatewayParametersElement.appendChild(locationElement);
    }

    if (parameters.getVnetId() != null) {
        Element vnetIdElement = requestDoc.createElementNS("http://schemas.microsoft.com/windowsazure",
                "VnetId");
        vnetIdElement.appendChild(requestDoc.createTextNode(parameters.getVnetId()));
        createVirtualNetworkGatewayParametersElement.appendChild(vnetIdElement);
    }

    DOMSource domSource = new DOMSource(requestDoc);
    StringWriter stringWriter = new StringWriter();
    StreamResult streamResult = new StreamResult(stringWriter);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.transform(domSource, streamResult);
    requestContent = stringWriter.toString();
    StringEntity entity = new StringEntity(requestContent);
    httpRequest.setEntity(entity);
    httpRequest.setHeader("Content-Type", "application/xml");

    // Send Request
    HttpResponse httpResponse = null;
    try {
        if (shouldTrace) {
            CloudTracing.sendRequest(invocationId, httpRequest);
        }
        httpResponse = this.getClient().getHttpClient().execute(httpRequest);
        if (shouldTrace) {
            CloudTracing.receiveResponse(invocationId, httpResponse);
        }
        int statusCode = httpResponse.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_CREATED) {
            ServiceException ex = ServiceException.createFromXml(httpRequest, requestContent, httpResponse,
                    httpResponse.getEntity());
            if (shouldTrace) {
                CloudTracing.error(invocationId, ex);
            }
            throw ex;
        }

        // Create Result
        GatewayOperationResponse result = null;
        // Deserialize Response
        if (statusCode == HttpStatus.SC_CREATED) {
            InputStream responseContent = httpResponse.getEntity().getContent();
            result = new GatewayOperationResponse();
            DocumentBuilderFactory documentBuilderFactory2 = DocumentBuilderFactory.newInstance();
            documentBuilderFactory2.setNamespaceAware(true);
            DocumentBuilder documentBuilder2 = documentBuilderFactory2.newDocumentBuilder();
            Document responseDoc = documentBuilder2.parse(new BOMInputStream(responseContent));

            Element gatewayOperationAsyncResponseElement = XmlUtility.getElementByTagNameNS(responseDoc,
                    "http://schemas.microsoft.com/windowsazure", "GatewayOperationAsyncResponse");
            if (gatewayOperationAsyncResponseElement != null) {
                Element idElement = XmlUtility.getElementByTagNameNS(gatewayOperationAsyncResponseElement,
                        "http://schemas.microsoft.com/windowsazure", "ID");
                if (idElement != null) {
                    String idInstance;
                    idInstance = idElement.getTextContent();
                    result.setOperationId(idInstance);
                }
            }

        }
        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();
        }
    }
}

From source file:com.microsoft.windowsazure.management.network.GatewayOperationsImpl.java

/**
* The Begin Creating Virtual network Gateway connection operation creates a
* new network gateway connection./*from  w  w w . j a va 2  s .  c o  m*/
*
* @param parameters Required. Parameters supplied to the Begin Creating
* Gateway Connection operation.
* @throws ParserConfigurationException Thrown if there was an error
* configuring the parser for the response body.
* @throws SAXException Thrown if there was an error parsing the response
* body.
* @throws TransformerException Thrown if there was an error creating the
* DOM transformer.
* @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.
* @return A standard service response including an HTTP status code and
* request ID.
*/
@Override
public GatewayOperationResponse beginCreatingGatewayConnection(GatewayConnectionCreateParameters parameters)
        throws ParserConfigurationException, SAXException, TransformerException, IOException, ServiceException {
    // Validate
    if (parameters == null) {
        throw new NullPointerException("parameters");
    }

    // 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("parameters", parameters);
        CloudTracing.enter(invocationId, this, "beginCreatingGatewayConnectionAsync", tracingParameters);
    }

    // Construct URL
    String url = "";
    url = url + "/";
    if (this.getClient().getCredentials().getSubscriptionId() != null) {
        url = url + URLEncoder.encode(this.getClient().getCredentials().getSubscriptionId(), "UTF-8");
    }
    url = url + "/services/networking/gatewayconnections";
    ArrayList<String> queryParameters = new ArrayList<String>();
    queryParameters.add("api-version=" + "2015-04-01");
    if (queryParameters.size() > 0) {
        url = url + "?" + CollectionStringBuilder.join(queryParameters, "&");
    }
    String baseUrl = this.getClient().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
    HttpPost httpRequest = new HttpPost(url);

    // Set Headers
    httpRequest.setHeader("Content-Type", "application/xml");
    httpRequest.setHeader("x-ms-version", "2015-04-01");

    // Serialize Request
    String requestContent = null;
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document requestDoc = documentBuilder.newDocument();

    Element createGatewayConnectionParametersElement = requestDoc
            .createElementNS("http://schemas.microsoft.com/windowsazure", "CreateGatewayConnectionParameters");
    requestDoc.appendChild(createGatewayConnectionParametersElement);

    if (parameters.getConnectedEntityId() != null) {
        Element connectedEntityIdElement = requestDoc
                .createElementNS("http://schemas.microsoft.com/windowsazure", "ConnectedEntityId");
        connectedEntityIdElement.appendChild(requestDoc.createTextNode(parameters.getConnectedEntityId()));
        createGatewayConnectionParametersElement.appendChild(connectedEntityIdElement);
    }

    if (parameters.getGatewayConnectionName() != null) {
        Element gatewayConnectionNameElement = requestDoc
                .createElementNS("http://schemas.microsoft.com/windowsazure", "GatewayConnectionName");
        gatewayConnectionNameElement
                .appendChild(requestDoc.createTextNode(parameters.getGatewayConnectionName()));
        createGatewayConnectionParametersElement.appendChild(gatewayConnectionNameElement);
    }

    if (parameters.getGatewayConnectionType() != null) {
        Element gatewayConnectionTypeElement = requestDoc
                .createElementNS("http://schemas.microsoft.com/windowsazure", "GatewayConnectionType");
        gatewayConnectionTypeElement
                .appendChild(requestDoc.createTextNode(parameters.getGatewayConnectionType()));
        createGatewayConnectionParametersElement.appendChild(gatewayConnectionTypeElement);
    }

    if (parameters.getVirtualNetworkGatewayId() != null) {
        Element virtualNetworkGatewayIdElement = requestDoc
                .createElementNS("http://schemas.microsoft.com/windowsazure", "VirtualNetworkGatewayId");
        virtualNetworkGatewayIdElement
                .appendChild(requestDoc.createTextNode(parameters.getVirtualNetworkGatewayId()));
        createGatewayConnectionParametersElement.appendChild(virtualNetworkGatewayIdElement);
    }

    Element routingWeightElement = requestDoc.createElementNS("http://schemas.microsoft.com/windowsazure",
            "RoutingWeight");
    routingWeightElement
            .appendChild(requestDoc.createTextNode(Integer.toString(parameters.getRoutingWeight())));
    createGatewayConnectionParametersElement.appendChild(routingWeightElement);

    if (parameters.getSharedKey() != null) {
        Element sharedKeyElement = requestDoc.createElementNS("http://schemas.microsoft.com/windowsazure",
                "SharedKey");
        sharedKeyElement.appendChild(requestDoc.createTextNode(parameters.getSharedKey()));
        createGatewayConnectionParametersElement.appendChild(sharedKeyElement);
    }

    DOMSource domSource = new DOMSource(requestDoc);
    StringWriter stringWriter = new StringWriter();
    StreamResult streamResult = new StreamResult(stringWriter);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.transform(domSource, streamResult);
    requestContent = stringWriter.toString();
    StringEntity entity = new StringEntity(requestContent);
    httpRequest.setEntity(entity);
    httpRequest.setHeader("Content-Type", "application/xml");

    // Send Request
    HttpResponse httpResponse = null;
    try {
        if (shouldTrace) {
            CloudTracing.sendRequest(invocationId, httpRequest);
        }
        httpResponse = this.getClient().getHttpClient().execute(httpRequest);
        if (shouldTrace) {
            CloudTracing.receiveResponse(invocationId, httpResponse);
        }
        int statusCode = httpResponse.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_ACCEPTED) {
            ServiceException ex = ServiceException.createFromXml(httpRequest, requestContent, httpResponse,
                    httpResponse.getEntity());
            if (shouldTrace) {
                CloudTracing.error(invocationId, ex);
            }
            throw ex;
        }

        // Create Result
        GatewayOperationResponse result = null;
        // Deserialize Response
        if (statusCode == HttpStatus.SC_ACCEPTED) {
            InputStream responseContent = httpResponse.getEntity().getContent();
            result = new GatewayOperationResponse();
            DocumentBuilderFactory documentBuilderFactory2 = DocumentBuilderFactory.newInstance();
            documentBuilderFactory2.setNamespaceAware(true);
            DocumentBuilder documentBuilder2 = documentBuilderFactory2.newDocumentBuilder();
            Document responseDoc = documentBuilder2.parse(new BOMInputStream(responseContent));

            Element gatewayOperationAsyncResponseElement = XmlUtility.getElementByTagNameNS(responseDoc,
                    "http://schemas.microsoft.com/windowsazure", "GatewayOperationAsyncResponse");
            if (gatewayOperationAsyncResponseElement != null) {
                Element idElement = XmlUtility.getElementByTagNameNS(gatewayOperationAsyncResponseElement,
                        "http://schemas.microsoft.com/windowsazure", "ID");
                if (idElement != null) {
                    String idInstance;
                    idInstance = idElement.getTextContent();
                    result.setOperationId(idInstance);
                }
            }

        }
        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();
        }
    }
}

From source file:com.microsoft.windowsazure.management.network.GatewayOperationsImpl.java

/**
* The Begin Set Virtual Network Gateway IPsec Parameters operation sets the
* IPsec parameters on the virtual network gateway for the specified
* connection to the specified local network in Azure.
*
* @param networkName Required. The name of the virtual network for this
* gateway./* w  ww  . j  a  v a  2  s . c  o  m*/
* @param localNetworkName Required. The name of the local network.
* @param parameters Required. Parameters supplied to the Begin Virtual
* Network Gateway Set IPsec Parameters request.
* @throws ParserConfigurationException Thrown if there was an error
* configuring the parser for the response body.
* @throws SAXException Thrown if there was an error parsing the response
* body.
* @throws TransformerException Thrown if there was an error creating the
* DOM transformer.
* @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.
* @return A standard service response including an HTTP status code and
* request ID.
*/
@Override
public GatewayOperationResponse beginSetIPsecParameters(String networkName, String localNetworkName,
        GatewaySetIPsecParametersParameters parameters)
        throws ParserConfigurationException, SAXException, TransformerException, IOException, ServiceException {
    // Validate
    if (networkName == null) {
        throw new NullPointerException("networkName");
    }
    if (localNetworkName == null) {
        throw new NullPointerException("localNetworkName");
    }
    if (parameters == null) {
        throw new NullPointerException("parameters");
    }

    // 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("networkName", networkName);
        tracingParameters.put("localNetworkName", localNetworkName);
        tracingParameters.put("parameters", parameters);
        CloudTracing.enter(invocationId, this, "beginSetIPsecParametersAsync", tracingParameters);
    }

    // Construct URL
    String url = "";
    url = url + "/";
    if (this.getClient().getCredentials().getSubscriptionId() != null) {
        url = url + URLEncoder.encode(this.getClient().getCredentials().getSubscriptionId(), "UTF-8");
    }
    url = url + "/services/networking/";
    url = url + URLEncoder.encode(networkName, "UTF-8");
    url = url + "/gateway/connection/";
    url = url + URLEncoder.encode(localNetworkName, "UTF-8");
    url = url + "/ipsecparameters";
    String baseUrl = this.getClient().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
    HttpPatch httpRequest = new HttpPatch(url);

    // Set Headers
    httpRequest.setHeader("Content-Type", "application/xml");
    httpRequest.setHeader("x-ms-version", "2015-04-01");

    // Serialize Request
    String requestContent = null;
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document requestDoc = documentBuilder.newDocument();

    if (parameters.getParameters() != null) {
        Element iPsecParametersElement = requestDoc.createElementNS("http://schemas.microsoft.com/windowsazure",
                "IPsecParameters");
        requestDoc.appendChild(iPsecParametersElement);

        if (parameters.getParameters().getEncryptionType() != null) {
            Element encryptionTypeElement = requestDoc
                    .createElementNS("http://schemas.microsoft.com/windowsazure", "EncryptionType");
            encryptionTypeElement
                    .appendChild(requestDoc.createTextNode(parameters.getParameters().getEncryptionType()));
            iPsecParametersElement.appendChild(encryptionTypeElement);
        }

        if (parameters.getParameters().getPfsGroup() != null) {
            Element pfsGroupElement = requestDoc.createElementNS("http://schemas.microsoft.com/windowsazure",
                    "PfsGroup");
            pfsGroupElement.appendChild(requestDoc.createTextNode(parameters.getParameters().getPfsGroup()));
            iPsecParametersElement.appendChild(pfsGroupElement);
        }

        Element sADataSizeKilobytesElement = requestDoc
                .createElementNS("http://schemas.microsoft.com/windowsazure", "SADataSizeKilobytes");
        sADataSizeKilobytesElement.appendChild(requestDoc
                .createTextNode(Integer.toString(parameters.getParameters().getSADataSizeKilobytes())));
        iPsecParametersElement.appendChild(sADataSizeKilobytesElement);

        Element sALifeTimeSecondsElement = requestDoc
                .createElementNS("http://schemas.microsoft.com/windowsazure", "SALifeTimeSeconds");
        sALifeTimeSecondsElement.appendChild(
                requestDoc.createTextNode(Integer.toString(parameters.getParameters().getSALifeTimeSeconds())));
        iPsecParametersElement.appendChild(sALifeTimeSecondsElement);

        if (parameters.getParameters().getHashAlgorithm() != null) {
            Element hashAlgorithmElement = requestDoc
                    .createElementNS("http://schemas.microsoft.com/windowsazure", "HashAlgorithm");
            hashAlgorithmElement
                    .appendChild(requestDoc.createTextNode(parameters.getParameters().getHashAlgorithm()));
            iPsecParametersElement.appendChild(hashAlgorithmElement);
        }
    }

    DOMSource domSource = new DOMSource(requestDoc);
    StringWriter stringWriter = new StringWriter();
    StreamResult streamResult = new StreamResult(stringWriter);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.transform(domSource, streamResult);
    requestContent = stringWriter.toString();
    StringEntity entity = new StringEntity(requestContent);
    httpRequest.setEntity(entity);
    httpRequest.setHeader("Content-Type", "application/xml");

    // Send Request
    HttpResponse httpResponse = null;
    try {
        if (shouldTrace) {
            CloudTracing.sendRequest(invocationId, httpRequest);
        }
        httpResponse = this.getClient().getHttpClient().execute(httpRequest);
        if (shouldTrace) {
            CloudTracing.receiveResponse(invocationId, httpResponse);
        }
        int statusCode = httpResponse.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_ACCEPTED) {
            ServiceException ex = ServiceException.createFromXml(httpRequest, requestContent, httpResponse,
                    httpResponse.getEntity());
            if (shouldTrace) {
                CloudTracing.error(invocationId, ex);
            }
            throw ex;
        }

        // Create Result
        GatewayOperationResponse result = null;
        // Deserialize Response
        if (statusCode == HttpStatus.SC_ACCEPTED) {
            InputStream responseContent = httpResponse.getEntity().getContent();
            result = new GatewayOperationResponse();
            DocumentBuilderFactory documentBuilderFactory2 = DocumentBuilderFactory.newInstance();
            documentBuilderFactory2.setNamespaceAware(true);
            DocumentBuilder documentBuilder2 = documentBuilderFactory2.newDocumentBuilder();
            Document responseDoc = documentBuilder2.parse(new BOMInputStream(responseContent));

            Element gatewayOperationAsyncResponseElement = XmlUtility.getElementByTagNameNS(responseDoc,
                    "http://schemas.microsoft.com/windowsazure", "GatewayOperationAsyncResponse");
            if (gatewayOperationAsyncResponseElement != null) {
                Element idElement = XmlUtility.getElementByTagNameNS(gatewayOperationAsyncResponseElement,
                        "http://schemas.microsoft.com/windowsazure", "ID");
                if (idElement != null) {
                    String idInstance;
                    idInstance = idElement.getTextContent();
                    result.setOperationId(idInstance);
                }
            }

        }
        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();
        }
    }
}

From source file:com.microsoft.windowsazure.management.network.GatewayOperationsImpl.java

/**
* The Begin Set Virtual Network Gateway IPsec Parameters V2 operation sets
* the IPsec parameters on the virtual network gateway connection.
*
* @param gatewayId Required. The virtual network for this gateway Id.
* @param connectedentityId Required. The connected entity Id.
* @param parameters Required. Parameters supplied to the Begin Virtual
* Network Gateway Set IPsec Parameters V2 request.
* @throws ParserConfigurationException Thrown if there was an error
* configuring the parser for the response body.
* @throws SAXException Thrown if there was an error parsing the response
* body./*ww w .  j  a va 2 s . com*/
* @throws TransformerException Thrown if there was an error creating the
* DOM transformer.
* @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.
* @return A standard service response including an HTTP status code and
* request ID.
*/
@Override
public GatewayOperationResponse beginSetIPsecParametersV2(String gatewayId, String connectedentityId,
        GatewaySetIPsecParametersParameters parameters)
        throws ParserConfigurationException, SAXException, TransformerException, IOException, ServiceException {
    // Validate
    if (gatewayId == null) {
        throw new NullPointerException("gatewayId");
    }
    if (connectedentityId == null) {
        throw new NullPointerException("connectedentityId");
    }
    if (parameters == null) {
        throw new NullPointerException("parameters");
    }

    // 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("gatewayId", gatewayId);
        tracingParameters.put("connectedentityId", connectedentityId);
        tracingParameters.put("parameters", parameters);
        CloudTracing.enter(invocationId, this, "beginSetIPsecParametersV2Async", tracingParameters);
    }

    // Construct URL
    String url = "";
    url = url + "/";
    if (this.getClient().getCredentials().getSubscriptionId() != null) {
        url = url + URLEncoder.encode(this.getClient().getCredentials().getSubscriptionId(), "UTF-8");
    }
    url = url + "/services/networking/virtualnetworkgateways/";
    url = url + URLEncoder.encode(gatewayId, "UTF-8");
    url = url + "/connectedentity/";
    url = url + URLEncoder.encode(connectedentityId, "UTF-8");
    url = url + "/ipsecparameters";
    String baseUrl = this.getClient().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
    HttpPatch httpRequest = new HttpPatch(url);

    // Set Headers
    httpRequest.setHeader("Content-Type", "application/xml");
    httpRequest.setHeader("x-ms-version", "2015-04-01");

    // Serialize Request
    String requestContent = null;
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document requestDoc = documentBuilder.newDocument();

    if (parameters.getParameters() != null) {
        Element iPsecParametersElement = requestDoc.createElementNS("http://schemas.microsoft.com/windowsazure",
                "IPsecParameters");
        requestDoc.appendChild(iPsecParametersElement);

        if (parameters.getParameters().getEncryptionType() != null) {
            Element encryptionTypeElement = requestDoc
                    .createElementNS("http://schemas.microsoft.com/windowsazure", "EncryptionType");
            encryptionTypeElement
                    .appendChild(requestDoc.createTextNode(parameters.getParameters().getEncryptionType()));
            iPsecParametersElement.appendChild(encryptionTypeElement);
        }

        if (parameters.getParameters().getPfsGroup() != null) {
            Element pfsGroupElement = requestDoc.createElementNS("http://schemas.microsoft.com/windowsazure",
                    "PfsGroup");
            pfsGroupElement.appendChild(requestDoc.createTextNode(parameters.getParameters().getPfsGroup()));
            iPsecParametersElement.appendChild(pfsGroupElement);
        }

        Element sADataSizeKilobytesElement = requestDoc
                .createElementNS("http://schemas.microsoft.com/windowsazure", "SADataSizeKilobytes");
        sADataSizeKilobytesElement.appendChild(requestDoc
                .createTextNode(Integer.toString(parameters.getParameters().getSADataSizeKilobytes())));
        iPsecParametersElement.appendChild(sADataSizeKilobytesElement);

        Element sALifeTimeSecondsElement = requestDoc
                .createElementNS("http://schemas.microsoft.com/windowsazure", "SALifeTimeSeconds");
        sALifeTimeSecondsElement.appendChild(
                requestDoc.createTextNode(Integer.toString(parameters.getParameters().getSALifeTimeSeconds())));
        iPsecParametersElement.appendChild(sALifeTimeSecondsElement);

        if (parameters.getParameters().getHashAlgorithm() != null) {
            Element hashAlgorithmElement = requestDoc
                    .createElementNS("http://schemas.microsoft.com/windowsazure", "HashAlgorithm");
            hashAlgorithmElement
                    .appendChild(requestDoc.createTextNode(parameters.getParameters().getHashAlgorithm()));
            iPsecParametersElement.appendChild(hashAlgorithmElement);
        }
    }

    DOMSource domSource = new DOMSource(requestDoc);
    StringWriter stringWriter = new StringWriter();
    StreamResult streamResult = new StreamResult(stringWriter);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.transform(domSource, streamResult);
    requestContent = stringWriter.toString();
    StringEntity entity = new StringEntity(requestContent);
    httpRequest.setEntity(entity);
    httpRequest.setHeader("Content-Type", "application/xml");

    // Send Request
    HttpResponse httpResponse = null;
    try {
        if (shouldTrace) {
            CloudTracing.sendRequest(invocationId, httpRequest);
        }
        httpResponse = this.getClient().getHttpClient().execute(httpRequest);
        if (shouldTrace) {
            CloudTracing.receiveResponse(invocationId, httpResponse);
        }
        int statusCode = httpResponse.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_ACCEPTED) {
            ServiceException ex = ServiceException.createFromXml(httpRequest, requestContent, httpResponse,
                    httpResponse.getEntity());
            if (shouldTrace) {
                CloudTracing.error(invocationId, ex);
            }
            throw ex;
        }

        // Create Result
        GatewayOperationResponse result = null;
        // Deserialize Response
        if (statusCode == HttpStatus.SC_ACCEPTED) {
            InputStream responseContent = httpResponse.getEntity().getContent();
            result = new GatewayOperationResponse();
            DocumentBuilderFactory documentBuilderFactory2 = DocumentBuilderFactory.newInstance();
            documentBuilderFactory2.setNamespaceAware(true);
            DocumentBuilder documentBuilder2 = documentBuilderFactory2.newDocumentBuilder();
            Document responseDoc = documentBuilder2.parse(new BOMInputStream(responseContent));

            Element gatewayOperationAsyncResponseElement = XmlUtility.getElementByTagNameNS(responseDoc,
                    "http://schemas.microsoft.com/windowsazure", "GatewayOperationAsyncResponse");
            if (gatewayOperationAsyncResponseElement != null) {
                Element idElement = XmlUtility.getElementByTagNameNS(gatewayOperationAsyncResponseElement,
                        "http://schemas.microsoft.com/windowsazure", "ID");
                if (idElement != null) {
                    String idInstance;
                    idInstance = idElement.getTextContent();
                    result.setOperationId(idInstance);
                }
            }

        }
        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();
        }
    }
}

From source file:com.microsoft.windowsazure.management.compute.DeploymentOperationsImpl.java

/**
* The Begin Swapping Deployment operation initiates a virtual IP address
* swap between the staging and production deployment environments for a
* service. If the service is currently running in the staging environment,
* it will be swapped to the production environment. If it is running in
* the production environment, it will be swapped to staging. For more
* information on this type of upgrade, see Performing Virtual IP Swap
* Upgrades at//from w  w  w  .  j a  va  2  s  .c o  m
* http://msdn.microsoft.com/en-us/library/windowsazure/ee517253.aspx.
* (see http://msdn.microsoft.com/en-us/library/windowsazure/ee460814.aspx
* for more information)
*
* @param serviceName Required. The cloud service to swap deployments for.
* @param parameters Required. Parameters supplied to the Begin Swapping
* Deployment operation.
* @throws ParserConfigurationException Thrown if there was an error
* configuring the parser for the response body.
* @throws SAXException Thrown if there was an error parsing the response
* body.
* @throws TransformerException Thrown if there was an error creating the
* DOM transformer.
* @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.
* @return A standard service response including an HTTP status code and
* request ID.
*/
@Override
public OperationResponse beginSwapping(String serviceName, DeploymentSwapParameters parameters)
        throws ParserConfigurationException, SAXException, TransformerException, IOException, ServiceException {
    // Validate
    if (serviceName == null) {
        throw new NullPointerException("serviceName");
    }
    // TODO: Validate serviceName is a valid DNS name.
    if (parameters == null) {
        throw new NullPointerException("parameters");
    }
    if (parameters.getSourceDeployment() == null) {
        throw new NullPointerException("parameters.SourceDeployment");
    }

    // 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("serviceName", serviceName);
        tracingParameters.put("parameters", parameters);
        CloudTracing.enter(invocationId, this, "beginSwappingAsync", tracingParameters);
    }

    // Construct URL
    String url = "";
    url = url + "/";
    if (this.getClient().getCredentials().getSubscriptionId() != null) {
        url = url + URLEncoder.encode(this.getClient().getCredentials().getSubscriptionId(), "UTF-8");
    }
    url = url + "/services/hostedservices/";
    url = url + URLEncoder.encode(serviceName, "UTF-8");
    String baseUrl = this.getClient().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
    HttpPost httpRequest = new HttpPost(url);

    // Set Headers
    httpRequest.setHeader("Content-Type", "application/xml");
    httpRequest.setHeader("x-ms-version", "2015-04-01");

    // Serialize Request
    String requestContent = null;
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document requestDoc = documentBuilder.newDocument();

    Element swapElement = requestDoc.createElementNS("http://schemas.microsoft.com/windowsazure", "Swap");
    requestDoc.appendChild(swapElement);

    if (parameters.getProductionDeployment() != null) {
        Element productionElement = requestDoc.createElementNS("http://schemas.microsoft.com/windowsazure",
                "Production");
        productionElement.appendChild(requestDoc.createTextNode(parameters.getProductionDeployment()));
        swapElement.appendChild(productionElement);
    }

    Element sourceDeploymentElement = requestDoc.createElementNS("http://schemas.microsoft.com/windowsazure",
            "SourceDeployment");
    sourceDeploymentElement.appendChild(requestDoc.createTextNode(parameters.getSourceDeployment()));
    swapElement.appendChild(sourceDeploymentElement);

    DOMSource domSource = new DOMSource(requestDoc);
    StringWriter stringWriter = new StringWriter();
    StreamResult streamResult = new StreamResult(stringWriter);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.transform(domSource, streamResult);
    requestContent = stringWriter.toString();
    StringEntity entity = new StringEntity(requestContent);
    httpRequest.setEntity(entity);
    httpRequest.setHeader("Content-Type", "application/xml");

    // Send Request
    HttpResponse httpResponse = null;
    try {
        if (shouldTrace) {
            CloudTracing.sendRequest(invocationId, httpRequest);
        }
        httpResponse = this.getClient().getHttpClient().execute(httpRequest);
        if (shouldTrace) {
            CloudTracing.receiveResponse(invocationId, httpResponse);
        }
        int statusCode = httpResponse.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_ACCEPTED) {
            ServiceException ex = ServiceException.createFromXml(httpRequest, requestContent, httpResponse,
                    httpResponse.getEntity());
            if (shouldTrace) {
                CloudTracing.error(invocationId, ex);
            }
            throw ex;
        }

        // Create Result
        OperationResponse result = null;
        // Deserialize Response
        result = new OperationResponse();
        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();
        }
    }
}

From source file:com.microsoft.windowsazure.management.compute.DeploymentOperationsImpl.java

/**
* The Begin Walking Upgrade Domain By Deployment Name operation specifies
* an update domain in which a role instance must be updated. For more
* information about updating role instances, see Update an Azure Service
* at http://msdn.microsoft.com/en-us/library/windowsazure/hh472157.aspx.
* This operation is an asynchronous operation. To determine whether the
* Management service has finished processing the request, call Get
* Operation Status. For more information on asynchronous operations, see
* Tracking Asynchronous Service Management Requests at
* http://msdn.microsoft.com/en-us/library/windowsazure/ee460791.aspx.
* Prior to calling the Walk Upgrade Domain operation you must have called
* Upgrade Deployment, Change Deployment Configuration, or Rollback Update
* Or Upgrade. By default, a service is deployed with five update domains,
* which are updated one at a time during an in-place update. For
* information on modifying the number of update domains in the service
* definition file, see the Azure Service Definition Schema (.csdef File).
* To perform a manual update of your deployment, proceed in this order:
* Call Upgrade Deployment with the Mode element set to manual. Call Walk
* Upgrade Domain to update each domain within the deployment. Update
* domains must be updated in order. For example, begin with domain 0,
* proceed to domain 1, and so on. Important: An update that adds or
* removes role instances will result in a configuration update to all
* roles that are deployed in the cloud service. Existing role instances
* need to be notified of new role instances so that all role instances can
* communicate together in the cloud service. While an update is in
* progress, call Get Deployment to determine its status. If the update is
* in progress, Get Deployment returns an UpgradeStatus element that
* contains information about the update. If the update is complete, or if
* no update is in progress, then the UpgradeStatus element is null.  (see
* http://msdn.microsoft.com/en-us/library/windowsazure/ee460800.aspx for
* more information)//from  w  w  w .ja  v  a  2 s. co  m
*
* @param serviceName Required. The name of the cloud service.
* @param deploymentName Required. The name of your deployment.
* @param parameters Required. Parameters supplied to the Begin Walking
* Upgrade Domain By Deployment Name operation.
* @throws ParserConfigurationException Thrown if there was an error
* configuring the parser for the response body.
* @throws SAXException Thrown if there was an error parsing the response
* body.
* @throws TransformerException Thrown if there was an error creating the
* DOM transformer.
* @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.
* @return A standard service response including an HTTP status code and
* request ID.
*/
@Override
public OperationResponse beginWalkingUpgradeDomainByDeploymentName(String serviceName, String deploymentName,
        DeploymentWalkUpgradeDomainParameters parameters)
        throws ParserConfigurationException, SAXException, TransformerException, IOException, ServiceException {
    // Validate
    if (serviceName == null) {
        throw new NullPointerException("serviceName");
    }
    if (deploymentName == null) {
        throw new NullPointerException("deploymentName");
    }
    if (parameters == null) {
        throw new NullPointerException("parameters");
    }

    // 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("serviceName", serviceName);
        tracingParameters.put("deploymentName", deploymentName);
        tracingParameters.put("parameters", parameters);
        CloudTracing.enter(invocationId, this, "beginWalkingUpgradeDomainByDeploymentNameAsync",
                tracingParameters);
    }

    // Construct URL
    String url = "";
    url = url + "/";
    if (this.getClient().getCredentials().getSubscriptionId() != null) {
        url = url + URLEncoder.encode(this.getClient().getCredentials().getSubscriptionId(), "UTF-8");
    }
    url = url + "/services/hostedservices/";
    url = url + URLEncoder.encode(serviceName, "UTF-8");
    url = url + "/deployments/";
    url = url + URLEncoder.encode(deploymentName, "UTF-8");
    ArrayList<String> queryParameters = new ArrayList<String>();
    queryParameters.add("comp=walkupgradedomain");
    if (queryParameters.size() > 0) {
        url = url + "?" + CollectionStringBuilder.join(queryParameters, "&");
    }
    String baseUrl = this.getClient().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
    HttpPost httpRequest = new HttpPost(url);

    // Set Headers
    httpRequest.setHeader("Content-Type", "application/xml");
    httpRequest.setHeader("x-ms-version", "2015-04-01");

    // Serialize Request
    String requestContent = null;
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document requestDoc = documentBuilder.newDocument();

    Element walkUpgradeDomainElement = requestDoc.createElementNS("http://schemas.microsoft.com/windowsazure",
            "WalkUpgradeDomain");
    requestDoc.appendChild(walkUpgradeDomainElement);

    Element upgradeDomainElement = requestDoc.createElementNS("http://schemas.microsoft.com/windowsazure",
            "UpgradeDomain");
    upgradeDomainElement
            .appendChild(requestDoc.createTextNode(Integer.toString(parameters.getUpgradeDomain())));
    walkUpgradeDomainElement.appendChild(upgradeDomainElement);

    DOMSource domSource = new DOMSource(requestDoc);
    StringWriter stringWriter = new StringWriter();
    StreamResult streamResult = new StreamResult(stringWriter);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.transform(domSource, streamResult);
    requestContent = stringWriter.toString();
    StringEntity entity = new StringEntity(requestContent);
    httpRequest.setEntity(entity);
    httpRequest.setHeader("Content-Type", "application/xml");

    // Send Request
    HttpResponse httpResponse = null;
    try {
        if (shouldTrace) {
            CloudTracing.sendRequest(invocationId, httpRequest);
        }
        httpResponse = this.getClient().getHttpClient().execute(httpRequest);
        if (shouldTrace) {
            CloudTracing.receiveResponse(invocationId, httpResponse);
        }
        int statusCode = httpResponse.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_ACCEPTED) {
            ServiceException ex = ServiceException.createFromXml(httpRequest, requestContent, httpResponse,
                    httpResponse.getEntity());
            if (shouldTrace) {
                CloudTracing.error(invocationId, ex);
            }
            throw ex;
        }

        // Create Result
        OperationResponse result = null;
        // Deserialize Response
        result = new OperationResponse();
        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();
        }
    }
}