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

The following examples show how to use software.amazon.awssdk.utils.IoUtils#copy() . 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: AddContentMd5HeaderInterceptor.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Override
public Optional<RequestBody> modifyHttpContent(Context.ModifyHttpRequest context,
                                               ExecutionAttributes executionAttributes) {

    if (!BLACKLIST_METHODS.contains(context.request().getClass()) && context.requestBody().isPresent()
        && !context.httpRequest().firstMatchingHeader(CONTENT_MD5).isPresent()) {

        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            IoUtils.copy(context.requestBody().get().contentStreamProvider().newStream(), baos);
            executionAttributes.putAttribute(CONTENT_MD5_ATTRIBUTE, Md5Utils.md5AsBase64(baos.toByteArray()));
            return context.requestBody();
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }

    return context.requestBody();
}
 
Example 2
Source File: ProcessCredentialsProvider.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
/**
 * Execute the external process to retrieve credentials.
 */
private String executeCommand() throws IOException, InterruptedException {
    ProcessBuilder processBuilder = new ProcessBuilder(command);

    ByteArrayOutputStream commandOutput = new ByteArrayOutputStream();

    Process process = processBuilder.start();
    try {
        IoUtils.copy(process.getInputStream(), commandOutput, processOutputLimit);

        process.waitFor();

        if (process.exitValue() != 0) {
            throw new IllegalStateException("Command returned non-zero exit value: " + process.exitValue());
        }

        return new String(commandOutput.toByteArray(), StandardCharsets.UTF_8);
    } finally {
        process.destroy();
    }
}
 
Example 3
Source File: S3BundlePersistenceProvider.java    From nifi-registry with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized void getBundleVersionContent(final BundleVersionCoordinate versionCoordinate, final OutputStream outputStream)
        throws BundlePersistenceException {
    final String key = getKey(versionCoordinate);
    LOGGER.debug("Retrieving bundle version from S3 bucket '{}' with key '{}'", new Object[]{s3BucketName, key});

    final GetObjectRequest request = GetObjectRequest.builder()
            .bucket(s3BucketName)
            .key(key)
            .build();

    try (final ResponseInputStream<GetObjectResponse> response = s3Client.getObject(request)) {
        IoUtils.copy(response, outputStream);
        LOGGER.debug("Successfully retrieved bundle version from S3 bucket '{}' with key '{}'", new Object[]{s3BucketName, key});
    } catch (Exception e) {
        throw new BundlePersistenceException("Error retrieving bundle version from S3 due to: " + e.getMessage(), e);
    }
}
 
Example 4
Source File: ResponseTransformer.java    From aws-sdk-java-v2 with Apache License 2.0 3 votes vote down vote up
/**
 * Creates a response transformer that writes all response content to the given {@link OutputStream}. Note that
 * the {@link OutputStream} is not closed or flushed after writing.
 *
 * @param outputStream Output stream to write data to.
 * @param <ResponseT>  Type of unmarshalled response POJO.
 * @return ResponseTransformer instance.
 */
static <ResponseT> ResponseTransformer<ResponseT, ResponseT> toOutputStream(OutputStream outputStream) {
    return (resp, in) -> {
        InterruptMonitor.checkInterrupted();
        IoUtils.copy(in, outputStream);
        return resp;
    };
}