Search in sources :

Example 1 with DefaultSftpSessionFactory

use of org.springframework.integration.sftp.session.DefaultSftpSessionFactory in project spring-integration by spring-projects.

the class SftpOutboundTests method testNotSharedSession.

@Test
public void testNotSharedSession() 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));
    new DirectFieldAccessor(jschSession1).setPropertyValue("isConnected", true);
    new DirectFieldAccessor(jschSession2).setPropertyValue("isConnected", true);
    when(jsch.getSession("foo", "host", 22)).thenReturn(jschSession1, jschSession2);
    ChannelSftp channel1 = spy(new ChannelSftp());
    ChannelSftp channel2 = spy(new ChannelSftp());
    new DirectFieldAccessor(channel1).setPropertyValue("session", jschSession1);
    new DirectFieldAccessor(channel2).setPropertyValue("session", jschSession1);
    doReturn(channel1).when(jschSession1).openChannel("sftp");
    doReturn(channel2).when(jschSession2).openChannel("sftp");
    DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(jsch, false);
    factory.setHost("host");
    factory.setUser("foo");
    factory.setPassword("bar");
    noopConnect(channel1);
    noopConnect(channel2);
    Session<LsEntry> s1 = factory.getSession();
    Session<LsEntry> s2 = factory.getSession();
    assertNotSame(TestUtils.getPropertyValue(s1, "jschSession"), TestUtils.getPropertyValue(s2, "jschSession"));
    assertSame(channel1, TestUtils.getPropertyValue(s1, "channel"));
    assertSame(channel2, TestUtils.getPropertyValue(s2, "channel"));
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) JSch(com.jcraft.jsch.JSch) DefaultSftpSessionFactory(org.springframework.integration.sftp.session.DefaultSftpSessionFactory) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry) Session(org.springframework.integration.file.remote.session.Session) SftpSession(org.springframework.integration.sftp.session.SftpSession) Test(org.junit.Test)

Example 2 with DefaultSftpSessionFactory

use of org.springframework.integration.sftp.session.DefaultSftpSessionFactory in project spring-integration by spring-projects.

the class SftpOutboundTests method testSharedSessionCachedReset.

@Test
public void testSharedSessionCachedReset() 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();
    final ChannelSftp channel3 = spy(new ChannelSftp());
    doReturn("channel3").when(channel3).toString();
    final ChannelSftp channel4 = spy(new ChannelSftp());
    doReturn("channel4").when(channel4).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");
    doAnswer(invocation -> n.getAndIncrement() < 3 ? channel3 : channel4).when(jschSession2).openChannel("sftp");
    DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(jsch, true);
    factory.setHost("host");
    factory.setUser("foo");
    factory.setPassword("bar");
    CachingSessionFactory<LsEntry> cachedFactory = new CachingSessionFactory<LsEntry>(factory);
    noopConnect(channel1);
    noopConnect(channel2);
    noopConnect(channel3);
    noopConnect(channel4);
    Session<LsEntry> s1 = cachedFactory.getSession();
    Session<LsEntry> s2 = cachedFactory.getSession();
    assertSame(jschSession1, TestUtils.getPropertyValue(s2, "targetSession.jschSession"));
    assertSame(channel1, TestUtils.getPropertyValue(s1, "targetSession.channel"));
    assertSame(channel2, TestUtils.getPropertyValue(s2, "targetSession.channel"));
    assertSame(TestUtils.getPropertyValue(s1, "targetSession.jschSession"), TestUtils.getPropertyValue(s2, "targetSession.jschSession"));
    s1.close();
    Session<LsEntry> s3 = cachedFactory.getSession();
    assertSame(TestUtils.getPropertyValue(s1, "targetSession"), TestUtils.getPropertyValue(s3, "targetSession"));
    assertSame(channel1, TestUtils.getPropertyValue(s3, "targetSession.channel"));
    s3.close();
    cachedFactory.resetCache();
    verify(jschSession1, never()).disconnect();
    s3 = cachedFactory.getSession();
    assertSame(jschSession2, TestUtils.getPropertyValue(s3, "targetSession.jschSession"));
    assertNotSame(TestUtils.getPropertyValue(s1, "targetSession"), TestUtils.getPropertyValue(s3, "targetSession"));
    assertSame(channel3, TestUtils.getPropertyValue(s3, "targetSession.channel"));
    s2.close();
    verify(jschSession1).disconnect();
    s2 = cachedFactory.getSession();
    assertSame(jschSession2, TestUtils.getPropertyValue(s2, "targetSession.jschSession"));
    assertNotSame(TestUtils.getPropertyValue(s3, "targetSession"), TestUtils.getPropertyValue(s2, "targetSession"));
    assertSame(channel4, TestUtils.getPropertyValue(s2, "targetSession.channel"));
    s2.close();
    s3.close();
    verify(jschSession2, never()).disconnect();
    cachedFactory.resetCache();
    verify(jschSession2).disconnect();
}
Also used : CachingSessionFactory(org.springframework.integration.file.remote.session.CachingSessionFactory) JSch(com.jcraft.jsch.JSch) DefaultSftpSessionFactory(org.springframework.integration.sftp.session.DefaultSftpSessionFactory) ChannelSftp(com.jcraft.jsch.ChannelSftp) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry) Session(org.springframework.integration.file.remote.session.Session) SftpSession(org.springframework.integration.sftp.session.SftpSession) Test(org.junit.Test)

Example 3 with DefaultSftpSessionFactory

use of org.springframework.integration.sftp.session.DefaultSftpSessionFactory in project spring-integration by spring-projects.

the class SftpTestSupport method sessionFactory.

public static SessionFactory<LsEntry> sessionFactory() {
    DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
    factory.setHost("localhost");
    factory.setPort(port);
    factory.setUser("foo");
    factory.setPassword("foo");
    factory.setAllowUnknownKeys(true);
    return new CachingSessionFactory<LsEntry>(factory);
}
Also used : CachingSessionFactory(org.springframework.integration.file.remote.session.CachingSessionFactory) DefaultSftpSessionFactory(org.springframework.integration.sftp.session.DefaultSftpSessionFactory)

Example 4 with DefaultSftpSessionFactory

use of org.springframework.integration.sftp.session.DefaultSftpSessionFactory 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"));
}
Also used : JSch(com.jcraft.jsch.JSch) DefaultSftpSessionFactory(org.springframework.integration.sftp.session.DefaultSftpSessionFactory) ChannelSftp(com.jcraft.jsch.ChannelSftp) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry) Session(org.springframework.integration.file.remote.session.Session) SftpSession(org.springframework.integration.sftp.session.SftpSession) Test(org.junit.Test)

Example 5 with DefaultSftpSessionFactory

use of org.springframework.integration.sftp.session.DefaultSftpSessionFactory in project spring-integration by spring-projects.

the class OutboundChannelAdapterParserTests method testOutboundChannelAdapterWithId.

@Test
public void testOutboundChannelAdapterWithId() {
    ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("OutboundChannelAdapterParserTests-context.xml", this.getClass());
    Object consumer = context.getBean("sftpOutboundAdapter");
    assertTrue(consumer instanceof EventDrivenConsumer);
    PublishSubscribeChannel channel = context.getBean("inputChannel", PublishSubscribeChannel.class);
    assertEquals(channel, TestUtils.getPropertyValue(consumer, "inputChannel"));
    assertEquals("sftpOutboundAdapter", ((EventDrivenConsumer) consumer).getComponentName());
    FileTransferringMessageHandler<?> handler = TestUtils.getPropertyValue(consumer, "handler", FileTransferringMessageHandler.class);
    String remoteFileSeparator = (String) TestUtils.getPropertyValue(handler, "remoteFileTemplate.remoteFileSeparator");
    assertNotNull(remoteFileSeparator);
    assertEquals(".", remoteFileSeparator);
    assertEquals(".bar", TestUtils.getPropertyValue(handler, "remoteFileTemplate.temporaryFileSuffix", String.class));
    Expression remoteDirectoryExpression = (Expression) TestUtils.getPropertyValue(handler, "remoteFileTemplate.directoryExpressionProcessor.expression");
    assertNotNull(remoteDirectoryExpression);
    assertTrue(remoteDirectoryExpression instanceof LiteralExpression);
    assertNotNull(TestUtils.getPropertyValue(handler, "remoteFileTemplate.temporaryDirectoryExpressionProcessor"));
    assertEquals(context.getBean("fileNameGenerator"), TestUtils.getPropertyValue(handler, "remoteFileTemplate.fileNameGenerator"));
    assertEquals("UTF-8", TestUtils.getPropertyValue(handler, "remoteFileTemplate.charset"));
    CachingSessionFactory<?> sessionFactory = TestUtils.getPropertyValue(handler, "remoteFileTemplate.sessionFactory", CachingSessionFactory.class);
    DefaultSftpSessionFactory clientFactory = TestUtils.getPropertyValue(sessionFactory, "sessionFactory", DefaultSftpSessionFactory.class);
    assertEquals("localhost", TestUtils.getPropertyValue(clientFactory, "host"));
    assertEquals(2222, TestUtils.getPropertyValue(clientFactory, "port"));
    assertEquals(23, TestUtils.getPropertyValue(handler, "order"));
    // verify subscription order
    @SuppressWarnings("unchecked") Set<MessageHandler> handlers = (Set<MessageHandler>) TestUtils.getPropertyValue(TestUtils.getPropertyValue(channel, "dispatcher"), "handlers");
    Iterator<MessageHandler> iterator = handlers.iterator();
    assertSame(TestUtils.getPropertyValue(context.getBean("sftpOutboundAdapterWithExpression"), "handler"), iterator.next());
    assertSame(handler, iterator.next());
    assertEquals(384, TestUtils.getPropertyValue(handler, "chmod"));
    context.close();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) EventDrivenConsumer(org.springframework.integration.endpoint.EventDrivenConsumer) Set(java.util.Set) PublishSubscribeChannel(org.springframework.integration.channel.PublishSubscribeChannel) MessageHandler(org.springframework.messaging.MessageHandler) FileTransferringMessageHandler(org.springframework.integration.file.remote.handler.FileTransferringMessageHandler) LiteralExpression(org.springframework.expression.common.LiteralExpression) DefaultSftpSessionFactory(org.springframework.integration.sftp.session.DefaultSftpSessionFactory) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) LiteralExpression(org.springframework.expression.common.LiteralExpression) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Expression(org.springframework.expression.Expression) Test(org.junit.Test)

Aggregations

DefaultSftpSessionFactory (org.springframework.integration.sftp.session.DefaultSftpSessionFactory)5 Test (org.junit.Test)4 ChannelSftp (com.jcraft.jsch.ChannelSftp)3 LsEntry (com.jcraft.jsch.ChannelSftp.LsEntry)3 JSch (com.jcraft.jsch.JSch)3 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)3 Session (org.springframework.integration.file.remote.session.Session)3 SftpSession (org.springframework.integration.sftp.session.SftpSession)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 CachingSessionFactory (org.springframework.integration.file.remote.session.CachingSessionFactory)2 Set (java.util.Set)1 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)1 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)1 Expression (org.springframework.expression.Expression)1 LiteralExpression (org.springframework.expression.common.LiteralExpression)1 SpelExpression (org.springframework.expression.spel.standard.SpelExpression)1 PublishSubscribeChannel (org.springframework.integration.channel.PublishSubscribeChannel)1 EventDrivenConsumer (org.springframework.integration.endpoint.EventDrivenConsumer)1 FileTransferringMessageHandler (org.springframework.integration.file.remote.handler.FileTransferringMessageHandler)1 MessageHandler (org.springframework.messaging.MessageHandler)1