1 /*
2  * Copyright 2013-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  *  http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */

15 package com.amazonaws.regions;
16
17 import com.amazonaws.AmazonWebServiceClient;
18 import com.amazonaws.ClientConfiguration;
19 import com.amazonaws.auth.AWSCredentialsProvider;
20 import com.amazonaws.util.ValidationUtils;
21
22 import java.lang.reflect.Constructor;
23 import java.util.Collection;
24
25 /**
26  * Metadata for an AWS region, including its name and what services
27  * are available in it.
28  */

29 public class Region {
30
31     private final RegionImpl regionImpl;
32
33     public Region(RegionImpl regionImpl) {
34         ValidationUtils.assertNotNull(regionImpl, "region implementation");
35         this.regionImpl = regionImpl;
36     }
37
38     /**
39      * Returns the region with the id given, or null if it cannot be found in
40      * the current regions.xml file.
41      */

42     public static Region getRegion(Regions region) {
43         return RegionUtils.getRegion(region.getName());
44     }
45
46     /**
47      * The unique system ID for this region; ex: "us-east-1".
48      *
49      * @return The unique system ID for this region.
50      */

51     public String getName() {
52         return regionImpl.getName();
53     }
54
55     /**
56      * Returns the domain for this region; ex: "amazonaws.com".
57      *
58      * @return The domain for this region.
59      */

60     public String getDomain() {
61         return regionImpl.getDomain();
62     }
63
64     /**
65      * Returns the partition this region is in. I.E. 'aws' or 'aws-cn'
66      *
67      * @return The partition this region is in.
68      */

69     public String getPartition() {
70         return regionImpl.getPartition();
71     }
72
73     /**
74      * Returns the endpoint for the service given.
75      *
76      * @param endpointPrefix
77      *         The service endpoint prefix which can be retrieved from the
78      *         constant ENDPOINT_PREFIX of the specific service client interface,
79      *         e.g. AmazonEC2.ENDPOINT_PREFIX.
80      */

81     public String getServiceEndpoint(String endpointPrefix) {
82         return regionImpl.getServiceEndpoint(endpointPrefix);
83     }
84
85     /**
86      * Returns whether the given service is supported in this region.
87      *
88      * @param serviceName
89      *         The service endpoint prefix which can be retrieved from the
90      *         constant ENDPOINT_PREFIX of the specific service client interface,
91      *         e.g. AmazonEC2.ENDPOINT_PREFIX.
92      */

93     public boolean isServiceSupported(String serviceName) {
94         return regionImpl.isServiceSupported(serviceName);
95     }
96
97     /**
98      * Returns whether the given service support the https protocol in this region.
99      *
100      * @param serviceName
101      *         The service endpoint prefix which can be retrieved from the
102      *         constant ENDPOINT_PREFIX of the specific service client interface,
103      *         e.g. AmazonEC2.ENDPOINT_PREFIX.
104      */

105     public boolean hasHttpsEndpoint(String serviceName) {
106         return regionImpl.hasHttpsEndpoint(serviceName);
107     }
108
109     /**
110      * Returns whether the given service support the http protocol in this region.
111      *
112      * @param serviceName
113      *         The service endpoint prefix which can be retrieved from the
114      *         constant ENDPOINT_PREFIX of the specific service client interface,
115      *         e.g. AmazonEC2.ENDPOINT_PREFIX.
116      */

117     public boolean hasHttpEndpoint(String serviceName) {
118         return regionImpl.hasHttpEndpoint(serviceName);
119     }
120
121     /**
122      * Returns a immutable collection of all endpoints available in the
123      * metadata.
124      */

125     public Collection<String> getAvailableEndpoints() {
126         return regionImpl.getAvailableEndpoints();
127     }
128
129     /**
130      * Creates a new service client of the class given and configures it. If
131      * credentials or config are null, defaults will be used.
132      *
133      * @param serviceClass The service client class to instantiate, e.g. AmazonS3Client.class
134      * @param credentials  The credentials provider to use, or null for the default
135      *                     credentials provider
136      * @param config       The configuration to use, or null for the default
137      *                     configuration
138      * @deprecated use appropriate {@link com.amazonaws.client.builder.AwsClientBuilder} implementation
139      *             for the service being constructed. For example:
140      *             {@code AmazonSNSClientBuilder.standard().withRegion(region).build();}
141      */

142     @Deprecated
143     public <T extends AmazonWebServiceClient> T createClient(Class<T> serviceClass,
144                                                              AWSCredentialsProvider credentials,
145                                                              ClientConfiguration config) {
146         Constructor<T> constructor;
147         T client;
148         try {
149             if (credentials == null && config == null) {
150                 constructor = serviceClass.getConstructor();
151                 client = constructor.newInstance();
152             } else if (credentials == null) {
153                 constructor = serviceClass.getConstructor(ClientConfiguration.class);
154                 client = constructor.newInstance(config);
155             } else if (config == null) {
156                 constructor = serviceClass.getConstructor(AWSCredentialsProvider.class);
157                 client = constructor.newInstance(credentials);
158             } else {
159                 constructor = serviceClass.getConstructor(AWSCredentialsProvider.class, ClientConfiguration.class);
160                 client = constructor.newInstance(credentials, config);
161             }
162
163             client.setRegion(this);
164             return client;
165         } catch (Exception e) {
166             throw new RuntimeException("Couldn't instantiate instance of " + serviceClass, e);
167         }
168     }
169
170     @Override
171     public boolean equals(Object obj) {
172         if (obj instanceof Region == false)
173             return false;
174
175         Region region = (Region) obj;
176         return this.getName().equals(region.getName());
177     }
178
179     @Override
180     public int hashCode() {
181         return getName().hashCode();
182     }
183
184     @Override
185     public String toString() {
186         return getName();
187     }
188
189 }
190