/** * @return if the saved sas token has expired and needs manual renewal */ private boolean isSasTokenExpired() { //Codes_SRS_IOTHUBTRANSPORT_28_003: [This function shall indicate if the device's sas token is expired.] return this.defaultConfig.getAuthenticationType() == DeviceClientConfig.AuthType.SAS_TOKEN && this.defaultConfig.getSasTokenAuthentication().isRenewalNecessary(); }
public CorrelationDetailsLoggingAssert(InternalClient internalClient) { this(internalClient.getConfig().getIotHubHostname(), internalClient.getConfig().getDeviceId(), internalClient.getConfig().getProtocol().toString(), internalClient.getConfig().getModuleId()); }
InternalClient(IotHubAuthenticationProvider iotHubAuthenticationProvider, IotHubClientProtocol protocol, long sendPeriodMillis, long receivePeriodMillis) throws IOException, TransportException { this.config = new DeviceClientConfig(iotHubAuthenticationProvider); this.config.setProtocol(protocol); this.deviceIO = new DeviceIO(this.config, sendPeriodMillis, receivePeriodMillis); this.logger = new CustomLogger(this.getClass()); }
private String chooseHostname() { String gatewayHostname = this.deviceClientConfig.getGatewayHostname(); if (gatewayHostname != null && !gatewayHostname.isEmpty()) { return gatewayHostname; } return this.deviceClientConfig.getIotHubHostname(); }
SSLContext sslContext = this.config.getAuthenticationProvider().getSSLContext(); if (this.config.getAuthenticationType() == DeviceClientConfig.AuthType.SAS_TOKEN) this.iotHubUserPassword = this.config.getSasTokenAuthentication().getRenewedSasToken(false); else if (this.config.getAuthenticationType() == DeviceClientConfig.AuthType.X509_CERTIFICATE) if (this.config.isUseWebsocket()) String clientUserAgentIdentifier = "DeviceClientType=" + URLEncoder.encode(this.config.getProductInfo().getUserAgentString(), "UTF-8").replaceAll("\\+", "%20"); String clientId = this.config.getDeviceId(); String moduleId = this.config.getModuleId(); if (moduleId != null && !moduleId.isEmpty()) this.iotHubUserName = this.config.getIotHubHostname() + "/" + clientId + "/" + API_VERSION + "&" + clientUserAgentIdentifier; String host = this.config.getGatewayHostname(); if (host == null || host.isEmpty()) host = this.config.getIotHubHostname(); if (this.config.isUseWebsocket()) this.deviceMessaging = new MqttMessaging(mqttConnection, this.config.getDeviceId(), this.listener, this, this.connectionId, this.config.getModuleId(), this.config.getGatewayHostname() != null); this.mqttConnection.setMqttCallback(this.deviceMessaging); this.deviceMethod = new MqttDeviceMethod(mqttConnection, this.connectionId);
IotHubMessageUri messageUri = new IotHubMessageUri(getHostName(), this.config.getDeviceId(), this.config.getModuleId()); URL messageUrl = this.buildUrlFromString(HTTPS_HEAD_TAG + messageUri.toString()); new HttpsRequest(messageUrl, HttpsMethod.GET, new byte[0], this.config.getProductInfo().getUserAgentString()). Integer.toString(this.config.getMessageLockTimeoutSecs())); transportMessage.setMessageCallback(this.config.getDeviceTelemetryMessageCallback(message.getInputName())); transportMessage.setMessageCallbackContext(this.config.getDeviceTelemetryMessageContext(message.getInputName()));
this.amqpProperties.put(Symbol.getSymbol(VERSION_IDENTIFIER_KEY), deviceClientConfig.getProductInfo().getUserAgentString()); String moduleId = deviceClientConfig.getModuleId(); String deviceId = deviceClientConfig.getDeviceId(); if (moduleId != null && !moduleId.isEmpty())
if (config.getIotHubHostname() == null || config.getIotHubHostname().length() == 0) if (config.getDeviceId() == null || config.getDeviceId().length() == 0) if (config.getIotHubName() == null || config.getIotHubName().length() == 0) this.useWebSockets = this.deviceClientConfig.isUseWebsocket(); if (useWebSockets)
userProperties.put(TYPE_KEY, TYPE_VALUE); String host = deviceClientConfig.getGatewayHostname(); if (host == null || host.isEmpty()) host = deviceClientConfig.getIotHubHostname(); userProperties.put(NAME_KEY, host + DEVICES_PATH + deviceClientConfig.getDeviceId()); ApplicationProperties applicationProperties = new ApplicationProperties(userProperties); outgoingMessage.setApplicationProperties(applicationProperties); try section = new AmqpValue(deviceClientConfig.getSasTokenAuthentication().getRenewedSasToken(true)); outgoingMessage.setBody(section);
if (config.getIotHubHostname() == null || config.getIotHubHostname().length() == 0) if (config.getDeviceId() == null || config.getDeviceId().length() == 0) if (config.getIotHubName() == null || config.getIotHubName().length() == 0)
transportMessage.setMessageCallback(this.config.getDeviceTwinMessageCallback()); transportMessage.setMessageCallbackContext(this.config.getDeviceTwinMessageContext()); break; case DEVICE_METHODS: transportMessage.setMessageCallback(this.config.getDeviceMethodsMessageCallback()); transportMessage.setMessageCallbackContext(this.config.getDeviceMethodsMessageContext()); break; case DEVICE_TELEMETRY: transportMessage.setMessageCallback(this.config.getDeviceTelemetryMessageCallback(transportMessage.getInputName())); transportMessage.setMessageCallbackContext(this.config.getDeviceTelemetryMessageContext(transportMessage.getInputName())); break; case UNKNOWN:
public ResponseMessage sendFileUploadMessage(IotHubTransportMessage message) throws IOException { //Codes_SRS_HTTPSTRANSPORTMANAGER_34_028 [This function shall set the uri path of the provided message to the // format devices/<deviceid>/modules/<moduleid>/files if a moduleId is present or // devices/<deviceid>/modules/<moduleid>/files otherwise, and then send it.] String uri = new IotHubUri("", this.config.getDeviceId(), PATH_FILES_STRING, this.config.getModuleId()).toStringWithoutApiVersion(); message.setUriPath(uri); return this.send(message, new HashMap<String, String>()); }
/** * This constructor creates an instance of AmqpsDeviceTelemetry class and initializes member variables * * @param deviceClientConfig The configuration settings for an IoT Hub client * @throws IllegalArgumentException if the deviceClientConfig argument is null */ AmqpsDeviceTelemetry(DeviceClientConfig deviceClientConfig) throws IllegalArgumentException { // Codes_SRS_AMQPSDEVICETELEMETRY_34_050: [This constructor shall call super with the provided user agent string.] super(deviceClientConfig, SENDER_LINK_ENDPOINT_PATH_DEVICES, RECEIVER_LINK_ENDPOINT_PATH_DEVICES, SENDER_LINK_ENDPOINT_PATH_MODULES, deviceClientConfig.getGatewayHostname() != null ? RECEIVER_LINK_ENDPOINT_PATH_MODULES_EDGEHUB : RECEIVER_LINK_ENDPOINT_PATH_MODULES, SENDER_LINK_TAG_PREFIX, RECEIVER_LINK_TAG_PREFIX); this.deviceClientConfig = deviceClientConfig; String moduleId = this.deviceClientConfig.getModuleId(); if (moduleId != null && !moduleId.isEmpty()) { this.amqpProperties.put(Symbol.getSymbol(CORRELATION_ID_KEY), Symbol.getSymbol(this.deviceClientConfig.getDeviceId() + "/" + moduleId)); } else { this.amqpProperties.put(Symbol.getSymbol(CORRELATION_ID_KEY), Symbol.getSymbol(this.deviceClientConfig.getDeviceId())); } }
/** * Asynchronously sends an event message to the IoT Hub. * * @param message the message to be sent. * @param callback the callback to be invoked when a response is received. * Can be {@code null}. * @param callbackContext a context to be passed to the callback. Can be * {@code null} if no callback is provided. * * @throws IllegalArgumentException if the message provided is {@code null}. * @throws IllegalStateException if the client has not been opened yet or is * already closed. */ public void sendEventAsync(Message message, IotHubEventCallback callback, Object callbackContext) { //Codes_SRS_INTERNALCLIENT_34_045: [This function shall set the provided message's connection device id to the config's saved device id.] message.setConnectionDeviceId(this.config.getDeviceId()); //Codes_SRS_INTERNALCLIENT_21_010: [The sendEventAsync shall asynchronously send the message using the deviceIO connection.] deviceIO.sendEventAsync(message, callback, callbackContext, this.config.getDeviceId()); }
/** * Convert Proton message to IoTHubMessage if the message type is methods * * @param amqpsMessage The Proton message to convert * @param deviceClientConfig The device client configuration * @return the converted message */ @Override protected AmqpsConvertFromProtonReturnValue convertFromProton(AmqpsMessage amqpsMessage, DeviceClientConfig deviceClientConfig) throws TransportException { if ((amqpsMessage.getAmqpsMessageType() == MessageType.DEVICE_METHODS) && (this.deviceClientConfig.getDeviceId().equals(deviceClientConfig.getDeviceId()))) { // Codes_SRS_AMQPSDEVICEMETHODS_12_016: [The function shall convert the amqpsMessage to IoTHubTransportMessage.] Message message = protonMessageToIoTHubMessage(amqpsMessage); MessageCallback messageCallback = deviceClientConfig.getDeviceMethodsMessageCallback(); Object messageContext = deviceClientConfig.getDeviceMethodsMessageContext(); // Codes_SRS_AMQPSDEVICEMETHODS_12_027: [The function shall create a AmqpsConvertFromProtonReturnValue and set the message field to the new IotHubTransportMessage.] // Codes_SRS_AMQPSDEVICEMETHODS_12_028: [The function shall create a AmqpsConvertFromProtonReturnValue and copy the DeviceClientConfig callback and context to it.] return new AmqpsConvertFromProtonReturnValue(message, messageCallback, messageContext); } else { // Codes_SRS_AMQPSDEVICEMETHODS_12_015: [The function shall return null if the message type is not DEVICE_METHODS.] return null; } }
/** * Convert Proton message to IoTHubMessage if the message type is twin * * @param amqpsMessage The Proton message to convert * @param deviceClientConfig The device client configuration * @throws TransportException if the conversion fails * @return the converted message */ @Override protected AmqpsConvertFromProtonReturnValue convertFromProton(AmqpsMessage amqpsMessage, DeviceClientConfig deviceClientConfig) throws TransportException { if ((amqpsMessage.getAmqpsMessageType() == MessageType.DEVICE_TWIN) && (this.deviceClientConfig.getDeviceId().equals(deviceClientConfig.getDeviceId()))) { // Codes_SRS_AMQPSDEVICETWIN_12_016: [The function shall convert the amqpsMessage to IoTHubTransportMessage.] Message message = protonMessageToIoTHubMessage(amqpsMessage); MessageCallback messageCallback = deviceClientConfig.getDeviceTwinMessageCallback(); Object messageContext = deviceClientConfig.getDeviceTwinMessageContext(); // Codes_SRS_AMQPSDEVICETWIN_12_027: [The function shall create a AmqpsConvertFromProtonReturnValue and set the message field to the new IotHubTransportMessage.] // Codes_SRS_AMQPSDEVICETWIN_12_028: [The function shall create a AmqpsConvertFromProtonReturnValue and copy the DeviceClientConfig callback and context to it.] return new AmqpsConvertFromProtonReturnValue(message, messageCallback, messageContext); } else { // Codes_SRS_AMQPSDEVICETWIN_12_015: [The function shall return null if the message type is not DEVICE_TWIN.] return null; } }
/** * Release all resources and close all links. */ public void close() { this.shutDownScheduler(); this.closeLinks(); if (this.deviceClientConfig.getAuthenticationType() == DeviceClientConfig.AuthType.SAS_TOKEN) { this.amqpsAuthenticatorState = AmqpsDeviceAuthenticationState.NOT_AUTHENTICATED; } }
/** * Convert Proton message to IoTHubMessage if the message type is telemetry * * @param amqpsMessage The Proton message to convert * @param deviceClientConfig The device client configuration * @return the converted message */ @Override protected AmqpsConvertFromProtonReturnValue convertFromProton(AmqpsMessage amqpsMessage, DeviceClientConfig deviceClientConfig) throws TransportException { if (((amqpsMessage.getAmqpsMessageType() == null) || (amqpsMessage.getAmqpsMessageType() == MessageType.DEVICE_TELEMETRY)) && (this.deviceClientConfig.getDeviceId().equals(deviceClientConfig.getDeviceId()))) { // Codes_SRS_AMQPSDEVICETELEMETRY_12_009: [The function shall create a new IoTHubMessage using the Proton message body.] // Codes_SRS_AMQPSDEVICETELEMETRY_12_010: [**The function shall copy the correlationId, messageId, To and userId properties to the IotHubMessage properties.] // Codes_SRS_AMQPSDEVICETELEMETRY_12_011: [The function shall copy the Proton application properties to IoTHubMessage properties excluding the reserved property names.] Message message = protonMessageToIoTHubMessage(amqpsMessage); MessageCallback messageCallback = deviceClientConfig.getDeviceTelemetryMessageCallback(message.getInputName()); Object messageContext = deviceClientConfig.getDeviceTelemetryMessageContext(message.getInputName()); // Codes_SRS_AMQPSDEVICETELEMETRY_12_012: [The function shall create a new AmqpsConvertFromProtonReturnValue object and fill it with the converted message and the user callback and user context values from the deviceClientConfig.] // Codes_SRS_AMQPSDEVICETELEMETRY_12_013: [The function shall return with the new AmqpsConvertFromProtonReturnValue object.] return new AmqpsConvertFromProtonReturnValue(message, messageCallback, messageContext); } else { // Codes_SRS_AMQPSDEVICETELEMETRY_12_008: [The function shall return null if the Proton message type is not null or DEVICE_TELEMETRY.] return null; } }
this.config = new DeviceClientConfig(new IotHubConnectionString(connString)); this.deviceIO = null; String moduleId = this.getConfig().getModuleId(); if (!(moduleId == null || moduleId.isEmpty())) this.getConfig().setProtocol(this.transportClient.getIotHubClientProtocol());
private static void commonConstructorVerifications(IotHubClientProtocol protocol, DeviceClientConfig config) { if (protocol == IotHubClientProtocol.HTTPS) { throw new UnsupportedOperationException("Only MQTT, MQTT_WS, AMQPS and AMQPS_WS are supported for ModuleClient."); } if (config.getModuleId() == null || config.getModuleId().isEmpty()) { throw new IllegalArgumentException("Connection string must contain field for ModuleId"); } } }