@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"); }
/** * Return a new FileSystemEntry that is a clone of this object, except having the specified path * * @param path - the new path value for the cloned file system entry * @return a new FileSystemEntry that has all the same values as this object except for its path */ public FileSystemEntry cloneWithNewPath(String path) { FileEntry clone = new FileEntry(path); clone.setLastModified(getLastModified()); clone.setOwner(getOwner()); clone.setGroup(getGroup()); clone.setPermissions(getPermissions()); clone.setContents(getCurrentBytes()); return clone; }
/** * @see java.lang.Object#toString() */ public String toString() { return "File['" + getPath() + "' size=" + getSize() + " lastModified=" + getLastModified() + " owner=" + getOwner() + " group=" + getGroup() + " permissions=" + getPermissions() + "]"; }
public final void persist(final FileEntry entry, final Session session) { prepareInsertStatement(session); final BoundStatement boundStatement = new BoundStatement(persistPs); //bind values from our bean to our insert query session.execute(boundStatement.bind(entry.getFileName(), entry.getMd5(), entry.getLastModified(), entry.getSize(), entry.getHashDate())); } private final synchronized void prepareInsertStatement(final Session session) { // prepare statement, if it doesn't exist. if (persistPs == null) { persistPs = session.prepare(BOUND_STATEMENT); } }
file = new FileEntry(path); getFileSystem().add(file); file.setPermissions(getUserAccount(session).getDefaultPermissionsForNewFile()); OutputStream out = file.createOutputStream(appendToOutputFile()); try { out.write(contents); LOG.error("Error writing to file [" + file.getPath() + "]", e); throw new FileSystemException(file.getPath(), null, e); out.close(); } catch (IOException e) { LOG.error("Error closing OutputStream for file [" + file.getPath() + "]", e);
public void putFile(String fileName, final String content) throws Exception { FileSystem fileSystem = fakeFtpServer.getFileSystem(); fileSystem.add(new FileEntry(baseDir + "/" + fileName, content)); }
InputStream input = fileEntry.createInputStream(); session.openDataConnection(); byte[] bytes = null; LOG.error("Error reading from file [" + fileEntry.getPath() + "]", e); throw new FileSystemException(fileEntry.getPath(), null, e); LOG.error("Error closing InputStream for file [" + fileEntry.getPath() + "]", e);
/** * Create and return an OutputStream for writing the contents of the file represented by this entry * * @param append - true if the OutputStream should append to any existing contents false if * any existing contents should be overwritten * @return an OutputStream * @throws FileSystemException - if an error occurs creating or initializing the OutputStream */ public OutputStream createOutputStream(boolean append) { // If appending and we already have an OutputStream, then continue to use it if (append && out != null) { return out; } out = new ByteArrayOutputStream(); byte[] initialContents = (append) ? bytes : EMPTY; try { out.write(initialContents); } catch (IOException e) { throw new FileSystemException(getPath(), null, e); } return out; }
/** * Construct a new instance with the specified path and file contents * * @param path - the value for path * @param contents - the contents of the file, as a String */ public FileEntry(String path, String contents) { super(path); setContents(contents); }
@Test public void testRemoteFile() throws IOException { File directory = TEMP_FOLDER.newFolder("ftp"); File ftpFile = new File(directory, "ftp_file"); String ftpFileContents = "Contents of ftp_file"; FileSystem fileSystem = new UnixFakeFileSystem(); fileSystem.add(new FileEntry(ftpFile.getAbsolutePath(), ftpFileContents)); String user = "user"; String password = "password"; FakeFtpServer ftpServer = new FakeFtpServer(); // Use any available port ftpServer.setServerControlPort(0); ftpServer.addUserAccount(new UserAccount(user, password, directory.getAbsolutePath())); ftpServer.setFileSystem(fileSystem); ftpServer.start(); try { URI uri = URI.create(String.format("ftp://%s:%s@localhost:%d/%s", user, password, ftpServer.getServerControlPort(), ftpFile.getName())); File localizationDir = TEMP_FOLDER.newFolder("localRemote"); File localizedResource = LocalizationUtils.localizeResource("file1", new LocalizeResource(uri, false), localizationDir); Assert.assertTrue(localizedResource.exists()); Assert.assertTrue(localizedResource.isFile()); Assert.assertEquals(ftpFileContents, com.google.common.io.Files.toString(localizedResource, Charsets.UTF_8)); } finally { ftpServer.stop(); } }
/** * Stop the fakeFtpServer. Remove all temp files from the baseDir * <p/> * The fakeFtpServer demon will normally be stopped by JUnit using the after() method. This method allows the sshd to * be stopped manually to support advanced testing scenarios. */ public void stop() { for(Object file:fakeFtpServer.getFileSystem().listFiles(baseDir)){ fakeFtpServer.getFileSystem().delete(((FileEntry)file).getPath()); } fakeFtpServer.getFileSystem().delete(baseDir); fakeFtpServer.stop(); }
@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"); }
FileSystem results = fakeFtpServer.getFileSystem(); FileEntry sampleFile = new FileEntry("c:\\data\\randombytes-2"); sampleFile.setContents("Just some random test test test chocolate"); results.add(sampleFile);