use of com.microsoft.azure.sdk.iot.device.exceptions.TransportException in project azure-iot-sdk-java by Azure.
the class PahoExceptionTranslatorTest method onConnectionLostMapsBrokerUnavailableException.
// Tests_SRS_PahoExceptionTranslator_34_143: [When deriving the TransportException from the provided MqttException, this function shall map REASON_CODE_BROKER_UNAVAILABLE to MqttServerUnavailableException.]
@Test
public void onConnectionLostMapsBrokerUnavailableException() throws IOException, TransportException {
// arrange
new NonStrictExpectations() {
{
mockedMqttException.getReasonCode();
result = MqttException.REASON_CODE_BROKER_UNAVAILABLE;
}
};
// act
Exception e = PahoExceptionTranslator.convertToMqttException(new MqttException(MqttException.REASON_CODE_BROKER_UNAVAILABLE), "");
// assert
assertTrue(e instanceof MqttServerUnavailableException);
}
use of com.microsoft.azure.sdk.iot.device.exceptions.TransportException in project azure-iot-sdk-java by Azure.
the class ModuleClient method invokeMethod.
/**
* Invoke a method on a device
* @param deviceId the device to invoke a method on
* @param methodRequest the request containing the method to invoke on the device
* @return the result of the method call
* @throws ModuleClientException if the method cannot be invoked
* @throws IllegalArgumentException if deviceid is null or empty
*/
public MethodResult invokeMethod(String deviceId, MethodRequest methodRequest) throws ModuleClientException, IllegalArgumentException {
if (deviceId == null || deviceId.isEmpty()) {
// Codes_SRS_MODULECLIENT_34_039: [If the provided deviceId is null or empty, this function shall throw an IllegalArgumentException.]
throw new IllegalArgumentException("DeviceId cannot be null or empty");
}
try {
// Codes_SRS_MODULECLIENT_34_033: [This function shall create an HttpsTransportManager and use it to invoke the method on the device.]
HttpsTransportManager httpsTransportManager = new HttpsTransportManager(this.config);
httpsTransportManager.open();
return httpsTransportManager.invokeMethod(methodRequest, deviceId, "");
} catch (URISyntaxException | IOException | TransportException e) {
// Codes_SRS_MODULECLIENT_34_034: [If this function encounters an exception, it shall throw a moduleClientException with that exception nested.]
throw new ModuleClientException("Could not invoke method", e);
}
}
use of com.microsoft.azure.sdk.iot.device.exceptions.TransportException in project azure-iot-sdk-java by Azure.
the class MultiplexingClient method open.
/**
* Opens this multiplexing client. This may be done before or after registering any number of device clients.
* <p>
* This call behaves synchronously, so if it returns without throwing, then all registered device clients were
* successfully opened.
* <p>
* If this client is already open, then this method will do nothing.
* <p>
* @param withRetry if true, this open call will apply the current retry policy to allow for the open call to be
* retried if it fails.
*
* @throws MultiplexingClientException If any IO or authentication errors occur while opening the multiplexed connection.
* @throws MultiplexingClientDeviceRegistrationAuthenticationException If one or many of the registered devices failed to authenticate.
* Any devices not found in the map of registration exceptions provided by
* {@link MultiplexingClientDeviceRegistrationAuthenticationException#getRegistrationExceptions()} have registered successfully.
* Even when this is thrown, the AMQPS/AMQPS_WS connection is still open, and other clients may be registered to it.
*/
public void open(boolean withRetry) throws MultiplexingClientException {
synchronized (this.operationLock) {
log.info("Opening multiplexing client");
try {
this.deviceIO.openWithoutWrappingException(withRetry);
} catch (TransportException e) {
// so that users don't need to look at the cause of the thrown exception to get this important information.
if (e instanceof MultiplexingDeviceUnauthorizedException) {
MultiplexingClientDeviceRegistrationAuthenticationException newException = new MultiplexingClientDeviceRegistrationAuthenticationException(OPEN_ERROR_MESSAGE, e);
// Bring the exceptions map from the cause to the root level exception, so that users don't have to use
// fields from inner exceptions.
newException.setRegistrationExceptionsMap(((MultiplexingDeviceUnauthorizedException) e).getRegistrationExceptions());
throw newException;
}
throw new MultiplexingClientException(OPEN_ERROR_MESSAGE, e);
}
log.info("Successfully opened multiplexing client");
}
}
use of com.microsoft.azure.sdk.iot.device.exceptions.TransportException in project azure-iot-sdk-java by Azure.
the class MqttIotHubConnection method onMessageArrived.
@Override
public void onMessageArrived(int messageId) {
IotHubTransportMessage transportMessage = null;
try {
transportMessage = this.deviceMethod.receive();
if (transportMessage != null) {
log.trace("Received MQTT device method message ({})", transportMessage);
} else {
transportMessage = deviceTwin.receive();
if (transportMessage != null) {
log.trace("Received MQTT device twin message ({})", transportMessage);
} else {
transportMessage = deviceMessaging.receive();
if (transportMessage != null) {
log.trace("Received MQTT device messaging message ({})", transportMessage);
}
}
}
} catch (TransportException e) {
this.listener.onMessageReceived(null, new TransportException("Failed to receive message from service", e));
log.error("Encountered exception while receiving message over MQTT", e);
}
if (transportMessage == null) {
// Ack is not sent to service for this message because we cannot interpret the message. Service will likely re-send
this.listener.onMessageReceived(null, new TransportException("Message sent from service could not be parsed"));
log.warn("Received message that could not be parsed. That message has been ignored.");
} else {
log.trace("MQTT received message so it has been added to the messages to acknowledge list ({})", transportMessage);
this.receivedMessagesToAcknowledge.put(transportMessage, messageId);
switch(transportMessage.getMessageType()) {
case DEVICE_TWIN:
transportMessage.setMessageCallback(this.config.getDeviceTwinMessageCallback());
transportMessage.setMessageCallbackContext(this.config.getDeviceTwinMessageContext());
break;
case DEVICE_METHODS:
transportMessage.setMessageCallback(this.config.getDeviceMethodsMessageCallback());
transportMessage.setMessageCallbackContext(this.config.getDeviceMethodsMessageContext());
break;
case DEVICE_TELEMETRY:
transportMessage.setMessageCallback(this.config.getDeviceTelemetryMessageCallback(transportMessage.getInputName()));
transportMessage.setMessageCallbackContext(this.config.getDeviceTelemetryMessageContext(transportMessage.getInputName()));
break;
case UNKNOWN:
case CBS_AUTHENTICATION:
default:
}
this.listener.onMessageReceived(transportMessage, null);
}
}
use of com.microsoft.azure.sdk.iot.device.exceptions.TransportException in project azure-iot-sdk-java by Azure.
the class MqttMessaging method appendPropertyIfPresent.
/**
* Appends the property to the provided stringbuilder if the property value is not null.
* @param stringBuilder the builder to build upon
* @param separatorNeeded if a separator should precede the new property
* @param propertyKey the mqtt topic string property key
* @param propertyValue the property value (message id, correlation id, etc.)
* @return true if a separator will be needed for any later properties appended on
*/
private boolean appendPropertyIfPresent(StringBuilder stringBuilder, boolean separatorNeeded, String propertyKey, String propertyValue, boolean isApplicationProperty) throws TransportException {
try {
if (propertyValue != null && !propertyValue.isEmpty()) {
if (separatorNeeded) {
stringBuilder.append(MESSAGE_PROPERTY_SEPARATOR);
}
if (isApplicationProperty) {
// URLEncoder.Encode incorrectly encodes space characters as '+'. For MQTT to work, we need to replace those '+' with "%20"
stringBuilder.append(URLEncoder.encode(propertyKey, StandardCharsets.UTF_8.name()).replaceAll("\\+", "%20"));
} else {
stringBuilder.append(propertyKey);
}
stringBuilder.append(MESSAGE_PROPERTY_KEY_VALUE_SEPARATOR);
// URLEncoder.Encode incorrectly encodes space characters as '+'. For MQTT to work, we need to replace those '+' with "%20"
stringBuilder.append(URLEncoder.encode(propertyValue, StandardCharsets.UTF_8.name()).replaceAll("\\+", "%20"));
return true;
}
return separatorNeeded;
} catch (UnsupportedEncodingException e) {
throw new TransportException("Could not utf-8 encode the property with name " + propertyKey + " and value " + propertyValue, e);
}
}
Aggregations