Search in sources :

Example 26 with HttpExecuteRequest

use of software.amazon.awssdk.http.HttpExecuteRequest in project aws-greengrass-nucleus by aws-greengrass.

the class DeviceProvisioningHelper method downloadFileFromURL.

/*
     * Download content from a URL to a local file.
     */
@SuppressWarnings("PMD.AvoidFileStream")
private void downloadFileFromURL(String url, File f) throws IOException {
    SdkHttpFullRequest request = SdkHttpFullRequest.builder().uri(URI.create(url)).method(SdkHttpMethod.GET).build();
    HttpExecuteRequest executeRequest = HttpExecuteRequest.builder().request(request).build();
    try (SdkHttpClient client = getSdkHttpClient()) {
        HttpExecuteResponse executeResponse = client.prepareRequest(executeRequest).call();
        int responseCode = executeResponse.httpResponse().statusCode();
        if (responseCode != HttpURLConnection.HTTP_OK) {
            throw new IOException("Received invalid response code: " + responseCode);
        }
        try (InputStream inputStream = executeResponse.responseBody().get();
            OutputStream outputStream = Files.newOutputStream(f.toPath(), StandardOpenOption.CREATE, StandardOpenOption.APPEND)) {
            IoUtils.copy(inputStream, outputStream);
        }
    }
}
Also used : HttpExecuteRequest(software.amazon.awssdk.http.HttpExecuteRequest) HttpExecuteResponse(software.amazon.awssdk.http.HttpExecuteResponse) SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) InputStream(java.io.InputStream) SdkHttpClient(software.amazon.awssdk.http.SdkHttpClient) OutputStream(java.io.OutputStream) IOException(java.io.IOException)

Aggregations

HttpExecuteRequest (software.amazon.awssdk.http.HttpExecuteRequest)26 HttpExecuteResponse (software.amazon.awssdk.http.HttpExecuteResponse)11 SdkHttpClient (software.amazon.awssdk.http.SdkHttpClient)11 Test (org.junit.Test)10 IOException (java.io.IOException)8 InputStream (java.io.InputStream)8 SdkHttpRequest (software.amazon.awssdk.http.SdkHttpRequest)7 StringInputStream (software.amazon.awssdk.utils.StringInputStream)7 AbortableInputStream (software.amazon.awssdk.http.AbortableInputStream)5 Duration (java.time.Duration)4 Optional (java.util.Optional)4 SdkHttpFullRequest (software.amazon.awssdk.http.SdkHttpFullRequest)4 IoUtils (software.amazon.awssdk.utils.IoUtils)4 HttpURLConnection (java.net.HttpURLConnection)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 SdkClientConfiguration (software.amazon.awssdk.core.client.config.SdkClientConfiguration)3 AmazonSyncHttpClient (software.amazon.awssdk.core.internal.http.AmazonSyncHttpClient)3 ContentStreamProvider (software.amazon.awssdk.http.ContentStreamProvider)3 MetricCollector (software.amazon.awssdk.metrics.MetricCollector)3 PackageDownloadException (com.aws.greengrass.componentmanager.exceptions.PackageDownloadException)2