All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ksyun.api.sdk.regions.InternalEndpointsParser Maven / Gradle / Ivy

There is a newer version: 2.1.8
Show newest version
package com.ksyun.api.sdk.regions;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import com.ksyun.api.sdk.auth.Credential;
import com.ksyun.api.sdk.exceptions.ClientException;
import com.ksyun.api.sdk.utils.XmlUtils;

public class InternalEndpointsParser implements IEndpointsProvider {

    private final static String BUNDLED_ENDPOINTS_RESOURCE_PATH = "/com/aliyuncs/endpoints/endpoints.xml";

    private static List parseEndpoints(final InputStream input) throws IOException,
            ParserConfigurationException, SAXException {
        Document document = XmlUtils.getDocument(new InputSource(input), null);
        NodeList endpointNodes = document.getElementsByTagName("Endpoint");
        List endpoints = new ArrayList();
        for (int i = 0; i < endpointNodes.getLength(); i++) {
            Element endpoint = (Element) endpointNodes.item(i);
            Set regionIds = new HashSet();
            List products = new ArrayList();
            NodeList regionNodes = endpoint.getElementsByTagName("RegionId");
            NodeList productNodes = endpoint.getElementsByTagName("Product");
            for (int j = 0; j < regionNodes.getLength(); j++)
                regionIds.add(((Element) regionNodes.item(j)).getTextContent());
            for (int j = 0; j < productNodes.getLength(); j++) {
                Element element = (Element) (productNodes.item(j));
                NodeList productNames = element.getElementsByTagName("ProductName");
                NodeList domainNames = element.getElementsByTagName("DomainName");
                for (int k = 0; k < productNames.getLength(); k++) {
                    String productName = ((Element) productNames.item(k)).getTextContent();
                    String domainName = ((Element) domainNames.item(k)).getTextContent();
                    products.add(new ProductDomain(productName, domainName));
                }
            }
            endpoints.add(new Endpoint(endpoint.getAttribute("name"), regionIds, products));
        }
        return endpoints;
    }

    @Override
    public Endpoint getEndpoint(String region, String product) throws ClientException {
        InputStream stream = this.getClass().getResourceAsStream(BUNDLED_ENDPOINTS_RESOURCE_PATH);
        try {
            List internalEndpoints = parseEndpoints(stream);
            for (Endpoint endpoint : internalEndpoints) {
                for (String regionId : endpoint.getRegionIds()) {
                    if (regionId.equals(region)) {
                        for (ProductDomain productDomain : endpoint.getProductDomains()) {
                            if (productDomain.getProductName().equals(product)) {
                                Set regionSet = new HashSet();
                                regionSet.add(region);

                                List productDomains = new ArrayList();
                                productDomains.add(productDomain);
                                Endpoint resultEndpoint = new Endpoint(endpoint.getName(), regionSet, productDomains);
                                return resultEndpoint;
                            }
                        }
                    }
                }
            }
            return null;
        } catch (IOException e) {
            throw new ClientException("SDK.MissingEndpointsFile", "Internal endpoints file is missing.");
        } catch (ParserConfigurationException e) {
            throw new ClientException("SDK.InvalidEndpointsFile", "Internal endpoints file is missing.");
        } catch (SAXException e) {
            throw new ClientException("SDK.EndpointsFileMalformed", "Internal endpoints file is missing.");
        }
    }

    @Override
    public Endpoint getEndpoint(String region, String product, String serviceCode, String endpointType,
                                Credential credential, LocationConfig locationConfig) throws ClientException {
        throw new UnsupportedOperationException();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy