List of usage examples for org.apache.commons.net.ftp FTPClient changeWorkingDirectory
public boolean changeWorkingDirectory(String pathname) throws IOException
From source file:org.apache.hadoop.fs.ftp.FTPFileSystem.java
/** * Convenience method, so that we don't open a new connection when using this * method from within another method. Otherwise every API invocation incurs * the overhead of opening/closing a TCP connection. * /*from w w w.j a va2s . c om*/ * @param client * @param src * @param dst * @return * @throws IOException */ private boolean rename(FTPClient client, Path src, Path dst) throws IOException { Path workDir = new Path(client.printWorkingDirectory()); Path absoluteSrc = makeAbsolute(workDir, src); Path absoluteDst = makeAbsolute(workDir, dst); if (!exists(client, absoluteSrc)) { throw new IOException("Source path " + src + " does not exist"); } if (exists(client, absoluteDst)) { throw new IOException("Destination path " + dst + " already exist, cannot rename!"); } String parentSrc = absoluteSrc.getParent().toUri().toString(); String parentDst = absoluteDst.getParent().toUri().toString(); String from = src.getName(); String to = dst.getName(); if (!parentSrc.equals(parentDst)) { throw new IOException( "Cannot rename parent(source): " + parentSrc + ", parent(destination): " + parentDst); } client.changeWorkingDirectory(parentSrc); boolean renamed = client.rename(from, to); return renamed; }
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 . j a v a2s . com 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.sqoop.connector.mainframe.MainframeFTPClientUtils.java
public static void setWorkingDirectory(TransferableContext context, FTPClient ftp, String datasetName) throws IOException { String modifiedDatasetName = MainframeUtils.getDatasetName(context, datasetName); boolean replyCode = ftp.changeWorkingDirectory(modifiedDatasetName); if (!replyCode) { throw new IOException("Unable to change working directory to " + modifiedDatasetName + " for ftp transfer with ftp client = " + ftp + ". " + ftp.getReplyString()); }//from w w w . j a v a 2s . c om }
From source file:org.apache.sqoop.util.MainframeFTPClientUtils.java
public static List<String> listSequentialDatasets(String pdsName, Configuration conf) throws IOException { List<String> datasets = new ArrayList<String>(); String dsName = pdsName;//from w w w . j a v a 2s . c o m String fileName = ""; MainframeDatasetPath p = null; try { p = new MainframeDatasetPath(dsName, conf); } catch (Exception e) { LOG.error(e.getMessage()); LOG.error("MainframeDatasetPath helper class incorrectly initialised"); e.printStackTrace(); } String dsType = conf.get(MainframeConfiguration.MAINFRAME_INPUT_DATASET_TYPE); boolean isTape = Boolean.parseBoolean(conf.get(MainframeConfiguration.MAINFRAME_INPUT_DATASET_TAPE)); boolean isSequentialDs = false; boolean isGDG = false; if (dsType != null && p != null) { isSequentialDs = p.getMainframeDatasetType().toString() .equals(MainframeConfiguration.MAINFRAME_INPUT_DATASET_TYPE_SEQUENTIAL); isGDG = p.getMainframeDatasetType().toString() .equals(MainframeConfiguration.MAINFRAME_INPUT_DATASET_TYPE_GDG); pdsName = p.getMainframeDatasetFolder(); fileName = p.getMainframeDatasetFileName(); } FTPClient ftp = null; try { ftp = getFTPConnection(conf); if (ftp != null) { ftp.changeWorkingDirectory("'" + pdsName + "'"); FTPFile[] ftpFiles = null; if (isTape) { FTPListParseEngine parser = ftp.initiateListParsing( MainframeConfiguration.MAINFRAME_FTP_FILE_ENTRY_PARSER_CLASSNAME, ""); List<FTPFile> listing = new ArrayList<FTPFile>(); while (parser.hasNext()) { FTPFile[] files = parser.getNext(25); for (FTPFile file : files) { if (file != null) { listing.add(file); LOG.info(String.format("Name: %s Type: %s", file.getName(), file.getType())); } // skip nulls returned from parser } ftpFiles = new FTPFile[listing.size()]; for (int i = 0; i < listing.size(); i++) { ftpFiles[i] = listing.get(i); } LOG.info("Files returned from mainframe parser:-"); for (FTPFile f : ftpFiles) { LOG.info(String.format("Name: %s, Type: %s", f.getName(), f.getType())); } } } else { ftpFiles = ftp.listFiles(); } if (!isGDG) { for (FTPFile f : ftpFiles) { LOG.info(String.format("Name: %s Type: %s", f.getName(), f.getType())); if (f.getType() == FTPFile.FILE_TYPE) { // only add datasets if default behaviour of partitioned data sets // or if it is a sequential data set, only add if the file name matches exactly if (!isSequentialDs || isSequentialDs && f.getName().equals(fileName) && !fileName.equals("")) { datasets.add(f.getName()); } } } } else { LOG.info("GDG branch. File list:-"); for (FTPFile f : ftpFiles) { LOG.info(String.format("Name: %s Type: %s", f.getName(), f.getType())); } if (ftpFiles.length > 0 && ftpFiles[ftpFiles.length - 1].getType() == FTPFile.FILE_TYPE) { // for GDG - add the last file in the collection datasets.add(ftpFiles[ftpFiles.length - 1].getName()); } } } } catch (IOException ioe) { throw new IOException("Could not list datasets from " + pdsName + ":" + ioe.toString()); } finally { if (ftp != null) { closeFTPConnection(ftp); } } return datasets; }
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//from ww w . j a v a 2 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.FTP.java
/** * Runs the task./*from w ww .ja va 2 s . c o m*/ * * @throws BuildException if the task fails or is not configured * correctly. */ public void execute() throws BuildException { checkAttributes(); FTPClient ftp = null; try { log("Opening FTP connection to " + server, Project.MSG_VERBOSE); ftp = new FTPClient(); if (this.isConfigurationSet) { ftp = FTPConfigurator.configure(ftp, this); } ftp.setRemoteVerificationEnabled(enableRemoteVerification); ftp.connect(server, port); if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) { throw new BuildException("FTP connection failed: " + ftp.getReplyString()); } log("connected", Project.MSG_VERBOSE); log("logging in to FTP server", Project.MSG_VERBOSE); if ((this.account != null && !ftp.login(userid, password, account)) || (this.account == null && !ftp.login(userid, password))) { throw new BuildException("Could not login to FTP server"); } log("login succeeded", Project.MSG_VERBOSE); if (binary) { ftp.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE); if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) { throw new BuildException("could not set transfer type: " + ftp.getReplyString()); } } else { ftp.setFileType(org.apache.commons.net.ftp.FTP.ASCII_FILE_TYPE); if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) { throw new BuildException("could not set transfer type: " + ftp.getReplyString()); } } if (passive) { log("entering passive mode", Project.MSG_VERBOSE); ftp.enterLocalPassiveMode(); if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) { throw new BuildException("could not enter into passive " + "mode: " + ftp.getReplyString()); } } // If an initial command was configured then send it. // Some FTP servers offer different modes of operation, // E.G. switching between a UNIX file system mode and // a legacy file system. if (this.initialSiteCommand != null) { RetryHandler h = new RetryHandler(this.retriesAllowed, this); final FTPClient lftp = ftp; executeRetryable(h, new Retryable() { public void execute() throws IOException { doSiteCommand(lftp, FTP.this.initialSiteCommand); } }, "initial site command: " + this.initialSiteCommand); } // For a unix ftp server you can set the default mask for all files // created. if (umask != null) { RetryHandler h = new RetryHandler(this.retriesAllowed, this); final FTPClient lftp = ftp; executeRetryable(h, new Retryable() { public void execute() throws IOException { doSiteCommand(lftp, "umask " + umask); } }, "umask " + umask); } // If the action is MK_DIR, then the specified remote // directory is the directory to create. if (action == MK_DIR) { RetryHandler h = new RetryHandler(this.retriesAllowed, this); final FTPClient lftp = ftp; executeRetryable(h, new Retryable() { public void execute() throws IOException { makeRemoteDir(lftp, remotedir); } }, remotedir); } else if (action == SITE_CMD) { RetryHandler h = new RetryHandler(this.retriesAllowed, this); final FTPClient lftp = ftp; executeRetryable(h, new Retryable() { public void execute() throws IOException { doSiteCommand(lftp, FTP.this.siteCommand); } }, "Site Command: " + this.siteCommand); } else { if (remotedir != null) { log("changing the remote directory to " + remotedir, Project.MSG_VERBOSE); ftp.changeWorkingDirectory(remotedir); if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) { throw new BuildException("could not change remote " + "directory: " + ftp.getReplyString()); } } if (newerOnly && timeDiffAuto) { // in this case we want to find how much time span there is between local // and remote timeDiffMillis = getTimeDiff(ftp); } log(ACTION_STRS[action] + " " + ACTION_TARGET_STRS[action]); transferFiles(ftp); } } catch (IOException ex) { throw new BuildException("error during FTP transfer: " + ex, ex); } finally { if (ftp != null && ftp.isConnected()) { try { log("disconnecting", Project.MSG_VERBOSE); ftp.logout(); ftp.disconnect(); } catch (IOException ex) { // ignore it } } } }
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//from ww w . jav a 2s. c om * @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./*from ww w . j av a 2 s . 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 a v a 2 s.com*/ * @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.apache.tools.ant.taskdefs.optional.net.FTPTaskMirrorImpl.java
public void doFTP() throws BuildException { FTPClient ftp = null; try {/*from w w w.j av a 2s . c om*/ task.log("Opening FTP connection to " + task.getServer(), Project.MSG_VERBOSE); ftp = new FTPClient(); if (task.isConfigurationSet()) { ftp = FTPConfigurator.configure(ftp, task); } ftp.setRemoteVerificationEnabled(task.getEnableRemoteVerification()); ftp.connect(task.getServer(), task.getPort()); if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) { throw new BuildException("FTP connection failed: " + ftp.getReplyString()); } task.log("connected", Project.MSG_VERBOSE); task.log("logging in to FTP server", Project.MSG_VERBOSE); if ((task.getAccount() != null && !ftp.login(task.getUserid(), task.getPassword(), task.getAccount())) || (task.getAccount() == null && !ftp.login(task.getUserid(), task.getPassword()))) { throw new BuildException("Could not login to FTP server"); } task.log("login succeeded", Project.MSG_VERBOSE); if (task.isBinary()) { ftp.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE); if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) { throw new BuildException("could not set transfer type: " + ftp.getReplyString()); } } else { ftp.setFileType(org.apache.commons.net.ftp.FTP.ASCII_FILE_TYPE); if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) { throw new BuildException("could not set transfer type: " + ftp.getReplyString()); } } if (task.isPassive()) { task.log("entering passive mode", Project.MSG_VERBOSE); ftp.enterLocalPassiveMode(); if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) { throw new BuildException("could not enter into passive " + "mode: " + ftp.getReplyString()); } } // If an initial command was configured then send it. // Some FTP servers offer different modes of operation, // E.G. switching between a UNIX file system mode and // a legacy file system. if (task.getInitialSiteCommand() != null) { RetryHandler h = new RetryHandler(task.getRetriesAllowed(), task); final FTPClient lftp = ftp; executeRetryable(h, new Retryable() { public void execute() throws IOException { doSiteCommand(lftp, task.getInitialSiteCommand()); } }, "initial site command: " + task.getInitialSiteCommand()); } // For a unix ftp server you can set the default mask for all files // created. if (task.getUmask() != null) { RetryHandler h = new RetryHandler(task.getRetriesAllowed(), task); final FTPClient lftp = ftp; executeRetryable(h, new Retryable() { public void execute() throws IOException { doSiteCommand(lftp, "umask " + task.getUmask()); } }, "umask " + task.getUmask()); } // If the action is MK_DIR, then the specified remote // directory is the directory to create. if (task.getAction() == FTPTask.MK_DIR) { RetryHandler h = new RetryHandler(task.getRetriesAllowed(), task); final FTPClient lftp = ftp; executeRetryable(h, new Retryable() { public void execute() throws IOException { makeRemoteDir(lftp, task.getRemotedir()); } }, task.getRemotedir()); } else if (task.getAction() == FTPTask.SITE_CMD) { RetryHandler h = new RetryHandler(task.getRetriesAllowed(), task); final FTPClient lftp = ftp; executeRetryable(h, new Retryable() { public void execute() throws IOException { doSiteCommand(lftp, task.getSiteCommand()); } }, "Site Command: " + task.getSiteCommand()); } else { if (task.getRemotedir() != null) { task.log("changing the remote directory", Project.MSG_VERBOSE); ftp.changeWorkingDirectory(task.getRemotedir()); if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) { throw new BuildException("could not change remote " + "directory: " + ftp.getReplyString()); } } if (task.isNewer() && task.isTimeDiffAuto()) { // in this case we want to find how much time span there is between local // and remote task.setTimeDiffMillis(getTimeDiff(ftp)); } task.log( FTPTask.ACTION_STRS[task.getAction()] + " " + FTPTask.ACTION_TARGET_STRS[task.getAction()]); transferFiles(ftp); } } catch (IOException ex) { throw new BuildException("error during FTP transfer: " + ex, ex); } finally { if (ftp != null && ftp.isConnected()) { try { task.log("disconnecting", Project.MSG_VERBOSE); ftp.logout(); ftp.disconnect(); } catch (IOException ex) { // ignore it } } } }