camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject svn commit: r566940 - in /activemq/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/
Date Fri, 17 Aug 2007 06:50:37 GMT
Author: jstrachan
Date: Thu Aug 16 23:50:36 2007
New Revision: 566940

URL: http://svn.apache.org/viewvc?view=rev&rev=566940
Log:
applied patch for CAMEL-109 from Aaron Crickenberger with huge thanks! Incidentally I figured
this should be the default behaviour - so I've enabled the preservation of file names by default,
which makes lots of sense

Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileProducer.java
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConfigureTest.java
    activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java
    activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpProducer.java
    activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileProducer.java
    activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConsumer.java
    activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpProducer.java

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java?view=diff&rev=566940&r1=566939&r2=566940
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java
Thu Aug 16 23:50:36 2007
@@ -33,6 +33,7 @@
     private boolean recursive = true;
     private String regexPattern = "";
     private long lastPollTime;
+    private boolean preserveFileName = true;
 
     public FileConsumer(final FileEndpoint endpoint, Processor processor) {
         super(endpoint, processor);
@@ -81,6 +82,14 @@
             FileProcessStrategy processStrategy = endpoint.getFileStrategy();
             FileExchange exchange = endpoint.createExchange(file);
 
+            if (isPreserveFileName()) {
+                String relativePath = file.getPath().substring(endpoint.getFile().getPath().length());
+                if (relativePath.startsWith(File.separator)) {
+                	relativePath = relativePath.substring(1);
+                }
+                exchange.getIn().setHeader(FileComponent.HEADER_FILE_NAME, relativePath);
+            }
+            
             try {
                 if (LOG.isDebugEnabled()) {
                     LOG.debug("About to process file:  " + file + " using exchange: " + exchange);
@@ -164,4 +173,12 @@
     public void setRegexPattern(String regexPattern) {
         this.regexPattern = regexPattern;
     }
+
+	public boolean isPreserveFileName() {
+		return preserveFileName;
+	}
+
+	public void setPreserveFileName(boolean preserveFileName) {
+		this.preserveFileName = preserveFileName;
+	}
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileProducer.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileProducer.java?view=diff&rev=566940&r1=566939&r2=566940
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileProducer.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileProducer.java
Thu Aug 16 23:50:36 2007
@@ -17,6 +17,7 @@
 package org.apache.camel.component.file;
 
 import org.apache.camel.Exchange;
+import org.apache.camel.Message;
 import org.apache.camel.Producer;
 import org.apache.camel.impl.DefaultProducer;
 import org.apache.camel.util.ExchangeHelper;
@@ -59,7 +60,7 @@
 
     public void process(FileExchange exchange) throws Exception {
         InputStream in = ExchangeHelper.getMandatoryInBody(exchange, InputStream.class);
-        File file = createFileName(exchange);
+        File file = createFileName(exchange.getIn());
         buildDirectory(file);
         if (LOG.isDebugEnabled()) {
             LOG.debug("About to write to: " + file + " from exchange: " + exchange);
@@ -144,26 +145,23 @@
         */
     }
 
-    protected File createFileName(FileExchange exchange) {
-        String fileName = exchange.getIn().getMessageId();
-
+    protected File createFileName(Message message) {
+        File answer;
         File endpointFile = endpoint.getFile();
-        String name = exchange.getIn().getHeader(FileComponent.HEADER_FILE_NAME, String.class);
-        if (name != null) {
-            File answer = new File(endpointFile, name);
-            if (answer.isDirectory()) {
-                return new File(answer, fileName);
-            }
-            else {
-                return answer;
+        String name = message.getHeader(FileComponent.HEADER_FILE_NAME, String.class);
+        if (endpointFile.isDirectory()) {
+            if (name != null) {
+                answer = new File(endpointFile, name);
+                if (answer.isDirectory()) {
+                    answer = new File(answer, message.getMessageId());
+                }
+            } else {
+                answer = new File(endpointFile, message.getMessageId());
             }
+        } else {
+            answer = endpointFile;
         }
-        if (endpointFile != null && endpointFile.isDirectory()) {
-            return new File(endpointFile, fileName);
-        }
-        else {
-            return new File(fileName);
-        }
+        return answer;
     }
 
     private void buildDirectory(File file) {

Modified: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConfigureTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConfigureTest.java?view=diff&rev=566940&r1=566939&r2=566940
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConfigureTest.java
(original)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConfigureTest.java
Thu Aug 16 23:50:36 2007
@@ -31,6 +31,13 @@
         assertFileEndpoint("file://target/foo/bar?delete=true", EXPECT_PATH);
         assertFileEndpoint("file:target/foo/bar?delete=true", EXPECT_PATH);
         assertFileEndpoint("file:target/foo/bar", EXPECT_PATH);
+        assertFileEndpoint("file://target/foo/bar/", EXPECT_PATH);
+        assertFileEndpoint("file://target/foo/bar/?delete=true", EXPECT_PATH);
+        assertFileEndpoint("file:target/foo/bar/?delete=true", EXPECT_PATH);
+        assertFileEndpoint("file:target/foo/bar/", EXPECT_PATH);
+        assertFileEndpoint("file:/target/foo/bar/", File.separator + EXPECT_PATH);      
 
+        assertFileEndpoint("file:/", File.separator);
+        assertFileEndpoint("file:///", File.separator);
     }
 
     private void assertFileEndpoint(String endpointUri, String expectedPath) {

Modified: activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java?view=diff&rev=566940&r1=566939&r2=566940
==============================================================================
--- activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java
(original)
+++ activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java
Thu Aug 16 23:50:36 2007
@@ -21,6 +21,7 @@
 import java.util.concurrent.ScheduledExecutorService;
 
 import org.apache.camel.Processor;
+import org.apache.camel.component.file.FileComponent;
 import org.apache.commons.net.ftp.FTPClient;
 import org.apache.commons.net.ftp.FTPFile;
 
@@ -30,6 +31,7 @@
     private long lastPollTime;
     private final FtpEndpoint endpoint;
     private FTPClient client;
+    private boolean setNames = false;
 
     public FtpConsumer(FtpEndpoint endpoint, Processor processor, FTPClient client) {
         super(endpoint, processor);
@@ -76,18 +78,28 @@
 
     private void pollFile(FTPFile ftpFile) throws Exception {
         if (ftpFile.getTimestamp().getTimeInMillis() > lastPollTime) { // TODO
-                                                                        // do we
-                                                                        // need
-                                                                        // to
-                                                                        // adjust
-                                                                        // the
-                                                                        // TZ?
-                                                                        // can
-                                                                        // we?
+                                                                       // do we
+                                                                       // need
+                                                                       // to
+                                                                       // adjust
+                                                                       // the
+                                                                       // TZ?
+                                                                       // can
+                                                                       // we?
             if (isMatched(ftpFile)) {
                 final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                 client.retrieveFile(ftpFile.getName(), byteArrayOutputStream);
-                getProcessor().process(endpoint.createExchange(getFullFileName(ftpFile),
byteArrayOutputStream));
+                RemoteFileExchange exchange = endpoint.createExchange(getFullFileName(ftpFile),
byteArrayOutputStream);
+
+                if (isSetNames()) {
+                    String relativePath = getFullFileName(ftpFile).substring(endpoint.getConfiguration().getFile().length());
+                    if (relativePath.startsWith("/")) {
+                        relativePath = relativePath.substring(1);
+                    }
+                    exchange.getIn().setHeader(FileComponent.HEADER_FILE_NAME, relativePath);
+                }
+
+                getProcessor().process(exchange);
             }
         }
     }
@@ -122,5 +134,13 @@
 
     public void setRegexPattern(String regexPattern) {
         this.regexPattern = regexPattern;
+    }
+
+    public boolean isSetNames() {
+        return setNames;
+    }
+
+    public void setSetNames(boolean setNames) {
+        this.setNames = setNames;
     }
 }

Modified: activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpProducer.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpProducer.java?view=diff&rev=566940&r1=566939&r2=566940
==============================================================================
--- activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpProducer.java
(original)
+++ activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpProducer.java
Thu Aug 16 23:50:36 2007
@@ -38,22 +38,13 @@
     }
 
     public void process(RemoteFileExchange exchange) throws Exception {
-        final String fileName;
         InputStream payload = exchange.getIn().getBody(InputStream.class);
         final String endpointFile = endpoint.getConfiguration().getFile();
         client.changeWorkingDirectory(endpointFile); // TODO this line might
-                                                        // not be needed...
-                                                        // check after finish
-                                                        // writing unit tests
-        if (endpointFile == null) {
-            throw new NullPointerException("Null Endpoint File");
-        } else {
-            if (endpoint.getConfiguration().isDirectory()) {
-                fileName = endpointFile + "/" + exchange.getIn().getMessageId();
-            } else {
-                fileName = endpointFile;
-            }
-        }
+                                                     // not be needed...
+                                                     // check after finish
+                                                     // writing unit tests
+        String fileName = createFileName(exchange.getIn(), endpoint.getConfiguration());
         buildDirectory(client, fileName.substring(0, fileName.lastIndexOf('/')));
         final boolean success = client.storeFile(fileName, payload);
         if (!success) {

Modified: activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileProducer.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileProducer.java?view=diff&rev=566940&r1=566939&r2=566940
==============================================================================
--- activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileProducer.java
(original)
+++ activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileProducer.java
Thu Aug 16 23:50:36 2007
@@ -16,10 +16,30 @@
  */
 package org.apache.camel.component.file.remote;
 
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.component.file.FileComponent;
 import org.apache.camel.impl.DefaultProducer;
 
 public abstract class RemoteFileProducer<T extends RemoteFileExchange> extends DefaultProducer<T>
{
+
     protected RemoteFileProducer(RemoteFileEndpoint<T> endpoint) {
         super(endpoint);
+    }
+
+    protected String createFileName(Message message, RemoteFileConfiguration fileConfig)
{
+        String answer;
+        String endpointFileName = fileConfig.getFile();
+        String headerFileName = message.getHeader(FileComponent.HEADER_FILE_NAME, String.class);
+        if (fileConfig.isDirectory()) {
+            if (headerFileName != null) {
+                answer = endpointFileName + "/" + headerFileName;
+            } else {
+                answer = endpointFileName + "/" + message.getMessageId();
+            }
+        } else {
+            answer = endpointFileName;
+        }
+        return answer;
     }
 }

Modified: activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConsumer.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConsumer.java?view=diff&rev=566940&r1=566939&r2=566940
==============================================================================
--- activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConsumer.java
(original)
+++ activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConsumer.java
Thu Aug 16 23:50:36 2007
@@ -23,6 +23,7 @@
 import com.jcraft.jsch.ChannelSftp;
 
 import org.apache.camel.Processor;
+import org.apache.camel.component.file.FileComponent;
 
 public class SftpConsumer extends RemoteFileConsumer<RemoteFileExchange> {
     private boolean recursive = true;
@@ -30,6 +31,7 @@
     private long lastPollTime;
     private final SftpEndpoint endpoint;
     private ChannelSftp channel;
+    private boolean setNames = false;
 
     public SftpConsumer(SftpEndpoint endpoint, Processor processor, ChannelSftp channel)
{
         super(endpoint, processor);
@@ -82,7 +84,17 @@
             if (isMatched(sftpFile)) {
                 final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                 channel.get(sftpFile.getFilename(), byteArrayOutputStream);
-                getProcessor().process(endpoint.createExchange(getFullFileName(sftpFile),
byteArrayOutputStream));
+                RemoteFileExchange exchange = endpoint.createExchange(getFullFileName(sftpFile),
byteArrayOutputStream);
+
+                if (isSetNames()) {
+                    String relativePath = getFullFileName(sftpFile).substring(endpoint.getConfiguration().getFile().length());
+                    if (relativePath.startsWith("/")) {
+                        relativePath = relativePath.substring(1);
+                    }
+                    exchange.getIn().setHeader(FileComponent.HEADER_FILE_NAME, relativePath);
+                }
+
+                getProcessor().process(exchange);
             }
         }
     }
@@ -117,5 +129,13 @@
 
     public void setRegexPattern(String regexPattern) {
         this.regexPattern = regexPattern;
+    }
+
+    public boolean isSetNames() {
+        return setNames;
+    }
+
+    public void setSetNames(boolean setNames) {
+        this.setNames = setNames;
     }
 }

Modified: activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpProducer.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpProducer.java?view=diff&rev=566940&r1=566939&r2=566940
==============================================================================
--- activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpProducer.java
(original)
+++ activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpProducer.java
Thu Aug 16 23:50:36 2007
@@ -40,19 +40,10 @@
     }
 
     public void process(RemoteFileExchange exchange) throws Exception {
-        final String fileName;
         InputStream payload = exchange.getIn().getBody(InputStream.class);
         final String endpointFile = endpoint.getConfiguration().getFile();
         channel.cd(endpointFile);
-        if (endpointFile == null) {
-            throw new NullPointerException("Null Endpoint File");
-        } else {
-            if (endpoint.getConfiguration().isDirectory()) {
-                fileName = endpointFile + "/" + exchange.getIn().getMessageId();
-            } else {
-                fileName = endpointFile;
-            }
-        }
+        String fileName = createFileName(exchange.getIn(), endpoint.getConfiguration());
         buildDirectory(channel, fileName.substring(0, fileName.lastIndexOf('/')));
         try {
             channel.put(payload, fileName);



Mime
View raw message