INT-2838 Allow @bean Expressions in (S)FTP Out
The message processors previously did not have a bean resolver. * Add the bean factory * Add tests
This commit is contained in:
committed by
Gunnar Hillert
parent
dd6af7b387
commit
969b3bec27
@@ -192,6 +192,9 @@ public abstract class AbstractRemoteFileOutboundGateway<F> extends AbstractReply
|
||||
"Failure during initialization of: " + this.getComponentType(), e);
|
||||
}
|
||||
}
|
||||
if (this.getBeanFactory() != null) {
|
||||
this.processor.setBeanFactory(this.getBeanFactory());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -23,6 +23,8 @@ import java.io.IOException;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.BeanFactoryAware;
|
||||
import org.springframework.expression.Expression;
|
||||
import org.springframework.integration.Message;
|
||||
import org.springframework.integration.MessageDeliveryException;
|
||||
@@ -64,6 +66,8 @@ public class FileTransferringMessageHandler<F> extends AbstractMessageHandler {
|
||||
|
||||
private volatile FileNameGenerator fileNameGenerator = new DefaultFileNameGenerator();
|
||||
|
||||
private volatile boolean fileNameGeneratorSet;
|
||||
|
||||
private volatile File temporaryDirectory = new File(System.getProperty("java.io.tmpdir"));
|
||||
|
||||
private volatile String charset = "UTF-8";
|
||||
@@ -119,6 +123,7 @@ public class FileTransferringMessageHandler<F> extends AbstractMessageHandler {
|
||||
|
||||
public void setFileNameGenerator(FileNameGenerator fileNameGenerator) {
|
||||
this.fileNameGenerator = (fileNameGenerator != null) ? fileNameGenerator : new DefaultFileNameGenerator();
|
||||
this.fileNameGeneratorSet = fileNameGenerator != null;
|
||||
}
|
||||
|
||||
public void setCharset(String charset) {
|
||||
@@ -134,6 +139,16 @@ public class FileTransferringMessageHandler<F> extends AbstractMessageHandler {
|
||||
@Override
|
||||
protected void onInit() throws Exception {
|
||||
Assert.notNull(this.directoryExpressionProcessor, "remoteDirectoryExpression is required");
|
||||
BeanFactory beanFactory = this.getBeanFactory();
|
||||
if (beanFactory != null) {
|
||||
this.directoryExpressionProcessor.setBeanFactory(beanFactory);
|
||||
if (this.temporaryDirectoryExpressionProcessor != null) {
|
||||
this.temporaryDirectoryExpressionProcessor.setBeanFactory(beanFactory);
|
||||
}
|
||||
if (!this.fileNameGeneratorSet && this.fileNameGenerator instanceof BeanFactoryAware) {
|
||||
((BeanFactoryAware) this.fileNameGenerator).setBeanFactory(beanFactory);
|
||||
}
|
||||
}
|
||||
if (this.autoCreateDirectory){
|
||||
Assert.hasText(this.remoteFileSeparator, "'remoteFileSeparator' must not be empty when 'autoCreateDirectory' is set to 'true'");
|
||||
}
|
||||
|
||||
@@ -76,4 +76,24 @@
|
||||
<bean id="fileNameGenerator" class="org.mockito.Mockito" factory-method="mock">
|
||||
<constructor-arg value="org.springframework.integration.file.FileNameGenerator"/>
|
||||
</bean>
|
||||
|
||||
<int-ftp:outbound-channel-adapter id="withBeanExpressions"
|
||||
channel="ftpChannel"
|
||||
remote-directory-expression="@fooBean"
|
||||
temporary-remote-directory-expression="@barBean"
|
||||
remote-filename-generator-expression="@bazBean"
|
||||
session-factory="cachingSessionFactory" />
|
||||
|
||||
<bean id="fooBean" class="java.lang.String">
|
||||
<constructor-arg value="foo" />
|
||||
</bean>
|
||||
|
||||
<bean id="barBean" class="java.lang.String">
|
||||
<constructor-arg value="bar" />
|
||||
</bean>
|
||||
|
||||
<bean id="bazBean" class="java.lang.String">
|
||||
<constructor-arg value="baz" />
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
|
||||
@@ -33,11 +33,14 @@ import org.springframework.integration.Message;
|
||||
import org.springframework.integration.channel.PublishSubscribeChannel;
|
||||
import org.springframework.integration.core.MessageHandler;
|
||||
import org.springframework.integration.endpoint.EventDrivenConsumer;
|
||||
import org.springframework.integration.file.DefaultFileNameGenerator;
|
||||
import org.springframework.integration.file.remote.handler.FileTransferringMessageHandler;
|
||||
import org.springframework.integration.file.remote.session.CachingSessionFactory;
|
||||
import org.springframework.integration.ftp.session.DefaultFtpSessionFactory;
|
||||
import org.springframework.integration.handler.ExpressionEvaluatingMessageProcessor;
|
||||
import org.springframework.integration.handler.advice.AbstractRequestHandlerAdvice;
|
||||
import org.springframework.integration.message.GenericMessage;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.integration.test.util.TestUtils;
|
||||
|
||||
/**
|
||||
@@ -120,6 +123,27 @@ public class FtpOutboundChannelAdapterParserTests {
|
||||
assertFalse((Boolean)TestUtils.getPropertyValue(handler,"useTemporaryFileName"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBeanExpressions() throws Exception{
|
||||
ApplicationContext ac =
|
||||
new ClassPathXmlApplicationContext("FtpOutboundChannelAdapterParserTests-context.xml", this.getClass());
|
||||
Object consumer = ac.getBean("withBeanExpressions");
|
||||
FileTransferringMessageHandler<?> handler = TestUtils.getPropertyValue(consumer, "handler", FileTransferringMessageHandler.class);
|
||||
ExpressionEvaluatingMessageProcessor<?> dirExpProc = TestUtils.getPropertyValue(handler,
|
||||
"directoryExpressionProcessor", ExpressionEvaluatingMessageProcessor.class);
|
||||
assertNotNull(dirExpProc);
|
||||
Message<String> message = MessageBuilder.withPayload("qux").build();
|
||||
assertEquals("foo", dirExpProc.processMessage(message));
|
||||
ExpressionEvaluatingMessageProcessor<?> tempDirExpProc = TestUtils.getPropertyValue(handler,
|
||||
"temporaryDirectoryExpressionProcessor", ExpressionEvaluatingMessageProcessor.class);
|
||||
assertNotNull(tempDirExpProc);
|
||||
assertEquals("bar", tempDirExpProc.processMessage(message));
|
||||
DefaultFileNameGenerator generator = TestUtils.getPropertyValue(handler,
|
||||
"fileNameGenerator", DefaultFileNameGenerator.class);
|
||||
assertNotNull(generator);
|
||||
assertEquals("baz", generator.generateFileName(message));
|
||||
}
|
||||
|
||||
public static class FooAdvice extends AbstractRequestHandlerAdvice {
|
||||
|
||||
@Override
|
||||
|
||||
@@ -47,6 +47,25 @@
|
||||
</int-ftp:request-handler-advice-chain>
|
||||
</int-ftp:outbound-gateway>
|
||||
|
||||
<int-ftp:outbound-gateway id="withBeanExpression"
|
||||
local-directory="local-test-dir"
|
||||
session-factory="sf"
|
||||
request-channel="inbound3"
|
||||
reply-channel="outbound"
|
||||
auto-startup="false"
|
||||
cache-sessions="false"
|
||||
filename-pattern="*"
|
||||
remote-file-separator="X"
|
||||
command="ls"
|
||||
command-options="-1 -f"
|
||||
expression="@fooBean"
|
||||
order="1"
|
||||
/>
|
||||
|
||||
<int:channel id="outbound"/>
|
||||
|
||||
<bean id="fooBean" class="java.lang.String">
|
||||
<constructor-arg value="foo" />
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
|
||||
@@ -30,8 +30,10 @@ import org.springframework.integration.Message;
|
||||
import org.springframework.integration.endpoint.AbstractEndpoint;
|
||||
import org.springframework.integration.file.remote.session.CachingSessionFactory;
|
||||
import org.springframework.integration.ftp.gateway.FtpOutboundGateway;
|
||||
import org.springframework.integration.handler.ExpressionEvaluatingMessageProcessor;
|
||||
import org.springframework.integration.handler.advice.AbstractRequestHandlerAdvice;
|
||||
import org.springframework.integration.message.GenericMessage;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.integration.test.util.TestUtils;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
@@ -54,6 +56,9 @@ public class FtpOutboundGatewayParserTests {
|
||||
@Autowired
|
||||
AbstractEndpoint gateway2;
|
||||
|
||||
@Autowired
|
||||
AbstractEndpoint withBeanExpression;
|
||||
|
||||
private static volatile int adviceCalled;
|
||||
|
||||
@Test
|
||||
@@ -94,6 +99,16 @@ public class FtpOutboundGatewayParserTests {
|
||||
assertEquals(1, adviceCalled);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithBeanExpression() {
|
||||
FtpOutboundGateway gateway = TestUtils.getPropertyValue(withBeanExpression,
|
||||
"handler", FtpOutboundGateway.class);
|
||||
ExpressionEvaluatingMessageProcessor<?> processor = TestUtils.getPropertyValue(gateway, "processor",
|
||||
ExpressionEvaluatingMessageProcessor.class);
|
||||
assertNotNull(processor);
|
||||
assertEquals("foo", processor.processMessage(MessageBuilder.withPayload("bar").build()));
|
||||
}
|
||||
|
||||
public static class FooAdvice extends AbstractRequestHandlerAdvice {
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user