Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2013-2015 Amazon Technologies, Inc. 
   *
   * Licensed under the Apache License, Version 2.0 (the "License"); 
   * you may not use this file except in compliance with the License. 
   * You may obtain a copy of the License at:
   * 
   *    http://aws.amazon.com/apache2.0
   *
  * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES 
  * OR CONDITIONS OF ANY KIND, either express or implied. See the 
  * License for the specific language governing permissions and 
  * limitations under the License. 
  */
 package com.amazonaws.regions;
 
 import java.util.List;
 
 
Parses a region metadata file to pull out information about the available regions, names, IDs, and what service endpoints are available in each region.
 
 public class RegionMetadataParser {
 
     private static final String REGION_TAG = "Region";
     private static final String REGION_ID_TAG = "Name";
     private static final String DOMAIN_TAG = "Domain";
     private static final String ENDPOINT_TAG = "Endpoint";
     private static final String SERVICE_TAG = "ServiceName";
     private static final String HTTP_TAG = "Http";
     private static final String HTTPS_TAG = "Https";
     private static final String HOSTNAME_TAG = "Hostname";

    
Parses the specified input stream and returns a RegionMetadata object.

Parameters:
input the input stream to parse
Returns:
the parsed region metadata
Throws:
java.io.IOException on error
 
     public static RegionMetadata parse(final InputStream input)
             throws IOException {
 
         return new RegionMetadata(internalParse(inputfalse));
     }

    

Deprecated:
since this object is stateless
 
     @Deprecated
     public RegionMetadataParser() {
     }
    
    
Parses the specified input stream and returns a list of the regions declared in it. By default, verification on the region endpoints is disabled.

Deprecated:
in favor of parse(java.io.InputStream)
Parameters:
input The stream containing the region metadata to parse.
Returns:
The list of parsed regions.
 
     @Deprecated
     public List<RegionparseRegionMetadata(InputStream input)
             throws IOException {
 
         return internalParse(inputfalse);
     }

    
Parses the specified input stream and optionally verifies that all of the endpoints end in ".amazonaws.com". This method is deprecated, since not all valid AWS endpoints end in ".amazonaws.com" any more.

Deprecated:
in favor of parse(java.io.InputStream)
Parameters:
input The stream containing the region metadata to parse.
endpointVerification Whether to verify each region endpoint
Returns:
The list of parsed regions.
 
     @Deprecated
    public List<RegionparseRegionMetadata(final InputStream input,
                                            final boolean endpointVerification)
            throws IOException {
        return internalParse(inputendpointVerification);
    }
    private static List<RegioninternalParse(
            final InputStream input,
            final boolean endpointVerificationthrows IOException {
        Document document;
        try {
            DocumentBuilderFactory factory =
                DocumentBuilderFactory.newInstance();
            DocumentBuilder documentBuilder = factory.newDocumentBuilder();
            document = documentBuilder.parse(input);
        } catch (IOException exception) {
            throw exception;
        } catch (Exception exception) {
            throw new IOException("Unable to parse region metadata file: "
                                  + exception.getMessage(),
                                  exception);
        } finally {
            try {
                input.close();
            } catch (IOException exception) {
            }
        }
        
        NodeList regionNodes = document.getElementsByTagName();
        List<Regionregions = new ArrayList<Region>();
        for (int i = 0; i < regionNodes.getLength(); i++) {
            Node node = regionNodes.item(i);
            if (node.getNodeType() == .) {
                Element element = (Element)node;
                regions.add(parseRegionElement(elementendpointVerification));
            }
        }
        
        return regions;
    }
    private static Region parseRegionElement(
            final Element regionElement,
            final boolean endpointVerification) {
        String name = getChildElementValue(regionElement);
        String domain = getChildElementValue(regionElement);
        Region region = new Region(namedomain);
        NodeList endpointNodes =
            regionElement.getElementsByTagName();
        for (int i = 0; i < endpointNodes.getLength(); i++) {
            addRegionEndpoint(region,
                              (ElementendpointNodes.item(i),
                              endpointVerification);
        }
        return region;
    }
    
    private static void addRegionEndpoint(
            final Region region,
            final Element endpointElement,
            final boolean endpointVerification) {
        String serviceName = getChildElementValue(endpointElement);
        String hostname = getChildElementValue(endpointElement);
        String http = getChildElementValue(endpointElement);
        String https = getChildElementValue(endpointElement);
        if ( endpointVerification && !verifyLegacyEndpoint(hostname) ) {
            throw new IllegalStateException("Invalid service endpoint ("
                                            + hostname + ") is detected.");
        }
        region.getServiceEndpoints().put(serviceNamehostname);
        region.getHttpSupport().put(serviceName"true".equals(http));
        region.getHttpsSupport().put(serviceName"true".equals(https));
    }
    private static String getChildElementValue(
            final String tagName,
            final Element element) {
        Node tagNode = element.getElementsByTagName(tagName).item(0);
        if ( tagNode == null )
            return null;
        NodeList nodestagNode.getChildNodes();
        Node node = (Node)nodes.item(0); 
     
        return node.getNodeValue();    
    }

    
Returns whether the provided endpoint is a "valid" AWS service endpoint ending in ".amazonaws.com".
    private static boolean verifyLegacyEndpoint(String endpoint) {
        return endpoint.endsWith(".amazonaws.com");
    }
New to GrepCode? Check out our FAQ X