Search in sources :

Example 16 with HttpResponse

use of com.microsoft.azure.sdk.iot.deps.transport.http.HttpResponse in project azure-iot-sdk-java by Azure.

the class HttpsResponseTest method getHeaderFieldRejectsInvalidFieldName.

// Tests_SRS_HTTPSRESPONSE_25_006: [If a value could not be found for the given header field name, the function shall throw an IllegalArgumentException.]
// Assert
@Test(expected = IllegalArgumentException.class)
public void getHeaderFieldRejectsInvalidFieldName() throws IllegalArgumentException {
    // Arrange
    final int status = 200;
    final byte[] body = { 1 };
    final byte[] errorReason = {};
    final Map<String, List<String>> headerFields = new HashMap<>();
    final String field = "test-field";
    // Act
    HttpResponse response = new HttpResponse(status, body, headerFields, errorReason);
    response.getHeaderField(field);
}
Also used : HashMap(java.util.HashMap) HttpResponse(com.microsoft.azure.sdk.iot.deps.transport.http.HttpResponse) List(java.util.List) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 17 with HttpResponse

use of com.microsoft.azure.sdk.iot.deps.transport.http.HttpResponse in project azure-iot-sdk-java by Azure.

the class RegistrationStatusManager method get.

/**
 * Get device registration status information.
 *
 * @see ProvisioningServiceClient#getDeviceRegistrationState(String)
 *
 * @param id the {@code String} that identifies the registration status. It cannot be {@code null} or empty.
 * @return An {@link DeviceRegistrationState} with the registration status 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 bulk operation.
 */
public DeviceRegistrationState get(String id) throws ProvisioningServiceClientException {
    /* SRS_REGISTRATION_STATUS_MANAGER_21_005: [The get shall throw IllegalArgumentException if the provided id is null or empty.] */
    if (Tools.isNullOrEmpty(id)) {
        throw new IllegalArgumentException("Id cannot be null or empty.");
    }
    /* SRS_REGISTRATION_STATUS_MANAGER_21_006: [The get shall send a Http request for the path `registrations/[id]`.] */
    String enrollmentPath = RegistrationStatusManager.getDeviceRegistrationStatePath(id);
    /* SRS_REGISTRATION_STATUS_MANAGER_21_007: [The get shall send a Http request with a Http verb `GET`.] */
    /* SRS_REGISTRATION_STATUS_MANAGER_21_008: [The get shall throw ProvisioningServiceClientTransportException if the request failed. Threw by the callee.] */
    /* SRS_REGISTRATION_STATUS_MANAGER_21_009: [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_REGISTRATION_STATUS_MANAGER_21_028: [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_REGISTRATION_STATUS_MANAGER_21_010: [The get shall return a DeviceRegistrationState object created from the body of the response for the Http request .] */
    return new DeviceRegistrationState(new String(body, StandardCharsets.UTF_8));
}
Also used : HttpResponse(com.microsoft.azure.sdk.iot.deps.transport.http.HttpResponse) DeviceRegistrationState(com.microsoft.azure.sdk.iot.provisioning.service.configs.DeviceRegistrationState)

Example 18 with HttpResponse

use of com.microsoft.azure.sdk.iot.deps.transport.http.HttpResponse 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 19 with HttpResponse

use of com.microsoft.azure.sdk.iot.deps.transport.http.HttpResponse 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)

Example 20 with HttpResponse

use of com.microsoft.azure.sdk.iot.deps.transport.http.HttpResponse in project azure-iot-sdk-java by Azure.

the class HttpsRequestTest method sendReturnsHeaderFieldsOnBadStatusException.

// Tests_SRS_HTTPSREQUEST_25_008: [If an I/O exception occurs because of a bad response status code, the function shall attempt to flush or read the error stream so that the underlying HTTPS connection can be reused.]
@Test
public void sendReturnsHeaderFieldsOnBadStatusException(@Mocked final HttpConnection mockConn, @Mocked final URL mockUrl) throws IOException {
    // Arrange
    final Map<String, List<String>> headerFields = new HashMap<>();
    final String field = "test-field";
    final List<String> values = new LinkedList<>();
    final String value = "test-value0";
    values.add(value);
    headerFields.put(field, values);
    final HttpMethod httpsMethod = HttpMethod.POST;
    final byte[] body = new byte[0];
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "http";
            mockConn.connect();
            result = new IOException();
            mockConn.getResponseHeaders();
            result = headerFields;
        }
    };
    HttpRequest request = new HttpRequest(mockUrl, httpsMethod, body);
    // Act
    HttpResponse response = request.send();
    String testValues = response.getHeaderField(field);
    // Assert
    assertThat(testValues, is(value));
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.deps.transport.http.HttpRequest) HashMap(java.util.HashMap) HttpResponse(com.microsoft.azure.sdk.iot.deps.transport.http.HttpResponse) List(java.util.List) LinkedList(java.util.LinkedList) IOException(java.io.IOException) LinkedList(java.util.LinkedList) HttpMethod(com.microsoft.azure.sdk.iot.deps.transport.http.HttpMethod) Test(org.junit.Test)

Aggregations

HttpResponse (com.microsoft.azure.sdk.iot.deps.transport.http.HttpResponse)26 Test (org.junit.Test)12 HttpRequest (com.microsoft.azure.sdk.iot.deps.transport.http.HttpRequest)10 HashMap (java.util.HashMap)10 LinkedList (java.util.LinkedList)8 List (java.util.List)8 ProvisioningServiceClientServiceException (com.microsoft.azure.sdk.iot.provisioning.service.exceptions.ProvisioningServiceClientServiceException)7 IOException (java.io.IOException)7 HttpMethod (com.microsoft.azure.sdk.iot.deps.transport.http.HttpMethod)6 URL (java.net.URL)4 UrlPathBuilder (com.microsoft.azure.sdk.iot.provisioning.device.internal.contract.UrlPathBuilder)3 ResponseData (com.microsoft.azure.sdk.iot.provisioning.device.internal.task.ResponseData)3 DeviceRegistrationParser (com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.DeviceRegistrationParser)2 EnrollmentGroup (com.microsoft.azure.sdk.iot.provisioning.service.configs.EnrollmentGroup)2 TpmRegistrationResultParser (com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.TpmRegistrationResultParser)1 ProvisioningConnectionString (com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningConnectionString)1 ProvisioningSasToken (com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningSasToken)1 AttestationMechanism (com.microsoft.azure.sdk.iot.provisioning.service.configs.AttestationMechanism)1 DeviceRegistrationState (com.microsoft.azure.sdk.iot.provisioning.service.configs.DeviceRegistrationState)1 QueryResult (com.microsoft.azure.sdk.iot.provisioning.service.configs.QueryResult)1