Example usage for javax.xml.bind DatatypeConverter parseBoolean

List of usage examples for javax.xml.bind DatatypeConverter parseBoolean

Introduction

In this page you can find the example usage for javax.xml.bind DatatypeConverter parseBoolean.

Prototype

public static boolean parseBoolean(String lexicalXSDBoolean) 

Source Link

Document

Converts the string argument into a boolean value.

Usage

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

/**
* The Get Disk operation retrieves a disk from the user image repository.
* The disk can be an operating system disk or a data disk.  (see
* http://msdn.microsoft.com/en-us/library/windowsazure/jj157178.aspx for
* more information)//from  w w  w.  j  a  v  a 2  s  . c o m
*
* @param name Required. The name of the disk.
* @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.
* @throws URISyntaxException Thrown if there was an error parsing a URI in
* the response.
* @return A virtual machine disk associated with your subscription.
*/
@Override
public VirtualMachineDiskGetResponse getDisk(String name)
        throws IOException, ServiceException, ParserConfigurationException, SAXException, URISyntaxException {
    // Validate
    if (name == null) {
        throw new NullPointerException("name");
    }

    // 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("name", name);
        CloudTracing.enter(invocationId, this, "getDiskAsync", 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/disks/";
    url = url + URLEncoder.encode(name, "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
    HttpGet httpRequest = new HttpGet(url);

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

    // 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, null, httpResponse,
                    httpResponse.getEntity());
            if (shouldTrace) {
                CloudTracing.error(invocationId, ex);
            }
            throw ex;
        }

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

            Element diskElement = XmlUtility.getElementByTagNameNS(responseDoc,
                    "http://schemas.microsoft.com/windowsazure", "Disk");
            if (diskElement != null) {
                Element affinityGroupElement = XmlUtility.getElementByTagNameNS(diskElement,
                        "http://schemas.microsoft.com/windowsazure", "AffinityGroup");
                if (affinityGroupElement != null) {
                    String affinityGroupInstance;
                    affinityGroupInstance = affinityGroupElement.getTextContent();
                    result.setAffinityGroup(affinityGroupInstance);
                }

                Element locationElement = XmlUtility.getElementByTagNameNS(diskElement,
                        "http://schemas.microsoft.com/windowsazure", "Location");
                if (locationElement != null) {
                    String locationInstance;
                    locationInstance = locationElement.getTextContent();
                    result.setLocation(locationInstance);
                }

                Element labelElement = XmlUtility.getElementByTagNameNS(diskElement,
                        "http://schemas.microsoft.com/windowsazure", "Label");
                if (labelElement != null) {
                    String labelInstance;
                    labelInstance = labelElement.getTextContent();
                    result.setLabel(labelInstance);
                }

                Element logicalDiskSizeInGBElement = XmlUtility.getElementByTagNameNS(diskElement,
                        "http://schemas.microsoft.com/windowsazure", "LogicalDiskSizeInGB");
                if (logicalDiskSizeInGBElement != null) {
                    int logicalDiskSizeInGBInstance;
                    logicalDiskSizeInGBInstance = DatatypeConverter
                            .parseInt(logicalDiskSizeInGBElement.getTextContent());
                    result.setLogicalSizeInGB(logicalDiskSizeInGBInstance);
                }

                Element mediaLinkElement = XmlUtility.getElementByTagNameNS(diskElement,
                        "http://schemas.microsoft.com/windowsazure", "MediaLink");
                if (mediaLinkElement != null) {
                    URI mediaLinkInstance;
                    mediaLinkInstance = new URI(mediaLinkElement.getTextContent());
                    result.setMediaLinkUri(mediaLinkInstance);
                }

                Element nameElement = XmlUtility.getElementByTagNameNS(diskElement,
                        "http://schemas.microsoft.com/windowsazure", "Name");
                if (nameElement != null) {
                    String nameInstance;
                    nameInstance = nameElement.getTextContent();
                    result.setName(nameInstance);
                }

                Element osElement = XmlUtility.getElementByTagNameNS(diskElement,
                        "http://schemas.microsoft.com/windowsazure", "OS");
                if (osElement != null) {
                    String osInstance;
                    osInstance = osElement.getTextContent();
                    result.setOperatingSystemType(osInstance);
                }

                Element sourceImageNameElement = XmlUtility.getElementByTagNameNS(diskElement,
                        "http://schemas.microsoft.com/windowsazure", "SourceImageName");
                if (sourceImageNameElement != null) {
                    String sourceImageNameInstance;
                    sourceImageNameInstance = sourceImageNameElement.getTextContent();
                    result.setSourceImageName(sourceImageNameInstance);
                }

                Element attachedToElement = XmlUtility.getElementByTagNameNS(diskElement,
                        "http://schemas.microsoft.com/windowsazure", "AttachedTo");
                if (attachedToElement != null) {
                    VirtualMachineDiskGetResponse.VirtualMachineDiskUsageDetails attachedToInstance = new VirtualMachineDiskGetResponse.VirtualMachineDiskUsageDetails();
                    result.setUsageDetails(attachedToInstance);

                    Element hostedServiceNameElement = XmlUtility.getElementByTagNameNS(attachedToElement,
                            "http://schemas.microsoft.com/windowsazure", "HostedServiceName");
                    if (hostedServiceNameElement != null) {
                        String hostedServiceNameInstance;
                        hostedServiceNameInstance = hostedServiceNameElement.getTextContent();
                        attachedToInstance.setHostedServiceName(hostedServiceNameInstance);
                    }

                    Element deploymentNameElement = XmlUtility.getElementByTagNameNS(attachedToElement,
                            "http://schemas.microsoft.com/windowsazure", "DeploymentName");
                    if (deploymentNameElement != null) {
                        String deploymentNameInstance;
                        deploymentNameInstance = deploymentNameElement.getTextContent();
                        attachedToInstance.setDeploymentName(deploymentNameInstance);
                    }

                    Element roleNameElement = XmlUtility.getElementByTagNameNS(attachedToElement,
                            "http://schemas.microsoft.com/windowsazure", "RoleName");
                    if (roleNameElement != null) {
                        String roleNameInstance;
                        roleNameInstance = roleNameElement.getTextContent();
                        attachedToInstance.setRoleName(roleNameInstance);
                    }
                }

                Element isCorruptedElement = XmlUtility.getElementByTagNameNS(diskElement,
                        "http://schemas.microsoft.com/windowsazure", "IsCorrupted");
                if (isCorruptedElement != null && isCorruptedElement.getTextContent() != null
                        && !isCorruptedElement.getTextContent().isEmpty()) {
                    boolean isCorruptedInstance;
                    isCorruptedInstance = DatatypeConverter
                            .parseBoolean(isCorruptedElement.getTextContent().toLowerCase());
                    result.setIsCorrupted(isCorruptedInstance);
                }

                Element isPremiumElement = XmlUtility.getElementByTagNameNS(diskElement,
                        "http://schemas.microsoft.com/windowsazure", "IsPremium");
                if (isPremiumElement != null && isPremiumElement.getTextContent() != null
                        && !isPremiumElement.getTextContent().isEmpty()) {
                    boolean isPremiumInstance;
                    isPremiumInstance = DatatypeConverter
                            .parseBoolean(isPremiumElement.getTextContent().toLowerCase());
                    result.setIsPremium(isPremiumInstance);
                }

                Element iOTypeElement = XmlUtility.getElementByTagNameNS(diskElement,
                        "http://schemas.microsoft.com/windowsazure", "IOType");
                if (iOTypeElement != null) {
                    String iOTypeInstance;
                    iOTypeInstance = iOTypeElement.getTextContent();
                    result.setIOType(iOTypeInstance);
                }
            }

        }
        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.VirtualMachineOSImageOperationsImpl.java

/**
* The List OS Images operation retrieves a list of the operating system
* images from the image repository.  (see
* http://msdn.microsoft.com/en-us/library/windowsazure/jj157191.aspx for
* more information)/*from   w w w. j a v  a 2  s  . co  m*/
*
* @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.
* @throws URISyntaxException Thrown if there was an error parsing a URI in
* the response.
* @return The List OS Images operation response.
*/
@Override
public VirtualMachineOSImageListResponse list()
        throws IOException, ServiceException, ParserConfigurationException, SAXException, URISyntaxException {
    // 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, "listAsync", 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/images";
    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
    HttpGet httpRequest = new HttpGet(url);

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

    // 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, null, httpResponse,
                    httpResponse.getEntity());
            if (shouldTrace) {
                CloudTracing.error(invocationId, ex);
            }
            throw ex;
        }

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

            Element imagesSequenceElement = XmlUtility.getElementByTagNameNS(responseDoc,
                    "http://schemas.microsoft.com/windowsazure", "Images");
            if (imagesSequenceElement != null) {
                for (int i1 = 0; i1 < com.microsoft.windowsazure.core.utils.XmlUtility
                        .getElementsByTagNameNS(imagesSequenceElement,
                                "http://schemas.microsoft.com/windowsazure", "OSImage")
                        .size(); i1 = i1 + 1) {
                    org.w3c.dom.Element imagesElement = ((org.w3c.dom.Element) com.microsoft.windowsazure.core.utils.XmlUtility
                            .getElementsByTagNameNS(imagesSequenceElement,
                                    "http://schemas.microsoft.com/windowsazure", "OSImage")
                            .get(i1));
                    VirtualMachineOSImageListResponse.VirtualMachineOSImage oSImageInstance = new VirtualMachineOSImageListResponse.VirtualMachineOSImage();
                    result.getImages().add(oSImageInstance);

                    Element affinityGroupElement = XmlUtility.getElementByTagNameNS(imagesElement,
                            "http://schemas.microsoft.com/windowsazure", "AffinityGroup");
                    if (affinityGroupElement != null) {
                        String affinityGroupInstance;
                        affinityGroupInstance = affinityGroupElement.getTextContent();
                        oSImageInstance.setAffinityGroup(affinityGroupInstance);
                    }

                    Element categoryElement = XmlUtility.getElementByTagNameNS(imagesElement,
                            "http://schemas.microsoft.com/windowsazure", "Category");
                    if (categoryElement != null) {
                        String categoryInstance;
                        categoryInstance = categoryElement.getTextContent();
                        oSImageInstance.setCategory(categoryInstance);
                    }

                    Element labelElement = XmlUtility.getElementByTagNameNS(imagesElement,
                            "http://schemas.microsoft.com/windowsazure", "Label");
                    if (labelElement != null) {
                        String labelInstance;
                        labelInstance = labelElement.getTextContent();
                        oSImageInstance.setLabel(labelInstance);
                    }

                    Element locationElement = XmlUtility.getElementByTagNameNS(imagesElement,
                            "http://schemas.microsoft.com/windowsazure", "Location");
                    if (locationElement != null) {
                        String locationInstance;
                        locationInstance = locationElement.getTextContent();
                        oSImageInstance.setLocation(locationInstance);
                    }

                    Element logicalSizeInGBElement = XmlUtility.getElementByTagNameNS(imagesElement,
                            "http://schemas.microsoft.com/windowsazure", "LogicalSizeInGB");
                    if (logicalSizeInGBElement != null) {
                        double logicalSizeInGBInstance;
                        logicalSizeInGBInstance = DatatypeConverter
                                .parseDouble(logicalSizeInGBElement.getTextContent());
                        oSImageInstance.setLogicalSizeInGB(logicalSizeInGBInstance);
                    }

                    Element mediaLinkElement = XmlUtility.getElementByTagNameNS(imagesElement,
                            "http://schemas.microsoft.com/windowsazure", "MediaLink");
                    if (mediaLinkElement != null) {
                        URI mediaLinkInstance;
                        mediaLinkInstance = new URI(mediaLinkElement.getTextContent());
                        oSImageInstance.setMediaLinkUri(mediaLinkInstance);
                    }

                    Element nameElement = XmlUtility.getElementByTagNameNS(imagesElement,
                            "http://schemas.microsoft.com/windowsazure", "Name");
                    if (nameElement != null) {
                        String nameInstance;
                        nameInstance = nameElement.getTextContent();
                        oSImageInstance.setName(nameInstance);
                    }

                    Element osElement = XmlUtility.getElementByTagNameNS(imagesElement,
                            "http://schemas.microsoft.com/windowsazure", "OS");
                    if (osElement != null) {
                        String osInstance;
                        osInstance = osElement.getTextContent();
                        oSImageInstance.setOperatingSystemType(osInstance);
                    }

                    Element eulaElement = XmlUtility.getElementByTagNameNS(imagesElement,
                            "http://schemas.microsoft.com/windowsazure", "Eula");
                    if (eulaElement != null) {
                        String eulaInstance;
                        eulaInstance = eulaElement.getTextContent();
                        oSImageInstance.setEula(eulaInstance);
                    }

                    Element descriptionElement = XmlUtility.getElementByTagNameNS(imagesElement,
                            "http://schemas.microsoft.com/windowsazure", "Description");
                    if (descriptionElement != null) {
                        String descriptionInstance;
                        descriptionInstance = descriptionElement.getTextContent();
                        oSImageInstance.setDescription(descriptionInstance);
                    }

                    Element imageFamilyElement = XmlUtility.getElementByTagNameNS(imagesElement,
                            "http://schemas.microsoft.com/windowsazure", "ImageFamily");
                    if (imageFamilyElement != null) {
                        String imageFamilyInstance;
                        imageFamilyInstance = imageFamilyElement.getTextContent();
                        oSImageInstance.setImageFamily(imageFamilyInstance);
                    }

                    Element showInGuiElement = XmlUtility.getElementByTagNameNS(imagesElement,
                            "http://schemas.microsoft.com/windowsazure", "ShowInGui");
                    if (showInGuiElement != null && showInGuiElement.getTextContent() != null
                            && !showInGuiElement.getTextContent().isEmpty()) {
                        boolean showInGuiInstance;
                        showInGuiInstance = DatatypeConverter
                                .parseBoolean(showInGuiElement.getTextContent().toLowerCase());
                        oSImageInstance.setShowInGui(showInGuiInstance);
                    }

                    Element publishedDateElement = XmlUtility.getElementByTagNameNS(imagesElement,
                            "http://schemas.microsoft.com/windowsazure", "PublishedDate");
                    if (publishedDateElement != null) {
                        Calendar publishedDateInstance;
                        publishedDateInstance = DatatypeConverter
                                .parseDateTime(publishedDateElement.getTextContent());
                        oSImageInstance.setPublishedDate(publishedDateInstance);
                    }

                    Element isPremiumElement = XmlUtility.getElementByTagNameNS(imagesElement,
                            "http://schemas.microsoft.com/windowsazure", "IsPremium");
                    if (isPremiumElement != null && isPremiumElement.getTextContent() != null
                            && !isPremiumElement.getTextContent().isEmpty()) {
                        boolean isPremiumInstance;
                        isPremiumInstance = DatatypeConverter
                                .parseBoolean(isPremiumElement.getTextContent().toLowerCase());
                        oSImageInstance.setIsPremium(isPremiumInstance);
                    }

                    Element privacyUriElement = XmlUtility.getElementByTagNameNS(imagesElement,
                            "http://schemas.microsoft.com/windowsazure", "PrivacyUri");
                    if (privacyUriElement != null) {
                        URI privacyUriInstance;
                        privacyUriInstance = new URI(privacyUriElement.getTextContent());
                        oSImageInstance.setPrivacyUri(privacyUriInstance);
                    }

                    Element recommendedVMSizeElement = XmlUtility.getElementByTagNameNS(imagesElement,
                            "http://schemas.microsoft.com/windowsazure", "RecommendedVMSize");
                    if (recommendedVMSizeElement != null) {
                        String recommendedVMSizeInstance;
                        recommendedVMSizeInstance = recommendedVMSizeElement.getTextContent();
                        oSImageInstance.setRecommendedVMSize(recommendedVMSizeInstance);
                    }

                    Element publisherNameElement = XmlUtility.getElementByTagNameNS(imagesElement,
                            "http://schemas.microsoft.com/windowsazure", "PublisherName");
                    if (publisherNameElement != null) {
                        String publisherNameInstance;
                        publisherNameInstance = publisherNameElement.getTextContent();
                        oSImageInstance.setPublisherName(publisherNameInstance);
                    }

                    Element pricingDetailLinkElement = XmlUtility.getElementByTagNameNS(imagesElement,
                            "http://schemas.microsoft.com/windowsazure", "PricingDetailLink");
                    if (pricingDetailLinkElement != null) {
                        URI pricingDetailLinkInstance;
                        pricingDetailLinkInstance = new URI(pricingDetailLinkElement.getTextContent());
                        oSImageInstance.setPricingDetailUri(pricingDetailLinkInstance);
                    }

                    Element iconUriElement = XmlUtility.getElementByTagNameNS(imagesElement,
                            "http://schemas.microsoft.com/windowsazure", "IconUri");
                    if (iconUriElement != null) {
                        String iconUriInstance;
                        iconUriInstance = iconUriElement.getTextContent();
                        oSImageInstance.setIconUri(iconUriInstance);
                    }

                    Element smallIconUriElement = XmlUtility.getElementByTagNameNS(imagesElement,
                            "http://schemas.microsoft.com/windowsazure", "SmallIconUri");
                    if (smallIconUriElement != null) {
                        String smallIconUriInstance;
                        smallIconUriInstance = smallIconUriElement.getTextContent();
                        oSImageInstance.setSmallIconUri(smallIconUriInstance);
                    }

                    Element languageElement = XmlUtility.getElementByTagNameNS(imagesElement,
                            "http://schemas.microsoft.com/windowsazure", "Language");
                    if (languageElement != null) {
                        String languageInstance;
                        languageInstance = languageElement.getTextContent();
                        oSImageInstance.setLanguage(languageInstance);
                    }

                    Element iOTypeElement = XmlUtility.getElementByTagNameNS(imagesElement,
                            "http://schemas.microsoft.com/windowsazure", "IOType");
                    if (iOTypeElement != null) {
                        String iOTypeInstance;
                        iOTypeInstance = iOTypeElement.getTextContent();
                        oSImageInstance.setIOType(iOTypeInstance);
                    }
                }
            }

        }
        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.servicebus.TopicOperationsImpl.java

/**
* Enumerates the topics in the service namespace. An empty feed is returned
* if no topic exists in the service namespace.  (see
* http://msdn.microsoft.com/en-us/library/windowsazure/hh780744.aspx for
* more information)//from  w  w  w.j  a  v a2s. c  om
*
* @param namespaceName Required. The namespace name.
* @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.
* @throws URISyntaxException Thrown if there was an error parsing a URI in
* the response.
* @return A response to a request for a list of topics.
*/
@Override
public ServiceBusTopicsResponse list(String namespaceName)
        throws IOException, ServiceException, ParserConfigurationException, SAXException, URISyntaxException {
    // Validate
    if (namespaceName == null) {
        throw new NullPointerException("namespaceName");
    }

    // 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("namespaceName", namespaceName);
        CloudTracing.enter(invocationId, this, "listAsync", 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/servicebus/namespaces/";
    url = url + URLEncoder.encode(namespaceName, "UTF-8");
    url = url + "/topics/";
    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
    HttpGet httpRequest = new HttpGet(url);

    // Set Headers
    httpRequest.setHeader("Content-Type", "application/xml; charset=utf-8");
    httpRequest.setHeader("x-ms-version", "2013-08-01");

    // 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, null, httpResponse,
                    httpResponse.getEntity());
            if (shouldTrace) {
                CloudTracing.error(invocationId, ex);
            }
            throw ex;
        }

        // Create Result
        ServiceBusTopicsResponse result = null;
        // Deserialize Response
        if (statusCode == HttpStatus.SC_OK) {
            InputStream responseContent = httpResponse.getEntity().getContent();
            result = new ServiceBusTopicsResponse();
            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));
                        ServiceBusTopic entryInstance = new ServiceBusTopic();
                        result.getTopics().add(entryInstance);

                        Element titleElement = XmlUtility.getElementByTagNameNS(entriesElement,
                                "http://www.w3.org/2005/Atom", "title");
                        if (titleElement != null) {
                            String titleInstance;
                            titleInstance = titleElement.getTextContent();
                            entryInstance.setName(titleInstance);
                        }

                        Element contentElement = XmlUtility.getElementByTagNameNS(entriesElement,
                                "http://www.w3.org/2005/Atom", "content");
                        if (contentElement != null) {
                            Element topicDescriptionElement = XmlUtility.getElementByTagNameNS(contentElement,
                                    "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                    "TopicDescription");
                            if (topicDescriptionElement != null) {
                                Element defaultMessageTimeToLiveElement = XmlUtility.getElementByTagNameNS(
                                        topicDescriptionElement,
                                        "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                        "DefaultMessageTimeToLive");
                                if (defaultMessageTimeToLiveElement != null) {
                                    String defaultMessageTimeToLiveInstance;
                                    defaultMessageTimeToLiveInstance = defaultMessageTimeToLiveElement
                                            .getTextContent();
                                    entryInstance.setDefaultMessageTimeToLive(defaultMessageTimeToLiveInstance);
                                }

                                Element maxSizeInMegabytesElement = XmlUtility.getElementByTagNameNS(
                                        topicDescriptionElement,
                                        "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                        "MaxSizeInMegabytes");
                                if (maxSizeInMegabytesElement != null) {
                                    int maxSizeInMegabytesInstance;
                                    maxSizeInMegabytesInstance = DatatypeConverter
                                            .parseInt(maxSizeInMegabytesElement.getTextContent());
                                    entryInstance.setMaxSizeInMegabytes(maxSizeInMegabytesInstance);
                                }

                                Element requiresDuplicateDetectionElement = XmlUtility.getElementByTagNameNS(
                                        topicDescriptionElement,
                                        "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                        "RequiresDuplicateDetection");
                                if (requiresDuplicateDetectionElement != null) {
                                    boolean requiresDuplicateDetectionInstance;
                                    requiresDuplicateDetectionInstance = DatatypeConverter.parseBoolean(
                                            requiresDuplicateDetectionElement.getTextContent().toLowerCase());
                                    entryInstance
                                            .setRequiresDuplicateDetection(requiresDuplicateDetectionInstance);
                                }

                                Element duplicateDetectionHistoryTimeWindowElement = XmlUtility
                                        .getElementByTagNameNS(topicDescriptionElement,
                                                "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                                "DuplicateDetectionHistoryTimeWindow");
                                if (duplicateDetectionHistoryTimeWindowElement != null) {
                                    String duplicateDetectionHistoryTimeWindowInstance;
                                    duplicateDetectionHistoryTimeWindowInstance = duplicateDetectionHistoryTimeWindowElement
                                            .getTextContent();
                                    entryInstance.setDuplicateDetectionHistoryTimeWindow(
                                            duplicateDetectionHistoryTimeWindowInstance);
                                }

                                Element enableBatchedOperationsElement = XmlUtility.getElementByTagNameNS(
                                        topicDescriptionElement,
                                        "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                        "EnableBatchedOperations");
                                if (enableBatchedOperationsElement != null) {
                                    boolean enableBatchedOperationsInstance;
                                    enableBatchedOperationsInstance = DatatypeConverter.parseBoolean(
                                            enableBatchedOperationsElement.getTextContent().toLowerCase());
                                    entryInstance.setEnableBatchedOperations(enableBatchedOperationsInstance);
                                }

                                Element sizeInBytesElement = XmlUtility.getElementByTagNameNS(
                                        topicDescriptionElement,
                                        "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                        "SizeInBytes");
                                if (sizeInBytesElement != null) {
                                    int sizeInBytesInstance;
                                    sizeInBytesInstance = DatatypeConverter
                                            .parseInt(sizeInBytesElement.getTextContent());
                                    entryInstance.setSizeInBytes(sizeInBytesInstance);
                                }

                                Element filteringMessagesBeforePublishingElement = XmlUtility
                                        .getElementByTagNameNS(topicDescriptionElement,
                                                "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                                "FilteringMessagesBeforePublishing");
                                if (filteringMessagesBeforePublishingElement != null) {
                                    boolean filteringMessagesBeforePublishingInstance;
                                    filteringMessagesBeforePublishingInstance = DatatypeConverter
                                            .parseBoolean(filteringMessagesBeforePublishingElement
                                                    .getTextContent().toLowerCase());
                                    entryInstance.setFilteringMessagesBeforePublishing(
                                            filteringMessagesBeforePublishingInstance);
                                }

                                Element isAnonymousAccessibleElement = XmlUtility.getElementByTagNameNS(
                                        topicDescriptionElement,
                                        "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                        "IsAnonymousAccessible");
                                if (isAnonymousAccessibleElement != null) {
                                    boolean isAnonymousAccessibleInstance;
                                    isAnonymousAccessibleInstance = DatatypeConverter.parseBoolean(
                                            isAnonymousAccessibleElement.getTextContent().toLowerCase());
                                    entryInstance.setIsAnonymousAccessible(isAnonymousAccessibleInstance);
                                }

                                Element authorizationRulesSequenceElement = XmlUtility.getElementByTagNameNS(
                                        topicDescriptionElement,
                                        "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                        "AuthorizationRules");
                                if (authorizationRulesSequenceElement != null) {
                                    for (int i2 = 0; i2 < com.microsoft.windowsazure.core.utils.XmlUtility
                                            .getElementsByTagNameNS(authorizationRulesSequenceElement,
                                                    "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                                    "AuthorizationRule")
                                            .size(); i2 = i2 + 1) {
                                        org.w3c.dom.Element authorizationRulesElement = ((org.w3c.dom.Element) com.microsoft.windowsazure.core.utils.XmlUtility
                                                .getElementsByTagNameNS(authorizationRulesSequenceElement,
                                                        "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                                        "AuthorizationRule")
                                                .get(i2));
                                        ServiceBusSharedAccessAuthorizationRule authorizationRuleInstance = new ServiceBusSharedAccessAuthorizationRule();
                                        entryInstance.getAuthorizationRules().add(authorizationRuleInstance);

                                        Element claimTypeElement = XmlUtility.getElementByTagNameNS(
                                                authorizationRulesElement,
                                                "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                                "ClaimType");
                                        if (claimTypeElement != null) {
                                            String claimTypeInstance;
                                            claimTypeInstance = claimTypeElement.getTextContent();
                                            authorizationRuleInstance.setClaimType(claimTypeInstance);
                                        }

                                        Element claimValueElement = XmlUtility.getElementByTagNameNS(
                                                authorizationRulesElement,
                                                "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                                "ClaimValue");
                                        if (claimValueElement != null) {
                                            String claimValueInstance;
                                            claimValueInstance = claimValueElement.getTextContent();
                                            authorizationRuleInstance.setClaimValue(claimValueInstance);
                                        }

                                        Element rightsSequenceElement = XmlUtility.getElementByTagNameNS(
                                                authorizationRulesElement,
                                                "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                                "Rights");
                                        if (rightsSequenceElement != null) {
                                            for (int i3 = 0; i3 < com.microsoft.windowsazure.core.utils.XmlUtility
                                                    .getElementsByTagNameNS(rightsSequenceElement,
                                                            "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                                            "AccessRights")
                                                    .size(); i3 = i3 + 1) {
                                                org.w3c.dom.Element rightsElement = ((org.w3c.dom.Element) com.microsoft.windowsazure.core.utils.XmlUtility
                                                        .getElementsByTagNameNS(rightsSequenceElement,
                                                                "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                                                "AccessRights")
                                                        .get(i3));
                                                authorizationRuleInstance.getRights().add(
                                                        AccessRight.valueOf(rightsElement.getTextContent()));
                                            }
                                        }

                                        Element createdTimeElement = XmlUtility.getElementByTagNameNS(
                                                authorizationRulesElement,
                                                "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                                "CreatedTime");
                                        if (createdTimeElement != null) {
                                            Calendar createdTimeInstance;
                                            createdTimeInstance = DatatypeConverter
                                                    .parseDateTime(createdTimeElement.getTextContent());
                                            authorizationRuleInstance.setCreatedTime(createdTimeInstance);
                                        }

                                        Element keyNameElement = XmlUtility.getElementByTagNameNS(
                                                authorizationRulesElement,
                                                "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                                "KeyName");
                                        if (keyNameElement != null) {
                                            String keyNameInstance;
                                            keyNameInstance = keyNameElement.getTextContent();
                                            authorizationRuleInstance.setKeyName(keyNameInstance);
                                        }

                                        Element modifiedTimeElement = XmlUtility.getElementByTagNameNS(
                                                authorizationRulesElement,
                                                "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                                "ModifiedTime");
                                        if (modifiedTimeElement != null) {
                                            Calendar modifiedTimeInstance;
                                            modifiedTimeInstance = DatatypeConverter
                                                    .parseDateTime(modifiedTimeElement.getTextContent());
                                            authorizationRuleInstance.setModifiedTime(modifiedTimeInstance);
                                        }

                                        Element primaryKeyElement = XmlUtility.getElementByTagNameNS(
                                                authorizationRulesElement,
                                                "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                                "PrimaryKey");
                                        if (primaryKeyElement != null) {
                                            String primaryKeyInstance;
                                            primaryKeyInstance = primaryKeyElement.getTextContent();
                                            authorizationRuleInstance.setPrimaryKey(primaryKeyInstance);
                                        }

                                        Element secondaryKeyElement = XmlUtility.getElementByTagNameNS(
                                                authorizationRulesElement,
                                                "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                                "SecondaryKey");
                                        if (secondaryKeyElement != null) {
                                            String secondaryKeyInstance;
                                            secondaryKeyInstance = secondaryKeyElement.getTextContent();
                                            authorizationRuleInstance.setSecondaryKey(secondaryKeyInstance);
                                        }
                                    }
                                }

                                Element statusElement = XmlUtility.getElementByTagNameNS(
                                        topicDescriptionElement,
                                        "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                        "Status");
                                if (statusElement != null) {
                                    String statusInstance;
                                    statusInstance = statusElement.getTextContent();
                                    entryInstance.setStatus(statusInstance);
                                }

                                Element createdAtElement = XmlUtility.getElementByTagNameNS(
                                        topicDescriptionElement,
                                        "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                        "CreatedAt");
                                if (createdAtElement != null) {
                                    Calendar createdAtInstance;
                                    createdAtInstance = DatatypeConverter
                                            .parseDateTime(createdAtElement.getTextContent());
                                    entryInstance.setCreatedAt(createdAtInstance);
                                }

                                Element updatedAtElement = XmlUtility.getElementByTagNameNS(
                                        topicDescriptionElement,
                                        "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                        "UpdatedAt");
                                if (updatedAtElement != null) {
                                    Calendar updatedAtInstance;
                                    updatedAtInstance = DatatypeConverter
                                            .parseDateTime(updatedAtElement.getTextContent());
                                    entryInstance.setUpdatedAt(updatedAtInstance);
                                }

                                Element accessedAtElement = XmlUtility.getElementByTagNameNS(
                                        topicDescriptionElement,
                                        "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                        "AccessedAt");
                                if (accessedAtElement != null) {
                                    Calendar accessedAtInstance;
                                    accessedAtInstance = DatatypeConverter
                                            .parseDateTime(accessedAtElement.getTextContent());
                                    entryInstance.setAccessedAt(accessedAtInstance);
                                }

                                Element supportOrderingElement = XmlUtility.getElementByTagNameNS(
                                        topicDescriptionElement,
                                        "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                        "SupportOrdering");
                                if (supportOrderingElement != null) {
                                    boolean supportOrderingInstance;
                                    supportOrderingInstance = DatatypeConverter.parseBoolean(
                                            supportOrderingElement.getTextContent().toLowerCase());
                                    entryInstance.setSupportOrdering(supportOrderingInstance);
                                }

                                Element countDetailsElement = XmlUtility.getElementByTagNameNS(
                                        topicDescriptionElement,
                                        "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                        "CountDetails");
                                if (countDetailsElement != null) {
                                    CountDetails countDetailsInstance = new CountDetails();
                                    entryInstance.setCountDetails(countDetailsInstance);
                                }

                                Element subscriptionCountElement = XmlUtility.getElementByTagNameNS(
                                        topicDescriptionElement,
                                        "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                        "SubscriptionCount");
                                if (subscriptionCountElement != null) {
                                    int subscriptionCountInstance;
                                    subscriptionCountInstance = DatatypeConverter
                                            .parseInt(subscriptionCountElement.getTextContent());
                                    entryInstance.setSubscriptionCount(subscriptionCountInstance);
                                }

                                Element autoDeleteOnIdleElement = XmlUtility.getElementByTagNameNS(
                                        topicDescriptionElement,
                                        "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                        "AutoDeleteOnIdle");
                                if (autoDeleteOnIdleElement != null) {
                                    String autoDeleteOnIdleInstance;
                                    autoDeleteOnIdleInstance = autoDeleteOnIdleElement.getTextContent();
                                    entryInstance.setAutoDeleteOnIdle(autoDeleteOnIdleInstance);
                                }

                                Element entityAvailabilityStatusElement = XmlUtility.getElementByTagNameNS(
                                        topicDescriptionElement,
                                        "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                        "EntityAvailabilityStatus");
                                if (entityAvailabilityStatusElement != null) {
                                    String entityAvailabilityStatusInstance;
                                    entityAvailabilityStatusInstance = entityAvailabilityStatusElement
                                            .getTextContent();
                                    entryInstance.setEntityAvailabilityStatus(entityAvailabilityStatusInstance);
                                }
                            }
                        }
                    }
                }
            }

        }
        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.VirtualMachineDiskOperationsImpl.java

/**
* The List Disks operation retrieves a list of the disks in your image
* repository.  (see/*from  ww w. ja  va  2s .c om*/
* http://msdn.microsoft.com/en-us/library/windowsazure/jj157176.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.
* @throws URISyntaxException Thrown if there was an error parsing a URI in
* the response.
* @return The List Disks operation response.
*/
@Override
public VirtualMachineDiskListResponse listDisks()
        throws IOException, ServiceException, ParserConfigurationException, SAXException, URISyntaxException {
    // 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, "listDisksAsync", 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/disks";
    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
    HttpGet httpRequest = new HttpGet(url);

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

    // 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, null, httpResponse,
                    httpResponse.getEntity());
            if (shouldTrace) {
                CloudTracing.error(invocationId, ex);
            }
            throw ex;
        }

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

            Element disksSequenceElement = XmlUtility.getElementByTagNameNS(responseDoc,
                    "http://schemas.microsoft.com/windowsazure", "Disks");
            if (disksSequenceElement != null) {
                for (int i1 = 0; i1 < com.microsoft.windowsazure.core.utils.XmlUtility
                        .getElementsByTagNameNS(disksSequenceElement,
                                "http://schemas.microsoft.com/windowsazure", "Disk")
                        .size(); i1 = i1 + 1) {
                    org.w3c.dom.Element disksElement = ((org.w3c.dom.Element) com.microsoft.windowsazure.core.utils.XmlUtility
                            .getElementsByTagNameNS(disksSequenceElement,
                                    "http://schemas.microsoft.com/windowsazure", "Disk")
                            .get(i1));
                    VirtualMachineDiskListResponse.VirtualMachineDisk diskInstance = new VirtualMachineDiskListResponse.VirtualMachineDisk();
                    result.getDisks().add(diskInstance);

                    Element affinityGroupElement = XmlUtility.getElementByTagNameNS(disksElement,
                            "http://schemas.microsoft.com/windowsazure", "AffinityGroup");
                    if (affinityGroupElement != null) {
                        String affinityGroupInstance;
                        affinityGroupInstance = affinityGroupElement.getTextContent();
                        diskInstance.setAffinityGroup(affinityGroupInstance);
                    }

                    Element locationElement = XmlUtility.getElementByTagNameNS(disksElement,
                            "http://schemas.microsoft.com/windowsazure", "Location");
                    if (locationElement != null) {
                        String locationInstance;
                        locationInstance = locationElement.getTextContent();
                        diskInstance.setLocation(locationInstance);
                    }

                    Element labelElement = XmlUtility.getElementByTagNameNS(disksElement,
                            "http://schemas.microsoft.com/windowsazure", "Label");
                    if (labelElement != null) {
                        String labelInstance;
                        labelInstance = labelElement.getTextContent();
                        diskInstance.setLabel(labelInstance);
                    }

                    Element logicalDiskSizeInGBElement = XmlUtility.getElementByTagNameNS(disksElement,
                            "http://schemas.microsoft.com/windowsazure", "LogicalDiskSizeInGB");
                    if (logicalDiskSizeInGBElement != null) {
                        int logicalDiskSizeInGBInstance;
                        logicalDiskSizeInGBInstance = DatatypeConverter
                                .parseInt(logicalDiskSizeInGBElement.getTextContent());
                        diskInstance.setLogicalSizeInGB(logicalDiskSizeInGBInstance);
                    }

                    Element mediaLinkElement = XmlUtility.getElementByTagNameNS(disksElement,
                            "http://schemas.microsoft.com/windowsazure", "MediaLink");
                    if (mediaLinkElement != null) {
                        URI mediaLinkInstance;
                        mediaLinkInstance = new URI(mediaLinkElement.getTextContent());
                        diskInstance.setMediaLinkUri(mediaLinkInstance);
                    }

                    Element nameElement = XmlUtility.getElementByTagNameNS(disksElement,
                            "http://schemas.microsoft.com/windowsazure", "Name");
                    if (nameElement != null) {
                        String nameInstance;
                        nameInstance = nameElement.getTextContent();
                        diskInstance.setName(nameInstance);
                    }

                    Element osElement = XmlUtility.getElementByTagNameNS(disksElement,
                            "http://schemas.microsoft.com/windowsazure", "OS");
                    if (osElement != null) {
                        String osInstance;
                        osInstance = osElement.getTextContent();
                        diskInstance.setOperatingSystemType(osInstance);
                    }

                    Element sourceImageNameElement = XmlUtility.getElementByTagNameNS(disksElement,
                            "http://schemas.microsoft.com/windowsazure", "SourceImageName");
                    if (sourceImageNameElement != null) {
                        String sourceImageNameInstance;
                        sourceImageNameInstance = sourceImageNameElement.getTextContent();
                        diskInstance.setSourceImageName(sourceImageNameInstance);
                    }

                    Element attachedToElement = XmlUtility.getElementByTagNameNS(disksElement,
                            "http://schemas.microsoft.com/windowsazure", "AttachedTo");
                    if (attachedToElement != null) {
                        VirtualMachineDiskListResponse.VirtualMachineDiskUsageDetails attachedToInstance = new VirtualMachineDiskListResponse.VirtualMachineDiskUsageDetails();
                        diskInstance.setUsageDetails(attachedToInstance);

                        Element hostedServiceNameElement = XmlUtility.getElementByTagNameNS(attachedToElement,
                                "http://schemas.microsoft.com/windowsazure", "HostedServiceName");
                        if (hostedServiceNameElement != null) {
                            String hostedServiceNameInstance;
                            hostedServiceNameInstance = hostedServiceNameElement.getTextContent();
                            attachedToInstance.setHostedServiceName(hostedServiceNameInstance);
                        }

                        Element deploymentNameElement = XmlUtility.getElementByTagNameNS(attachedToElement,
                                "http://schemas.microsoft.com/windowsazure", "DeploymentName");
                        if (deploymentNameElement != null) {
                            String deploymentNameInstance;
                            deploymentNameInstance = deploymentNameElement.getTextContent();
                            attachedToInstance.setDeploymentName(deploymentNameInstance);
                        }

                        Element roleNameElement = XmlUtility.getElementByTagNameNS(attachedToElement,
                                "http://schemas.microsoft.com/windowsazure", "RoleName");
                        if (roleNameElement != null) {
                            String roleNameInstance;
                            roleNameInstance = roleNameElement.getTextContent();
                            attachedToInstance.setRoleName(roleNameInstance);
                        }
                    }

                    Element isCorruptedElement = XmlUtility.getElementByTagNameNS(disksElement,
                            "http://schemas.microsoft.com/windowsazure", "IsCorrupted");
                    if (isCorruptedElement != null && isCorruptedElement.getTextContent() != null
                            && !isCorruptedElement.getTextContent().isEmpty()) {
                        boolean isCorruptedInstance;
                        isCorruptedInstance = DatatypeConverter
                                .parseBoolean(isCorruptedElement.getTextContent().toLowerCase());
                        diskInstance.setIsCorrupted(isCorruptedInstance);
                    }

                    Element isPremiumElement = XmlUtility.getElementByTagNameNS(disksElement,
                            "http://schemas.microsoft.com/windowsazure", "IsPremium");
                    if (isPremiumElement != null && isPremiumElement.getTextContent() != null
                            && !isPremiumElement.getTextContent().isEmpty()) {
                        boolean isPremiumInstance;
                        isPremiumInstance = DatatypeConverter
                                .parseBoolean(isPremiumElement.getTextContent().toLowerCase());
                        diskInstance.setIsPremium(isPremiumInstance);
                    }

                    Element iOTypeElement = XmlUtility.getElementByTagNameNS(disksElement,
                            "http://schemas.microsoft.com/windowsazure", "IOType");
                    if (iOTypeElement != null) {
                        String iOTypeInstance;
                        iOTypeInstance = iOTypeElement.getTextContent();
                        diskInstance.setIOType(iOTypeInstance);
                    }
                }
            }

        }
        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.sql.DatabaseOperationsImpl.java

/**
* Updates the properties of an Azure SQL Database.
*
* @param serverName Required. The name of the Azure SQL Database Server
* where the database is hosted.//from   ww  w. j  a  v  a  2 s. c o  m
* @param databaseName Required. The name of the Azure SQL Database to be
* updated.
* @param parameters Required. The parameters for the Update Database
* 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 Contains the response from a request to Update Database.
*/
@Override
public DatabaseUpdateResponse update(String serverName, String databaseName,
        DatabaseUpdateParameters parameters)
        throws ParserConfigurationException, SAXException, TransformerException, IOException, ServiceException {
    // Validate
    if (serverName == null) {
        throw new NullPointerException("serverName");
    }
    if (databaseName == null) {
        throw new NullPointerException("databaseName");
    }
    if (parameters == null) {
        throw new NullPointerException("parameters");
    }
    if (parameters.getEdition() == null) {
        throw new NullPointerException("parameters.Edition");
    }

    // 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("serverName", serverName);
        tracingParameters.put("databaseName", databaseName);
        tracingParameters.put("parameters", parameters);
        CloudTracing.enter(invocationId, this, "updateAsync", 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/sqlservers/servers/";
    url = url + URLEncoder.encode(serverName, "UTF-8");
    url = url + "/databases/";
    url = url + URLEncoder.encode(databaseName, "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
    HttpPut httpRequest = new HttpPut(url);

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

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

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

    if (parameters.getName() != null) {
        Element nameElement = requestDoc.createElementNS("http://schemas.microsoft.com/windowsazure", "Name");
        nameElement.appendChild(requestDoc.createTextNode(parameters.getName()));
        serviceResourceElement.appendChild(nameElement);
    }

    Element editionElement = requestDoc.createElementNS("http://schemas.microsoft.com/windowsazure", "Edition");
    editionElement.appendChild(requestDoc.createTextNode(parameters.getEdition()));
    serviceResourceElement.appendChild(editionElement);

    if (parameters.getMaximumDatabaseSizeInGB() != null) {
        Element maxSizeGBElement = requestDoc.createElementNS("http://schemas.microsoft.com/windowsazure",
                "MaxSizeGB");
        maxSizeGBElement.appendChild(
                requestDoc.createTextNode(Integer.toString(parameters.getMaximumDatabaseSizeInGB())));
        serviceResourceElement.appendChild(maxSizeGBElement);
    }

    if (parameters.getMaximumDatabaseSizeInBytes() != null) {
        Element maxSizeBytesElement = requestDoc.createElementNS("http://schemas.microsoft.com/windowsazure",
                "MaxSizeBytes");
        maxSizeBytesElement.appendChild(
                requestDoc.createTextNode(Long.toString(parameters.getMaximumDatabaseSizeInBytes())));
        serviceResourceElement.appendChild(maxSizeBytesElement);
    }

    if (parameters.getServiceObjectiveId() != null) {
        Element serviceObjectiveIdElement = requestDoc
                .createElementNS("http://schemas.microsoft.com/windowsazure", "ServiceObjectiveId");
        serviceObjectiveIdElement.appendChild(requestDoc.createTextNode(parameters.getServiceObjectiveId()));
        serviceResourceElement.appendChild(serviceObjectiveIdElement);
    }

    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
        DatabaseUpdateResponse result = null;
        // Deserialize Response
        if (statusCode == HttpStatus.SC_OK) {
            InputStream responseContent = httpResponse.getEntity().getContent();
            result = new DatabaseUpdateResponse();
            DocumentBuilderFactory documentBuilderFactory2 = DocumentBuilderFactory.newInstance();
            documentBuilderFactory2.setNamespaceAware(true);
            DocumentBuilder documentBuilder2 = documentBuilderFactory2.newDocumentBuilder();
            Document responseDoc = documentBuilder2.parse(new BOMInputStream(responseContent));

            Element serviceResourceElement2 = XmlUtility.getElementByTagNameNS(responseDoc,
                    "http://schemas.microsoft.com/windowsazure", "ServiceResource");
            if (serviceResourceElement2 != null) {
                Database serviceResourceInstance = new Database();
                result.setDatabase(serviceResourceInstance);

                Element idElement = XmlUtility.getElementByTagNameNS(serviceResourceElement2,
                        "http://schemas.microsoft.com/windowsazure", "Id");
                if (idElement != null) {
                    int idInstance;
                    idInstance = DatatypeConverter.parseInt(idElement.getTextContent());
                    serviceResourceInstance.setId(idInstance);
                }

                Element editionElement2 = XmlUtility.getElementByTagNameNS(serviceResourceElement2,
                        "http://schemas.microsoft.com/windowsazure", "Edition");
                if (editionElement2 != null) {
                    String editionInstance;
                    editionInstance = editionElement2.getTextContent();
                    serviceResourceInstance.setEdition(editionInstance);
                }

                Element maxSizeGBElement2 = XmlUtility.getElementByTagNameNS(serviceResourceElement2,
                        "http://schemas.microsoft.com/windowsazure", "MaxSizeGB");
                if (maxSizeGBElement2 != null) {
                    int maxSizeGBInstance;
                    maxSizeGBInstance = DatatypeConverter.parseInt(maxSizeGBElement2.getTextContent());
                    serviceResourceInstance.setMaximumDatabaseSizeInGB(maxSizeGBInstance);
                }

                Element maxSizeBytesElement2 = XmlUtility.getElementByTagNameNS(serviceResourceElement2,
                        "http://schemas.microsoft.com/windowsazure", "MaxSizeBytes");
                if (maxSizeBytesElement2 != null) {
                    long maxSizeBytesInstance;
                    maxSizeBytesInstance = DatatypeConverter.parseLong(maxSizeBytesElement2.getTextContent());
                    serviceResourceInstance.setMaximumDatabaseSizeInBytes(maxSizeBytesInstance);
                }

                Element collationNameElement = XmlUtility.getElementByTagNameNS(serviceResourceElement2,
                        "http://schemas.microsoft.com/windowsazure", "CollationName");
                if (collationNameElement != null) {
                    String collationNameInstance;
                    collationNameInstance = collationNameElement.getTextContent();
                    serviceResourceInstance.setCollationName(collationNameInstance);
                }

                Element creationDateElement = XmlUtility.getElementByTagNameNS(serviceResourceElement2,
                        "http://schemas.microsoft.com/windowsazure", "CreationDate");
                if (creationDateElement != null) {
                    Calendar creationDateInstance;
                    creationDateInstance = DatatypeConverter
                            .parseDateTime(creationDateElement.getTextContent());
                    serviceResourceInstance.setCreationDate(creationDateInstance);
                }

                Element isFederationRootElement = XmlUtility.getElementByTagNameNS(serviceResourceElement2,
                        "http://schemas.microsoft.com/windowsazure", "IsFederationRoot");
                if (isFederationRootElement != null) {
                    boolean isFederationRootInstance;
                    isFederationRootInstance = DatatypeConverter
                            .parseBoolean(isFederationRootElement.getTextContent().toLowerCase());
                    serviceResourceInstance.setIsFederationRoot(isFederationRootInstance);
                }

                Element isSystemObjectElement = XmlUtility.getElementByTagNameNS(serviceResourceElement2,
                        "http://schemas.microsoft.com/windowsazure", "IsSystemObject");
                if (isSystemObjectElement != null) {
                    boolean isSystemObjectInstance;
                    isSystemObjectInstance = DatatypeConverter
                            .parseBoolean(isSystemObjectElement.getTextContent().toLowerCase());
                    serviceResourceInstance.setIsSystemObject(isSystemObjectInstance);
                }

                Element sizeMBElement = XmlUtility.getElementByTagNameNS(serviceResourceElement2,
                        "http://schemas.microsoft.com/windowsazure", "SizeMB");
                if (sizeMBElement != null) {
                    String sizeMBInstance;
                    sizeMBInstance = sizeMBElement.getTextContent();
                    serviceResourceInstance.setSizeMB(sizeMBInstance);
                }

                Element serviceObjectiveAssignmentErrorCodeElement = XmlUtility.getElementByTagNameNS(
                        serviceResourceElement2, "http://schemas.microsoft.com/windowsazure",
                        "ServiceObjectiveAssignmentErrorCode");
                if (serviceObjectiveAssignmentErrorCodeElement != null) {
                    String serviceObjectiveAssignmentErrorCodeInstance;
                    serviceObjectiveAssignmentErrorCodeInstance = serviceObjectiveAssignmentErrorCodeElement
                            .getTextContent();
                    serviceResourceInstance.setServiceObjectiveAssignmentErrorCode(
                            serviceObjectiveAssignmentErrorCodeInstance);
                }

                Element serviceObjectiveAssignmentErrorDescriptionElement = XmlUtility.getElementByTagNameNS(
                        serviceResourceElement2, "http://schemas.microsoft.com/windowsazure",
                        "ServiceObjectiveAssignmentErrorDescription");
                if (serviceObjectiveAssignmentErrorDescriptionElement != null) {
                    String serviceObjectiveAssignmentErrorDescriptionInstance;
                    serviceObjectiveAssignmentErrorDescriptionInstance = serviceObjectiveAssignmentErrorDescriptionElement
                            .getTextContent();
                    serviceResourceInstance.setServiceObjectiveAssignmentErrorDescription(
                            serviceObjectiveAssignmentErrorDescriptionInstance);
                }

                Element serviceObjectiveAssignmentStateElement = XmlUtility.getElementByTagNameNS(
                        serviceResourceElement2, "http://schemas.microsoft.com/windowsazure",
                        "ServiceObjectiveAssignmentState");
                if (serviceObjectiveAssignmentStateElement != null) {
                    String serviceObjectiveAssignmentStateInstance;
                    serviceObjectiveAssignmentStateInstance = serviceObjectiveAssignmentStateElement
                            .getTextContent();
                    serviceResourceInstance
                            .setServiceObjectiveAssignmentState(serviceObjectiveAssignmentStateInstance);
                }

                Element serviceObjectiveAssignmentStateDescriptionElement = XmlUtility.getElementByTagNameNS(
                        serviceResourceElement2, "http://schemas.microsoft.com/windowsazure",
                        "ServiceObjectiveAssignmentStateDescription");
                if (serviceObjectiveAssignmentStateDescriptionElement != null) {
                    String serviceObjectiveAssignmentStateDescriptionInstance;
                    serviceObjectiveAssignmentStateDescriptionInstance = serviceObjectiveAssignmentStateDescriptionElement
                            .getTextContent();
                    serviceResourceInstance.setServiceObjectiveAssignmentStateDescription(
                            serviceObjectiveAssignmentStateDescriptionInstance);
                }

                Element serviceObjectiveAssignmentSuccessDateElement = XmlUtility.getElementByTagNameNS(
                        serviceResourceElement2, "http://schemas.microsoft.com/windowsazure",
                        "ServiceObjectiveAssignmentSuccessDate");
                if (serviceObjectiveAssignmentSuccessDateElement != null) {
                    String serviceObjectiveAssignmentSuccessDateInstance;
                    serviceObjectiveAssignmentSuccessDateInstance = serviceObjectiveAssignmentSuccessDateElement
                            .getTextContent();
                    serviceResourceInstance.setServiceObjectiveAssignmentSuccessDate(
                            serviceObjectiveAssignmentSuccessDateInstance);
                }

                Element serviceObjectiveIdElement2 = XmlUtility.getElementByTagNameNS(serviceResourceElement2,
                        "http://schemas.microsoft.com/windowsazure", "ServiceObjectiveId");
                if (serviceObjectiveIdElement2 != null) {
                    String serviceObjectiveIdInstance;
                    serviceObjectiveIdInstance = serviceObjectiveIdElement2.getTextContent();
                    serviceResourceInstance.setServiceObjectiveId(serviceObjectiveIdInstance);
                }

                Element assignedServiceObjectiveIdElement = XmlUtility.getElementByTagNameNS(
                        serviceResourceElement2, "http://schemas.microsoft.com/windowsazure",
                        "AssignedServiceObjectiveId");
                if (assignedServiceObjectiveIdElement != null) {
                    String assignedServiceObjectiveIdInstance;
                    assignedServiceObjectiveIdInstance = assignedServiceObjectiveIdElement.getTextContent();
                    serviceResourceInstance.setAssignedServiceObjectiveId(assignedServiceObjectiveIdInstance);
                }

                Element recoveryPeriodStartDateElement = XmlUtility.getElementByTagNameNS(
                        serviceResourceElement2, "http://schemas.microsoft.com/windowsazure",
                        "RecoveryPeriodStartDate");
                if (recoveryPeriodStartDateElement != null
                        && recoveryPeriodStartDateElement.getTextContent() != null
                        && !recoveryPeriodStartDateElement.getTextContent().isEmpty()) {
                    Calendar recoveryPeriodStartDateInstance;
                    recoveryPeriodStartDateInstance = DatatypeConverter
                            .parseDateTime(recoveryPeriodStartDateElement.getTextContent());
                    serviceResourceInstance.setRecoveryPeriodStartDate(recoveryPeriodStartDateInstance);
                }

                Element nameElement2 = XmlUtility.getElementByTagNameNS(serviceResourceElement2,
                        "http://schemas.microsoft.com/windowsazure", "Name");
                if (nameElement2 != null) {
                    String nameInstance;
                    nameInstance = nameElement2.getTextContent();
                    serviceResourceInstance.setName(nameInstance);
                }

                Element typeElement = XmlUtility.getElementByTagNameNS(serviceResourceElement2,
                        "http://schemas.microsoft.com/windowsazure", "Type");
                if (typeElement != null) {
                    String typeInstance;
                    typeInstance = typeElement.getTextContent();
                    serviceResourceInstance.setType(typeInstance);
                }

                Element stateElement = XmlUtility.getElementByTagNameNS(serviceResourceElement2,
                        "http://schemas.microsoft.com/windowsazure", "State");
                if (stateElement != null) {
                    String stateInstance;
                    stateInstance = stateElement.getTextContent();
                    serviceResourceInstance.setState(stateInstance);
                }
            }

        }
        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.VirtualMachineVMImageOperationsImpl.java

/**
* The List Virtual Machine Images operation retrieves a list of the virtual
* machine images./*from w  ww  .j av  a2s  . c om*/
*
* @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.
* @throws URISyntaxException Thrown if there was an error parsing a URI in
* the response.
* @return The List VM Images operation response.
*/
@Override
public VirtualMachineVMImageListResponse list()
        throws IOException, ServiceException, ParserConfigurationException, SAXException, URISyntaxException {
    // 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, "listAsync", 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/vmimages";
    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
    HttpGet httpRequest = new HttpGet(url);

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

    // 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, null, httpResponse,
                    httpResponse.getEntity());
            if (shouldTrace) {
                CloudTracing.error(invocationId, ex);
            }
            throw ex;
        }

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

            Element vMImagesSequenceElement = XmlUtility.getElementByTagNameNS(responseDoc,
                    "http://schemas.microsoft.com/windowsazure", "VMImages");
            if (vMImagesSequenceElement != null) {
                for (int i1 = 0; i1 < com.microsoft.windowsazure.core.utils.XmlUtility
                        .getElementsByTagNameNS(vMImagesSequenceElement,
                                "http://schemas.microsoft.com/windowsazure", "VMImage")
                        .size(); i1 = i1 + 1) {
                    org.w3c.dom.Element vMImagesElement = ((org.w3c.dom.Element) com.microsoft.windowsazure.core.utils.XmlUtility
                            .getElementsByTagNameNS(vMImagesSequenceElement,
                                    "http://schemas.microsoft.com/windowsazure", "VMImage")
                            .get(i1));
                    VirtualMachineVMImageListResponse.VirtualMachineVMImage vMImageInstance = new VirtualMachineVMImageListResponse.VirtualMachineVMImage();
                    result.getVMImages().add(vMImageInstance);

                    Element nameElement = XmlUtility.getElementByTagNameNS(vMImagesElement,
                            "http://schemas.microsoft.com/windowsazure", "Name");
                    if (nameElement != null) {
                        String nameInstance;
                        nameInstance = nameElement.getTextContent();
                        vMImageInstance.setName(nameInstance);
                    }

                    Element labelElement = XmlUtility.getElementByTagNameNS(vMImagesElement,
                            "http://schemas.microsoft.com/windowsazure", "Label");
                    if (labelElement != null) {
                        String labelInstance;
                        labelInstance = labelElement.getTextContent();
                        vMImageInstance.setLabel(labelInstance);
                    }

                    Element categoryElement = XmlUtility.getElementByTagNameNS(vMImagesElement,
                            "http://schemas.microsoft.com/windowsazure", "Category");
                    if (categoryElement != null) {
                        String categoryInstance;
                        categoryInstance = categoryElement.getTextContent();
                        vMImageInstance.setCategory(categoryInstance);
                    }

                    Element descriptionElement = XmlUtility.getElementByTagNameNS(vMImagesElement,
                            "http://schemas.microsoft.com/windowsazure", "Description");
                    if (descriptionElement != null) {
                        String descriptionInstance;
                        descriptionInstance = descriptionElement.getTextContent();
                        vMImageInstance.setDescription(descriptionInstance);
                    }

                    Element oSDiskConfigurationElement = XmlUtility.getElementByTagNameNS(vMImagesElement,
                            "http://schemas.microsoft.com/windowsazure", "OSDiskConfiguration");
                    if (oSDiskConfigurationElement != null) {
                        VirtualMachineVMImageListResponse.OSDiskConfiguration oSDiskConfigurationInstance = new VirtualMachineVMImageListResponse.OSDiskConfiguration();
                        vMImageInstance.setOSDiskConfiguration(oSDiskConfigurationInstance);

                        Element nameElement2 = XmlUtility.getElementByTagNameNS(oSDiskConfigurationElement,
                                "http://schemas.microsoft.com/windowsazure", "Name");
                        if (nameElement2 != null) {
                            String nameInstance2;
                            nameInstance2 = nameElement2.getTextContent();
                            oSDiskConfigurationInstance.setName(nameInstance2);
                        }

                        Element hostCachingElement = XmlUtility.getElementByTagNameNS(
                                oSDiskConfigurationElement, "http://schemas.microsoft.com/windowsazure",
                                "HostCaching");
                        if (hostCachingElement != null) {
                            String hostCachingInstance;
                            hostCachingInstance = hostCachingElement.getTextContent();
                            oSDiskConfigurationInstance.setHostCaching(hostCachingInstance);
                        }

                        Element oSStateElement = XmlUtility.getElementByTagNameNS(oSDiskConfigurationElement,
                                "http://schemas.microsoft.com/windowsazure", "OSState");
                        if (oSStateElement != null) {
                            String oSStateInstance;
                            oSStateInstance = oSStateElement.getTextContent();
                            oSDiskConfigurationInstance.setOSState(oSStateInstance);
                        }

                        Element osElement = XmlUtility.getElementByTagNameNS(oSDiskConfigurationElement,
                                "http://schemas.microsoft.com/windowsazure", "OS");
                        if (osElement != null) {
                            String osInstance;
                            osInstance = osElement.getTextContent();
                            oSDiskConfigurationInstance.setOperatingSystem(osInstance);
                        }

                        Element mediaLinkElement = XmlUtility.getElementByTagNameNS(oSDiskConfigurationElement,
                                "http://schemas.microsoft.com/windowsazure", "MediaLink");
                        if (mediaLinkElement != null) {
                            URI mediaLinkInstance;
                            mediaLinkInstance = new URI(mediaLinkElement.getTextContent());
                            oSDiskConfigurationInstance.setMediaLink(mediaLinkInstance);
                        }

                        Element logicalDiskSizeInGBElement = XmlUtility.getElementByTagNameNS(
                                oSDiskConfigurationElement, "http://schemas.microsoft.com/windowsazure",
                                "LogicalDiskSizeInGB");
                        if (logicalDiskSizeInGBElement != null) {
                            int logicalDiskSizeInGBInstance;
                            logicalDiskSizeInGBInstance = DatatypeConverter
                                    .parseInt(logicalDiskSizeInGBElement.getTextContent());
                            oSDiskConfigurationInstance.setLogicalDiskSizeInGB(logicalDiskSizeInGBInstance);
                        }

                        Element iOTypeElement = XmlUtility.getElementByTagNameNS(oSDiskConfigurationElement,
                                "http://schemas.microsoft.com/windowsazure", "IOType");
                        if (iOTypeElement != null) {
                            String iOTypeInstance;
                            iOTypeInstance = iOTypeElement.getTextContent();
                            oSDiskConfigurationInstance.setIOType(iOTypeInstance);
                        }
                    }

                    Element dataDiskConfigurationsSequenceElement = XmlUtility.getElementByTagNameNS(
                            vMImagesElement, "http://schemas.microsoft.com/windowsazure",
                            "DataDiskConfigurations");
                    if (dataDiskConfigurationsSequenceElement != null) {
                        for (int i2 = 0; i2 < com.microsoft.windowsazure.core.utils.XmlUtility
                                .getElementsByTagNameNS(dataDiskConfigurationsSequenceElement,
                                        "http://schemas.microsoft.com/windowsazure", "DataDiskConfiguration")
                                .size(); i2 = i2 + 1) {
                            org.w3c.dom.Element dataDiskConfigurationsElement = ((org.w3c.dom.Element) com.microsoft.windowsazure.core.utils.XmlUtility
                                    .getElementsByTagNameNS(dataDiskConfigurationsSequenceElement,
                                            "http://schemas.microsoft.com/windowsazure",
                                            "DataDiskConfiguration")
                                    .get(i2));
                            VirtualMachineVMImageListResponse.DataDiskConfiguration dataDiskConfigurationInstance = new VirtualMachineVMImageListResponse.DataDiskConfiguration();
                            vMImageInstance.getDataDiskConfigurations().add(dataDiskConfigurationInstance);

                            Element nameElement3 = XmlUtility.getElementByTagNameNS(
                                    dataDiskConfigurationsElement, "http://schemas.microsoft.com/windowsazure",
                                    "Name");
                            if (nameElement3 != null) {
                                String nameInstance3;
                                nameInstance3 = nameElement3.getTextContent();
                                dataDiskConfigurationInstance.setName(nameInstance3);
                            }

                            Element hostCachingElement2 = XmlUtility.getElementByTagNameNS(
                                    dataDiskConfigurationsElement, "http://schemas.microsoft.com/windowsazure",
                                    "HostCaching");
                            if (hostCachingElement2 != null) {
                                String hostCachingInstance2;
                                hostCachingInstance2 = hostCachingElement2.getTextContent();
                                dataDiskConfigurationInstance.setHostCaching(hostCachingInstance2);
                            }

                            Element lunElement = XmlUtility.getElementByTagNameNS(dataDiskConfigurationsElement,
                                    "http://schemas.microsoft.com/windowsazure", "Lun");
                            if (lunElement != null && lunElement.getTextContent() != null
                                    && !lunElement.getTextContent().isEmpty()) {
                                int lunInstance;
                                lunInstance = DatatypeConverter.parseInt(lunElement.getTextContent());
                                dataDiskConfigurationInstance.setLogicalUnitNumber(lunInstance);
                            }

                            Element mediaLinkElement2 = XmlUtility.getElementByTagNameNS(
                                    dataDiskConfigurationsElement, "http://schemas.microsoft.com/windowsazure",
                                    "MediaLink");
                            if (mediaLinkElement2 != null) {
                                URI mediaLinkInstance2;
                                mediaLinkInstance2 = new URI(mediaLinkElement2.getTextContent());
                                dataDiskConfigurationInstance.setMediaLink(mediaLinkInstance2);
                            }

                            Element logicalDiskSizeInGBElement2 = XmlUtility.getElementByTagNameNS(
                                    dataDiskConfigurationsElement, "http://schemas.microsoft.com/windowsazure",
                                    "LogicalDiskSizeInGB");
                            if (logicalDiskSizeInGBElement2 != null) {
                                int logicalDiskSizeInGBInstance2;
                                logicalDiskSizeInGBInstance2 = DatatypeConverter
                                        .parseInt(logicalDiskSizeInGBElement2.getTextContent());
                                dataDiskConfigurationInstance
                                        .setLogicalDiskSizeInGB(logicalDiskSizeInGBInstance2);
                            }

                            Element iOTypeElement2 = XmlUtility.getElementByTagNameNS(
                                    dataDiskConfigurationsElement, "http://schemas.microsoft.com/windowsazure",
                                    "IOType");
                            if (iOTypeElement2 != null) {
                                String iOTypeInstance2;
                                iOTypeInstance2 = iOTypeElement2.getTextContent();
                                dataDiskConfigurationInstance.setIOType(iOTypeInstance2);
                            }
                        }
                    }

                    Element serviceNameElement = XmlUtility.getElementByTagNameNS(vMImagesElement,
                            "http://schemas.microsoft.com/windowsazure", "ServiceName");
                    if (serviceNameElement != null) {
                        String serviceNameInstance;
                        serviceNameInstance = serviceNameElement.getTextContent();
                        vMImageInstance.setServiceName(serviceNameInstance);
                    }

                    Element deploymentNameElement = XmlUtility.getElementByTagNameNS(vMImagesElement,
                            "http://schemas.microsoft.com/windowsazure", "DeploymentName");
                    if (deploymentNameElement != null) {
                        String deploymentNameInstance;
                        deploymentNameInstance = deploymentNameElement.getTextContent();
                        vMImageInstance.setDeploymentName(deploymentNameInstance);
                    }

                    Element roleNameElement = XmlUtility.getElementByTagNameNS(vMImagesElement,
                            "http://schemas.microsoft.com/windowsazure", "RoleName");
                    if (roleNameElement != null) {
                        String roleNameInstance;
                        roleNameInstance = roleNameElement.getTextContent();
                        vMImageInstance.setRoleName(roleNameInstance);
                    }

                    Element affinityGroupElement = XmlUtility.getElementByTagNameNS(vMImagesElement,
                            "http://schemas.microsoft.com/windowsazure", "AffinityGroup");
                    if (affinityGroupElement != null) {
                        String affinityGroupInstance;
                        affinityGroupInstance = affinityGroupElement.getTextContent();
                        vMImageInstance.setAffinityGroup(affinityGroupInstance);
                    }

                    Element locationElement = XmlUtility.getElementByTagNameNS(vMImagesElement,
                            "http://schemas.microsoft.com/windowsazure", "Location");
                    if (locationElement != null) {
                        String locationInstance;
                        locationInstance = locationElement.getTextContent();
                        vMImageInstance.setLocation(locationInstance);
                    }

                    Element createdTimeElement = XmlUtility.getElementByTagNameNS(vMImagesElement,
                            "http://schemas.microsoft.com/windowsazure", "CreatedTime");
                    if (createdTimeElement != null && createdTimeElement.getTextContent() != null
                            && !createdTimeElement.getTextContent().isEmpty()) {
                        Calendar createdTimeInstance;
                        createdTimeInstance = DatatypeConverter
                                .parseDateTime(createdTimeElement.getTextContent());
                        vMImageInstance.setCreatedTime(createdTimeInstance);
                    }

                    Element modifiedTimeElement = XmlUtility.getElementByTagNameNS(vMImagesElement,
                            "http://schemas.microsoft.com/windowsazure", "ModifiedTime");
                    if (modifiedTimeElement != null && modifiedTimeElement.getTextContent() != null
                            && !modifiedTimeElement.getTextContent().isEmpty()) {
                        Calendar modifiedTimeInstance;
                        modifiedTimeInstance = DatatypeConverter
                                .parseDateTime(modifiedTimeElement.getTextContent());
                        vMImageInstance.setModifiedTime(modifiedTimeInstance);
                    }

                    Element languageElement = XmlUtility.getElementByTagNameNS(vMImagesElement,
                            "http://schemas.microsoft.com/windowsazure", "Language");
                    if (languageElement != null) {
                        String languageInstance;
                        languageInstance = languageElement.getTextContent();
                        vMImageInstance.setLanguage(languageInstance);
                    }

                    Element imageFamilyElement = XmlUtility.getElementByTagNameNS(vMImagesElement,
                            "http://schemas.microsoft.com/windowsazure", "ImageFamily");
                    if (imageFamilyElement != null) {
                        String imageFamilyInstance;
                        imageFamilyInstance = imageFamilyElement.getTextContent();
                        vMImageInstance.setImageFamily(imageFamilyInstance);
                    }

                    Element recommendedVMSizeElement = XmlUtility.getElementByTagNameNS(vMImagesElement,
                            "http://schemas.microsoft.com/windowsazure", "RecommendedVMSize");
                    if (recommendedVMSizeElement != null) {
                        String recommendedVMSizeInstance;
                        recommendedVMSizeInstance = recommendedVMSizeElement.getTextContent();
                        vMImageInstance.setRecommendedVMSize(recommendedVMSizeInstance);
                    }

                    Element isPremiumElement = XmlUtility.getElementByTagNameNS(vMImagesElement,
                            "http://schemas.microsoft.com/windowsazure", "IsPremium");
                    if (isPremiumElement != null && isPremiumElement.getTextContent() != null
                            && !isPremiumElement.getTextContent().isEmpty()) {
                        boolean isPremiumInstance;
                        isPremiumInstance = DatatypeConverter
                                .parseBoolean(isPremiumElement.getTextContent().toLowerCase());
                        vMImageInstance.setIsPremium(isPremiumInstance);
                    }

                    Element eulaElement = XmlUtility.getElementByTagNameNS(vMImagesElement,
                            "http://schemas.microsoft.com/windowsazure", "Eula");
                    if (eulaElement != null) {
                        String eulaInstance;
                        eulaInstance = eulaElement.getTextContent();
                        vMImageInstance.setEula(eulaInstance);
                    }

                    Element iconUriElement = XmlUtility.getElementByTagNameNS(vMImagesElement,
                            "http://schemas.microsoft.com/windowsazure", "IconUri");
                    if (iconUriElement != null) {
                        String iconUriInstance;
                        iconUriInstance = iconUriElement.getTextContent();
                        vMImageInstance.setIconUri(iconUriInstance);
                    }

                    Element smallIconUriElement = XmlUtility.getElementByTagNameNS(vMImagesElement,
                            "http://schemas.microsoft.com/windowsazure", "SmallIconUri");
                    if (smallIconUriElement != null) {
                        String smallIconUriInstance;
                        smallIconUriInstance = smallIconUriElement.getTextContent();
                        vMImageInstance.setSmallIconUri(smallIconUriInstance);
                    }

                    Element privacyUriElement = XmlUtility.getElementByTagNameNS(vMImagesElement,
                            "http://schemas.microsoft.com/windowsazure", "PrivacyUri");
                    if (privacyUriElement != null) {
                        URI privacyUriInstance;
                        privacyUriInstance = new URI(privacyUriElement.getTextContent());
                        vMImageInstance.setPrivacyUri(privacyUriInstance);
                    }

                    Element publisherNameElement = XmlUtility.getElementByTagNameNS(vMImagesElement,
                            "http://schemas.microsoft.com/windowsazure", "PublisherName");
                    if (publisherNameElement != null) {
                        String publisherNameInstance;
                        publisherNameInstance = publisherNameElement.getTextContent();
                        vMImageInstance.setPublisherName(publisherNameInstance);
                    }

                    Element publishedDateElement = XmlUtility.getElementByTagNameNS(vMImagesElement,
                            "http://schemas.microsoft.com/windowsazure", "PublishedDate");
                    if (publishedDateElement != null && publishedDateElement.getTextContent() != null
                            && !publishedDateElement.getTextContent().isEmpty()) {
                        Calendar publishedDateInstance;
                        publishedDateInstance = DatatypeConverter
                                .parseDateTime(publishedDateElement.getTextContent());
                        vMImageInstance.setPublishedDate(publishedDateInstance);
                    }

                    Element showInGuiElement = XmlUtility.getElementByTagNameNS(vMImagesElement,
                            "http://schemas.microsoft.com/windowsazure", "ShowInGui");
                    if (showInGuiElement != null && showInGuiElement.getTextContent() != null
                            && !showInGuiElement.getTextContent().isEmpty()) {
                        boolean showInGuiInstance;
                        showInGuiInstance = DatatypeConverter
                                .parseBoolean(showInGuiElement.getTextContent().toLowerCase());
                        vMImageInstance.setShowInGui(showInGuiInstance);
                    }

                    Element pricingDetailLinkElement = XmlUtility.getElementByTagNameNS(vMImagesElement,
                            "http://schemas.microsoft.com/windowsazure", "PricingDetailLink");
                    if (pricingDetailLinkElement != null) {
                        URI pricingDetailLinkInstance;
                        pricingDetailLinkInstance = new URI(pricingDetailLinkElement.getTextContent());
                        vMImageInstance.setPricingDetailLink(pricingDetailLinkInstance);
                    }
                }
            }

        }
        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.servicebus.QueueOperationsImpl.java

/**
* Enumerates the queues in the service namespace. The result is returned in
* pages, each containing up to 100 queues. If the namespace contains more
* than 100 queues, a feed is returned that contains the first page and a
* next link with the URI to view the next page of data.  (see
* http://msdn.microsoft.com/en-us/library/windowsazure/hh780759.asp for
* more information)/*from  w  ww. jav a 2 s .  c  o m*/
*
* @param namespaceName Required. The namespace name.
* @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.
* @throws URISyntaxException Thrown if there was an error parsing a URI in
* the response.
* @return A response to a request for a list of queues.
*/
@Override
public ServiceBusQueuesResponse list(String namespaceName)
        throws IOException, ServiceException, ParserConfigurationException, SAXException, URISyntaxException {
    // Validate
    if (namespaceName == null) {
        throw new NullPointerException("namespaceName");
    }

    // 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("namespaceName", namespaceName);
        CloudTracing.enter(invocationId, this, "listAsync", 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/servicebus/namespaces/";
    url = url + URLEncoder.encode(namespaceName, "UTF-8");
    url = url + "/Queues";
    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
    HttpGet httpRequest = new HttpGet(url);

    // Set Headers
    httpRequest.setHeader("Content-Type", "application/xml; charset=utf-8");
    httpRequest.setHeader("x-ms-version", "2013-08-01");

    // 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, null, httpResponse,
                    httpResponse.getEntity());
            if (shouldTrace) {
                CloudTracing.error(invocationId, ex);
            }
            throw ex;
        }

        // Create Result
        ServiceBusQueuesResponse result = null;
        // Deserialize Response
        if (statusCode == HttpStatus.SC_OK) {
            InputStream responseContent = httpResponse.getEntity().getContent();
            result = new ServiceBusQueuesResponse();
            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));
                        ServiceBusQueue entryInstance = new ServiceBusQueue();
                        result.getQueues().add(entryInstance);

                        Element titleElement = XmlUtility.getElementByTagNameNS(entriesElement,
                                "http://www.w3.org/2005/Atom", "title");
                        if (titleElement != null) {
                            String titleInstance;
                            titleInstance = titleElement.getTextContent();
                            entryInstance.setName(titleInstance);
                        }

                        Element contentElement = XmlUtility.getElementByTagNameNS(entriesElement,
                                "http://www.w3.org/2005/Atom", "content");
                        if (contentElement != null) {
                            Element queueDescriptionElement = XmlUtility.getElementByTagNameNS(contentElement,
                                    "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                    "QueueDescription");
                            if (queueDescriptionElement != null) {
                                Element lockDurationElement = XmlUtility.getElementByTagNameNS(
                                        queueDescriptionElement,
                                        "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                        "LockDuration");
                                if (lockDurationElement != null) {
                                    String lockDurationInstance;
                                    lockDurationInstance = lockDurationElement.getTextContent();
                                    entryInstance.setLockDuration(lockDurationInstance);
                                }

                                Element maxSizeInMegabytesElement = XmlUtility.getElementByTagNameNS(
                                        queueDescriptionElement,
                                        "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                        "MaxSizeInMegabytes");
                                if (maxSizeInMegabytesElement != null) {
                                    int maxSizeInMegabytesInstance;
                                    maxSizeInMegabytesInstance = DatatypeConverter
                                            .parseInt(maxSizeInMegabytesElement.getTextContent());
                                    entryInstance.setMaxSizeInMegabytes(maxSizeInMegabytesInstance);
                                }

                                Element requiresDuplicateDetectionElement = XmlUtility.getElementByTagNameNS(
                                        queueDescriptionElement,
                                        "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                        "RequiresDuplicateDetection");
                                if (requiresDuplicateDetectionElement != null) {
                                    boolean requiresDuplicateDetectionInstance;
                                    requiresDuplicateDetectionInstance = DatatypeConverter.parseBoolean(
                                            requiresDuplicateDetectionElement.getTextContent().toLowerCase());
                                    entryInstance
                                            .setRequiresDuplicateDetection(requiresDuplicateDetectionInstance);
                                }

                                Element requiresSessionElement = XmlUtility.getElementByTagNameNS(
                                        queueDescriptionElement,
                                        "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                        "RequiresSession");
                                if (requiresSessionElement != null) {
                                    boolean requiresSessionInstance;
                                    requiresSessionInstance = DatatypeConverter.parseBoolean(
                                            requiresSessionElement.getTextContent().toLowerCase());
                                    entryInstance.setRequiresSession(requiresSessionInstance);
                                }

                                Element defaultMessageTimeToLiveElement = XmlUtility.getElementByTagNameNS(
                                        queueDescriptionElement,
                                        "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                        "DefaultMessageTimeToLive");
                                if (defaultMessageTimeToLiveElement != null) {
                                    String defaultMessageTimeToLiveInstance;
                                    defaultMessageTimeToLiveInstance = defaultMessageTimeToLiveElement
                                            .getTextContent();
                                    entryInstance.setDefaultMessageTimeToLive(defaultMessageTimeToLiveInstance);
                                }

                                Element deadLetteringOnMessageExpirationElement = XmlUtility
                                        .getElementByTagNameNS(queueDescriptionElement,
                                                "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                                "DeadLetteringOnMessageExpiration");
                                if (deadLetteringOnMessageExpirationElement != null) {
                                    boolean deadLetteringOnMessageExpirationInstance;
                                    deadLetteringOnMessageExpirationInstance = DatatypeConverter
                                            .parseBoolean(deadLetteringOnMessageExpirationElement
                                                    .getTextContent().toLowerCase());
                                    entryInstance.setDeadLetteringOnMessageExpiration(
                                            deadLetteringOnMessageExpirationInstance);
                                }

                                Element duplicateDetectionHistoryTimeWindowElement = XmlUtility
                                        .getElementByTagNameNS(queueDescriptionElement,
                                                "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                                "DuplicateDetectionHistoryTimeWindow");
                                if (duplicateDetectionHistoryTimeWindowElement != null) {
                                    String duplicateDetectionHistoryTimeWindowInstance;
                                    duplicateDetectionHistoryTimeWindowInstance = duplicateDetectionHistoryTimeWindowElement
                                            .getTextContent();
                                    entryInstance.setDuplicateDetectionHistoryTimeWindow(
                                            duplicateDetectionHistoryTimeWindowInstance);
                                }

                                Element maxDeliveryCountElement = XmlUtility.getElementByTagNameNS(
                                        queueDescriptionElement,
                                        "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                        "MaxDeliveryCount");
                                if (maxDeliveryCountElement != null) {
                                    int maxDeliveryCountInstance;
                                    maxDeliveryCountInstance = DatatypeConverter
                                            .parseInt(maxDeliveryCountElement.getTextContent());
                                    entryInstance.setMaxDeliveryCount(maxDeliveryCountInstance);
                                }

                                Element enableBatchedOperationsElement = XmlUtility.getElementByTagNameNS(
                                        queueDescriptionElement,
                                        "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                        "EnableBatchedOperations");
                                if (enableBatchedOperationsElement != null) {
                                    boolean enableBatchedOperationsInstance;
                                    enableBatchedOperationsInstance = DatatypeConverter.parseBoolean(
                                            enableBatchedOperationsElement.getTextContent().toLowerCase());
                                    entryInstance.setEnableBatchedOperations(enableBatchedOperationsInstance);
                                }

                                Element sizeInBytesElement = XmlUtility.getElementByTagNameNS(
                                        queueDescriptionElement,
                                        "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                        "SizeInBytes");
                                if (sizeInBytesElement != null) {
                                    int sizeInBytesInstance;
                                    sizeInBytesInstance = DatatypeConverter
                                            .parseInt(sizeInBytesElement.getTextContent());
                                    entryInstance.setSizeInBytes(sizeInBytesInstance);
                                }

                                Element messageCountElement = XmlUtility.getElementByTagNameNS(
                                        queueDescriptionElement,
                                        "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                        "MessageCount");
                                if (messageCountElement != null) {
                                    int messageCountInstance;
                                    messageCountInstance = DatatypeConverter
                                            .parseInt(messageCountElement.getTextContent());
                                    entryInstance.setMessageCount(messageCountInstance);
                                }

                                Element isAnonymousAccessibleElement = XmlUtility.getElementByTagNameNS(
                                        queueDescriptionElement,
                                        "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                        "IsAnonymousAccessible");
                                if (isAnonymousAccessibleElement != null) {
                                    boolean isAnonymousAccessibleInstance;
                                    isAnonymousAccessibleInstance = DatatypeConverter.parseBoolean(
                                            isAnonymousAccessibleElement.getTextContent().toLowerCase());
                                    entryInstance.setIsAnonymousAccessible(isAnonymousAccessibleInstance);
                                }

                                Element authorizationRulesSequenceElement = XmlUtility.getElementByTagNameNS(
                                        queueDescriptionElement,
                                        "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                        "AuthorizationRules");
                                if (authorizationRulesSequenceElement != null) {
                                    for (int i2 = 0; i2 < com.microsoft.windowsazure.core.utils.XmlUtility
                                            .getElementsByTagNameNS(authorizationRulesSequenceElement,
                                                    "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                                    "AuthorizationRule")
                                            .size(); i2 = i2 + 1) {
                                        org.w3c.dom.Element authorizationRulesElement = ((org.w3c.dom.Element) com.microsoft.windowsazure.core.utils.XmlUtility
                                                .getElementsByTagNameNS(authorizationRulesSequenceElement,
                                                        "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                                        "AuthorizationRule")
                                                .get(i2));
                                        ServiceBusSharedAccessAuthorizationRule authorizationRuleInstance = new ServiceBusSharedAccessAuthorizationRule();
                                        entryInstance.getAuthorizationRules().add(authorizationRuleInstance);

                                        Element claimTypeElement = XmlUtility.getElementByTagNameNS(
                                                authorizationRulesElement,
                                                "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                                "ClaimType");
                                        if (claimTypeElement != null) {
                                            String claimTypeInstance;
                                            claimTypeInstance = claimTypeElement.getTextContent();
                                            authorizationRuleInstance.setClaimType(claimTypeInstance);
                                        }

                                        Element claimValueElement = XmlUtility.getElementByTagNameNS(
                                                authorizationRulesElement,
                                                "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                                "ClaimValue");
                                        if (claimValueElement != null) {
                                            String claimValueInstance;
                                            claimValueInstance = claimValueElement.getTextContent();
                                            authorizationRuleInstance.setClaimValue(claimValueInstance);
                                        }

                                        Element rightsSequenceElement = XmlUtility.getElementByTagNameNS(
                                                authorizationRulesElement,
                                                "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                                "Rights");
                                        if (rightsSequenceElement != null) {
                                            for (int i3 = 0; i3 < com.microsoft.windowsazure.core.utils.XmlUtility
                                                    .getElementsByTagNameNS(rightsSequenceElement,
                                                            "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                                            "AccessRights")
                                                    .size(); i3 = i3 + 1) {
                                                org.w3c.dom.Element rightsElement = ((org.w3c.dom.Element) com.microsoft.windowsazure.core.utils.XmlUtility
                                                        .getElementsByTagNameNS(rightsSequenceElement,
                                                                "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                                                "AccessRights")
                                                        .get(i3));
                                                authorizationRuleInstance.getRights().add(
                                                        AccessRight.valueOf(rightsElement.getTextContent()));
                                            }
                                        }

                                        Element createdTimeElement = XmlUtility.getElementByTagNameNS(
                                                authorizationRulesElement,
                                                "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                                "CreatedTime");
                                        if (createdTimeElement != null) {
                                            Calendar createdTimeInstance;
                                            createdTimeInstance = DatatypeConverter
                                                    .parseDateTime(createdTimeElement.getTextContent());
                                            authorizationRuleInstance.setCreatedTime(createdTimeInstance);
                                        }

                                        Element keyNameElement = XmlUtility.getElementByTagNameNS(
                                                authorizationRulesElement,
                                                "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                                "KeyName");
                                        if (keyNameElement != null) {
                                            String keyNameInstance;
                                            keyNameInstance = keyNameElement.getTextContent();
                                            authorizationRuleInstance.setKeyName(keyNameInstance);
                                        }

                                        Element modifiedTimeElement = XmlUtility.getElementByTagNameNS(
                                                authorizationRulesElement,
                                                "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                                "ModifiedTime");
                                        if (modifiedTimeElement != null) {
                                            Calendar modifiedTimeInstance;
                                            modifiedTimeInstance = DatatypeConverter
                                                    .parseDateTime(modifiedTimeElement.getTextContent());
                                            authorizationRuleInstance.setModifiedTime(modifiedTimeInstance);
                                        }

                                        Element primaryKeyElement = XmlUtility.getElementByTagNameNS(
                                                authorizationRulesElement,
                                                "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                                "PrimaryKey");
                                        if (primaryKeyElement != null) {
                                            String primaryKeyInstance;
                                            primaryKeyInstance = primaryKeyElement.getTextContent();
                                            authorizationRuleInstance.setPrimaryKey(primaryKeyInstance);
                                        }

                                        Element secondaryKeyElement = XmlUtility.getElementByTagNameNS(
                                                authorizationRulesElement,
                                                "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                                "SecondaryKey");
                                        if (secondaryKeyElement != null) {
                                            String secondaryKeyInstance;
                                            secondaryKeyInstance = secondaryKeyElement.getTextContent();
                                            authorizationRuleInstance.setSecondaryKey(secondaryKeyInstance);
                                        }
                                    }
                                }

                                Element statusElement = XmlUtility.getElementByTagNameNS(
                                        queueDescriptionElement,
                                        "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                        "Status");
                                if (statusElement != null) {
                                    String statusInstance;
                                    statusInstance = statusElement.getTextContent();
                                    entryInstance.setStatus(statusInstance);
                                }

                                Element createdAtElement = XmlUtility.getElementByTagNameNS(
                                        queueDescriptionElement,
                                        "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                        "CreatedAt");
                                if (createdAtElement != null) {
                                    Calendar createdAtInstance;
                                    createdAtInstance = DatatypeConverter
                                            .parseDateTime(createdAtElement.getTextContent());
                                    entryInstance.setCreatedAt(createdAtInstance);
                                }

                                Element updatedAtElement = XmlUtility.getElementByTagNameNS(
                                        queueDescriptionElement,
                                        "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                        "UpdatedAt");
                                if (updatedAtElement != null) {
                                    Calendar updatedAtInstance;
                                    updatedAtInstance = DatatypeConverter
                                            .parseDateTime(updatedAtElement.getTextContent());
                                    entryInstance.setUpdatedAt(updatedAtInstance);
                                }

                                Element accessedAtElement = XmlUtility.getElementByTagNameNS(
                                        queueDescriptionElement,
                                        "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                        "AccessedAt");
                                if (accessedAtElement != null) {
                                    Calendar accessedAtInstance;
                                    accessedAtInstance = DatatypeConverter
                                            .parseDateTime(accessedAtElement.getTextContent());
                                    entryInstance.setAccessedAt(accessedAtInstance);
                                }

                                Element supportOrderingElement = XmlUtility.getElementByTagNameNS(
                                        queueDescriptionElement,
                                        "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                        "SupportOrdering");
                                if (supportOrderingElement != null) {
                                    boolean supportOrderingInstance;
                                    supportOrderingInstance = DatatypeConverter.parseBoolean(
                                            supportOrderingElement.getTextContent().toLowerCase());
                                    entryInstance.setSupportOrdering(supportOrderingInstance);
                                }

                                Element countDetailsElement = XmlUtility.getElementByTagNameNS(
                                        queueDescriptionElement,
                                        "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                        "CountDetails");
                                if (countDetailsElement != null) {
                                    CountDetails countDetailsInstance = new CountDetails();
                                    entryInstance.setCountDetails(countDetailsInstance);

                                    Element activeMessageCountElement = XmlUtility.getElementByTagNameNS(
                                            countDetailsElement,
                                            "http://schemas.microsoft.com/netservices/2011/06/servicebus",
                                            "ActiveMessageCount");
                                    if (activeMessageCountElement != null) {
                                        int activeMessageCountInstance;
                                        activeMessageCountInstance = DatatypeConverter
                                                .parseInt(activeMessageCountElement.getTextContent());
                                        countDetailsInstance.setActiveMessageCount(activeMessageCountInstance);
                                    }

                                    Element deadLetterMessageCountElement = XmlUtility.getElementByTagNameNS(
                                            countDetailsElement,
                                            "http://schemas.microsoft.com/netservices/2011/06/servicebus",
                                            "DeadLetterMessageCount");
                                    if (deadLetterMessageCountElement != null) {
                                        int deadLetterMessageCountInstance;
                                        deadLetterMessageCountInstance = DatatypeConverter
                                                .parseInt(deadLetterMessageCountElement.getTextContent());
                                        countDetailsInstance
                                                .setDeadLetterMessageCount(deadLetterMessageCountInstance);
                                    }

                                    Element scheduledMessageCountElement = XmlUtility.getElementByTagNameNS(
                                            countDetailsElement,
                                            "http://schemas.microsoft.com/netservices/2011/06/servicebus",
                                            "ScheduledMessageCount");
                                    if (scheduledMessageCountElement != null) {
                                        int scheduledMessageCountInstance;
                                        scheduledMessageCountInstance = DatatypeConverter
                                                .parseInt(scheduledMessageCountElement.getTextContent());
                                        countDetailsInstance
                                                .setScheduledMessageCount(scheduledMessageCountInstance);
                                    }

                                    Element transferDeadLetterMessageCountElement = XmlUtility
                                            .getElementByTagNameNS(countDetailsElement,
                                                    "http://schemas.microsoft.com/netservices/2011/06/servicebus",
                                                    "TransferDeadLetterMessageCount");
                                    if (transferDeadLetterMessageCountElement != null) {
                                        int transferDeadLetterMessageCountInstance;
                                        transferDeadLetterMessageCountInstance = DatatypeConverter.parseInt(
                                                transferDeadLetterMessageCountElement.getTextContent());
                                        countDetailsInstance.setTransferDeadLetterMessageCount(
                                                transferDeadLetterMessageCountInstance);
                                    }

                                    Element transferMessageCountElement = XmlUtility.getElementByTagNameNS(
                                            countDetailsElement,
                                            "http://schemas.microsoft.com/netservices/2011/06/servicebus",
                                            "TransferMessageCount");
                                    if (transferMessageCountElement != null) {
                                        int transferMessageCountInstance;
                                        transferMessageCountInstance = DatatypeConverter
                                                .parseInt(transferMessageCountElement.getTextContent());
                                        countDetailsInstance
                                                .setTransferMessageCount(transferMessageCountInstance);
                                    }
                                }

                                Element autoDeleteOnIdleElement = XmlUtility.getElementByTagNameNS(
                                        queueDescriptionElement,
                                        "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                        "AutoDeleteOnIdle");
                                if (autoDeleteOnIdleElement != null) {
                                    String autoDeleteOnIdleInstance;
                                    autoDeleteOnIdleInstance = autoDeleteOnIdleElement.getTextContent();
                                    entryInstance.setAutoDeleteOnIdle(autoDeleteOnIdleInstance);
                                }

                                Element entityAvailabilityStatusElement = XmlUtility.getElementByTagNameNS(
                                        queueDescriptionElement,
                                        "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                        "EntityAvailabilityStatus");
                                if (entityAvailabilityStatusElement != null) {
                                    String entityAvailabilityStatusInstance;
                                    entityAvailabilityStatusInstance = entityAvailabilityStatusElement
                                            .getTextContent();
                                    entryInstance.setEntityAvailabilityStatus(entityAvailabilityStatusInstance);
                                }
                            }
                        }
                    }
                }
            }

        }
        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.servicebus.NamespaceOperationsImpl.java

/**
* Lists the available namespaces.  (see/*from  w  ww  .  jav a2  s.c  o m*/
* http://msdn.microsoft.com/en-us/library/azure/hh780759.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.
* @throws URISyntaxException Thrown if there was an error parsing a URI in
* the response.
* @return The response to the request for a listing of namespaces.
*/
@Override
public ServiceBusNamespacesResponse list()
        throws IOException, ServiceException, ParserConfigurationException, SAXException, URISyntaxException {
    // 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, "listAsync", 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/servicebus/namespaces/";
    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
    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.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, null, httpResponse,
                    httpResponse.getEntity());
            if (shouldTrace) {
                CloudTracing.error(invocationId, ex);
            }
            throw ex;
        }

        // Create Result
        ServiceBusNamespacesResponse result = null;
        // Deserialize Response
        if (statusCode == HttpStatus.SC_OK) {
            InputStream responseContent = httpResponse.getEntity().getContent();
            result = new ServiceBusNamespacesResponse();
            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));
                        ServiceBusNamespace entryInstance = new ServiceBusNamespace();
                        result.getNamespaces().add(entryInstance);

                        Element contentElement = XmlUtility.getElementByTagNameNS(entriesElement,
                                "http://www.w3.org/2005/Atom", "content");
                        if (contentElement != null) {
                            Element namespaceDescriptionElement = XmlUtility.getElementByTagNameNS(
                                    contentElement,
                                    "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                    "NamespaceDescription");
                            if (namespaceDescriptionElement != null) {
                                Element nameElement = XmlUtility.getElementByTagNameNS(
                                        namespaceDescriptionElement,
                                        "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                        "Name");
                                if (nameElement != null) {
                                    String nameInstance;
                                    nameInstance = nameElement.getTextContent();
                                    entryInstance.setName(nameInstance);
                                }

                                Element regionElement = XmlUtility.getElementByTagNameNS(
                                        namespaceDescriptionElement,
                                        "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                        "Region");
                                if (regionElement != null) {
                                    String regionInstance;
                                    regionInstance = regionElement.getTextContent();
                                    entryInstance.setRegion(regionInstance);
                                }

                                Element statusElement = XmlUtility.getElementByTagNameNS(
                                        namespaceDescriptionElement,
                                        "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                        "Status");
                                if (statusElement != null) {
                                    String statusInstance;
                                    statusInstance = statusElement.getTextContent();
                                    entryInstance.setStatus(statusInstance);
                                }

                                Element createdAtElement = XmlUtility.getElementByTagNameNS(
                                        namespaceDescriptionElement,
                                        "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                        "CreatedAt");
                                if (createdAtElement != null) {
                                    Calendar createdAtInstance;
                                    createdAtInstance = DatatypeConverter
                                            .parseDateTime(createdAtElement.getTextContent());
                                    entryInstance.setCreatedAt(createdAtInstance);
                                }

                                Element acsManagementEndpointElement = XmlUtility.getElementByTagNameNS(
                                        namespaceDescriptionElement,
                                        "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                        "AcsManagementEndpoint");
                                if (acsManagementEndpointElement != null) {
                                    URI acsManagementEndpointInstance;
                                    acsManagementEndpointInstance = new URI(
                                            acsManagementEndpointElement.getTextContent());
                                    entryInstance.setAcsManagementEndpoint(acsManagementEndpointInstance);
                                }

                                Element serviceBusEndpointElement = XmlUtility.getElementByTagNameNS(
                                        namespaceDescriptionElement,
                                        "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                        "ServiceBusEndpoint");
                                if (serviceBusEndpointElement != null) {
                                    URI serviceBusEndpointInstance;
                                    serviceBusEndpointInstance = new URI(
                                            serviceBusEndpointElement.getTextContent());
                                    entryInstance.setServiceBusEndpoint(serviceBusEndpointInstance);
                                }

                                Element subscriptionIdElement = XmlUtility.getElementByTagNameNS(
                                        namespaceDescriptionElement,
                                        "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                        "SubscriptionId");
                                if (subscriptionIdElement != null) {
                                    String subscriptionIdInstance;
                                    subscriptionIdInstance = subscriptionIdElement.getTextContent();
                                    entryInstance.setSubscriptionId(subscriptionIdInstance);
                                }

                                Element enabledElement = XmlUtility.getElementByTagNameNS(
                                        namespaceDescriptionElement,
                                        "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                        "Enabled");
                                if (enabledElement != null) {
                                    boolean enabledInstance;
                                    enabledInstance = DatatypeConverter
                                            .parseBoolean(enabledElement.getTextContent().toLowerCase());
                                    entryInstance.setEnabled(enabledInstance);
                                }

                                Element createACSNamespaceElement = XmlUtility.getElementByTagNameNS(
                                        namespaceDescriptionElement,
                                        "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                        "CreateACSNamespace");
                                if (createACSNamespaceElement != null) {
                                    boolean createACSNamespaceInstance;
                                    createACSNamespaceInstance = DatatypeConverter.parseBoolean(
                                            createACSNamespaceElement.getTextContent().toLowerCase());
                                    entryInstance.setCreateACSNamespace(createACSNamespaceInstance);
                                }

                                Element namespaceTypeElement = XmlUtility.getElementByTagNameNS(
                                        namespaceDescriptionElement,
                                        "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
                                        "NamespaceType");
                                if (namespaceTypeElement != null
                                        && namespaceTypeElement.getTextContent() != null
                                        && !namespaceTypeElement.getTextContent().isEmpty()) {
                                    NamespaceType namespaceTypeInstance;
                                    namespaceTypeInstance = NamespaceType
                                            .valueOf(namespaceTypeElement.getTextContent());
                                    entryInstance.setNamespaceType(namespaceTypeInstance);
                                }
                            }
                        }
                    }
                }
            }

        }
        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.VirtualMachineDiskOperationsImpl.java

/**
* The Add Disk operation adds a disk to the user image repository. The disk
* can be an operating system disk or a data disk.  (see
* http://msdn.microsoft.com/en-us/library/windowsazure/jj157178.aspx for
* more information)/*w w  w.j  a  v a 2  s .c o  m*/
*
* @param name Required. The name of the disk being updated.
* @param parameters Required. Parameters supplied to the Update Virtual
* Machine Disk 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.
* @throws URISyntaxException Thrown if there was an error parsing a URI in
* the response.
* @return A virtual machine disk associated with your subscription.
*/
@Override
public VirtualMachineDiskUpdateResponse updateDisk(String name, VirtualMachineDiskUpdateParameters parameters)
        throws ParserConfigurationException, SAXException, TransformerException, IOException, ServiceException,
        URISyntaxException {
    // Validate
    if (name == null) {
        throw new NullPointerException("name");
    }
    if (parameters == null) {
        throw new NullPointerException("parameters");
    }
    if (parameters.getLabel() == null) {
        throw new NullPointerException("parameters.Label");
    }
    if (parameters.getName() == null) {
        throw new NullPointerException("parameters.Name");
    }

    // 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("name", name);
        tracingParameters.put("parameters", parameters);
        CloudTracing.enter(invocationId, this, "updateDiskAsync", 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/disks/";
    url = url + URLEncoder.encode(name, "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
    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 diskElement = requestDoc.createElementNS("http://schemas.microsoft.com/windowsazure", "Disk");
    requestDoc.appendChild(diskElement);

    if (parameters.isHasOperatingSystem() != null) {
        Element hasOperatingSystemElement = requestDoc
                .createElementNS("http://schemas.microsoft.com/windowsazure", "HasOperatingSystem");
        hasOperatingSystemElement.appendChild(
                requestDoc.createTextNode(Boolean.toString(parameters.isHasOperatingSystem()).toLowerCase()));
        diskElement.appendChild(hasOperatingSystemElement);
    }

    if (parameters.getOperatingSystemType() != null) {
        Element osElement = requestDoc.createElementNS("http://schemas.microsoft.com/windowsazure", "OS");
        osElement.appendChild(requestDoc.createTextNode(parameters.getOperatingSystemType()));
        diskElement.appendChild(osElement);
    }

    Element labelElement = requestDoc.createElementNS("http://schemas.microsoft.com/windowsazure", "Label");
    labelElement.appendChild(requestDoc.createTextNode(parameters.getLabel()));
    diskElement.appendChild(labelElement);

    if (parameters.getMediaLinkUri() != null) {
        Element mediaLinkElement = requestDoc.createElementNS("http://schemas.microsoft.com/windowsazure",
                "MediaLink");
        mediaLinkElement.appendChild(requestDoc.createTextNode(parameters.getMediaLinkUri().toString()));
        diskElement.appendChild(mediaLinkElement);
    }

    Element nameElement = requestDoc.createElementNS("http://schemas.microsoft.com/windowsazure", "Name");
    nameElement.appendChild(requestDoc.createTextNode(parameters.getName()));
    diskElement.appendChild(nameElement);

    if (parameters.getResizedSizeInGB() != null) {
        Element resizedSizeInGBElement = requestDoc.createElementNS("http://schemas.microsoft.com/windowsazure",
                "ResizedSizeInGB");
        resizedSizeInGBElement
                .appendChild(requestDoc.createTextNode(Integer.toString(parameters.getResizedSizeInGB())));
        diskElement.appendChild(resizedSizeInGBElement);
    }

    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
        VirtualMachineDiskUpdateResponse result = null;
        // Deserialize Response
        if (statusCode == HttpStatus.SC_OK) {
            InputStream responseContent = httpResponse.getEntity().getContent();
            result = new VirtualMachineDiskUpdateResponse();
            DocumentBuilderFactory documentBuilderFactory2 = DocumentBuilderFactory.newInstance();
            documentBuilderFactory2.setNamespaceAware(true);
            DocumentBuilder documentBuilder2 = documentBuilderFactory2.newDocumentBuilder();
            Document responseDoc = documentBuilder2.parse(new BOMInputStream(responseContent));

            Element diskElement2 = XmlUtility.getElementByTagNameNS(responseDoc,
                    "http://schemas.microsoft.com/windowsazure", "Disk");
            if (diskElement2 != null) {
                Element osElement2 = XmlUtility.getElementByTagNameNS(diskElement2,
                        "http://schemas.microsoft.com/windowsazure", "OS");
                if (osElement2 != null) {
                    String osInstance;
                    osInstance = osElement2.getTextContent();
                    result.setOperatingSystem(osInstance);
                }

                Element labelElement2 = XmlUtility.getElementByTagNameNS(diskElement2,
                        "http://schemas.microsoft.com/windowsazure", "Label");
                if (labelElement2 != null) {
                    String labelInstance;
                    labelInstance = labelElement2.getTextContent();
                    result.setLabel(labelInstance);
                }

                Element affinityGroupElement = XmlUtility.getElementByTagNameNS(diskElement2,
                        "http://schemas.microsoft.com/windowsazure", "AffinityGroup");
                if (affinityGroupElement != null) {
                    String affinityGroupInstance;
                    affinityGroupInstance = affinityGroupElement.getTextContent();
                    result.setAffinityGroup(affinityGroupInstance);
                }

                Element locationElement = XmlUtility.getElementByTagNameNS(diskElement2,
                        "http://schemas.microsoft.com/windowsazure", "Location");
                if (locationElement != null) {
                    String locationInstance;
                    locationInstance = locationElement.getTextContent();
                    result.setLocation(locationInstance);
                }

                Element logicalDiskSizeInGBElement = XmlUtility.getElementByTagNameNS(diskElement2,
                        "http://schemas.microsoft.com/windowsazure", "LogicalDiskSizeInGB");
                if (logicalDiskSizeInGBElement != null) {
                    int logicalDiskSizeInGBInstance;
                    logicalDiskSizeInGBInstance = DatatypeConverter
                            .parseInt(logicalDiskSizeInGBElement.getTextContent());
                    result.setLogicalSizeInGB(logicalDiskSizeInGBInstance);
                }

                Element mediaLinkElement2 = XmlUtility.getElementByTagNameNS(diskElement2,
                        "http://schemas.microsoft.com/windowsazure", "MediaLink");
                if (mediaLinkElement2 != null) {
                    URI mediaLinkInstance;
                    mediaLinkInstance = new URI(mediaLinkElement2.getTextContent());
                    result.setMediaLinkUri(mediaLinkInstance);
                }

                Element nameElement2 = XmlUtility.getElementByTagNameNS(diskElement2,
                        "http://schemas.microsoft.com/windowsazure", "Name");
                if (nameElement2 != null) {
                    String nameInstance;
                    nameInstance = nameElement2.getTextContent();
                    result.setName(nameInstance);
                }

                Element isPremiumElement = XmlUtility.getElementByTagNameNS(diskElement2,
                        "http://schemas.microsoft.com/windowsazure", "IsPremium");
                if (isPremiumElement != null && isPremiumElement.getTextContent() != null
                        && !isPremiumElement.getTextContent().isEmpty()) {
                    boolean isPremiumInstance;
                    isPremiumInstance = DatatypeConverter
                            .parseBoolean(isPremiumElement.getTextContent().toLowerCase());
                    result.setIsPremium(isPremiumInstance);
                }

                Element iOTypeElement = XmlUtility.getElementByTagNameNS(diskElement2,
                        "http://schemas.microsoft.com/windowsazure", "IOType");
                if (iOTypeElement != null) {
                    String iOTypeInstance;
                    iOTypeInstance = iOTypeElement.getTextContent();
                    result.setIOType(iOTypeInstance);
                }
            }

        }
        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.NetworkSecurityGroupOperationsImpl.java

/**
* Gets the details for the specified Network Security Group in the
* subscription./*from w ww  .j  a v a2 s  .c  om*/
*
* @param networkSecurityGroupName Required. The name of the Network
* Security Group to retrieve.
* @param detailLevel Optional. Use 'Full' to list rules.
* @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 Network Security Group associated with your subscription.
*/
@Override
public NetworkSecurityGroupGetResponse get(String networkSecurityGroupName, String detailLevel)
        throws IOException, ServiceException, ParserConfigurationException, SAXException {
    // Validate
    if (networkSecurityGroupName == null) {
        throw new NullPointerException("networkSecurityGroupName");
    }

    // 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("networkSecurityGroupName", networkSecurityGroupName);
        tracingParameters.put("detailLevel", detailLevel);
        CloudTracing.enter(invocationId, this, "getAsync", 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/networksecuritygroups/";
    url = url + URLEncoder.encode(networkSecurityGroupName, "UTF-8");
    ArrayList<String> queryParameters = new ArrayList<String>();
    if (detailLevel != null) {
        queryParameters.add("detaillevel=" + URLEncoder.encode(detailLevel, "UTF-8"));
    }
    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
    HttpGet httpRequest = new HttpGet(url);

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

    // 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, null, httpResponse,
                    httpResponse.getEntity());
            if (shouldTrace) {
                CloudTracing.error(invocationId, ex);
            }
            throw ex;
        }

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

            Element networkSecurityGroupElement = XmlUtility.getElementByTagNameNS(responseDoc,
                    "http://schemas.microsoft.com/windowsazure", "NetworkSecurityGroup");
            if (networkSecurityGroupElement != null) {
                Element nameElement = XmlUtility.getElementByTagNameNS(networkSecurityGroupElement,
                        "http://schemas.microsoft.com/windowsazure", "Name");
                if (nameElement != null) {
                    String nameInstance;
                    nameInstance = nameElement.getTextContent();
                    result.setName(nameInstance);
                }

                Element labelElement = XmlUtility.getElementByTagNameNS(networkSecurityGroupElement,
                        "http://schemas.microsoft.com/windowsazure", "Label");
                if (labelElement != null) {
                    String labelInstance;
                    labelInstance = labelElement.getTextContent();
                    result.setLabel(labelInstance);
                }

                Element locationElement = XmlUtility.getElementByTagNameNS(networkSecurityGroupElement,
                        "http://schemas.microsoft.com/windowsazure", "Location");
                if (locationElement != null) {
                    String locationInstance;
                    locationInstance = locationElement.getTextContent();
                    result.setLocation(locationInstance);
                }

                Element rulesSequenceElement = XmlUtility.getElementByTagNameNS(networkSecurityGroupElement,
                        "http://schemas.microsoft.com/windowsazure", "Rules");
                if (rulesSequenceElement != null) {
                    for (int i1 = 0; i1 < com.microsoft.windowsazure.core.utils.XmlUtility
                            .getElementsByTagNameNS(rulesSequenceElement,
                                    "http://schemas.microsoft.com/windowsazure", "Rule")
                            .size(); i1 = i1 + 1) {
                        org.w3c.dom.Element rulesElement = ((org.w3c.dom.Element) com.microsoft.windowsazure.core.utils.XmlUtility
                                .getElementsByTagNameNS(rulesSequenceElement,
                                        "http://schemas.microsoft.com/windowsazure", "Rule")
                                .get(i1));
                        NetworkSecurityRule ruleInstance = new NetworkSecurityRule();
                        result.getRules().add(ruleInstance);

                        Element nameElement2 = XmlUtility.getElementByTagNameNS(rulesElement,
                                "http://schemas.microsoft.com/windowsazure", "Name");
                        if (nameElement2 != null) {
                            String nameInstance2;
                            nameInstance2 = nameElement2.getTextContent();
                            ruleInstance.setName(nameInstance2);
                        }

                        Element typeElement = XmlUtility.getElementByTagNameNS(rulesElement,
                                "http://schemas.microsoft.com/windowsazure", "Type");
                        if (typeElement != null) {
                            String typeInstance;
                            typeInstance = typeElement.getTextContent();
                            ruleInstance.setType(typeInstance);
                        }

                        Element priorityElement = XmlUtility.getElementByTagNameNS(rulesElement,
                                "http://schemas.microsoft.com/windowsazure", "Priority");
                        if (priorityElement != null) {
                            int priorityInstance;
                            priorityInstance = DatatypeConverter.parseInt(priorityElement.getTextContent());
                            ruleInstance.setPriority(priorityInstance);
                        }

                        Element actionElement = XmlUtility.getElementByTagNameNS(rulesElement,
                                "http://schemas.microsoft.com/windowsazure", "Action");
                        if (actionElement != null) {
                            String actionInstance;
                            actionInstance = actionElement.getTextContent();
                            ruleInstance.setAction(actionInstance);
                        }

                        Element sourceAddressPrefixElement = XmlUtility.getElementByTagNameNS(rulesElement,
                                "http://schemas.microsoft.com/windowsazure", "SourceAddressPrefix");
                        if (sourceAddressPrefixElement != null) {
                            String sourceAddressPrefixInstance;
                            sourceAddressPrefixInstance = sourceAddressPrefixElement.getTextContent();
                            ruleInstance.setSourceAddressPrefix(sourceAddressPrefixInstance);
                        }

                        Element sourcePortRangeElement = XmlUtility.getElementByTagNameNS(rulesElement,
                                "http://schemas.microsoft.com/windowsazure", "SourcePortRange");
                        if (sourcePortRangeElement != null) {
                            String sourcePortRangeInstance;
                            sourcePortRangeInstance = sourcePortRangeElement.getTextContent();
                            ruleInstance.setSourcePortRange(sourcePortRangeInstance);
                        }

                        Element destinationAddressPrefixElement = XmlUtility.getElementByTagNameNS(rulesElement,
                                "http://schemas.microsoft.com/windowsazure", "DestinationAddressPrefix");
                        if (destinationAddressPrefixElement != null) {
                            String destinationAddressPrefixInstance;
                            destinationAddressPrefixInstance = destinationAddressPrefixElement.getTextContent();
                            ruleInstance.setDestinationAddressPrefix(destinationAddressPrefixInstance);
                        }

                        Element destinationPortRangeElement = XmlUtility.getElementByTagNameNS(rulesElement,
                                "http://schemas.microsoft.com/windowsazure", "DestinationPortRange");
                        if (destinationPortRangeElement != null) {
                            String destinationPortRangeInstance;
                            destinationPortRangeInstance = destinationPortRangeElement.getTextContent();
                            ruleInstance.setDestinationPortRange(destinationPortRangeInstance);
                        }

                        Element protocolElement = XmlUtility.getElementByTagNameNS(rulesElement,
                                "http://schemas.microsoft.com/windowsazure", "Protocol");
                        if (protocolElement != null) {
                            String protocolInstance;
                            protocolInstance = protocolElement.getTextContent();
                            ruleInstance.setProtocol(protocolInstance);
                        }

                        Element stateElement = XmlUtility.getElementByTagNameNS(rulesElement,
                                "http://schemas.microsoft.com/windowsazure", "State");
                        if (stateElement != null) {
                            String stateInstance;
                            stateInstance = stateElement.getTextContent();
                            ruleInstance.setState(stateInstance);
                        }

                        Element isDefaultElement = XmlUtility.getElementByTagNameNS(rulesElement,
                                "http://schemas.microsoft.com/windowsazure", "IsDefault");
                        if (isDefaultElement != null) {
                            boolean isDefaultInstance;
                            isDefaultInstance = DatatypeConverter
                                    .parseBoolean(isDefaultElement.getTextContent().toLowerCase());
                            ruleInstance.setIsDefault(isDefaultInstance);
                        }
                    }
                }
            }

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