This is an automated email from the ASF dual-hosted git repository.

acosentino pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 0fd74a5245caea9a0c626144b31c037c786ec2e0
Author: Andrea Cosentino <anco...@gmail.com>
AuthorDate: Tue Jul 2 09:29:03 2019 +0200

    CAMEL-13714 - Camel-AWS-S3: Add and option to specify the S3 Key also as 
endpoint parameter, adding test
---
 .../src/main/docs/aws-s3-component.adoc            |   3 +-
 .../aws/s3/S3ComponentKeyNameFileTest.java         | 150 +++++++++++++++++++++
 .../endpoint/dsl/S3EndpointBuilderFactory.java     |  10 ++
 .../s3/springboot/S3ComponentConfiguration.java    |  13 ++
 4 files changed, 175 insertions(+), 1 deletion(-)

diff --git a/components/camel-aws-s3/src/main/docs/aws-s3-component.adoc 
b/components/camel-aws-s3/src/main/docs/aws-s3-component.adoc
index 86da7b0..d907e47 100644
--- a/components/camel-aws-s3/src/main/docs/aws-s3-component.adoc
+++ b/components/camel-aws-s3/src/main/docs/aws-s3-component.adoc
@@ -81,7 +81,7 @@ with the following path and query parameters:
 |===
 
 
-==== Query Parameters (54 parameters):
+==== Query Parameters (55 parameters):
 
 
 [width="100%",cols="2,5,^1,2",options="header"]
@@ -111,6 +111,7 @@ with the following path and query parameters:
 | *exchangePattern* (consumer) | Sets the exchange pattern when the consumer 
creates an exchange. |  | ExchangePattern
 | *pollStrategy* (consumer) | A pluggable 
org.apache.camel.PollingConsumerPollingStrategy allowing you to provide your 
custom implementation to control error handling usually occurred during the 
poll operation before an Exchange have been created and being routed in Camel. 
|  | PollingConsumerPoll Strategy
 | *deleteAfterWrite* (producer) | Delete file object after the S3 file has 
been uploaded | false | boolean
+| *keyName* (producer) | Setting the key name for an element in the bucket 
through endpoint parameter |  | String
 | *lazyStartProducer* (producer) | Whether the producer should be started lazy 
(on the first message). By starting lazy you can use this to allow CamelContext 
and routes to startup in situations where a producer may otherwise fail during 
starting and cause the route to fail being started. By deferring this startup 
to be lazy then the startup failure can be handled during routing messages via 
Camel's routing error handlers. Beware that when the first message is processed 
then creating and [...]
 | *multiPartUpload* (producer) | If it is true, camel will upload the file 
with multi part format, the part size is decided by the option of partSize | 
false | boolean
 | *operation* (producer) | The operation to do in case the user don't want to 
do only an upload |  | S3Operations
diff --git 
a/components/camel-aws-s3/src/test/java/org/apache/camel/component/aws/s3/S3ComponentKeyNameFileTest.java
 
b/components/camel-aws-s3/src/test/java/org/apache/camel/component/aws/s3/S3ComponentKeyNameFileTest.java
new file mode 100644
index 0000000..d873514
--- /dev/null
+++ 
b/components/camel-aws-s3/src/test/java/org/apache/camel/component/aws/s3/S3ComponentKeyNameFileTest.java
@@ -0,0 +1,150 @@
+/*
+ * 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.aws.s3;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileWriter;
+import java.io.InputStream;
+import java.util.Map;
+
+import com.amazonaws.services.s3.model.PutObjectRequest;
+
+import org.apache.camel.BindToRegistry;
+import org.apache.camel.EndpointInject;
+import org.apache.camel.Exchange;
+import org.apache.camel.ExchangePattern;
+import org.apache.camel.Message;
+import org.apache.camel.Processor;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.apache.camel.util.FileUtil;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class S3ComponentKeyNameFileTest extends CamelTestSupport {
+
+    @BindToRegistry("amazonS3Client")
+    AmazonS3ClientMock client = new AmazonS3ClientMock();
+    
+    @EndpointInject("direct:startKeep")
+    ProducerTemplate templateKeep;
+
+    @EndpointInject("direct:startDelete")
+    ProducerTemplate templateDelete;
+
+    @EndpointInject("mock:result")
+    MockEndpoint result;
+
+    File testFile;
+
+    String getCamelBucket() {
+        return "mycamelbucket";
+    }
+
+    @Before
+    public void setup() throws Exception {
+        super.setUp();
+
+        testFile = FileUtil.createTempFile("test", "file", new 
File("target/tmp"));
+
+        FileWriter writer = new FileWriter(testFile);
+        writer.write("This is my bucket content.");
+        writer.close();
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        super.tearDown();
+
+        FileUtil.deleteFile(testFile);
+    }
+
+    @Test
+    public void sendFile() throws Exception {
+        result.expectedMessageCount(1);
+
+        Exchange exchange = templateKeep.send("direct:startKeep", 
ExchangePattern.InOnly, new Processor() {
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setBody(new FileInputStream(testFile));
+            }
+        });
+
+        assertMockEndpointsSatisfied();
+
+        assertResultExchange(result.getExchanges().get(0), true);
+
+        PutObjectRequest putObjectRequest = client.putObjectRequests.get(0);
+        assertEquals(getCamelBucket(), putObjectRequest.getBucketName());
+
+        assertResponseMessage(exchange.getIn());
+
+        assertFileExists(testFile.getAbsolutePath());
+    }
+
+    void assertResultExchange(Exchange resultExchange, boolean delete) {
+        assertIsInstanceOf(InputStream.class, 
resultExchange.getIn().getBody());
+
+        if (!delete) {
+            // assert on the file content only in case the "deleteAfterWrite"
+            // option is NOT enabled
+            // in which case we would still have the file and thereby could
+            // assert on it's content
+            assertEquals("This is my bucket content.", 
resultExchange.getIn().getBody(String.class));
+        }
+
+        assertEquals(getCamelBucket(), 
resultExchange.getIn().getHeader(S3Constants.BUCKET_NAME));
+        assertEquals("CamelUnitTest", 
resultExchange.getIn().getHeader(S3Constants.KEY));
+        assertNull(resultExchange.getIn().getHeader(S3Constants.VERSION_ID)); 
// not
+                                                                              
// enabled
+                                                                              
// on
+                                                                              
// this
+                                                                              
// bucket
+        
assertNull(resultExchange.getIn().getHeader(S3Constants.LAST_MODIFIED));
+        assertNull(resultExchange.getIn().getHeader(S3Constants.E_TAG));
+        assertNull(resultExchange.getIn().getHeader(S3Constants.CONTENT_TYPE));
+        
assertNull(resultExchange.getIn().getHeader(S3Constants.CONTENT_ENCODING));
+        assertEquals(0L, 
resultExchange.getIn().getHeader(S3Constants.CONTENT_LENGTH));
+        
assertNull(resultExchange.getIn().getHeader(S3Constants.CONTENT_DISPOSITION));
+        assertNull(resultExchange.getIn().getHeader(S3Constants.CONTENT_MD5));
+        
assertNull(resultExchange.getIn().getHeader(S3Constants.CACHE_CONTROL));
+        
assertNotNull(resultExchange.getIn().getHeader(S3Constants.USER_METADATA));
+        assertEquals(0, 
resultExchange.getIn().getHeader(S3Constants.S3_HEADERS, Map.class).size());
+    }
+
+    void assertResponseMessage(Message message) {
+        assertEquals("3a5c8b1ad448bca04584ecb55b836264", 
message.getHeader(S3Constants.E_TAG));
+        assertNull(message.getHeader(S3Constants.VERSION_ID));
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                String awsEndpoint = "aws-s3://" + getCamelBucket() + 
"?amazonS3Client=#amazonS3Client";
+
+                from("direct:startKeep").to(awsEndpoint + 
"&deleteAfterWrite=false&keyName=CamelUnitTest");
+
+                from(awsEndpoint + "&maxMessagesPerPoll=5").to("mock:result");
+            }
+        };
+    }
+}
diff --git 
a/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/S3EndpointBuilderFactory.java
 
b/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/S3EndpointBuilderFactory.java
index 8643694..2ec8de0 100644
--- 
a/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/S3EndpointBuilderFactory.java
+++ 
b/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/S3EndpointBuilderFactory.java
@@ -1159,6 +1159,16 @@ public interface S3EndpointBuilderFactory {
             return this;
         }
         /**
+         * Setting the key name for an element in the bucket through endpoint
+         * parameter.
+         * The option is a <code>java.lang.String</code> type.
+         * @group producer
+         */
+        default S3EndpointProducerBuilder keyName(String keyName) {
+            setProperty("keyName", keyName);
+            return this;
+        }
+        /**
          * Whether the producer should be started lazy (on the first message).
          * By starting lazy you can use this to allow CamelContext and routes 
to
          * startup in situations where a producer may otherwise fail during
diff --git 
a/platforms/spring-boot/components-starter/camel-aws-s3-starter/src/main/java/org/apache/camel/component/aws/s3/springboot/S3ComponentConfiguration.java
 
b/platforms/spring-boot/components-starter/camel-aws-s3-starter/src/main/java/org/apache/camel/component/aws/s3/springboot/S3ComponentConfiguration.java
index 3517759..5e629d7 100644
--- 
a/platforms/spring-boot/components-starter/camel-aws-s3-starter/src/main/java/org/apache/camel/component/aws/s3/springboot/S3ComponentConfiguration.java
+++ 
b/platforms/spring-boot/components-starter/camel-aws-s3-starter/src/main/java/org/apache/camel/component/aws/s3/springboot/S3ComponentConfiguration.java
@@ -279,6 +279,11 @@ public class S3ComponentConfiguration
          * Setting the autocreation of the bucket
          */
         private Boolean autoCreateBucket = true;
+        /**
+         * Setting the key name for an element in the bucket through endpoint
+         * parameter
+         */
+        private String keyName;
 
         public Long getPartSize() {
             return partSize;
@@ -537,5 +542,13 @@ public class S3ComponentConfiguration
         public void setAutoCreateBucket(Boolean autoCreateBucket) {
             this.autoCreateBucket = autoCreateBucket;
         }
+
+        public String getKeyName() {
+            return keyName;
+        }
+
+        public void setKeyName(String keyName) {
+            this.keyName = keyName;
+        }
     }
 }
\ No newline at end of file

Reply via email to