Search in sources :

Example 16 with StringInputStream

use of software.amazon.awssdk.utils.StringInputStream in project aws-sdk-java-v2 by aws.

the class Crc32ValidationTest method adapt_InputStreamWithNoGzipOrCrc32_NotWrappedWhenAdapted.

@Test
public void adapt_InputStreamWithNoGzipOrCrc32_NotWrappedWhenAdapted() {
    InputStream content = new StringInputStream("content");
    SdkHttpFullResponse httpResponse = SdkHttpFullResponse.builder().statusCode(200).content(AbortableInputStream.create(content)).build();
    SdkHttpFullResponse adapted = adapt(httpResponse);
    InputStream in = adapted.content().get().delegate();
    assertThat(in).isEqualTo(content);
}
Also used : SdkHttpFullResponse(software.amazon.awssdk.http.SdkHttpFullResponse) StringInputStream(software.amazon.awssdk.utils.StringInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) Crc32ChecksumValidatingInputStream(software.amazon.awssdk.core.internal.util.Crc32ChecksumValidatingInputStream) AbortableInputStream(software.amazon.awssdk.http.AbortableInputStream) StringInputStream(software.amazon.awssdk.utils.StringInputStream) InputStream(java.io.InputStream) Test(org.junit.Test)

Example 17 with StringInputStream

use of software.amazon.awssdk.utils.StringInputStream in project aws-sdk-java-v2 by aws.

the class Crc32ValidationTest method adapt_InvalidGzipContent_ThrowsException.

@Test(expected = UncheckedIOException.class)
public void adapt_InvalidGzipContent_ThrowsException() throws UnsupportedEncodingException {
    InputStream content = new StringInputStream("this isn't GZIP");
    SdkHttpFullResponse httpResponse = SdkHttpFullResponse.builder().statusCode(200).putHeader("Content-Encoding", "gzip").content(AbortableInputStream.create(content)).build();
    SdkHttpFullResponse adapted = adapt(httpResponse);
    InputStream in = adapted.content().get().delegate();
    assertThat(in).isInstanceOf((GZIPInputStream.class));
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) SdkHttpFullResponse(software.amazon.awssdk.http.SdkHttpFullResponse) StringInputStream(software.amazon.awssdk.utils.StringInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) Crc32ChecksumValidatingInputStream(software.amazon.awssdk.core.internal.util.Crc32ChecksumValidatingInputStream) AbortableInputStream(software.amazon.awssdk.http.AbortableInputStream) StringInputStream(software.amazon.awssdk.utils.StringInputStream) InputStream(java.io.InputStream) Test(org.junit.Test)

Example 18 with StringInputStream

use of software.amazon.awssdk.utils.StringInputStream in project aws-sdk-java-v2 by aws.

the class S3MrapIntegrationTest method applyPresignedUrl.

private String applyPresignedUrl(PresignedRequest presignedRequest, String content) {
    try {
        HttpExecuteRequest.Builder builder = HttpExecuteRequest.builder().request(presignedRequest.httpRequest());
        if (!isEmpty(content)) {
            builder.contentStreamProvider(() -> new StringInputStream(content));
        }
        HttpExecuteRequest request = builder.build();
        HttpExecuteResponse response = ApacheHttpClient.create().prepareRequest(request).call();
        return response.responseBody().map(stream -> invokeSafely(() -> IoUtils.toUtf8String(stream))).orElseThrow(() -> new IOException("No input stream"));
    } catch (IOException e) {
        log.error(() -> "Error occurred ", e);
    }
    return null;
}
Also used : HttpExecuteRequest(software.amazon.awssdk.http.HttpExecuteRequest) ApacheHttpClient(software.amazon.awssdk.http.apache.ApacheHttpClient) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BucketAlreadyOwnedByYouException(software.amazon.awssdk.services.s3.model.BucketAlreadyOwnedByYouException) Context(software.amazon.awssdk.core.interceptor.Context) ListMultiRegionAccessPointsResponse(software.amazon.awssdk.services.s3control.model.ListMultiRegionAccessPointsResponse) BucketAlreadyExistsException(software.amazon.awssdk.services.s3control.model.BucketAlreadyExistsException) SignerConstant(software.amazon.awssdk.auth.signer.internal.SignerConstant) Duration(java.time.Duration) PutObjectRequest(software.amazon.awssdk.services.s3.model.PutObjectRequest) StsClient(software.amazon.awssdk.services.sts.StsClient) CreateMultiRegionAccessPointInput(software.amazon.awssdk.services.s3control.model.CreateMultiRegionAccessPointInput) PresignedGetObjectRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedGetObjectRequest) StringUtils.isEmpty(software.amazon.awssdk.utils.StringUtils.isEmpty) PresignedRequest(software.amazon.awssdk.awscore.presigner.PresignedRequest) ExecutionInterceptor(software.amazon.awssdk.core.interceptor.ExecutionInterceptor) S3SignerExecutionAttribute(software.amazon.awssdk.auth.signer.S3SignerExecutionAttribute) MultiRegionAccessPointStatus(software.amazon.awssdk.services.s3control.model.MultiRegionAccessPointStatus) StandardCharsets(java.nio.charset.StandardCharsets) Waiter(software.amazon.awssdk.core.waiters.Waiter) List(java.util.List) StringInputStream(software.amazon.awssdk.utils.StringInputStream) Optional(java.util.Optional) S3Configuration(software.amazon.awssdk.services.s3.S3Configuration) SdkRequest(software.amazon.awssdk.core.SdkRequest) Bucket(software.amazon.awssdk.services.s3.model.Bucket) NoSuchKeyException(software.amazon.awssdk.services.s3.model.NoSuchKeyException) BeforeClass(org.junit.BeforeClass) GetMultiRegionAccessPointResponse(software.amazon.awssdk.services.s3control.model.GetMultiRegionAccessPointResponse) FunctionalUtils.invokeSafely(software.amazon.awssdk.utils.FunctionalUtils.invokeSafely) ArrayList(java.util.ArrayList) S3Presigner(software.amazon.awssdk.services.s3.presigner.S3Presigner) ExecutionAttributes(software.amazon.awssdk.core.interceptor.ExecutionAttributes) SdkHttpMethod(software.amazon.awssdk.http.SdkHttpMethod) Region(software.amazon.awssdk.regions.Region) WaiterAcceptor(software.amazon.awssdk.core.waiters.WaiterAcceptor) UploadPartRequest(software.amazon.awssdk.services.s3.model.UploadPartRequest) SdkHttpRequest(software.amazon.awssdk.http.SdkHttpRequest) S3Client(software.amazon.awssdk.services.s3.S3Client) Logger(software.amazon.awssdk.utils.Logger) IOException(java.io.IOException) Test(org.junit.Test) FixedDelayBackoffStrategy(software.amazon.awssdk.core.retry.backoff.FixedDelayBackoffStrategy) RequestBody(software.amazon.awssdk.core.sync.RequestBody) CreateMultiRegionAccessPointResponse(software.amazon.awssdk.services.s3control.model.CreateMultiRegionAccessPointResponse) HttpExecuteResponse(software.amazon.awssdk.http.HttpExecuteResponse) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ClientOverrideConfiguration(software.amazon.awssdk.core.client.config.ClientOverrideConfiguration) IoUtils(software.amazon.awssdk.utils.IoUtils) HttpExecuteRequest(software.amazon.awssdk.http.HttpExecuteRequest) StringInputStream(software.amazon.awssdk.utils.StringInputStream) HttpExecuteResponse(software.amazon.awssdk.http.HttpExecuteResponse) IOException(java.io.IOException)

Example 19 with StringInputStream

use of software.amazon.awssdk.utils.StringInputStream in project aws-sdk-java-v2 by aws.

the class ProfileDisableMultiRegionProviderTest method specifiedInOverrideConfig_shouldUse.

@Test
public void specifiedInOverrideConfig_shouldUse() {
    ExecutionInterceptor interceptor = Mockito.spy(AbstractExecutionInterceptor.class);
    String profileFileContent = "[default]\n" + "s3_disable_multiregion_access_points = true\n";
    ProfileFile profileFile = ProfileFile.builder().type(ProfileFile.Type.CONFIGURATION).content(new StringInputStream(profileFileContent)).build();
    S3Client s3 = S3Client.builder().region(Region.US_WEST_2).credentialsProvider(AnonymousCredentialsProvider.create()).overrideConfiguration(c -> c.defaultProfileFile(profileFile).defaultProfileName("default").addExecutionInterceptor(interceptor).retryPolicy(r -> r.numRetries(0))).serviceConfiguration(s -> s.useArnRegionEnabled(true)).build();
    String arn = "arn:aws:s3:us-banana-46:12345567890:accesspoint:foo";
    assertThatThrownBy(() -> s3.getObject(r -> r.bucket(arn).key("bar"))).isInstanceOf(SdkException.class);
    ArgumentCaptor<Context.BeforeTransmission> context = ArgumentCaptor.forClass(Context.BeforeTransmission.class);
    Mockito.verify(interceptor).beforeTransmission(context.capture(), any());
    String host = context.getValue().httpRequest().host();
    assertThat(host).contains("us-banana-46");
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ProfileFile(software.amazon.awssdk.profiles.ProfileFile) AWS_CONFIG_FILE(software.amazon.awssdk.profiles.ProfileFileSystemSetting.AWS_CONFIG_FILE) S3Client(software.amazon.awssdk.services.s3.S3Client) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SdkException(software.amazon.awssdk.core.exception.SdkException) ExecutionInterceptor(software.amazon.awssdk.core.interceptor.ExecutionInterceptor) Test(org.junit.jupiter.api.Test) Context(software.amazon.awssdk.core.interceptor.Context) Mockito(org.mockito.Mockito) AfterEach(org.junit.jupiter.api.AfterEach) StringInputStream(software.amazon.awssdk.utils.StringInputStream) ArgumentCaptor(org.mockito.ArgumentCaptor) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Optional(java.util.Optional) AnonymousCredentialsProvider(software.amazon.awssdk.auth.credentials.AnonymousCredentialsProvider) Region(software.amazon.awssdk.regions.Region) TRUE(java.lang.Boolean.TRUE) Context(software.amazon.awssdk.core.interceptor.Context) StringInputStream(software.amazon.awssdk.utils.StringInputStream) ExecutionInterceptor(software.amazon.awssdk.core.interceptor.ExecutionInterceptor) S3Client(software.amazon.awssdk.services.s3.S3Client) ProfileFile(software.amazon.awssdk.profiles.ProfileFile) Test(org.junit.jupiter.api.Test)

Example 20 with StringInputStream

use of software.amazon.awssdk.utils.StringInputStream in project aws-lambda-powertools-java by awslabs.

the class CloudFormationResponse method send.

/**
 * Forwards a response containing a custom payload to the target resource specified by the event. The payload is
 * formed from the event, context, and response data.
 *
 * @param event        custom CF resource event. Cannot be null.
 * @param context      used to specify when the function and any callbacks have completed execution, or to
 *                     access information from within the Lambda execution environment. Cannot be null.
 * @param responseData response to send, e.g. a list of name-value pairs. If null, an empty success is assumed.
 * @return the response object
 * @throws IOException                     when unable to generate or send the request
 * @throws CustomResourceResponseException when unable to serialize the response payload
 */
public HttpExecuteResponse send(CloudFormationCustomResourceEvent event, Context context, Response responseData) throws IOException, CustomResourceResponseException {
    // no need to explicitly close in-memory stream
    StringInputStream stream = responseBodyStream(event, context, responseData);
    URI uri = URI.create(event.getResponseUrl());
    SdkHttpRequest request = SdkHttpRequest.builder().uri(uri).method(SdkHttpMethod.PUT).headers(headers(stream.available())).build();
    HttpExecuteRequest httpExecuteRequest = HttpExecuteRequest.builder().request(request).contentStreamProvider(() -> stream).build();
    return client.prepareRequest(httpExecuteRequest).call();
}
Also used : HttpExecuteRequest(software.amazon.awssdk.http.HttpExecuteRequest) StringInputStream(software.amazon.awssdk.utils.StringInputStream) SdkHttpRequest(software.amazon.awssdk.http.SdkHttpRequest) URI(java.net.URI)

Aggregations

StringInputStream (software.amazon.awssdk.utils.StringInputStream)47 Test (org.junit.Test)20 Test (org.junit.jupiter.api.Test)19 ProfileFile (software.amazon.awssdk.profiles.ProfileFile)19 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)10 ProfileCredentialsUtils (software.amazon.awssdk.auth.credentials.internal.ProfileCredentialsUtils)9 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)7 AbortableInputStream (software.amazon.awssdk.http.AbortableInputStream)7 Region (software.amazon.awssdk.regions.Region)7 IOException (java.io.IOException)6 InputStream (java.io.InputStream)6 Optional (java.util.Optional)6 ExecutionInterceptor (software.amazon.awssdk.core.interceptor.ExecutionInterceptor)6 URI (java.net.URI)5 Duration (java.time.Duration)5 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)5 SdkHttpRequest (software.amazon.awssdk.http.SdkHttpRequest)5 SdkHttpClient (software.amazon.awssdk.http.SdkHttpClient)4 SdkAutoCloseable (software.amazon.awssdk.utils.SdkAutoCloseable)4 ArrayList (java.util.ArrayList)3