Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added API to accept key-value pairs which are appended to the connection username #765

Merged
merged 16 commits into from
Mar 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Change Log - AWS SDK for Android

## [Release 2.12.5](https://github.com/aws/aws-sdk-android/releases/tag/release_v2.12.5)

### Enhancements

desokroshan marked this conversation as resolved.
Show resolved Hide resolved
* **AWS IoT**
* 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.4](https://github.com/aws/aws-sdk-android/releases/tag/release_v2.12.4)

* **AWS Core**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ public class AWSIotMqttManager {
*/
private boolean metricsIsEnabled = true;

/** User metadata string. */
String userMetaData;

/**
* This is your custom endpoint that allows you to connect to AWS IoT.
*/
Expand Down Expand Up @@ -210,6 +213,31 @@ public boolean isMetricsEnabled() {
/** Override value for System.currentTimeInMillis. Used for unit testing reconnect logic. */
private Long unitTestMillisOverride;

/**
* Sets the userMetaData map.
*
* @param userMetaDataMap userMetaData map
*/
public void addUserMetaData(Map<String, String> 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
for (Map.Entry<String, String> 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 user metadata" +
" cannot exceed " + (255 - baseLength)));
this.userMetaData = userMetadata.substring(0, 255);
} else {
this.userMetaData = userMetadata.toString();
}
}

/**
* Return the customer specific endpoint prefix.
*
Expand Down Expand Up @@ -799,16 +827,17 @@ 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");

options.setCleanSession(cleanSession);
options.setKeepAliveInterval(userKeepAlive);

// Setup userName if metrics are enabled. We use the connection username as metadata for metrics calculation.
if (isMetricsEnabled()) {
options.setUserName("?SDK=Android&Version=" + SDK_VERSION);
options.setUserName(userMetaData);
}
LOGGER.info("metrics collection is " +
(isMetricsEnabled() ? "enabled" : "disabled") +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -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 user metadata
Map<String, String> userMetaData = new HashMap<String, String>();
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());
assertEquals(4, testClient.getMinReconnectRetryTime());
Expand All @@ -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.userMetaData);


testClient.setAutoReconnect(false);
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,7 @@ public void onMessageArrived(String topic, byte[] data) {
*/
@Ignore
@Test
public void mqttPersistantSession() throws Exception {
public void mqttPersistentSession() throws Exception {
mutablealligator marked this conversation as resolved.
Show resolved Hide resolved
final ArrayList<AWSIotMqttClientStatusCallback.AWSIotMqttClientStatus> statuses = new ArrayList<AWSIotMqttClientStatusCallback.AWSIotMqttClientStatus>();
final ArrayList<String> messages = new ArrayList<String>();

Expand Down