camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r1296778 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/component/file/ camel-core/src/test/java/org/apache/camel/component/file/ components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/ components/camel-ftp/...
Date Sun, 04 Mar 2012 10:43:46 GMT
Author: davsclaus
Date: Sun Mar  4 10:43:45 2012
New Revision: 1296778

URL: http://svn.apache.org/viewvc?rev=1296778&view=rev
Log:
CAMEL-4602: GenericFileFilter now supports directories. Thanks to Michael for partly patch.

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerDirectoryFilterTest.java
    camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpRemoteFileFilterDirectoryTest.java
      - copied, changed from r1296761, camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpRemoteFileFilterTest.java
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/AntPathMatcherGenericFileFilter.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFile.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileConsumer.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerFileFilterTest.java
    camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java
    camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConsumer.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/AntPathMatcherGenericFileFilter.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/AntPathMatcherGenericFileFilter.java?rev=1296778&r1=1296777&r2=1296778&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/AntPathMatcherGenericFileFilter.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/AntPathMatcherGenericFileFilter.java
Sun Mar  4 10:43:45 2012
@@ -36,6 +36,11 @@ public class AntPathMatcherGenericFileFi
     }
 
     public boolean accept(GenericFile<T> file) {
+        // directories should always be accepted by ANT path matcher
+        if (file.isDirectory()) {
+            return true;
+        }
+
         String path = file.getRelativeFilePath();
         return filter.acceptPathName(path);
     }

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java?rev=1296778&r1=1296777&r2=1296778&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java
Sun Mar  4 10:43:45 2012
@@ -125,6 +125,7 @@ public class FileConsumer extends Generi
         answer.setFile(file);
         answer.setFileNameOnly(file.getName());
         answer.setFileLength(file.length());
+        answer.setDirectory(file.isDirectory());
         // must use FileUtil.isAbsolute to have consistent check for whether the file is
         // absolute or not. As windows do not consider \ paths as absolute where as all
         // other OS platforms will consider \ as absolute. The logic in Camel mandates

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFile.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFile.java?rev=1296778&r1=1296777&r2=1296778&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFile.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFile.java
Sun Mar  4 10:43:45 2012
@@ -44,6 +44,7 @@ public class GenericFile<T> implements W
     private T file;
     private GenericFileBinding<T> binding;
     private boolean absolute;
+    private boolean directory;
 
     public char getFileSeparator() {
         return File.separatorChar;
@@ -65,6 +66,7 @@ public class GenericFile<T> implements W
         }
         result.setEndpointPath(source.getEndpointPath());
         result.setAbsolute(source.isAbsolute());
+        result.setDirectory(source.isDirectory());
         result.setAbsoluteFilePath(source.getAbsoluteFilePath());
         result.setRelativeFilePath(source.getRelativeFilePath());
         result.setFileName(source.getFileName());
@@ -333,6 +335,14 @@ public class GenericFile<T> implements W
         this.fileNameOnly = fileNameOnly;
     }
 
+    public boolean isDirectory() {
+        return directory;
+    }
+
+    public void setDirectory(boolean directory) {
+        this.directory = directory;
+    }
+
     /**
      * Fixes the path separator to be according to the protocol
      */

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileConsumer.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileConsumer.java?rev=1296778&r1=1296777&r2=1296778&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileConsumer.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileConsumer.java
Sun Mar  4 10:43:45 2012
@@ -391,11 +391,6 @@ public abstract class GenericFileConsume
             return false;
         }
 
-        // directories so far is always regarded as matched (matching on the name is only
for files)
-        if (isDirectory) {
-            return true;
-        }
-
         if (endpoint.getFilter() != null) {
             if (!endpoint.getFilter().accept(file)) {
                 return false;
@@ -408,6 +403,11 @@ public abstract class GenericFileConsume
             }
         }
 
+        // directories are regarded as matched if filter accepted them
+        if (isDirectory) {
+            return true;
+        }
+
         if (ObjectHelper.isNotEmpty(endpoint.getExclude())) {
             if (name.matches(endpoint.getExclude())) {
                 return false;

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerDirectoryFilterTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerDirectoryFilterTest.java?rev=1296778&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerDirectoryFilterTest.java
(added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerDirectoryFilterTest.java
Sun Mar  4 10:43:45 2012
@@ -0,0 +1,102 @@
+/**
+ * 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.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.impl.JndiRegistry;
+
+/**
+ * Unit test for the file filter option using directories
+ */
+public class FileConsumerDirectoryFilterTest extends ContextTestSupport {
+
+    private String fileUrl = "file://target/directoryfilter/?recursive=true&filter=#myFilter";
+    private static final List<String> names = new ArrayList<String>();
+
+    @Override
+    protected JndiRegistry createRegistry() throws Exception {
+        JndiRegistry jndi = super.createRegistry();
+        jndi.bind("myFilter", new MyDirectoryFilter<Object>());
+        return jndi;
+    }
+
+    @Override
+    protected void setUp() throws Exception {
+        deleteDirectory("target/directoryfilter");
+        super.setUp();
+    }
+
+    public void testFilterFilesWithARegularFile() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+        mock.expectedBodiesReceived("Hello World");
+
+        template.sendBodyAndHeader("file:target/directoryfilter/skipDir/", "This is a file
to be filtered",
+                Exchange.FILE_NAME, "skipme.txt");
+
+        template.sendBodyAndHeader("file:target/directoryfilter/skipDir2/", "This is a file
to be filtered",
+                Exchange.FILE_NAME, "skipme.txt");
+
+        template.sendBodyAndHeader("file:target/directoryfilter/okDir/", "Hello World",
+                Exchange.FILE_NAME, "hello.txt");
+
+        mock.assertIsSatisfied();
+
+        // check names
+        assertEquals(4, names.size());
+        Collections.sort(names);
+        assertEquals("okDir", names.get(0));
+        // windows or unix paths
+        assertTrue(names.get(0), names.get(1).equals("okDir/hello.txt") || names.get(1).equals("okDir\\hello.txt"));
+        assertEquals("skipDir", names.get(2));
+        assertEquals("skipDir2", names.get(3));
+    }
+
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() throws Exception {
+                from(fileUrl).convertBodyTo(String.class).to("mock:result");
+            }
+        };
+    }
+
+    // START SNIPPET: e1
+    public class MyDirectoryFilter<T> implements GenericFileFilter<T> {
+
+        public boolean accept(GenericFile<T> file) {
+            // remember the name due unit testing (should not be needed in regular use-cases)
+            names.add(file.getFileName());
+            
+            // we dont accept any files within directory starting with skip in the name
+            if (file.isDirectory() && file.getFileName().startsWith("skip")) {
+                return false;
+            }
+
+            return true;
+        }
+
+    }
+    // END SNIPPET: e1
+
+}
\ No newline at end of file

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerFileFilterTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerFileFilterTest.java?rev=1296778&r1=1296777&r2=1296778&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerFileFilterTest.java
(original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerFileFilterTest.java
Sun Mar  4 10:43:45 2012
@@ -77,9 +77,13 @@ public class FileConsumerFileFilterTest 
 
     // START SNIPPET: e1
     public class MyFileFilter<T> implements GenericFileFilter<T> {
-        public boolean accept(GenericFile<T> pathname) {
+        public boolean accept(GenericFile<T> file) {
+            // we want all directories
+            if (file.isDirectory()) {
+                return true;
+            }
             // we dont accept any files starting with skip in the name
-            return !pathname.getFileName().startsWith("skip");
+            return !file.getFileName().startsWith("skip");
         }
     }
     // END SNIPPET: e1

Modified: camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java?rev=1296778&r1=1296777&r2=1296778&view=diff
==============================================================================
--- camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java
(original)
+++ camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java
Sun Mar  4 10:43:45 2012
@@ -142,6 +142,7 @@ public class FtpConsumer extends RemoteF
         answer.setFile(file);
         answer.setFileNameOnly(file.getName());
         answer.setFileLength(file.getSize());
+        answer.setDirectory(file.isDirectory());
         if (file.getTimestamp() != null) {
             answer.setLastModified(file.getTimestamp().getTimeInMillis());
         }

Modified: camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConsumer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConsumer.java?rev=1296778&r1=1296777&r2=1296778&view=diff
==============================================================================
--- camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConsumer.java
(original)
+++ camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConsumer.java
Sun Mar  4 10:43:45 2012
@@ -143,6 +143,7 @@ public class SftpConsumer extends Remote
         answer.setFileLength(file.getAttrs().getSize());
         answer.setLastModified(file.getAttrs().getMTime() * 1000L);
         answer.setHostname(((RemoteFileConfiguration) endpoint.getConfiguration()).getHost());
+        answer.setDirectory(file.getAttrs().isDir());
 
         // absolute or relative path
         boolean absolute = FileUtil.hasLeadingSeparator(absolutePath);

Copied: camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpRemoteFileFilterDirectoryTest.java
(from r1296761, camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpRemoteFileFilterTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpRemoteFileFilterDirectoryTest.java?p2=camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpRemoteFileFilterDirectoryTest.java&p1=camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpRemoteFileFilterTest.java&r1=1296761&r2=1296778&rev=1296778&view=diff
==============================================================================
--- camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpRemoteFileFilterTest.java
(original)
+++ camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpRemoteFileFilterDirectoryTest.java
Sun Mar  4 10:43:45 2012
@@ -27,10 +27,10 @@ import org.junit.Test;
 /**
  * Unit test to verify FTP filter option.
  */
-public class FromFtpRemoteFileFilterTest extends FtpServerTestSupport {
+public class FromFtpRemoteFileFilterDirectoryTest extends FtpServerTestSupport {
 
     private String getFtpUrl() {
-        return "ftp://admin@localhost:" + getPort() + "/filefilter?password=admin&filter=#myFilter";
+        return "ftp://admin@localhost:" + getPort() + "/filefilter?password=admin&recursive=true&filter=#myFilter";
     }
 
     @Override
@@ -55,8 +55,8 @@ public class FromFtpRemoteFileFilterTest
         }
 
         MockEndpoint mock = getMockEndpoint("mock:result");
-        mock.expectedMessageCount(2);
-        mock.expectedBodiesReceivedInAnyOrder("Report 1", "Report 2");
+        mock.expectedMessageCount(1);
+        mock.expectedBodiesReceived("Hello World");
         
         mock.assertIsSatisfied();
     }
@@ -64,10 +64,9 @@ public class FromFtpRemoteFileFilterTest
     private void prepareFtpServer() throws Exception {
         // prepares the FTP Server by creating files on the server that we want to unit
         // test that we can pool        
-        sendFile(getFtpUrl(), "Hello World", "hello.txt");
-        sendFile(getFtpUrl(), "Report 1", "report1.txt");
-        sendFile(getFtpUrl(), "Bye World", "bye.txt");
-        sendFile(getFtpUrl(), "Report 2", "report2.txt");
+        sendFile(getFtpUrl(), "This is a file to be filtered", "skipDir/skipme.txt");
+        sendFile(getFtpUrl(), "This is a file to be filtered", "skipDir2/skipme.txt");
+        sendFile(getFtpUrl(), "Hello World", "okDir/hello.txt");
     }
 
     protected RouteBuilder createRouteBuilder() throws Exception {
@@ -82,8 +81,12 @@ public class FromFtpRemoteFileFilterTest
     public class MyFileFilter<T> implements GenericFileFilter<T> {
 
         public boolean accept(GenericFile<T> file) {
-            // we only want report files 
-            return file.getFileName().startsWith("report");
+            // we dont accept any files within directory starting with skip in the name
+            if (file.isDirectory() && file.getFileName().startsWith("skip")) {
+                return false;
+            }
+
+            return true;
         }
     }
     // END SNIPPET: e1



Mime
View raw message