cleaning up SFTP + FTP code
This commit is contained in:
@@ -14,6 +14,7 @@ import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.integration.file.FileReadingMessageSource;
|
||||
import org.springframework.scheduling.TaskScheduler;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ErrorHandler;
|
||||
|
||||
import java.io.File;
|
||||
@@ -94,7 +95,7 @@ public class FTPMessageSourceFactoryBean extends AbstractFactoryBean<FTPFileSour
|
||||
File ftpTmp = new File(tmp, "ftpInbound");
|
||||
this.localWorkingDirectory = "file://" + ftpTmp.getAbsolutePath();
|
||||
}
|
||||
assert !StringUtils.isEmpty(this.localWorkingDirectory) : "the local working directory can't be null!";
|
||||
Assert.hasText( this.localWorkingDirectory , "the local working directory can't be null!" );
|
||||
|
||||
ResourceEditor resourceEditor = new ResourceEditor(this.resourceLoader);
|
||||
resourceEditor.setAsText(this.localWorkingDirectory);
|
||||
@@ -130,7 +131,6 @@ public class FTPMessageSourceFactoryBean extends AbstractFactoryBean<FTPFileSour
|
||||
defaultFTPClientFactory.setPort(this.port);
|
||||
defaultFTPClientFactory.setRemoteWorkingDirectory(this.remoteDirectory);
|
||||
defaultFTPClientFactory.setUsername(this.username);
|
||||
|
||||
defaultFTPClientFactory.setClientMode(this.clientMode);
|
||||
|
||||
QueuedFTPClientPool queuedFTPClientPool = new QueuedFTPClientPool(15, defaultFTPClientFactory);
|
||||
|
||||
@@ -54,13 +54,11 @@ public class FTPSendingMessageHandlerFactoryBean extends AbstractFactoryBean<FTP
|
||||
defaultFTPClientFactory.setPort(this.port);
|
||||
defaultFTPClientFactory.setRemoteWorkingDirectory(this.remoteDirectory);
|
||||
defaultFTPClientFactory.setUsername(this.username);
|
||||
|
||||
defaultFTPClientFactory.setClientMode(this.clientMode);
|
||||
|
||||
QueuedFTPClientPool queuedFTPClientPool = new QueuedFTPClientPool(15, defaultFTPClientFactory);
|
||||
|
||||
FTPSendingMessageHandler ftpSendingMessageHandler = new FTPSendingMessageHandler(queuedFTPClientPool);
|
||||
|
||||
ftpSendingMessageHandler.afterPropertiesSet();
|
||||
|
||||
return ftpSendingMessageHandler;
|
||||
|
||||
@@ -20,11 +20,9 @@ import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
|
||||
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
|
||||
import org.springframework.beans.factory.xml.ParserContext;
|
||||
|
||||
import org.springframework.integration.config.xml.AbstractOutboundChannelAdapterParser;
|
||||
import org.springframework.integration.config.xml.AbstractPollingInboundChannelAdapterParser;
|
||||
import org.springframework.integration.config.xml.IntegrationNamespaceUtils;
|
||||
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
import java.util.HashMap;
|
||||
@@ -83,7 +81,8 @@ public class FTPNamespaceHandler extends NamespaceHandlerSupport {
|
||||
protected String parseSource(Element element, ParserContext parserContext) {
|
||||
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(PACKAGE_NAME + ".FTPMessageSourceFactoryBean");
|
||||
|
||||
for (String p : ("auto-create-directories,username,password,host,port," + "remote-directory,local-working-directory").split(",")) { //auto-delete-remote-files-on-sync
|
||||
for (String p : ("auto-create-directories,username,password,host,port," + "remote-directory,local-working-directory").split(",")) {
|
||||
//auto-delete-remote-files-on-sync
|
||||
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, p);
|
||||
}
|
||||
|
||||
|
||||
@@ -15,10 +15,10 @@
|
||||
*/
|
||||
package org.springframework.integration.sftp;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
|
||||
/**
|
||||
@@ -39,48 +39,21 @@ public class SFTPSessionFactory implements FactoryBean<SFTPSession>, Initializin
|
||||
private volatile int port = 22; // the default
|
||||
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
assert !StringUtils.isEmpty(this.remoteHost) : "remoteHost can't be empty!";
|
||||
assert !StringUtils.isEmpty(this.user) : "user can't be empty!";
|
||||
assert !StringUtils.isEmpty(this.password) || !StringUtils.isEmpty(this.privateKey) || !StringUtils.isEmpty(this.privateKeyPassphrase) : "you must configure either a password or a private key and/or a private key passphrase!";
|
||||
assert this.port >= 0 : "port must be a valid number! ";
|
||||
}
|
||||
|
||||
public String getKnownHosts() {
|
||||
return knownHosts;
|
||||
Assert.hasText(this.remoteHost, "remoteHost can't be empty!");
|
||||
Assert.hasText(this.user, "user can't be empty!");
|
||||
Assert.state(StringUtils.hasText(this.password) || StringUtils.hasText(this.privateKey) || StringUtils.hasText(this.privateKeyPassphrase),
|
||||
"you must configure either a password or a private key and/or a private key passphrase!");
|
||||
Assert.state(this.port >= 0, "port must be a valid number! ");
|
||||
}
|
||||
|
||||
public SFTPSession getObject() throws Exception {
|
||||
return new SFTPSession(this.getUser(), this.getRemoteHost(), this.getPassword(), this.getPort(), this.getKnownHosts(), null, this.getPrivateKey(), this.getPrivateKeyPassphrase());
|
||||
return new SFTPSession( this.user, this.remoteHost , this.password ,this.port, this.knownHosts, null, this.privateKey , this.privateKeyPassphrase);
|
||||
}
|
||||
|
||||
public Class<?extends SFTPSession> getObjectType() {
|
||||
return SFTPSession.class;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public String getPrivateKey() {
|
||||
return privateKey;
|
||||
}
|
||||
|
||||
public String getPrivateKeyPassphrase() {
|
||||
return privateKeyPassphrase;
|
||||
}
|
||||
|
||||
public String getRemoteHost() {
|
||||
return remoteHost;
|
||||
}
|
||||
|
||||
public String getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public boolean isSingleton() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -16,8 +16,6 @@
|
||||
package org.springframework.integration.sftp.config;
|
||||
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
|
||||
import org.springframework.integration.sftp.QueuedSFTPSessionPool;
|
||||
import org.springframework.integration.sftp.SFTPSendingMessageHandler;
|
||||
import org.springframework.integration.sftp.SFTPSessionFactory;
|
||||
@@ -29,7 +27,7 @@ import org.springframework.integration.sftp.SFTPSessionFactory;
|
||||
*
|
||||
* @author Josh Long
|
||||
*/
|
||||
public class SFTPMessageSendingConsumerFactoryBean implements InitializingBean, FactoryBean<SFTPSendingMessageHandler> {
|
||||
public class SFTPMessageSendingConsumerFactoryBean implements FactoryBean<SFTPSendingMessageHandler> {
|
||||
private String host;
|
||||
private String keyFile;
|
||||
private String keyFilePassword;
|
||||
@@ -39,33 +37,15 @@ public class SFTPMessageSendingConsumerFactoryBean implements InitializingBean,
|
||||
private boolean autoCreateDirectories;
|
||||
private int port;
|
||||
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
if (isAutoCreateDirectories()) {
|
||||
// todo figure out this value
|
||||
}
|
||||
}
|
||||
|
||||
public String getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
public String getKeyFile() {
|
||||
return keyFile;
|
||||
}
|
||||
|
||||
public String getKeyFilePassword() {
|
||||
return keyFilePassword;
|
||||
}
|
||||
|
||||
public SFTPSendingMessageHandler getObject() throws Exception {
|
||||
SFTPSessionFactory sessionFactory = SFTPSessionUtils.buildSftpSessionFactory(this.getHost(), this.getPassword(), this.getUsername(), this.getKeyFile(), this.getKeyFilePassword(),
|
||||
this.getPort());
|
||||
SFTPSessionFactory sessionFactory = SFTPSessionUtils.buildSftpSessionFactory(
|
||||
this.host, this.password, this.username, this.keyFile , this.keyFilePassword, this.port);
|
||||
|
||||
QueuedSFTPSessionPool queuedSFTPSessionPool = new QueuedSFTPSessionPool(15, sessionFactory);
|
||||
queuedSFTPSessionPool.afterPropertiesSet();
|
||||
|
||||
SFTPSendingMessageHandler sftpSendingMessageHandler = new SFTPSendingMessageHandler(queuedSFTPSessionPool);
|
||||
sftpSendingMessageHandler.setRemoteDirectory(this.getRemoteDirectory());
|
||||
sftpSendingMessageHandler.setRemoteDirectory(this.remoteDirectory);
|
||||
sftpSendingMessageHandler.afterPropertiesSet();
|
||||
|
||||
return sftpSendingMessageHandler;
|
||||
@@ -75,26 +55,6 @@ public class SFTPMessageSendingConsumerFactoryBean implements InitializingBean,
|
||||
return SFTPSendingMessageHandler.class;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public String getRemoteDirectory() {
|
||||
return remoteDirectory;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public boolean isAutoCreateDirectories() {
|
||||
return autoCreateDirectories;
|
||||
}
|
||||
|
||||
public boolean isSingleton() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -20,11 +20,9 @@ import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
|
||||
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
|
||||
import org.springframework.beans.factory.xml.ParserContext;
|
||||
|
||||
import org.springframework.integration.config.xml.AbstractOutboundChannelAdapterParser;
|
||||
import org.springframework.integration.config.xml.AbstractPollingInboundChannelAdapterParser;
|
||||
import org.springframework.integration.config.xml.IntegrationNamespaceUtils;
|
||||
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
|
||||
@@ -49,7 +47,7 @@ public class SFTPNamespaceHandler extends NamespaceHandlerSupport {
|
||||
private static class SFTPMessageSendingConsumerBeanDefinitionParser extends AbstractOutboundChannelAdapterParser {
|
||||
@Override
|
||||
protected AbstractBeanDefinition parseConsumer(Element element, ParserContext parserContext) {
|
||||
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(PACKAGE_NAME + ".config.SFTPMessageSendingConsumerFactoryBean");
|
||||
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(SFTPMessageSendingConsumerFactoryBean.class.getName());
|
||||
|
||||
for (String p : "auto-create-directories,username,password,host,key-file,key-file-password,remote-directory".split(",")) {
|
||||
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, p);
|
||||
@@ -65,9 +63,8 @@ public class SFTPNamespaceHandler extends NamespaceHandlerSupport {
|
||||
*/
|
||||
private static class SFTPMessageSourceBeanDefinitionParser extends AbstractPollingInboundChannelAdapterParser {
|
||||
@Override
|
||||
@SuppressWarnings("unused")
|
||||
protected String parseSource(Element element, ParserContext parserContext) {
|
||||
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(PACKAGE_NAME + ".config.SFTPMessageSourceFactoryBean");
|
||||
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition( SFTPMessageSendingConsumerFactoryBean.class.getName());
|
||||
|
||||
for (String p : "auto-create-directories,username,password,host,key-file,key-file-password,remote-directory,local-working-directory,auto-delete-remote-files-on-sync".split(",")) {
|
||||
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, p);
|
||||
|
||||
Reference in New Issue
Block a user