INT-3622: (S)FTP: Add MessageSessionCallback

JIRA: https://jira.spring.io/browse/INT-3622

INT-3622: Polishing
This commit is contained in:
Artem Bilan
2015-08-07 23:18:52 -04:00
committed by Gary Russell
parent d89dabe72f
commit e408331e67
14 changed files with 396 additions and 59 deletions

View File

@@ -22,6 +22,7 @@ import java.util.List;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.integration.file.remote.AbstractFileInfo;
import org.springframework.integration.file.remote.MessageSessionCallback;
import org.springframework.integration.file.remote.RemoteFileTemplate;
import org.springframework.integration.file.remote.gateway.AbstractRemoteFileOutboundGateway;
import org.springframework.integration.file.remote.session.SessionFactory;
@@ -33,19 +34,55 @@ import com.jcraft.jsch.ChannelSftp.LsEntry;
* Outbound Gateway for performing remote file operations via SFTP.
*
* @author Gary Russell
* @author Artem Bilan
* @since 2.1
*/
public class SftpOutboundGateway extends AbstractRemoteFileOutboundGateway<LsEntry> {
/**
* Construct an instance using the provided session factory and callback for
* performing operations on the session.
* @param sessionFactory the session factory.
* @param messageSessionCallback the callback.
*/
public SftpOutboundGateway(SessionFactory<LsEntry> sessionFactory,
MessageSessionCallback<LsEntry, ?> messageSessionCallback) {
super(sessionFactory, messageSessionCallback);
}
/**
* Construct an instance with the supplied remote file template and callback
* for performing operations on the session.
* @param remoteFileTemplate the remote file template.
* @param messageSessionCallback the callback.
*/
public SftpOutboundGateway(RemoteFileTemplate<LsEntry> remoteFileTemplate,
MessageSessionCallback<LsEntry, ?> messageSessionCallback) {
super(remoteFileTemplate, messageSessionCallback);
}
/**
* Construct an instance with the supplied session factory, a command ('ls', 'get'
* etc), and an expression to determine the filename.
* @param sessionFactory the session factory.
* @param command the command.
* @param expression the filename expression.
*/
public SftpOutboundGateway(SessionFactory<LsEntry> sessionFactory, String command, String expression) {
super(sessionFactory, command, expression);
}
/**
* Construct an instance with the supplied remote file template, a command ('ls',
* 'get' etc), and an expression to determine the filename.
* @param remoteFileTemplate the remote file template.
* @param command the command.
* @param expression the filename expression.
*/
public SftpOutboundGateway(RemoteFileTemplate<LsEntry> remoteFileTemplate, String command, String expression) {
super(remoteFileTemplate, command, expression);
}
@Override
protected boolean isDirectory(LsEntry file) {
return file.getAttrs().isDir();

View File

@@ -250,7 +250,7 @@
<xsd:element name="request-handler-advice-chain" type="integration:handlerAdviceChainType"
minOccurs="0" maxOccurs="1" />
</xsd:all>
<xsd:attribute name="command" use="required">
<xsd:attribute name="command">
<xsd:annotation>
<xsd:documentation>
sftp command.
@@ -260,6 +260,20 @@
<xsd:union memberTypes="int-file:remoteGatewayCommand xsd:string"/>
</xsd:simpleType>
</xsd:attribute>
<xsd:attribute name="session-callback" type="xsd:string">
<xsd:annotation>
<xsd:appinfo>
<tool:annotation kind="ref">
<tool:expected-type
type="org.springframework.integration.file.remote.MessageSessionCallback" />
</tool:annotation>
</xsd:appinfo>
<xsd:documentation>
The 'MessageSessionCallback' bean reference to perform custom operation(s) on 'Session'
with 'requestMessage'.
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="command-options" type="xsd:string">
<xsd:annotation>
<xsd:documentation>
@@ -276,7 +290,7 @@
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="expression" use="required" type="xsd:string">
<xsd:attribute name="expression" type="xsd:string">
<xsd:annotation>
<xsd:documentation>
SpEL expression representing the path in the

View File

@@ -153,4 +153,13 @@
<int:service-activator input-channel="markers"
expression="payload.mark.toString().equals('END') ? headers['file_remoteSession'].close() : null"/>
<int-sftp:outbound-gateway
session-factory="sftpSessionFactory"
session-callback="messageSessionCallback"
request-channel="inboundCallback"
reply-channel="output"/>
<bean id="messageSessionCallback"
class="org.springframework.integration.sftp.outbound.SftpServerOutboundTests$TestMessageSessionCallback"/>
</beans>

View File

@@ -47,6 +47,7 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.file.FileHeaders;
import org.springframework.integration.file.remote.MessageSessionCallback;
import org.springframework.integration.file.remote.SessionCallback;
import org.springframework.integration.file.remote.session.Session;
import org.springframework.integration.sftp.TestSftpServer;
@@ -62,6 +63,7 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.FileCopyUtils;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.ChannelSftp.LsEntry;
/**
@@ -129,6 +131,9 @@ public class SftpServerOutboundTests {
@Autowired
private DirectChannel inboundGetStream;
@Autowired
private DirectChannel inboundCallback;
@Before
@After
public void setup() {
@@ -420,6 +425,14 @@ public class SftpServerOutboundTests {
assertFalse(((Session<?>) result.getHeaders().get(FileHeaders.REMOTE_SESSION)).isOpen());
}
@Test
public void testMessageSessionCallback() {
this.inboundCallback.send(new GenericMessage<String>("foo"));
Message<?> receive = this.output.receive(10000);
assertNotNull(receive);
assertEquals("FOO", receive.getPayload());
}
private void assertLength6(SftpRemoteFileTemplate template) {
LsEntry[] files = template.execute(new SessionCallback<LsEntry, LsEntry[]>() {
@@ -432,4 +445,14 @@ public class SftpServerOutboundTests {
assertEquals(6, files[0].getAttrs().getSize());
}
private static final class TestMessageSessionCallback
implements MessageSessionCallback<LsEntry, Object> {
@Override
public Object doInSession(Session<ChannelSftp.LsEntry> session, Message<?> requestMessage) throws IOException {
return ((String) requestMessage.getPayload()).toUpperCase();
}
}
}