Search in sources :

Example 6 with IotHubServiceSasToken

use of com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken in project azure-iot-sdk-java by Azure.

the class ServiceClientTest method constructor_create_sas_token.

// Tests_SRS_SERVICE_SDK_JAVA_SERVICECLIENT_12_005: [The constructor shall create a SAS token object using the IotHubConnectionString]
// Tests_SRS_SERVICE_SDK_JAVA_SERVICECLIENT_12_006: [The constructor shall store connection string, hostname, username and sasToken]
// Tests_SRS_SERVICE_SDK_JAVA_SERVICECLIENT_12_007: [The constructor shall create a new instance of AmqpSend object]
@Test
public void constructor_create_sas_token() throws Exception {
    // Arrange
    String iotHubName = "IOTHUBNAME";
    String hostName = "HOSTNAME";
    String sharedAccessKeyName = "ACCESSKEYNAME";
    String policyName = "SharedAccessKey";
    String sharedAccessKey = "1234567890abcdefghijklmnopqrstvwxyz=";
    String connectionString = "HostName=" + hostName + "." + iotHubName + ";SharedAccessKeyName=" + sharedAccessKeyName + ";" + policyName + "=" + sharedAccessKey;
    IotHubConnectionString iotHubConnectionString = IotHubConnectionStringBuilder.createConnectionString(connectionString);
    IotHubServiceClientProtocol iotHubServiceClientProtocol = IotHubServiceClientProtocol.AMQPS;
    new Expectations() {

        {
            iotHubServiceSasToken = new IotHubServiceSasToken(withNotNull());
            amqpSend = new AmqpSend(anyString, anyString, anyString, iotHubServiceClientProtocol);
        }
    };
    // Act
    ServiceClient serviceClient = ServiceClient.createFromConnectionString(connectionString, iotHubServiceClientProtocol);
    // Assert
    assertNotEquals(hostName, Deencapsulation.getField(serviceClient, "hostName"));
    assertEquals(iotHubConnectionString.getUserString(), Deencapsulation.getField(serviceClient, "userName"));
}
Also used : Expectations(mockit.Expectations) IotHubServiceSasToken(com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken) ServiceClient(com.microsoft.azure.sdk.iot.service.ServiceClient) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) AmqpSend(com.microsoft.azure.sdk.iot.service.transport.amqps.AmqpSend) IotHubServiceClientProtocol(com.microsoft.azure.sdk.iot.service.IotHubServiceClientProtocol) Test(org.junit.Test)

Example 7 with IotHubServiceSasToken

use of com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken 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 IotHubServiceSasToken

use of com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken 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 IotHubServiceSasToken

use of com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken 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 IotHubServiceSasToken

use of com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken 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

IotHubServiceSasToken (com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken)16 HttpRequest (com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest)10 HttpResponse (com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse)10 URL (java.net.URL)9 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)6 Test (org.junit.Test)5 Expectations (mockit.Expectations)2 IotHubServiceClientProtocol (com.microsoft.azure.sdk.iot.service.IotHubServiceClientProtocol)1 ServiceClient (com.microsoft.azure.sdk.iot.service.ServiceClient)1 AmqpSend (com.microsoft.azure.sdk.iot.service.transport.amqps.AmqpSend)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 URLEncoder (java.net.URLEncoder)1 ArrayList (java.util.ArrayList)1 Mac (javax.crypto.Mac)1 SecretKeySpec (javax.crypto.spec.SecretKeySpec)1 JsonArray (javax.json.JsonArray)1 JsonObject (javax.json.JsonObject)1 JsonReader (javax.json.JsonReader)1 Base64 (org.apache.commons.codec.binary.Base64)1