use of com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken in project azure-iot-sdk-java by Azure.
the class RegistryManager method getDevice.
/**
* Get device data by device Id from IotHub
*
* @param deviceId The id of requested device
* @return The device object of requested device
* @throws IOException This exception is thrown if the IO operation failed
* @throws IotHubException This exception is thrown if the response verification failed
*/
public Device getDevice(String deviceId) throws IOException, IotHubException, JsonSyntaxException {
// Codes_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_12_014: [The constructor 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_015: [The function shall get the URL for the device]
URL url = iotHubConnectionString.getUrlDevice(deviceId);
// Codes_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_12_016: [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_017: [The function shall create a new HttpRequest for getting a device from IotHub]
HttpRequest request = CreateRequest(url, HttpMethod.GET, new byte[0], sasTokenString);
// Codes_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_12_018: [The function shall send the created request and get the response]
HttpResponse response = request.send();
// Codes_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_12_019: [The function shall verify the response status and throw proper Exception]
IotHubExceptionManager.httpResponseVerification(response);
// Codes_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_12_020: [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;
}
use of com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken in project azure-iot-sdk-java by Azure.
the class RegistryManager method importDevices.
/**
* Create a bulk import job.
*
* @param importBlobContainerUri URI containing SAS token to a blob container that contains registry data to sync
* @param outputBlobContainerUri URI containing SAS token to a blob container where the result of the bulk import operation will be placed
*
* @return A JobProperties object for the newly created bulk import job
*
* @throws IllegalArgumentException This exception is thrown if the importBlobContainerUri or outputBlobContainerUri 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 importDevices(String importBlobContainerUri, String outputBlobContainerUri) throws IllegalArgumentException, IOException, IotHubException, JsonSyntaxException {
// CODES_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_15_069: [The function shall throw IllegalArgumentException if any of the input parameters is null]
if (importBlobContainerUri == null || outputBlobContainerUri == null) {
throw new IllegalArgumentException("Import blob uri or output blob uri cannot be null");
}
//CODES_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_15_070: [The function shall get the URL for the bulk import job creation]
URL url = iotHubConnectionString.getUrlCreateExportImportJob();
// CODES_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_15_071: [The function shall create a new SAS token for the bulk import job]
String sasTokenString = new IotHubServiceSasToken(this.iotHubConnectionString).toString();
// CODES_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_15_072: [The function shall create a new HttpRequest for the bulk import job creation]
String jobPropertiesJson = CreateImportJobPropertiesJson(importBlobContainerUri, outputBlobContainerUri);
HttpRequest request = CreateRequest(url, HttpMethod.POST, jobPropertiesJson.getBytes(), sasTokenString);
// CODES_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_15_073: [The function shall send the created request and get the response]
HttpResponse response = request.send();
// CODES_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_15_075: [The function shall create a new JobProperties object from the response and return it]
return ProcessJobResponse(response);
}
use of com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken in project azure-iot-sdk-java by Azure.
the class DeviceOperations method request.
/**
* Send a http request to the IoTHub using the Twin/Method standard, and return its response.
*
* @param iotHubConnectionString is the connection string for the IoTHub
* @param url is the Twin URL for the device ID.
* @param method is the HTTP method (GET, POST, DELETE, PATCH, PUT).
* @param payload is the array of bytes that contains the payload.
* @param requestId is an unique number that identify the request.
* @param timeoutInMs is timeout in milliseconds.
* @return the result of the request.
* @throws IotHubException This exception is thrown if the response verification failed
* @throws IOException This exception is thrown if the IO operation failed
*/
public static HttpResponse request(IotHubConnectionString iotHubConnectionString, URL url, HttpMethod method, byte[] payload, String requestId, long timeoutInMs) throws IOException, IotHubException, IllegalArgumentException {
/* Codes_SRS_DEVICE_OPERATIONS_21_001: [The request shall throw IllegalArgumentException if the provided `iotHubConnectionString` is null.] */
if (iotHubConnectionString == null) {
throw new IllegalArgumentException("Null ConnectionString");
}
/* Codes_SRS_DEVICE_OPERATIONS_21_002: [The request shall throw IllegalArgumentException if the provided `url` is null.] */
if (url == null) {
throw new IllegalArgumentException("Null URL");
}
/* Codes_SRS_DEVICE_OPERATIONS_21_003: [The request shall throw IllegalArgumentException if the provided `method` is null.] */
if (method == null) {
throw new IllegalArgumentException("Null method");
}
/* Codes_SRS_DEVICE_OPERATIONS_21_004: [The request shall throw IllegalArgumentException if the provided `payload` is null.] */
if (payload == null) {
throw new IllegalArgumentException("Null payload");
}
/* Codes_SRS_DEVICE_OPERATIONS_21_005: [The request shall throw IllegalArgumentException if the provided `requestId` is null or empty.] */
if ((requestId == null) || requestId.isEmpty()) {
throw new IllegalArgumentException("requestId is null or empty");
}
/* Codes_SRS_DEVICE_OPERATIONS_99_018: [The request shall throw IllegalArgumentException if the provided `timeoutInMs` exceed Integer.MAX_VALUE.] */
if ((timeoutInMs + DEFAULT_HTTP_TIMEOUT_MS) > Integer.MAX_VALUE) {
throw new IllegalArgumentException("HTTP Request timeout shouldn't not exceed " + Integer.MAX_VALUE + " milliseconds");
}
/* Codes_SRS_DEVICE_OPERATIONS_21_006: [The request shall create a new SASToken with the ServiceConnect rights.] */
String sasTokenString = new IotHubServiceSasToken(iotHubConnectionString).toString();
/* Codes_SRS_DEVICE_OPERATIONS_21_007: [If the SASToken is null or empty, the request shall throw IOException.] */
if ((sasTokenString == null) || sasTokenString.isEmpty()) {
throw new IOException("Illegal sasToken null or empty");
}
/* Codes_SRS_DEVICE_OPERATIONS_21_008: [The request shall create a new HttpRequest with the provided `url`, http `method`, and `payload`.] */
HttpRequest request = new HttpRequest(url, method, payload);
/* Codes_SRS_DEVICE_OPERATIONS_21_009: [The request shall add to the HTTP header the sum of timeout and default timeout in milliseconds.] */
request.setReadTimeoutMillis((int) (timeoutInMs + DEFAULT_HTTP_TIMEOUT_MS));
/* Codes_SRS_DEVICE_OPERATIONS_21_010: [The request shall add to the HTTP header an `authorization` key with the SASToken.] */
request.setHeaderField(AUTHORIZATION, sasTokenString);
/* Codes_SRS_DEVICE_OPERATIONS_21_011: [The request shall add to the HTTP header a `Request-Id` key with a new unique string value for every request.] */
request.setHeaderField(REQUEST_ID, requestId);
/* Codes_SRS_DEVICE_OPERATIONS_21_012: [The request shall add to the HTTP header a `User-Agent` key with the client Id and service version.] */
request.setHeaderField(USER_AGENT, TransportUtils.getJavaServiceClientIdentifier() + TransportUtils.getServiceVersion());
/* Codes_SRS_DEVICE_OPERATIONS_21_013: [The request shall add to the HTTP header a `Accept` key with `application/json`.] */
request.setHeaderField(ACCEPT, ACCEPT_VALUE);
/* Codes_SRS_DEVICE_OPERATIONS_21_014: [The request shall add to the HTTP header a `Content-Type` key with `application/json; charset=utf-8`.] */
request.setHeaderField(CONTENT_TYPE, ACCEPT_VALUE + "; " + ACCEPT_CHARSET);
/* Codes_SRS_DEVICE_OPERATIONS_21_015: [The request shall send the created request and get the response.] */
HttpResponse response = request.send();
/* Codes_SRS_DEVICE_OPERATIONS_21_016: [If the resulted HttpResponseStatus represents fail, the request shall throw proper Exception by calling httpResponseVerification.] */
IotHubExceptionManager.httpResponseVerification(response);
/* Codes_SRS_DEVICE_OPERATIONS_21_017: [If the resulted status represents success, the request shall return the http response.] */
return response;
}
use of com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken in project azure-iot-sdk-java by Azure.
the class RegistryManager method getDevices.
/**
* Get list of devices
*
* @param maxCount The requested count of devices
* @return The array of requested device objects
* @throws IOException This exception is thrown if the IO operation failed
* @throws IotHubException This exception is thrown if the response verification failed
*/
public ArrayList<Device> getDevices(Integer maxCount) throws IOException, IotHubException, JsonSyntaxException {
// Codes_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_12_023: [The constructor shall throw IllegalArgumentException if the input count number is less than 1]
if (maxCount < 1) {
throw new IllegalArgumentException("maxCount cannot be less then 1");
}
// Codes_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_12_024: [The function shall get the URL for the device]
URL url = iotHubConnectionString.getUrlDeviceList(maxCount);
// Codes_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_12_025: [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_026: [The function shall create a new HttpRequest for getting a device list from IotHub]
HttpRequest request = CreateRequest(url, HttpMethod.GET, new byte[0], sasTokenString);
// Codes_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_12_027: [The function shall send the created request and get the response]
HttpResponse response = request.send();
// Codes_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_12_028: [The function shall verify the response status and throw proper Exception]
IotHubExceptionManager.httpResponseVerification(response);
// Codes_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_12_029: [The function shall create a new ArrayList<Device> object from the response and return with it]
String bodyStr = new String(response.getBody(), StandardCharsets.UTF_8);
try (JsonReader jsonReader = Json.createReader(new StringReader(bodyStr))) {
ArrayList<Device> deviceList = new ArrayList<>();
JsonArray deviceArray = jsonReader.readArray();
for (int i = 0; i < deviceArray.size(); i++) {
JsonObject jsonObject = deviceArray.getJsonObject(i);
Device iotHubDevice = gson.fromJson(jsonObject.toString(), Device.class);
deviceList.add(iotHubDevice);
}
return deviceList;
}
}
use of com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken in project azure-iot-sdk-java by Azure.
the class RegistryManager method getJob.
/**
* Get the properties of an existing job.
*
* @param jobId The id of the job to be retrieved.
*
* @return A JobProperties object for the requested job id
*
* @throws IllegalArgumentException This exception is thrown if the jobId parameter is 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 getJob(String jobId) throws IllegalArgumentException, IOException, IotHubException, JsonSyntaxException {
// CODES_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_15_077: [The function shall throw IllegalArgumentException if the input parameter is null]
if (jobId == null) {
throw new IllegalArgumentException("Job id cannot be null");
}
// CODES_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_15_078: [The function shall get the URL for the get request]
URL url = iotHubConnectionString.getUrlImportExportJob(jobId);
// CODES_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_15_079: [The function shall create a new SAS token for the get request **]
String sasTokenString = new IotHubServiceSasToken(this.iotHubConnectionString).toString();
// CODES_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_15_080: [The function shall create a new HttpRequest for getting the properties of a job]
HttpRequest request = CreateRequest(url, HttpMethod.GET, new byte[0], sasTokenString);
// CODES_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_15_081: [The function shall send the created request and get the response]
HttpResponse response = request.send();
// CODES_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_15_083: [The function shall create a new JobProperties object from the response and return it]
return ProcessJobResponse(response);
}
Aggregations