Search in sources :

Example 1 with HttpRequest

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

the class ContractApiHttpTest method requestThrowsOnSendHttpRequestFailed.

/* SRS_HTTP_DEVICE_REGISTRATION_CLIENT_21_015: [If the HttpRequest failed send the message, the request shall throw ProvisioningServiceClientTransportException, threw by the callee.*/
@Test(expected = ProvisioningServiceClientException.class)
public void requestThrowsOnSendHttpRequestFailed() throws ProvisioningServiceClientException, IOException {
    // arrange
    new NonStrictExpectations() {

        {
            new ProvisioningSasToken(mockedProvisioningConnectionString);
            result = mockedProvisioningSasToken;
            mockedProvisioningSasToken.toString();
            result = VALID_SASTOKEN;
            mockedProvisioningConnectionString.getHostName();
            result = VALID_HOST_NAME;
            new URL((String) any);
            result = mockedURL;
            new HttpRequest(mockedURL, HttpMethod.PUT, VALID_PAYLOAD.getBytes(StandardCharsets.UTF_8));
            result = mockedHttpRequest;
            mockedHttpRequest.send();
            result = new ProvisioningServiceClientException();
        }
    };
    ContractApiHttp contractApiHttp = ContractApiHttp.createFromConnectionString(mockedProvisioningConnectionString);
    // act
    contractApiHttp.request(HttpMethod.PUT, VALID_PATH, VALID_HEADER, VALID_PAYLOAD);
// assert
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.deps.transport.http.HttpRequest) ProvisioningSasToken(com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningSasToken) ContractApiHttp(com.microsoft.azure.sdk.iot.provisioning.service.contract.ContractApiHttp) URL(java.net.URL) ProvisioningServiceClientException(com.microsoft.azure.sdk.iot.provisioning.service.exceptions.ProvisioningServiceClientException) Test(org.junit.Test)

Example 2 with HttpRequest

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

the class ContractApiHttpTest method requestCreatesSasToken.

/* SRS_HTTP_DEVICE_REGISTRATION_CLIENT_21_005: [The request shall create a SAS token based on the connection string.*/
@Test
public void requestCreatesSasToken() throws ProvisioningServiceClientException, IOException {
    // arrange
    new Expectations() {

        {
            new ProvisioningSasToken(mockedProvisioningConnectionString);
            result = mockedProvisioningSasToken;
            mockedProvisioningSasToken.toString();
            result = VALID_SASTOKEN;
            mockedProvisioningConnectionString.getHostName();
            result = VALID_HOST_NAME;
            new URL((String) any);
            result = mockedURL;
            new HttpRequest(mockedURL, HttpMethod.PUT, VALID_PAYLOAD.getBytes(StandardCharsets.UTF_8));
            result = mockedHttpRequest;
            mockedHttpRequest.send();
            result = mockedHttpResponse;
            mockedHttpResponse.getStatus();
            result = VALID_SUCCESS_STATUS;
            mockedHttpResponse.getErrorReason();
            result = VALID_SUCCESS_MESSAGE.getBytes(StandardCharsets.UTF_8);
            mockedHttpResponse.getBody();
            result = VALID_BODY;
            mockedHttpResponse.getHeaderFields();
            result = VALID_HEADER;
            ProvisioningServiceClientExceptionManager.httpResponseVerification(VALID_SUCCESS_STATUS, VALID_SUCCESS_MESSAGE);
        }
    };
    ContractApiHttp contractApiHttp = ContractApiHttp.createFromConnectionString(mockedProvisioningConnectionString);
    requestNonStrictExpectations();
    // act
    contractApiHttp.request(HttpMethod.PUT, VALID_PATH, VALID_HEADER, VALID_PAYLOAD);
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.deps.transport.http.HttpRequest) ProvisioningSasToken(com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningSasToken) ContractApiHttp(com.microsoft.azure.sdk.iot.provisioning.service.contract.ContractApiHttp) URL(java.net.URL) Test(org.junit.Test)

Example 3 with HttpRequest

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

the class ContractApiHttpTest method requestThrowsOnHttpRequestFailed.

/* SRS_HTTP_DEVICE_REGISTRATION_CLIENT_21_011: [If the request get problem creating the HttpRequest, it shall throw ProvisioningServiceClientTransportException.*/
@Test(expected = ProvisioningServiceClientTransportException.class)
public void requestThrowsOnHttpRequestFailed() throws ProvisioningServiceClientException, IOException {
    // arrange
    new NonStrictExpectations() {

        {
            new ProvisioningSasToken(mockedProvisioningConnectionString);
            result = mockedProvisioningSasToken;
            mockedProvisioningSasToken.toString();
            result = VALID_SASTOKEN;
            mockedProvisioningConnectionString.getHostName();
            result = VALID_HOST_NAME;
            new URL((String) any);
            result = mockedURL;
            new HttpRequest(mockedURL, HttpMethod.PUT, VALID_PAYLOAD.getBytes(StandardCharsets.UTF_8));
            result = new IOException();
        }
    };
    ContractApiHttp contractApiHttp = ContractApiHttp.createFromConnectionString(mockedProvisioningConnectionString);
    // act
    contractApiHttp.request(HttpMethod.PUT, VALID_PATH, VALID_HEADER, VALID_PAYLOAD);
// assert
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.deps.transport.http.HttpRequest) ProvisioningSasToken(com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningSasToken) IOException(java.io.IOException) ContractApiHttp(com.microsoft.azure.sdk.iot.provisioning.service.contract.ContractApiHttp) URL(java.net.URL) Test(org.junit.Test)

Example 4 with HttpRequest

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

the class ContractApiHttp method request.

/**
 * This function sends a raw information to the Device Provisioning Service service using http protocol.
 * <p>
 *    The purpose of this function is be the base communication between the controllers and the
 *    Service, and should be used only if you have full understanding of the Device Provisioning Service rest APIs.
 *    We highly recommend that you uses the APis under <b>{@link ProvisioningServiceClient}</b>
 *    instead of directly access the rest API using this class.
 * </p>
 *
 * @param httpMethod is the http verb in the request (GET, POST, PUT, DELETE, PATCH).
 * @param path is the path to the resource in the service that will compose the URL.
 * @param headerParameters is a list of pairs key values that contains optional parameters in the http header.
 * @param payload is the body of the message.
 * @return the {@code HttpResponse} that contains the response of the request.
 * @throws ProvisioningServiceClientTransportException if the Service Client failed to exchange http messages with the Provisioning Service.
 * @throws ProvisioningServiceClientException if the Provisioning Service response contains an error message.
 * @throws IllegalArgumentException if the provided parameters are not correct.
 */
public synchronized HttpResponse request(HttpMethod httpMethod, String path, Map<String, String> headerParameters, String payload) throws ProvisioningServiceClientException {
    /* SRS_HTTP_DEVICE_REGISTRATION_CLIENT_21_005: [The request shall create a SAS token based on the connection string.*/
    /* SRS_HTTP_DEVICE_REGISTRATION_CLIENT_21_006: [If the request get problem to create the SAS token, it shall throw IllegalArgumentException.*/
    String sasTokenString = new ProvisioningSasToken(this.provisioningConnectionString).toString();
    /* SRS_HTTP_DEVICE_REGISTRATION_CLIENT_21_007: [The request shall create a HTTP URL based on the Device Registration path.*/
    URL url = getUrlForPath(path);
    /* SRS_HTTP_DEVICE_REGISTRATION_CLIENT_21_010: [The request shall create a new HttpRequest.*/
    HttpRequest request = createRequest(url, httpMethod, headerParameters, payload.getBytes(StandardCharsets.UTF_8), sasTokenString);
    /* SRS_HTTP_DEVICE_REGISTRATION_CLIENT_21_014: [The request shall send the request to the Device Provisioning Service service by using the HttpRequest.send().*/
    /* SRS_HTTP_DEVICE_REGISTRATION_CLIENT_21_015: [If the HttpRequest failed send the message, the request shall throw ProvisioningServiceClientTransportException, threw by the callee.*/
    HttpResponse httpResponse;
    try {
        httpResponse = request.send();
    } catch (IOException e) {
        throw new ProvisioningServiceClientTransportException(e);
    }
    /* SRS_HTTP_DEVICE_REGISTRATION_CLIENT_21_016: [If the Device Provisioning Service service respond to the HttpRequest with any error code, the request shall throw the appropriated ProvisioningServiceClientException, by calling ProvisioningServiceClientExceptionManager.responseVerification().*/
    ProvisioningServiceClientExceptionManager.httpResponseVerification(httpResponse.getStatus(), new String(httpResponse.getErrorReason(), StandardCharsets.UTF_8));
    return httpResponse;
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.deps.transport.http.HttpRequest) ProvisioningServiceClientTransportException(com.microsoft.azure.sdk.iot.provisioning.service.exceptions.ProvisioningServiceClientTransportException) ProvisioningSasToken(com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningSasToken) HttpResponse(com.microsoft.azure.sdk.iot.deps.transport.http.HttpResponse) ProvisioningConnectionString(com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningConnectionString) IOException(java.io.IOException) URL(java.net.URL)

Example 5 with HttpRequest

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

the class ContractApiHttp method createRequest.

private HttpRequest createRequest(URL url, HttpMethod method, Map<String, String> headerParameters, byte[] payload, String sasToken) throws ProvisioningServiceClientTransportException {
    /* SRS_HTTP_DEVICE_REGISTRATION_CLIENT_21_011: [If the request get problem creating the HttpRequest, it shall throw ProvisioningServiceClientTransportException.*/
    HttpRequest request;
    try {
        request = new HttpRequest(url, method, payload);
    } catch (IOException e) {
        throw new ProvisioningServiceClientTransportException(e);
    }
    /* SRS_HTTP_DEVICE_REGISTRATION_CLIENT_21_012: [The request shall fill the http header with the standard parameters.] */
    request.setReadTimeoutMillis(DEFAULT_HTTP_TIMEOUT_MS);
    request.setHeaderField(HEADER_FIELD_NAME_AUTHORIZATION, sasToken);
    request.setHeaderField(HEADER_FIELD_NAME_USER_AGENT, SDKUtils.getUserAgentString());
    request.setHeaderField(HEADER_FIELD_NAME_REQUEST_ID, HEADER_FIELD_VALUE_REQUEST_ID);
    request.setHeaderField(HEADER_FIELD_NAME_ACCEPT, HEADER_FIELD_VALUE_ACCEPT);
    request.setHeaderField(HEADER_FIELD_NAME_CONTENT_TYPE, HEADER_FIELD_VALUE_CONTENT_TYPE);
    request.setHeaderField(HEADER_FIELD_NAME_CHARSET, HEADER_FIELD_VALUE_CHARSET);
    request.setHeaderField(HEADER_FIELD_NAME_CONTENT_LENGTH, payload != null ? String.valueOf(payload.length) : "0");
    /* SRS_HTTP_DEVICE_REGISTRATION_CLIENT_21_013: [The request shall add the headerParameters to the http header, if provided.] */
    if (headerParameters != null) {
        for (Map.Entry<String, String> entry : headerParameters.entrySet()) {
            request.setHeaderField(entry.getKey(), entry.getValue());
        }
    }
    return request;
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.deps.transport.http.HttpRequest) ProvisioningServiceClientTransportException(com.microsoft.azure.sdk.iot.provisioning.service.exceptions.ProvisioningServiceClientTransportException) IOException(java.io.IOException) ProvisioningConnectionString(com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningConnectionString) Map(java.util.Map)

Aggregations

HttpRequest (com.microsoft.azure.sdk.iot.deps.transport.http.HttpRequest)27 Test (org.junit.Test)21 HttpMethod (com.microsoft.azure.sdk.iot.deps.transport.http.HttpMethod)16 URL (java.net.URL)12 IOException (java.io.IOException)11 HttpResponse (com.microsoft.azure.sdk.iot.deps.transport.http.HttpResponse)10 ProvisioningSasToken (com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningSasToken)6 HashMap (java.util.HashMap)6 ContractApiHttp (com.microsoft.azure.sdk.iot.provisioning.service.contract.ContractApiHttp)5 LinkedList (java.util.LinkedList)5 List (java.util.List)5 HttpConnection (com.microsoft.azure.sdk.iot.deps.transport.http.HttpConnection)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 Map (java.util.Map)3 DeviceRegistrationParser (com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.DeviceRegistrationParser)2 ProvisioningConnectionString (com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningConnectionString)2 ProvisioningServiceClientTransportException (com.microsoft.azure.sdk.iot.provisioning.service.exceptions.ProvisioningServiceClientTransportException)2 TpmRegistrationResultParser (com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.TpmRegistrationResultParser)1 ProvisioningServiceClientException (com.microsoft.azure.sdk.iot.provisioning.service.exceptions.ProvisioningServiceClientException)1