This is an automated email from the ASF dual-hosted git repository. acosentino pushed a commit to branch camel-3.20.x in repository https://gitbox.apache.org/repos/asf/camel.git
commit 2c0d4b02ca42c7497a060c36ccb66ac3b9496e4a Author: Andrea Cosentino <[email protected]> AuthorDate: Fri Feb 10 14:06:24 2023 +0100 CAMEL-19034 - Camel-AWS2-S3: GetObject should preserve the metadata Signed-off-by: Andrea Cosentino <[email protected]> --- .../camel/component/aws2/s3/AWS2S3Producer.java | 17 +++++ .../s3/integration/S3GetObjectOperationIT.java | 85 ++++++++++++++++++++++ 2 files changed, 102 insertions(+) diff --git a/components/camel-aws/camel-aws2-s3/src/main/java/org/apache/camel/component/aws2/s3/AWS2S3Producer.java b/components/camel-aws/camel-aws2-s3/src/main/java/org/apache/camel/component/aws2/s3/AWS2S3Producer.java index 4d825d69628..c22f2aa0892 100644 --- a/components/camel-aws/camel-aws2-s3/src/main/java/org/apache/camel/component/aws2/s3/AWS2S3Producer.java +++ b/components/camel-aws/camel-aws2-s3/src/main/java/org/apache/camel/component/aws2/s3/AWS2S3Producer.java @@ -510,6 +510,7 @@ public class AWS2S3Producer extends DefaultProducer { = s3Client.getObject((GetObjectRequest) payload, ResponseTransformer.toInputStream()); Message message = getMessageForResponse(exchange); message.setBody(res); + populateMetadata(res, message); } } else { final String bucketName = AWS2S3Utils.determineBucketName(exchange, getConfiguration()); @@ -519,6 +520,7 @@ public class AWS2S3Producer extends DefaultProducer { Message message = getMessageForResponse(exchange); message.setBody(res); + populateMetadata(res, message); } } @@ -652,6 +654,21 @@ public class AWS2S3Producer extends DefaultProducer { return objectMetadata; } + private static void populateMetadata(ResponseInputStream<GetObjectResponse> res, Message message) { + message.setHeader(AWS2S3Constants.E_TAG, res.response().eTag()); + message.setHeader(AWS2S3Constants.VERSION_ID, res.response().versionId()); + message.setHeader(AWS2S3Constants.CONTENT_TYPE, res.response().contentType()); + message.setHeader(AWS2S3Constants.CONTENT_LENGTH, res.response().contentLength()); + message.setHeader(AWS2S3Constants.CONTENT_ENCODING, res.response().contentEncoding()); + message.setHeader(AWS2S3Constants.CONTENT_DISPOSITION, res.response().contentDisposition()); + message.setHeader(AWS2S3Constants.CACHE_CONTROL, res.response().cacheControl()); + message.setHeader(AWS2S3Constants.SERVER_SIDE_ENCRYPTION, res.response().serverSideEncryption()); + message.setHeader(AWS2S3Constants.EXPIRATION_TIME, res.response().expiration()); + message.setHeader(AWS2S3Constants.REPLICATION_STATUS, res.response().replicationStatus()); + message.setHeader(AWS2S3Constants.STORAGE_CLASS, res.response().storageClass()); + message.setHeader(AWS2S3Constants.METADATA, res.response().metadata()); + } + protected AWS2S3Configuration getConfiguration() { return getEndpoint().getConfiguration(); } diff --git a/components/camel-aws/camel-aws2-s3/src/test/java/org/apache/camel/component/aws2/s3/integration/S3GetObjectOperationIT.java b/components/camel-aws/camel-aws2-s3/src/test/java/org/apache/camel/component/aws2/s3/integration/S3GetObjectOperationIT.java new file mode 100644 index 00000000000..99003ec4f14 --- /dev/null +++ b/components/camel-aws/camel-aws2-s3/src/test/java/org/apache/camel/component/aws2/s3/integration/S3GetObjectOperationIT.java @@ -0,0 +1,85 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.aws2.s3.integration; + +import org.apache.camel.*; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.aws2.s3.AWS2S3Constants; +import org.apache.camel.component.aws2.s3.AWS2S3Operations; +import org.apache.camel.component.mock.MockEndpoint; +import org.junit.jupiter.api.Test; +import software.amazon.awssdk.utils.IoUtils; + +import java.io.InputStream; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class S3GetObjectOperationIT extends Aws2S3Base { + + @EndpointInject + private ProducerTemplate template; + + @EndpointInject("mock:result") + private MockEndpoint result; + + @Test + public void sendIn() throws Exception { + result.expectedMessageCount(1); + + template.send("direct:putObject", new Processor() { + + @Override + public void process(Exchange exchange) { + exchange.getIn().setHeader(AWS2S3Constants.KEY, "camel.txt"); + exchange.getIn().setHeader(AWS2S3Constants.CONTENT_TYPE, "application/text"); + exchange.getIn().setBody("Camel rocks!"); + } + }); + + template.request("direct:getObject", new Processor() { + + @Override + public void process(Exchange exchange) { + exchange.getIn().setHeader(AWS2S3Constants.BUCKET_NAME, "mycamel"); + exchange.getIn().setHeader(AWS2S3Constants.KEY, "camel.txt"); + exchange.getIn().setHeader(AWS2S3Constants.S3_OPERATION, AWS2S3Operations.getObject); + } + }); + + Message resp = result.getExchanges().get(0).getMessage(); + assertEquals("Camel rocks!", new String(IoUtils.toByteArray(resp.getBody(InputStream.class)))); + assertEquals("application/text", resp.getHeader(AWS2S3Constants.CONTENT_TYPE)); + assertNotNull(resp.getHeader(AWS2S3Constants.E_TAG)); + MockEndpoint.assertIsSatisfied(context); + } + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + String awsEndpoint = "aws2-s3://mycamel?autoCreateBucket=true"; + + from("direct:putObject").to(awsEndpoint); + + from("direct:getObject").to(awsEndpoint).to("mock:result"); + + } + }; + } +}
