public static void cleanUp(RemoteFileTemplate<LsEntry> template, final String... fileNames) { if (template != null) { template.execute((SessionCallback<LsEntry, Void>) session -> { for (int i = 0; i < fileNames.length; i++) { try { session.remove("si.sftp.sample/" + fileNames[i]); } catch (IOException e) {} } // should be empty session.rmdir("si.sftp.sample"); return null; }); } }
/** * Construct an instance with a {@link DefaultSessionFactoryLocator} using the * supplied factories and default key. * @param factories the factories. * @param defaultFactory the default to use if the lookup fails. */ public DelegatingSessionFactory(Map<Object, SessionFactory<F>> factories, SessionFactory<F> defaultFactory) { this(new DefaultSessionFactoryLocator<F>(factories, defaultFactory)); }
/** * Test the session is still alive, e.g. when checking out from a pool. * The default implementation simply delegates to {@link #isOpen()}. * @return true if the test is successful. * @since 5.1 */ default boolean test() { return this.isOpen(); }
when(sessionFactory.getSession()).thenReturn(session); fileInbound.start();
public Session<F> getSession(Object key) { SessionFactory<F> sessionFactory = this.factoryLocator.getSessionFactory(key); Assert.notNull(sessionFactory, "No default SessionFactory configured"); return sessionFactory.getSession(); }
public static void createTestFiles(RemoteFileTemplate<LsEntry> template, final String... fileNames) { if (template != null) { final ByteArrayInputStream stream = new ByteArrayInputStream("foo".getBytes()); template.execute((SessionCallback<LsEntry, Void>) session -> { try { session.mkdir("si.sftp.sample"); } catch (Exception e) { assertThat(e.getMessage(), containsString("failed to create")); } for (int i = 0; i < fileNames.length; i++) { stream.reset(); session.write(stream, "si.sftp.sample/" + fileNames[i]); } return null; }); } }
public static boolean fileExists(RemoteFileTemplate<LsEntry> template, final String... fileNames) { if (template != null) { return template.execute(session -> { ChannelSftp channel = (ChannelSftp) session.getClientInstance(); for (int i = 0; i < fileNames.length; i++) { try { SftpATTRS stat = channel.stat("si.sftp.sample/" + fileNames[i]); if (stat == null) { System.out.println("stat returned null for " + fileNames[i]); return false; } } catch (SftpException e) { System.out.println("Remote file not present: " + e.getMessage() + ": " + fileNames[i]); return false; } } return true; }); } else { return false; } }
verify(this.session).write(any(InputStream.class), eq("foo/002.txt.writing")); if (!fail) { verify(this.session).write(any(InputStream.class), eq("foo/006.txt.writing")); verify(this.session).write(any(InputStream.class), eq("foo/009.txt.writing")); verify(this.session).rename("foo/002.txt.writing", "foo/002.txt"); verify(this.session).rename("foo/006.txt.writing", "foo/006.txt"); verify(this.session).rename("foo/009.txt.writing", "foo/009.txt");
@Test public void testDelegates() { assertEquals(foo.mockSession, this.dsf.getSession("foo")); assertEquals(bar.mockSession, this.dsf.getSession("bar")); assertEquals(bar.mockSession, this.dsf.getSession("junk")); assertEquals(bar.mockSession, this.dsf.getSession()); this.dsf.setThreadKey("foo"); assertEquals(foo.mockSession, this.dsf.getSession("foo")); this.dsf.clearThreadKey(); TestSessionFactory factory = new TestSessionFactory(); this.sessionFactoryLocator.addSessionFactory("baz", factory); this.dsf.setThreadKey("baz"); assertEquals(factory.mockSession, this.dsf.getSession("baz")); this.dsf.clearThreadKey(); assertSame(factory, sessionFactoryLocator.removeSessionFactory("baz")); }
@Override public boolean exists(final String path) { return execute(session -> session.exists(path)); }
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<>(factory); }
@Test public void testFailure() throws Exception { willThrow(new RuntimeException("fail test exception")) .given(this.session).write(any(InputStream.class), eq("foo/002.txt.writing")); String message = runTest(true); assertThat(message).contains("File split and transfer failed"); assertThat(message).contains("fail test exception"); assertThat(message).contains(TestUtils.applySystemFileSeparator("/tmp/out/002.txt")); }
@Bean public SessionFactoryLocator<FTPFile> factoryLocator() { return new DefaultSessionFactoryLocator<>(new SessionFactoryMapBuilder<FTPFile>() .put("one", factory1()) .put("two", factory2()) .put("three", factory3()) .get()); }
/** * Perform remote delete for the provided path. * The message can be consulted to determine some context; * isn't used in the default implementation. * @param message the request message related to the path to remove * @param session the remote protocol session to perform remove command * @param remoteFilePath the remote path to remove * @return true or false as a result of the remote removal * @throws IOException the IO exception during performing remote command * @since 5.0 */ protected boolean rm(Message<?> message, Session<F> session, String remoteFilePath) throws IOException { return session.remove(remoteFilePath); }
@Bean public SessionFactoryLocator<String> sessionFactoryLocator() { Map<Object, SessionFactory<String>> factories = new HashMap<>(); factories.put("foo", foo()); TestSessionFactory bar = bar(); factories.put("bar", bar); return new DefaultSessionFactoryLocator<>(factories, bar); }
@Bean DelegatingSessionFactory<String> dsf() { SessionFactoryLocator<String> sff = sessionFactoryLocator(); return new DelegatingSessionFactory<>(sff); }
@Override public Session<String> getSession() { return new TestSession("session:" + ++n); }
/** * Get a session from the pool (or block if none available). */ @Override public Session<F> getSession() { return new CachedSession(this.pool.getItem(), this.sharedSessionEpoch); }
SourcePollingChannelAdapter fileInbound = context.getBean(SourcePollingChannelAdapter.class); when(sessionFactory.getSession()).thenThrow(new RuntimeException("Force Failure")); fileInbound.start();
private SessionFactory<FTPFile> mockSf() { @SuppressWarnings("unchecked") SessionFactory<FTPFile> mocksf = mock(SessionFactory.class); given(mocksf.getSession()).willReturn(mockSession()); return mocksf; }