From b5a261f300febbf1a35c2c750cbf3b11cfa622cc Mon Sep 17 00:00:00 2001 From: Roshan Kumar Date: Mon, 4 Mar 2019 16:21:59 -0800 Subject: [PATCH 01/14] Added API to enable users to pass additional key-value pairs which are appended to the username used for connection. --- CHANGELOG.md | 7 ++++ .../iot/AWSIotMqttManager.java | 34 +++++++++++++++++-- .../iot/MqttManagerIntegrationTest.java | 2 +- 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 66d1de4b70..cb91cc5626 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Change Log - AWS SDK for Android +## [Release 2.12.3](https://github.com/aws/aws-sdk-android/releases/tag/release_v2.12.3) + +### Enhancements + +* **AWS IoT** + * AWSIotMqttManager now exposes an API `setUsernameFields` for clients to pass additional key-value pairs which are appended to the username used for connection. + ## [Release 2.12.2](https://github.com/aws/aws-sdk-android/releases/tag/release_v2.12.2) ### Misc. Updates diff --git a/aws-android-sdk-iot/src/main/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManager.java b/aws-android-sdk-iot/src/main/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManager.java index 40acfbc5d5..0a3ec4038b 100644 --- a/aws-android-sdk-iot/src/main/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManager.java +++ b/aws-android-sdk-iot/src/main/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManager.java @@ -162,6 +162,11 @@ public class AWSIotMqttManager { */ private boolean metricsIsEnabled = true; + /** User-specified map of additional values to be passed as components of + * username for the connection in addition to SDK and Version + */ + private Map usernameFields; + /** * This is your custom endpoint that allows you to connect to AWS IoT. */ @@ -210,6 +215,24 @@ public boolean isMetricsEnabled() { /** Override value for System.currentTimeInMillis. Used for unit testing reconnect logic. */ private Long unitTestMillisOverride; + /** + * Sets the usernameFields map. + * + * @param usernameFields usernameFields map + */ + public void setUsernameFields(Map usernameFields) { + this.usernameFields = usernameFields; + } + + /** + * Returns the usernameFields map + * + * @return usernameFields map + */ + public Map getUsernameFields() { + return usernameFields; + } + /** * Return the customer specific endpoint prefix. * @@ -799,7 +822,7 @@ public void run() { /** * Connect to the MQTT service. * - * @param options MQTT connect options containing a TLS socket factory for authentication. + * @param options MQTT connect options containing a TLS socket factory for authentication. */ private void mqttConnect(MqttConnectOptions options) { LOGGER.debug("ready to do mqtt connect"); @@ -808,7 +831,14 @@ private void mqttConnect(MqttConnectOptions options) { options.setKeepAliveInterval(userKeepAlive); if (isMetricsEnabled()) { - options.setUserName("?SDK=Android&Version=" + SDK_VERSION); + StringBuilder username = new StringBuilder("?SDK=Android&Version=" + SDK_VERSION); + + // Append each of the user-specified key-value pair to the username field for the connection + for (Map.Entry usernameField : usernameFields.entrySet()) { + username.append("&" + usernameField.getKey() + "=" + usernameField.getValue()); + } + + options.setUserName(username.toString()); } LOGGER.info("metrics collection is " + (isMetricsEnabled() ? "enabled" : "disabled") + diff --git a/aws-android-sdk-iot/src/test/java/com/amazonaws/mobileconnectors/iot/MqttManagerIntegrationTest.java b/aws-android-sdk-iot/src/test/java/com/amazonaws/mobileconnectors/iot/MqttManagerIntegrationTest.java index bfd93c372d..b921c5a59b 100644 --- a/aws-android-sdk-iot/src/test/java/com/amazonaws/mobileconnectors/iot/MqttManagerIntegrationTest.java +++ b/aws-android-sdk-iot/src/test/java/com/amazonaws/mobileconnectors/iot/MqttManagerIntegrationTest.java @@ -864,7 +864,7 @@ public void onMessageArrived(String topic, byte[] data) { */ @Ignore @Test - public void mqttPersistantSession() throws Exception { + public void mqttPersistentSession() throws Exception { final ArrayList statuses = new ArrayList(); final ArrayList messages = new ArrayList(); From 4540c7bb069dc47c76ba02307598d627f6995f96 Mon Sep 17 00:00:00 2001 From: Roshan Kumar Date: Mon, 4 Mar 2019 16:34:28 -0800 Subject: [PATCH 02/14] Add null check for usernameFields map --- .../amazonaws/mobileconnectors/iot/AWSIotMqttManager.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/aws-android-sdk-iot/src/main/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManager.java b/aws-android-sdk-iot/src/main/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManager.java index 0a3ec4038b..07f29461ca 100644 --- a/aws-android-sdk-iot/src/main/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManager.java +++ b/aws-android-sdk-iot/src/main/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManager.java @@ -833,9 +833,11 @@ private void mqttConnect(MqttConnectOptions options) { if (isMetricsEnabled()) { StringBuilder username = new StringBuilder("?SDK=Android&Version=" + SDK_VERSION); - // Append each of the user-specified key-value pair to the username field for the connection - for (Map.Entry usernameField : usernameFields.entrySet()) { - username.append("&" + usernameField.getKey() + "=" + usernameField.getValue()); + if (usernameFields != null) { + // Append each of the user-specified key-value pair to the username field for the connection + for (Map.Entry usernameField : usernameFields.entrySet()) { + username.append("&" + usernameField.getKey() + "=" + usernameField.getValue()); + } } options.setUserName(username.toString()); From 1b95b22aa84c0e951663ce9d62e8b77ebc9a29b1 Mon Sep 17 00:00:00 2001 From: Roshan Kumar Date: Tue, 5 Mar 2019 10:44:13 -0800 Subject: [PATCH 03/14] Add unit test snippets for username fields --- .../iot/AWSIotMqttManagerTest.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/aws-android-sdk-iot/src/test/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManagerTest.java b/aws-android-sdk-iot/src/test/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManagerTest.java index 6a5c06281d..c70de115e7 100644 --- a/aws-android-sdk-iot/src/test/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManagerTest.java +++ b/aws-android-sdk-iot/src/test/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManagerTest.java @@ -3,6 +3,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import com.amazonaws.AmazonClientException; @@ -27,6 +28,8 @@ import java.security.KeyPair; import java.security.KeyStore; import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; import java.util.concurrent.ConcurrentLinkedQueue; @RunWith(RobolectricTestRunner.class) @@ -87,6 +90,13 @@ public void testCreateClientWithPrefix() throws Exception { AWSIotMqttManager testClient = new AWSIotMqttManager("test-client", Region.getRegion(Regions.US_EAST_1), TEST_ENDPOINT_PREFIX); + // Set username fields + Map usernameFields = new HashMap(); + usernameFields.put("AFRSDK", "Android"); + usernameFields.put("AFRSDKVersion", "1.0.0"); + usernameFields.put("AFRLibVersion", "1.4.1"); + testClient.setUsernameFields(usernameFields); + assertEquals(true, testClient.isAutoReconnect()); assertEquals(4, testClient.getReconnectTimeout()); assertEquals(4, testClient.getMinReconnectRetryTime()); @@ -98,6 +108,7 @@ public void testCreateClientWithPrefix() throws Exception { assertEquals(100L, (long)testClient.getOfflinePublishQueueBound()); assertEquals(TEST_ENDPOINT_PREFIX, testClient.getAccountEndpointPrefix()); assertEquals(MqttManagerConnectionState.Disconnected, testClient.getConnectionState()); + assertNotNull(testClient.getUsernameFields()); testClient.setAutoReconnect(false); @@ -130,7 +141,7 @@ public void testCreateClientWithPrefix() throws Exception { @Test public void testCreateClientWithEndpoint() throws Exception { AWSIotMqttManager testClient = new AWSIotMqttManager("test-client", - "ABCDEFG.iot.us-east-1.amazonaws.com"); + TEST_ENDPOINT); assertEquals(true, testClient.isAutoReconnect()); assertEquals(4, testClient.getReconnectTimeout()); @@ -178,12 +189,12 @@ public void testIllegalReconnectTimes() throws Exception { @Test(expected = IllegalArgumentException.class) public void testCreateClientWithEndpointNullClientId() throws Exception { - AWSIotMqttManager testClient = new AWSIotMqttManager(null, "ABCDEFG.iot.us-east-1.amazonaws.com"); + AWSIotMqttManager testClient = new AWSIotMqttManager(null, TEST_ENDPOINT); } @Test(expected = IllegalArgumentException.class) public void testCreateClientWithEndpointEmptyClientId() throws Exception { - AWSIotMqttManager testClient = new AWSIotMqttManager("", "ABCDEFG.iot.us-east-1.amazonaws.com"); + AWSIotMqttManager testClient = new AWSIotMqttManager("", TEST_ENDPOINT); } @Test(expected = IllegalArgumentException.class) From 9c051bf6842b35fced13dda9c0639628d384b891 Mon Sep 17 00:00:00 2001 From: Roshan Kumar Date: Tue, 12 Mar 2019 07:23:58 -0700 Subject: [PATCH 04/14] Add length validation for usename fields --- .../iot/AWSIotMqttManager.java | 34 ++++++++++++++----- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/aws-android-sdk-iot/src/main/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManager.java b/aws-android-sdk-iot/src/main/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManager.java index 07f29461ca..92a8eb9aa5 100644 --- a/aws-android-sdk-iot/src/main/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManager.java +++ b/aws-android-sdk-iot/src/main/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManager.java @@ -222,6 +222,30 @@ public boolean isMetricsEnabled() { */ public void setUsernameFields(Map usernameFields) { this.usernameFields = usernameFields; + + StringBuilder username = getUsername(); + + if(username.length() > 225) { + throw new IllegalArgumentException("Total number of characters in username fields" + + " cannot exceed " + (255 - ("?SDK=Android&Version=" + SDK_VERSION).length())); + } + } + + /** + * Utility method to return username from the usernameFields map + * + * @return username + */ + private StringBuilder getUsername() { + StringBuilder username = new StringBuilder("?SDK=Android&Version=" + SDK_VERSION); + + if (usernameFields != null) { + // Append each of the user-specified key-value pair to the username field for the connection + for (Map.Entry usernameField : usernameFields.entrySet()) { + username.append("&" + usernameField.getKey() + "=" + usernameField.getValue()); + } + } + return username; } /** @@ -831,15 +855,7 @@ private void mqttConnect(MqttConnectOptions options) { options.setKeepAliveInterval(userKeepAlive); if (isMetricsEnabled()) { - StringBuilder username = new StringBuilder("?SDK=Android&Version=" + SDK_VERSION); - - if (usernameFields != null) { - // Append each of the user-specified key-value pair to the username field for the connection - for (Map.Entry usernameField : usernameFields.entrySet()) { - username.append("&" + usernameField.getKey() + "=" + usernameField.getValue()); - } - } - + StringBuilder username = getUsername(); options.setUserName(username.toString()); } LOGGER.info("metrics collection is " + From dab378d04f441f85af8b9a003125df585645a188 Mon Sep 17 00:00:00 2001 From: Roshan Kumar Date: Tue, 12 Mar 2019 07:45:48 -0700 Subject: [PATCH 05/14] Rename usernameFields to userMetaData --- .../iot/AWSIotMqttManager.java | 25 ++++++------------- .../iot/AWSIotMqttManagerTest.java | 4 +-- 2 files changed, 10 insertions(+), 19 deletions(-) diff --git a/aws-android-sdk-iot/src/main/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManager.java b/aws-android-sdk-iot/src/main/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManager.java index 92a8eb9aa5..323748b2df 100644 --- a/aws-android-sdk-iot/src/main/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManager.java +++ b/aws-android-sdk-iot/src/main/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManager.java @@ -165,7 +165,7 @@ public class AWSIotMqttManager { /** User-specified map of additional values to be passed as components of * username for the connection in addition to SDK and Version */ - private Map usernameFields; + Map userMetaData; /** * This is your custom endpoint that allows you to connect to AWS IoT. @@ -216,12 +216,12 @@ public boolean isMetricsEnabled() { private Long unitTestMillisOverride; /** - * Sets the usernameFields map. + * Sets the userMetaData map. * - * @param usernameFields usernameFields map + * @param userMetaData userMetaData map */ - public void setUsernameFields(Map usernameFields) { - this.usernameFields = usernameFields; + public void setUserMetaData(Map userMetaData) { + this.userMetaData = userMetaData; StringBuilder username = getUsername(); @@ -232,31 +232,22 @@ public void setUsernameFields(Map usernameFields) { } /** - * Utility method to return username from the usernameFields map + * Utility method to return username from the userMetaData map * * @return username */ private StringBuilder getUsername() { StringBuilder username = new StringBuilder("?SDK=Android&Version=" + SDK_VERSION); - if (usernameFields != null) { + if (userMetaData != null) { // Append each of the user-specified key-value pair to the username field for the connection - for (Map.Entry usernameField : usernameFields.entrySet()) { + for (Map.Entry usernameField : userMetaData.entrySet()) { username.append("&" + usernameField.getKey() + "=" + usernameField.getValue()); } } return username; } - /** - * Returns the usernameFields map - * - * @return usernameFields map - */ - public Map getUsernameFields() { - return usernameFields; - } - /** * Return the customer specific endpoint prefix. * diff --git a/aws-android-sdk-iot/src/test/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManagerTest.java b/aws-android-sdk-iot/src/test/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManagerTest.java index c70de115e7..030a52aefa 100644 --- a/aws-android-sdk-iot/src/test/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManagerTest.java +++ b/aws-android-sdk-iot/src/test/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManagerTest.java @@ -95,7 +95,7 @@ public void testCreateClientWithPrefix() throws Exception { usernameFields.put("AFRSDK", "Android"); usernameFields.put("AFRSDKVersion", "1.0.0"); usernameFields.put("AFRLibVersion", "1.4.1"); - testClient.setUsernameFields(usernameFields); + testClient.setUserMetaData(usernameFields); assertEquals(true, testClient.isAutoReconnect()); assertEquals(4, testClient.getReconnectTimeout()); @@ -108,7 +108,7 @@ public void testCreateClientWithPrefix() throws Exception { assertEquals(100L, (long)testClient.getOfflinePublishQueueBound()); assertEquals(TEST_ENDPOINT_PREFIX, testClient.getAccountEndpointPrefix()); assertEquals(MqttManagerConnectionState.Disconnected, testClient.getConnectionState()); - assertNotNull(testClient.getUsernameFields()); + assertNotNull(testClient.userMetaData); testClient.setAutoReconnect(false); From f52d5f37d9035dc2a4b0b0744b4e6c3d02b25c92 Mon Sep 17 00:00:00 2001 From: Roshan Kumar Date: Tue, 12 Mar 2019 07:50:01 -0700 Subject: [PATCH 06/14] Update Changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a293bcf83..a3f86b6070 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## [Release 2.12.4](https://github.com/aws/aws-sdk-android/releases/tag/release_v2.12.4) +### Enhancements + * **AWS IoT** * AWSIotMqttManager now exposes an API `setUsernameFields` for clients to pass additional key-value pairs which are appended to the username used for connection. From d9134d592d0e17e8283a275905e3e268a20bb798 Mon Sep 17 00:00:00 2001 From: Roshan Kumar Date: Tue, 12 Mar 2019 10:45:05 -0700 Subject: [PATCH 07/14] Rename the API to add user meta data --- .../com/amazonaws/mobileconnectors/iot/AWSIotMqttManager.java | 2 +- .../amazonaws/mobileconnectors/iot/AWSIotMqttManagerTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/aws-android-sdk-iot/src/main/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManager.java b/aws-android-sdk-iot/src/main/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManager.java index 323748b2df..7b196effd4 100644 --- a/aws-android-sdk-iot/src/main/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManager.java +++ b/aws-android-sdk-iot/src/main/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManager.java @@ -220,7 +220,7 @@ public boolean isMetricsEnabled() { * * @param userMetaData userMetaData map */ - public void setUserMetaData(Map userMetaData) { + public void addUserMetaData(Map userMetaData) { this.userMetaData = userMetaData; StringBuilder username = getUsername(); diff --git a/aws-android-sdk-iot/src/test/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManagerTest.java b/aws-android-sdk-iot/src/test/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManagerTest.java index 030a52aefa..60c3467abc 100644 --- a/aws-android-sdk-iot/src/test/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManagerTest.java +++ b/aws-android-sdk-iot/src/test/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManagerTest.java @@ -95,7 +95,7 @@ public void testCreateClientWithPrefix() throws Exception { usernameFields.put("AFRSDK", "Android"); usernameFields.put("AFRSDKVersion", "1.0.0"); usernameFields.put("AFRLibVersion", "1.4.1"); - testClient.setUserMetaData(usernameFields); + testClient.addUserMetaData(usernameFields); assertEquals(true, testClient.isAutoReconnect()); assertEquals(4, testClient.getReconnectTimeout()); From cae13ab032253e42909744a312df9773303d528b Mon Sep 17 00:00:00 2001 From: Roshan Kumar Date: Tue, 12 Mar 2019 11:00:50 -0700 Subject: [PATCH 08/14] Refactoring --- .../mobileconnectors/iot/AWSIotMqttManagerTest.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/aws-android-sdk-iot/src/test/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManagerTest.java b/aws-android-sdk-iot/src/test/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManagerTest.java index 60c3467abc..1fbb2f1cad 100644 --- a/aws-android-sdk-iot/src/test/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManagerTest.java +++ b/aws-android-sdk-iot/src/test/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManagerTest.java @@ -91,11 +91,11 @@ public void testCreateClientWithPrefix() throws Exception { Region.getRegion(Regions.US_EAST_1), TEST_ENDPOINT_PREFIX); // Set username fields - Map usernameFields = new HashMap(); - usernameFields.put("AFRSDK", "Android"); - usernameFields.put("AFRSDKVersion", "1.0.0"); - usernameFields.put("AFRLibVersion", "1.4.1"); - testClient.addUserMetaData(usernameFields); + Map userMetaData = new HashMap(); + userMetaData.put("AFRSDK", "Android"); + userMetaData.put("AFRSDKVersion", "1.0.0"); + userMetaData.put("AFRLibVersion", "1.4.1"); + testClient.addUserMetaData(userMetaData); assertEquals(true, testClient.isAutoReconnect()); assertEquals(4, testClient.getReconnectTimeout()); From 9af41c967ee7453385e7b6dc56b986410ab60ed6 Mon Sep 17 00:00:00 2001 From: Roshan Kumar Date: Tue, 12 Mar 2019 15:32:24 -0700 Subject: [PATCH 09/14] Refactoring --- .../iot/AWSIotMqttManager.java | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/aws-android-sdk-iot/src/main/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManager.java b/aws-android-sdk-iot/src/main/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManager.java index 7b196effd4..53ed23f7da 100644 --- a/aws-android-sdk-iot/src/main/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManager.java +++ b/aws-android-sdk-iot/src/main/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManager.java @@ -223,9 +223,9 @@ public boolean isMetricsEnabled() { public void addUserMetaData(Map userMetaData) { this.userMetaData = userMetaData; - StringBuilder username = getUsername(); + StringBuilder userMetadata = getUserMetaData(); - if(username.length() > 225) { + if(userMetadata.length() > 225) { throw new IllegalArgumentException("Total number of characters in username fields" + " cannot exceed " + (255 - ("?SDK=Android&Version=" + SDK_VERSION).length())); } @@ -236,16 +236,16 @@ public void addUserMetaData(Map userMetaData) { * * @return username */ - private StringBuilder getUsername() { - StringBuilder username = new StringBuilder("?SDK=Android&Version=" + SDK_VERSION); + private StringBuilder getUserMetaData() { + StringBuilder userMetaData = new StringBuilder("?SDK=Android&Version=" + SDK_VERSION); - if (userMetaData != null) { + if (this.userMetaData != null) { // Append each of the user-specified key-value pair to the username field for the connection - for (Map.Entry usernameField : userMetaData.entrySet()) { - username.append("&" + usernameField.getKey() + "=" + usernameField.getValue()); + for (Map.Entry metaData : this.userMetaData.entrySet()) { + userMetaData.append("&" + metaData.getKey() + "=" + metaData.getValue()); } } - return username; + return userMetaData; } /** @@ -845,8 +845,9 @@ private void mqttConnect(MqttConnectOptions options) { options.setCleanSession(cleanSession); options.setKeepAliveInterval(userKeepAlive); + // Setup userName if metrics are enabled. We use the connection username as metadata for metrics calculation. if (isMetricsEnabled()) { - StringBuilder username = getUsername(); + StringBuilder username = getUserMetaData(); options.setUserName(username.toString()); } LOGGER.info("metrics collection is " + From 728227c90f8e56fb565f3417fa1d6a2cf19c9b0e Mon Sep 17 00:00:00 2001 From: Roshan Kumar Date: Tue, 12 Mar 2019 16:45:54 -0700 Subject: [PATCH 10/14] Truncate usermeta data when it exceed the required length --- CHANGELOG.md | 2 +- .../iot/AWSIotMqttManager.java | 43 ++++++++----------- .../iot/AWSIotMqttManagerTest.java | 2 +- 3 files changed, 20 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a3f86b6070..9cd3103f80 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ ### Enhancements * **AWS IoT** - * AWSIotMqttManager now exposes an API `setUsernameFields` for clients to pass additional key-value pairs which are appended to the username used for connection. + * AWSIotMqttManager now exposes an API `addUserMetaData` for clients to pass additional key-value pairs which are appended to the username used for connection. ## [Release 2.12.3](https://github.com/aws/aws-sdk-android/releases/tag/release_v2.12.3) diff --git a/aws-android-sdk-iot/src/main/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManager.java b/aws-android-sdk-iot/src/main/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManager.java index 53ed23f7da..d76f330fbd 100644 --- a/aws-android-sdk-iot/src/main/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManager.java +++ b/aws-android-sdk-iot/src/main/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManager.java @@ -165,7 +165,10 @@ public class AWSIotMqttManager { /** User-specified map of additional values to be passed as components of * username for the connection in addition to SDK and Version */ - Map userMetaData; + Map userMetaDataMap; + + /** User metadata string. */ + private String userMetaData; /** * This is your custom endpoint that allows you to connect to AWS IoT. @@ -218,34 +221,25 @@ public boolean isMetricsEnabled() { /** * Sets the userMetaData map. * - * @param userMetaData userMetaData map + * @param userMetaDataMap userMetaData map */ - public void addUserMetaData(Map userMetaData) { - this.userMetaData = userMetaData; - - StringBuilder userMetadata = getUserMetaData(); - - if(userMetadata.length() > 225) { - throw new IllegalArgumentException("Total number of characters in username fields" + - " cannot exceed " + (255 - ("?SDK=Android&Version=" + SDK_VERSION).length())); - } - } + public void addUserMetaData(Map userMetaDataMap) { + this.userMetaDataMap = userMetaDataMap; - /** - * Utility method to return username from the userMetaData map - * - * @return username - */ - private StringBuilder getUserMetaData() { - StringBuilder userMetaData = new StringBuilder("?SDK=Android&Version=" + SDK_VERSION); + StringBuilder userMetadata = new StringBuilder("?SDK=Android&Version=" + SDK_VERSION); - if (this.userMetaData != null) { + if (this.userMetaDataMap != null) { // Append each of the user-specified key-value pair to the username field for the connection - for (Map.Entry metaData : this.userMetaData.entrySet()) { - userMetaData.append("&" + metaData.getKey() + "=" + metaData.getValue()); + for (Map.Entry metaData : this.userMetaDataMap.entrySet()) { + userMetadata.append("&" + metaData.getKey() + "=" + metaData.getValue()); } } - return userMetaData; + + if(userMetadata.length() > 255) { + LOGGER.warn("Too many characters. User metadata was truncated.", new IllegalArgumentException("Total number of characters in username fields" + + " cannot exceed " + (255 - ("?SDK=Android&Version=" + SDK_VERSION).length()))); + this.userMetaData = userMetadata.substring(0, 255); + } } /** @@ -847,8 +841,7 @@ private void mqttConnect(MqttConnectOptions options) { // Setup userName if metrics are enabled. We use the connection username as metadata for metrics calculation. if (isMetricsEnabled()) { - StringBuilder username = getUserMetaData(); - options.setUserName(username.toString()); + options.setUserName(userMetaData); } LOGGER.info("metrics collection is " + (isMetricsEnabled() ? "enabled" : "disabled") + diff --git a/aws-android-sdk-iot/src/test/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManagerTest.java b/aws-android-sdk-iot/src/test/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManagerTest.java index 1fbb2f1cad..aa9832516b 100644 --- a/aws-android-sdk-iot/src/test/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManagerTest.java +++ b/aws-android-sdk-iot/src/test/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManagerTest.java @@ -108,7 +108,7 @@ public void testCreateClientWithPrefix() throws Exception { assertEquals(100L, (long)testClient.getOfflinePublishQueueBound()); assertEquals(TEST_ENDPOINT_PREFIX, testClient.getAccountEndpointPrefix()); assertEquals(MqttManagerConnectionState.Disconnected, testClient.getConnectionState()); - assertNotNull(testClient.userMetaData); + assertNotNull(testClient.userMetaDataMap); testClient.setAutoReconnect(false); From 4a7e74f7541d110b6c2ba4a967ffe3df90625f17 Mon Sep 17 00:00:00 2001 From: Roshan Kumar Date: Tue, 12 Mar 2019 17:04:39 -0700 Subject: [PATCH 11/14] Refactoring --- .../mobileconnectors/iot/AWSIotMqttManager.java | 15 +++++---------- .../iot/AWSIotMqttManagerTest.java | 2 +- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/aws-android-sdk-iot/src/main/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManager.java b/aws-android-sdk-iot/src/main/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManager.java index d76f330fbd..1fedd5f42d 100644 --- a/aws-android-sdk-iot/src/main/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManager.java +++ b/aws-android-sdk-iot/src/main/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManager.java @@ -162,13 +162,8 @@ public class AWSIotMqttManager { */ private boolean metricsIsEnabled = true; - /** User-specified map of additional values to be passed as components of - * username for the connection in addition to SDK and Version - */ - Map userMetaDataMap; - /** User metadata string. */ - private String userMetaData; + String userMetaData; /** * This is your custom endpoint that allows you to connect to AWS IoT. @@ -224,13 +219,11 @@ public boolean isMetricsEnabled() { * @param userMetaDataMap userMetaData map */ public void addUserMetaData(Map userMetaDataMap) { - this.userMetaDataMap = userMetaDataMap; - StringBuilder userMetadata = new StringBuilder("?SDK=Android&Version=" + SDK_VERSION); - if (this.userMetaDataMap != null) { + if (userMetaDataMap != null) { // Append each of the user-specified key-value pair to the username field for the connection - for (Map.Entry metaData : this.userMetaDataMap.entrySet()) { + for (Map.Entry metaData : userMetaDataMap.entrySet()) { userMetadata.append("&" + metaData.getKey() + "=" + metaData.getValue()); } } @@ -239,6 +232,8 @@ public void addUserMetaData(Map userMetaDataMap) { LOGGER.warn("Too many characters. User metadata was truncated.", new IllegalArgumentException("Total number of characters in username fields" + " cannot exceed " + (255 - ("?SDK=Android&Version=" + SDK_VERSION).length()))); this.userMetaData = userMetadata.substring(0, 255); + } else { + this.userMetaData = userMetadata.toString(); } } diff --git a/aws-android-sdk-iot/src/test/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManagerTest.java b/aws-android-sdk-iot/src/test/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManagerTest.java index aa9832516b..1fbb2f1cad 100644 --- a/aws-android-sdk-iot/src/test/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManagerTest.java +++ b/aws-android-sdk-iot/src/test/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManagerTest.java @@ -108,7 +108,7 @@ public void testCreateClientWithPrefix() throws Exception { assertEquals(100L, (long)testClient.getOfflinePublishQueueBound()); assertEquals(TEST_ENDPOINT_PREFIX, testClient.getAccountEndpointPrefix()); assertEquals(MqttManagerConnectionState.Disconnected, testClient.getConnectionState()); - assertNotNull(testClient.userMetaDataMap); + assertNotNull(testClient.userMetaData); testClient.setAutoReconnect(false); From 8e267f5bf19961edbdde0fc548d133ee76be1438 Mon Sep 17 00:00:00 2001 From: Roshan Kumar Date: Tue, 12 Mar 2019 17:39:03 -0700 Subject: [PATCH 12/14] Refactoring --- .../com/amazonaws/mobileconnectors/iot/AWSIotMqttManager.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/aws-android-sdk-iot/src/main/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManager.java b/aws-android-sdk-iot/src/main/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManager.java index 1fedd5f42d..1ea72fd77f 100644 --- a/aws-android-sdk-iot/src/main/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManager.java +++ b/aws-android-sdk-iot/src/main/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManager.java @@ -222,14 +222,14 @@ public void addUserMetaData(Map userMetaDataMap) { StringBuilder userMetadata = new StringBuilder("?SDK=Android&Version=" + SDK_VERSION); if (userMetaDataMap != null) { - // Append each of the user-specified key-value pair to the username field for the connection + // Append each of the user-specified key-value pair to the user metadata for the connection for (Map.Entry metaData : userMetaDataMap.entrySet()) { userMetadata.append("&" + metaData.getKey() + "=" + metaData.getValue()); } } if(userMetadata.length() > 255) { - LOGGER.warn("Too many characters. User metadata was truncated.", new IllegalArgumentException("Total number of characters in username fields" + + LOGGER.warn("Too many characters. User metadata was truncated.", new IllegalArgumentException("Total number of characters in user metadata" + " cannot exceed " + (255 - ("?SDK=Android&Version=" + SDK_VERSION).length()))); this.userMetaData = userMetadata.substring(0, 255); } else { From 2eb998c8152e5f66a8f337a2fa10eebf11d090ff Mon Sep 17 00:00:00 2001 From: Roshan Kumar Date: Tue, 12 Mar 2019 17:40:39 -0700 Subject: [PATCH 13/14] Refactoring --- .../amazonaws/mobileconnectors/iot/AWSIotMqttManagerTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws-android-sdk-iot/src/test/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManagerTest.java b/aws-android-sdk-iot/src/test/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManagerTest.java index 1fbb2f1cad..a3b5e1190b 100644 --- a/aws-android-sdk-iot/src/test/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManagerTest.java +++ b/aws-android-sdk-iot/src/test/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManagerTest.java @@ -90,7 +90,7 @@ public void testCreateClientWithPrefix() throws Exception { AWSIotMqttManager testClient = new AWSIotMqttManager("test-client", Region.getRegion(Regions.US_EAST_1), TEST_ENDPOINT_PREFIX); - // Set username fields + // Set user metadata Map userMetaData = new HashMap(); userMetaData.put("AFRSDK", "Android"); userMetaData.put("AFRSDKVersion", "1.0.0"); From 360112b8569f76a755d869639e61749b48ea2f50 Mon Sep 17 00:00:00 2001 From: Roshan Kumar Date: Wed, 13 Mar 2019 07:36:28 -0700 Subject: [PATCH 14/14] Refactoring --- .../com/amazonaws/mobileconnectors/iot/AWSIotMqttManager.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/aws-android-sdk-iot/src/main/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManager.java b/aws-android-sdk-iot/src/main/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManager.java index 1ea72fd77f..e3bb858c21 100644 --- a/aws-android-sdk-iot/src/main/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManager.java +++ b/aws-android-sdk-iot/src/main/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManager.java @@ -220,6 +220,7 @@ public boolean isMetricsEnabled() { */ public void addUserMetaData(Map userMetaDataMap) { StringBuilder userMetadata = new StringBuilder("?SDK=Android&Version=" + SDK_VERSION); + int baseLength = userMetadata.length(); if (userMetaDataMap != null) { // Append each of the user-specified key-value pair to the user metadata for the connection @@ -230,7 +231,7 @@ public void addUserMetaData(Map userMetaDataMap) { if(userMetadata.length() > 255) { LOGGER.warn("Too many characters. User metadata was truncated.", new IllegalArgumentException("Total number of characters in user metadata" + - " cannot exceed " + (255 - ("?SDK=Android&Version=" + SDK_VERSION).length()))); + " cannot exceed " + (255 - baseLength))); this.userMetaData = userMetadata.substring(0, 255); } else { this.userMetaData = userMetadata.toString();