Author: davsclaus
Date: Sun Apr 6 06:34:18 2008
New Revision: 645240
URL: http://svn.apache.org/viewvc?rev=645240&view=rev
Log:
CAMEL-420
- Changed default option for FTPConsumer: setNames is now true.
- Added unit test
Added:
activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpToFileNoFileNameHeaderTest.java
Modified:
activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java
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?rev=645240&r1=645239&r2=645240&view=diff
==============================================================================
---
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
Sun Apr 6 06:34:18 2008
@@ -31,13 +31,14 @@
public class FtpConsumer extends RemoteFileConsumer<RemoteFileExchange> {
private static final transient Log LOG =
LogFactory.getLog(FtpConsumer.class);
+
private final FtpEndpoint endpoint;
- private boolean recursive = true;
- private String regexPattern = "";
private long lastPollTime;
-
private FTPClient client;
- private boolean setNames;
+
+ private boolean recursive = true;
+ private String regexPattern;
+ private boolean setNames = true;
public FtpConsumer(FtpEndpoint endpoint, Processor processor, FTPClient
client) {
super(endpoint, processor);
@@ -121,14 +122,19 @@
// TODO do we need to adjust the TZ? can we?
if (ftpFile.getTimestamp().getTimeInMillis() > lastPollTime) {
if (isMatched(ftpFile)) {
+ String fullFileName = getFullFileName(ftpFile);
final ByteArrayOutputStream byteArrayOutputStream = new
ByteArrayOutputStream();
client.retrieveFile(ftpFile.getName(), byteArrayOutputStream);
- RemoteFileExchange exchange =
endpoint.createExchange(getFullFileName(ftpFile), byteArrayOutputStream);
+ RemoteFileExchange exchange =
endpoint.createExchange(fullFileName, byteArrayOutputStream);
if (isSetNames()) {
- String relativePath =
getFullFileName(ftpFile).substring(endpoint.getConfiguration().getFile().length());
- if (relativePath.startsWith("/")) {
- relativePath = relativePath.substring(1);
+ // set the filename in the special header filename marker
to the ftp filename
+ String ftpBasePath = endpoint.getConfiguration().getFile();
+ String relativePath =
fullFileName.substring(ftpBasePath.length() + 1);
+ relativePath = relativePath.replaceFirst("/", "");
+
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Setting exchange filename to " +
relativePath);
}
exchange.getIn().setHeader(FileComponent.HEADER_FILE_NAME,
relativePath);
}
@@ -141,7 +147,7 @@
protected boolean isMatched(FTPFile file) {
boolean result = true;
if (regexPattern != null && regexPattern.length() > 0) {
- result = file.getName().matches(getRegexPattern());
+ result = file.getName().matches(regexPattern);
}
return result;
}
Added:
activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpToFileNoFileNameHeaderTest.java
URL:
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpToFileNoFileNameHeaderTest.java?rev=645240&view=auto
==============================================================================
---
activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpToFileNoFileNameHeaderTest.java
(added)
+++
activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpToFileNoFileNameHeaderTest.java
Sun Apr 6 06:34:18 2008
@@ -0,0 +1,83 @@
+/**
+ * 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.remote;
+
+import java.io.File;
+
+import org.apache.camel.Endpoint;
+import org.apache.camel.Exchange;
+import org.apache.camel.Producer;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.file.FileComponent;
+import org.apache.camel.component.mock.MockEndpoint;
+
+/**
+ * Unit test for consuming files from a FTP Server to files where we want to
use the filename
+ * from the FTPServer instead of explicit setting a filename using the file
headername option.
+ */
+public class FromFtpToFileNoFileNameHeaderTest extends FtpServerTestSupport {
+
+ private String port = "20015";
+ private String ftpUrl = "ftp://[EMAIL PROTECTED]:" + port +
"/tmp3/camel?password=admin&binary=false";
+
+ public void testCorrectFilename() throws Exception {
+ MockEndpoint resultEndpoint = getMockEndpoint("mock:result");
+ resultEndpoint.expectedMinimumMessageCount(1);
+ resultEndpoint.expectedBodiesReceived("Hello World from FTPServer");
+ resultEndpoint.assertIsSatisfied();
+
+ // assert the file
+ File file = new File("target/ftptest/hello.txt");
+ assertTrue("The file should exists", file.exists());
+ assertTrue("File size wrong", file.length() > 10);
+ }
+
+ public String getPort() {
+ return port;
+ }
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ prepareFtpServer();
+ }
+
+ private void prepareFtpServer() throws Exception {
+ // prepares the FTP Server by creating a file on the server that we
want to unit
+ // test that we can pool and store as a local file
+ Endpoint endpoint = context.getEndpoint(ftpUrl);
+ Exchange exchange = endpoint.createExchange();
+ exchange.getIn().setBody("Hello World from FTPServer");
+ exchange.getIn().setHeader(FileComponent.HEADER_FILE_NAME,
"hello.txt");
+ Producer producer = endpoint.createProducer();
+ producer.start();
+ producer.process(exchange);
+ producer.stop();
+ }
+
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ public void configure() throws Exception {
+ String fileUrl = "file:target/ftptest/?append=false&noop=true";
+ // we do not set any filename in the header propery so the
filename should be the one
+ // from the FTP server we downloaded
+
from(ftpUrl).convertBodyTo(String.class).to(fileUrl).to("mock:result");
+ }
+ };
+ }
+
+}