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; } }
throws IOException { if (!session.exists(path)) { while (nextSeparatorIndex > -1) { String pathSegment = path.substring(0, nextSeparatorIndex); if (pathSegment.length() == 0 || session.exists(pathSegment)) { logger.debug("Creating '" + pathToCreate + "'"); session.mkdir(pathToCreate); session.mkdir(path);
protected void doTest(SshServer server, Session<LsEntry> session) throws IOException { assertEquals(1, server.getActiveSessions().size()); LsEntry[] list = session.list("."); if (list.length > 0) { session.remove("*"); } session.write(new ByteArrayInputStream("foo".getBytes()), "bar"); list = session.list("."); assertEquals("bar", list[1].getFilename()); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); session.read("bar", outputStream); assertEquals("foo", new String(outputStream.toByteArray())); session.remove("bar"); session.close(); }
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; }); } }
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; }); } }
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");
template.setUseTemporaryFileName(false); template.execute(session -> { session.mkdir("foo/"); return session.mkdir("foo/bar/"); }); template.append(new GenericMessage<String>("foo")); LsEntry[] files = session.list("foo/"); assertEquals(4, files.length); assertTrue(session.remove("foo/foobar.txt")); assertTrue(session.rmdir("foo/bar/")); files = session.list("foo/"); assertEquals(2, files.length); List<LsEntry> list = Arrays.asList(files); assertThat(list.stream().map(l -> l.getFilename()).collect(Collectors.toList()), containsInAnyOrder(".", "..")); assertTrue(session.rmdir("foo/")); }); assertFalse(template.exists("foo"));
@Override public F[] list(String path) { return execute(session -> session.list(path)); }
@Test public void testInt3100RawGET() throws Exception { Session<?> session = this.ftpSessionFactory.getSession(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); FileCopyUtils.copy(session.readRaw("ftpSource/ ftpSource1.txt"), baos); assertTrue(session.finalizeRaw()); assertEquals("source1", new String(baos.toByteArray())); baos = new ByteArrayOutputStream(); FileCopyUtils.copy(session.readRaw("ftpSource/ftpSource2.txt"), baos); assertTrue(session.finalizeRaw()); assertEquals("source2", new String(baos.toByteArray())); session.close(); }
@Test @SuppressWarnings("unchecked") public void testMGETOnNullDir() throws IOException { Session<FTPFile> session = ftpSessionFactory.getSession(); ((FTPClient) session.getClientInstance()).changeWorkingDirectory("ftpSource"); session.close(); this.inboundMGet.send(new GenericMessage<Object>("")); Message<?> result = this.output.receive(1000); assertNotNull(result); List<File> localFiles = (List<File>) result.getPayload(); assertThat(localFiles.size(), Matchers.greaterThan(0)); for (File file : localFiles) { assertThat(file.getName(), isOneOf(" localTarget1.txt", "localTarget2.txt")); assertThat(file.getName(), not(containsString("null"))); } }
@Test @SuppressWarnings("unchecked") public void testLsForNullDir() throws IOException { Session<FTPFile> session = ftpSessionFactory.getSession(); ((FTPClient) session.getClientInstance()).changeWorkingDirectory("ftpSource"); session.close(); this.inboundLs.send(new GenericMessage<String>("foo")); Message<?> receive = this.output.receive(10000); assertNotNull(receive); assertThat(receive.getPayload(), instanceOf(List.class)); List<String> files = (List<String>) receive.getPayload(); assertEquals(2, files.size()); assertThat(files, containsInAnyOrder(" ftpSource1.txt", "ftpSource2.txt")); FTPFile[] ftpFiles = ftpSessionFactory.getSession().list(null); for (FTPFile ftpFile : ftpFiles) { if (!ftpFile.isDirectory()) { assertTrue(files.contains(ftpFile.getName())); } } }
@Override public boolean exists(final String path) { return execute(session -> session.exists(path)); }
@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")); }
@SuppressWarnings("unchecked") @Override public Session<String> getSession() { if (this.session != null) { return this.session; } try { Session<String> session = mock(Session.class); willReturn(new String[] { "/foo/foo", "/foo/bar" }).given(session).list("/foo"); ByteArrayInputStream foo = new ByteArrayInputStream("foo\nbar".getBytes()); ByteArrayInputStream bar = new ByteArrayInputStream("baz\nqux".getBytes()); willReturn(foo).given(session).readRaw("/foo/foo"); willReturn(bar).given(session).readRaw("/foo/bar"); willReturn(new String[] { "/bar/foo", "/bar/bar" }).given(session).list("/bar"); ByteArrayInputStream foo2 = new ByteArrayInputStream("foo\r\nbar".getBytes()); ByteArrayInputStream bar2 = new ByteArrayInputStream("baz\r\nqux".getBytes()); willReturn(foo2).given(session).readRaw("/bar/foo"); willReturn(bar2).given(session).readRaw("/bar/bar"); willReturn(new String[] { "/bad/file" }).given(session).list("/bad"); willThrow(new IOException("No file")).given(session).readRaw("/bad/file"); given(session.finalizeRaw()).willReturn(true); this.session = session; return session; } catch (Exception e) { throw new RuntimeException("failed to mock session", e); } }
F fileInfo = fileInfoParam; if (fileInfo == null) { F[] files = session.list(remoteFilePath); if (files == null) { throw new MessagingException("Session returned null when listing " + remoteFilePath); session.read(remoteFilePath, outputStream); boolean result = session.remove(remoteFilePath); if (!result) { logger.error("Failed to delete: " + remoteFilePath);
assertEquals("foo/bar.txt", path); ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); verify(session).write(any(InputStream.class), captor.capture()); assertEquals("foo/bar.txt.writing", captor.getValue()); verify(session).rename("foo/bar.txt.writing", "foo/bar.txt"); assertEquals("foo/bar.txt", path); captor = ArgumentCaptor.forClass(String.class); verify(session, times(2)).write(any(InputStream.class), captor.capture()); assertEquals("foo/bar.txt.writing", captor.getValue()); verify(session, times(2)).rename("foo/bar.txt.writing", "foo/bar.txt"); assertEquals("foo/bar.txt", path); captor = ArgumentCaptor.forClass(String.class); verify(session).append(any(InputStream.class), captor.capture()); assertEquals("foo/bar.txt", captor.getValue()); assertEquals("foo/bar.txt", path); verify(session, times(2)).write(any(InputStream.class), anyString()); verify(session, times(1)).append(any(InputStream.class), anyString());
@Override public <T> T invoke(OperationsCallback<F, T> action) { Session<F> contextSession = this.contextSessions.get(); if (contextSession == null) { this.contextSessions.set(this.sessionFactory.getSession()); } this.activeTemplateCallbacks.incrementAndGet(); try { return action.doInOperations(this); } finally { this.activeTemplateCallbacks.decrementAndGet(); if (contextSession == null) { Session<F> session = this.contextSessions.get(); if (session != null) { session.close(); } this.contextSessions.remove(); } } }
/** * 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); }