Search in sources :

Example 1 with StringInputStream

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

the class XmlProtocolMarshaller method finishMarshalling.

private SdkHttpFullRequest finishMarshalling(SdkPojo pojo) {
    // Content may already be set if the payload is binary data.
    if (hasPayloadMembers(pojo) && request.contentStreamProvider() == null && marshallerContext.xmlGenerator() != null) {
        String content = marshallerContext.xmlGenerator().stringWriter().getBuffer().toString();
        if (!content.isEmpty()) {
            request.contentStreamProvider(() -> new StringInputStream(content));
            request.putHeader("Content-Length", Integer.toString(content.getBytes(StandardCharsets.UTF_8).length));
            setContentTypeHeaderIfNeeded("application/xml");
        }
    }
    return request.build();
}
Also used : StringInputStream(software.amazon.awssdk.utils.StringInputStream) InstantToString(software.amazon.awssdk.protocols.core.InstantToString)

Example 2 with StringInputStream

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

the class XmlDomParserTest method multipleElementsWithSameName_ParsedCorrectly.

@Test
public void multipleElementsWithSameName_ParsedCorrectly() {
    String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<Struct>" + " <member>valOne</member>" + " <member>valTwo</member>" + "</Struct>";
    XmlElement element = XmlDomParser.parse(new StringInputStream(xml));
    assertThat(element.getElementsByName("member")).hasSize(2);
    assertThat(element.getElementsByName("member").get(0).textContent()).isEqualTo("valOne");
    assertThat(element.getElementsByName("member").get(1).textContent()).isEqualTo("valTwo");
}
Also used : StringInputStream(software.amazon.awssdk.utils.StringInputStream) XmlElement(software.amazon.awssdk.protocols.query.unmarshall.XmlElement) Test(org.junit.jupiter.api.Test)

Example 3 with StringInputStream

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

the class RequestBodyTest method streamConstructorHasCorrectContentType.

@Test
public void streamConstructorHasCorrectContentType() {
    StringInputStream inputStream = new StringInputStream("hello world");
    RequestBody requestBody = RequestBody.fromInputStream(inputStream, 11);
    assertThat(requestBody.contentType()).isEqualTo(Mimetype.MIMETYPE_OCTET_STREAM);
    IoUtils.closeQuietly(inputStream, null);
}
Also used : StringInputStream(software.amazon.awssdk.utils.StringInputStream) Test(org.junit.Test)

Example 4 with StringInputStream

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

the class S3PresignerIntegrationTest method putObject_PresignedHttpRequestCanBeInvokedDirectlyBySdk.

@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();
    // or UrlConnectionHttpClient.builder().build()
    SdkHttpClient httpClient = ApacheHttpClient.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);
}
Also used : HttpURLConnection(java.net.HttpURLConnection) BeforeClass(org.junit.BeforeClass) ApacheHttpClient(software.amazon.awssdk.http.apache.ApacheHttpClient) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CreateMultipartUploadResponse(software.amazon.awssdk.services.s3.model.CreateMultipartUploadResponse) SdkBytes(software.amazon.awssdk.core.SdkBytes) PresignedAbortMultipartUploadRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedAbortMultipartUploadRequest) RequestPayer(software.amazon.awssdk.services.s3.model.RequestPayer) S3Presigner(software.amazon.awssdk.services.s3.presigner.S3Presigner) Duration(java.time.Duration) After(org.junit.After) PresignedPutObjectRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedPutObjectRequest) Before(org.junit.Before) UploadPartRequest(software.amazon.awssdk.services.s3.model.UploadPartRequest) AfterClass(org.junit.AfterClass) MultipartUpload(software.amazon.awssdk.services.s3.model.MultipartUpload) PresignedGetObjectRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedGetObjectRequest) ContentStreamProvider(software.amazon.awssdk.http.ContentStreamProvider) PresignedUploadPartRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedUploadPartRequest) IOException(java.io.IOException) Test(org.junit.Test) PresignedRequest(software.amazon.awssdk.awscore.presigner.PresignedRequest) UUID(java.util.UUID) SdkHttpClient(software.amazon.awssdk.http.SdkHttpClient) CompleteMultipartUploadRequest(software.amazon.awssdk.services.s3.model.CompleteMultipartUploadRequest) PresignedCompleteMultipartUploadRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedCompleteMultipartUploadRequest) S3TestUtils(software.amazon.awssdk.services.s3.utils.S3TestUtils) Consumer(java.util.function.Consumer) AbortableInputStream(software.amazon.awssdk.http.AbortableInputStream) StringInputStream(software.amazon.awssdk.utils.StringInputStream) PresignedCreateMultipartUploadRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedCreateMultipartUploadRequest) Optional(java.util.Optional) RequestBody(software.amazon.awssdk.core.sync.RequestBody) HttpExecuteResponse(software.amazon.awssdk.http.HttpExecuteResponse) CreateMultipartUploadRequest(software.amazon.awssdk.services.s3.model.CreateMultipartUploadRequest) AbortMultipartUploadRequest(software.amazon.awssdk.services.s3.model.AbortMultipartUploadRequest) IoUtils(software.amazon.awssdk.utils.IoUtils) InputStream(java.io.InputStream) HttpExecuteRequest(software.amazon.awssdk.http.HttpExecuteRequest) UploadPartResponse(software.amazon.awssdk.services.s3.model.UploadPartResponse) HttpExecuteRequest(software.amazon.awssdk.http.HttpExecuteRequest) StringInputStream(software.amazon.awssdk.utils.StringInputStream) HttpExecuteResponse(software.amazon.awssdk.http.HttpExecuteResponse) AbortableInputStream(software.amazon.awssdk.http.AbortableInputStream) PresignedPutObjectRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedPutObjectRequest) SdkHttpClient(software.amazon.awssdk.http.SdkHttpClient) ContentStreamProvider(software.amazon.awssdk.http.ContentStreamProvider) Test(org.junit.Test)

Example 5 with StringInputStream

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

the class TraceIdTest method traceIdInterceptorIsEnabled.

@Test
public void traceIdInterceptorIsEnabled() {
    EnvironmentVariableHelper.run(env -> {
        env.set("AWS_LAMBDA_FUNCTION_NAME", "foo");
        env.set("_X_AMZN_TRACE_ID", "bar");
        try (MockSyncHttpClient mockHttpClient = new MockSyncHttpClient();
            ProtocolRestJsonClient client = ProtocolRestJsonClient.builder().region(Region.US_WEST_2).credentialsProvider(AnonymousCredentialsProvider.create()).httpClient(mockHttpClient).build()) {
            mockHttpClient.stubNextResponse(HttpExecuteResponse.builder().response(SdkHttpResponse.builder().statusCode(200).build()).responseBody(AbortableInputStream.create(new StringInputStream("{}"))).build());
            client.allTypes();
            assertThat(mockHttpClient.getLastRequest().firstMatchingHeader("X-Amzn-Trace-Id")).hasValue("bar");
        }
    });
}
Also used : StringInputStream(software.amazon.awssdk.utils.StringInputStream) ProtocolRestJsonClient(software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonClient) MockSyncHttpClient(software.amazon.awssdk.testutils.service.http.MockSyncHttpClient) Test(org.junit.jupiter.api.Test)

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