Search in sources :

Example 1 with ConnectResponse

use of io.swagger.server.api.model.ConnectResponse in project azure-iot-sdk-java by Azure.

the class ServiceGlue method connect.

public void connect(String connectionString, Handler<AsyncResult<ConnectResponse>> handler) {
    System.out.printf("connect called%n");
    DeviceMethod client = null;
    try {
        client = DeviceMethod.createFromConnectionString(connectionString);
    } catch (IOException e) {
        handler.handle(Future.failedFuture(e));
    }
    this._clientCount++;
    String connectionId = "serviceClient_" + this._clientCount;
    this._map.put(connectionId, client);
    ConnectResponse cr = new ConnectResponse();
    cr.setConnectionId(connectionId);
    handler.handle(Future.succeededFuture(cr));
}
Also used : ConnectResponse(io.swagger.server.api.model.ConnectResponse) IOException(java.io.IOException) DeviceMethod(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethod)

Example 2 with ConnectResponse

use of io.swagger.server.api.model.ConnectResponse in project azure-iot-sdk-java by Azure.

the class RegistryGlue method connect.

public void connect(String connectionString, Handler<AsyncResult<ConnectResponse>> handler) {
    System.out.printf("Connect called%n");
    try {
        DeviceTwin client = DeviceTwin.createFromConnectionString(connectionString);
        this._clientCount++;
        String connectionId = "registryClient_" + this._clientCount;
        this._map.put(connectionId, client);
        ConnectResponse cr = new ConnectResponse();
        cr.setConnectionId(connectionId);
        handler.handle(Future.succeededFuture(cr));
    } catch (IOException e) {
        handler.handle(Future.failedFuture(e));
    }
}
Also used : ConnectResponse(io.swagger.server.api.model.ConnectResponse) IOException(java.io.IOException) DeviceTwin(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin)

Example 3 with ConnectResponse

use of io.swagger.server.api.model.ConnectResponse in project azure-iot-sdk-java by Azure.

the class ModuleGlue method connect.

public void connect(String transportType, String connectionString, Certificate caCertificate, Handler<AsyncResult<ConnectResponse>> handler) {
    System.out.printf("Connect called with transport %s%n", transportType);
    IotHubClientProtocol protocol = this.transportFromString(transportType);
    if (protocol == null) {
        handler.handle(Future.failedFuture(new MainApiException(500, "invalid transport")));
        return;
    }
    try {
        ModuleClient client = new ModuleClient(connectionString, protocol);
        String cert = caCertificate.getCert();
        if (cert != null && !cert.isEmpty()) {
            client.setOption("SetCertificateAuthority", cert);
        }
        client.open();
        this._clientCount++;
        String connectionId = "moduleClient_" + this._clientCount;
        this._map.put(connectionId, client);
        ConnectResponse cr = new ConnectResponse();
        cr.setConnectionId(connectionId);
        handler.handle(Future.succeededFuture(cr));
    } catch (ModuleClientException | URISyntaxException | IOException e) {
        handler.handle(Future.failedFuture(e));
    }
}
Also used : ConnectResponse(io.swagger.server.api.model.ConnectResponse) ModuleClientException(com.microsoft.azure.sdk.iot.device.exceptions.ModuleClientException) IOException(java.io.IOException) MainApiException(io.swagger.server.api.MainApiException)

Example 4 with ConnectResponse

use of io.swagger.server.api.model.ConnectResponse in project azure-iot-sdk-java by Azure.

the class ModuleGlue method connectFromEnvironment.

public void connectFromEnvironment(String transportType, Handler<AsyncResult<ConnectResponse>> handler) {
    System.out.printf("ConnectFromEnvironment called with transport %s%n", transportType);
    // This is the default URL stream handler factory
    URLStreamHandlerFactory fac = protocol -> {
        if (protocol.equals("http")) {
            return new sun.net.www.protocol.http.Handler();
        } else if (protocol.equals("https")) {
            return new sun.net.www.protocol.https.Handler();
        }
        return null;
    };
    try {
        /*
            This line of code used to be run in older versions of the SDK, and it caused bugs when this library builds
            a module client from environment through the edgelet in conjunction with any other library that called this API.
            This API can only be called once per JVM process, so we had to remove the call to this API from our SDK to maintain
            compatibility with other libraries that call this API. We call this API in this test now so that we guarantee
            that our module client code works even when this API is used beforehand to avoid regression
             */
        URL.setURLStreamHandlerFactory(fac);
    } catch (Error e) {
    // this function only throws if the factory has already been set, so we can ignore this error
    }
    IotHubClientProtocol protocol = this.transportFromString(transportType);
    if (protocol == null) {
        handler.handle(Future.failedFuture(new MainApiException(500, "invalid transport")));
        return;
    }
    try {
        ModuleClient client = ModuleClient.createFromEnvironment(protocol);
        client.open();
        this._clientCount++;
        String connectionId = "moduleClient_" + this._clientCount;
        this._map.put(connectionId, client);
        ConnectResponse cr = new ConnectResponse();
        cr.setConnectionId(connectionId);
        handler.handle(Future.succeededFuture(cr));
    } catch (ModuleClientException | IOException e) {
        handler.handle(Future.failedFuture(e));
    }
}
Also used : Property(com.microsoft.azure.sdk.iot.device.DeviceTwin.Property) MainApiException(io.swagger.server.api.MainApiException) RoundtripMethodCallBody(io.swagger.server.api.model.RoundtripMethodCallBody) DeviceMethodData(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodData) Json(io.vertx.core.json.Json) java.util(java.util) com.microsoft.azure.sdk.iot.device(com.microsoft.azure.sdk.iot.device) DeviceMethodCallback(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodCallback) TwinPropertyCallBack(com.microsoft.azure.sdk.iot.device.DeviceTwin.TwinPropertyCallBack) IOException(java.io.IOException) MethodRequest(com.microsoft.azure.sdk.iot.device.edge.MethodRequest) Future(io.vertx.core.Future) StandardCharsets(java.nio.charset.StandardCharsets) MethodResult(com.microsoft.azure.sdk.iot.device.edge.MethodResult) ConnectResponse(io.swagger.server.api.model.ConnectResponse) java.net(java.net) ModuleClientException(com.microsoft.azure.sdk.iot.device.exceptions.ModuleClientException) JsonObject(io.vertx.core.json.JsonObject) Certificate(io.swagger.server.api.model.Certificate) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) ConnectResponse(io.swagger.server.api.model.ConnectResponse) ModuleClientException(com.microsoft.azure.sdk.iot.device.exceptions.ModuleClientException) Handler(io.vertx.core.Handler) IOException(java.io.IOException) java.net(java.net) MainApiException(io.swagger.server.api.MainApiException)

Aggregations

ConnectResponse (io.swagger.server.api.model.ConnectResponse)4 IOException (java.io.IOException)4 ModuleClientException (com.microsoft.azure.sdk.iot.device.exceptions.ModuleClientException)2 MainApiException (io.swagger.server.api.MainApiException)2 com.microsoft.azure.sdk.iot.device (com.microsoft.azure.sdk.iot.device)1 DeviceMethodCallback (com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodCallback)1 DeviceMethodData (com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodData)1 Property (com.microsoft.azure.sdk.iot.device.DeviceTwin.Property)1 TwinPropertyCallBack (com.microsoft.azure.sdk.iot.device.DeviceTwin.TwinPropertyCallBack)1 MethodRequest (com.microsoft.azure.sdk.iot.device.edge.MethodRequest)1 MethodResult (com.microsoft.azure.sdk.iot.device.edge.MethodResult)1 DeviceMethod (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethod)1 DeviceTwin (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin)1 Certificate (io.swagger.server.api.model.Certificate)1 RoundtripMethodCallBody (io.swagger.server.api.model.RoundtripMethodCallBody)1 AsyncResult (io.vertx.core.AsyncResult)1 Future (io.vertx.core.Future)1 Handler (io.vertx.core.Handler)1 Json (io.vertx.core.json.Json)1 JsonObject (io.vertx.core.json.JsonObject)1