@Test public void basicFileUpload() throws IOException { TestRunner runner = TestRunners.newTestRunner(PutFTP.class); runner.setProperty(FTPTransfer.HOSTNAME, "localhost"); runner.setProperty(FTPTransfer.USERNAME, username); runner.setProperty(FTPTransfer.PASSWORD, password); runner.setProperty(FTPTransfer.PORT, Integer.toString(ftpPort)); try (FileInputStream fis = new FileInputStream("src/test/resources/randombytes-1");) { Map<String, String> attributes = new HashMap<String, String>(); attributes.put(CoreAttributes.FILENAME.key(), "randombytes-1"); runner.enqueue(fis, attributes); runner.run(); } runner.assertQueueEmpty(); runner.assertAllFlowFilesTransferred(PutFTP.REL_SUCCESS); FileSystem results = fakeFtpServer.getFileSystem(); // Check file was uploaded Assert.assertTrue(results.exists("c:\\data\\randombytes-1")); }
@Test public void basicFileFetch() throws IOException { FileSystem results = fakeFtpServer.getFileSystem(); FileEntry sampleFile = new FileEntry("c:\\data\\randombytes-2"); sampleFile.setContents("Just some random test test test chocolate"); results.add(sampleFile); // Check file exists Assert.assertTrue(results.exists("c:\\data\\randombytes-2")); TestRunner runner = TestRunners.newTestRunner(FetchFTP.class); runner.setProperty(FetchFTP.HOSTNAME, "${host}"); runner.setProperty(FetchFTP.USERNAME, "${username}"); runner.setProperty(FTPTransfer.PASSWORD, password); runner.setProperty(FTPTransfer.PORT, "${port}"); runner.setProperty(FetchFTP.REMOTE_FILENAME, "c:\\data\\randombytes-2"); runner.setProperty(FetchFTP.COMPLETION_STRATEGY, FetchFTP.COMPLETION_MOVE); runner.setProperty(FetchFTP.MOVE_DESTINATION_DIR, "data"); Map<String, String> attrs = new HashMap<String, String>(); attrs.put("host", "localhost"); attrs.put("username", username); attrs.put("port", Integer.toString(ftpPort)); runner.enqueue("", attrs); runner.run(); final MockFlowFile retrievedFile = runner.getFlowFilesForRelationship(FetchFTP.REL_SUCCESS).get(0); retrievedFile.assertContentEquals("Just some random test test test chocolate"); }
@Test public void basicFileGet() throws IOException { FileSystem results = fakeFtpServer.getFileSystem(); FileEntry sampleFile = new FileEntry("c:\\data\\randombytes-2"); sampleFile.setContents("Just some random test test test chocolate"); results.add(sampleFile); // Check file exists Assert.assertTrue(results.exists("c:\\data\\randombytes-2")); TestRunner runner = TestRunners.newTestRunner(GetFTP.class); runner.setProperty(FTPTransfer.HOSTNAME, "localhost"); runner.setProperty(FTPTransfer.USERNAME, username); runner.setProperty(FTPTransfer.PASSWORD, password); runner.setProperty(FTPTransfer.PORT, Integer.toString(ftpPort)); runner.setProperty(FTPTransfer.REMOTE_PATH, "/"); runner.run(); final MockFlowFile retrievedFile = runner.getFlowFilesForRelationship(GetFTP.REL_SUCCESS).get(0); retrievedFile.assertContentEquals("Just some random test test test chocolate"); }
Assert.assertTrue(results.exists("c:\\data\\randombytes-2"));
protected void handle(Command command, Session session) { verifyLoggedIn(session); String path = getRealPath(session, command.getParameter(0)); // User must have read permission to the path if (getFileSystem().exists(path)) { this.replyCodeForFileSystemException = ReplyCodes.READ_FILE_ERROR; verifyReadPermission(session, path); } this.replyCodeForFileSystemException = ReplyCodes.SYSTEM_ERROR; List fileEntries = getFileSystem().listFiles(path); Iterator iter = fileEntries.iterator(); List lines = new ArrayList(); while (iter.hasNext()) { FileSystemEntry entry = (FileSystemEntry) iter.next(); lines.add(getFileSystem().formatDirectoryListing(entry)); } String result = StringUtil.join(lines, endOfLine()); result += result.length() > 0 ? endOfLine() : ""; sendReply(session, ReplyCodes.TRANSFER_DATA_INITIAL_OK); session.openDataConnection(); LOG.info("Sending [" + result + "]"); session.sendData(result.getBytes(), result.length()); session.closeDataConnection(); sendReply(session, ReplyCodes.TRANSFER_DATA_FINAL_OK); }
protected void handle(Command command, Session session) { verifyLoggedIn(session); String path = getRealPath(session, command.getParameter(0)); // User must have read permission to the path if (getFileSystem().exists(path)) { this.replyCodeForFileSystemException = ReplyCodes.READ_FILE_ERROR; verifyReadPermission(session, path); } this.replyCodeForFileSystemException = ReplyCodes.SYSTEM_ERROR; List names = getFileSystem().listNames(path); String directoryListing = StringUtil.join(names, endOfLine()); directoryListing += directoryListing.length() > 0 ? endOfLine() : ""; sendReply(session, ReplyCodes.TRANSFER_DATA_INITIAL_OK); session.openDataConnection(); session.sendData(directoryListing.getBytes(), directoryListing.length()); session.closeDataConnection(); sendReply(session, ReplyCodes.TRANSFER_DATA_FINAL_OK); }
protected void handle(Command command, Session session) { verifyLoggedIn(session); String fromPath = getRealPath(session, command.getRequiredParameter(0)); this.replyCodeForFileSystemException = ReplyCodes.READ_FILE_ERROR; verifyFileSystemCondition(getFileSystem().exists(fromPath), fromPath, "filesystem.doesNotExist"); // User must have read permission to the file verifyReadPermission(session, fromPath); session.setAttribute(SessionKeys.RENAME_FROM, fromPath); sendReply(session, ReplyCodes.RNFR_OK, "rnfr"); }
protected void handle(Command command, Session session) { verifyLoggedIn(session); String path = getRealPath(session, command.getRequiredParameter(0)); String parent = getFileSystem().getParent(path); this.replyCodeForFileSystemException = ReplyCodes.READ_FILE_ERROR; verifyFileSystemCondition(getFileSystem().exists(parent), parent, "filesystem.doesNotExist"); verifyFileSystemCondition(!getFileSystem().exists(path), path, "filesystem.alreadyExists"); // User must have write permission to the parent directory verifyWritePermission(session, parent); // User must have execute permission to the parent directory verifyExecutePermission(session, parent); DirectoryEntry dirEntry = new DirectoryEntry(path); getFileSystem().add(dirEntry); dirEntry.setPermissions(getUserAccount(session).getDefaultPermissionsForNewDirectory()); sendReply(session, ReplyCodes.MKD_OK, "mkd", list(path)); }
protected void handle(Command command, Session session) { verifyLoggedIn(session); String path = getRealPath(session, command.getRequiredParameter(0)); this.replyCodeForFileSystemException = ReplyCodes.READ_FILE_ERROR; verifyFileSystemCondition(getFileSystem().exists(path), path, "filesystem.doesNotExist"); verifyFileSystemCondition(getFileSystem().isDirectory(path), path, "filesystem.isNotADirectory"); // User must have execute permission to the directory verifyExecutePermission(session, path); session.setAttribute(SessionKeys.CURRENT_DIRECTORY, path); sendReply(session, ReplyCodes.CWD_OK, "cwd", list(path)); }
protected void handle(Command command, Session session) { verifyLoggedIn(session); String path = getRealPath(session, command.getRequiredParameter(0)); this.replyCodeForFileSystemException = ReplyCodes.READ_FILE_ERROR; verifyFileSystemCondition(getFileSystem().exists(path), path, "filesystem.doesNotExist"); verifyFileSystemCondition(getFileSystem().isDirectory(path), path, "filesystem.isNotADirectory"); verifyFileSystemCondition(getFileSystem().listNames(path).size() == 0, path, "filesystem.directoryIsNotEmpty"); // User must have write permission to the parent directory verifyWritePermission(session, getFileSystem().getParent(path)); getFileSystem().delete(path); sendReply(session, ReplyCodes.RMD_OK, "rmd", list(path)); }
String pathMustBeWritable = getFileSystem().exists(path) ? path : parentPath; verifyWritePermission(session, pathMustBeWritable);
protected void handle(Command command, Session session) { verifyLoggedIn(session); String toPath = getRealPath(session, command.getRequiredParameter(0)); String fromPath = (String) getRequiredSessionAttribute(session, SessionKeys.RENAME_FROM); this.replyCodeForFileSystemException = ReplyCodes.WRITE_FILE_ERROR; verifyFileSystemCondition(!getFileSystem().isDirectory(toPath), toPath, "filesystem.isDirectory"); // User must have write permission to the directory String parentPath = getFileSystem().getParent(toPath); verifyFileSystemCondition(notNullOrEmpty(parentPath), parentPath, "filesystem.doesNotExist"); verifyFileSystemCondition(getFileSystem().exists(parentPath), parentPath, "filesystem.doesNotExist"); verifyWritePermission(session, parentPath); getFileSystem().rename(fromPath, toPath); session.removeAttribute(SessionKeys.RENAME_FROM); sendReply(session, ReplyCodes.RNTO_OK, "rnto", list(fromPath, toPath)); }