Search in sources :

Example 1 with ServiceClientOptions

use of com.microsoft.azure.sdk.iot.service.ServiceClientOptions in project azure-iot-sdk-java by Azure.

the class ServiceClientTests method cloudToDeviceTelemetry.

public void cloudToDeviceTelemetry(boolean withProxy, boolean withPayload, boolean withLargestPayload, boolean withCustomSSLContext, boolean withAzureSasCredential) throws Exception {
    // We remove and recreate the device for a clean start
    RegistryManager registryManager = RegistryManager.createFromConnectionString(iotHubConnectionString, RegistryManagerOptions.builder().httpReadTimeout(HTTP_READ_TIMEOUT).build());
    TestDeviceIdentity testDeviceIdentity = Tools.getTestDevice(iotHubConnectionString, IotHubClientProtocol.AMQPS, AuthenticationType.SAS, false);
    Device device = testDeviceIdentity.getDevice();
    Device deviceGetBefore = registryManager.getDevice(device.getDeviceId());
    // Create service client
    ProxyOptions proxyOptions = null;
    if (withProxy) {
        Proxy testProxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(testProxyHostname, testProxyPort));
        proxyOptions = new ProxyOptions(testProxy);
    }
    SSLContext sslContext = null;
    if (withCustomSSLContext) {
        sslContext = new IotHubSSLContext().getSSLContext();
    }
    ServiceClientOptions serviceClientOptions = ServiceClientOptions.builder().proxyOptions(proxyOptions).sslContext(sslContext).build();
    ServiceClient serviceClient;
    if (withAzureSasCredential) {
        serviceClient = buildServiceClientWithAzureSasCredential(testInstance.protocol, serviceClientOptions);
    } else {
        serviceClient = new ServiceClient(iotHubConnectionString, testInstance.protocol, serviceClientOptions);
    }
    serviceClient.open();
    Message message;
    if (withPayload) {
        if (withLargestPayload) {
            message = new Message(LARGEST_PAYLOAD);
        } else {
            message = new Message(SMALL_PAYLOAD);
        }
    } else {
        message = new Message();
    }
    serviceClient.send(device.getDeviceId(), message);
    Device deviceGetAfter = registryManager.getDevice(device.getDeviceId());
    serviceClient.close();
    Tools.disposeTestIdentity(testDeviceIdentity, iotHubConnectionString);
    // Assert
    assertEquals(buildExceptionMessage("", hostName), deviceGetBefore.getDeviceId(), deviceGetAfter.getDeviceId());
    assertEquals(buildExceptionMessage("", hostName), 0, deviceGetBefore.getCloudToDeviceMessageCount());
    assertEquals(buildExceptionMessage("", hostName), 1, deviceGetAfter.getCloudToDeviceMessageCount());
    registryManager.close();
}
Also used : IotHubSSLContext(com.microsoft.azure.sdk.iot.deps.auth.IotHubSSLContext) Proxy(java.net.Proxy) ProxyOptions(com.microsoft.azure.sdk.iot.service.ProxyOptions) Message(com.microsoft.azure.sdk.iot.service.Message) CorrelationDetailsLoggingAssert.buildExceptionMessage(tests.integration.com.microsoft.azure.sdk.iot.helpers.CorrelationDetailsLoggingAssert.buildExceptionMessage) Device(com.microsoft.azure.sdk.iot.service.Device) InetSocketAddress(java.net.InetSocketAddress) ServiceClient(com.microsoft.azure.sdk.iot.service.ServiceClient) RegistryManager(com.microsoft.azure.sdk.iot.service.RegistryManager) SSLContext(javax.net.ssl.SSLContext) IotHubSSLContext(com.microsoft.azure.sdk.iot.deps.auth.IotHubSSLContext) ServiceClientOptions(com.microsoft.azure.sdk.iot.service.ServiceClientOptions) TestDeviceIdentity(tests.integration.com.microsoft.azure.sdk.iot.helpers.TestDeviceIdentity)

Example 2 with ServiceClientOptions

use of com.microsoft.azure.sdk.iot.service.ServiceClientOptions in project azure-iot-sdk-java by Azure.

the class AzureSasCredentialSample method runServiceClientSample.

private static void runServiceClientSample(String iotHubHostName, AzureSasCredential credential, String deviceId) {
    // ServiceClient has some configurable options for setting a custom SSLContext, as well as for setting proxies.
    // For this sample, the default options will be used though.
    ServiceClientOptions options = ServiceClientOptions.builder().build();
    // This constructor takes in your implementation of AzureSasCredential which allows you to use symmetric key based
    // authentication without giving the client your connection string.
    ServiceClient serviceClient = new ServiceClient(iotHubHostName, credential, IotHubServiceClientProtocol.AMQPS, options);
    String cloudToDeviceMessagePayload = "This is a message sent by an RBAC authenticated service client!";
    Message cloudToDeviceMessage = new Message(cloudToDeviceMessagePayload.getBytes(StandardCharsets.UTF_8));
    try {
        System.out.println("Sending cloud to device message to the new device");
        serviceClient.send(deviceId, cloudToDeviceMessage);
        System.out.println("Successfully sent cloud to device message to the new device");
    } catch (IOException | IotHubException e) {
        System.err.println("Failed to send a cloud to device message to the new device");
        e.printStackTrace();
        System.exit(-1);
    }
    try {
        // FeedbackReceiver will use the same authentication mechanism that the ServiceClient itself uses,
        // so the below APIs are also RBAC authenticated.
        FeedbackReceiver feedbackReceiver = serviceClient.getFeedbackReceiver();
        System.out.println("Opening feedback receiver to listen for feedback messages");
        feedbackReceiver.open();
        FeedbackBatch feedbackBatch = feedbackReceiver.receive(FEEDBACK_MESSAGE_LISTEN_SECONDS);
        if (feedbackBatch != null) {
            for (FeedbackRecord feedbackRecord : feedbackBatch.getRecords()) {
                System.out.println(String.format("Feedback record received for device %s with status %s", feedbackRecord.getDeviceId(), feedbackRecord.getStatusCode()));
            }
        } else {
            System.out.println("No feedback records were received");
        }
        feedbackReceiver.close();
    } catch (IOException | InterruptedException e) {
        System.err.println("Failed to listen for feedback messages");
        e.printStackTrace();
        System.exit(-1);
    }
    try {
        // FileUploadNotificationReceiver will use the same authentication mechanism that the ServiceClient itself uses,
        // so the below APIs are also RBAC authenticated.
        FileUploadNotificationReceiver fileUploadNotificationReceiver = serviceClient.getFileUploadNotificationReceiver();
        System.out.println("Opening file upload notification receiver and listening for file upload notifications");
        fileUploadNotificationReceiver.open();
        FileUploadNotification fileUploadNotification = fileUploadNotificationReceiver.receive(FILE_UPLOAD_NOTIFICATION_LISTEN_SECONDS);
        if (fileUploadNotification != null) {
            System.out.println("File upload notification received for device " + fileUploadNotification.getDeviceId());
        } else {
            System.out.println("No feedback records were received");
        }
        fileUploadNotificationReceiver.close();
    } catch (IOException | InterruptedException e) {
        System.err.println("Failed to listen for file upload notification messages");
        e.printStackTrace();
        System.exit(-1);
    }
}
Also used : FileUploadNotificationReceiver(com.microsoft.azure.sdk.iot.service.FileUploadNotificationReceiver) Message(com.microsoft.azure.sdk.iot.service.Message) FeedbackReceiver(com.microsoft.azure.sdk.iot.service.FeedbackReceiver) ServiceClientOptions(com.microsoft.azure.sdk.iot.service.ServiceClientOptions) IOException(java.io.IOException) FileUploadNotification(com.microsoft.azure.sdk.iot.service.FileUploadNotification) ServiceClient(com.microsoft.azure.sdk.iot.service.ServiceClient) FeedbackBatch(com.microsoft.azure.sdk.iot.service.FeedbackBatch) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException) FeedbackRecord(com.microsoft.azure.sdk.iot.service.FeedbackRecord)

Example 3 with ServiceClientOptions

use of com.microsoft.azure.sdk.iot.service.ServiceClientOptions in project azure-iot-sdk-java by Azure.

the class RoleBasedAuthenticationSample method runServiceClientSample.

private static void runServiceClientSample(String iotHubHostName, TokenCredential credential, String deviceId) {
    // ServiceClient has some configurable options for setting a custom SSLContext, as well as for setting proxies.
    // For this sample, the default options will be used though.
    ServiceClientOptions options = ServiceClientOptions.builder().build();
    // This constructor takes in your implementation of TokenCredential which allows you to use RBAC authentication
    // rather than symmetric key based authentication that comes with constructors that take connection strings.
    ServiceClient serviceClient = new ServiceClient(iotHubHostName, credential, IotHubServiceClientProtocol.AMQPS, options);
    String cloudToDeviceMessagePayload = "This is a message sent by an RBAC authenticated service client!";
    Message cloudToDeviceMessage = new Message(cloudToDeviceMessagePayload.getBytes(StandardCharsets.UTF_8));
    try {
        System.out.println("Sending cloud to device message to the new device");
        serviceClient.send(deviceId, cloudToDeviceMessage);
        System.out.println("Successfully sent cloud to device message to the new device");
    } catch (IOException | IotHubException e) {
        System.err.println("Failed to send a cloud to device message to the new device");
        e.printStackTrace();
        System.exit(-1);
    }
    try {
        // FeedbackReceiver will use the same authentication mechanism that the ServiceClient itself uses,
        // so the below APIs are also RBAC authenticated.
        FeedbackReceiver feedbackReceiver = serviceClient.getFeedbackReceiver();
        System.out.println("Opening feedback receiver to listen for feedback messages");
        feedbackReceiver.open();
        FeedbackBatch feedbackBatch = feedbackReceiver.receive(FEEDBACK_MESSAGE_LISTEN_SECONDS);
        if (feedbackBatch != null) {
            for (FeedbackRecord feedbackRecord : feedbackBatch.getRecords()) {
                System.out.println(String.format("Feedback record received for device %s with status %s", feedbackRecord.getDeviceId(), feedbackRecord.getStatusCode()));
            }
        } else {
            System.out.println("No feedback records were received");
        }
        feedbackReceiver.close();
    } catch (IOException | InterruptedException e) {
        System.err.println("Failed to listen for feedback messages");
        e.printStackTrace();
        System.exit(-1);
    }
    try {
        // FileUploadNotificationReceiver will use the same authentication mechanism that the ServiceClient itself uses,
        // so the below APIs are also RBAC authenticated.
        FileUploadNotificationReceiver fileUploadNotificationReceiver = serviceClient.getFileUploadNotificationReceiver();
        System.out.println("Opening file upload notification receiver and listening for file upload notifications");
        fileUploadNotificationReceiver.open();
        FileUploadNotification fileUploadNotification = fileUploadNotificationReceiver.receive(FILE_UPLOAD_NOTIFICATION_LISTEN_SECONDS);
        if (fileUploadNotification != null) {
            System.out.println("File upload notification received for device " + fileUploadNotification.getDeviceId());
        } else {
            System.out.println("No feedback records were received");
        }
        fileUploadNotificationReceiver.close();
    } catch (IOException | InterruptedException e) {
        System.err.println("Failed to listen for file upload notification messages");
        e.printStackTrace();
        System.exit(-1);
    }
}
Also used : FileUploadNotificationReceiver(com.microsoft.azure.sdk.iot.service.FileUploadNotificationReceiver) Message(com.microsoft.azure.sdk.iot.service.Message) FeedbackReceiver(com.microsoft.azure.sdk.iot.service.FeedbackReceiver) ServiceClientOptions(com.microsoft.azure.sdk.iot.service.ServiceClientOptions) IOException(java.io.IOException) FileUploadNotification(com.microsoft.azure.sdk.iot.service.FileUploadNotification) ServiceClient(com.microsoft.azure.sdk.iot.service.ServiceClient) FeedbackBatch(com.microsoft.azure.sdk.iot.service.FeedbackBatch) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException) FeedbackRecord(com.microsoft.azure.sdk.iot.service.FeedbackRecord)

Aggregations

Message (com.microsoft.azure.sdk.iot.service.Message)3 ServiceClient (com.microsoft.azure.sdk.iot.service.ServiceClient)3 ServiceClientOptions (com.microsoft.azure.sdk.iot.service.ServiceClientOptions)3 FeedbackBatch (com.microsoft.azure.sdk.iot.service.FeedbackBatch)2 FeedbackReceiver (com.microsoft.azure.sdk.iot.service.FeedbackReceiver)2 FeedbackRecord (com.microsoft.azure.sdk.iot.service.FeedbackRecord)2 FileUploadNotification (com.microsoft.azure.sdk.iot.service.FileUploadNotification)2 FileUploadNotificationReceiver (com.microsoft.azure.sdk.iot.service.FileUploadNotificationReceiver)2 IotHubException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)2 IOException (java.io.IOException)2 IotHubSSLContext (com.microsoft.azure.sdk.iot.deps.auth.IotHubSSLContext)1 Device (com.microsoft.azure.sdk.iot.service.Device)1 ProxyOptions (com.microsoft.azure.sdk.iot.service.ProxyOptions)1 RegistryManager (com.microsoft.azure.sdk.iot.service.RegistryManager)1 InetSocketAddress (java.net.InetSocketAddress)1 Proxy (java.net.Proxy)1 SSLContext (javax.net.ssl.SSLContext)1 CorrelationDetailsLoggingAssert.buildExceptionMessage (tests.integration.com.microsoft.azure.sdk.iot.helpers.CorrelationDetailsLoggingAssert.buildExceptionMessage)1 TestDeviceIdentity (tests.integration.com.microsoft.azure.sdk.iot.helpers.TestDeviceIdentity)1