Search in sources :

Example 1 with ProxyOptions

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

the class DeviceTwin method queryTwinCollection.

/**
 * Create a {@link QueryCollection} object that can be used to query whole pages of results at a time.
 * QueryCollection objects also allow you to provide a continuation token for the query to pick up from.
 *
 * @param sqlQuery The SQL-style query to run.
 * @param pageSize the number of results to return at a time.
 * @return The created QueryCollection object that can be used to query the service.
 * @throws MalformedURLException If twin query URL is not correct.
 */
public synchronized QueryCollection queryTwinCollection(String sqlQuery, Integer pageSize) throws MalformedURLException {
    ProxyOptions proxyOptions = options.getProxyOptions();
    Proxy proxy = proxyOptions != null ? proxyOptions.getProxy() : null;
    if (this.credentialCache != null) {
        return new QueryCollection(sqlQuery, pageSize, QueryType.TWIN, this.credentialCache, IotHubConnectionString.getUrlTwinQuery(this.hostName), HttpMethod.POST, options.getHttpConnectTimeout(), options.getHttpReadTimeout(), proxy);
    } else if (this.azureSasCredential != null) {
        return new QueryCollection(sqlQuery, pageSize, QueryType.TWIN, this.azureSasCredential, IotHubConnectionString.getUrlTwinQuery(this.hostName), HttpMethod.POST, options.getHttpConnectTimeout(), options.getHttpReadTimeout(), proxy);
    } else {
        return new QueryCollection(sqlQuery, pageSize, QueryType.TWIN, this.iotHubConnectionString, IotHubConnectionString.getUrlTwinQuery(this.hostName), HttpMethod.POST, options.getHttpConnectTimeout(), options.getHttpReadTimeout(), proxy);
    }
}
Also used : Proxy(java.net.Proxy) ProxyOptions(com.microsoft.azure.sdk.iot.service.ProxyOptions)

Example 2 with ProxyOptions

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

the class DeviceTwin method queryTwin.

/**
 * SQL-style query for twin.
 *
 * @param sqlQuery SQL-style query string to query Iot hub for Twin.
 * @param pageSize Size to limit query response by.
 * @return {@link Query} Object to be used for looking up responses for this query.
 * @throws IotHubException If query request was not successful at the Iot hub.
 * @throws IOException If input parameters are invalid.
 */
public synchronized Query queryTwin(String sqlQuery, Integer pageSize) throws IotHubException, IOException {
    if (sqlQuery == null || sqlQuery.length() == 0) {
        throw new IllegalArgumentException("Query cannot be null or empty.");
    }
    if (pageSize <= 0) {
        throw new IllegalArgumentException("pageSize cannot be negative or zero.");
    }
    Query deviceTwinQuery = new Query(sqlQuery, pageSize, QueryType.TWIN);
    ProxyOptions proxyOptions = options.getProxyOptions();
    Proxy proxy = proxyOptions != null ? proxyOptions.getProxy() : null;
    deviceTwinQuery.sendQueryRequest(this.credentialCache, this.azureSasCredential, this.iotHubConnectionString, IotHubConnectionString.getUrlTwinQuery(this.hostName), HttpMethod.POST, options.getHttpConnectTimeout(), options.getHttpReadTimeout(), proxy);
    return deviceTwinQuery;
}
Also used : Proxy(java.net.Proxy) ProxyOptions(com.microsoft.azure.sdk.iot.service.ProxyOptions)

Example 3 with ProxyOptions

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

the class JobClient method queryDeviceJob.

/**
 * Query for device Job
 *
 * @param sqlQuery sql style query over device.jobs
 * @param pageSize the value per which to limit the size of query response by.
 * @return Query object for this query
 * @throws IotHubException When IotHub fails to respond
 * @throws IOException When any of the parameters are incorrect
 */
public synchronized Query queryDeviceJob(String sqlQuery, Integer pageSize) throws IotHubException, IOException {
    if (sqlQuery == null || sqlQuery.length() == 0) {
        throw new IllegalArgumentException("Query cannot be null or empty");
    }
    if (pageSize <= 0) {
        throw new IllegalArgumentException("pagesize cannot be negative or zero");
    }
    Query deviceJobQuery = new Query(sqlQuery, pageSize, QueryType.DEVICE_JOB);
    ProxyOptions proxyOptions = options.getProxyOptions();
    Proxy proxy = proxyOptions != null ? proxyOptions.getProxy() : null;
    deviceJobQuery.sendQueryRequest(this.credentialCache, this.azureSasCredential, this.iotHubConnectionString, IotHubConnectionString.getUrlTwinQuery(this.hostName), HttpMethod.POST, options.getHttpConnectTimeout(), options.getHttpReadTimeout(), proxy);
    return deviceJobQuery;
}
Also used : Proxy(java.net.Proxy) Query(com.microsoft.azure.sdk.iot.service.devicetwin.Query) ProxyOptions(com.microsoft.azure.sdk.iot.service.ProxyOptions)

Example 4 with ProxyOptions

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

the class JobClient method cancelJob.

/**
 * Cancel a current jod 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 cancelJob(String jobId) throws IllegalArgumentException, IOException, IotHubException {
    URL url;
    if (Tools.isNullOrEmpty(jobId)) {
        throw new IllegalArgumentException("jobId cannot be null or empty");
    }
    try {
        url = IotHubConnectionString.getUrlJobsCancel(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.POST, EMPTY_JSON, 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 5 with ProxyOptions

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

the class JobClient method scheduleUpdateTwin.

/**
 * Creates a new Job to update twin tags and desired properties 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 updateTwin Twin object to use for the update
 * @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
 * @throws IotHubException if the http request failed
 */
public synchronized JobResult scheduleUpdateTwin(String jobId, String queryCondition, DeviceTwinDevice updateTwin, Date startTimeUtc, long maxExecutionTimeInSeconds) throws IllegalArgumentException, IOException, IotHubException {
    URL url;
    if (Tools.isNullOrEmpty(jobId)) {
        throw new IllegalArgumentException("jobId cannot be null or empty");
    }
    if (updateTwin == null) {
        throw new IllegalArgumentException("updateTwin cannot be null");
    }
    if (startTimeUtc == null) {
        throw new IllegalArgumentException("startTimeUtc cannot be null");
    }
    if (maxExecutionTimeInSeconds < 0) {
        throw new IllegalArgumentException("maxExecutionTimeInSeconds cannot be negative");
    }
    JobsParser jobsParser = new JobsParser(jobId, getParserFromDevice(updateTwin), 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)

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