Search in sources :

Example 6 with ProxyOptions

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

the class JobClient method scheduleDeviceMethod.

/**
 * Creates a new Job to invoke method on one or multiple devices
 *
 * @param jobId Unique Job Id for this job
 * @param queryCondition Query condition to evaluate which devices to run the job on. It can be {@code null} or empty
 * @param methodName Method name to be invoked
 * @param responseTimeoutInSeconds Maximum interval of time, in seconds, that the Direct Method will wait for answer. It can be {@code null}.
 * @param connectTimeoutInSeconds Maximum interval of time, in seconds, that the Direct Method will wait for the connection. It can be {@code null}.
 * @param payload Object that contains the payload defined by the user. It can be {@code null}.
 * @param startTimeUtc Date time in Utc to start the job
 * @param maxExecutionTimeInSeconds Max execution time in seconds, i.e., ttl duration the job can run
 * @return a jobResult object
 * @throws IllegalArgumentException if one of the provided parameters is invalid
 * @throws IOException if the function cannot create a URL for the job, or the IO failed on request
 * @throws IotHubException if the http request failed
 */
public synchronized JobResult scheduleDeviceMethod(String jobId, String queryCondition, String methodName, Long responseTimeoutInSeconds, Long connectTimeoutInSeconds, Object payload, Date startTimeUtc, long maxExecutionTimeInSeconds) throws IllegalArgumentException, IOException, IotHubException {
    URL url;
    if (Tools.isNullOrEmpty(jobId)) {
        throw new IllegalArgumentException("jobId cannot be null or empty");
    }
    if (Tools.isNullOrEmpty(methodName)) {
        throw new IllegalArgumentException("method name cannot be null or empty");
    }
    if (startTimeUtc == null) {
        throw new IllegalArgumentException("startTimeUtc cannot be null");
    }
    if (maxExecutionTimeInSeconds < 0) {
        throw new IllegalArgumentException("maxExecutionTimeInSeconds cannot be less than 0");
    }
    MethodParser cloudToDeviceMethod = new MethodParser(methodName, responseTimeoutInSeconds, connectTimeoutInSeconds, payload);
    JobsParser jobsParser = new JobsParser(jobId, cloudToDeviceMethod, queryCondition, startTimeUtc, maxExecutionTimeInSeconds);
    String json = jobsParser.toJson();
    try {
        url = IotHubConnectionString.getUrlJobs(this.hostName, jobId);
    } catch (MalformedURLException e) {
        throw new IllegalArgumentException("Invalid JobId to create url");
    }
    ProxyOptions proxyOptions = options.getProxyOptions();
    Proxy proxy = proxyOptions != null ? proxyOptions.getProxy() : null;
    HttpResponse response = DeviceOperations.request(this.getAuthenticationToken(), url, HttpMethod.PUT, json.getBytes(StandardCharsets.UTF_8), null, options.getHttpConnectTimeout(), options.getHttpReadTimeout(), proxy);
    return new JobResult(response.getBody());
}
Also used : MalformedURLException(java.net.MalformedURLException) Proxy(java.net.Proxy) JobsParser(com.microsoft.azure.sdk.iot.deps.serializer.JobsParser) ProxyOptions(com.microsoft.azure.sdk.iot.service.ProxyOptions) HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) URL(java.net.URL) MethodParser(com.microsoft.azure.sdk.iot.deps.serializer.MethodParser)

Example 7 with ProxyOptions

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

the class RegistryManagerTests method deviceLifecycleWithProxy.

@Test
public void deviceLifecycleWithProxy() throws Exception {
    Proxy testProxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(testProxyHostname, testProxyPort));
    ProxyOptions proxyOptions = new ProxyOptions(testProxy);
    RegistryManagerOptions registryManagerOptions = RegistryManagerOptions.builder().proxyOptions(proxyOptions).build();
    RegistryManagerTestInstance testInstance = new RegistryManagerTestInstance(registryManagerOptions);
    // -Create-//
    Device deviceAdded = Device.createFromId(testInstance.deviceId, DeviceStatus.Enabled, null);
    Tools.addDeviceWithRetry(testInstance.registryManager, deviceAdded);
    // -Read-//
    Device deviceRetrieved = testInstance.registryManager.getDevice(testInstance.deviceId);
    // -Update-//
    Device deviceUpdated = testInstance.registryManager.getDevice(testInstance.deviceId);
    deviceUpdated.setStatus(DeviceStatus.Disabled);
    deviceUpdated = testInstance.registryManager.updateDevice(deviceUpdated);
    // -Delete-//
    testInstance.registryManager.removeDevice(testInstance.deviceId);
    // Assert
    assertEquals(buildExceptionMessage("", hostName), testInstance.deviceId, deviceAdded.getDeviceId());
    assertEquals(buildExceptionMessage("", hostName), testInstance.deviceId, deviceRetrieved.getDeviceId());
    assertEquals(buildExceptionMessage("", hostName), DeviceStatus.Disabled, deviceUpdated.getStatus());
    assertTrue(buildExceptionMessage("", hostName), deviceWasDeletedSuccessfully(testInstance.registryManager, testInstance.deviceId));
}
Also used : Proxy(java.net.Proxy) ProxyOptions(com.microsoft.azure.sdk.iot.service.ProxyOptions) InetSocketAddress(java.net.InetSocketAddress) Device(com.microsoft.azure.sdk.iot.service.Device) RegistryManagerOptions(com.microsoft.azure.sdk.iot.service.RegistryManagerOptions) ContinuousIntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest) IotHubTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) Test(org.junit.Test)

Example 8 with ProxyOptions

use of com.microsoft.azure.sdk.iot.service.ProxyOptions 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 9 with ProxyOptions

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

the class GetTwinTests method testGetDeviceTwinWithProxy.

@Test
@StandardTierHubOnlyTest
public void testGetDeviceTwinWithProxy() throws IOException, InterruptedException, IotHubException, GeneralSecurityException, ModuleClientException, URISyntaxException {
    if (testInstance.protocol != IotHubClientProtocol.MQTT || testInstance.authenticationType != AuthenticationType.SAS || testInstance.clientType != ClientType.DEVICE_CLIENT) {
        // when the device is using MQTT with SAS auth
        return;
    }
    super.setUpNewDeviceAndModule();
    String testProxyHostname = "127.0.0.1";
    int testProxyPort = 8892;
    HttpProxyServer proxyServer = DefaultHttpProxyServer.bootstrap().withPort(testProxyPort).start();
    try {
        Proxy serviceSideProxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(testProxyHostname, testProxyPort));
        ProxyOptions proxyOptions = new ProxyOptions(serviceSideProxy);
        DeviceTwinClientOptions options = DeviceTwinClientOptions.builder().proxyOptions(proxyOptions).build();
        testInstance.twinServiceClient = DeviceTwin.createFromConnectionString(iotHubConnectionString, options);
        super.testGetDeviceTwin();
    } finally {
        proxyServer.stop();
    }
}
Also used : DefaultHttpProxyServer(org.littleshoot.proxy.impl.DefaultHttpProxyServer) HttpProxyServer(org.littleshoot.proxy.HttpProxyServer) Proxy(java.net.Proxy) ProxyOptions(com.microsoft.azure.sdk.iot.service.ProxyOptions) InetSocketAddress(java.net.InetSocketAddress) DeviceTwinClientOptions(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinClientOptions) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) IotHubTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) Test(org.junit.Test)

Example 10 with ProxyOptions

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

the class DeviceMethodTests method invokeMethodWithServiceSideProxy.

@Test
@StandardTierHubOnlyTest
public void invokeMethodWithServiceSideProxy() throws Exception {
    if (testInstance.protocol != IotHubClientProtocol.MQTT || testInstance.authenticationType != AuthenticationType.SAS || testInstance.clientType != ClientType.DEVICE_CLIENT) {
        // when the device is using MQTT with SAS auth
        return;
    }
    String testProxyHostname = "127.0.0.1";
    int testProxyPort = 8894;
    HttpProxyServer proxyServer = DefaultHttpProxyServer.bootstrap().withPort(testProxyPort).start();
    try {
        Proxy serviceSideProxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(testProxyHostname, testProxyPort));
        ProxyOptions proxyOptions = new ProxyOptions(serviceSideProxy);
        DeviceMethodClientOptions options = DeviceMethodClientOptions.builder().proxyOptions(proxyOptions).httpReadTimeout(HTTP_READ_TIMEOUT).build();
        this.testInstance.methodServiceClient = DeviceMethod.createFromConnectionString(iotHubConnectionString, options);
        super.openDeviceClientAndSubscribeToMethods();
        super.invokeMethodSucceed();
    } finally {
        proxyServer.stop();
    }
}
Also used : DefaultHttpProxyServer(org.littleshoot.proxy.impl.DefaultHttpProxyServer) HttpProxyServer(org.littleshoot.proxy.HttpProxyServer) Proxy(java.net.Proxy) ProxyOptions(com.microsoft.azure.sdk.iot.service.ProxyOptions) DeviceMethodClientOptions(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethodClientOptions) InetSocketAddress(java.net.InetSocketAddress) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) ContinuousIntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest) IotHubTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) Test(org.junit.Test)

Aggregations

ProxyOptions (com.microsoft.azure.sdk.iot.service.ProxyOptions)16 Proxy (java.net.Proxy)16 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)8 HttpResponse (com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse)6 InetSocketAddress (java.net.InetSocketAddress)5 URL (java.net.URL)5 MalformedURLException (java.net.MalformedURLException)4 Test (org.junit.Test)4 StandardTierHubOnlyTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest)4 IotHubTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest)3 JobsParser (com.microsoft.azure.sdk.iot.deps.serializer.JobsParser)2 MethodParser (com.microsoft.azure.sdk.iot.deps.serializer.MethodParser)2 TwinState (com.microsoft.azure.sdk.iot.deps.twin.TwinState)2 Device (com.microsoft.azure.sdk.iot.service.Device)2 Query (com.microsoft.azure.sdk.iot.service.devicetwin.Query)2 HttpProxyServer (org.littleshoot.proxy.HttpProxyServer)2 DefaultHttpProxyServer (org.littleshoot.proxy.impl.DefaultHttpProxyServer)2 IntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest)2 ContinuousIntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest)2 IotHubSSLContext (com.microsoft.azure.sdk.iot.deps.auth.IotHubSSLContext)1