software.amazon.awssdk.http.apache.ApacheHttpClient Java Examples

The following examples show how to use software.amazon.awssdk.http.apache.ApacheHttpClient. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SignersIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 7 votes vote down vote up
@Test
public void sign_WithoutUsingSdkClient_ThroughExecutionAttributes() throws Exception {
    Aws4Signer signer = Aws4Signer.create();
    SdkHttpFullRequest httpFullRequest = generateBasicRequest();

    // sign the request
    SdkHttpFullRequest signedRequest = signer.sign(httpFullRequest, constructExecutionAttributes());

    SdkHttpClient httpClient = ApacheHttpClient.builder().build();

    HttpExecuteRequest request = HttpExecuteRequest.builder()
                                                   .request(signedRequest)
                                                   .contentStreamProvider(signedRequest.contentStreamProvider().orElse(null))
                                                   .build();

    HttpExecuteResponse response = httpClient.prepareRequest(request)
                                             .call();

    assertEquals("Non success http status code", 200, response.httpResponse().statusCode());

    String actualResult = IoUtils.toUtf8String(response.responseBody().get());
    assertEquals(getExpectedResult(), actualResult);
}
 
Example #2
Source File: SignersIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void test_SignMethod_WithModeledParam_And_WithoutUsingSdkClient() throws Exception {
    Aws4Signer signer = Aws4Signer.create();
    SdkHttpFullRequest httpFullRequest = generateBasicRequest();

    // sign the request
    SdkHttpFullRequest signedRequest = signer.sign(httpFullRequest, constructSignerParams());

    SdkHttpClient httpClient = ApacheHttpClient.builder().build();

    HttpExecuteRequest request = HttpExecuteRequest.builder()
                                                   .request(signedRequest)
                                                   .contentStreamProvider(signedRequest.contentStreamProvider().orElse(null))
                                                   .build();
    HttpExecuteResponse response = httpClient.prepareRequest(request)
                                             .call();

    assertEquals("Non success http status code", 200, response.httpResponse().statusCode());

    String actualResult = IoUtils.toUtf8String(response.responseBody().get());
    assertEquals(getExpectedResult(), actualResult);
}
 
Example #3
Source File: AwsS3V4SignerIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void test_SignMethod_WithModeledParam_And_WithoutUsingSdkClient() throws Exception {
    AwsS3V4Signer signer = AwsS3V4Signer.create();
    SdkHttpFullRequest httpFullRequest = generateBasicGetRequest();

    // sign the request
    SdkHttpFullRequest signedRequest = signer.sign(httpFullRequest, constructSignerParams());

    SdkHttpClient httpClient = ApacheHttpClient.builder().build();

    HttpExecuteResponse response = httpClient.prepareRequest(HttpExecuteRequest.builder().request(signedRequest).build())
                                             .call();

    assertEquals("Non success http status code", 200, response.httpResponse().statusCode());

    String actualResult = IoUtils.toUtf8String(response.responseBody().get());
    assertEquals(CONTENT, actualResult);
}
 
Example #4
Source File: AwsS3V4SignerIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void test_SignMethod_WithExecutionAttributes_And_WithoutUsingSdkClient() throws Exception {
    AwsS3V4Signer signer = AwsS3V4Signer.create();
    SdkHttpFullRequest httpFullRequest = generateBasicGetRequest();

    // sign the request
    SdkHttpFullRequest signedRequest = signer.sign(httpFullRequest, constructExecutionAttributes());

    SdkHttpClient httpClient = ApacheHttpClient.builder().build();

    HttpExecuteResponse response = httpClient.prepareRequest(HttpExecuteRequest.builder().request(signedRequest).build())
                                             .call();

    assertEquals("Non success http status code", 200, response.httpResponse().statusCode());

    String actualResult = IoUtils.toUtf8String(response.responseBody().get());
    assertEquals(CONTENT, actualResult);
}
 
Example #5
Source File: DependencyFactory.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * @return an instance of DynamoDbClient
 */
public static DynamoDbClient dynamoDbClient() {
    return DynamoDbClient.builder()
                   .credentialsProvider(EnvironmentVariableCredentialsProvider.create())
                   .region(Region.of(System.getenv(SdkSystemSetting.AWS_REGION.environmentVariable())))
                   .httpClientBuilder(ApacheHttpClient.builder())
                   .build();
}
 
Example #6
Source File: STSCredentialProviderV2.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public static SdkHttpClient.Builder initConnectionSettings(Configuration conf) {
  final ApacheHttpClient.Builder httpBuilder = ApacheHttpClient.builder();
  httpBuilder.maxConnections(intOption(conf, Constants.MAXIMUM_CONNECTIONS, Constants.DEFAULT_MAXIMUM_CONNECTIONS, 1));
  httpBuilder.connectionTimeout(
    Duration.ofSeconds(intOption(conf, Constants.ESTABLISH_TIMEOUT, Constants.DEFAULT_ESTABLISH_TIMEOUT, 0)));
  httpBuilder.socketTimeout(
    Duration.ofSeconds(intOption(conf, Constants.SOCKET_TIMEOUT, Constants.DEFAULT_SOCKET_TIMEOUT, 0)));
  httpBuilder.proxyConfiguration(initProxySupport(conf));

  return httpBuilder;
}
 
Example #7
Source File: AmazonHttpClientSslHandshakeTimeoutIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 60 * 1000)
public void testSslHandshakeTimeout() {
    AmazonSyncHttpClient httpClient = HttpTestUtils.testClientBuilder()
                                                   .retryPolicy(RetryPolicy.none())
                                                   .httpClient(ApacheHttpClient.builder()
                                                                           .socketTimeout(CLIENT_SOCKET_TO)
                                                                           .build())
                                                   .build();

    System.out.println("Sending request to localhost...");

    try {
        SdkHttpFullRequest request = server.configureHttpsEndpoint(SdkHttpFullRequest.builder())
                                           .method(SdkHttpMethod.GET)
                                           .build();
        httpClient.requestExecutionBuilder()
                  .request(request)
                  .originalRequest(NoopTestRequest.builder().build())
                  .executionContext(executionContext(request))
                  .execute(combinedSyncResponseHandler(null, new NullErrorResponseHandler()));
        fail("Client-side socket read timeout is expected!");

    } catch (SdkClientException e) {
        /**
         * Http client catches the SocketTimeoutException and throws a
         * ConnectTimeoutException.
         * {@link DefaultHttpClientConnectionOperator#connect(ManagedHttpClientConnection, HttpHost,
         * InetSocketAddress, int, SocketConfig, HttpContext)}
         */
        Assert.assertTrue(e.getCause() instanceof ConnectTimeoutException);

        ConnectTimeoutException cte = (ConnectTimeoutException) e.getCause();
        Assert.assertThat(cte.getMessage(), org.hamcrest.Matchers
                .containsString("Read timed out"));
    }
}
 
Example #8
Source File: ConnectionPoolMaxConnectionsIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 60 * 1000)
public void leasing_a_new_connection_fails_with_connection_pool_timeout() {

    AmazonSyncHttpClient httpClient = HttpTestUtils.testClientBuilder()
                                                   .retryPolicy(RetryPolicy.none())
                                                   .httpClient(ApacheHttpClient.builder()
                                                                               .connectionTimeout(Duration.ofMillis(100))
                                                                               .maxConnections(1)
                                                                               .build())
                                                   .build();

    SdkHttpFullRequest request = server.configureHttpEndpoint(SdkHttpFullRequest.builder())
                                       .method(SdkHttpMethod.GET)
                                       .build();

    // Block the first connection in the pool with this request.
    httpClient.requestExecutionBuilder()
              .request(request)
              .originalRequest(NoopTestRequest.builder().build())
              .executionContext(executionContext(request))
              .execute(combinedSyncResponseHandler(new EmptySdkResponseHandler(), null));

    try {
        // A new connection will be leased here which would fail in
        // ConnectionPoolTimeoutException.
        httpClient.requestExecutionBuilder()
                  .request(request)
                  .originalRequest(NoopTestRequest.builder().build())
                  .executionContext(executionContext(request))
                  .execute(combinedSyncResponseHandler(null, null));
        Assert.fail("Connection pool timeout exception is expected!");
    } catch (SdkClientException e) {
        Assert.assertTrue(e.getCause() instanceof ConnectionPoolTimeoutException);
    }
}
 
Example #9
Source File: V2DefaultClientCreationBenchmark.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Override
@Benchmark
public void createClient(Blackhole blackhole) throws Exception {
    client = DynamoDbClient.builder()
                           .endpointDiscoveryEnabled(false)
                           .httpClient(ApacheHttpClient.builder().build()).build();
    blackhole.consume(client);
}
 
Example #10
Source File: V2OptimizedClientCreationBenchmark.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Override
@Benchmark
public void createClient(Blackhole blackhole) throws Exception {
    client = DynamoDbClient.builder()
                                    .region(Region.US_WEST_2)
                                    .credentialsProvider(StaticCredentialsProvider.create(
                                        AwsBasicCredentials.create("test", "test")))
                                    .httpClient(ApacheHttpClient.builder().build())
                                    .overrideConfiguration(ClientOverrideConfiguration.builder().build())
                                    .endpointDiscoveryEnabled(false)
                                    .build();

    blackhole.consume(client);
}
 
Example #11
Source File: ApacheHttpClientBenchmark.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Setup(Level.Trial)
public void setup() throws Exception {
    mockServer = new MockServer();
    mockServer.start();
    sdkHttpClient = ApacheHttpClient.builder()
                                    .buildWithDefaults(trustAllTlsAttributeMapBuilder().build());
    client = ProtocolRestJsonClient.builder()
                                   .endpointOverride(mockServer.getHttpsUri())
                                   .httpClient(sdkHttpClient)
                                   .build();
    executorService = Executors.newFixedThreadPool(CONCURRENT_CALLS);

    client.allTypes();
}
 
Example #12
Source File: ResponseTransformerTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private ProtocolRestJsonClientBuilder testClientBuilder() {
    return ProtocolRestJsonClient.builder()
                                 .region(Region.US_WEST_1)
                                 .endpointOverride(URI.create("http://localhost:" + wireMock.port()))
                                 .credentialsProvider(() -> AwsBasicCredentials.create("akid", "skid"))
                                 .httpClientBuilder(ApacheHttpClient.builder().socketTimeout(Duration.ofSeconds(1)));
}
 
Example #13
Source File: ResponseTransformerTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void streamingAbortActuallyAborts() {
    stubForSuccess();

    ProtocolRestJsonClient client = testClientBuilder()
            .httpClientBuilder(ApacheHttpClient.builder()
                                               .connectionAcquisitionTimeout(Duration.ofSeconds(1))
                                               .maxConnections(1))
            .build();


    // Two successful requests with a max of one connection means that closing the connection worked.
    client.streamingOutputOperation(StreamingOutputOperationRequest.builder().build()).abort();
    client.streamingOutputOperation(StreamingOutputOperationRequest.builder().build()).abort();
}
 
Example #14
Source File: ResponseTransformerTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void streamingCloseActuallyCloses() throws IOException {
    stubForSuccess();

    ProtocolRestJsonClient client = testClientBuilder()
            .httpClientBuilder(ApacheHttpClient.builder()
                                               .connectionAcquisitionTimeout(Duration.ofSeconds(1))
                                               .maxConnections(1))
            .build();


    // Two successful requests with a max of one connection means that closing the connection worked.
    client.streamingOutputOperation(StreamingOutputOperationRequest.builder().build()).close();
    client.streamingOutputOperation(StreamingOutputOperationRequest.builder().build()).close();
}
 
Example #15
Source File: DependencyFactory.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * @return an instance of DynamoDbStreamsClient
 */
public static DynamoDbStreamsClient dynamoDbStreamsClient() {
    return DynamoDbStreamsClient.builder()
                   .credentialsProvider(EnvironmentVariableCredentialsProvider.create())
                   .region(Region.AP_SOUTHEAST_1)
                   .httpClientBuilder(ApacheHttpClient.builder())
                   .build();
}
 
Example #16
Source File: DependencyFactory.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * @return an instance of WafRegionalClient
 */
public static WafRegionalClient wafRegionalClient() {
    return WafRegionalClient.builder()
                   .credentialsProvider(EnvironmentVariableCredentialsProvider.create())
                   .region(Region.AP_SOUTHEAST_1)
                   .httpClientBuilder(ApacheHttpClient.builder())
                   .build();
}
 
Example #17
Source File: S3PresignerIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example #18
Source File: S3PresignerIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void putObject_PresignedHttpRequestCanBeInvokedDirectlyBySdk() throws IOException {
    String objectKey = generateRandomObjectKey();
    S3TestUtils.addCleanupTask(S3PresignerIntegrationTest.class,
                               () -> client.deleteObject(r -> r.bucket(testBucket).key(objectKey)));

    PresignedPutObjectRequest presigned =
        presigner.presignPutObject(r -> r.signatureDuration(Duration.ofMinutes(5))
                                         .putObjectRequest(por -> por.bucket(testBucket).key(objectKey)));

    assertThat(presigned.isBrowserExecutable()).isFalse();

    SdkHttpClient httpClient = ApacheHttpClient.builder().build(); // or UrlConnectionHttpClient.builder().build()

    ContentStreamProvider requestPayload = () -> new StringInputStream(testObjectContent);

    HttpExecuteRequest request = HttpExecuteRequest.builder()
                                                   .request(presigned.httpRequest())
                                                   .contentStreamProvider(requestPayload)
                                                   .build();

    HttpExecuteResponse response = httpClient.prepareRequest(request).call();

    assertThat(response.responseBody()).isPresent();
    assertThat(response.httpResponse().isSuccessful()).isTrue();
    response.responseBody().ifPresent(AbortableInputStream::abort);
    String content = client.getObjectAsBytes(r -> r.bucket(testBucket).key(objectKey)).asUtf8String();
    assertThat(content).isEqualTo(testObjectContent);
}
 
Example #19
Source File: S3PresignerIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void getObject_PresignedHttpRequestCanBeInvokedDirectlyBySdk() throws IOException {
    PresignedGetObjectRequest presigned =
        presigner.presignGetObject(r -> r.signatureDuration(Duration.ofMinutes(5))
                                         .getObjectRequest(gor -> gor.bucket(testBucket)
                                                                     .key(testGetObjectKey)
                                                                     .requestPayer(RequestPayer.REQUESTER)));

    assertThat(presigned.isBrowserExecutable()).isFalse();

    SdkHttpClient httpClient = ApacheHttpClient.builder().build(); // or UrlConnectionHttpClient.builder().build()

    ContentStreamProvider requestPayload = presigned.signedPayload()
                                                    .map(SdkBytes::asContentStreamProvider)
                                                    .orElse(null);

    HttpExecuteRequest request = HttpExecuteRequest.builder()
                                                   .request(presigned.httpRequest())
                                                   .contentStreamProvider(requestPayload)
                                                   .build();

    HttpExecuteResponse response = httpClient.prepareRequest(request).call();

    assertThat(response.responseBody()).isPresent();
    try (InputStream responseStream = response.responseBody().get()) {
        assertThat(IoUtils.toUtf8String(responseStream)).isEqualTo(testObjectContent);
    }
}
 
Example #20
Source File: S3RandomAccessFile.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private S3RandomAccessFile(String url) throws IOException {
  super(url, s3BufferSize, s3MaxReadCacheSize);

  // Region is tricky. Since we are using AWS SDK to manage connections to all object stores, we might have users
  // who use netCDF-Java and never touch AWS. If that's they case, they likely have not setup a basic credentials or
  // configuration file, and thus lack a default region. What we will do here is check to see if there is one set.
  // If, by the time we make the client, profileRegion isn't set, we will default to the AWS_GLOBAL region, which is
  // like a no-op region when it comes to S3. This will allow requests to non-AWS-S3 object stores to work, because
  // a region must be set, even if it's useless.
  Optional<Region> profileRegion = ProfileFile.defaultProfileFile().profile("default")
      .map(p -> p.properties().get(ProfileProperty.REGION)).map(Region::of);

  try {
    uri = new CdmS3Uri(url);
  } catch (URISyntaxException urie) {
    // If we are given a string that is not a valid CdmS3Uri
    // throw an IOException
    throw new IOException(urie.getCause());
  }

  Builder httpConfig = ApacheHttpClient.builder().maxConnections(maxConnections)
      .connectionTimeout(Duration.ofMillis(connectionTimeout)).socketTimeout(Duration.ofMillis(socketTimeout));

  S3ClientBuilder s3ClientBuilder = S3Client.builder().httpClientBuilder(httpConfig);

  // if we are accessing an S3 compatible service, we need to override the server endpoint
  uri.getEndpoint().ifPresent(s3ClientBuilder::endpointOverride);

  // build up a chain of credentials providers
  AwsCredentialsProviderChain.Builder cdmCredentialsProviderChainBuilder = AwsCredentialsProviderChain.builder();

  // if uri has a profile name, we need setup a credentials provider to look for potential credentials, and see if a
  // region has been set
  if (uri.getProfile().isPresent()) {
    // get the profile name
    String profileName = uri.getProfile().get();

    ProfileCredentialsProvider namedProfileCredentials =
        ProfileCredentialsProvider.builder().profileName(profileName).build();

    // add it to the chain that it is the first thing checked for credentials
    cdmCredentialsProviderChainBuilder.addCredentialsProvider(namedProfileCredentials);

    // Read the region associated with the profile, if set
    // Note: the java sdk does not do this by default
    Optional<Region> namedProfileRegion = ProfileFile.defaultProfileFile().profile(profileName)
        .map(p -> p.properties().get(ProfileProperty.REGION)).map(Region::of);
    // if the named profile has a region, update profileRegion to use it.
    if (namedProfileRegion.isPresent()) {
      profileRegion = namedProfileRegion;
    }
  }

  // Add the Default Credentials Provider Chain:
  // https://docs.aws.amazon.com/sdk-for-java/v2/developer-guide/credentials.html
  cdmCredentialsProviderChainBuilder.addCredentialsProvider(DefaultCredentialsProvider.create());

  // Add the AnonymousCredentialsProvider last
  cdmCredentialsProviderChainBuilder.addCredentialsProvider(AnonymousCredentialsProvider.create());

  // build the credentials provider that we'll use
  AwsCredentialsProviderChain cdmCredentialsProviderChain = cdmCredentialsProviderChainBuilder.build();

  // Add the credentials provider to the client builder
  s3ClientBuilder.credentialsProvider(cdmCredentialsProviderChain);

  // Set the region for the client builder (default to AWS_GLOBAL)
  s3ClientBuilder.region(profileRegion.orElse(Region.AWS_GLOBAL));

  // Build the client
  client = s3ClientBuilder.build();

  // request HEAD for the object
  HeadObjectRequest headdObjectRequest =
      HeadObjectRequest.builder().bucket(uri.getBucket()).key(uri.getKey()).build();

  objectHeadResponse = client.headObject(headdObjectRequest);
}
 
Example #21
Source File: ApacheClientConfiguration.java    From micronaut-aws with Apache License 2.0 4 votes vote down vote up
/**
 * @return The builder for {@link ApacheHttpClient}
 */
public ApacheHttpClient.Builder getBuilder() {
    return builder.proxyConfiguration(proxy.build());
}