Search in sources :

Example 26 with StringInputStream

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

the class Crc32ValidationTest method adapt_InputStreamWithCrc32Header_WrappedWithValidatingStream.

@Test
public void adapt_InputStreamWithCrc32Header_WrappedWithValidatingStream() throws UnsupportedEncodingException {
    InputStream content = new StringInputStream("content");
    SdkHttpFullResponse httpResponse = SdkHttpFullResponse.builder().statusCode(200).putHeader("x-amz-crc32", "1234").content(AbortableInputStream.create(content)).build();
    SdkHttpFullResponse adapted = adapt(httpResponse);
    InputStream in = adapted.content().get().delegate();
    assertThat(in).isInstanceOf((Crc32ChecksumValidatingInputStream.class));
}
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) Crc32ChecksumValidatingInputStream(software.amazon.awssdk.core.internal.util.Crc32ChecksumValidatingInputStream) Test(org.junit.Test)

Example 27 with StringInputStream

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

the class SyncChecksumValidationInterceptorTest method afterUnmarshalling_putObjectRequest_shouldValidateChecksum_throwExceptionIfInvalid.

@Test
public void afterUnmarshalling_putObjectRequest_shouldValidateChecksum_throwExceptionIfInvalid() {
    SdkHttpResponse sdkHttpResponse = getSdkHttpResponseWithChecksumHeader();
    PutObjectResponse response = PutObjectResponse.builder().eTag(INVALID_CHECKSUM).build();
    PutObjectRequest putObjectRequest = PutObjectRequest.builder().build();
    SdkHttpRequest sdkHttpRequest = SdkHttpFullRequest.builder().uri(URI.create("http://localhost:8080")).method(SdkHttpMethod.PUT).contentStreamProvider(() -> new StringInputStream("Test")).build();
    Context.AfterUnmarshalling afterUnmarshallingContext = InterceptorContext.builder().request(putObjectRequest).httpRequest(sdkHttpRequest).response(response).httpResponse(sdkHttpResponse).requestBody(RequestBody.fromString("Test")).build();
    ExecutionAttributes attributes = getExecutionAttributesWithChecksum();
    interceptor.modifyHttpContent(afterUnmarshallingContext, attributes);
    assertThatThrownBy(() -> interceptor.afterUnmarshalling(afterUnmarshallingContext, attributes)).hasMessageContaining("Data read has a different checksum than expected.");
}
Also used : InterceptorContext(software.amazon.awssdk.core.interceptor.InterceptorContext) Context(software.amazon.awssdk.core.interceptor.Context) StringInputStream(software.amazon.awssdk.utils.StringInputStream) SdkHttpRequest(software.amazon.awssdk.http.SdkHttpRequest) ExecutionAttributes(software.amazon.awssdk.core.interceptor.ExecutionAttributes) PutObjectResponse(software.amazon.awssdk.services.s3.model.PutObjectResponse) SdkHttpResponse(software.amazon.awssdk.http.SdkHttpResponse) PutObjectRequest(software.amazon.awssdk.services.s3.model.PutObjectRequest) Test(org.junit.jupiter.api.Test)

Example 28 with StringInputStream

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

the class SyncChecksumValidationInterceptorTest method checksumCalculatingStreamProvider_shouldReturnNewStreamResetChecksum.

@Test
public void checksumCalculatingStreamProvider_shouldReturnNewStreamResetChecksum() throws IOException {
    List<CloseAwareStream> closeAwareStreams = new ArrayList<>();
    ContentStreamProvider underlyingStreamProvider = () -> {
        CloseAwareStream stream = new CloseAwareStream(new StringInputStream("helloWorld"));
        closeAwareStreams.add(stream);
        return stream;
    };
    SdkChecksum checksum = new Md5Checksum();
    ChecksumCalculatingStreamProvider checksumCalculatingStreamProvider = new ChecksumCalculatingStreamProvider(underlyingStreamProvider, checksum);
    ChecksumCalculatingInputStream currentStream = (ChecksumCalculatingInputStream) checksumCalculatingStreamProvider.newStream();
    IoUtils.drainInputStream(currentStream);
    byte[] checksumBytes = currentStream.getChecksumBytes();
    ChecksumCalculatingInputStream newStream = (ChecksumCalculatingInputStream) checksumCalculatingStreamProvider.newStream();
    assertThat(closeAwareStreams.get(0).isClosed).isTrue();
    IoUtils.drainInputStream(newStream);
    byte[] newStreamChecksumBytes = newStream.getChecksumBytes();
    assertThat(getChecksum(checksumBytes)).isEqualTo(getChecksum(newStreamChecksumBytes));
    newStream.close();
}
Also used : StringInputStream(software.amazon.awssdk.utils.StringInputStream) ArrayList(java.util.ArrayList) ChecksumCalculatingStreamProvider(software.amazon.awssdk.services.s3.internal.handlers.SyncChecksumValidationInterceptor.ChecksumCalculatingStreamProvider) SdkChecksum(software.amazon.awssdk.core.checksums.SdkChecksum) Md5Checksum(software.amazon.awssdk.core.checksums.Md5Checksum) ContentStreamProvider(software.amazon.awssdk.http.ContentStreamProvider) ChecksumCalculatingInputStream(software.amazon.awssdk.services.s3.checksums.ChecksumCalculatingInputStream) Test(org.junit.jupiter.api.Test)

Example 29 with StringInputStream

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

the class ProfileUseArnRegionProviderTest method specifiedInOverrideConfig_shouldUse.

@Test
public void specifiedInOverrideConfig_shouldUse() {
    ExecutionInterceptor interceptor = Mockito.spy(AbstractExecutionInterceptor.class);
    String profileFileContent = "[default]\n" + "s3_use_arn_region = 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))).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) FALSE(java.lang.Boolean.FALSE) 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 30 with StringInputStream

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

the class InterceptorTestUtils method modifyHttpResponse.

public static Context.ModifyHttpResponse modifyHttpResponse(SdkRequest request, SdkHttpResponse sdkHttpResponse) {
    Publisher<ByteBuffer> publisher = new EmptyPublisher<>();
    InputStream responseBody = new StringInputStream("helloworld");
    return new Context.ModifyResponse() {

        @Override
        public SdkResponse response() {
            return null;
        }

        @Override
        public SdkHttpResponse httpResponse() {
            return sdkHttpResponse;
        }

        @Override
        public Optional<Publisher<ByteBuffer>> responsePublisher() {
            return Optional.of(publisher);
        }

        @Override
        public Optional<InputStream> responseBody() {
            return Optional.of(responseBody);
        }

        @Override
        public SdkHttpRequest httpRequest() {
            return SdkHttpRequest.builder().build();
        }

        @Override
        public Optional<RequestBody> requestBody() {
            return Optional.empty();
        }

        @Override
        public Optional<AsyncRequestBody> asyncRequestBody() {
            return Optional.empty();
        }

        @Override
        public SdkRequest request() {
            return request;
        }
    };
}
Also used : StringInputStream(software.amazon.awssdk.utils.StringInputStream) StringInputStream(software.amazon.awssdk.utils.StringInputStream) InputStream(java.io.InputStream) EmptyPublisher(software.amazon.awssdk.core.async.EmptyPublisher) AsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody) EmptyPublisher(software.amazon.awssdk.core.async.EmptyPublisher) Publisher(org.reactivestreams.Publisher) ByteBuffer(java.nio.ByteBuffer) AsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody) RequestBody(software.amazon.awssdk.core.sync.RequestBody)

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