Search in sources :

Example 6 with ProvisioningServiceClientServiceException

use of com.microsoft.azure.sdk.iot.provisioning.service.exceptions.ProvisioningServiceClientServiceException in project azure-iot-sdk-java by Azure.

the class IndividualEnrollmentManager method createOrUpdate.

/**
 * Create or update a device enrollment record.
 *
 * @see ProvisioningServiceClient#createOrUpdateIndividualEnrollment(IndividualEnrollment)
 *
 * @param individualEnrollment is an {@link IndividualEnrollment} that describes the enrollment that will be created of updated. It cannot be {@code null}.
 * @return An {@link IndividualEnrollment} with the result of the creation or update request.
 * @throws IllegalArgumentException if the provided parameter is not correct.
 * @throws ProvisioningServiceClientTransportException if the SDK failed to send the request to the Device Provisioning Service.
 * @throws ProvisioningServiceClientException if the Device Provisioning Service was not able to create or update the enrollment.
 */
IndividualEnrollment createOrUpdate(IndividualEnrollment individualEnrollment) throws ProvisioningServiceClientException {
    /* SRS_INDIVIDUAL_ENROLLMENT_MANAGER_21_005: [The createOrUpdate shall throw IllegalArgumentException if the provided individualEnrollment is null.] */
    if (individualEnrollment == null) {
        throw new IllegalArgumentException("individualEnrollment cannot be null.");
    }
    /* SRS_INDIVIDUAL_ENROLLMENT_MANAGER_21_006: [The createOrUpdate shall send a Http request for the path `enrollments/[registrationId]`.] */
    String id = individualEnrollment.getRegistrationId();
    String enrollmentPath = IndividualEnrollmentManager.getEnrollmentPath(id);
    /* SRS_INDIVIDUAL_ENROLLMENT_MANAGER_21_007: [The createOrUpdate shall send a Http request with a body with the individualEnrollment content in JSON format.] */
    String enrollmentPayload = individualEnrollment.toJson();
    /* SRS_INDIVIDUAL_ENROLLMENT_MANAGER_21_045: [If the individualEnrollment contains eTag, the createOrUpdate shall send a Http request with `If-Match` the eTag in the header.] */
    Map<String, String> headerParameters = new HashMap<>();
    if (!Tools.isNullOrEmpty(individualEnrollment.getEtag())) {
        headerParameters.put(CONDITION_KEY, individualEnrollment.getEtag());
    }
    /* SRS_INDIVIDUAL_ENROLLMENT_MANAGER_21_008: [The createOrUpdate shall send a Http request with a Http verb `PUT`.] */
    /* SRS_INDIVIDUAL_ENROLLMENT_MANAGER_21_009: [The createOrUpdate shall throw ProvisioningServiceClientTransportException if the request failed. Threw by the callee.] */
    /* SRS_INDIVIDUAL_ENROLLMENT_MANAGER_21_010: [The createOrUpdate shall throw ProvisioningServiceClientException if the Device Provisioning Service could not successfully execute the request. Threw by the callee.] */
    HttpResponse httpResponse = contractApiHttp.request(HttpMethod.PUT, enrollmentPath, headerParameters, enrollmentPayload);
    /* SRS_INDIVIDUAL_ENROLLMENT_MANAGER_21_042: [The createOrUpdate shall throw ProvisioningServiceClientServiceException if the heepResponse contains a null body.] */
    byte[] body = httpResponse.getBody();
    if (body == null) {
        throw new ProvisioningServiceClientServiceException("Http response for createOrUpdate cannot contains a null body");
    }
    /* SRS_INDIVIDUAL_ENROLLMENT_MANAGER_21_011: [The createOrUpdate shall return an IndividualEnrollment object created from the body of the response for the Http request .] */
    return new IndividualEnrollment(new String(body, StandardCharsets.UTF_8));
}
Also used : ProvisioningServiceClientServiceException(com.microsoft.azure.sdk.iot.provisioning.service.exceptions.ProvisioningServiceClientServiceException) HashMap(java.util.HashMap) HttpResponse(com.microsoft.azure.sdk.iot.deps.transport.http.HttpResponse)

Example 7 with ProvisioningServiceClientServiceException

use of com.microsoft.azure.sdk.iot.provisioning.service.exceptions.ProvisioningServiceClientServiceException in project azure-iot-sdk-java by Azure.

the class IndividualEnrollmentManager method get.

/**
 * Get individualEnrollment information.
 *
 * @see ProvisioningServiceClient#getIndividualEnrollment(String)
 *
 * @param registrationId the {@code String} that identifies the individualEnrollment. It cannot be {@code null} or empty.
 * @return An {@link IndividualEnrollment} with the enrollment information.
 * @throws IllegalArgumentException if the provided parameter is not correct.
 * @throws ProvisioningServiceClientTransportException if the SDK failed to send the request to the Device Provisioning Service.
 * @throws ProvisioningServiceClientException if the Device Provisioning Service was not able to execute the get operation.
 */
IndividualEnrollment get(String registrationId) throws ProvisioningServiceClientException {
    /* SRS_INDIVIDUAL_ENROLLMENT_MANAGER_21_020: [The get shall throw IllegalArgumentException if the provided registrationId is null or empty.] */
    if (Tools.isNullOrEmpty(registrationId)) {
        throw new IllegalArgumentException("registrationId cannot be null or empty.");
    }
    /* SRS_INDIVIDUAL_ENROLLMENT_MANAGER_21_021: [The get shall send a Http request for the path `enrollments/[registrationId]`.] */
    String enrollmentPath = IndividualEnrollmentManager.getEnrollmentPath(registrationId);
    /* SRS_INDIVIDUAL_ENROLLMENT_MANAGER_21_022: [The get shall send a Http request with a Http verb `GET`.] */
    /* SRS_INDIVIDUAL_ENROLLMENT_MANAGER_21_023: [The get shall throw ProvisioningServiceClientTransportException if the request failed. Threw by the callee.] */
    /* SRS_INDIVIDUAL_ENROLLMENT_MANAGER_21_024: [The get shall throw ProvisioningServiceClientException if the Device Provisioning Service could not successfully execute the request. Threw by the callee.] */
    HttpResponse httpResponse = contractApiHttp.request(HttpMethod.GET, enrollmentPath, null, "");
    /* SRS_INDIVIDUAL_ENROLLMENT_MANAGER_21_044: [The get shall throw ProvisioningServiceClientServiceException if the heepResponse contains a null body.] */
    byte[] body = httpResponse.getBody();
    if (body == null) {
        throw new ProvisioningServiceClientServiceException("Http response for get cannot contains a null body");
    }
    /* SRS_INDIVIDUAL_ENROLLMENT_MANAGER_21_025: [The get shall return an IndividualEnrollment object created from the body of the response for the Http request .] */
    return new IndividualEnrollment(new String(body, StandardCharsets.UTF_8));
}
Also used : ProvisioningServiceClientServiceException(com.microsoft.azure.sdk.iot.provisioning.service.exceptions.ProvisioningServiceClientServiceException) HttpResponse(com.microsoft.azure.sdk.iot.deps.transport.http.HttpResponse)

Aggregations

HttpResponse (com.microsoft.azure.sdk.iot.deps.transport.http.HttpResponse)7 ProvisioningServiceClientServiceException (com.microsoft.azure.sdk.iot.provisioning.service.exceptions.ProvisioningServiceClientServiceException)7 EnrollmentGroup (com.microsoft.azure.sdk.iot.provisioning.service.configs.EnrollmentGroup)2 HashMap (java.util.HashMap)2 AttestationMechanism (com.microsoft.azure.sdk.iot.provisioning.service.configs.AttestationMechanism)1