Skip to content

Commit b9b15d3

Browse files
authored
Allow updates to iot meta data field (#912)
* Allow updates to iot meta data field
1 parent be0b3f4 commit b9b15d3

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

aws-android-sdk-iot/src/main/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManager.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import java.security.NoSuchAlgorithmException;
4747
import java.security.NoSuchProviderException;
4848
import java.security.UnrecoverableKeyException;
49+
import java.util.HashMap;
4950
import java.util.concurrent.ConcurrentHashMap;
5051
import java.util.concurrent.ConcurrentLinkedQueue;
5152
import java.util.Map;
@@ -170,6 +171,11 @@ public class AWSIotMqttManager {
170171
*/
171172
String userMetaData = "?SDK=Android&Version=" + VersionInfoUtils.getVersion();
172173

174+
/**
175+
* Map to store user metadata components.
176+
*/
177+
Map<String, String> userMetaDataMap = new HashMap<String, String>();
178+
173179
/**
174180
* This is your custom endpoint that allows you to connect to AWS IoT.
175181
*/
@@ -214,10 +220,14 @@ public boolean isMetricsEnabled() {
214220
private Long unitTestMillisOverride;
215221

216222
/**
223+
* @deprecated Since 2.13.2 this method will be removed in the next minor version.
224+
* Please use updateUserMetaData method instead.
225+
*
217226
* Sets the userMetaData map.
218227
*
219228
* @param userMetaDataMap userMetaData map
220229
*/
230+
@Deprecated
221231
public void addUserMetaData(Map<String, String> userMetaDataMap) {
222232
StringBuilder userMetadata = new StringBuilder(this.userMetaData);
223233
int baseLength = userMetadata.length();
@@ -243,6 +253,45 @@ public void addUserMetaData(Map<String, String> userMetaDataMap) {
243253
}
244254
}
245255

256+
/**
257+
* This is an internal method.
258+
*
259+
* @param userMetaDataMap userMetaData map
260+
*/
261+
public void updateUserMetaData(Map<String, String> userMetaDataMap) {
262+
StringBuilder userMetadata = new StringBuilder("?SDK=Android&Version=" + VersionInfoUtils.getVersion());
263+
int baseLength = userMetadata.length();
264+
265+
// Update the meta data map
266+
if (userMetaDataMap != null) {
267+
for (Map.Entry<String, String> metaData : userMetaDataMap.entrySet()) {
268+
this.userMetaDataMap.put(metaData.getKey(), metaData.getValue());
269+
}
270+
}
271+
272+
// Append each of the user-specified key-value pair to the user metadata for the connection
273+
for (Map.Entry<String, String> metaData : this.userMetaDataMap.entrySet()) {
274+
if (!(metaData.getKey().equals("SDK") || metaData.getKey().equals("Version"))) {
275+
String metaDataValue = metaData.getValue();
276+
if (metaDataValue == null || "".equals(metaDataValue)){
277+
userMetadata.append("&" + metaData.getKey());
278+
} else {
279+
userMetadata.append("&" + metaData.getKey() + "=" + metaData.getValue());
280+
}
281+
} else {
282+
LOGGER.warn("Keynames 'SDK' and 'Version' are reserved and will be skipped");
283+
}
284+
}
285+
286+
if(userMetadata.length() > 255) {
287+
LOGGER.warn("Too many characters. User metadata was truncated.", new IllegalArgumentException("Total number of characters in user metadata" +
288+
" cannot exceed " + (255 - baseLength)));
289+
this.userMetaData = userMetadata.substring(0, 255);
290+
} else {
291+
this.userMetaData = userMetadata.toString();
292+
}
293+
}
294+
246295
/**
247296
* Return the customer specific endpoint prefix.
248297
*

aws-android-sdk-iot/src/test/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManagerTest.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,7 @@ public void testCreateClientWithPrefix() throws Exception {
9797
Map<String, String> userMetaData = new HashMap<String, String>();
9898
userMetaData.put("AFRSDK", "Android");
9999
userMetaData.put("AFRSDKVersion", "1.0.0");
100-
userMetaData.put("AFRLibVersion", "1.4.1");
101-
testClient.addUserMetaData(userMetaData);
100+
testClient.updateUserMetaData(userMetaData);
102101

103102
assertEquals(true, testClient.isAutoReconnect());
104103
assertEquals(4, testClient.getReconnectTimeout());
@@ -112,8 +111,14 @@ public void testCreateClientWithPrefix() throws Exception {
112111
assertEquals(TEST_ENDPOINT_PREFIX, testClient.getAccountEndpointPrefix());
113112
assertEquals(MqttManagerConnectionState.Disconnected, testClient.getConnectionState());
114113
assertEquals(testClient.userMetaData, "?SDK=Android&Version=" + VersionInfoUtils.getVersion() +
115-
"&AFRSDK=Android&AFRSDKVersion=1.0.0&AFRLibVersion=1.4.1");
114+
"&AFRSDK=Android&AFRSDKVersion=1.0.0");
116115

116+
userMetaData.put("AFRSDK", "Android");
117+
userMetaData.put("AFRSDKVersion", "1.0.1");
118+
userMetaData.put("AFRLibVersion", "1.4.1");
119+
testClient.updateUserMetaData(userMetaData);
120+
assertEquals(testClient.userMetaData, "?SDK=Android&Version=" + VersionInfoUtils.getVersion() +
121+
"&AFRSDK=Android&AFRSDKVersion=1.0.1&AFRLibVersion=1.4.1");
117122

118123
testClient.setAutoReconnect(false);
119124
testClient.setReconnectRetryLimits(64, 128);

0 commit comments

Comments
 (0)