diff --git a/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/outbound/FtpSendingMessageHandler.java b/spring-integration-file/src/main/java/org/springframework/integration/file/remote/handler/FileTransferringMessageHandler.java similarity index 73% rename from spring-integration-ftp/src/main/java/org/springframework/integration/ftp/outbound/FtpSendingMessageHandler.java rename to spring-integration-file/src/main/java/org/springframework/integration/file/remote/handler/FileTransferringMessageHandler.java index 31a4518276..c32913e8d6 100644 --- a/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/outbound/FtpSendingMessageHandler.java +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/remote/handler/FileTransferringMessageHandler.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.integration.ftp.outbound; +package org.springframework.integration.file.remote.handler; import java.io.File; import java.io.FileInputStream; @@ -37,44 +37,43 @@ import org.springframework.integration.handler.AbstractMessageHandler; import org.springframework.integration.handler.ExpressionEvaluatingMessageProcessor; import org.springframework.util.Assert; import org.springframework.util.FileCopyUtils; +import org.springframework.util.StringUtils; /** - * A {@link org.springframework.integration.core.MessageHandler} implementation that sends files to an FTP server. + * A {@link org.springframework.integration.core.MessageHandler} implementation that transfers files to a remote server. * * @author Iwein Fuld * @author Mark Fisher * @author Josh Long * @author Oleg Zhurakousky + * @since 2.0 */ -public class FtpSendingMessageHandler extends AbstractMessageHandler { +public class FileTransferringMessageHandler extends AbstractMessageHandler { private static final String TEMPORARY_FILE_SUFFIX = ".writing"; private final SessionFactory sessionFactory; - private volatile ExpressionEvaluatingMessageProcessor directoryExpressionProcesor; - - private volatile Expression remoteDirectoryExpression; + private volatile ExpressionEvaluatingMessageProcessor directoryExpressionProcessor; private volatile FileNameGenerator fileNameGenerator = new DefaultFileNameGenerator(); private volatile File temporaryBufferFolderFile; - private volatile Resource temporaryBufferFolder = - new FileSystemResource(System.getProperty("java.io.tmpdir")); + private volatile Resource temporaryBufferFolder = new FileSystemResource(System.getProperty("java.io.tmpdir")); private volatile String charset = Charset.defaultCharset().name(); - public FtpSendingMessageHandler(SessionFactory sessionFactory) { + public FileTransferringMessageHandler(SessionFactory sessionFactory) { Assert.notNull(sessionFactory, "sessionFactory must not be null"); this.sessionFactory = sessionFactory; } public void setRemoteDirectoryExpression(Expression remoteDirectoryExpression) { - this.remoteDirectoryExpression = remoteDirectoryExpression; + this.directoryExpressionProcessor = new ExpressionEvaluatingMessageProcessor(remoteDirectoryExpression); } public void setTemporaryBufferFolder(Resource temporaryBufferFolder) { @@ -90,11 +89,49 @@ public class FtpSendingMessageHandler extends AbstractMessageHandler { } protected void onInit() throws Exception { - Assert.notNull(this.temporaryBufferFolder, "'temporaryBufferFolder' must not be null"); + Assert.notNull(this.directoryExpressionProcessor, "remoteDirectoryExpression is required"); + Assert.notNull(this.temporaryBufferFolder, "temporaryBufferFolder must not be null"); this.temporaryBufferFolderFile = this.temporaryBufferFolder.getFile(); - if (this.remoteDirectoryExpression != null) { - this.directoryExpressionProcesor = - new ExpressionEvaluatingMessageProcessor(this.remoteDirectoryExpression, String.class); + } + + @Override + protected void handleMessageInternal(Message message) throws Exception { + File file = this.redeemForStorableFile(message); + if (file != null && file.exists()) { + Session session = this.sessionFactory.getSession(); + boolean sentSuccesfully = false; + try { + String targetDirectory = this.directoryExpressionProcessor.processMessage(message); + sentSuccesfully = this.sendFileToRemoteDirectory(file, targetDirectory, session); + } + catch (FileNotFoundException e) { + throw new MessageDeliveryException(message, + "File [" + file + "] not found in local working directory; it was moved or deleted unexpectedly.", e); + } + catch (IOException e) { + throw new MessageDeliveryException(message, + "Failed to transfer file [" + file + "] from local working directory to remote FTP directory.", e); + } + catch (Exception e) { + throw new MessageDeliveryException(message, + "Error handling message for file [" + file + "]", e); + } + finally { + if (file.exists()) { + try { + file.delete(); + } + catch (Throwable th) { + // ignore + } + } + if (session != null) { + session.close(); + } + } + if (!sentSuccesfully) { + throw new MessageDeliveryException(message, "Failed to transfer file '" + file + "'"); + } } } @@ -121,9 +158,9 @@ public class FtpSendingMessageHandler extends AbstractMessageHandler { try { Object payload = message.getPayload(); String generateFileName = this.fileNameGenerator.generateFileName(message); - File tempFile = new File(temporaryBufferFolderFile, generateFileName + TEMPORARY_FILE_SUFFIX); - File resultFile = new File(temporaryBufferFolderFile, generateFileName); - File sendableFile; + File tempFile = new File(this.temporaryBufferFolderFile, generateFileName + TEMPORARY_FILE_SUFFIX); + File resultFile = new File(this.temporaryBufferFolderFile, generateFileName); + File sendableFile = null; if (payload instanceof String) { sendableFile = this.handleStringMessage((String) payload, tempFile, resultFile, this.charset); } @@ -133,63 +170,19 @@ public class FtpSendingMessageHandler extends AbstractMessageHandler { else if (payload instanceof byte[]) { sendableFile = this.handleByteArrayMessage((byte[]) payload, tempFile, resultFile); } - else { - sendableFile = null; - } return sendableFile; } - catch (Throwable th) { - throw new MessageDeliveryException(message); + catch (Exception e) { + throw new MessageDeliveryException(message, "Failed to create sendable file.", e); } } - @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()) { - Session session = this.sessionFactory.getSession(); - boolean sentSuccesfully; - try { - String targetDirectory = this.directoryExpressionProcesor.processMessage(message); - sentSuccesfully = sendFile(file, targetDirectory, session); - } - catch (FileNotFoundException e) { - throw new MessageDeliveryException(message, - "File [" + file + "] not found in local working directory; it was moved or deleted unexpectedly", e); - } - catch (IOException e) { - throw new MessageDeliveryException(message, - "Error transferring file [" + file + "] from local working directory to remote FTP directory", e); - } - catch (Exception e) { - throw new MessageDeliveryException(message, - "Error handling message for file [" + file + "]", e); - } - finally { - if (file.exists()) { - try { - file.delete(); - } - catch (Throwable th) { - // ignore - } - } - if (session != null) { - session.close(); - } - } - if (!sentSuccesfully) { - throw new MessageDeliveryException(message, "Failed to store file '" + file + "'"); - } - } - } - - private boolean sendFile(File file, String targetDirectory, Session session) throws FileNotFoundException, IOException { + private boolean sendFileToRemoteDirectory(File file, String remoteDirectory, Session session) throws FileNotFoundException, IOException { FileInputStream fileInputStream = new FileInputStream(file); - String remoteFilePath = targetDirectory + File.separatorChar + file.getName(); + if (!StringUtils.endsWithIgnoreCase(remoteDirectory, File.separator)) { + remoteDirectory += File.separatorChar; + } + String remoteFilePath = remoteDirectory + file.getName(); session.put(fileInputStream, remoteFilePath); fileInputStream.close(); return true; diff --git a/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/config/FtpOutboundChannelAdapterParser.java b/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/config/FtpOutboundChannelAdapterParser.java index d27ca3b93a..7ec3bf25eb 100644 --- a/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/config/FtpOutboundChannelAdapterParser.java +++ b/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/config/FtpOutboundChannelAdapterParser.java @@ -38,7 +38,7 @@ public class FtpOutboundChannelAdapterParser extends AbstractOutboundChannelAdap @Override protected AbstractBeanDefinition parseConsumer(Element element, ParserContext parserContext) { BeanDefinitionBuilder handlerBuilder = BeanDefinitionBuilder.genericBeanDefinition( - "org.springframework.integration.ftp.outbound.FtpSendingMessageHandler"); + "org.springframework.integration.file.remote.handler.FileTransferringMessageHandler"); BeanDefinitionBuilder sessionFactoryBuilder = BeanDefinitionBuilder.genericBeanDefinition( "org.springframework.integration.file.remote.session.CachingSessionFactory"); sessionFactoryBuilder.addConstructorArgReference(element.getAttribute("session-factory")); 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 index 14a7fb4f0f..2f08a982d2 100644 --- 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 @@ -28,7 +28,7 @@ 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.ftp.outbound.FtpSendingMessageHandler; +import org.springframework.integration.file.remote.handler.FileTransferringMessageHandler; import org.springframework.integration.message.GenericMessage; import org.springframework.integration.test.util.TestUtils; @@ -45,7 +45,7 @@ public class FtpParserOutboundTests { 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"); + FileTransferringMessageHandler handler = (FileTransferringMessageHandler) TestUtils.getPropertyValue(fileOutboundEndpoint, "handler"); Message message = new GenericMessage("ftp file generator test"); try { handler.handleMessage(message); diff --git a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/config/FtpOutboundChannelAdapterParserTests.java b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/config/FtpOutboundChannelAdapterParserTests.java index c8247cde66..93f51ad111 100644 --- a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/config/FtpOutboundChannelAdapterParserTests.java +++ b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/config/FtpOutboundChannelAdapterParserTests.java @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.integration.ftp.config; import static junit.framework.Assert.assertEquals; @@ -24,14 +25,14 @@ import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.integration.endpoint.EventDrivenConsumer; +import org.springframework.integration.file.remote.handler.FileTransferringMessageHandler; import org.springframework.integration.file.remote.session.CachingSessionFactory; -import org.springframework.integration.ftp.outbound.FtpSendingMessageHandler; import org.springframework.integration.ftp.session.DefaultFtpSessionFactory; import org.springframework.integration.test.util.TestUtils; /** * @author Oleg Zhurakousky - * + * @since 2.0 */ public class FtpOutboundChannelAdapterParserTests { @@ -43,7 +44,7 @@ public class FtpOutboundChannelAdapterParserTests { assertTrue(consumer instanceof EventDrivenConsumer); assertEquals(ac.getBean("ftpChannel"), TestUtils.getPropertyValue(consumer, "inputChannel")); assertEquals("ftpOutbound", ((EventDrivenConsumer)consumer).getComponentName()); - FtpSendingMessageHandler handler = (FtpSendingMessageHandler) TestUtils.getPropertyValue(consumer, "handler"); + FileTransferringMessageHandler handler = (FileTransferringMessageHandler) TestUtils.getPropertyValue(consumer, "handler"); assertEquals(ac.getBean("fileNameGenerator"), TestUtils.getPropertyValue(handler, "fileNameGenerator")); assertEquals("UTF-8", TestUtils.getPropertyValue(handler, "charset")); assertNotNull(TestUtils.getPropertyValue(handler, "temporaryBufferFolder")); diff --git a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/config/FtpsOutboundChannelAdapterParserTests.java b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/config/FtpsOutboundChannelAdapterParserTests.java index 12a5d0eb0b..8b6328c0cf 100644 --- a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/config/FtpsOutboundChannelAdapterParserTests.java +++ b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/config/FtpsOutboundChannelAdapterParserTests.java @@ -25,8 +25,8 @@ import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.integration.endpoint.EventDrivenConsumer; +import org.springframework.integration.file.remote.handler.FileTransferringMessageHandler; import org.springframework.integration.file.remote.session.CachingSessionFactory; -import org.springframework.integration.ftp.outbound.FtpSendingMessageHandler; import org.springframework.integration.ftp.session.DefaultFtpsSessionFactory; import org.springframework.integration.test.util.TestUtils; @@ -38,13 +38,12 @@ public class FtpsOutboundChannelAdapterParserTests { @Test public void testFtpsOutboundChannelAdapterComplete() throws Exception{ - ApplicationContext ac = - new ClassPathXmlApplicationContext("FtpsOutboundChannelAdapterParserTests-context.xml", this.getClass()); + ApplicationContext ac = new ClassPathXmlApplicationContext("FtpsOutboundChannelAdapterParserTests-context.xml", this.getClass()); Object consumer = ac.getBean("ftpOutbound"); assertTrue(consumer instanceof EventDrivenConsumer); assertEquals(ac.getBean("ftpChannel"), TestUtils.getPropertyValue(consumer, "inputChannel")); assertEquals("ftpOutbound", ((EventDrivenConsumer)consumer).getComponentName()); - FtpSendingMessageHandler handler = (FtpSendingMessageHandler) TestUtils.getPropertyValue(consumer, "handler"); + FileTransferringMessageHandler handler = (FileTransferringMessageHandler) TestUtils.getPropertyValue(consumer, "handler"); assertEquals(ac.getBean("fileNameGenerator"), TestUtils.getPropertyValue(handler, "fileNameGenerator")); assertEquals("UTF-8", TestUtils.getPropertyValue(handler, "charset")); assertNotNull(TestUtils.getPropertyValue(handler, "temporaryBufferFolder")); diff --git a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/outbound/FtpSendingMessageHandlerTest.java b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/outbound/FtpSendingMessageHandlerTest.java index 5cf32a66a3..4a80b529be 100644 --- a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/outbound/FtpSendingMessageHandlerTest.java +++ b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/outbound/FtpSendingMessageHandlerTest.java @@ -35,6 +35,7 @@ import org.mockito.stubbing.Answer; import org.springframework.expression.common.LiteralExpression; import org.springframework.integration.Message; import org.springframework.integration.file.FileNameGenerator; +import org.springframework.integration.file.remote.handler.FileTransferringMessageHandler; import org.springframework.integration.ftp.session.AbstractFtpSessionFactory; import org.springframework.integration.message.GenericMessage; import org.springframework.util.FileCopyUtils; @@ -65,7 +66,7 @@ public class FtpSendingMessageHandlerTest { file.delete(); } assertFalse(file.exists()); - FtpSendingMessageHandler handler = new FtpSendingMessageHandler(sessionFactory); + FileTransferringMessageHandler handler = new FileTransferringMessageHandler(sessionFactory); handler.setRemoteDirectoryExpression(new LiteralExpression("remote-target-dir")); handler.setFileNameGenerator(new FileNameGenerator() { public String generateFileName(Message message) { @@ -83,7 +84,7 @@ public class FtpSendingMessageHandlerTest { file.delete(); } assertFalse(file.exists()); - FtpSendingMessageHandler handler = new FtpSendingMessageHandler(sessionFactory); + FileTransferringMessageHandler handler = new FileTransferringMessageHandler(sessionFactory); handler.setRemoteDirectoryExpression(new LiteralExpression("remote-target-dir")); handler.setFileNameGenerator(new FileNameGenerator() { public String generateFileName(Message message) { @@ -102,7 +103,7 @@ public class FtpSendingMessageHandlerTest { file.delete(); } assertFalse(file.exists()); - FtpSendingMessageHandler handler = new FtpSendingMessageHandler(sessionFactory); + FileTransferringMessageHandler handler = new FileTransferringMessageHandler(sessionFactory); handler.setRemoteDirectoryExpression(new LiteralExpression("remote-target-dir")); handler.setFileNameGenerator(new FileNameGenerator() { public String generateFileName(Message message) { diff --git a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/config/SftpOutboundChannelAdapterParser.java b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/config/SftpOutboundChannelAdapterParser.java index 32c527d9c1..51c1cbae9d 100644 --- a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/config/SftpOutboundChannelAdapterParser.java +++ b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/config/SftpOutboundChannelAdapterParser.java @@ -45,7 +45,7 @@ public class SftpOutboundChannelAdapterParser extends AbstractOutboundChannelAda String sessionPoolName = BeanDefinitionReaderUtils.registerWithGeneratedName( sessionPoolBuilder.getBeanDefinition(), parserContext.getRegistry()); BeanDefinitionBuilder handlerBuilder = BeanDefinitionBuilder.genericBeanDefinition( - "org.springframework.integration.sftp.outbound.SftpSendingMessageHandler"); + "org.springframework.integration.file.remote.handler.FileTransferringMessageHandler"); handlerBuilder.addConstructorArgReference(sessionPoolName); IntegrationNamespaceUtils.setValueIfAttributeDefined(handlerBuilder, element, "charset"); String remoteDirectory = element.getAttribute("remote-directory"); diff --git a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/outbound/SftpSendingMessageHandler.java b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/outbound/SftpSendingMessageHandler.java deleted file mode 100644 index 48b6e10007..0000000000 --- a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/outbound/SftpSendingMessageHandler.java +++ /dev/null @@ -1,190 +0,0 @@ -/* - * 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.sftp.outbound; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStreamWriter; -import java.nio.charset.Charset; - -import org.springframework.core.io.FileSystemResource; -import org.springframework.core.io.Resource; -import org.springframework.expression.Expression; -import org.springframework.integration.Message; -import org.springframework.integration.MessageDeliveryException; -import org.springframework.integration.MessagingException; -import org.springframework.integration.file.DefaultFileNameGenerator; -import org.springframework.integration.file.FileNameGenerator; -import org.springframework.integration.file.remote.session.Session; -import org.springframework.integration.file.remote.session.SessionFactory; -import org.springframework.integration.handler.AbstractMessageHandler; -import org.springframework.integration.handler.ExpressionEvaluatingMessageProcessor; -import org.springframework.util.Assert; -import org.springframework.util.FileCopyUtils; -import org.springframework.util.StringUtils; - -/** - * Sends message payloads to a remote SFTP endpoint. - * Assumes that the payload of the inbound message is of type {@link java.io.File}. - * - * @author Josh Long - * @author Oleg Zhurakousky - * @since 2.0 - */ -public class SftpSendingMessageHandler extends AbstractMessageHandler { - - private static final String TEMPORARY_FILE_SUFFIX = ".writing"; - - private final SessionFactory sessionFactory; - - private volatile ExpressionEvaluatingMessageProcessor directoryExpressionProcesor; - - private volatile Expression remoteDirectoryExpression; - - private volatile FileNameGenerator fileNameGenerator = new DefaultFileNameGenerator(); - - private volatile File temporaryBufferFolderFile; - - private volatile Resource temporaryBufferFolder = - new FileSystemResource(System.getProperty("java.io.tmpdir")); - - private volatile String charset = Charset.defaultCharset().name(); - - - public SftpSendingMessageHandler(SessionFactory sessionFactory) { - Assert.notNull(sessionFactory, "sessionFactory must not be null"); - this.sessionFactory = sessionFactory; - } - - - public void setTemporaryBufferFolder(Resource temporaryBufferFolder) { - this.temporaryBufferFolder = temporaryBufferFolder; - } - - public void setFileNameGenerator(FileNameGenerator fileNameGenerator) { - this.fileNameGenerator = fileNameGenerator; - } - - public void setRemoteDirectoryExpression(Expression remoteDirectoryExpression) { - this.remoteDirectoryExpression = remoteDirectoryExpression; - } - - public void setCharset(String charset) { - this.charset = charset; - } - - @Override - protected void onInit() throws Exception { - this.temporaryBufferFolderFile = this.temporaryBufferFolder.getFile(); - if (this.remoteDirectoryExpression != null) { - this.directoryExpressionProcesor = - new ExpressionEvaluatingMessageProcessor(this.remoteDirectoryExpression, String.class); - } - } - - @Override - protected void handleMessageInternal(Message message) throws Exception { - File inboundFilePayload = this.redeemForStorableFile(message); - try { - if ((inboundFilePayload != null) && inboundFilePayload.exists()) { - this.sendFileToRemoteEndpoint(message, inboundFilePayload); - } - } - catch (Exception e) { - throw new MessageDeliveryException(message, "Failed to transfer '" + message.getPayload() + "' to " + - this.remoteDirectoryExpression.getExpressionString(), e); - } - finally { - if (inboundFilePayload != null && inboundFilePayload.exists()) { - inboundFilePayload.delete(); - } - } - } - - private File handleFileMessage(File sourceFile, File tempFile, File resultFile) throws IOException { - FileCopyUtils.copy(sourceFile, tempFile); - tempFile.renameTo(resultFile); - return resultFile; - } - - private File handleByteArrayMessage(byte[] bytes, File tempFile, File resultFile) throws IOException { - FileCopyUtils.copy(bytes, tempFile); - tempFile.renameTo(resultFile); - return resultFile; - } - - private File handleStringMessage(String content, File tempFile, File resultFile, String charset) throws IOException { - OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(tempFile), charset); - FileCopyUtils.copy(content, writer); - tempFile.renameTo(resultFile); - return resultFile; - } - - private File redeemForStorableFile(Message message) throws MessageDeliveryException { - try { - Object payload = message.getPayload(); - String generateFileName = this.fileNameGenerator.generateFileName(message); - File tempFile = new File(this.temporaryBufferFolderFile, generateFileName + TEMPORARY_FILE_SUFFIX); - File resultFile = new File(this.temporaryBufferFolderFile, generateFileName); - File sendableFile = null; - if (payload instanceof String) { - sendableFile = this.handleStringMessage((String) payload, tempFile, resultFile, this.charset); - } - else if (payload instanceof File) { - sendableFile = this.handleFileMessage((File) payload, tempFile, resultFile); - } - else if (payload instanceof byte[]) { - sendableFile = this.handleByteArrayMessage((byte[]) payload, tempFile, resultFile); - } - return sendableFile; - } - catch (Exception e) { - throw new MessageDeliveryException(message, "Failed to create sendable file.", e); - } - } - - private boolean sendFileToRemoteEndpoint(Message message, File file) throws Exception { - Session session = this.sessionFactory.getSession(); - if (session == null) { - throw new MessagingException("The session returned from the pool is null, cannot proceed."); - } - InputStream fileInputStream = null; - try { - fileInputStream = new FileInputStream(file); - String baseOfRemotePath = ""; - if (this.directoryExpressionProcesor != null) { - String result = this.directoryExpressionProcesor.processMessage(message); - if (StringUtils.hasText(result)) { - baseOfRemotePath = result; - } - } - if (!StringUtils.endsWithIgnoreCase(baseOfRemotePath, "/")) { - baseOfRemotePath += "/"; - } - session.put(fileInputStream, baseOfRemotePath + file.getName()); - return true; - } - finally { - fileInputStream.close(); - session.close(); - } - } - -} diff --git a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/OutboundChannelAdapaterParserTests.java b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/OutboundChannelAdapaterParserTests.java index 0c096b1a73..85bbea7c96 100644 --- a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/OutboundChannelAdapaterParserTests.java +++ b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/OutboundChannelAdapaterParserTests.java @@ -30,8 +30,8 @@ import org.springframework.expression.common.LiteralExpression; import org.springframework.expression.spel.standard.SpelExpression; import org.springframework.integration.endpoint.EventDrivenConsumer; import org.springframework.integration.file.FileNameGenerator; +import org.springframework.integration.file.remote.handler.FileTransferringMessageHandler; import org.springframework.integration.file.remote.session.CachingSessionFactory; -import org.springframework.integration.sftp.outbound.SftpSendingMessageHandler; import org.springframework.integration.sftp.session.DefaultSftpSessionFactory; import org.springframework.integration.test.util.TestUtils; @@ -49,8 +49,8 @@ public class OutboundChannelAdapaterParserTests { assertTrue(consumer instanceof EventDrivenConsumer); assertEquals(context.getBean("inputChannel"), TestUtils.getPropertyValue(consumer, "inputChannel")); assertEquals("sftpOutboundAdapter", ((EventDrivenConsumer)consumer).getComponentName()); - SftpSendingMessageHandler handler = (SftpSendingMessageHandler) TestUtils.getPropertyValue(consumer, "handler"); - Expression remoteDirectoryExpression = (Expression) TestUtils.getPropertyValue(handler, "remoteDirectoryExpression"); + FileTransferringMessageHandler handler = (FileTransferringMessageHandler) TestUtils.getPropertyValue(consumer, "handler"); + Expression remoteDirectoryExpression = (Expression) TestUtils.getPropertyValue(handler, "directoryExpressionProcessor.expression"); assertNotNull(remoteDirectoryExpression); assertTrue(remoteDirectoryExpression instanceof LiteralExpression); assertEquals(context.getBean("fileNameGenerator"), TestUtils.getPropertyValue(handler, "fileNameGenerator")); @@ -71,8 +71,8 @@ public class OutboundChannelAdapaterParserTests { assertTrue(consumer instanceof EventDrivenConsumer); assertEquals(context.getBean("inputChannel"), TestUtils.getPropertyValue(consumer, "inputChannel")); assertEquals("sftpOutboundAdapterWithExpression", ((EventDrivenConsumer)consumer).getComponentName()); - SftpSendingMessageHandler handler = (SftpSendingMessageHandler) TestUtils.getPropertyValue(consumer, "handler"); - SpelExpression remoteDirectoryExpression = (SpelExpression) TestUtils.getPropertyValue(handler, "remoteDirectoryExpression"); + FileTransferringMessageHandler handler = (FileTransferringMessageHandler) TestUtils.getPropertyValue(consumer, "handler"); + SpelExpression remoteDirectoryExpression = (SpelExpression) TestUtils.getPropertyValue(handler, "directoryExpressionProcessor.expression"); assertNotNull(remoteDirectoryExpression); assertEquals("'foo' + '/' + 'bar'", remoteDirectoryExpression.getExpressionString()); FileNameGenerator generator = (FileNameGenerator) TestUtils.getPropertyValue(handler, "fileNameGenerator"); diff --git a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/outbound/SftpSendingMessageHandlerTests.java b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/outbound/SftpSendingMessageHandlerTests.java index 2a7eb341f5..b0aa881317 100644 --- a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/outbound/SftpSendingMessageHandlerTests.java +++ b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/outbound/SftpSendingMessageHandlerTests.java @@ -27,6 +27,7 @@ import java.io.File; import org.junit.Test; import org.springframework.expression.spel.standard.SpelExpressionParser; +import org.springframework.integration.file.remote.handler.FileTransferringMessageHandler; import org.springframework.integration.file.remote.session.Session; import org.springframework.integration.file.remote.session.SessionFactory; import org.springframework.integration.message.GenericMessage; @@ -43,9 +44,8 @@ public class SftpSendingMessageHandlerTests { SessionFactory sessionFactory = mock(SessionFactory.class); Session session = mock(Session.class); when(sessionFactory.getSession()).thenReturn(session); - SftpSendingMessageHandler handler = new SftpSendingMessageHandler(sessionFactory); + FileTransferringMessageHandler handler = new FileTransferringMessageHandler(sessionFactory); handler.setRemoteDirectoryExpression(new SpelExpressionParser().parseExpression("'foo.txt'")); - handler.handleMessage(new GenericMessage("hello")); verify(sessionFactory, times(1)).getSession(); } @@ -56,7 +56,7 @@ public class SftpSendingMessageHandlerTests { SessionFactory sessionFactory = mock(SessionFactory.class); Session session = mock(Session.class); when(sessionFactory.getSession()).thenReturn(session); - SftpSendingMessageHandler handler = new SftpSendingMessageHandler(sessionFactory); + FileTransferringMessageHandler handler = new FileTransferringMessageHandler(sessionFactory); handler.setRemoteDirectoryExpression(new SpelExpressionParser().parseExpression("'foo.txt'")); handler.handleMessage(new GenericMessage("hello".getBytes())); @@ -69,7 +69,7 @@ public class SftpSendingMessageHandlerTests { SessionFactory sessionFactory = mock(SessionFactory.class); Session session = mock(Session.class); when(sessionFactory.getSession()).thenReturn(session); - SftpSendingMessageHandler handler = new SftpSendingMessageHandler(sessionFactory); + FileTransferringMessageHandler handler = new FileTransferringMessageHandler(sessionFactory); handler.setRemoteDirectoryExpression(new SpelExpressionParser().parseExpression("'foo.txt'")); handler.handleMessage(new GenericMessage("hello".getBytes()));