Search in sources :

Example 6 with HttpRequest

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

the class RegistryManagerTest method removeDeviceAsync_future_return_ok.

// Tests_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_12_053: [The function shall create an async wrapper around the removeDevice() function call, handle the return value or delegate exception]
@Test
public void removeDeviceAsync_future_return_ok() throws Exception {
    String connectionString = "HostName=aaa.bbb.ccc;SharedAccessKeyName=XXX;SharedAccessKey=YYY";
    String deviceId = "somedevice";
    commonExpectations(connectionString, deviceId);
    RegistryManager registryManager = RegistryManager.createFromConnectionString(connectionString);
    CompletableFuture completableFuture = registryManager.removeDeviceAsync(deviceId);
    completableFuture.get();
    new VerificationsInOrder() {

        {
            iotHubConnectionString.getUrlDevice(deviceId);
            times = 1;
            new HttpRequest(mockUrl, HttpMethod.DELETE, new byte[0]);
            times = 1;
            mockHttpRequest.setReadTimeoutMillis(anyInt);
            mockHttpRequest.setHeaderField("authorization", anyString);
            mockHttpRequest.setHeaderField("If-Match", "*");
        }
    };
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.junit.Test)

Example 7 with HttpRequest

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

the class RegistryManager method removeDevice.

/**
     * Remove device
     *
     * @param deviceId The device name to remove
     * @throws IOException This exception is thrown if the IO operation failed
     * @throws IotHubException This exception is thrown if the response verification failed
     */
public void removeDevice(String deviceId) throws IOException, IotHubException {
    // Codes_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_12_046: [The function shall throw IllegalArgumentException if the input string is null or empty]
    if (Tools.isNullOrEmpty(deviceId)) {
        throw new IllegalArgumentException("deviceId cannot be null or empty");
    }
    // Codes_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_12_047: [The function shall get the URL for the device]
    URL url = iotHubConnectionString.getUrlDevice(deviceId);
    // Codes_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_12_048: [The function shall create a new SAS token for the device]
    String sasToken = new IotHubServiceSasToken(this.iotHubConnectionString).toString();
    // Codes_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_12_049: [The function shall create a new HttpRequest for removing the device from IotHub]
    HttpRequest request = new HttpRequest(url, HttpMethod.DELETE, new byte[0]);
    request.setReadTimeoutMillis(DEFAULT_HTTP_TIMEOUT_MS);
    request.setHeaderField("authorization", sasToken);
    request.setHeaderField("If-Match", "*");
    // Codes_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_12_050: [The function shall send the created request and get the response]
    HttpResponse response = request.send();
    // Codes_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_12_051: [The function shall verify the response status and throw proper Exception]
    IotHubExceptionManager.httpResponseVerification(response);
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest) IotHubServiceSasToken(com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken) HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) URL(java.net.URL)

Example 8 with HttpRequest

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

the class RegistryManager method addDevice.

/**
     * Add device using the given Device object
     * Return with the response device object from IotHub
     *
     * @param device The device object to add
     * @return The future object for the requested operation
     * @throws IOException This exception is thrown if the IO operation failed
     * @throws IotHubException This exception is thrown if the response verification failed
     */
public Device addDevice(Device device) throws IOException, IotHubException, JsonSyntaxException {
    // Codes_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_12_004: [The constructor shall throw IllegalArgumentException if the input device is null]
    if (device == null) {
        throw new IllegalArgumentException("device cannot be null");
    }
    // Codes_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_12_005: [The function shall deserialize the given device object to Json string]
    String deviceJson = gson.toJson(device);
    // Codes_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_12_006: [The function shall get the URL for the device]
    URL url = iotHubConnectionString.getUrlDevice(device.getDeviceId());
    // Codes_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_12_007: [The function shall create a new SAS token for the device]
    String sasTokenString = new IotHubServiceSasToken(this.iotHubConnectionString).toString();
    // Codes_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_12_008: [The function shall create a new HttpRequest for adding the device to IotHub]
    HttpRequest request = CreateRequest(url, HttpMethod.PUT, deviceJson.getBytes(), sasTokenString);
    // Codes_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_12_009: [The function shall send the created request and get the response]
    HttpResponse response = request.send();
    // Codes_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_12_010: [The function shall verify the response status and throw proper Exception]
    IotHubExceptionManager.httpResponseVerification(response);
    // Codes_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_12_011: [The function shall create a new Device object from the response and return with it]
    String bodyStr = new String(response.getBody(), StandardCharsets.UTF_8);
    Device iotHubDevice = gson.fromJson(bodyStr, Device.class);
    return iotHubDevice;
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest) IotHubServiceSasToken(com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken) HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) URL(java.net.URL)

Example 9 with HttpRequest

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

the class RegistryManager method exportDevices.

/**
     * Create a bulk export job.
     *
     * @param exportBlobContainerUri URI containing SAS token to a blob container where export data will be placed
     * @param excludeKeys Whether the devices keys should be excluded from the exported data or not
     *
     * @return A JobProperties object for the newly created bulk export job
     *
     * @throws IllegalArgumentException This exception is thrown if the exportBlobContainerUri or excludeKeys parameters are null
     * @throws IOException This exception is thrown if the IO operation failed
     * @throws IotHubException This exception is thrown if the response verification failed
     */
public JobProperties exportDevices(String exportBlobContainerUri, Boolean excludeKeys) throws IllegalArgumentException, IOException, IotHubException, JsonSyntaxException {
    // if any of the input parameters is null]
    if (exportBlobContainerUri == null || excludeKeys == null) {
        throw new IllegalArgumentException("Export blob uri cannot be null");
    }
    // CODES_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_15_062: [The function shall get the URL for the bulk export job creation]
    URL url = iotHubConnectionString.getUrlCreateExportImportJob();
    // CODES_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_15_063: [The function shall create a new SAS token for the bulk export job]
    String sasTokenString = new IotHubServiceSasToken(this.iotHubConnectionString).toString();
    // CODES_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_15_064: [The function shall create a new HttpRequest for the bulk export job creation ]
    String jobPropertiesJson = CreateExportJobPropertiesJson(exportBlobContainerUri, excludeKeys);
    HttpRequest request = CreateRequest(url, HttpMethod.POST, jobPropertiesJson.getBytes(), sasTokenString);
    // CODES_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_15_065: [The function shall send the created request and get the response]
    HttpResponse response = request.send();
    // CODES_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_15_067: [The function shall create a new JobProperties object from the response and return it]
    return ProcessJobResponse(response);
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest) IotHubServiceSasToken(com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken) HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) URL(java.net.URL)

Example 10 with HttpRequest

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

the class RegistryManager method updateDevice.

/**
     * Update device with forceUpdate input parameter
     *
     * @param device The device object containing updated data
     * @param forceUpdate True if the update has to be forced regardless of the device state
     * @return The updated device object
     * @throws IOException This exception is thrown if the IO operation failed
     * @throws IotHubException This exception is thrown if the response verification failed
     */
public Device updateDevice(Device device, Boolean forceUpdate) throws IOException, IotHubException, JsonSyntaxException {
    // Codes_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_12_034: [The function shall throw IllegalArgumentException if the input device is null]
    if (device == null) {
        throw new IllegalArgumentException("device cannot be null");
    }
    // Codes_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_12_035: [The function shall set forceUpdate on the device]
    device.setForceUpdate(forceUpdate);
    // Codes_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_12_036: [The function shall get the URL for the device]
    URL url = iotHubConnectionString.getUrlDevice(device.getDeviceId());
    // Codes_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_12_037: [The function shall create a new SAS token for the device]
    String sasTokenString = new IotHubServiceSasToken(this.iotHubConnectionString).toString();
    // Codes_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_12_038: [The function shall create a new HttpRequest for updating the device on IotHub]
    HttpRequest request = CreateRequest(url, HttpMethod.PUT, gson.toJson(device).getBytes(), sasTokenString);
    request.setHeaderField("If-Match", "*");
    // Codes_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_12_039: [The function shall send the created request and get the response]
    HttpResponse response = request.send();
    // Codes_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_12_040: [The function shall verify the response status and throw proper Exception]
    IotHubExceptionManager.httpResponseVerification(response);
    // Codes_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_12_041: [The function shall create a new Device object from the response and return with it]
    String bodyStr = new String(response.getBody(), StandardCharsets.UTF_8);
    Device iotHubDevice = gson.fromJson(bodyStr, Device.class);
    return iotHubDevice;
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest) IotHubServiceSasToken(com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken) HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) URL(java.net.URL)

Aggregations

HttpRequest (com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest)28 Test (org.junit.Test)17 HttpMethod (com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod)16 HttpResponse (com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse)16 URL (java.net.URL)12 IotHubServiceSasToken (com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken)10 IOException (java.io.IOException)6 HashMap (java.util.HashMap)5 LinkedList (java.util.LinkedList)5 List (java.util.List)5 HttpConnection (com.microsoft.azure.sdk.iot.service.transport.http.HttpConnection)4 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)1 StringReader (java.io.StringReader)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 JsonArray (javax.json.JsonArray)1 JsonObject (javax.json.JsonObject)1 JsonReader (javax.json.JsonReader)1