if (iotHubConnectionString.isUsingX509()) iotHubConnectionString.getHostName(), iotHubConnectionString.getGatewayHostName(), iotHubConnectionString.getDeviceId(), iotHubConnectionString.getModuleId(), iotHubConnectionString.getSharedAccessKey(), iotHubConnectionString.getSharedAccessToken()); iotHubConnectionString.getHostName(), iotHubConnectionString.getDeviceId(), logger.getMethodName());
/** * Getter for the IoT Hub name. * @return the IoT Hub name. */ public String getIotHubName() { // Codes_SRS_DEVICECLIENTCONFIG_11_007: [The function shall return the IoT Hub name given in the constructor, where the IoT Hub name is embedded in the IoT Hub hostname as follows: [IoT Hub name].[valid HTML chars]+.] return IotHubConnectionString.parseHubName(this.authenticationProvider.getHostname()); }
connectionString.getHostName(), connectionString.getGatewayHostName(), connectionString.getDeviceId(), connectionString.getModuleId(), securityProvider); connectionString.getHostName(), connectionString.getGatewayHostName(), connectionString.getDeviceId(), connectionString.getModuleId(), ((SecurityProviderSymmetricKey) securityProvider).getSymmetricKey().toString(), null); connectionString.getHostName(), connectionString.getGatewayHostName(), connectionString.getDeviceId(), connectionString.getModuleId(), securityProvider); connectionString.getHostName(), connectionString.getDeviceId(), logger.getMethodName());
public IotHubConnectionString(String hostName, String deviceId, String sharedAccessKey, String sharedAccessToken, String gatewayHostName) throws IllegalArgumentException, URISyntaxException { this.isUsingX509 = (sharedAccessKey == null && sharedAccessToken == null); /* Codes_SRS_IOTHUB_CONNECTIONSTRING_21_025: [If the parameters for the connection string is not valid, the constructor shall throw an IllegalArgumentException.] */ validateTerms(hostName, deviceId, sharedAccessKey, sharedAccessToken, this.isUsingX509); /* Codes_SRS_IOTHUB_CONNECTIONSTRING_21_020: [The constructor shall save the IoT Hub hostname as the value of `hostName` in the connection string.] */ this.hostName = hostName; /* Codes_SRS_IOTHUB_CONNECTIONSTRING_21_021: [The constructor shall save the first part of the IoT Hub hostname as the value of `hubName`, hostname split by `.`.] */ this.hubName = parseHubName(this.hostName); /* Codes_SRS_IOTHUB_CONNECTIONSTRING_21_022: [The constructor shall save the device ID as the UTF-8 URL-decoded value of `deviceId` in the connection string.] */ this.deviceId = deviceId; /* Codes_SRS_IOTHUB_CONNECTIONSTRING_21_023: [The constructor shall save the device key as the value of `sharedAccessKey` in the connection string.] */ this.sharedAccessKey = sharedAccessKey; /* Codes_SRS_IOTHUB_CONNECTIONSTRING_21_024: [The constructor shall save the shared access token as the value of `sharedAccessToken` in the connection string.] */ this.sharedAccessToken = sharedAccessToken; /* Codes_SRS_IOTHUB_CONNECTIONSTRING_34_036: [If the SAS Token has expired, throw a SecurityException.] */ if (this.sharedAccessToken != null && IotHubSasToken.isExpired(this.sharedAccessToken)) { throw new SecurityException("Your SAS Token has expired"); } this.gatewayHostName = gatewayHostName; if (this.gatewayHostName != null && !this.gatewayHostName.isEmpty()) { this.hostName = gatewayHostName; } this.logger = new CustomLogger(this.getClass()); logger.LogInfo("IotHubConnectionString object is created successfully, method name is %s ", logger.getMethodName()); }
IotHubConnectionString connectionString = new IotHubConnectionString(uri, deviceId, null, null);
public IotHubMessageResult execute(Message msg, Object context) { Counter counter = (Counter) context; System.out.println( "Device Client " + msg.getIotHubConnectionString().getDeviceId() + " received message " + counter.toString() + " with content: " + new String(msg.getBytes(), Message.DEFAULT_IOTHUB_MESSAGE_CHARSET)); for (MessageProperty messageProperty : msg.getProperties()) { System.out.println(messageProperty.getName() + " : " + messageProperty.getValue()); } IotHubMessageResult res = IotHubMessageResult.COMPLETE; System.out.println( "Responding to message " + counter.toString() + " with " + res.name()); counter.increment(); return res; } }
private void commonConstructorVerification(IotHubConnectionString connectionString, IotHubClientProtocol protocol) { if (connectionString == null) { throw new IllegalArgumentException("Connection string cannot be null"); } if (protocol == null) { throw new IllegalArgumentException("Protocol cannot be null."); } String gatewayHostName = connectionString.getGatewayHostName(); if (gatewayHostName != null && !gatewayHostName.isEmpty() && protocol == HTTPS) { throw new UnsupportedOperationException("Communication with edgehub only supported by MQTT/MQTT_WS and AMQPS/AMQPS_WS"); } } }
if (!iotHubConnectionString.isUsingX509()) iotHubConnectionString.getHostName(), iotHubConnectionString.getGatewayHostName(), iotHubConnectionString.getDeviceId(), iotHubConnectionString.getModuleId(), publicKeyCertificate, isPathForPublic, privateKey, isPathForPrivate);
validateTerms(this.hostName, this.deviceId, this.sharedAccessKey, this.sharedAccessToken, this.isUsingX509); this.hubName = parseHubName(this.hostName);
/** * Constructor for a ModuleClient instance. * @param connectionString The connection string for the edge module to connect to. Must be in format * HostName=xxxx;deviceId=xxxx;SharedAccessKey= * xxxx;moduleId=xxxx; * * or * * HostName=xxxx;DeviceId=xxxx;SharedAccessKey= * xxxx;moduleId=xxxx;HostNameGateway=xxxx * @param protocol The protocol to use when communicating with the module * @throws ModuleClientException if an exception is encountered when parsing the connection string * @throws UnsupportedOperationException if using any protocol besides MQTT, if the connection string is missing * the "moduleId" field, or if the connection string uses x509 * @throws IllegalArgumentException if the provided connection string is null or empty, or if the provided protocol is null * @throws URISyntaxException if the connection string cannot be parsed for a valid hostname */ public ModuleClient(String connectionString, IotHubClientProtocol protocol) throws ModuleClientException, IllegalArgumentException, UnsupportedOperationException, URISyntaxException { //Codes_SRS_MODULECLIENT_34_006: [This function shall invoke the super constructor.] super(new IotHubConnectionString(connectionString), protocol, SEND_PERIOD_MILLIS, getReceivePeriod(protocol)); //Codes_SRS_MODULECLIENT_34_007: [If the provided protocol is not MQTT, AMQPS, MQTT_WS, or AMQPS_WS, this function shall throw an UnsupportedOperationException.] //Codes_SRS_MODULECLIENT_34_004: [If the provided connection string does not contain a module id, this function shall throw an IllegalArgumentException.] commonConstructorVerifications(protocol, this.config); }
/** * Constructor that takes a connection string as an argument. * * @param connString the connection string. The connection string is a set * of key-value pairs that are separated by ';', with the keys and values * separated by '='. It should contain values for the following keys: * {@code HostName}, {@code DeviceId}, and {@code SharedAccessKey}. * @param protocol the communication protocol used (i.e. HTTPS). * * @throws IllegalArgumentException if any of {@code connString} or * {@code protocol} are {@code null}; or if {@code connString} is missing * one of the following attributes:{@code HostName}, {@code DeviceId}, or * {@code SharedAccessKey} or if the IoT hub hostname does not conform to * RFC 3986 or if the provided {@code connString} is for an x509 authenticated device * @throws URISyntaxException if the hostname in the connection string is not a valid URI */ public DeviceClient(String connString, IotHubClientProtocol protocol) throws URISyntaxException, IllegalArgumentException { // Codes_SRS_DEVICECLIENT_21_001: [The constructor shall interpret the connection string as a set of key-value pairs delimited by ';', using the object IotHubConnectionString.] super(new IotHubConnectionString(connString), protocol, SEND_PERIOD_MILLIS, getReceivePeriod(protocol)); // Codes_SRS_DEVICECLIENT_34_075: [If the provided connection string contains a module id field, this function shall throw an UnsupportedOperationException.] commonConstructorVerifications(); // Codes_SRS_DEVICECLIENT_12_012: [The constructor shall set the connection type to SINGLE_CLIENT.] // Codes_SRS_DEVICECLIENT_12_015: [The constructor shall set the transportClient to null.] commonConstructorSetup(); }
parseHubName(hostName);
/** * Create a module client instance that uses x509 authentication. * * <p>Note! Communication from a module to another EdgeHub using x509 authentication is not currently supported and * the service will always return "UNAUTHORIZED"</p> * * <p>Communication from a module directly to the IotHub does support x509 authentication, though.</p> * @param connectionString The connection string for the edge module to connect to. Must be in format * HostName=xxxx;deviceId=xxxx;SharedAccessKey= * xxxx;moduleId=xxxx; * * or * * HostName=xxxx;DeviceId=xxxx;SharedAccessKey= * xxxx;moduleId=xxxx;HostNameGateway=xxxx * @param protocol The protocol to communicate with * @param publicKeyCertificate The PEM formatted string for the public key certificate or the system path to the file containing the PEM. * @param isCertificatePath 'false' if the publicKeyCertificate argument is a path to the PEM, and 'true' if it is the PEM string itself, * @param privateKey The PEM formatted string for the private key or the system path to the file containing the PEM. * @param isPrivateKeyPath 'false' if the privateKey argument is a path to the PEM, and 'true' if it is the PEM string itself, * @throws URISyntaxException If the connString cannot be parsed * @throws ModuleClientException if any other exception occurs while building the module client */ public ModuleClient(String connectionString, IotHubClientProtocol protocol, String publicKeyCertificate, boolean isCertificatePath, String privateKey, boolean isPrivateKeyPath) throws ModuleClientException, URISyntaxException { super(new IotHubConnectionString(connectionString), protocol, publicKeyCertificate, isCertificatePath, privateKey, isPrivateKeyPath, SEND_PERIOD_MILLIS, getReceivePeriod(protocol)); //Codes_SRS_MODULECLIENT_34_008: [If the provided protocol is not MQTT, AMQPS, MQTT_WS, or AMQPS_WS, this function shall throw an UnsupportedOperationException.] //Codes_SRS_MODULECLIENT_34_009: [If the provided connection string does not contain a module id, this function shall throw an IllegalArgumentException.] commonConstructorVerifications(protocol, this.getConfig()); }
super(new IotHubConnectionString(connString), protocol, publicKeyCertificate, isCertificatePath, privateKey, isPrivateKeyPath, SEND_PERIOD_MILLIS, getReceivePeriod(protocol)); commonConstructorVerifications();
this.config = new DeviceClientConfig(new IotHubConnectionString(connString)); this.deviceIO = null;