Search in sources :

Example 1 with ProvisioningServiceClientTransportException

use of com.microsoft.azure.sdk.iot.provisioning.service.exceptions.ProvisioningServiceClientTransportException 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 2 with ProvisioningServiceClientTransportException

use of com.microsoft.azure.sdk.iot.provisioning.service.exceptions.ProvisioningServiceClientTransportException 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)2 ProvisioningConnectionString (com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningConnectionString)2 ProvisioningServiceClientTransportException (com.microsoft.azure.sdk.iot.provisioning.service.exceptions.ProvisioningServiceClientTransportException)2 IOException (java.io.IOException)2 HttpResponse (com.microsoft.azure.sdk.iot.deps.transport.http.HttpResponse)1 ProvisioningSasToken (com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningSasToken)1 URL (java.net.URL)1 Map (java.util.Map)1