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());
}
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));
}
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();
}
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();
}
}
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();
}
}
Aggregations