Search in sources :

Example 16 with ProxyOptions

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

the class DeviceMethod method invokeMethod.

/**
 * Directly invokes a method on the device and return its result.
 *
 * @param url is the path where the request is send to.
 * @param methodName is the name of the method that shall be invoked on the device.
 * @param responseTimeoutInSeconds is the maximum waiting time for a response from the device in seconds.
 * @param connectTimeoutInSeconds is the maximum waiting time for a response from the connection in seconds.
 * @param payload is the the method parameter.
 * @return the status and payload resulted from the method invoke.
 * @throws IotHubException This exception is thrown if the response verification failed.
 * @throws IOException This exception is thrown if the IO operation failed.
 */
private synchronized MethodResult invokeMethod(URL url, String methodName, Long responseTimeoutInSeconds, Long connectTimeoutInSeconds, Object payload) throws IotHubException, IOException {
    MethodParser methodParser = new MethodParser(methodName, responseTimeoutInSeconds, connectTimeoutInSeconds, payload);
    String json = methodParser.toJson();
    if (json == null) {
        throw new IllegalArgumentException("MethodParser return null Json");
    }
    ProxyOptions proxyOptions = options.getProxyOptions();
    Proxy proxy = proxyOptions != null ? proxyOptions.getProxy() : null;
    HttpResponse response = DeviceOperations.request(this.getAuthenticationToken(), url, HttpMethod.POST, json.getBytes(StandardCharsets.UTF_8), String.valueOf(requestId++), options.getHttpConnectTimeout(), options.getHttpReadTimeout(), proxy);
    MethodParser methodParserResponse = new MethodParser();
    methodParserResponse.fromJson(new String(response.getBody(), StandardCharsets.UTF_8));
    return new MethodResult(methodParserResponse.getStatus(), methodParserResponse.getPayload());
}
Also used : Proxy(java.net.Proxy) 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) MethodParser(com.microsoft.azure.sdk.iot.deps.serializer.MethodParser)

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