/** * Constructs a new AWSIotMqttManager. * * @param mqttClientId MQTT client ID to use with this client. * @param endpoint AWS IoT endpoint. * Expected endpoint formats: * XXXXXXX.iot.[region].amazonaws.com or * XXXXXXX-ats.iot.[region].amazonaws.com or * XXXXXXX.ats.iot.cn-north-1.amazonaws.com.cn */ public AWSIotMqttManager(String mqttClientId, String endpoint) { if (mqttClientId == null || mqttClientId.isEmpty()) { throw new IllegalArgumentException("mqttClientId is null or empty"); } if (endpoint == null) { throw new IllegalArgumentException("endpoint is null"); } this.topicListeners = new ConcurrentHashMap<String, AWSIotMqttTopic>(); this.mqttMessageQueue = new ConcurrentLinkedQueue<AWSIotMqttQueueMessage>(); this.mqttClientId = mqttClientId; this.endpoint = endpoint; this.accountEndpointPrefix = null; this.region = AwsIotEndpointUtility.getRegionFromIotEndpoint(endpoint); initDefaults(); }
/** * Parse AWS region from endpoint. * @param endpoint endpoint to parse. * @return Region contained in endpoint. */ static Region getRegionFromIotEndpoint(String endpoint) { try { String endpointWithoutPort = stripPort(endpoint); String[] splits = splitEndpoint(endpointWithoutPort); final int offset = (splits.length == ENDPOINT_CN_ATS_SPLIT_SIZE) ? (ENDPOINT_REGION_OFFSET + 1) : ENDPOINT_REGION_OFFSET; return Region.getRegion(Regions.fromName(splits[offset])); } catch (Exception ex) { throw new IllegalArgumentException("Cannot parse region from endpoint.", ex); } } }
@Test(expected=IllegalArgumentException.class) public void testGetRegionFromCustomIotEndpointNoPrefix() throws Exception { String testEndpoint = "iot.us-east-1.amazonaws.com"; AwsIotEndpointUtility.getRegionFromIotEndpoint(testEndpoint); }
/** * Parse AWS region from endpoint. * @param endpoint endpoint to parse. * @return Region contained in endpoint. */ static Region getRegionFromIotEndpoint(String endpoint) { try { String endpointWithoutPort = stripPort(endpoint); String[] splits = splitEndpoint(endpointWithoutPort); final int offset = (splits.length == ENDPOINT_CN_ATS_SPLIT_SIZE) ? (ENDPOINT_REGION_OFFSET + 1) : ENDPOINT_REGION_OFFSET; return Region.getRegion(Regions.fromName(splits[offset])); } catch (Exception ex) { throw new IllegalArgumentException("Cannot parse region from endpoint.", ex); } } }
@Test (expected = IllegalArgumentException.class) public void testEndpointTooLongShouldThrowException() throws Exception { String longEndpoint = "long.iot.cn-north-1.amazonaws.com.cn.long"; AwsIotEndpointUtility.getRegionFromIotEndpoint(longEndpoint); } }
@Test(expected = IllegalArgumentException.class) public void testRegionFromBadEndpointMissingCN() throws Exception { String testEndpoint = "random.ats.iot.cn-north-1.amazonaws.com"; AwsIotEndpointUtility.getRegionFromIotEndpoint(testEndpoint); }
Region signerRegion = AwsIotEndpointUtility.getRegionFromIotEndpoint(endpoint); String signerRegionName = signerRegion.getName();
@Test public void testRegionFromCNATSEndpoint() throws Exception { String testEndpoint = "random.ats.iot.cn-north-1.amazonaws.com.cn"; assertEquals(Region.getRegion(Regions.CN_NORTH_1), AwsIotEndpointUtility.getRegionFromIotEndpoint(testEndpoint)); assertEquals("amazonaws.com.cn", AwsIotEndpointUtility.getRegionFromIotEndpoint(testEndpoint).getDomain()); }
@Test(expected=IllegalArgumentException.class) public void testGetRegionFromCustomIotEndpointBadRegion2() throws Exception { String testEndpoint = "ABCDEFG.iot.us-east-9.amazonaws.com"; assertEquals(Region.getRegion(Regions.US_EAST_1), AwsIotEndpointUtility.getRegionFromIotEndpoint(testEndpoint)); }
@Test(expected=IllegalArgumentException.class) public void testGetRegionFromCustomIotEndpointBadRegion3() throws Exception { String testEndpoint = "ABCDEFG.iot.us-central-1.amazonaws.com"; assertEquals(Region.getRegion(Regions.US_EAST_1), AwsIotEndpointUtility.getRegionFromIotEndpoint(testEndpoint)); }
@Test public void testGetRegionFromCustomIotEndpoint() throws Exception { String testEndpoint = "ABCDEFG.iot.us-east-1.amazonaws.com"; assertEquals(Region.getRegion(Regions.US_EAST_1), AwsIotEndpointUtility.getRegionFromIotEndpoint(testEndpoint)); }
@Test public void testGetRegionFromCustomIotEndpointWithPort() throws Exception { String testEndpoint = "ABCDEFG.iot.us-east-1.amazonaws.com:443"; assertEquals(Region.getRegion(Regions.US_EAST_1), AwsIotEndpointUtility.getRegionFromIotEndpoint(testEndpoint)); }
@Test(expected=IllegalArgumentException.class) public void testGetRegionFromCustomIotEndpointBadRegion1() throws Exception { String testEndpoint = "ABCDEFG.iot.us-east.amazonaws.com"; assertEquals(Region.getRegion(Regions.US_EAST_1), AwsIotEndpointUtility.getRegionFromIotEndpoint(testEndpoint)); }
@Test public void testGetRegionFromDataIotEndpointWithPort() throws Exception { String testEndpoint = "data.iot.us-east-1.amazonaws.com:443"; assertEquals(Region.getRegion(Regions.US_EAST_1), AwsIotEndpointUtility.getRegionFromIotEndpoint(testEndpoint)); }
@Test public void testRegionFromCNEndpoint() throws Exception { String testEndpoint = "random.iot.cn-north-1.amazonaws.com.cn"; assertEquals(Region.getRegion(Regions.CN_NORTH_1), AwsIotEndpointUtility.getRegionFromIotEndpoint(testEndpoint)); }
/** * Constructs a new AWSIotMqttManager. * * @param mqttClientId MQTT client ID to use with this client. * @param endpoint AWS IoT endpoint. * Expected endpoint formats: * XXXXXXX.iot.[region].amazonaws.com or * XXXXXXX-ats.iot.[region].amazonaws.com or * XXXXXXX.ats.iot.cn-north-1.amazonaws.com.cn */ public AWSIotMqttManager(String mqttClientId, String endpoint) { if (mqttClientId == null || mqttClientId.isEmpty()) { throw new IllegalArgumentException("mqttClientId is null or empty"); } if (endpoint == null) { throw new IllegalArgumentException("endpoint is null"); } this.topicListeners = new ConcurrentHashMap<String, AWSIotMqttTopic>(); this.mqttMessageQueue = new ConcurrentLinkedQueue<AWSIotMqttQueueMessage>(); this.mqttClientId = mqttClientId; this.endpoint = endpoint; this.accountEndpointPrefix = null; this.region = AwsIotEndpointUtility.getRegionFromIotEndpoint(endpoint); initDefaults(); }
Region signerRegion = AwsIotEndpointUtility.getRegionFromIotEndpoint(endpoint); String signerRegionName = signerRegion.getName();