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; }); } }
/** * 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); }
@Override public boolean remove(final String path) { return execute(session -> session.remove(path)); }
session.remove(remoteFilePath); if (this.logger.isDebugEnabled()) { this.logger.debug("deleted remote file: " + remoteFilePath);
boolean result = session.remove(remoteFilePath); if (!result) { logger.error("Failed to delete: " + remoteFilePath);
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(); }
@Test public void testRm() throws Exception { SessionFactory sessionFactory = mock(SessionFactory.class); Session session = mock(Session.class); TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway(sessionFactory, "rm", "payload"); gw.afterPropertiesSet(); when(sessionFactory.getSession()).thenReturn(session); when(session.remove("testremote/x/f1")).thenReturn(Boolean.TRUE); @SuppressWarnings("unchecked") MessageBuilder<Boolean> out = (MessageBuilder<Boolean>) gw .handleRequestMessage(new GenericMessage<>("testremote/x/f1")); assertEquals(Boolean.TRUE, out.getPayload()); verify(session).remove("testremote/x/f1"); assertEquals("testremote/x/", out.getHeaders().get(FileHeaders.REMOTE_DIRECTORY)); assertEquals("f1", out.getHeaders().get(FileHeaders.REMOTE_FILE)); }
LsEntry[] files = session.list("foo/"); assertEquals(4, files.length); assertTrue(session.remove("foo/foobar.txt")); assertTrue(session.rmdir("foo/bar/")); files = session.list("foo/");
assertTrue(session.remove("foo/foobar.txt")); assertTrue(session.rmdir("foo/bar/")); FTPFile[] files = session.list("foo/");