forked from aws/aws-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegionMetadataParser.java
More file actions
134 lines (119 loc) · 5.23 KB
/
RegionMetadataParser.java
File metadata and controls
134 lines (119 loc) · 5.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* Copyright 2013-2013 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.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
* Parses the Eclipse toolkit 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 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 list of the regions
* declared in it. By default, verification on the region endpoints is disabled.
*
* @param input
* The stream containing the region metadata to parse.
*
* @return The list of parsed regions.
*/
public List<Region> parseRegionMetadata(InputStream input) throws IOException {
return parseRegionMetadata(input, false);
}
/**
* Parses the specified input stream and optionally verifies all the region endpoints.
* Returns a list of all the declared regions.
*
* @param input
* The stream containing the region metadata to parse.
* @param endpointVerification
* Whether to verify each region endpoint
*
* @return The list of parsed regions.
*/
public List<Region> parseRegionMetadata(InputStream input, boolean endpointVerification) throws IOException {
Document document;
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = factory.newDocumentBuilder();
document = documentBuilder.parse(input);
} catch (Exception e) {
throw new RuntimeException("Unable to parse region metadata file: " + e.getMessage(), e);
} finally {
input.close();
}
NodeList regionNodes = document.getElementsByTagName(REGION_TAG);
List<Region> regions = new ArrayList<Region>();
for (int i = 0; i < regionNodes.getLength(); i++) {
Node node = regionNodes.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element)node;
regions.add(parseRegionElement(element, endpointVerification));
}
}
return regions;
}
private Region parseRegionElement(Element regionElement, boolean endpointVerification) {
String name = getChildElementValue(REGION_ID_TAG, regionElement);
Region region = new Region(name);
NodeList endpointNodes = regionElement.getElementsByTagName(ENDPOINT_TAG);
for (int i = 0; i < endpointNodes.getLength(); i++) {
addRegionEndpoint(region, (Element) endpointNodes.item(i), endpointVerification);
}
return region;
}
private void addRegionEndpoint(Region region, Element endpointElement, boolean endpointVerification) {
String serviceName = getChildElementValue(SERVICE_TAG, endpointElement);
String hostname = getChildElementValue(HOSTNAME_TAG, endpointElement);
String http = getChildElementValue(HTTP_TAG, endpointElement);
String https = getChildElementValue(HTTPS_TAG, endpointElement);
if ( endpointVerification && !verifyEndpoint(hostname) ) {
throw new RuntimeException("Invalid service endpoint (" + hostname + ") is detected.");
}
region.getServiceEndpoints().put(serviceName, hostname);
region.getHttpSupport().put(serviceName, "true".equals(http));
region.getHttpsSupport().put(serviceName, "true".equals(https));
}
private static String getChildElementValue(String tagName, Element element){
Node tagNode = element.getElementsByTagName(tagName).item(0);
if ( tagNode == null )
return null;
NodeList nodes= tagNode.getChildNodes();
Node node = (Node)nodes.item(0);
return node.getNodeValue();
}
/** Returns whether the provided endpoint is a valid AWS service endpoint.
* TODO We might want to do more complicated verification in the future.
*/
private static boolean verifyEndpoint(String endpoint) {
return endpoint.endsWith(".amazonaws.com");
}
}