Search in sources :

Example 11 with ProxyOptions

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

the class DigitalTwinClientTests method getDigitalTwinWithProxy.

@Test
@StandardTierHubOnlyTest
public void getDigitalTwinWithProxy() {
    // arrange
    Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(testProxyHostname, testProxyPort));
    ProxyOptions proxyOptions = new ProxyOptions(proxy);
    DigitalTwinClientOptions clientOptions = DigitalTwinClientOptions.builder().proxyOptions(proxyOptions).httpReadTimeout(0).build();
    digitalTwinClient = new DigitalTwinClient(IOTHUB_CONNECTION_STRING, clientOptions);
    // act
    BasicDigitalTwin response = digitalTwinClient.getDigitalTwin(deviceId, BasicDigitalTwin.class);
    ServiceResponseWithHeaders<BasicDigitalTwin, DigitalTwinGetHeaders> responseWithHeaders = digitalTwinClient.getDigitalTwinWithResponse(deviceId, BasicDigitalTwin.class);
    // assert
    assertEquals(response.getMetadata().getModelId(), E2ETestConstants.THERMOSTAT_MODEL_ID);
    assertEquals(responseWithHeaders.body().getMetadata().getModelId(), E2ETestConstants.THERMOSTAT_MODEL_ID);
}
Also used : Proxy(java.net.Proxy) ProxyOptions(com.microsoft.azure.sdk.iot.service.ProxyOptions) BasicDigitalTwin(com.microsoft.azure.sdk.iot.service.digitaltwin.serialization.BasicDigitalTwin) InetSocketAddress(java.net.InetSocketAddress) DigitalTwinClientOptions(com.microsoft.azure.sdk.iot.service.digitaltwin.DigitalTwinClientOptions) DigitalTwinGetHeaders(com.microsoft.azure.sdk.iot.service.digitaltwin.customized.DigitalTwinGetHeaders) DigitalTwinClient(com.microsoft.azure.sdk.iot.service.digitaltwin.DigitalTwinClient) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) DigitalTwinTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.DigitalTwinTest) 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 12 with ProxyOptions

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

the class JobClient method getJob.

/**
 * Get the current job on the iotHub.
 *
 * @param jobId Unique Job Id for this job
 * @return a jobResult object
 * @throws IllegalArgumentException if the jobId 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 getJob(String jobId) throws IllegalArgumentException, IOException, IotHubException {
    URL url;
    if (Tools.isNullOrEmpty(jobId)) {
        throw new IllegalArgumentException("jobId cannot be null or empty");
    }
    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.GET, new byte[] {}, null, options.getHttpConnectTimeout(), options.getHttpReadTimeout(), proxy);
    return new JobResult(response.getBody());
}
Also used : MalformedURLException(java.net.MalformedURLException) Proxy(java.net.Proxy) ProxyOptions(com.microsoft.azure.sdk.iot.service.ProxyOptions) HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) URL(java.net.URL)

Example 13 with ProxyOptions

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

the class JobClient method queryJobResponse.

/**
 * Query the iot hub for a jobs response. Query response are limited by page size per attempt
 *
 * @param jobType The type of job to query for
 * @param jobStatus The status of the job to query for
 * @param pageSize The value to which to limit the job response size by
 * @return A query object on which to look for responses by
 * @throws IOException If any of the input parameters are incorrect
 * @throws IotHubException If IotHub failed to respond
 */
public synchronized Query queryJobResponse(JobType jobType, JobStatus jobStatus, Integer pageSize) throws IOException, IotHubException {
    if (pageSize <= 0) {
        throw new IllegalArgumentException("pagesize cannot be negative or zero");
    }
    Query jobResponseQuery = new Query(pageSize, QueryType.JOB_RESPONSE);
    String jobTypeString = (jobType == null) ? null : jobType.toString();
    String jobStatusString = (jobStatus == null) ? null : jobStatus.toString();
    ProxyOptions proxyOptions = options.getProxyOptions();
    Proxy proxy = proxyOptions != null ? proxyOptions.getProxy() : null;
    jobResponseQuery.sendQueryRequest(this.credentialCache, this.azureSasCredential, this.iotHubConnectionString, IotHubConnectionString.getUrlQuery(this.hostName, jobTypeString, jobStatusString), HttpMethod.GET, options.getHttpConnectTimeout(), options.getHttpReadTimeout(), proxy);
    return jobResponseQuery;
}
Also used : Proxy(java.net.Proxy) Query(com.microsoft.azure.sdk.iot.service.devicetwin.Query) ProxyOptions(com.microsoft.azure.sdk.iot.service.ProxyOptions) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString)

Example 14 with ProxyOptions

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

the class DeviceTwin method getTwinOperation.

private void getTwinOperation(URL url, DeviceTwinDevice device) throws IotHubException, IOException {
    ProxyOptions proxyOptions = options.getProxyOptions();
    Proxy proxy = proxyOptions != null ? proxyOptions.getProxy() : null;
    HttpResponse response = DeviceOperations.request(this.getAuthenticationToken(), url, HttpMethod.GET, new byte[0], String.valueOf(requestId++), options.getHttpConnectTimeout(), options.getHttpReadTimeout(), proxy);
    String twin = new String(response.getBody(), StandardCharsets.UTF_8);
    TwinState twinState = TwinState.createFromTwinJson(twin);
    device.setVersion(twinState.getVersion());
    device.setModelId(twinState.getModelId());
    device.setETag(twinState.getETag());
    device.setTags(twinState.getTags());
    device.setDesiredProperties(twinState.getDesiredProperty());
    device.setReportedProperties(twinState.getReportedProperty());
    device.setCapabilities(twinState.getCapabilities());
    device.setConfigurations(twinState.getConfigurations());
    device.setConnectionState(twinState.getConnectionState());
}
Also used : Proxy(java.net.Proxy) TwinState(com.microsoft.azure.sdk.iot.deps.twin.TwinState) 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)

Example 15 with ProxyOptions

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

the class DeviceTwin method updateTwin.

/**
 * This method updates device twin for the specified device.
 * <p>This API uses the IoT Hub PATCH API when sending updates, but it sends the full twin with each patch update.
 * As a result, devices subscribed to twin will receive notifications that each property is changed when this API is
 * called, even if only some of the properties were changed.</p>
 * <p>See <a href="https://docs.microsoft.com/en-us/rest/api/iothub/service/devices/updatetwin">PATCH</a> for
 * more details.</p>
 *
 * @param device The device with a valid Id for which device twin is to be updated.
 * @throws IOException This exception is thrown if the IO operation failed.
 * @throws IotHubException This exception is thrown if the response verification failed.
 */
public synchronized void updateTwin(DeviceTwinDevice device) throws IotHubException, IOException {
    if (device == null || device.getDeviceId() == null || device.getDeviceId().length() == 0) {
        throw new IllegalArgumentException("Instantiate a device and set device Id to be used.");
    }
    if ((device.getDesiredMap() == null || device.getDesiredMap().isEmpty()) && (device.getTagsMap() == null || device.getTagsMap().isEmpty())) {
        throw new IllegalArgumentException("Set either desired properties or tags for the device to be updated.");
    }
    URL url;
    if ((device.getModuleId() == null) || device.getModuleId().length() == 0) {
        url = IotHubConnectionString.getUrlTwin(this.hostName, device.getDeviceId());
    } else {
        url = IotHubConnectionString.getUrlModuleTwin(this.hostName, device.getDeviceId(), device.getModuleId());
    }
    TwinState twinState = new TwinState(device.getTagsMap(), device.getDesiredMap(), null);
    String twinJson = twinState.toJsonElement().toString();
    ProxyOptions proxyOptions = options.getProxyOptions();
    Proxy proxy = proxyOptions != null ? proxyOptions.getProxy() : null;
    DeviceOperations.request(this.getAuthenticationToken(), url, HttpMethod.PATCH, twinJson.getBytes(StandardCharsets.UTF_8), String.valueOf(requestId++), options.getHttpConnectTimeout(), options.getHttpReadTimeout(), proxy);
}
Also used : TwinState(com.microsoft.azure.sdk.iot.deps.twin.TwinState) Proxy(java.net.Proxy) ProxyOptions(com.microsoft.azure.sdk.iot.service.ProxyOptions) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) URL(java.net.URL)

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