use of com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest 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);
}
use of com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest in project azure-iot-sdk-java by Azure.
the class RegistryManager method getStatistics.
/**
* Get device statistics
*
* @return RegistryStatistics object containing the requested data
* @throws IOException This exception is thrown if the IO operation failed
* @throws IotHubException This exception is thrown if the response verification failed
*/
public RegistryStatistics getStatistics() throws IOException, IotHubException, JsonSyntaxException {
// Codes_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_12_054: [The function shall get the URL for the device]
URL url = iotHubConnectionString.getUrlDeviceStatistics();
// Codes_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_12_055: [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_056: [The function shall create a new HttpRequest for getting statistics a device from IotHub]
HttpRequest request = CreateRequest(url, HttpMethod.GET, new byte[0], sasTokenString);
// Codes_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_12_057: [The function shall send the created request and get the response]
HttpResponse response = request.send();
// Codes_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_12_058: [The function shall verify the response status and throw proper Exception]
IotHubExceptionManager.httpResponseVerification(response);
// Codes_SRS_SERVICE_SDK_JAVA_REGISTRYMANAGER_12_059: [The function shall create a new RegistryStatistics object from the response and return with it]
String bodyStr = new String(response.getBody(), StandardCharsets.UTF_8);
RegistryStatistics registryStatistics = gson.fromJson(bodyStr, RegistryStatistics.class);
return registryStatistics;
}
use of com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest in project azure-iot-sdk-java by Azure.
the class RegistryManager method CreateRequest.
private HttpRequest CreateRequest(URL url, HttpMethod method, byte[] payload, String sasToken) throws IOException {
HttpRequest request = new HttpRequest(url, method, payload);
request.setReadTimeoutMillis(DEFAULT_HTTP_TIMEOUT_MS);
request.setHeaderField("authorization", sasToken);
request.setHeaderField("Request-Id", "1001");
request.setHeaderField("Accept", "application/json");
request.setHeaderField("Content-Type", "application/json");
request.setHeaderField("charset", "utf-8");
return request;
}
use of com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest in project azure-iot-sdk-java by Azure.
the class HttpsRequestTest method constructorSetsHttpsMethodCorrectly.
// Tests_SRS_SERVICE_SDK_JAVA_HTTPSREQUEST_12_003: [The function shall use the given HTTPS method (i.e. GET) as the request method.]
@Test
public void constructorSetsHttpsMethodCorrectly(@Mocked final HttpConnection mockConn) throws IOException {
// Arrange
final HttpMethod httpsMethod = HttpMethod.GET;
final byte[] body = new byte[0];
new NonStrictExpectations() {
{
mockUrl.getProtocol();
result = "http";
}
};
// Act
new HttpRequest(mockUrl, httpsMethod, body);
// Assert
new Verifications() {
{
new HttpConnection((URL) any, httpsMethod);
}
};
}
use of com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest in project azure-iot-sdk-java by Azure.
the class HttpsRequestTest method sendWritesBodyToOutputStream.
// Tests_SRS_SERVICE_SDK_JAVA_HTTPSREQUEST_12_005: [The function shall send an HTTPS request as formatted in the constructor.]
@Test
public void sendWritesBodyToOutputStream() throws IOException {
// Arrange
final HttpMethod httpsMethod = HttpMethod.POST;
final byte[] expectedBody = { 1, 2, 3 };
new MockUp<HttpConnection>() {
byte[] testBody;
@Mock
public void $init(URL url, HttpMethod method) {
}
@Mock
public void connect() throws IOException {
assertThat(testBody, is(expectedBody));
}
@Mock
public void writeOutput(byte[] body) {
this.testBody = body;
}
// every method that is used must be manually mocked.
@Mock
public void setRequestHeader(String field, String value) {
}
@Mock
public void setRequestMethod(HttpMethod method) {
}
@Mock
public byte[] readInput() throws IOException {
return new byte[0];
}
@Mock
public byte[] readError() throws IOException {
return new byte[0];
}
@Mock
public int getResponseStatus() throws IOException {
return 0;
}
@Mock
public Map<String, List<String>> getResponseHeaders() throws IOException {
return new HashMap<>();
}
};
// Assert
new NonStrictExpectations() {
{
mockUrl.getProtocol();
result = "http";
}
};
// Act
HttpRequest request = new HttpRequest(mockUrl, httpsMethod, expectedBody);
request.send();
}
Aggregations