camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [2/2] camel git commit: CAMEL-7865 - Support to carry file attributes on file endpoint
Date Mon, 25 Jan 2016 12:59:16 GMT
CAMEL-7865 - Support to carry file attributes on file endpoint


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/64577e59
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/64577e59
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/64577e59

Branch: refs/heads/master
Commit: 64577e5931bcac7d286d4c4a1389aed7ff9a16e7
Parents: 712b335
Author: lburgazzoli <lburgazzoli@gmail.com>
Authored: Mon Jan 18 15:43:51 2016 +0100
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Mon Jan 25 13:55:17 2016 +0100

----------------------------------------------------------------------
 .../camel/component/file/FileConsumer.java      |  51 +++++++++-
 .../camel/component/file/GenericFile.java       |  13 +++
 .../component/file/GenericFileEndpoint.java     |  15 ++-
 .../FileConsumerExtendedAttributesTest.java     | 101 +++++++++++++++++++
 4 files changed, 178 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/64577e59/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java b/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java
index 11aaf05..f26d5a1 100644
--- a/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java
+++ b/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java
@@ -17,8 +17,15 @@
 package org.apache.camel.component.file;
 
 import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
 import java.util.Arrays;
+import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Map;
+import java.util.Set;
 
 import org.apache.camel.Processor;
 import org.apache.camel.util.FileUtil;
@@ -30,10 +37,18 @@ import org.apache.camel.util.ObjectHelper;
 public class FileConsumer extends GenericFileConsumer<File> {
 
     private String endpointPath;
+    private Set<String> extendedAttributes;
 
     public FileConsumer(GenericFileEndpoint<File> endpoint, Processor processor, GenericFileOperations<File>
operations) {
         super(endpoint, processor, operations);
         this.endpointPath = endpoint.getConfiguration().getDirectory();
+        this.extendedAttributes = new HashSet<>();
+
+        if (endpoint.getExtendedAttributes() != null) {
+            for (String attribute : endpoint.getExtendedAttributes().split(",")) {
+                extendedAttributes.add(attribute);
+            }
+        }
     }
 
     @Override
@@ -67,7 +82,7 @@ public class FileConsumer extends GenericFileConsumer<File> {
         }
         List<File> files = Arrays.asList(dirFiles);
 
-        for (File file : files) {
+        for (File file : dirFiles) {
             // check if we can continue polling in files
             if (!canPollMoreFiles(fileList)) {
                 return false;
@@ -96,6 +111,40 @@ public class FileConsumer extends GenericFileConsumer<File> {
                 if (depth >= endpoint.minDepth && isValidFile(gf, false, files))
{
                     log.trace("Adding valid file: {}", file);
                     // matched file so add
+                    if (!extendedAttributes.isEmpty()) {
+                        Path path = file.toPath();
+                        Map<String, Object> allAttributes = new HashMap<>();
+                        for (String attribute : extendedAttributes) {
+                            try {
+                                String prefix = null;
+                                if (attribute.endsWith(":*")) {
+                                    prefix = attribute.substring(0, attribute.length() -
1);
+                                } else if (attribute.equals("*")) {
+                                    prefix = "basic:";
+                                }
+
+                                if (ObjectHelper.isNotEmpty(prefix)) {
+                                    Map<String, Object> attributes = Files.readAttributes(path,
attribute);
+                                    if (attributes != null) {
+                                        for (Map.Entry<String, Object> entry : attributes.entrySet())
{
+                                            allAttributes.put(prefix + entry.getKey(), entry.getValue());
+                                        }
+                                    }
+                                } else if (!attribute.contains(":")) {
+                                    allAttributes.put("basic:" + attribute, Files.getAttribute(path,
attribute));
+                                } else {
+                                    allAttributes.put(attribute, Files.getAttribute(path,
attribute));
+                                }
+                            } catch (IOException e) {
+                                if (log.isDebugEnabled()) {
+                                    log.debug("Unable to read attribute {} on file {}", attribute,
file, e);
+                                }
+                            }
+                        }
+
+                        gf.setExtendedAttributes(allAttributes);
+                    }
+
                     fileList.add(gf);
                 }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/64577e59/camel-core/src/main/java/org/apache/camel/component/file/GenericFile.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/component/file/GenericFile.java b/camel-core/src/main/java/org/apache/camel/component/file/GenericFile.java
index 9c8bc46..2e3fee3 100644
--- a/camel-core/src/main/java/org/apache/camel/component/file/GenericFile.java
+++ b/camel-core/src/main/java/org/apache/camel/component/file/GenericFile.java
@@ -50,6 +50,7 @@ public class GenericFile<T> implements WrappedFile<T>  {
     private boolean absolute;
     private boolean directory;
     private String charset;
+    private Map<String, Object> extendedAttributes;
 
     public GenericFile() {
         this(false);
@@ -146,6 +147,10 @@ public class GenericFile<T> implements WrappedFile<T>  {
             message.setHeader("CamelFileAbsolute", isAbsolute());
             message.setHeader("CamelFileAbsolutePath", getAbsoluteFilePath());
 
+            if (extendedAttributes != null) {
+                message.setHeader("CamelFileExtendedAttributes", extendedAttributes);
+            }
+            
             if (probeContentType && file instanceof File) {
                 File f = (File) file;
                 Path path = f.toPath();
@@ -290,6 +295,14 @@ public class GenericFile<T> implements WrappedFile<T>  {
         this.charset = charset;
     }
 
+    public Map<String, Object> getExtendedAttributes() {
+        return extendedAttributes;
+    }
+
+    public void setExtendedAttributes(Map<String, Object> extendedAttributes) {
+        this.extendedAttributes = extendedAttributes;
+    }
+
     @Override
     public T getFile() {
         return file;

http://git-wip-us.apache.org/repos/asf/camel/blob/64577e59/camel-core/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java
b/camel-core/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java
index 52d2af1..0e20247 100644
--- a/camel-core/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java
+++ b/camel-core/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java
@@ -42,7 +42,6 @@ import org.apache.camel.spi.ExceptionHandler;
 import org.apache.camel.spi.FactoryFinder;
 import org.apache.camel.spi.IdempotentRepository;
 import org.apache.camel.spi.Language;
-import org.apache.camel.spi.Metadata;
 import org.apache.camel.spi.UriParam;
 import org.apache.camel.util.FileUtil;
 import org.apache.camel.util.IOHelper;
@@ -183,6 +182,8 @@ public abstract class GenericFileEndpoint<T> extends ScheduledPollEndpoint
imple
     protected GenericFileExclusiveReadLockStrategy<T> exclusiveReadLockStrategy;
     @UriParam(label = "consumer,advanced")
     protected ExceptionHandler onCompletionExceptionHandler;
+    @UriParam(label = "consumer,advanced")
+    protected String extendedAttributes;
 
     public GenericFileEndpoint() {
     }
@@ -1189,6 +1190,18 @@ public abstract class GenericFileEndpoint<T> extends ScheduledPollEndpoint
imple
         this.onCompletionExceptionHandler = onCompletionExceptionHandler;
     }
 
+    public String getExtendedAttributes() {
+        return extendedAttributes;
+    }
+
+    /**
+     * To define which file attributes of interest. Like posix:permissions,posix:owner,basic:lastAccessTime,
+     * it supports basic wildcard like posix:*, basic:lastAccessTime
+     */
+    public void setExtendedAttributes(String extendedAttributes) {
+        this.extendedAttributes = extendedAttributes;
+    }
+
     /**
      * Configures the given message with the file which sets the body to the
      * file object.

http://git-wip-us.apache.org/repos/asf/camel/blob/64577e59/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerExtendedAttributesTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerExtendedAttributesTest.java
b/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerExtendedAttributesTest.java
new file mode 100644
index 0000000..d505d9a
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerExtendedAttributesTest.java
@@ -0,0 +1,101 @@
+/**
+ * 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.file;
+
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.Map;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.Test;
+
+/**
+ * Unit test for consuming a batch of files (multiple files in one consume)
+ */
+public class FileConsumerExtendedAttributesTest extends ContextTestSupport {
+    private static final String ROOT = "target/extended-attributes";
+    private static final String FILE = "attributes.txt";
+
+    @Override
+    protected void setUp() throws Exception {
+        deleteDirectory(ROOT);
+
+        super.setUp();
+
+        Files.createFile(Paths.get(ROOT, "basic", FILE));
+        Files.createFile(Paths.get(ROOT, "basic-as-default", FILE));
+        Files.createFile(Paths.get(ROOT, "basic-as-default-with-filter", FILE));
+        Files.createFile(Paths.get(ROOT, "posix", FILE));
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() throws Exception {
+                fromF("file://%s/basic?extendedAttributes=basic:*", ROOT)
+                    .convertBodyTo(String.class)
+                    .to("mock:basic");
+                fromF("file://%s/basic-as-default?extendedAttributes=*", ROOT)
+                    .convertBodyTo(String.class)
+                    .to("mock:basic-as-default");
+                fromF("file://%s/basic-as-default-with-filter?extendedAttributes=size,lastModifiedTime,lastAccessTime",
ROOT)
+                    .convertBodyTo(String.class)
+                    .to("mock:basic-as-default-with-filter");
+                fromF("file://%s/posix?extendedAttributes=posix:*", ROOT)
+                    .convertBodyTo(String.class)
+                    .to("mock:posix");
+            }
+        };
+    }
+
+    @Test
+    public void testBasicAttributes() throws Exception {
+        testAttributes("mock:basic", "basic:");
+    }
+
+    @Test
+    public void testBasicAttributesAsDefault() throws Exception {
+        testAttributes("mock:basic-as-default", "basic:");
+    }
+
+    @Test
+    public void testBasicAttributesAsDefaultWithFilter() throws Exception {
+        testAttributes("mock:basic-as-default", "basic:");
+    }
+
+    @Test
+    public void testPosixAttributes() throws Exception {
+        testAttributes("mock:posix", "posix:");
+    }
+
+    private void testAttributes(String mockEndpoint, String prefix) throws Exception {
+        MockEndpoint mock = getMockEndpoint(mockEndpoint);
+        mock.expectedMessageCount(1);
+        mock.message(0).header("CamelFileExtendedAttributes").isNotNull();
+        mock.message(0).header("CamelFileExtendedAttributes").convertTo(Map.class);
+        assertMockEndpointsSatisfied();
+
+        Map<String, Object> attributes = mock.getExchanges().get(0).getIn().getHeader("CamelFileExtendedAttributes",
Map.class);
+        assertNotNull(attributes);
+        assertFalse(attributes.isEmpty());
+        for (Map.Entry<String, Object> entry : attributes.entrySet()) {
+            assertTrue(entry.getKey().startsWith(prefix));
+        }
+    }
+}
\ No newline at end of file


Mime
View raw message