Search in sources :

Example 21 with HttpExecuteRequest

use of software.amazon.awssdk.http.HttpExecuteRequest in project aws-sdk-java-v2 by aws.

the class S3PresignerIntegrationTest method execute.

private HttpExecuteResponse execute(PresignedRequest presigned, String payload) throws IOException {
    SdkHttpClient httpClient = ApacheHttpClient.builder().build();
    ContentStreamProvider requestPayload = payload == null ? null : () -> new StringInputStream(payload);
    HttpExecuteRequest request = HttpExecuteRequest.builder().request(presigned.httpRequest()).contentStreamProvider(requestPayload).build();
    return httpClient.prepareRequest(request).call();
}
Also used : HttpExecuteRequest(software.amazon.awssdk.http.HttpExecuteRequest) StringInputStream(software.amazon.awssdk.utils.StringInputStream) SdkHttpClient(software.amazon.awssdk.http.SdkHttpClient) ContentStreamProvider(software.amazon.awssdk.http.ContentStreamProvider)

Example 22 with HttpExecuteRequest

use of software.amazon.awssdk.http.HttpExecuteRequest in project aws-sdk-java-v2 by aws.

the class ApacheMetricsTest method makeRequestWithMetrics.

private HttpExecuteResponse makeRequestWithMetrics(SdkHttpClient httpClient, MetricCollector metricCollector) throws IOException {
    SdkHttpRequest httpRequest = SdkHttpFullRequest.builder().method(SdkHttpMethod.GET).protocol("http").host("localhost:" + wireMockServer.port()).build();
    HttpExecuteRequest request = HttpExecuteRequest.builder().request(httpRequest).metricCollector(metricCollector).build();
    return httpClient.prepareRequest(request).call();
}
Also used : HttpExecuteRequest(software.amazon.awssdk.http.HttpExecuteRequest) SdkHttpRequest(software.amazon.awssdk.http.SdkHttpRequest)

Example 23 with HttpExecuteRequest

use of software.amazon.awssdk.http.HttpExecuteRequest in project aws-sdk-java-v2 by aws.

the class ApacheHttpRequestFactoryTest method defaultHttpPortsAreNotInDefaultHostHeader.

@Test
public void defaultHttpPortsAreNotInDefaultHostHeader() {
    SdkHttpRequest sdkRequest = SdkHttpRequest.builder().uri(URI.create("http://localhost:80/")).method(SdkHttpMethod.HEAD).build();
    HttpExecuteRequest request = HttpExecuteRequest.builder().request(sdkRequest).build();
    HttpRequestBase result = instance.create(request, requestConfig);
    Header[] hostHeaders = result.getHeaders(HttpHeaders.HOST);
    assertNotNull(hostHeaders);
    assertEquals(1, hostHeaders.length);
    assertEquals("localhost", hostHeaders[0].getValue());
    sdkRequest = SdkHttpRequest.builder().uri(URI.create("https://localhost:443/")).method(SdkHttpMethod.HEAD).build();
    request = HttpExecuteRequest.builder().request(sdkRequest).build();
    result = instance.create(request, requestConfig);
    hostHeaders = result.getHeaders(HttpHeaders.HOST);
    assertNotNull(hostHeaders);
    assertEquals(1, hostHeaders.length);
    assertEquals("localhost", hostHeaders[0].getValue());
}
Also used : HttpExecuteRequest(software.amazon.awssdk.http.HttpExecuteRequest) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) SdkHttpRequest(software.amazon.awssdk.http.SdkHttpRequest) Header(org.apache.http.Header) Test(org.junit.jupiter.api.Test)

Example 24 with HttpExecuteRequest

use of software.amazon.awssdk.http.HttpExecuteRequest in project aws-sdk-java-v2 by aws.

the class UrlConnectionHttpClient method createAndConfigureConnection.

private HttpURLConnection createAndConfigureConnection(HttpExecuteRequest request) {
    HttpURLConnection connection = connectionFactory.createConnection(request.httpRequest().getUri());
    request.httpRequest().headers().forEach((key, values) -> values.forEach(value -> connection.setRequestProperty(key, value)));
    invokeSafely(() -> connection.setRequestMethod(request.httpRequest().method().name()));
    if (request.contentStreamProvider().isPresent()) {
        connection.setDoOutput(true);
    }
    // Disable following redirects since it breaks SDK error handling and matches Apache.
    // See: https://github.com/aws/aws-sdk-java-v2/issues/975
    connection.setInstanceFollowRedirects(false);
    request.httpRequest().firstMatchingHeader(CONTENT_LENGTH).map(Long::parseLong).ifPresent(connection::setFixedLengthStreamingMode);
    return connection;
}
Also used : HttpURLConnection(java.net.HttpURLConnection) X509Certificate(java.security.cert.X509Certificate) SSLContext(javax.net.ssl.SSLContext) SdkPublicApi(software.amazon.awssdk.annotations.SdkPublicApi) TrustManager(javax.net.ssl.TrustManager) HttpStatusFamily(software.amazon.awssdk.http.HttpStatusFamily) TlsKeyManagersProvider(software.amazon.awssdk.http.TlsKeyManagersProvider) AttributeMap(software.amazon.awssdk.utils.AttributeMap) Supplier(java.util.function.Supplier) FunctionalUtils.invokeSafely(software.amazon.awssdk.utils.FunctionalUtils.invokeSafely) SSLSession(javax.net.ssl.SSLSession) NumericUtils.saturatedCast(software.amazon.awssdk.utils.NumericUtils.saturatedCast) Duration(java.time.Duration) Map(java.util.Map) URI(java.net.URI) HostnameVerifier(javax.net.ssl.HostnameVerifier) TlsTrustManagersProvider(software.amazon.awssdk.http.TlsTrustManagersProvider) OutputStream(java.io.OutputStream) CONTENT_LENGTH(software.amazon.awssdk.http.Header.CONTENT_LENGTH) SdkHttpResponse(software.amazon.awssdk.http.SdkHttpResponse) Validate(software.amazon.awssdk.utils.Validate) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Logger(software.amazon.awssdk.utils.Logger) ContentStreamProvider(software.amazon.awssdk.http.ContentStreamProvider) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) SdkHttpClient(software.amazon.awssdk.http.SdkHttpClient) SdkHttpConfigurationOption(software.amazon.awssdk.http.SdkHttpConfigurationOption) Collectors(java.util.stream.Collectors) ExecutableHttpRequest(software.amazon.awssdk.http.ExecutableHttpRequest) KeyManager(javax.net.ssl.KeyManager) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) CLIENT_ERROR(software.amazon.awssdk.http.HttpStatusFamily.CLIENT_ERROR) UncheckedIOException(java.io.UncheckedIOException) Objects(java.util.Objects) AbortableInputStream(software.amazon.awssdk.http.AbortableInputStream) List(java.util.List) X509TrustManager(javax.net.ssl.X509TrustManager) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Optional(java.util.Optional) HttpExecuteResponse(software.amazon.awssdk.http.HttpExecuteResponse) SERVER_ERROR(software.amazon.awssdk.http.HttpStatusFamily.SERVER_ERROR) IoUtils(software.amazon.awssdk.utils.IoUtils) InputStream(java.io.InputStream) HttpExecuteRequest(software.amazon.awssdk.http.HttpExecuteRequest) HttpURLConnection(java.net.HttpURLConnection)

Example 25 with HttpExecuteRequest

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

the class GreengrassRepositoryDownloader method getDownloadSizeWithoutRetry.

private Long getDownloadSizeWithoutRetry() throws InterruptedException, PackageDownloadException, IOException {
    String url = getArtifactDownloadURL(identifier, artifact.getArtifactUri().getSchemeSpecificPart());
    try (SdkHttpClient client = getSdkHttpClient()) {
        HttpExecuteRequest executeRequest = HttpExecuteRequest.builder().request(SdkHttpFullRequest.builder().uri(URI.create(url)).method(SdkHttpMethod.GET).build()).build();
        HttpExecuteResponse executeResponse = client.prepareRequest(executeRequest).call();
        int responseCode = executeResponse.httpResponse().statusCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            long length = getContentLengthLong(executeResponse.httpResponse());
            if (length == -1) {
                throw new PackageDownloadException(getErrorString("Failed to get download size"));
            }
            return length;
        } else {
            throw new PackageDownloadException(getErrorString("Failed to get download size. HTTP response: " + responseCode));
        }
    }
}
Also used : HttpExecuteRequest(software.amazon.awssdk.http.HttpExecuteRequest) HttpExecuteResponse(software.amazon.awssdk.http.HttpExecuteResponse) SdkHttpClient(software.amazon.awssdk.http.SdkHttpClient) PackageDownloadException(com.aws.greengrass.componentmanager.exceptions.PackageDownloadException)

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