Java Code Examples for software.amazon.awssdk.utils.IoUtils#toUtf8String()

The following examples show how to use software.amazon.awssdk.utils.IoUtils#toUtf8String() . 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: LocalS3ClientTest.java    From synapse with Apache License 2.0 6 votes vote down vote up
@Test
public void getObjectShouldReturnStreamWithData() throws Exception {
    // given
    testee.putObject(PutObjectRequest.builder()
                    .bucket("someBucket")
                    .key("someKey")
                    .build(),
            RequestBody.fromString("testdata"));
    //when
    ResponseInputStream<GetObjectResponse> inputStream = testee.getObject(GetObjectRequest.builder()
            .bucket("someBucket")
            .key("someKey")
            .build());

    //then
    String data = IoUtils.toUtf8String(inputStream);
    assertThat(data, is("testdata"));
}
 
Example 6
Source File: LocalS3ClientTest.java    From edison-microservice with Apache License 2.0 6 votes vote down vote up
@Test
public void getObjectShouldReturnStreamWithData() throws Exception {
    // given
    testee.putObject(PutObjectRequest.builder()
                    .bucket("someBucket")
                    .key("someKey")
                    .build(),
            RequestBody.fromString("testdata"));
    //when
    ResponseInputStream<GetObjectResponse> inputStream = testee.getObject(GetObjectRequest.builder()
            .bucket("someBucket")
            .key("someKey")
            .build());

    //then
    String data = IoUtils.toUtf8String(inputStream);
    assertThat(data, is("testdata"));
}
 
Example 7
Source File: StsWebIdentityCredentialsProviderFactory.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private String getToken(Path file) {
    try (InputStream webIdentityTokenStream = Files.newInputStream(file)) {
        return IoUtils.toUtf8String(webIdentityTokenStream);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
 
Example 8
Source File: SSMServiceIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testAll() throws Exception {

    String documentContent = IoUtils.toUtf8String(getClass().getResourceAsStream(DOCUMENT_LOCATION));
    testCreateDocument(DOCUMENT_NAME, documentContent);
    testDescribeDocument();
}
 
Example 9
Source File: AwsIntegrationTestBase.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * Reads a system resource fully into a String
 *
 * @param location
 *            Relative or absolute location of system resource.
 * @return String contents of resource file
 * @throws RuntimeException
 *             if any error occurs
 */
protected String getResourceAsString(Class<?> clazz, String location) {
    try (InputStream resourceStream = clazz.getResourceAsStream(location)) {
        String resourceAsString = IoUtils.toUtf8String(resourceStream);
        resourceStream.close();
        return resourceAsString;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 10
Source File: HttpCredentialsProviderTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void setup() throws IOException {
    try (InputStream successInputStream = HttpCredentialsProviderTest.class.getResourceAsStream
        ("/resources/wiremock/successResponse.json");
         InputStream responseWithInvalidBodyInputStream = HttpCredentialsProviderTest.class.getResourceAsStream
             ("/resources/wiremock/successResponseWithInvalidBody.json")) {
        successResponse = IoUtils.toUtf8String(successInputStream);
        successResponseWithInvalidBody = IoUtils.toUtf8String(responseWithInvalidBodyInputStream);
    }
}
 
Example 11
Source File: MockServer.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
public DummyResponseServerBehavior(HttpResponse response) {
    this.response = response;
    try {
        this.content = IoUtils.toUtf8String(response.getEntity().getContent());
    } catch (Exception e) {
        // Ignored or expected.
    }
}
 
Example 12
Source File: UnmarshallingTestRunner.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
@Override
public Void transform(Object response, AbortableInputStream inputStream) throws Exception {
    this.captured = IoUtils.toUtf8String(inputStream);
    return null;
}
 
Example 13
Source File: IntermediateModel.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
private String loadDefaultFileHeader() throws IOException {
    try (InputStream inputStream = getClass()
        .getResourceAsStream("/software/amazon/awssdk/codegen/DefaultFileHeader.txt")) {
        return IoUtils.toUtf8String(inputStream);
    }
}
 
Example 14
Source File: HttpResourcesUtils.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
/**
 * Connects to the given endpoint to read the resource
 * and returns the text contents.
 *
 * @param endpointProvider The endpoint provider.
 * @param method The HTTP request method to use.
 * @return The text payload returned from the container metadata endpoint
 * service for the specified resource path.
 * @throws IOException If any problems were encountered while connecting to the
 * service for the requested resource path.
 * @throws SdkClientException If the requested service is not found.
 */
public String readResource(ResourcesEndpointProvider endpointProvider, String method) throws IOException {
    int retriesAttempted = 0;
    InputStream inputStream = null;

    while (true) {
        try {
            HttpURLConnection connection = connectionUtils.connectToEndpoint(endpointProvider.endpoint(),
                                                                             endpointProvider.headers(),
                                                                             method);

            int statusCode = connection.getResponseCode();

            if (statusCode == HttpURLConnection.HTTP_OK) {
                inputStream = connection.getInputStream();
                return IoUtils.toUtf8String(inputStream);
            } else if (statusCode == HttpURLConnection.HTTP_NOT_FOUND) {
                // This is to preserve existing behavior of EC2 Instance metadata service.
                throw SdkClientException.builder()
                                        .message("The requested metadata is not found at " + connection.getURL())
                                        .build();
            } else {
                if (!endpointProvider.retryPolicy().shouldRetry(retriesAttempted++,
                                                                ResourcesEndpointRetryParameters.builder()
                                                                                                .withStatusCode(statusCode)
                                                                                                .build())) {
                    inputStream = connection.getErrorStream();
                    handleErrorResponse(inputStream, statusCode, connection.getResponseMessage());
                }
            }
        } catch (IOException ioException) {
            if (!endpointProvider.retryPolicy().shouldRetry(retriesAttempted++,
                                                            ResourcesEndpointRetryParameters.builder()
                                                                                            .withException(ioException)
                                                                                            .build())) {
                throw ioException;
            }
            log.debug("An IOException occurred when connecting to endpoint: {} \n Retrying to connect again",
                      endpointProvider.endpoint());

        } finally {
            IoUtils.closeQuietly(inputStream, log);
        }
    }

}
 
Example 15
Source File: AwsTestBase.java    From aws-sdk-java-v2 with Apache License 2.0 3 votes vote down vote up
/**
 * Reads a system resource fully into a String
 *
 * @param location
 *            Relative or absolute location of system resource.
 * @return String contents of resource file
 * @throws RuntimeException
 *             if any error occurs
 */
protected static String getResourceAsString(Class<?> clazz, String location) {
    try (InputStream resourceStream = clazz.getResourceAsStream(location)) {
        return IoUtils.toUtf8String(resourceStream);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}