INT-1484, added support for filename-generator to FTP Outbound adapter, restructured FTP module to be consistent with other modules

This commit is contained in:
Oleg Zhurakousky
2010-10-15 09:11:57 -04:00
parent ec8f3486a1
commit b77796e914
16 changed files with 151 additions and 31 deletions

View File

@@ -15,8 +15,14 @@
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.springframework.ide.eclipse.core.springbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.springframework.ide.eclipse.core.springnature</nature>
<nature>org.maven.ide.eclipse.maven2Nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>

View File

@@ -70,6 +70,12 @@
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-test</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>

View File

@@ -27,14 +27,13 @@ import java.nio.charset.Charset;
import org.apache.commons.lang.SystemUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.integration.Message;
import org.springframework.integration.MessageDeliveryException;
import org.springframework.integration.core.MessageHandler;
import org.springframework.integration.file.DefaultFileNameGenerator;
import org.springframework.integration.file.FileNameGenerator;
import org.springframework.integration.handler.AbstractMessageHandler;
import org.springframework.util.Assert;
import org.springframework.util.FileCopyUtils;
@@ -44,12 +43,12 @@ import org.springframework.util.FileCopyUtils;
* @author Iwein Fuld
* @author Mark Fisher
* @author Josh Long
* @author Oleg Zhurakousky
*/
public class FtpSendingMessageHandler implements MessageHandler, InitializingBean {
public class FtpSendingMessageHandler extends AbstractMessageHandler{
private static final String TEMPORARY_FILE_SUFFIX = ".writing";
private volatile FtpClientPool ftpClientPool;
private volatile FileNameGenerator fileNameGenerator = new DefaultFileNameGenerator();
@@ -85,7 +84,7 @@ public class FtpSendingMessageHandler implements MessageHandler, InitializingBea
this.charset = charset;
}
public void afterPropertiesSet() throws Exception {
protected void onInit() throws Exception {
Assert.notNull(ftpClientPool, "'ftpClientPool' must not be null");
Assert.notNull(temporaryBufferFolder,
"'temporaryBufferFolder' must not be null");
@@ -143,13 +142,29 @@ public class FtpSendingMessageHandler implements MessageHandler, InitializingBea
}
}
/* Ugh this needs to be put in a convenient place accessible for all the file:, sftp:, and ftp:* adapters */
private boolean sendFile(File file, FTPClient client) throws FileNotFoundException, IOException {
FileInputStream fileInputStream = new FileInputStream(file);
boolean sent = client.storeFile(file.getName(), fileInputStream);
fileInputStream.close();
return sent;
}
public void handleMessage(Message<?> message) {
private FTPClient getFtpClient() throws SocketException, IOException {
FTPClient client;
client = this.ftpClientPool.getClient();
Assert.state(client != null, FtpClientPool.class.getSimpleName() +
" returned 'null' client this most likely a bug in the pool implementation.");
return client;
}
@Override
protected void handleMessageInternal(Message<?> message) throws Exception {
Assert.notNull(message, "'message' must not be null");
Object payload = message.getPayload();
Assert.notNull(payload, "Message payload must not be null");
File file = this.redeemForStorableFile(message);
if ((file != null) && file.exists()) {
FTPClient client = null;
boolean sentSuccesfully;
@@ -190,19 +205,4 @@ public class FtpSendingMessageHandler implements MessageHandler, InitializingBea
}
}
private boolean sendFile(File file, FTPClient client) throws FileNotFoundException, IOException {
FileInputStream fileInputStream = new FileInputStream(file);
boolean sent = client.storeFile(file.getName(), fileInputStream);
fileInputStream.close();
return sent;
}
private FTPClient getFtpClient() throws SocketException, IOException {
FTPClient client;
client = this.ftpClientPool.getClient();
Assert.state(client != null, FtpClientPool.class.getSimpleName() +
" returned 'null' client this most likely a bug in the pool implementation.");
return client;
}
}

View File

@@ -8,6 +8,7 @@ import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.ResourceLoader;
import org.springframework.integration.file.FileNameGenerator;
/**
@@ -27,11 +28,17 @@ public class FtpSendingMessageHandlerFactoryBean extends AbstractFactoryBean<Ftp
protected int clientMode;
private int fileType;
private ResourceLoader resourceLoader;
private ApplicationContext applicationContext;
private FileNameGenerator fileNameGenerator;
private ApplicationContext applicationContext;
public void setCharset(String charset) {
this.charset = charset;
}
public void setFileNameGenerator(FileNameGenerator fileNameGenerator) {
this.fileNameGenerator = fileNameGenerator;
}
public void setFileType(int fileType) {
this.fileType = fileType;
@@ -72,6 +79,7 @@ public class FtpSendingMessageHandlerFactoryBean extends AbstractFactoryBean<Ftp
defaultFtpClientFactory);
FtpSendingMessageHandler ftpSendingMessageHandler = new FtpSendingMessageHandler(queuedFtpClientPool);
ftpSendingMessageHandler.setFileNameGenerator(this.fileNameGenerator);
if (this.charset != null) {
ftpSendingMessageHandler.setCharset(this.charset);
}

View File

@@ -22,6 +22,7 @@ public class FtpMessageSendingConsumerBeanDefinitionParser
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(
FtpSendingMessageHandlerFactoryBean.class.getName());
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder,element,"charset");
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder,element,"filename-generator", "fileNameGenerator");
FtpNamespaceParserSupport.configureCoreFtpClient(builder, element,
parserContext);

View File

@@ -14,8 +14,7 @@ import org.w3c.dom.Element;
*
* @author Josh Long
*/
public class FtpsMessageSendingConsumerBeanDefinitionParser
extends AbstractOutboundChannelAdapterParser {
public class FtpsMessageSendingConsumerBeanDefinitionParser extends AbstractOutboundChannelAdapterParser {
@Override
protected AbstractBeanDefinition parseConsumer(Element element,
ParserContext parserContext) {

View File

@@ -50,13 +50,25 @@
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="username" type="xsd:string" use="required"/>
<xsd:attribute name="remote-directory" type="xsd:string" use="required"/>
<xsd:attribute name="host" type="xsd:string" use="required"/>
<xsd:attribute name="password" type="xsd:string"/>
<xsd:attribute name="password" type="xsd:string" use="required"/>
<xsd:attribute name="charset" type="xsd:string" default="UTF-8"/>
<xsd:attribute name="port" type="xsd:int" default="22"/>
<xsd:attribute name="filename-generator" type="xsd:string">
<xsd:annotation>
<xsd:documentation>
Allows you to specify a reference to
[org.springframework.integration.file.FileNameGenerator] implementation.
</xsd:documentation>
<xsd:appinfo>
<tool:annotation kind="ref">
<tool:expected-type type="org.springframework.integration.file.FileNameGenerator"/>
</tool:annotation>
</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute use="optional" name="client-mode" default="active-local-data-connection-mode">
<xsd:annotation>
<xsd:documentation><![CDATA[

View File

@@ -23,14 +23,13 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author Oleg Zhurakousky
*
*/
public class FtpParserTests {
public class FtpParserInboundTests {
@Before
public void prepare(){
new File("target/foo").delete();
@@ -39,14 +38,14 @@ public class FtpParserTests {
@Test
public void testLocalFilesAutoCreationTrue() throws Exception{
assertTrue(!new File("target/foo").exists());
new ClassPathXmlApplicationContext("FtpParserTests-inbound.xml", this.getClass());
new ClassPathXmlApplicationContext("FtpParserInboundTests-context.xml", this.getClass());
assertTrue(new File("target/foo").exists());
assertTrue(!new File("target/bar").exists());
}
@Test(expected=BeanCreationException.class)
public void testLocalFilesAutoCreationFalse() throws Exception{
assertTrue(!new File("target/bar").exists());
new ClassPathXmlApplicationContext("FtpParserTests-inbound-fail.xml", this.getClass());
new ClassPathXmlApplicationContext("FtpParserInboundTests-fail-context.xml", this.getClass());
}
@After

View File

@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-ftp="http://www.springframework.org/schema/integration/ftp"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.0.xsd
http://www.springframework.org/schema/integration/ftp http://www.springframework.org/schema/integration/ftp/spring-integration-ftp-2.0.xsd">
<int:channel id="ftpOutbound"/>
<int-ftp:outbound-channel-adapter id="ftpOutboundAdapter"
username="ozhurakousky"
password="seva@1994"
channel="ftpOutbound"
remote-directory="temp"
host="localhost"
filename-generator="fileNameGenerator"/>
<bean id="fileNameGenerator" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="org.springframework.integration.file.FileNameGenerator"/>
</bean>
</beans>

View File

@@ -0,0 +1,66 @@
/*
* Copyright 2002-2010 the original author or authors.
*
* Licensed 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.springframework.integration.ftp;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertTrue;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.io.File;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.Message;
import org.springframework.integration.endpoint.EventDrivenConsumer;
import org.springframework.integration.file.FileNameGenerator;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.test.util.TestUtils;
/**
* @author Oleg Zhurakousky
*
*/
public class FtpParserOutboundTests {
@Test
public void testFtpOutboundWithFileGenerator() throws Exception{
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("FtpParserOutboundTests-context.xml", this.getClass());
FileNameGenerator fileNameGenerator = context.getBean("fileNameGenerator", FileNameGenerator.class);
assertNotNull(fileNameGenerator);
when(fileNameGenerator.generateFileName(Mockito.any(Message.class))).thenReturn("oleg-ftp-test.txt");
EventDrivenConsumer fileOutboundEndpoint = context.getBean("ftpOutboundAdapter", EventDrivenConsumer.class);
FtpSendingMessageHandler handler = (FtpSendingMessageHandler) TestUtils.getPropertyValue(fileOutboundEndpoint, "handler");
Message<String> message = new GenericMessage<String>("ftp file generator test");
try {
handler.handleMessage(message);
} catch (Exception e) {
// ignore
}
verify(fileNameGenerator, times(1)).generateFileName(message);
}
}