Search in sources :

Example 31 with TransportException

use of com.microsoft.azure.sdk.iot.device.exceptions.TransportException in project azure-iot-sdk-java by Azure.

the class TransportUtils method throwTransportExceptionWithIotHubServiceType.

public static void throwTransportExceptionWithIotHubServiceType(Exception e, TransportException.IotHubService service) throws TransportException {
    TransportException transportException = new TransportException(e);
    transportException.setIotHubService(service);
    throw transportException;
}
Also used : TransportException(com.microsoft.azure.sdk.iot.device.exceptions.TransportException)

Example 32 with TransportException

use of com.microsoft.azure.sdk.iot.device.exceptions.TransportException in project azure-iot-sdk-java by Azure.

the class AmqpsCbsSenderLinkHandler method createCBSAuthenticationMessage.

// The warning is for how getSasTokenAuthentication() may return null, but this code only executes when our config
// uses SAS_TOKEN auth, and that is sufficient at confirming that getSasTokenAuthentication() will return a non-null instance
@SuppressWarnings("ConstantConditions")
private MessageImpl createCBSAuthenticationMessage(DeviceClientConfig deviceClientConfig, UUID correlationId) throws TransportException {
    MessageImpl outgoingMessage = (MessageImpl) Proton.message();
    Properties properties = new Properties();
    // Note that setting "messageId = correlationId" is intentional.
    // IotHub only responds correctly if this correlation id is set this way
    properties.setMessageId(correlationId);
    properties.setTo(CBS_TO);
    properties.setReplyTo(CBS_REPLY);
    outgoingMessage.setProperties(properties);
    Map<String, Object> userProperties = new HashMap<>(3);
    userProperties.put(OPERATION_KEY, OPERATION_VALUE);
    userProperties.put(TYPE_KEY, TYPE_VALUE);
    String host = deviceClientConfig.getGatewayHostname();
    if (host == null || host.isEmpty()) {
        host = deviceClientConfig.getIotHubHostname();
    }
    userProperties.put(NAME_KEY, host + DEVICES_PATH + deviceClientConfig.getDeviceId());
    ApplicationProperties applicationProperties = new ApplicationProperties(userProperties);
    outgoingMessage.setApplicationProperties(applicationProperties);
    Section section;
    try {
        section = new AmqpValue(String.valueOf(deviceClientConfig.getSasTokenAuthentication().getSasToken()));
        outgoingMessage.setBody(section);
    } catch (IOException e) {
        log.error("Failed to renew sas token while building new cbs authentication message", e);
        throw new TransportException(e);
    }
    return outgoingMessage;
}
Also used : HashMap(java.util.HashMap) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) IOException(java.io.IOException) Properties(org.apache.qpid.proton.amqp.messaging.Properties) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) MessageImpl(org.apache.qpid.proton.message.impl.MessageImpl) Section(org.apache.qpid.proton.amqp.messaging.Section) TransportException(com.microsoft.azure.sdk.iot.device.exceptions.TransportException) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue)

Example 33 with TransportException

use of com.microsoft.azure.sdk.iot.device.exceptions.TransportException in project azure-iot-sdk-java by Azure.

the class AmqpsSasTokenRenewalHandler method handleAuthenticationResponseMessage.

@Override
public DeliveryState handleAuthenticationResponseMessage(int status, String description, Reactor reactor) {
    try {
        if (nextToAuthenticate != null) {
            nextToAuthenticate.sendAuthenticationMessage(reactor);
            // only need to chain the next authentication once, so remove this connection
            nextToAuthenticate = null;
        }
    } catch (TransportException e) {
        log.error("Failed to send authentication message for device {}", nextToAuthenticate.amqpsSessionHandler.getDeviceId(), e);
    }
    if (status == 200) {
        log.debug("CBS message authentication succeeded for device {}", this.amqpsSessionHandler.getDeviceId());
        amqpsSessionHandler.openLinks();
    } else {
        TransportException exception = IotHubStatusCode.getConnectionStatusException(IotHubStatusCode.getIotHubStatusCode(status), description);
        this.amqpsCbsSessionHandler.onAuthenticationFailed(this.amqpsSessionHandler.getDeviceId(), exception);
    }
    return Accepted.getInstance();
}
Also used : TransportException(com.microsoft.azure.sdk.iot.device.exceptions.TransportException)

Example 34 with TransportException

use of com.microsoft.azure.sdk.iot.device.exceptions.TransportException in project azure-iot-sdk-java by Azure.

the class Mqtt method subscribe.

/**
 * Method to subscribe to mqtt broker connection.
 *
 * @param topic the topic to subscribe on mqtt broker connection.
 * @throws TransportException if failed to subscribe the mqtt topic.
 * @throws IllegalArgumentException if topic is null
 */
void subscribe(String topic) throws TransportException {
    synchronized (this.stateLock) {
        try {
            if (topic == null) {
                throw new IllegalArgumentException("Topic cannot be null");
            } else if (!this.mqttAsyncClient.isConnected()) {
                TransportException transportException = new TransportException("Cannot subscribe when mqtt client is disconnected");
                transportException.setRetryable(true);
                throw transportException;
            }
            log.debug("Sending MQTT SUBSCRIBE packet for topic {}", topic);
            IMqttToken subToken = this.mqttAsyncClient.subscribe(topic, QOS);
            subToken.waitForCompletion(MAX_SUBSCRIBE_ACK_WAIT_TIME);
            log.debug("Sent MQTT SUBSCRIBE packet for topic {} was acknowledged", topic);
        } catch (MqttException e) {
            log.warn("Encountered exception while sending MQTT SUBSCRIBE packet for topic {}", topic, e);
            throw PahoExceptionTranslator.convertToMqttException(e, "Unable to subscribe to topic :" + topic);
        }
    }
}
Also used : TransportException(com.microsoft.azure.sdk.iot.device.exceptions.TransportException)

Example 35 with TransportException

use of com.microsoft.azure.sdk.iot.device.exceptions.TransportException in project azure-iot-sdk-java by Azure.

the class MqttDeviceTwin method throwDeviceTwinTransportException.

// This method currently has a single caller (with a single value for "message"),
@SuppressWarnings("SameParameterValue")
private // but can be used to create a new TransportException with any message string.
void throwDeviceTwinTransportException(String message) throws TransportException {
    TransportException transportException = new TransportException(message);
    transportException.setIotHubService(TransportException.IotHubService.TWIN);
    throw transportException;
}
Also used : TransportException(com.microsoft.azure.sdk.iot.device.exceptions.TransportException)

Aggregations

TransportException (com.microsoft.azure.sdk.iot.device.exceptions.TransportException)46 Test (org.junit.Test)18 IOException (java.io.IOException)14 ProtocolException (com.microsoft.azure.sdk.iot.device.exceptions.ProtocolException)8 MqttException (org.eclipse.paho.client.mqttv3.MqttException)7 CountDownLatch (java.util.concurrent.CountDownLatch)4 Expectations (mockit.Expectations)4 NonStrictExpectations (mockit.NonStrictExpectations)4 Message (com.microsoft.azure.sdk.iot.device.Message)3 HttpsConnection (com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection)3 HttpsMethod (com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod)3 MqttMessaging (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging)3 Pair (org.apache.commons.lang3.tuple.Pair)3 IotHubConnectionString (com.microsoft.azure.sdk.iot.device.IotHubConnectionString)2 ModuleClientException (com.microsoft.azure.sdk.iot.device.exceptions.ModuleClientException)2 URISyntaxException (java.net.URISyntaxException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 IotHubSSLContext (com.microsoft.azure.sdk.iot.deps.auth.IotHubSSLContext)1 IotHubAuthenticationProvider (com.microsoft.azure.sdk.iot.device.auth.IotHubAuthenticationProvider)1 SignatureProvider (com.microsoft.azure.sdk.iot.device.auth.SignatureProvider)1