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;
}
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;
}
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();
}
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);
}
}
}
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;
}
Aggregations