Search in sources :

Example 1 with ProtocolException

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

the class AmqpsSenderLinkHandler method sendMessageAndGetDeliveryTag.

AmqpsSendResult sendMessageAndGetDeliveryTag(MessageImpl protonMessage) {
    // want to avoid negative delivery tags since -1 is the designated failure value
    if (this.nextTag == Integer.MAX_VALUE || this.nextTag < 0) {
        this.nextTag = 0;
    } else {
        this.nextTag++;
    }
    byte[] msgData = new byte[1024];
    int length;
    while (true) {
        try {
            length = protonMessage.encode(msgData, 0, msgData.length);
            break;
        } catch (BufferOverflowException e) {
            msgData = new byte[msgData.length * 2];
        }
    }
    byte[] deliveryTag = String.valueOf(this.nextTag).getBytes(StandardCharsets.UTF_8);
    Delivery delivery = this.senderLink.delivery(deliveryTag);
    try {
        log.trace("Sending {} bytes over the amqp {} sender link with address {} and link correlation id {} with link credit", length, getLinkInstanceType(), this.senderLinkAddress, this.linkCorrelationId, this.senderLink.getCredit());
        int bytesSent = this.senderLink.send(msgData, 0, length);
        if (bytesSent != length) {
            throw new ProtocolException(String.format("Amqp send operation did not send all of the expected bytes for %s sender link with link correlation id %s, retrying to send the message", getLinkInstanceType(), this.linkCorrelationId));
        }
        boolean canAdvance = this.senderLink.advance();
        if (!canAdvance) {
            throw new ProtocolException(String.format("Failed to advance the senderLink after sending a message on %s sender link with link correlation id %s, retrying to send the message", getLinkInstanceType(), this.linkCorrelationId));
        }
        log.trace("Message was sent over {} sender link with address {} and link correlation id {} with delivery tag {}", getLinkInstanceType(), this.senderLinkAddress, this.linkCorrelationId, new String(deliveryTag, StandardCharsets.UTF_8));
        log.trace("Current link credit on {} sender link with address {} and link correlation id {} is {}", this.getLinkInstanceType(), this.senderLinkAddress, this.linkCorrelationId, senderLink.getCredit());
        return new AmqpsSendResult(deliveryTag);
    } catch (Exception e) {
        log.warn("Encountered a problem while sending a message on {} sender link with address {} and link correlation id {}", getLinkInstanceType(), this.senderLinkAddress, this.linkCorrelationId, e);
        this.senderLink.advance();
        delivery.free();
        return new AmqpsSendResult();
    }
}
Also used : ProtocolException(com.microsoft.azure.sdk.iot.device.exceptions.ProtocolException) BufferOverflowException(java.nio.BufferOverflowException) Delivery(org.apache.qpid.proton.engine.Delivery) BufferOverflowException(java.nio.BufferOverflowException) ProtocolException(com.microsoft.azure.sdk.iot.device.exceptions.ProtocolException)

Example 2 with ProtocolException

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

the class PahoExceptionTranslatorTest method onConnectionLostMapsUnknownPahoException.

// Tests_SRS_PahoExceptionTranslator_34_148: [When deriving the TransportException from the provided MqttException, this function shall map all other MqttExceptions to ProtocolException.]
@Test
public void onConnectionLostMapsUnknownPahoException() throws IOException, TransportException {
    // arrange
    new NonStrictExpectations() {

        {
            mockedMqttException.getReasonCode();
            result = MqttException.REASON_CODE_SSL_CONFIG_ERROR;
        }
    };
    // act
    Exception e = PahoExceptionTranslator.convertToMqttException(new MqttException(MqttException.REASON_CODE_SSL_CONFIG_ERROR), "");
    // assert
    assertTrue(e instanceof ProtocolException);
}
Also used : ProtocolException(com.microsoft.azure.sdk.iot.device.exceptions.ProtocolException) MqttException(org.eclipse.paho.client.mqttv3.MqttException) MqttException(org.eclipse.paho.client.mqttv3.MqttException) ProtocolException(com.microsoft.azure.sdk.iot.device.exceptions.ProtocolException) IOException(java.io.IOException) TransportException(com.microsoft.azure.sdk.iot.device.exceptions.TransportException) Test(org.junit.Test)

Example 3 with ProtocolException

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

the class AmqpsIotHubConnection method onMessageAcknowledged.

@Override
public void onMessageAcknowledged(Message message, DeliveryState deliveryState, String deviceId) {
    if (deliveryState == Accepted.getInstance()) {
        this.listener.onMessageSent(message, deviceId, null);
    } else if (deliveryState instanceof Rejected) {
        // The message was not accepted by the server, and the reason why is found within the nested error
        TransportException ex = AmqpsExceptionTranslator.convertFromAmqpException(((Rejected) deliveryState).getError());
        this.listener.onMessageSent(message, deviceId, ex);
    } else if (deliveryState == Released.getInstance()) {
        // As per AMQP spec, this state means the message should be re-delivered to the server at a later time
        ProtocolException protocolException = new ProtocolException("Message was released by the amqp server");
        protocolException.setRetryable(true);
        this.listener.onMessageSent(message, deviceId, protocolException);
    } else {
        log.warn("Unexpected delivery state for sent message ({})", message);
    }
}
Also used : ProtocolException(com.microsoft.azure.sdk.iot.device.exceptions.ProtocolException) Rejected(org.apache.qpid.proton.amqp.messaging.Rejected) TransportException(com.microsoft.azure.sdk.iot.device.exceptions.TransportException)

Example 4 with ProtocolException

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

the class HttpsIotHubConnectionTest method sendMessageResultThrowsProtocolExceptionIfRequestFails.

// Tests_SRS_HTTPSIOTHUBCONNECTION_11_037: [If the IoT Hub could not be reached, the function shall throw a ProtocolException.]
@Test(expected = ProtocolException.class)
public void sendMessageResultThrowsProtocolExceptionIfRequestFails(@Mocked final IotHubRejectUri mockUri, @Mocked final IotHubStatusCode mockStatusCode) throws TransportException {
    final ProtocolException protocolException = new ProtocolException();
    final String eTag = "test-etag";
    new NonStrictExpectations() {

        {
            mockResponse.getStatus();
            returns(200, 204);
            IotHubStatusCode.getIotHubStatusCode(200);
            result = IotHubStatusCode.OK;
            IotHubStatusCode.getIotHubStatusCode(204);
            result = IotHubStatusCode.OK_EMPTY;
            mockResponse.getHeaderField(withMatch("(?i)etag"));
            result = eTag;
            mockRequest.send();
            result = protocolException;
        }
    };
    HttpsIotHubConnection conn = new HttpsIotHubConnection(mockConfig);
    Map<Message, String> eTagMap = Deencapsulation.getField(conn, "messageToETagMap");
    eTagMap.put(mockedTransportMessage, eTag);
    conn.sendMessageResult(mockedTransportMessage, IotHubMessageResult.REJECT);
}
Also used : ProtocolException(com.microsoft.azure.sdk.iot.device.exceptions.ProtocolException) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) Test(org.junit.Test)

Example 5 with ProtocolException

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

the class HttpsIotHubConnectionTest method sendEventThrowsProtocolConnectionExceptionIfRequestFails.

// Tests_SRS_HTTPSIOTHUBCONNECTION_11_012: [If the IoT Hub could not be reached, the function shall throw a TransportException.]
@Test(expected = TransportException.class)
public void sendEventThrowsProtocolConnectionExceptionIfRequestFails(@Mocked final IotHubEventUri mockUri) throws TransportException {
    final ProtocolException exception = new ProtocolException();
    new NonStrictExpectations() {

        {
            new HttpsRequest((URL) any, (HttpsMethod) any, (byte[]) any, anyString, null);
            result = mockRequest;
            mockRequest.send();
            result = exception;
        }
    };
    HttpsIotHubConnection conn = new HttpsIotHubConnection(mockConfig);
    conn.sendMessage(mockedMessage);
}
Also used : ProtocolException(com.microsoft.azure.sdk.iot.device.exceptions.ProtocolException) Test(org.junit.Test)

Aggregations

ProtocolException (com.microsoft.azure.sdk.iot.device.exceptions.ProtocolException)6 Test (org.junit.Test)3 TransportException (com.microsoft.azure.sdk.iot.device.exceptions.TransportException)2 IotHubTransportMessage (com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage)1 IOException (java.io.IOException)1 NoRouteToHostException (java.net.NoRouteToHostException)1 SocketException (java.net.SocketException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 UnknownHostException (java.net.UnknownHostException)1 BufferOverflowException (java.nio.BufferOverflowException)1 Rejected (org.apache.qpid.proton.amqp.messaging.Rejected)1 Delivery (org.apache.qpid.proton.engine.Delivery)1 MqttException (org.eclipse.paho.client.mqttv3.MqttException)1