Search in sources :

Example 91 with FTPFile

use of org.apache.commons.net.ftp.FTPFile in project ant by apache.

the class FTP method findFileName.

/**
 *  find a suitable name for local and remote temporary file
 */
private File findFileName(FTPClient ftp) {
    FTPFile[] files = null;
    final int maxIterations = 1000;
    for (int counter = 1; counter < maxIterations; counter++) {
        File localFile = FILE_UTILS.createTempFile("ant" + Integer.toString(counter), ".tmp", null, false, false);
        String fileName = localFile.getName();
        boolean found = false;
        try {
            if (files == null) {
                files = ftp.listFiles();
            }
            for (FTPFile file : files) {
                if (file != null && file.getName().equals(fileName)) {
                    found = true;
                    break;
                }
            }
        } catch (IOException ioe) {
            throw new BuildException(ioe, getLocation());
        }
        if (!found) {
            localFile.deleteOnExit();
            return localFile;
        }
    }
    return null;
}
Also used : FTPFile(org.apache.commons.net.ftp.FTPFile) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) FTPFile(org.apache.commons.net.ftp.FTPFile)

Example 92 with FTPFile

use of org.apache.commons.net.ftp.FTPFile in project ant by apache.

the class FTP method getTimeDiff.

/**
 * auto find the time difference between local and remote
 * @param ftp handle to ftp client
 * @return number of millis to add to remote time to make it comparable to local time
 * @since ant 1.6
 */
private long getTimeDiff(FTPClient ftp) {
    long returnValue = 0;
    File tempFile = findFileName(ftp);
    try {
        // create a local temporary file
        FILE_UTILS.createNewFile(tempFile);
        long localTimeStamp = tempFile.lastModified();
        BufferedInputStream instream = new BufferedInputStream(Files.newInputStream(tempFile.toPath()));
        ftp.storeFile(tempFile.getName(), instream);
        instream.close();
        boolean success = FTPReply.isPositiveCompletion(ftp.getReplyCode());
        if (success) {
            FTPFile[] ftpFiles = ftp.listFiles(tempFile.getName());
            if (ftpFiles.length == 1) {
                long remoteTimeStamp = ftpFiles[0].getTimestamp().getTime().getTime();
                returnValue = localTimeStamp - remoteTimeStamp;
            }
            ftp.deleteFile(ftpFiles[0].getName());
        }
        // delegate the deletion of the local temp file to the delete task
        // because of race conditions occurring on Windows
        Delete mydelete = new Delete();
        mydelete.bindToOwner(this);
        mydelete.setFile(tempFile.getCanonicalFile());
        mydelete.execute();
    } catch (Exception e) {
        throw new BuildException(e, getLocation());
    }
    return returnValue;
}
Also used : Delete(org.apache.tools.ant.taskdefs.Delete) BufferedInputStream(java.io.BufferedInputStream) FTPFile(org.apache.commons.net.ftp.FTPFile) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) FTPFile(org.apache.commons.net.ftp.FTPFile) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException)

Example 93 with FTPFile

use of org.apache.commons.net.ftp.FTPFile in project ant by apache.

the class FTP method getFile.

/**
 * Retrieve a single file from the remote host. <code>filename</code> may
 * contain a relative path specification. <p>
 *
 * The file will then be retrieved using the entire relative path spec -
 * no attempt is made to change directories. It is anticipated that this
 * may eventually cause problems with some FTP servers, but it simplifies
 * the coding.</p>
 * @param ftp the ftp client
 * @param dir local base directory to which the file should go back
 * @param filename relative path of the file based upon the ftp remote directory
 *        and/or the local base directory (dir)
 * @throws IOException  in unknown circumstances
 * @throws BuildException if skipFailedTransfers is false
 * and the file cannot be retrieved.
 */
protected void getFile(FTPClient ftp, String dir, String filename) throws IOException, BuildException {
    File file = getProject().resolveFile(new File(dir, filename).getPath());
    OutputStream outstream = null;
    try {
        if (newerOnly && isUpToDate(ftp, file, resolveFile(filename))) {
            return;
        }
        if (verbose) {
            log("transferring " + filename + " to " + file.getAbsolutePath());
        }
        File pdir = file.getParentFile();
        if (!pdir.exists()) {
            pdir.mkdirs();
        }
        outstream = new BufferedOutputStream(Files.newOutputStream(file.toPath()));
        ftp.retrieveFile(resolveFile(filename), outstream);
        if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
            String s = "could not get file: " + ftp.getReplyString();
            if (skipFailedTransfers) {
                log(s, Project.MSG_WARN);
                skipped++;
            } else {
                throw new BuildException(s);
            }
        } else {
            log("File " + file.getAbsolutePath() + " copied from " + server, Project.MSG_VERBOSE);
            transferred++;
            if (preserveLastModified) {
                outstream.close();
                outstream = null;
                FTPFile[] remote = ftp.listFiles(resolveFile(filename));
                if (remote.length > 0) {
                    FILE_UTILS.setFileLastModified(file, remote[0].getTimestamp().getTime().getTime());
                }
            }
        }
    } finally {
        FileUtils.close(outstream);
    }
}
Also used : BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FTPFile(org.apache.commons.net.ftp.FTPFile) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) FTPFile(org.apache.commons.net.ftp.FTPFile) BufferedOutputStream(java.io.BufferedOutputStream)

Example 94 with FTPFile

use of org.apache.commons.net.ftp.FTPFile in project ant by apache.

the class FTPTaskMirrorImpl method findFileName.

/**
 *  find a suitable name for local and remote temporary file
 */
private File findFileName(FTPClient ftp) {
    FTPFile[] files = null;
    final int maxIterations = 1000;
    for (int counter = 1; counter < maxIterations; counter++) {
        File localFile = FILE_UTILS.createTempFile("ant" + Integer.toString(counter), ".tmp", null, false, false);
        String fileName = localFile.getName();
        boolean found = false;
        try {
            if (files == null) {
                files = ftp.listFiles();
            }
            for (FTPFile file : files) {
                if (file != null && file.getName().equals(fileName)) {
                    found = true;
                    break;
                }
            }
        } catch (IOException ioe) {
            throw new BuildException(ioe, task.getLocation());
        }
        if (!found) {
            localFile.deleteOnExit();
            return localFile;
        }
    }
    return null;
}
Also used : FTPFile(org.apache.commons.net.ftp.FTPFile) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) FTPFile(org.apache.commons.net.ftp.FTPFile)

Example 95 with FTPFile

use of org.apache.commons.net.ftp.FTPFile in project ant by apache.

the class FTPTaskMirrorImpl method getFile.

/**
 * Retrieve a single file from the remote host. <code>filename</code> may
 * contain a relative path specification. <p>
 *
 * The file will then be retrieved using the entire relative path spec -
 * no attempt is made to change directories. It is anticipated that this
 * may eventually cause problems with some FTP servers, but it simplifies
 * the coding.</p>
 * @param ftp the ftp client
 * @param dir local base directory to which the file should go back
 * @param filename relative path of the file based upon the ftp remote directory
 *        and/or the local base directory (dir)
 * @throws IOException  in unknown circumstances
 * @throws BuildException if skipFailedTransfers is false
 * and the file cannot be retrieved.
 */
protected void getFile(FTPClient ftp, String dir, String filename) throws IOException, BuildException {
    OutputStream outstream = null;
    try {
        File file = task.getProject().resolveFile(new File(dir, filename).getPath());
        if (task.isNewer() && isUpToDate(ftp, file, resolveFile(filename))) {
            return;
        }
        if (task.isVerbose()) {
            task.log("transferring " + filename + " to " + file.getAbsolutePath());
        }
        File pdir = file.getParentFile();
        if (!pdir.exists()) {
            pdir.mkdirs();
        }
        outstream = new BufferedOutputStream(Files.newOutputStream(file.toPath()));
        ftp.retrieveFile(resolveFile(filename), outstream);
        if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
            String s = "could not get file: " + ftp.getReplyString();
            if (task.isSkipFailedTransfers()) {
                task.log(s, Project.MSG_WARN);
                skipped++;
            } else {
                throw new BuildException(s);
            }
        } else {
            task.log("File " + file.getAbsolutePath() + " copied from " + task.getServer(), Project.MSG_VERBOSE);
            transferred++;
            if (task.isPreserveLastModified()) {
                outstream.close();
                outstream = null;
                FTPFile[] remote = ftp.listFiles(resolveFile(filename));
                if (remote.length > 0) {
                    FILE_UTILS.setFileLastModified(file, remote[0].getTimestamp().getTime().getTime());
                }
            }
        }
    } finally {
        FileUtils.close(outstream);
    }
}
Also used : BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FTPFile(org.apache.commons.net.ftp.FTPFile) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) FTPFile(org.apache.commons.net.ftp.FTPFile) BufferedOutputStream(java.io.BufferedOutputStream)

Aggregations

FTPFile (org.apache.commons.net.ftp.FTPFile)120 IOException (java.io.IOException)59 FTPClient (org.apache.commons.net.ftp.FTPClient)34 Test (org.junit.Test)32 File (java.io.File)28 InputStream (java.io.InputStream)16 ArrayList (java.util.ArrayList)15 FrameworkException (org.structr.common.error.FrameworkException)15 Tx (org.structr.core.graph.Tx)15 FtpTest (org.structr.web.files.FtpTest)15 FileOutputStream (java.io.FileOutputStream)11 OutputStream (java.io.OutputStream)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 BuildException (org.apache.tools.ant.BuildException)8 List (java.util.List)7 Matchers.containsString (org.hamcrest.Matchers.containsString)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 BeanFactory (org.springframework.beans.factory.BeanFactory)5 LiteralExpression (org.springframework.expression.common.LiteralExpression)5 HashSet (java.util.HashSet)4