Author: davsclaus
Date: Thu Aug 12 11:33:44 2010
New Revision: 984720
URL: http://svn.apache.org/viewvc?rev=984720&view=rev
Log:
CAMEL-2822: Added support for sending site commands after succesful login.
Added:
camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerMultipleSiteCommandTest.java
camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerSiteCommandTest.java
- copied, changed from r984718,
camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerTempPrefixTest.java
Modified:
camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java
camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileConfiguration.java
camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileOperations.java
camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
Modified:
camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java?rev=984720&r1=984719&r2=984720&view=diff
==============================================================================
---
camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java
(original)
+++
camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java
Thu Aug 12 11:33:44 2010
@@ -24,6 +24,7 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Iterator;
import java.util.List;
import org.apache.camel.Exchange;
@@ -172,6 +173,25 @@ public class FtpOperations implements Re
throw new
GenericFileOperationFailedException(client.getReplyCode(),
client.getReplyString(), e.getMessage(), e);
}
+ // site commands
+ if (endpoint.getConfiguration().getSiteCommand() != null) {
+ // commands can be separated using new line
+ Iterator it =
ObjectHelper.createIterator(endpoint.getConfiguration().getSiteCommand(), "\n");
+ while (it.hasNext()) {
+ Object next = it.next();
+ String command =
endpoint.getCamelContext().getTypeConverter().convertTo(String.class, next);
+ if (log.isTraceEnabled()) {
+ log.trace("Site command to sent: " + command);
+ }
+ if (command != null) {
+ boolean result = sendSiteCommand(command);
+ if (!result) {
+ throw new GenericFileOperationFailedException("Site
command: " + command + " returned false");
+ }
+ }
+ }
+ }
+
return true;
}
@@ -218,7 +238,7 @@ public class FtpOperations implements Re
public boolean buildDirectory(String directory, boolean absolute) throws
GenericFileOperationFailedException {
if (log.isTraceEnabled()) {
- log.trace("Building directory: " + directory);
+ log.trace("makeDirectory(" + directory + ")");
}
try {
String originalDirectory = client.printWorkingDirectory();
@@ -251,6 +271,9 @@ public class FtpOperations implements Re
}
public boolean retrieveFile(String name, Exchange exchange) throws
GenericFileOperationFailedException {
+ if (log.isTraceEnabled()) {
+ log.trace("retrieveFile(" + name + ")");
+ }
if (ObjectHelper.isNotEmpty(endpoint.getLocalWorkDirectory())) {
// local work directory is configured so we should store file
content as files in this local directory
return retrieveFileToFileInLocalWorkDirectory(name, exchange);
@@ -386,6 +409,9 @@ public class FtpOperations implements Re
}
public boolean storeFile(String name, Exchange exchange) throws
GenericFileOperationFailedException {
+ if (log.isTraceEnabled()) {
+ log.trace("storeFile(" + name + ")");
+ }
// if an existing file already exists what should we do?
if (endpoint.getFileExist() == GenericFileExist.Ignore ||
endpoint.getFileExist() == GenericFileExist.Fail) {
@@ -419,6 +445,9 @@ public class FtpOperations implements Re
}
public boolean existsFile(String name) throws
GenericFileOperationFailedException {
+ if (log.isTraceEnabled()) {
+ log.trace("existsFile(" + name + ")");
+ }
// check whether a file already exists
String directory = FileUtil.onlyPath(name);
if (directory == null) {
@@ -444,6 +473,9 @@ public class FtpOperations implements Re
}
public String getCurrentDirectory() throws
GenericFileOperationFailedException {
+ if (log.isTraceEnabled()) {
+ log.trace("printWorkingDirectory()");
+ }
try {
return client.printWorkingDirectory();
} catch (IOException e) {
@@ -453,7 +485,7 @@ public class FtpOperations implements Re
public void changeCurrentDirectory(String path) throws
GenericFileOperationFailedException {
if (log.isTraceEnabled()) {
- log.trace("Changing current directory to: " + path);
+ log.trace("changeWorkingDirectory(" + path + ")");
}
try {
client.changeWorkingDirectory(path);
@@ -463,6 +495,9 @@ public class FtpOperations implements Re
}
public List<FTPFile> listFiles() throws
GenericFileOperationFailedException {
+ if (log.isTraceEnabled()) {
+ log.trace("listFiles()");
+ }
try {
final List<FTPFile> list = new ArrayList<FTPFile>();
FTPFile[] files = client.listFiles();
@@ -477,6 +512,9 @@ public class FtpOperations implements Re
}
public List<FTPFile> listFiles(String path) throws
GenericFileOperationFailedException {
+ if (log.isTraceEnabled()) {
+ log.trace("listFiles(" + path + ")");
+ }
// use current directory if path not given
if (ObjectHelper.isEmpty(path)) {
path = ".";
@@ -496,6 +534,9 @@ public class FtpOperations implements Re
}
public boolean sendNoop() throws GenericFileOperationFailedException {
+ if (log.isTraceEnabled()) {
+ log.trace("sendNoOp");
+ }
try {
return client.sendNoOp();
} catch (IOException e) {
@@ -503,6 +544,17 @@ public class FtpOperations implements Re
}
}
+ public boolean sendSiteCommand(String command) throws
GenericFileOperationFailedException {
+ if (log.isTraceEnabled()) {
+ log.trace("sendSiteCommand(" + command + ")");
+ }
+ try {
+ return client.sendSiteCommand(command);
+ } catch (IOException e) {
+ throw new
GenericFileOperationFailedException(client.getReplyCode(),
client.getReplyString(), e.getMessage(), e);
+ }
+ }
+
protected FTPClient getFtpClient() {
return client;
}
Modified:
camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileConfiguration.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileConfiguration.java?rev=984720&r1=984719&r2=984720&view=diff
==============================================================================
---
camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileConfiguration.java
(original)
+++
camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileConfiguration.java
Thu Aug 12 11:33:44 2010
@@ -35,6 +35,7 @@ public abstract class RemoteFileConfigur
private int timeout = 30000;
private int soTimeout;
private boolean throwExceptionOnConnectFailed;
+ private String siteCommand;
public RemoteFileConfiguration() {
}
@@ -184,4 +185,19 @@ public abstract class RemoteFileConfigur
public void setThrowExceptionOnConnectFailed(boolean
throwExceptionOnConnectFailed) {
this.throwExceptionOnConnectFailed = throwExceptionOnConnectFailed;
}
+
+ public String getSiteCommand() {
+ return siteCommand;
+ }
+
+ /**
+ * Sets optional site command(s) to be executed after successful login.
+ * <p/>
+ * Multiple site commands can be separated using a new line character (\n).
+ *
+ * @param siteCommand the site command(s).
+ */
+ public void setSiteCommand(String siteCommand) {
+ this.siteCommand = siteCommand;
+ }
}
Modified:
camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileOperations.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileOperations.java?rev=984720&r1=984719&r2=984720&view=diff
==============================================================================
---
camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileOperations.java
(original)
+++
camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileOperations.java
Thu Aug 12 11:33:44 2010
@@ -28,7 +28,7 @@ public interface RemoteFileOperations<T>
* Connects to the remote server
*
* @param configuration configuration
- * @return true if connected
+ * @return <tt>true</tt> if connected
* @throws GenericFileOperationFailedException can be thrown
*/
boolean connect(RemoteFileConfiguration configuration) throws
GenericFileOperationFailedException;
@@ -36,7 +36,7 @@ public interface RemoteFileOperations<T>
/**
* Returns whether we are connected to the remote server or not
*
- * @return true if connected, false if not
+ * @return <tt>true</tt> if connected, <tt>false</tt> if not
* @throws GenericFileOperationFailedException can be thrown
*/
boolean isConnected() throws GenericFileOperationFailedException;
@@ -51,9 +51,18 @@ public interface RemoteFileOperations<T>
/**
* Sends a noop command to the remote server
*
- * @throws GenericFileOperationFailedException can be thrown
* @return <tt>true</tt> if the noop was a success, <tt>false</tt>
otherwise
+ * @throws GenericFileOperationFailedException can be thrown
*/
boolean sendNoop() throws GenericFileOperationFailedException;
+ /**
+ * Sends a site command to the remote server
+ *
+ * @param command the command
+ * @return <tt>true</tt> if the command was a success, <tt>false</tt>
otherwise
+ * @throws GenericFileOperationFailedException can be thrown
+ */
+ boolean sendSiteCommand(String command) throws
GenericFileOperationFailedException;
+
}
Modified:
camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java?rev=984720&r1=984719&r2=984720&view=diff
==============================================================================
---
camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
(original)
+++
camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
Thu Aug 12 11:33:44 2010
@@ -545,4 +545,9 @@ public class SftpOperations implements R
// is not implemented
return true;
}
+
+ public boolean sendSiteCommand(String command) throws
GenericFileOperationFailedException {
+ // is not implemented
+ return true;
+ }
}
Added:
camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerMultipleSiteCommandTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerMultipleSiteCommandTest.java?rev=984720&view=auto
==============================================================================
---
camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerMultipleSiteCommandTest.java
(added)
+++
camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerMultipleSiteCommandTest.java
Thu Aug 12 11:33:44 2010
@@ -0,0 +1,53 @@
+/**
+ * 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.converter.IOConverter;
+import org.apache.camel.impl.JndiRegistry;
+import org.junit.Test;
+
+public class FtpProducerMultipleSiteCommandTest extends FtpServerTestSupport {
+
+ @Override
+ protected JndiRegistry createRegistry() throws Exception {
+ JndiRegistry jndi = super.createRegistry();
+ jndi.bind("site", "help site\nhelp site");
+ return jndi;
+ }
+
+ @Override
+ public boolean isUseRouteBuilder() {
+ return false;
+ }
+
+ private String getFtpUrl() {
+ return "ftp://ad...@localhost:" + getPort() +
"/site?password=admin&siteCommand=#site";
+ }
+
+ @Test
+ public void testSiteCommands() throws Exception {
+ sendFile(getFtpUrl(), "Hello World", "hello.txt");
+
+ File file = new File(FTP_ROOT_DIR + "site/hello.txt");
+ file = file.getAbsoluteFile();
+ assertTrue("The uploaded file should exists", file.exists());
+ assertEquals("Hello World", IOConverter.toString(file, null));
+ }
+
+}
\ No newline at end of file
Copied:
camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerSiteCommandTest.java
(from r984718,
camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerTempPrefixTest.java)
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerSiteCommandTest.java?p2=camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerSiteCommandTest.java&p1=camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerTempPrefixTest.java&r1=984718&r2=984720&rev=984720&view=diff
==============================================================================
---
camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerTempPrefixTest.java
(original)
+++
camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerSiteCommandTest.java
Thu Aug 12 11:33:44 2010
@@ -21,10 +21,7 @@ import java.io.File;
import org.apache.camel.converter.IOConverter;
import org.junit.Test;
-/**
- * Unit test to verify that Camel can build remote directory on FTP server if
missing (full or part of).
- */
-public class FtpProducerTempPrefixTest extends FtpServerTestSupport {
+public class FtpProducerSiteCommandTest extends FtpServerTestSupport {
@Override
public boolean isUseRouteBuilder() {
@@ -32,16 +29,17 @@ public class FtpProducerTempPrefixTest e
}
private String getFtpUrl() {
- return "ftp://ad...@localhost:" + getPort() +
"/upload/user/claus?binary=false&password=admin&tempPrefix=.uploading";
+ return "ftp://ad...@localhost:" + getPort() +
"/site?password=admin&siteCommand=help site";
}
@Test
- public void testProduceTempPrefixTest() throws Exception {
- sendFile(getFtpUrl(), "Hello World", "claus.txt");
+ public void testSiteCommand() throws Exception {
+ sendFile(getFtpUrl(), "Hello World", "hello.txt");
- File file = new File(FTP_ROOT_DIR + "upload/user/claus/claus.txt");
+ File file = new File(FTP_ROOT_DIR + "site/hello.txt");
file = file.getAbsoluteFile();
assertTrue("The uploaded file should exists", file.exists());
assertEquals("Hello World", IOConverter.toString(file, null));
}
+
}
\ No newline at end of file