Search in sources :

Example 16 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 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);
}
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 17 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 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;
}
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 18 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 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;
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest)

Example 19 with HttpRequest

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);
        }
    };
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest) HttpConnection(com.microsoft.azure.sdk.iot.service.transport.http.HttpConnection) HttpMethod(com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod) Test(org.junit.Test)

Example 20 with HttpRequest

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();
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest) HashMap(java.util.HashMap) List(java.util.List) LinkedList(java.util.LinkedList) HttpMethod(com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod) URL(java.net.URL) Test(org.junit.Test)

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