Search in sources :

Example 16 with Session

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));
}
Also used : SessionFactory(org.springframework.integration.file.remote.session.SessionFactory) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageBuilder(org.springframework.integration.support.MessageBuilder) List(java.util.List) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Session(org.springframework.integration.file.remote.session.Session) Test(org.junit.Test)

Example 17 with Session

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));
}
Also used : SessionFactory(org.springframework.integration.file.remote.session.SessionFactory) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageBuilder(org.springframework.integration.support.MessageBuilder) List(java.util.List) ArrayList(java.util.ArrayList) Session(org.springframework.integration.file.remote.session.Session) Test(org.junit.Test)

Example 18 with Session

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);
}
Also used : MessagingException(org.springframework.messaging.MessagingException) Arrays(java.util.Arrays) SessionFactory(org.springframework.integration.file.remote.session.SessionFactory) FileHeaders(org.springframework.integration.file.FileHeaders) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) FileExistsMode(org.springframework.integration.file.support.FileExistsMode) BufferedOutputStream(java.io.BufferedOutputStream) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ValueExpression(org.springframework.integration.expression.ValueExpression) FileListFilter(org.springframework.integration.file.filters.FileListFilter) MessageHandlingException(org.springframework.messaging.MessageHandlingException) RemoteFileUtils(org.springframework.integration.file.remote.RemoteFileUtils) Session(org.springframework.integration.file.remote.session.Session) FunctionExpression(org.springframework.integration.expression.FunctionExpression) AbstractFileInfo(org.springframework.integration.file.remote.AbstractFileInfo) MessageSessionCallback(org.springframework.integration.file.remote.MessageSessionCallback) IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) MutableMessage(org.springframework.integration.support.MutableMessage) Message(org.springframework.messaging.Message) RemoteFileTemplate(org.springframework.integration.file.remote.RemoteFileTemplate) OutputStream(java.io.OutputStream) RemoteFileOperations(org.springframework.integration.file.remote.RemoteFileOperations) Iterator(java.util.Iterator) Collection(java.util.Collection) ObjectUtils(org.springframework.util.ObjectUtils) FileOutputStream(java.io.FileOutputStream) Set(java.util.Set) ExpressionUtils(org.springframework.integration.expression.ExpressionUtils) IOException(java.io.IOException) PartialSuccessException(org.springframework.integration.support.PartialSuccessException) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) ExpressionEvaluatingMessageProcessor(org.springframework.integration.handler.ExpressionEvaluatingMessageProcessor) EvaluationContext(org.springframework.expression.EvaluationContext) List(java.util.List) Expression(org.springframework.expression.Expression) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Collections(java.util.Collections) Assert(org.springframework.util.Assert) StringUtils(org.springframework.util.StringUtils) IOException(java.io.IOException) MessageHandlingException(org.springframework.messaging.MessageHandlingException)

Example 19 with 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"));
}
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 20 with Session

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"));
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) Session(org.springframework.integration.file.remote.session.Session) Test(org.junit.Test)

Aggregations

Session (org.springframework.integration.file.remote.session.Session)23 Test (org.junit.Test)22 SessionFactory (org.springframework.integration.file.remote.session.SessionFactory)16 List (java.util.List)13 GenericMessage (org.springframework.messaging.support.GenericMessage)13 ArrayList (java.util.ArrayList)12 MessageBuilder (org.springframework.integration.support.MessageBuilder)12 Matchers.containsString (org.hamcrest.Matchers.containsString)8 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)6 CachingSessionFactory (org.springframework.integration.file.remote.session.CachingSessionFactory)5 ChannelSftp (com.jcraft.jsch.ChannelSftp)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)4 LsEntry (com.jcraft.jsch.ChannelSftp.LsEntry)3 JSch (com.jcraft.jsch.JSch)3 File (java.io.File)3 IOException (java.io.IOException)2 DefaultSftpSessionFactory (org.springframework.integration.sftp.session.DefaultSftpSessionFactory)2 SftpSession (org.springframework.integration.sftp.session.SftpSession)2 MessagingException (org.springframework.messaging.MessagingException)2