You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2016/01/19 19:49:20 UTC

[2/3] camel git commit: CAMEL-9524: file consumer - Allow to turn on|off probe content type

CAMEL-9524: file consumer - Allow to turn on|off probe content type


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

Branch: refs/heads/master
Commit: 5f58e203eba001a77e90bf035169406cda3cf158
Parents: 85ad128
Author: Claus Ibsen <da...@apache.org>
Authored: Tue Jan 19 19:17:52 2016 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Tue Jan 19 19:17:52 2016 +0100

----------------------------------------------------------------------
 .../apache/camel/component/file/FileConsumer.java | 18 ++++++++++++++++--
 .../apache/camel/component/file/FileEndpoint.java | 16 ++++++++++++++++
 .../apache/camel/component/file/GenericFile.java  | 16 +++++++++++++---
 .../camel/component/file/FileConfigureTest.java   |  2 +-
 .../component/file/GenericFileMessageTest.java    |  8 +++-----
 .../camel/language/FileLanguageExtSingleTest.java |  2 +-
 .../apache/camel/language/FileLanguageTest.java   |  4 ++--
 7 files changed, 52 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/5f58e203/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 3d55e70..11aaf05 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
@@ -80,7 +80,7 @@ public class FileConsumer extends GenericFileConsumer<File> {
             }
 
             // creates a generic file
-            GenericFile<File> gf = asGenericFile(endpointPath, file, getEndpoint().getCharset());
+            GenericFile<File> gf = asGenericFile(endpointPath, file, getEndpoint().getCharset(), getEndpoint().isProbeContentType());
 
             if (file.isDirectory()) {
                 if (endpoint.isRecursive() && depth < endpoint.getMaxDepth() && isValidFile(gf, true, files)) {
@@ -124,9 +124,23 @@ public class FileConsumer extends GenericFileConsumer<File> {
      * @param endpointPath the starting directory the endpoint was configured with
      * @param file the source file
      * @return wrapped as a GenericFile
+     * @deprecated use {@link #asGenericFile(String, File, String, boolean)}
      */
+    @Deprecated
     public static GenericFile<File> asGenericFile(String endpointPath, File file, String charset) {
-        GenericFile<File> answer = new GenericFile<File>();
+        return asGenericFile(endpointPath, file, charset, false);
+    }
+
+    /**
+     * Creates a new GenericFile<File> based on the given file.
+     *
+     * @param endpointPath the starting directory the endpoint was configured with
+     * @param file the source file
+     * @param probeContentType whether to probe the content type of the file or not
+     * @return wrapped as a GenericFile
+     */
+    public static GenericFile<File> asGenericFile(String endpointPath, File file, String charset, boolean probeContentType) {
+        GenericFile<File> answer = new GenericFile<File>(probeContentType);
         // use file specific binding
         answer.setBinding(new FileBinding());
 

http://git-wip-us.apache.org/repos/asf/camel/blob/5f58e203/camel-core/src/main/java/org/apache/camel/component/file/FileEndpoint.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/component/file/FileEndpoint.java b/camel-core/src/main/java/org/apache/camel/component/file/FileEndpoint.java
index 94802f6..2d51753 100644
--- a/camel-core/src/main/java/org/apache/camel/component/file/FileEndpoint.java
+++ b/camel-core/src/main/java/org/apache/camel/component/file/FileEndpoint.java
@@ -18,9 +18,11 @@ package org.apache.camel.component.file;
 
 import java.io.File;
 import java.io.FileNotFoundException;
+import java.nio.file.Files;
 
 import org.apache.camel.Component;
 import org.apache.camel.Exchange;
+import org.apache.camel.Message;
 import org.apache.camel.Processor;
 import org.apache.camel.processor.idempotent.MemoryIdempotentRepository;
 import org.apache.camel.spi.Metadata;
@@ -46,6 +48,8 @@ public class FileEndpoint extends GenericFileEndpoint<File> {
     private boolean renameUsingCopy;
     @UriParam(label = "producer,advanced", defaultValue = "true")
     private boolean forceWrites = true;
+    @UriParam(label = "consumer,advanced")
+    private boolean probeContentType;
 
     public FileEndpoint() {
         // use marker file as default exclusive read locks
@@ -209,4 +213,16 @@ public class FileEndpoint extends GenericFileEndpoint<File> {
     public void setForceWrites(boolean forceWrites) {
         this.forceWrites = forceWrites;
     }
+
+    public boolean isProbeContentType() {
+        return probeContentType;
+    }
+
+    /**
+     * Whether to enable probing of the content type. If enable then the consumer uses {@link Files#probeContentType(java.nio.file.Path)} to
+     * determine the content-type of the file, and store that as a header with key {@link Exchange#FILE_CONTENT_TYPE} on the {@link Message}.
+     */
+    public void setProbeContentType(boolean probeContentType) {
+        this.probeContentType = probeContentType;
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/5f58e203/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 e517550..9c8bc46 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
@@ -35,6 +35,8 @@ import org.slf4j.LoggerFactory;
 public class GenericFile<T> implements WrappedFile<T>  {
     private static final Logger LOG = LoggerFactory.getLogger(GenericFile.class);
 
+    private final boolean probeContentType;
+
     private String copyFromAbsoluteFilePath;
     private String endpointPath;
     private String fileName;
@@ -49,6 +51,14 @@ public class GenericFile<T> implements WrappedFile<T>  {
     private boolean directory;
     private String charset;
 
+    public GenericFile() {
+        this(false);
+    }
+
+    public GenericFile(boolean probeContentType) {
+        this.probeContentType = probeContentType;
+    }
+
     public char getFileSeparator() {
         return File.separatorChar;
     }
@@ -135,13 +145,13 @@ public class GenericFile<T> implements WrappedFile<T>  {
             message.setHeader(Exchange.FILE_NAME_CONSUMED, getFileName());
             message.setHeader("CamelFileAbsolute", isAbsolute());
             message.setHeader("CamelFileAbsolutePath", getAbsoluteFilePath());
-            
-            if (file instanceof File) {
+
+            if (probeContentType && file instanceof File) {
                 File f = (File) file;
                 Path path = f.toPath();
                 try {
                     message.setHeader(Exchange.FILE_CONTENT_TYPE, Files.probeContentType(path));
-                } catch (Exception ex) {
+                } catch (Throwable e) {
                     // just ignore the exception
                 }
             }

http://git-wip-us.apache.org/repos/asf/camel/blob/5f58e203/camel-core/src/test/java/org/apache/camel/component/file/FileConfigureTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/component/file/FileConfigureTest.java b/camel-core/src/test/java/org/apache/camel/component/file/FileConfigureTest.java
index 3e1f8c0..89939a6 100644
--- a/camel-core/src/test/java/org/apache/camel/component/file/FileConfigureTest.java
+++ b/camel-core/src/test/java/org/apache/camel/component/file/FileConfigureTest.java
@@ -122,7 +122,7 @@ public class FileConfigureTest extends ContextTestSupport {
             assertDirectoryEquals("For uri: " + endpointUri + " the file is not equal", expectedPath, path);
 
             file = new File(expectedPath + (expectedPath.endsWith(File.separator) ? "" : File.separator) + EXPECT_FILE);
-            GenericFile<File> consumedFile = FileConsumer.asGenericFile(endpoint.getFile().getPath(), file, null);
+            GenericFile<File> consumedFile = FileConsumer.asGenericFile(endpoint.getFile().getPath(), file, null, false);
 
             assertEquals(EXPECT_FILE, consumedFile.getRelativeFilePath());
         }

http://git-wip-us.apache.org/repos/asf/camel/blob/5f58e203/camel-core/src/test/java/org/apache/camel/component/file/GenericFileMessageTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/component/file/GenericFileMessageTest.java b/camel-core/src/test/java/org/apache/camel/component/file/GenericFileMessageTest.java
index 5382622..6108029 100644
--- a/camel-core/src/test/java/org/apache/camel/component/file/GenericFileMessageTest.java
+++ b/camel-core/src/test/java/org/apache/camel/component/file/GenericFileMessageTest.java
@@ -28,7 +28,7 @@ public class GenericFileMessageTest extends ContextTestSupport {
         GenericFileMessage<File> message = new GenericFileMessage<File>(); 
         assertStringContains(message.toString(), "org.apache.camel.component.file.GenericFileMessage@");
         
-        GenericFile<File> file = new GenericFile<File>();
+        GenericFile<File> file = new GenericFile<File>(true);
         file.setFileName("target/test.txt");
         file.setFile(new File("target/test.txt"));
         message = new GenericFileMessage<File>(file); 
@@ -37,13 +37,11 @@ public class GenericFileMessageTest extends ContextTestSupport {
     }
     
     public void testGenericFileContentType() throws Exception {
-        GenericFileMessage<File> message = new GenericFileMessage<File>(); 
-        
-        GenericFile<File> file = new GenericFile<File>();
+        GenericFile<File> file = new GenericFile<File>(true);
         file.setEndpointPath("target");
         file.setFileName("target");
         file.setFile(new File("target/camel-core-test.log"));
-        message = new GenericFileMessage<File>(file); 
+        GenericFileMessage<File> message = new GenericFileMessage<File>(file);
         file.populateHeaders(message);
         assertEquals("Get a wrong file content type", "txt", message.getHeader(Exchange.FILE_CONTENT_TYPE));
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/5f58e203/camel-core/src/test/java/org/apache/camel/language/FileLanguageExtSingleTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/language/FileLanguageExtSingleTest.java b/camel-core/src/test/java/org/apache/camel/language/FileLanguageExtSingleTest.java
index ccdc314..3d82dfe 100644
--- a/camel-core/src/test/java/org/apache/camel/language/FileLanguageExtSingleTest.java
+++ b/camel-core/src/test/java/org/apache/camel/language/FileLanguageExtSingleTest.java
@@ -54,7 +54,7 @@ public class FileLanguageExtSingleTest extends LanguageTestSupport {
 
         // get the file handle
         file = new File("target/filelanguage/test/bye.def.txt");
-        GenericFile<File> gf = FileConsumer.asGenericFile("target/filelanguage", file, null);
+        GenericFile<File> gf = FileConsumer.asGenericFile("target/filelanguage", file, null, false);
 
         FileEndpoint endpoint = getMandatoryEndpoint(uri, FileEndpoint.class);
 

http://git-wip-us.apache.org/repos/asf/camel/blob/5f58e203/camel-core/src/test/java/org/apache/camel/language/FileLanguageTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/language/FileLanguageTest.java b/camel-core/src/test/java/org/apache/camel/language/FileLanguageTest.java
index 1e98e1d..b108a60 100644
--- a/camel-core/src/test/java/org/apache/camel/language/FileLanguageTest.java
+++ b/camel-core/src/test/java/org/apache/camel/language/FileLanguageTest.java
@@ -169,7 +169,7 @@ public class FileLanguageTest extends LanguageTestSupport {
         file = new File("target/filelanguage/test/bigfile.tar.gz");
 
         String uri = "file://target/filelanguage?fileExist=Override";
-        GenericFile<File> gf = FileConsumer.asGenericFile("target/filelanguage", file, null);
+        GenericFile<File> gf = FileConsumer.asGenericFile("target/filelanguage", file, null, false);
 
         FileEndpoint endpoint = getMandatoryEndpoint(uri, FileEndpoint.class);
 
@@ -188,7 +188,7 @@ public class FileLanguageTest extends LanguageTestSupport {
 
         // get the file handle
         file = new File("target/filelanguage/test/hello.txt");
-        GenericFile<File> gf = FileConsumer.asGenericFile("target/filelanguage", file, null);
+        GenericFile<File> gf = FileConsumer.asGenericFile("target/filelanguage", file, null, false);
 
         FileEndpoint endpoint = getMandatoryEndpoint(uri, FileEndpoint.class);