use of org.springframework.integration.file.remote.session.Session in project spring-integration by spring-projects.
the class RemoteFileOutboundGatewayTests method testLs_1_dirs_links.
@Test
public void testLs_1_dirs_links() throws Exception {
SessionFactory sessionFactory = mock(SessionFactory.class);
Session session = mock(Session.class);
TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway(sessionFactory, "ls", "payload");
gw.setOptions("-1 -dirs -links");
gw.afterPropertiesSet();
when(sessionFactory.getSession()).thenReturn(session);
TestLsEntry[] files = fileList();
when(session.list("testremote/")).thenReturn(files);
@SuppressWarnings("unchecked") MessageBuilder<List<String>> out = (MessageBuilder<List<String>>) gw.handleRequestMessage(new GenericMessage<>("testremote"));
assertEquals(4, out.getPayload().size());
assertEquals("f1", out.getPayload().get(0));
assertEquals("f2", out.getPayload().get(1));
assertEquals("f3", out.getPayload().get(2));
assertEquals("f4", out.getPayload().get(3));
}
use of org.springframework.integration.file.remote.session.Session in project spring-integration by spring-projects.
the class RemoteFileOutboundGatewayTests method testLs_f_R.
@Test
public void testLs_f_R() throws Exception {
SessionFactory sessionFactory = mock(SessionFactory.class);
Session session = mock(Session.class);
TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway(sessionFactory, "ls", "payload");
gw.setOptions("-f -R");
gw.afterPropertiesSet();
when(sessionFactory.getSession()).thenReturn(session);
TestLsEntry[] level1 = level1List();
TestLsEntry[] level2 = level2List();
TestLsEntry[] level3 = level3List();
when(session.list("testremote/x/")).thenReturn(level1);
when(session.list("testremote/x/d1/")).thenReturn(level2);
when(session.list("testremote/x/d1/d2/")).thenReturn(level3);
@SuppressWarnings("unchecked") MessageBuilder<List<TestLsEntry>> out = (MessageBuilder<List<TestLsEntry>>) gw.handleRequestMessage(new GenericMessage<>("testremote/x"));
assertEquals(4, out.getPayload().size());
assertEquals("f1", out.getPayload().get(0).getFilename());
assertEquals("d1/d2/f4", out.getPayload().get(1).getFilename());
assertEquals("d1/f3", out.getPayload().get(2).getFilename());
assertEquals("f2", out.getPayload().get(3).getFilename());
assertEquals("testremote/x/", out.getHeaders().get(FileHeaders.REMOTE_DIRECTORY));
}
use of org.springframework.integration.file.remote.session.Session in project spring-integration by spring-projects.
the class AbstractRemoteFileOutboundGateway method doGet.
private Object doGet(final Message<?> requestMessage) {
final String remoteFilePath = this.fileNameProcessor.processMessage(requestMessage);
final String remoteFilename = getRemoteFilename(remoteFilePath);
final String remoteDir = getRemoteDirectory(remoteFilePath, remoteFilename);
Session<F> session = null;
Object payload;
if (this.options.contains(Option.STREAM)) {
session = this.remoteFileTemplate.getSessionFactory().getSession();
try {
payload = session.readRaw(remoteFilePath);
} catch (IOException e) {
throw new MessageHandlingException(requestMessage, "Failed to get the remote file [" + remoteFilePath + "] as a stream", e);
}
} else {
payload = this.remoteFileTemplate.execute(session1 -> get(requestMessage, session1, remoteDir, remoteFilePath, remoteFilename, null));
}
return getMessageBuilderFactory().withPayload(payload).setHeader(FileHeaders.REMOTE_DIRECTORY, remoteDir).setHeader(FileHeaders.REMOTE_FILE, remoteFilename).setHeader(IntegrationMessageHeaderAccessor.CLOSEABLE_RESOURCE, session);
}
use of org.springframework.integration.file.remote.session.Session in project spring-integration by spring-projects.
the class SftpOutboundTests method testSharedSession.
@Test
public void testSharedSession() throws Exception {
JSch jsch = spy(new JSch());
Constructor<com.jcraft.jsch.Session> ctor = com.jcraft.jsch.Session.class.getDeclaredConstructor(JSch.class, String.class, String.class, int.class);
ctor.setAccessible(true);
com.jcraft.jsch.Session jschSession1 = spy(ctor.newInstance(jsch, "foo", "host", 22));
com.jcraft.jsch.Session jschSession2 = spy(ctor.newInstance(jsch, "foo", "host", 22));
willAnswer(invocation -> {
new DirectFieldAccessor(jschSession1).setPropertyValue("isConnected", true);
return null;
}).given(jschSession1).connect();
willAnswer(invocation -> {
new DirectFieldAccessor(jschSession2).setPropertyValue("isConnected", true);
return null;
}).given(jschSession2).connect();
when(jsch.getSession("foo", "host", 22)).thenReturn(jschSession1, jschSession2);
final ChannelSftp channel1 = spy(new ChannelSftp());
doReturn("channel1").when(channel1).toString();
final ChannelSftp channel2 = spy(new ChannelSftp());
doReturn("channel2").when(channel2).toString();
new DirectFieldAccessor(channel1).setPropertyValue("session", jschSession1);
new DirectFieldAccessor(channel2).setPropertyValue("session", jschSession1);
// Can't use when(session.open()) with a spy
final AtomicInteger n = new AtomicInteger();
doAnswer(invocation -> n.getAndIncrement() == 0 ? channel1 : channel2).when(jschSession1).openChannel("sftp");
DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(jsch, true);
factory.setHost("host");
factory.setUser("foo");
factory.setPassword("bar");
noopConnect(channel1);
noopConnect(channel2);
Session<LsEntry> s1 = factory.getSession();
Session<LsEntry> s2 = factory.getSession();
assertSame(TestUtils.getPropertyValue(s1, "jschSession"), TestUtils.getPropertyValue(s2, "jschSession"));
assertSame(channel1, TestUtils.getPropertyValue(s1, "channel"));
assertSame(channel2, TestUtils.getPropertyValue(s2, "channel"));
}
use of org.springframework.integration.file.remote.session.Session in project spring-integration by spring-projects.
the class FtpServerOutboundTests method testStream.
@Test
public void testStream() {
String dir = "ftpSource/";
this.inboundGetStream.send(new GenericMessage<Object>(dir + " ftpSource1.txt"));
Message<?> result = this.output.receive(1000);
assertNotNull(result);
assertEquals("source1", result.getPayload());
assertEquals("ftpSource/", result.getHeaders().get(FileHeaders.REMOTE_DIRECTORY));
assertEquals(" ftpSource1.txt", result.getHeaders().get(FileHeaders.REMOTE_FILE));
Session<?> session = (Session<?>) result.getHeaders().get(IntegrationMessageHeaderAccessor.CLOSEABLE_RESOURCE);
// Returned to cache
assertTrue(session.isOpen());
// Raw reading is finished
assertFalse(TestUtils.getPropertyValue(session, "targetSession.readingRaw", AtomicBoolean.class).get());
// Check that we can use the same session from cache to read another remote InputStream
this.inboundGetStream.send(new GenericMessage<Object>(dir + "ftpSource2.txt"));
result = this.output.receive(1000);
assertNotNull(result);
assertEquals("source2", result.getPayload());
assertEquals("ftpSource/", result.getHeaders().get(FileHeaders.REMOTE_DIRECTORY));
assertEquals("ftpSource2.txt", result.getHeaders().get(FileHeaders.REMOTE_FILE));
assertSame(TestUtils.getPropertyValue(session, "targetSession"), TestUtils.getPropertyValue(result.getHeaders().get(IntegrationMessageHeaderAccessor.CLOSEABLE_RESOURCE), "targetSession"));
}
Aggregations