use of com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceClientException in project azure-iot-sdk-java by Azure.
the class ProvisioningAmqpOperations method sendAmqpMessage.
private synchronized void sendAmqpMessage(String msgType, String operationId, byte[] msgBody) throws ProvisioningDeviceClientException {
try {
AmqpMessage outgoingMessage = new AmqpMessage();
Map<String, Object> userProperties = new HashMap<>();
userProperties.put(AMQP_OP_TYPE_PROPERTY, msgType);
if (operationId != null && !operationId.isEmpty()) {
userProperties.put(AMQP_OPERATION_ID, operationId);
}
outgoingMessage.setApplicationProperty(userProperties);
if (msgBody != null && msgBody.length > 0) {
outgoingMessage.setBody(msgBody);
}
this.amqpConnection.sendAmqpMessage(outgoingMessage);
} catch (Exception e) {
throw new ProvisioningDeviceTransportException("Failure sending AMQP message", e);
}
}
use of com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceClientException in project azure-iot-sdk-java by Azure.
the class ProvisioningAmqpOperations method open.
/**
* Opens the Amqp connection
* @param registrationId The specified registration id for the connection
* @param sslContext The SSLContext that will get used for this connection
* @param saslHandler custom handler for sasl logic. May be null if no sasl frames are expected
* @param useWebSockets Flag to determine if using WebSockets
* @throws ProvisioningDeviceConnectionException if connection could not succeed for any reason.
*/
public void open(String registrationId, SSLContext sslContext, SaslHandler saslHandler, boolean useWebSockets) throws ProvisioningDeviceConnectionException {
// SRS_ProvisioningAmqpOperations_07_003: [If amqpConnection is not null and is connected, open shall do nothing .]
try {
if (this.amqpConnection == null || !this.amqpConnection.isConnected()) {
// SRS_ProvisioningAmqpOperations_07_004: [open shall throw ProvisioningDeviceClientException if either registrationId or sslContext are null or empty.]
if (registrationId == null || registrationId.isEmpty()) {
throw new ProvisioningDeviceConnectionException(new IllegalArgumentException("registration Id cannot be null or empty"));
}
if (sslContext == null) {
throw new ProvisioningDeviceConnectionException(new IllegalArgumentException("sslContext cannot be null"));
}
addAmqpLinkProperty(API_VERSION_KEY, SDKUtils.getServiceApiVersion());
addAmqpLinkProperty(CLIENT_VERSION_IDENTIFIER_KEY, SDKUtils.getUserAgentString());
// SRS_ProvisioningAmqpOperations_07_005: [This method shall construct the Link Address with /<scopeId>/registrations/<registrationId>.]
this.amqpLinkAddress = String.format(AMQP_ADDRESS_FMT, this.idScope, registrationId);
this.amqpConnection = new AmqpsConnection(this.hostName, this, sslContext, saslHandler, useWebSockets);
this.amqpConnection.setListener(this);
if (saslHandler != null) {
// SRS_ProvisioningAmqpOperations_34_019: [If the provided sasl handler is not null, this function shall open the underlying amqpConnection asynchronously.]
this.amqpConnection.openAmqpAsync();
} else {
// SRS_ProvisioningAmqpOperations_34_020: [If the provided sasl handler is null, this function shall open the underlying amqpConnection synchronously.]
this.amqpConnection.open();
}
}
} catch (Exception ex) {
// SRS_ProvisioningAmqpOperations_07_006: [This method shall connect to the amqp connection and throw ProvisioningDeviceConnectionException on error.]
throw new ProvisioningDeviceConnectionException("Failure opening amqp connection", ex);
}
}
use of com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceClientException in project azure-iot-sdk-java by Azure.
the class ContractAPIAmqp method requestNonceForTPM.
/**
* Requests hub to provide a device key to begin authentication over AMQP (Only for TPM)
* @param requestData A non {@code null} value for the RequestData to be used
* @param responseCallback A non {@code null} value for the callback
* @param authorizationCallbackContext An object for context. Can be {@code null}
* @throws ProvisioningDeviceClientException If any of the parameters are invalid ({@code null} or empty)
* @throws ProvisioningDeviceTransportException If any of the API calls to transport fail
* @throws ProvisioningDeviceHubException If hub responds back with an invalid status
*/
public synchronized void requestNonceForTPM(RequestData requestData, ResponseCallback responseCallback, Object authorizationCallbackContext) throws ProvisioningDeviceClientException {
if (requestData == null) {
throw new ProvisioningDeviceClientException(new IllegalArgumentException("requestData cannot be null"));
}
if (responseCallback == null) {
throw new ProvisioningDeviceClientException("responseCallback cannot be null");
}
String registrationId = requestData.getRegistrationId();
if (registrationId == null || registrationId.isEmpty()) {
throw new ProvisioningDeviceClientException(new IllegalArgumentException("registration Id cannot be null or empty"));
}
byte[] endorsementKey = requestData.getEndorsementKey();
if (endorsementKey == null) {
throw new ProvisioningDeviceClientException(new IllegalArgumentException("Endorsement key cannot be null"));
}
byte[] storageRootKey = requestData.getStorageRootKey();
if (storageRootKey == null) {
throw new ProvisioningDeviceClientException(new IllegalArgumentException("Storage root key cannot be null"));
}
if (requestData.getSslContext() == null) {
throw new ProvisioningDeviceClientException(new IllegalArgumentException("sslContext cannot be null"));
}
SSLContext sslContext = requestData.getSslContext();
this.amqpSaslHandler = new AmqpsProvisioningSaslHandler(this.idScope, requestData.getRegistrationId(), requestData.getEndorsementKey(), requestData.getStorageRootKey(), responseCallback, authorizationCallbackContext);
this.provisioningAmqpOperations.open(registrationId, sslContext, this.amqpSaslHandler, this.useWebSockets);
}
use of com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceClientException in project azure-iot-sdk-java by Azure.
the class ContractAPIAmqp method getRegistrationStatus.
/**
* Gets the registration status over AMQP
* @param requestData A non {@code null} value with all the request data
* @param responseCallback A non {@code null} value for the callback
* @param callbackContext An object for context. Can be {@code null}
* @throws ProvisioningDeviceClientException If any of the parameters are invalid ({@code null} or empty)
* @throws ProvisioningDeviceTransportException If any of the API calls to transport fail
* @throws ProvisioningDeviceHubException If hub responds back with an invalid status
*/
public synchronized void getRegistrationStatus(RequestData requestData, ResponseCallback responseCallback, Object callbackContext) throws ProvisioningDeviceClientException {
// SRS_ContractAPIAmqp_07_009: [If requestData is null this method shall throw ProvisioningDeviceClientException.]
if (requestData == null) {
throw new ProvisioningDeviceClientException(new IllegalArgumentException("requestData cannot be null"));
}
// SRS_ContractAPIAmqp_07_010: [If requestData.getOperationId() is null or empty, this method shall throw ProvisioningDeviceClientException.]
String operationId = requestData.getOperationId();
if (operationId == null || operationId.isEmpty()) {
throw new ProvisioningDeviceClientException(new IllegalArgumentException("operationId cannot be null or empty"));
}
// SRS_ContractAPIAmqp_07_011: [If responseCallback is null, this method shall throw ProvisioningDeviceClientException.]
if (responseCallback == null) {
throw new ProvisioningDeviceClientException("responseCallback cannot be null");
}
// SRS_ContractAPIAmqp_07_012: [If amqpConnection is null or not connected, this method shall throw ProvisioningDeviceConnectionException.]
try {
if (!this.provisioningAmqpOperations.isAmqpConnected()) {
throw new ProvisioningDeviceConnectionException("Amqp is not connected");
}
} catch (Exception e) {
throw new ProvisioningDeviceClientException(e);
}
// SRS_ContractAPIAmqp_07_013: [This method shall send an AMQP message with the property of iotdps-get-operationstatus and the OperationId.]
this.provisioningAmqpOperations.sendStatusMessage(operationId, responseCallback, callbackContext);
}
use of com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceClientException in project azure-iot-sdk-java by Azure.
the class AmqpsProvisioningSaslHandler method handleThirdChallenge.
private byte[] handleThirdChallenge(byte[] challengeData) throws ProvisioningDeviceClientException {
// validate challenge
if (challengeData.length < 1 || challengeData[0] != FINAL_SEGMENT_CONTROL_BYTE) {
// Codes_SRS_AMQPSPROVISIONINGSASLHANDLER_34_015: [If this object is waiting for the third challenge, this function shall validate that this challenge payload contains a control byte with the mask 0xC1 and shall throw an IllegalStateException if it is not.]
throw new IllegalStateException("Unexpected challenge data");
}
// Codes_SRS_AMQPSPROVISIONINGSASLHANDLER_34_016: [If this object is waiting for the third challenge, this function shall read the challenge in the format "control byte + nonce (second half)" and save the nonce portion.]
this.challengeKey = buildNonceFromThirdChallenge(challengeData);
// Codes_SRS_AMQPSPROVISIONINGSASLHANDLER_34_017: [If this object is waiting for the third challenge, this function shall put together the full nonce byte array and run the saved responseCallback with the nonce and DPS_REGISTRATION_RECEIVED.]
this.responseCallback.run(new ResponseData(this.challengeKey, ContractState.DPS_REGISTRATION_RECEIVED, 0), this.authorizationCallbackContext);
this.challengeState = ChallengeState.WAITING_TO_SEND_SAS_TOKEN;
// Codes_SRS_AMQPSPROVISIONINGSASLHANDLER_34_018: [If this object is waiting for the third challenge, after running the saved responseCallback, this function shall wait for the sas token to be set before returning a payload in the format "control byte + sas token".]
long millisecondsElapsed = 0;
long waitTimeStart = System.currentTimeMillis();
while (this.sasToken == null && millisecondsElapsed < MAX_MILLISECONDS_TIMEOUT_FOR_SAS_TOKEN_WAIT) {
try {
// noinspection BusyWait
Thread.sleep(WAIT_INTERVALS);
} catch (InterruptedException e) {
throw new ProvisioningDeviceClientException(e);
}
millisecondsElapsed = System.currentTimeMillis() - waitTimeStart;
}
if (millisecondsElapsed >= MAX_MILLISECONDS_TIMEOUT_FOR_SAS_TOKEN_WAIT) {
// Codes_SRS_AMQPSPROVISIONINGSASLHANDLER_34_019: [If this object is waiting for the third challenge, and if the sas token is not provided within 3 minutes of waiting, this function shall throw a SecurityException.]
throw new ProvisioningDeviceSecurityException("Sasl negotiation failed: Sas token was never supplied to finish negotiation");
}
this.challengeState = ChallengeState.WAITING_FOR_FINAL_OUTCOME;
return prependByteArrayWithControlByte(INIT_SEGMENT_CONTROL_BYTE, sasToken.getBytes(StandardCharsets.UTF_8));
}
Aggregations