Search in sources :

Example 1 with ProvisioningErrorParser

use of com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.ProvisioningErrorParser in project azure-iot-sdk-java by Azure.

the class ProvisioningErrorParserTest method getExceptionMessageContainsErrorCodeTrackingIdAndMessage.

// Tests_SRS_PROVISIONING_ERROR_PARSER_34_002: [This function shall return a string containing the saved error code, message, and tracking id]
@Test
public void getExceptionMessageContainsErrorCodeTrackingIdAndMessage() {
    // arrange
    final int statusCode = 200;
    final String trackingIdValue = "some tracking id";
    final String messageValue = "This is an error message!";
    final String provisioningErrorParserJson = "{\"errorCode\" : " + statusCode + ", \"trackingId\" : \"" + trackingIdValue + "\", \"message\" : \"" + messageValue + "\"}";
    ProvisioningErrorParser errorParser = ProvisioningErrorParser.createFromJson(provisioningErrorParserJson);
    // act
    String errorMessage = errorParser.getExceptionMessage();
    // assert
    assertTrue(errorMessage.contains(String.valueOf(statusCode)));
    assertTrue(errorMessage.contains(messageValue));
    assertTrue(errorMessage.contains(trackingIdValue));
}
Also used : ProvisioningErrorParser(com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.ProvisioningErrorParser) Test(org.junit.Test)

Example 2 with ProvisioningErrorParser

use of com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.ProvisioningErrorParser in project azure-iot-sdk-java by Azure.

the class RegisterTask method authenticateWithSasToken.

private RegistrationOperationStatusParser authenticateWithSasToken(RequestData requestData) throws IOException, InterruptedException, ProvisioningDeviceClientException, SecurityProviderException {
    /*SRS_RegisterTask_25_014: [ If the provided security client is for Key then, this method shall construct SasToken by doing the following

            1. Build a tokenScope of format <scope>/registrations/<registrationId>
            2. Sign the HSM with the string of format <tokenScope>/n<expiryTime> and receive a token
            3. Encode the token to Base64 format and UrlEncode it to generate the signature. ]*/
    String sasToken = this.constructSasToken();
    requestData.setSasToken(sasToken);
    // SRS_RegisterTask_25_016: [ If the provided security client is for Key then, this method shall trigger authenticateWithProvisioningService on the contract API using the sasToken generated and wait for response and return it. ]
    ResponseData responseDataForSasTokenAuth = new ResponseData();
    this.provisioningDeviceClientContract.authenticateWithProvisioningService(requestData, responseCallback, responseDataForSasTokenAuth);
    waitForResponse(responseDataForSasTokenAuth);
    if (responseDataForSasTokenAuth.getResponseData() != null && responseDataForSasTokenAuth.getContractState() == DPS_REGISTRATION_RECEIVED) {
        this.authorization.setSasToken(sasToken);
        String jsonBody = new String(responseDataForSasTokenAuth.getResponseData(), StandardCharsets.UTF_8);
        try {
            return RegistrationOperationStatusParser.createFromJson(jsonBody);
        } catch (IllegalArgumentException e) {
            ProvisioningErrorParser provisioningErrorParser = ProvisioningErrorParser.createFromJson(jsonBody);
            throw new ProvisioningDeviceClientException(provisioningErrorParser.getExceptionMessage());
        }
    } else {
        // SRS_RegisterTask_25_017: [ If the provided security client is for Key then, this method shall throw ProvisioningDeviceClientException if null response to authenticateWithProvisioningService is received. ]
        throw new ProvisioningDeviceClientAuthenticationException("Service did not authorize SasToken");
    }
}
Also used : ProvisioningErrorParser(com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.ProvisioningErrorParser) ProvisioningDeviceClientAuthenticationException(com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceClientAuthenticationException) ProvisioningDeviceClientException(com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceClientException)

Example 3 with ProvisioningErrorParser

use of com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.ProvisioningErrorParser in project azure-iot-sdk-java by Azure.

the class RegisterTask method authenticateWithX509.

private RegistrationOperationStatusParser authenticateWithX509(RequestData requestData) throws ProvisioningDeviceClientException {
    try {
        // SRS_RegisterTask_25_006: [ If the provided security client is for X509 then, this method shall trigger authenticateWithProvisioningService on the contract API and wait for response and return it. ]
        ResponseData dpsRegistrationData = new ResponseData();
        this.provisioningDeviceClientContract.authenticateWithProvisioningService(requestData, responseCallback, dpsRegistrationData);
        waitForResponse(dpsRegistrationData);
        if (dpsRegistrationData.getResponseData() != null && dpsRegistrationData.getContractState() == DPS_REGISTRATION_RECEIVED) {
            String jsonBody = new String(dpsRegistrationData.getResponseData(), StandardCharsets.UTF_8);
            try {
                return RegistrationOperationStatusParser.createFromJson(jsonBody);
            } catch (IllegalArgumentException e) {
                // SRS_StatusTask_34_010: [ If the response data cannot be parsed into a RegistrationOperationStatusParser,
                // this function shall parse it into a ProvisioningErrorParser and throw a ProvisioningDeviceClientException with the parsed message. ]
                ProvisioningErrorParser provisioningErrorParser = ProvisioningErrorParser.createFromJson(jsonBody);
                throw new ProvisioningDeviceClientException(provisioningErrorParser.getExceptionMessage());
            }
        } else {
            // SRS_RegisterTask_25_007: [ If the provided security client is for X509 then, this method shall throw ProvisioningDeviceClientException if null response is received. ]
            throw new ProvisioningDeviceClientException("Did not receive DPS registration successfully");
        }
    } catch (InterruptedException e) {
        throw new ProvisioningDeviceClientException(e);
    }
}
Also used : ProvisioningErrorParser(com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.ProvisioningErrorParser) ProvisioningDeviceClientException(com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceClientException)

Example 4 with ProvisioningErrorParser

use of com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.ProvisioningErrorParser in project azure-iot-sdk-java by Azure.

the class StatusTask method getRegistrationStatus.

private RegistrationOperationStatusParser getRegistrationStatus(String operationId, Authorization authorization) throws ProvisioningDeviceClientException {
    try {
        // SRS_StatusTask_25_003: [ This method shall throw ProvisioningDeviceClientException if registration id is null or empty. ]
        String registrationId = this.securityProvider.getRegistrationId();
        if (registrationId == null || registrationId.isEmpty()) {
            throw new ProvisioningDeviceSecurityException("registrationId cannot be null or empty");
        }
        // SRS_StatusTask_25_004: [ This method shall retrieve the SSL context from Authorization and throw ProvisioningDeviceClientException if it is null. ]
        SSLContext sslContext = authorization.getSslContext();
        if (sslContext == null) {
            throw new ProvisioningDeviceSecurityException("SSL context cannot be null");
        }
        RequestData requestData = new RequestData(registrationId, operationId, authorization.getSslContext(), authorization.getSasToken(), null);
        // SRS_StatusTask_25_005: [ This method shall trigger getRegistrationState on the contract API and wait for response and return it. ]
        ResponseData responseData = new ResponseData();
        provisioningDeviceClientContract.getRegistrationStatus(requestData, new ResponseCallbackImpl(), responseData);
        if (responseData.getResponseData() == null || responseData.getContractState() != ContractState.DPS_REGISTRATION_RECEIVED) {
            Thread.sleep(MAX_WAIT_FOR_STATUS_RESPONSE);
        }
        if (responseData.getResponseData() != null && responseData.getContractState() == ContractState.DPS_REGISTRATION_RECEIVED) {
            String jsonBody = new String(responseData.getResponseData(), StandardCharsets.UTF_8);
            try {
                return RegistrationOperationStatusParser.createFromJson(jsonBody);
            } catch (IllegalArgumentException e) {
                // SRS_StatusTask_34_007: [ If the response data cannot be parsed into a RegistrationOperationStatusParser,
                // this function shall parse it into a ProvisioningErrorParser and throw a ProvisioningDeviceClientException with the parsed message. ]
                ProvisioningErrorParser provisioningErrorParser = ProvisioningErrorParser.createFromJson(jsonBody);
                throw new ProvisioningDeviceClientException(provisioningErrorParser.getExceptionMessage());
            }
        } else {
            // SRS_StatusTask_25_006: [ This method shall throw ProvisioningDeviceClientException if null response or no response is received in maximum time of 90 seconds. ]
            throw new ProvisioningDeviceClientException("Did not receive DPS Status information");
        }
    } catch (InterruptedException | SecurityProviderException e) {
        throw new ProvisioningDeviceClientException(e);
    }
}
Also used : ProvisioningDeviceSecurityException(com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceSecurityException) ProvisioningErrorParser(com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.ProvisioningErrorParser) SecurityProviderException(com.microsoft.azure.sdk.iot.provisioning.security.exceptions.SecurityProviderException) SSLContext(javax.net.ssl.SSLContext) ProvisioningDeviceClientException(com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceClientException)

Example 5 with ProvisioningErrorParser

use of com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.ProvisioningErrorParser in project azure-iot-sdk-java by Azure.

the class ProvisioningErrorParserTest method createFromJsonWorks.

// Tests_SRS_PROVISIONING_ERROR_PARSER_34_001: [This function shall create a ProvisioningErrorParser instance from the provided json]
@Test
public void createFromJsonWorks() {
    // arrange
    final int statusCode = 200;
    final String trackingIdValue = "some tracking id";
    final String messageValue = "This is an error message!";
    final String provisioningErrorParserJson = "{\"errorCode\" : " + statusCode + ", \"trackingId\" : \"" + trackingIdValue + "\", \"message\" : \"" + messageValue + "\"}";
    // act
    ProvisioningErrorParser errorParser = ProvisioningErrorParser.createFromJson(provisioningErrorParserJson);
    // assert
    assertEquals(statusCode, (int) Deencapsulation.getField(errorParser, "errorCode"));
    assertEquals(trackingIdValue, Deencapsulation.getField(errorParser, "trackingId"));
    assertEquals(messageValue, Deencapsulation.getField(errorParser, "message"));
}
Also used : ProvisioningErrorParser(com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.ProvisioningErrorParser) Test(org.junit.Test)

Aggregations

ProvisioningErrorParser (com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.ProvisioningErrorParser)5 ProvisioningDeviceClientException (com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceClientException)3 Test (org.junit.Test)2 ProvisioningDeviceClientAuthenticationException (com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceClientAuthenticationException)1 ProvisioningDeviceSecurityException (com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceSecurityException)1 SecurityProviderException (com.microsoft.azure.sdk.iot.provisioning.security.exceptions.SecurityProviderException)1 SSLContext (javax.net.ssl.SSLContext)1