From b77796e914a2076fb3598136b05cf59d725bbc3c Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Fri, 15 Oct 2010 09:11:57 -0400 Subject: [PATCH] INT-1484, added support for filename-generator to FTP Outbound adapter, restructured FTP module to be consistent with other modules --- spring-integration-ftp/.project | 6 ++ spring-integration-ftp/pom.xml | 6 ++ .../ftp/FtpSendingMessageHandler.java | 44 ++++++------- .../FtpSendingMessageHandlerFactoryBean.java | 10 ++- ...geSendingConsumerBeanDefinitionParser.java | 1 + ...geSendingConsumerBeanDefinitionParser.java | 3 +- .../ftp/config/spring-integration-ftp-2.0.xsd | 16 ++++- ....xml => FtpParserInboundTests-context.xml} | 0 ...=> FtpParserInboundTests-fail-context.xml} | 0 ...rTests.java => FtpParserInboundTests.java} | 7 +- .../ftp/FtpParserOutboundTests-context.xml | 23 +++++++ .../ftp/FtpParserOutboundTests.java | 66 +++++++++++++++++++ .../integration/ftp}/inbound-ftp-context.xml | 0 .../integration/ftp}/inbound-ftps-context.xml | 0 .../integration/ftp}/outbound-ftp-context.xml | 0 .../ftp}/outbound-ftps-context.xml | 0 16 files changed, 151 insertions(+), 31 deletions(-) rename spring-integration-ftp/src/test/java/org/springframework/integration/ftp/{FtpParserTests-inbound.xml => FtpParserInboundTests-context.xml} (100%) rename spring-integration-ftp/src/test/java/org/springframework/integration/ftp/{FtpParserTests-inbound-fail.xml => FtpParserInboundTests-fail-context.xml} (100%) rename spring-integration-ftp/src/test/java/org/springframework/integration/ftp/{FtpParserTests.java => FtpParserInboundTests.java} (85%) create mode 100644 spring-integration-ftp/src/test/java/org/springframework/integration/ftp/FtpParserOutboundTests-context.xml create mode 100644 spring-integration-ftp/src/test/java/org/springframework/integration/ftp/FtpParserOutboundTests.java rename spring-integration-ftp/src/test/{resources => java/org/springframework/integration/ftp}/inbound-ftp-context.xml (100%) rename spring-integration-ftp/src/test/{resources => java/org/springframework/integration/ftp}/inbound-ftps-context.xml (100%) rename spring-integration-ftp/src/test/{resources => java/org/springframework/integration/ftp}/outbound-ftp-context.xml (100%) rename spring-integration-ftp/src/test/{resources => java/org/springframework/integration/ftp}/outbound-ftps-context.xml (100%) diff --git a/spring-integration-ftp/.project b/spring-integration-ftp/.project index 53e5185f7c..78f8f6bd2d 100644 --- a/spring-integration-ftp/.project +++ b/spring-integration-ftp/.project @@ -15,8 +15,14 @@ + + org.springframework.ide.eclipse.core.springbuilder + + + + org.springframework.ide.eclipse.core.springnature org.maven.ide.eclipse.maven2Nature org.eclipse.jdt.core.javanature diff --git a/spring-integration-ftp/pom.xml b/spring-integration-ftp/pom.xml index 1e205193b1..fa15545c9e 100644 --- a/spring-integration-ftp/pom.xml +++ b/spring-integration-ftp/pom.xml @@ -70,6 +70,12 @@ ${project.version} compile + + org.springframework.integration + spring-integration-test + ${project.version} + test + commons-lang commons-lang diff --git a/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/FtpSendingMessageHandler.java b/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/FtpSendingMessageHandler.java index d3311c3143..036aad90e0 100644 --- a/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/FtpSendingMessageHandler.java +++ b/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/FtpSendingMessageHandler.java @@ -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; - } - } diff --git a/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/FtpSendingMessageHandlerFactoryBean.java b/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/FtpSendingMessageHandlerFactoryBean.java index 9e1476ddec..61ddb5ca39 100644 --- a/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/FtpSendingMessageHandlerFactoryBean.java +++ b/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/FtpSendingMessageHandlerFactoryBean.java @@ -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 - - + + + + + Allows you to specify a reference to + [org.springframework.integration.file.FileNameGenerator] implementation. + + + + + + + + + + + + + + + + + + diff --git a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/FtpParserOutboundTests.java b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/FtpParserOutboundTests.java new file mode 100644 index 0000000000..4a59d2e96a --- /dev/null +++ b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/FtpParserOutboundTests.java @@ -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 message = new GenericMessage("ftp file generator test"); + try { + handler.handleMessage(message); + } catch (Exception e) { + // ignore + } + verify(fileNameGenerator, times(1)).generateFileName(message); + } + +} \ No newline at end of file diff --git a/spring-integration-ftp/src/test/resources/inbound-ftp-context.xml b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/inbound-ftp-context.xml similarity index 100% rename from spring-integration-ftp/src/test/resources/inbound-ftp-context.xml rename to spring-integration-ftp/src/test/java/org/springframework/integration/ftp/inbound-ftp-context.xml diff --git a/spring-integration-ftp/src/test/resources/inbound-ftps-context.xml b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/inbound-ftps-context.xml similarity index 100% rename from spring-integration-ftp/src/test/resources/inbound-ftps-context.xml rename to spring-integration-ftp/src/test/java/org/springframework/integration/ftp/inbound-ftps-context.xml diff --git a/spring-integration-ftp/src/test/resources/outbound-ftp-context.xml b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/outbound-ftp-context.xml similarity index 100% rename from spring-integration-ftp/src/test/resources/outbound-ftp-context.xml rename to spring-integration-ftp/src/test/java/org/springframework/integration/ftp/outbound-ftp-context.xml diff --git a/spring-integration-ftp/src/test/resources/outbound-ftps-context.xml b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/outbound-ftps-context.xml similarity index 100% rename from spring-integration-ftp/src/test/resources/outbound-ftps-context.xml rename to spring-integration-ftp/src/test/java/org/springframework/integration/ftp/outbound-ftps-context.xml