List of usage examples for org.apache.commons.net.ftp FTPClient printWorkingDirectory
public String printWorkingDirectory() throws IOException
From source file:org.apache.nifi.processors.standard.util.FTPUtils.java
/** * Handles the logic required to change to the given directory RELATIVE TO THE CURRENT DIRECTORY which can include creating new directories needed. * * This will first attempt to change to the full path of the given directory outright. If that fails, then it will attempt to change from the top of the tree of the given directory all the way * down to the final leaf node of the given directory. * * @param client - the ftp client with an already active connection * @param dirPath - the path to change or create directories to * @param createDirs - if true will attempt to create any missing directories * @param processor - used solely for targeting logging output. * @throws IOException if any access problem occurs *//* w w w . ja v a 2s . c o m*/ public static void changeWorkingDirectory(final FTPClient client, final String dirPath, final boolean createDirs, final Processor processor) throws IOException { final String currentWorkingDirectory = client.printWorkingDirectory(); final File dir = new File(dirPath); logger.debug(processor + " attempting to change directory from " + currentWorkingDirectory + " to " + dir.getPath()); boolean dirExists = false; final String forwardPaths = dir.getPath().replaceAll(Matcher.quoteReplacement("\\"), Matcher.quoteReplacement("/")); //always use forward paths for long string attempt try { dirExists = client.changeWorkingDirectory(forwardPaths); if (dirExists) { logger.debug(processor + " changed working directory to '" + forwardPaths + "' from '" + currentWorkingDirectory + "'"); } else { logger.debug(processor + " could not change directory to '" + forwardPaths + "' from '" + currentWorkingDirectory + "' so trying the hard way."); } } catch (final IOException ioe) { logger.debug(processor + " could not change directory to '" + forwardPaths + "' from '" + currentWorkingDirectory + "' so trying the hard way."); } if (!dirExists) { //couldn't navigate directly...begin hard work final Deque<String> stack = new LinkedList<>(); File fakeFile = new File(dir.getPath()); do { stack.push(fakeFile.getName()); } while ((fakeFile = fakeFile.getParentFile()) != null); String dirName = null; while ((dirName = stack.peek()) != null) { stack.pop(); //find out if exists, if not make it if configured to do so or throw exception dirName = ("".equals(dirName.trim())) ? "/" : dirName; boolean exists = false; try { exists = client.changeWorkingDirectory(dirName); } catch (final IOException ioe) { exists = false; } if (!exists && createDirs) { logger.debug(processor + " creating new directory and changing to it " + dirName); client.makeDirectory(dirName); if (!(client.makeDirectory(dirName) || client.changeWorkingDirectory(dirName))) { throw new IOException( processor + " could not create and change to newly created directory " + dirName); } else { logger.debug(processor + " successfully changed working directory to " + dirName); } } else if (!exists) { throw new IOException(processor + " could not change directory to '" + dirName + "' from '" + currentWorkingDirectory + "'"); } } } }
From source file:org.apache.tools.ant.taskdefs.optional.net.FTP.java
/** * Create the specified directory on the remote host. * * @param ftp The FTP client connection/* w ww .j a va2 s.co m*/ * @param dir The directory to create (format must be correct for host * type) * @throws IOException in unknown circumstances * @throws BuildException if ignoreNoncriticalErrors has not been set to true * and a directory could not be created, for instance because it was * already existing. Precisely, the codes 521, 550 and 553 will trigger * a BuildException */ protected void makeRemoteDir(FTPClient ftp, String dir) throws IOException, BuildException { String workingDirectory = ftp.printWorkingDirectory(); if (verbose) { if (dir.startsWith("/") || workingDirectory == null) { log("Creating directory: " + dir + " in /"); } else { log("Creating directory: " + dir + " in " + workingDirectory); } } if (dir.startsWith("/")) { ftp.changeWorkingDirectory("/"); } String subdir = ""; StringTokenizer st = new StringTokenizer(dir, "/"); while (st.hasMoreTokens()) { subdir = st.nextToken(); log("Checking " + subdir, Project.MSG_DEBUG); if (!ftp.changeWorkingDirectory(subdir)) { if (!ftp.makeDirectory(subdir)) { // codes 521, 550 and 553 can be produced by FTP Servers // to indicate that an attempt to create a directory has // failed because the directory already exists. int rc = ftp.getReplyCode(); if (!(ignoreNoncriticalErrors && (rc == CODE_550 || rc == CODE_553 || rc == CODE_521))) { throw new BuildException("could not create directory: " + ftp.getReplyString()); } if (verbose) { log("Directory already exists"); } } else { if (verbose) { log("Directory created OK"); } ftp.changeWorkingDirectory(subdir); } } } if (workingDirectory != null) { ftp.changeWorkingDirectory(workingDirectory); } }
From source file:org.apache.tools.ant.taskdefs.optional.net.FTPTaskMirrorImpl.java
/** * check FTPFiles to check whether they function as directories too * the FTPFile API seem to make directory and symbolic links incompatible * we want to find out if we can cd to a symbolic link * @param dir the parent directory of the file to test * @param file the file to test// w w w . ja v a 2 s. com * @return true if it is possible to cd to this directory * @since ant 1.6 */ private boolean isFunctioningAsDirectory(FTPClient ftp, String dir, FTPFile file) { boolean result = false; String currentWorkingDir = null; if (file.isDirectory()) { return true; } else if (file.isFile()) { return false; } try { currentWorkingDir = ftp.printWorkingDirectory(); } catch (IOException ioe) { task.log("could not find current working directory " + dir + " while checking a symlink", Project.MSG_DEBUG); } if (currentWorkingDir != null) { try { result = ftp.changeWorkingDirectory(file.getLink()); } catch (IOException ioe) { task.log("could not cd to " + file.getLink() + " while checking a symlink", Project.MSG_DEBUG); } if (result) { boolean comeback = false; try { comeback = ftp.changeWorkingDirectory(currentWorkingDir); } catch (IOException ioe) { task.log("could not cd back to " + dir + " while checking a symlink", Project.MSG_ERR); } finally { if (!comeback) { throw new BuildException("could not cd back to " + dir + " while checking a symlink"); } } } } return result; }
From source file:org.apache.tools.ant.taskdefs.optional.net.FTPTaskMirrorImpl.java
/** * Creates all parent directories specified in a complete relative * pathname. Attempts to create existing directories will not cause * errors.// www . jav a 2s .c o m * * @param ftp the FTP client instance to use to execute FTP actions on * the remote server. * @param filename the name of the file whose parents should be created. * @throws IOException under non documented circumstances * @throws BuildException if it is impossible to cd to a remote directory * */ protected void createParents(FTPClient ftp, String filename) throws IOException, BuildException { File dir = new File(filename); if (dirCache.contains(dir)) { return; } Vector parents = new Vector(); String dirname; while ((dirname = dir.getParent()) != null) { File checkDir = new File(dirname); if (dirCache.contains(checkDir)) { break; } dir = checkDir; parents.addElement(dir); } // find first non cached dir int i = parents.size() - 1; if (i >= 0) { String cwd = ftp.printWorkingDirectory(); String parent = dir.getParent(); if (parent != null) { if (!ftp.changeWorkingDirectory(resolveFile(parent))) { throw new BuildException("could not change to " + "directory: " + ftp.getReplyString()); } } while (i >= 0) { dir = (File) parents.elementAt(i--); // check if dir exists by trying to change into it. if (!ftp.changeWorkingDirectory(dir.getName())) { // could not change to it - try to create it task.log("creating remote directory " + resolveFile(dir.getPath()), Project.MSG_VERBOSE); if (!ftp.makeDirectory(dir.getName())) { handleMkDirFailure(ftp); } if (!ftp.changeWorkingDirectory(dir.getName())) { throw new BuildException("could not change to " + "directory: " + ftp.getReplyString()); } } dirCache.add(dir); } ftp.changeWorkingDirectory(cwd); } }
From source file:org.apache.tools.ant.taskdefs.optional.net.FTPTaskMirrorImpl.java
/** * Create the specified directory on the remote host. * * @param ftp The FTP client connection/* w ww . j av a 2 s . c om*/ * @param dir The directory to create (format must be correct for host * type) * @throws IOException in unknown circumstances * @throws BuildException if ignoreNoncriticalErrors has not been set to true * and a directory could not be created, for instance because it was * already existing. Precisely, the codes 521, 550 and 553 will trigger * a BuildException */ protected void makeRemoteDir(FTPClient ftp, String dir) throws IOException, BuildException { String workingDirectory = ftp.printWorkingDirectory(); if (task.isVerbose()) { if (dir.startsWith("/") || workingDirectory == null) { task.log("Creating directory: " + dir + " in /"); } else { task.log("Creating directory: " + dir + " in " + workingDirectory); } } if (dir.startsWith("/")) { ftp.changeWorkingDirectory("/"); } String subdir = ""; StringTokenizer st = new StringTokenizer(dir, "/"); while (st.hasMoreTokens()) { subdir = st.nextToken(); task.log("Checking " + subdir, Project.MSG_DEBUG); if (!ftp.changeWorkingDirectory(subdir)) { if (!ftp.makeDirectory(subdir)) { // codes 521, 550 and 553 can be produced by FTP Servers // to indicate that an attempt to create a directory has // failed because the directory already exists. int rc = ftp.getReplyCode(); if (!(task.isIgnoreNoncriticalErrors() && (rc == CODE_550 || rc == CODE_553 || rc == CODE_521))) { throw new BuildException("could not create directory: " + ftp.getReplyString()); } if (task.isVerbose()) { task.log("Directory already exists"); } } else { if (task.isVerbose()) { task.log("Directory created OK"); } ftp.changeWorkingDirectory(subdir); } } } if (workingDirectory != null) { ftp.changeWorkingDirectory(workingDirectory); } }
From source file:org.jboss.ejb3.examples.ch06.filetransfer.FileTransferBean.java
@Override public String pwd() { // Get the client final FTPClient client = this.getClient(); // Exec pwd/*from ww w . j a v a2 s . co m*/ try { final FTPFile[] files = client.listFiles(); for (final FTPFile file : files) { log.info(file.toString()); } // Exec pwd String dir = client.printWorkingDirectory(); String separator = File.separator; if ("\\".equals(separator)) { // reformat to use for windows if (dir.startsWith("/")) { dir = dir.substring(1); } dir = dir.replaceAll("/", "\\" + separator); } return dir; } catch (final IOException ioe) { throw new FileTransferException("Could not print working directory", ioe); } }
From source file:org.mockftpserver.stub.example.FtpWorkingDirectory.java
/** * Return the current working directory for the FTP account on the server * @return the current working directory * @throws SocketException/*w w w .j a v a 2s .c o m*/ * @throws IOException */ public String getWorkingDirectory() throws SocketException, IOException { FTPClient ftpClient = new FTPClient(); ftpClient.connect(server, port); return ftpClient.printWorkingDirectory(); }
From source file:org.mockftpserver.stub.StubFtpServer_StartTest.java
/** * Test setting a non-default port number for the StubFtpServer control connection socket. *///w w w. j a v a2s . co m public void testCustomServerControlPort() throws Exception { final int SERVER_CONTROL_PORT = 9187; final String DIR = "abc 1234567"; PwdCommandHandler pwd = new PwdCommandHandler(); pwd.setDirectory(DIR); stubFtpServer = new StubFtpServer(); stubFtpServer.setServerControlPort(SERVER_CONTROL_PORT); stubFtpServer.setCommandHandler(CommandNames.PWD, pwd); stubFtpServer.start(); try { FTPClient ftpClient = new FTPClient(); ftpClient.connect(SERVER, SERVER_CONTROL_PORT); assertEquals("pwd", DIR, ftpClient.printWorkingDirectory()); } finally { stubFtpServer.stop(); } }
From source file:org.moxie.ftp.FTP.java
/** * Create the specified directory on the remote host. * * @param ftp The FTP client connection/*from w w w .jav a 2 s .c om*/ * @param dir The directory to create (format must be correct for host * type) * @throws IOException in unknown circumstances * @throws BuildException if ignoreNoncriticalErrors has not been set to true * and a directory could not be created, for instance because it was * already existing. Precisely, the codes 521, 550 and 553 will trigger * a BuildException */ protected void makeRemoteDir(FTPClient ftp, String dir) throws IOException, BuildException { String workingDirectory = ftp.printWorkingDirectory(); if (verbose) { if (dir.indexOf("/") == 0 || workingDirectory == null) { log("Creating directory: " + dir + " in /"); } else { log("Creating directory: " + dir + " in " + workingDirectory); } } if (dir.indexOf("/") == 0) { ftp.changeWorkingDirectory("/"); } String subdir = ""; StringTokenizer st = new StringTokenizer(dir, "/"); while (st.hasMoreTokens()) { subdir = st.nextToken(); log("Checking " + subdir, Project.MSG_DEBUG); if (!ftp.changeWorkingDirectory(subdir)) { if (!ftp.makeDirectory(subdir)) { // codes 521, 550 and 553 can be produced by FTP Servers // to indicate that an attempt to create a directory has // failed because the directory already exists. int rc = ftp.getReplyCode(); if (!(ignoreNoncriticalErrors && (rc == CODE_550 || rc == CODE_553 || rc == CODE_521))) { throw new BuildException("could not create directory: " + ftp.getReplyString()); } if (verbose) { log("Directory already exists"); } } else { if (verbose) { log("Directory created OK"); } ftp.changeWorkingDirectory(subdir); } } } if (workingDirectory != null) { ftp.changeWorkingDirectory(workingDirectory); } }
From source file:org.moxie.ftp.FTPTaskMirrorImpl.java
/** * Create the specified directory on the remote host. * * @param ftp The FTP client connection/*from w w w . j a v a 2s . c om*/ * @param dir The directory to create (format must be correct for host * type) * @throws IOException in unknown circumstances * @throws BuildException if ignoreNoncriticalErrors has not been set to true * and a directory could not be created, for instance because it was * already existing. Precisely, the codes 521, 550 and 553 will trigger * a BuildException */ protected void makeRemoteDir(FTPClient ftp, String dir) throws IOException, BuildException { String workingDirectory = ftp.printWorkingDirectory(); if (task.isVerbose()) { if (dir.indexOf("/") == 0 || workingDirectory == null) { task.log("Creating directory: " + dir + " in /"); } else { task.log("Creating directory: " + dir + " in " + workingDirectory); } } if (dir.indexOf("/") == 0) { ftp.changeWorkingDirectory("/"); } String subdir = ""; StringTokenizer st = new StringTokenizer(dir, "/"); while (st.hasMoreTokens()) { subdir = st.nextToken(); task.log("Checking " + subdir, Project.MSG_DEBUG); if (!ftp.changeWorkingDirectory(subdir)) { if (!ftp.makeDirectory(subdir)) { // codes 521, 550 and 553 can be produced by FTP Servers // to indicate that an attempt to create a directory has // failed because the directory already exists. int rc = ftp.getReplyCode(); if (!(task.isIgnoreNoncriticalErrors() && (rc == CODE_550 || rc == CODE_553 || rc == CODE_521))) { throw new BuildException("could not create directory: " + ftp.getReplyString()); } if (task.isVerbose()) { task.log("Directory already exists"); } } else { if (task.isVerbose()) { task.log("Directory created OK"); } ftp.changeWorkingDirectory(subdir); } } } if (workingDirectory != null) { ftp.changeWorkingDirectory(workingDirectory); } }