Java源码示例:software.amazon.awssdk.core.sync.ResponseTransformer
示例1
@Test
public void sse_AWSKMS_succeeds() throws Exception {
String key = UUID.randomUUID().toString();
PutObjectRequest request = PutObjectRequest.builder()
.key(key)
.bucket(BUCKET)
.serverSideEncryption(ServerSideEncryption.AWS_KMS)
.build();
s3.putObject(request, file.toPath());
GetObjectRequest getObjectRequest = GetObjectRequest.builder()
.key(key)
.bucket(BUCKET)
.build();
String response = s3.getObject(getObjectRequest, ResponseTransformer.toBytes()).asUtf8String();
SdkAsserts.assertStringEqualsStream(response, new FileInputStream(file));
}
示例2
@Test
public void sse_onBucket_succeeds() throws FileNotFoundException {
String key = UUID.randomUUID().toString();
PutObjectRequest request = PutObjectRequest.builder()
.key(key)
.bucket(BUCKET_WITH_SSE)
.build();
s3.putObject(request, file.toPath());
GetObjectRequest getObjectRequest = GetObjectRequest.builder()
.key(key)
.bucket(BUCKET_WITH_SSE)
.build();
String response = s3.getObject(getObjectRequest, ResponseTransformer.toBytes()).asUtf8String();
SdkAsserts.assertStringEqualsStream(response, new FileInputStream(file));
}
示例3
/**
* Generate a simple method for operations with streaming input and output members.
* Streaming input member that reads data from a file and a streaming output member that write response content to a file.
*/
private MethodSpec streamingInputOutputFileSimpleMethod(OperationModel opModel,
TypeName responseType,
ClassName requestType) {
return MethodSpec.methodBuilder(opModel.getMethodName())
.returns(responseType)
.addModifiers(Modifier.PUBLIC, Modifier.DEFAULT)
.addParameter(requestType, opModel.getInput().getVariableName())
.addParameter(ClassName.get(Path.class), "sourcePath")
.addParameter(ClassName.get(Path.class), "destinationPath")
.addJavadoc(opModel.getDocs(model, ClientType.SYNC, SimpleMethodOverload.FILE))
.addExceptions(getExceptionClasses(model, opModel))
.addStatement("return $L($L, $T.fromFile(sourcePath), $T.toFile(destinationPath))",
opModel.getMethodName(),
opModel.getInput().getVariableName(),
ClassName.get(RequestBody.class),
ClassName.get(ResponseTransformer.class))
.build();
}
示例4
@GET
@Path("download/{objectKey}")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response downloadFile(@PathParam("objectKey") String objectKey) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GetObjectResponse object = s3.getObject(buildGetRequest(objectKey), ResponseTransformer.toOutputStream(baos));
ResponseBuilder response = Response.ok((StreamingOutput) output -> baos.writeTo(output));
response.header("Content-Disposition", "attachment;filename=" + objectKey);
response.header("Content-Type", object.contentType());
return response.build();
}
示例5
@GET
@Path("blocking")
@Produces(TEXT_PLAIN)
public String testBlockingS3() {
LOG.info("Testing S3 Blocking client with bucket: " + SYNC_BUCKET);
String keyValue = UUID.randomUUID().toString();
String result = null;
try {
if (S3Utils.createBucket(s3Client, SYNC_BUCKET)) {
if (s3Client.putObject(S3Utils.createPutRequest(SYNC_BUCKET, keyValue),
RequestBody.fromString(SAMPLE_S3_OBJECT)) != null) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GetObjectResponse object = s3Client.getObject(S3Utils.createGetRequest(SYNC_BUCKET, keyValue),
ResponseTransformer.toOutputStream(baos));
if (object != null) {
result = metadata(object) + "+" + baos.toString();
}
}
}
} catch (Exception ex) {
LOG.error("Error during S3 operations.", ex);
return "ERROR";
}
return result;
}
示例6
@Test
public void toInputStream_loadFromProperties() throws IOException {
s3.putObject(b -> b.bucket(BUCKET).key(PROPERTY_KEY), RequestBody.fromString("test: test"));
try (ResponseInputStream<GetObjectResponse> object = s3.getObject(b -> b.bucket(BUCKET).key(PROPERTY_KEY),
ResponseTransformer.toInputStream())) {
Properties properties = new Properties();
properties.load(object);
assertThat(properties.getProperty("test")).isEqualTo("test");
}
}
示例7
@Test
public void downloadToExistingFileDoesNotRetry() throws IOException {
stubForRetriesTimeoutReadingFromStreams();
assertThatThrownBy(() -> testClient().streamingOutputOperation(StreamingOutputOperationRequest.builder().build(),
ResponseTransformer
.toFile(new File(".."))))
.isInstanceOf(SdkClientException.class);
}
示例8
@Test
public void downloadToOutputStreamDoesNotRetry() throws IOException {
stubForRetriesTimeoutReadingFromStreams();
assertThatThrownBy(() -> testClient().streamingOutputOperation(StreamingOutputOperationRequest.builder().build(),
ResponseTransformer
.toOutputStream(new ByteArrayOutputStream())))
.isInstanceOf(SdkClientException.class);
}
示例9
@Test
public void syncStreamingShouldContainSdkHttpDate() {
stubWithHeaders(EXPECTED_HEADERS);
ResponseBytes<StreamingOutputOperationResponse> responseBytes = client
.streamingOutputOperation(SdkBuilder::build, ResponseTransformer.toBytes());
StreamingOutputOperationResponse response = responseBytes.response();
verifySdkHttpResponse(response);
verifyResponseMetadata(response);
}
示例10
public SlowFileResponseTransformer() {
try {
this.delegate = ResponseTransformer.toFile(File.createTempFile("ApiCallTiemoutTest", ".txt"));
} catch (IOException e) {
e.printStackTrace();
}
}
示例11
@Test
public void streamingOut_successfulResponse_shouldCloseConnection() {
ClosableStringInputStream inputStream = new ClosableStringInputStream("{}");
mockHttpClient.stubNextResponse(mockResponse(inputStream, 200));
client.streamingOutputOperation(b -> b.build(), ResponseTransformer.toBytes());
assertThat(inputStream.isClosed()).isTrue();
}
示例12
@Test
public void streamingOut_errorResponse_shouldCloseConnection() {
ClosableStringInputStream inputStream = new ClosableStringInputStream("{\"__type\":\"SomeUnknownType\"}");
mockHttpClient.stubNextResponse(mockResponse(inputStream, 400));
assertThatThrownBy(() -> client.streamingOutputOperation(b -> b.build(), ResponseTransformer.toBytes()))
.isExactlyInstanceOf(ProtocolRestJsonException.class);
assertThat(inputStream.isClosed()).isTrue();
}
示例13
@Test
public void streamingOut_toInputStream_closeResponseStreamShouldCloseUnderlyingStream() throws IOException {
ClosableStringInputStream inputStream = new ClosableStringInputStream("{}");
mockHttpClient.stubNextResponse(mockResponse(inputStream, 200));
ResponseInputStream<StreamingOutputOperationResponse> responseInputStream =
client.streamingOutputOperation(b -> b.build(), ResponseTransformer.toInputStream());
assertThat(inputStream.isClosed()).isFalse();
responseInputStream.close();
assertThat(inputStream.isClosed()).isTrue();
}
示例14
@Test
public void syncStreaming_shouldContainResponseMetadata() {
stubResponseWithHeaders();
ResponseBytes<StreamingOutputOperationResponse> streamingOutputOperationResponseResponseBytes =
client.streamingOutputOperation(SdkBuilder::build, ResponseTransformer.toBytes());
verifyResponseMetadata(streamingOutputOperationResponseResponseBytes.response());
}
示例15
/**
* Call the operation (with a streaming output) method on the client with the given request.
*
* @param requestObject POJO request object.
* @param responseHandler Response handler for an operation with a streaming output.
* @return Unmarshalled result
*/
public Object invokeStreamingMethod(TestCase testCase,
Object requestObject,
ResponseTransformer<?, ?> responseHandler) throws Exception {
String operationName = testCase.getWhen().getOperationName();
Method operationMethod = getOperationMethod(operationName, requestObject.getClass(), ResponseTransformer.class);
return operationMethod.invoke(client, requestObject, responseHandler);
}
示例16
private static void streamingMethod(MethodSpec.Builder methodBuilder, OperationModel opModel, TypeName responseType) {
if (opModel.hasStreamingInput()) {
methodBuilder.addParameter(ClassName.get(RequestBody.class), "requestBody");
}
if (opModel.hasStreamingOutput()) {
methodBuilder.addTypeVariable(STREAMING_TYPE_VARIABLE);
ParameterizedTypeName streamingResponseHandlerType = ParameterizedTypeName
.get(ClassName.get(ResponseTransformer.class), responseType, STREAMING_TYPE_VARIABLE);
methodBuilder.addParameter(streamingResponseHandlerType, "responseTransformer");
}
}
示例17
/**
* @return Simple method for streaming output operations to get content as an input stream.
*/
private MethodSpec inputStreamSimpleMethod(OperationModel opModel, TypeName responseType, ClassName requestType) {
TypeName returnType = ParameterizedTypeName.get(ClassName.get(ResponseInputStream.class), responseType);
return MethodSpec.methodBuilder(opModel.getMethodName())
.returns(returnType)
.addModifiers(Modifier.PUBLIC, Modifier.DEFAULT)
.addParameter(requestType, opModel.getInput().getVariableName())
.addJavadoc(opModel.getDocs(model, ClientType.SYNC, SimpleMethodOverload.INPUT_STREAM))
.addExceptions(getExceptionClasses(model, opModel))
.addStatement("return $L($L, $T.toInputStream())", opModel.getMethodName(),
opModel.getInput().getVariableName(),
ClassName.get(ResponseTransformer.class))
.build();
}
示例18
/**
* @return Simple method for streaming output operations to get the content as a byte buffer or other in-memory types.
*/
private MethodSpec bytesSimpleMethod(OperationModel opModel, TypeName responseType, ClassName requestType) {
TypeName returnType = ParameterizedTypeName.get(ClassName.get(ResponseBytes.class), responseType);
return MethodSpec.methodBuilder(opModel.getMethodName() + "AsBytes")
.returns(returnType)
.addModifiers(Modifier.PUBLIC, Modifier.DEFAULT)
.addParameter(requestType, opModel.getInput().getVariableName())
.addJavadoc(opModel.getDocs(model, ClientType.SYNC, SimpleMethodOverload.BYTES))
.addExceptions(getExceptionClasses(model, opModel))
.addStatement("return $L($L, $T.toBytes())", opModel.getMethodName(),
opModel.getInput().getVariableName(),
ClassName.get(ResponseTransformer.class))
.build();
}
示例19
/**
* @return Simple method for streaming output operations to write response content to a file.
*/
private MethodSpec downloadToFileSimpleMethod(OperationModel opModel, TypeName responseType, ClassName requestType) {
return MethodSpec.methodBuilder(opModel.getMethodName())
.returns(responseType)
.addModifiers(Modifier.PUBLIC, Modifier.DEFAULT)
.addParameter(requestType, opModel.getInput().getVariableName())
.addParameter(ClassName.get(Path.class), "filePath")
.addJavadoc(opModel.getDocs(model, ClientType.SYNC, SimpleMethodOverload.FILE))
.addExceptions(getExceptionClasses(model, opModel))
.addStatement("return $L($L, $T.toFile($L))", opModel.getMethodName(),
opModel.getInput().getVariableName(),
ClassName.get(ResponseTransformer.class),
"filePath")
.build();
}
示例20
@Override
public <InputT extends SdkRequest, OutputT extends SdkResponse, ReturnT> ReturnT execute(
ClientExecutionParams<InputT, OutputT> executionParams,
ResponseTransformer<OutputT, ReturnT> responseTransformer) {
validateExecutionParams(executionParams);
if (executionParams.getCombinedResponseHandler() != null) {
// There is no support for catching errors in a body for streaming responses
throw new IllegalArgumentException("A streaming 'responseTransformer' may not be used when a "
+ "'combinedResponseHandler' has been specified in a "
+ "ClientExecutionParams object.");
}
ExecutionContext executionContext = createExecutionContext(executionParams, createInitialExecutionAttributes());
HttpResponseHandler<OutputT> decoratedResponseHandlers =
decorateResponseHandlers(executionParams.getResponseHandler(), executionContext);
HttpResponseHandler<ReturnT> httpResponseHandler =
new HttpResponseHandlerAdapter<>(decoratedResponseHandlers, responseTransformer);
return doExecute(
executionParams,
executionContext,
new CombinedResponseHandler<>(httpResponseHandler, executionParams.getErrorResponseHandler()));
}
示例21
@Override
public void copyToLocalFile(URI srcUri, File dstFile)
throws Exception {
LOGGER.info("Copy {} to local {}", srcUri, dstFile.getAbsolutePath());
URI base = getBase(srcUri);
String prefix = sanitizePath(base.relativize(srcUri).getPath());
GetObjectRequest getObjectRequest = GetObjectRequest.builder().bucket(srcUri.getHost()).key(prefix).build();
_s3Client.getObject(getObjectRequest, ResponseTransformer.toFile(dstFile));
}
示例22
@Test
public void toOutputStream() throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GetObjectResponse response = s3.getObject(getObjectRequest, ResponseTransformer.toOutputStream(baos));
}
示例23
private RequestCountingResponseTransformer(ResponseTransformer<ResponseT, ReturnT> delegate) {
this.delegate = delegate;
}
示例24
private void verifyMultipartUploadResult(String key, List<String> contentsToUpload) throws Exception {
ResponseBytes<GetObjectResponse> objectAsBytes = s3.getObject(b -> b.bucket(BUCKET).key(key),
ResponseTransformer.toBytes());
String appendedString = String.join("", contentsToUpload);
assertThat(objectAsBytes.asUtf8String()).isEqualTo(appendedString);
}
示例25
@Test
public void syncStreaming_shouldContainResponseMetadata() {
ResponseBytes<GetObjectResponse> responseBytes = s3.getObject(b -> b.key(KEY).bucket(BUCKET), ResponseTransformer.toBytes());
GetObjectResponse response = responseBytes.response();
verifyResponseMetadata(response);
}
示例26
public SlowBytesResponseTransformer() {
this.delegate = ResponseTransformer.toBytes();
}
示例27
public SlowInputStreamResponseTransformer() {
this.delegate = ResponseTransformer.toInputStream();
}
示例28
@Override
protected Callable streamingCallable() {
return () -> client.streamingOutputOperation(SdkBuilder::build, ResponseTransformer.toBytes());
}
示例29
@Override
protected Callable streamingCallable() {
return () -> client.streamingOutputOperation(SdkBuilder::build, ResponseTransformer.toBytes());
}
示例30
/**
* Some operation with a streaming output
*
* @param streamingOutputOperationRequest
* @param responseTransformer
* Functional interface for processing the streamed response content. The unmarshalled
* StreamingOutputOperationResponse and an InputStream to the response content are provided as parameters
* to the callback. The callback may return a transformed type which will be the return value of this method.
* See {@link software.amazon.awssdk.core.sync.ResponseTransformer} for details on implementing this
* interface and for links to pre-canned implementations for common scenarios like downloading to a file. The
* service documentation for the response content is as follows 'This be a stream'.
* @return The transformed result of the ResponseTransformer.
* @throws SdkException
* Base class for all exceptions that can be thrown by the SDK (both service and client). Can be used for
* catch all scenarios.
* @throws SdkClientException
* If any client side error occurs such as an IO related failure, failure to get credentials, etc.
* @throws JsonException
* Base class for all service exceptions. Unknown exceptions will be thrown as an instance of this type.
* @sample JsonClient.StreamingOutputOperation
* @see <a href="http://docs.aws.amazon.com/goto/WebAPI/json-service-2010-05-08/StreamingOutputOperation"
* target="_top">AWS API Documentation</a>
*/
@Override
public <ReturnT> ReturnT streamingOutputOperation(StreamingOutputOperationRequest streamingOutputOperationRequest,
ResponseTransformer<StreamingOutputOperationResponse, ReturnT> responseTransformer) throws AwsServiceException,
SdkClientException, JsonException {
JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(true)
.isPayloadJson(false).build();
HttpResponseHandler<StreamingOutputOperationResponse> responseHandler = protocolFactory.createResponseHandler(
operationMetadata, StreamingOutputOperationResponse::builder);
HttpResponseHandler<AwsServiceException> errorResponseHandler = createErrorResponseHandler(protocolFactory,
operationMetadata);
return clientHandler.execute(
new ClientExecutionParams<StreamingOutputOperationRequest, StreamingOutputOperationResponse>()
.withOperationName("StreamingOutputOperation")
.withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler)
.withInput(streamingOutputOperationRequest)
.withMarshaller(new StreamingOutputOperationRequestMarshaller(protocolFactory)), responseTransformer);
}