in iothub/device/iot-device-client/src/main/java/com/microsoft/azure/sdk/iot/device/IotHubConnectionString.java [57:112]
public IotHubConnectionString(String connectionString) throws IllegalArgumentException
{
/* Codes_SRS_IOTHUB_CONNECTIONSTRING_21_016: [If the connection string is null or empty, the constructor shall throw an IllegalArgumentException.] */
if ((connectionString == null) || connectionString.isEmpty())
{
throw new IllegalArgumentException("The connection string cannot be null or empty.");
}
/* Codes_SRS_IOTHUB_CONNECTIONSTRING_21_010: [The constructor shall interpret the connection string as a set of key-value pairs delimited by ';', with keys and values separated by '='.] */
String[] connStringAttrs = connectionString.split(";");
for (String attr : connStringAttrs)
{
/* Codes_SRS_IOTHUB_CONNECTIONSTRING_21_011: [The constructor shall save the IoT Hub hostName as the value of 'hostName' in the connection string.] */
if (attr.toLowerCase().startsWith(HOSTNAME_ATTRIBUTE.toLowerCase()))
{
this.hostName = attr.substring(HOSTNAME_ATTRIBUTE.length());
}
/* Codes_SRS_IOTHUB_CONNECTIONSTRING_21_013: [The constructor shall save the device ID as the UTF-8 URL-decoded value of 'deviceId' in the connection string.] */
else if (attr.toLowerCase().startsWith(DEVICE_ID_ATTRIBUTE.toLowerCase()))
{
String urlEncodedDeviceId = attr.substring(DEVICE_ID_ATTRIBUTE.length());
try
{
this.deviceId = URLDecoder.decode(urlEncodedDeviceId, CONNECTION_STRING_CHARSET.name());
}
catch (UnsupportedEncodingException e)
{
// should never happen, since the encoding is hard-coded.
throw new IllegalStateException(e);
}
}
/* Codes_SRS_IOTHUB_CONNECTIONSTRING_21_014: [The constructor shall save the device key as the value of 'sharedAccessKey' in the connection string.] */
else if (attr.toLowerCase().startsWith(SHARED_ACCESS_KEY_ATTRIBUTE.toLowerCase()))
{
this.sharedAccessKey = attr.substring(SHARED_ACCESS_KEY_ATTRIBUTE.length());
}
/* Codes_SRS_IOTHUB_CONNECTIONSTRING_21_015: [The constructor shall save the shared access token as the value of 'sharedAccessToken' in the connection string.] */
else if (attr.toLowerCase().startsWith(SHARED_ACCESS_TOKEN_ATTRIBUTE.toLowerCase()))
{
this.sharedAccessToken = attr.substring(SHARED_ACCESS_TOKEN_ATTRIBUTE.length());
}
else if (attr.toLowerCase().startsWith(MODULE_ID_ATTRIBUTE.toLowerCase()))
{
this.moduleId = attr.substring(MODULE_ID_ATTRIBUTE.length());
}
else if (attr.toLowerCase().startsWith(GATEWAY_HOST_NAME_ATTRIBUTE.toLowerCase()))
{
this.gatewayHostName = attr.substring(GATEWAY_HOST_NAME_ATTRIBUTE.length());
}
}
this.isUsingX509 = connectionString.contains(X509_ENABLED_ATTRIBUTE);
validateTerms(this.hostName, this.deviceId, this.sharedAccessKey, this.sharedAccessToken, this.isUsingX509);
this.hubName = parseHubName(this.hostName);
}