Example usage for org.apache.commons.net.ftp FTPReply isPositiveCompletion

List of usage examples for org.apache.commons.net.ftp FTPReply isPositiveCompletion

Introduction

In this page you can find the example usage for org.apache.commons.net.ftp FTPReply isPositiveCompletion.

Prototype

public static boolean isPositiveCompletion(int reply) 

Source Link

Document

Determine if a reply code is a positive completion response.

Usage

From source file:org.mcisb.util.io.FtpClient.java

/**
 * //from   ww  w .j  ava  2 s . c o m
 * @throws IOException
 */
private void check() throws IOException {
    final int reply = client.getReplyCode();

    if (!FTPReply.isPositiveCompletion(reply)) {
        final String response = client.getReplyString();
        client.disconnect();
        throw new IOException(response);
    }
}

From source file:org.mousephenotype.dcc.crawler.Downloader.java

private boolean ftpConnect(String username, String password) {
    boolean returnValue = false;
    try {//from   www. j a v  a2  s . c  om
        client = new FTPClient();
        client.connect(hostname);
        client.login(username, password);
        int reply = client.getReplyCode();
        if (FTPReply.isPositiveCompletion(reply)) {
            if (client.setFileType(FTPClient.BINARY_FILE_TYPE)) {
                returnValue = true;
            } else {
                logger.error("Failed to set ftp transfer mode to binary for '{}'... will now close connection",
                        hostname);
                client.disconnect();
            }
        } else {
            logger.error("Server at '{}' refused connection", hostname);
            client.disconnect();
        }
    } catch (IOException e) {
        logger.error(e.getMessage());
    }
    return returnValue;
}

From source file:org.mousephenotype.dcc.crawler.FtpCrawler.java

private boolean connect(String username, String password) {
    boolean returnValue = false;
    try {/*  www. jav  a2 s .  c o m*/
        logger.debug("Crawler is trying to connect to '{}'", hostname);
        client = new FTPClient();
        client.setConnectTimeout(CONNECT_TIMEOUT_MILLISECS);
        client.connect(hostname);
        client.login(username, password);
        int reply = client.getReplyCode();
        if (FTPReply.isPositiveCompletion(reply)) {
            logger.debug("Crawler is connected to ftp server at '{}'", hostname);
            returnValue = true;
        } else {
            logger.error("Server at '{}' refused connection", hostname);
            client.disconnect();
        }
    } catch (IOException e) {
        logger.error(e.getMessage());
    }
    return returnValue;
}

From source file:org.moxie.ftp.FTPTaskMirrorImpl.java

/**
 * auto find the time difference between local and remote
 * @param ftp handle to ftp client/*from  w w w  .  java2  s.co  m*/
 * @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(new FileInputStream(tempFile));
        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 occuring on Windows
        Delete mydelete = new Delete();
        mydelete.bindToOwner(task);
        mydelete.setFile(tempFile.getCanonicalFile());
        mydelete.execute();
    } catch (Exception e) {
        throw new BuildException(e, task.getLocation());
    }
    return returnValue;
}

From source file:org.moxie.ftp.FTPTaskMirrorImpl.java

/**
 * Sends a single file to the remote host. <code>filename</code> may
 * contain a relative path specification. When this is the case, <code>sendFile</code>
 * will attempt to create any necessary parent directories before sending
 * the file. The file will then be sent 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./*from   ww w  .  j  a v  a  2 s .co m*/
 * @param ftp ftp client
 * @param dir base directory of the file to be sent (local)
 * @param filename relative path of the file to be send
 *        locally relative to dir
 *        remotely relative to the remotedir attribute
 * @throws IOException  in unknown circumstances
 * @throws BuildException in unknown circumstances
 */
protected void sendFile(FTPClient ftp, String dir, String filename) throws IOException, BuildException {
    InputStream instream = null;

    try {
        // XXX - why not simply new File(dir, filename)?
        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 " + file.getAbsolutePath());
        }

        instream = new BufferedInputStream(new FileInputStream(file));

        createParents(ftp, filename);

        ftp.storeFile(resolveFile(filename), instream);

        boolean success = FTPReply.isPositiveCompletion(ftp.getReplyCode());

        if (!success) {
            String s = "could not put file: " + ftp.getReplyString();

            if (task.isSkipFailedTransfers()) {
                task.log(s, Project.MSG_WARN);
                skipped++;
            } else {
                throw new BuildException(s);
            }

        } else {
            // see if we should issue a chmod command
            if (task.getChmod() != null) {
                doSiteCommand(ftp, "chmod " + task.getChmod() + " " + resolveFile(filename));
            }
            task.log("File " + file.getAbsolutePath() + " copied to " + task.getServer(), Project.MSG_VERBOSE);
            transferred++;
        }
    } finally {
        if (instream != null) {
            try {
                instream.close();
            } catch (IOException ex) {
                // ignore it
            }
        }
    }
}

From source file:org.moxie.ftp.FTPTaskMirrorImpl.java

/**
 * Retrieve a single file from the remote host. <code>filename</code> may
 * contain a relative path specification. <p>
 *
 * The file will then be retreived 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>/* w w  w .  ja va2 s.  com*/
 * @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(new FileOutputStream(file));
        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 {
        if (outstream != null) {
            try {
                outstream.close();
            } catch (IOException ex) {
                // ignore it
            }
        }
    }
}

From source file:org.mule.transport.ftp.FtpConnectionFactory.java

public Object makeObject() throws Exception {
    FTPClient client = new FTPClient();
    if (uri.getPort() > 0) {
        client.connect(uri.getHost(), uri.getPort());
    } else {/*from   ww w  .  ja  v a2s .c o  m*/
        client.connect(uri.getHost());
    }
    if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
        throw new IOException("Ftp connect failed: " + client.getReplyCode());
    }
    if (!client.login(uri.getUser(), uri.getPassword())) {
        throw new IOException("Ftp login failed: " + client.getReplyCode());
    }
    if (!client.setFileType(FTP.BINARY_FILE_TYPE)) {
        throw new IOException("Ftp error. Couldn't set BINARY transfer type: " + client.getReplyCode());
    }
    return client;
}

From source file:org.mule.transport.ftp.FtpMessageReceiver.java

protected FTPFile[] listFiles() throws Exception {
    FTPClient client = null;/* w  w  w  .jav  a2s. co m*/
    try {
        client = connector.createFtpClient(endpoint);
        FTPListParseEngine engine = client.initiateListParsing();
        FTPFile[] files = null;
        List<FTPFile> v = new ArrayList<FTPFile>();
        while (engine.hasNext()) {
            if (getLifecycleState().isStopping()) {
                break;
            }
            files = engine.getNext(FTP_LIST_PAGE_SIZE);
            if (files == null || files.length == 0) {
                return files;
            }
            for (FTPFile file : files) {
                if (file.isFile()) {
                    if (filenameFilter == null || filenameFilter.accept(null, file.getName())) {
                        v.add(file);
                    }
                }
            }
        }

        if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
            throw new IOException("Failed to list files. Ftp error: " + client.getReplyCode());
        }

        return v.toArray(new FTPFile[v.size()]);
    } finally {
        if (client != null) {
            connector.releaseFtp(endpoint.getEndpointURI(), client);
        }
    }
}

From source file:org.mule.transport.ftp.FtpMessageRequester.java

protected FTPFile findFileToProcess(FTPClient client) throws Exception {
    FTPListParseEngine engine = client.initiateListParsing();
    FTPFile[] files = null;//ww w .  ja  v a2  s.co  m
    while (engine.hasNext()) {
        files = engine.getNext(FTP_LIST_PAGE_SIZE);
        if (files == null) {
            break;
        }
        FilenameFilter filenameFilter = getFilenameFilter();
        for (int i = 0; i < files.length; i++) {
            FTPFile file = files[i];
            if (file.isFile()) {
                if (filenameFilter.accept(null, file.getName())) {
                    if (connector.validateFile(file)) {
                        // only read the first one
                        return file;
                    }
                }
            }
        }
    }
    if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
        throw new IOException("Ftp error: " + client.getReplyCode());
    }

    return null;
}

From source file:org.mule.transport.ftps.FtpsConnectionFactory.java

public Object makeObject() throws Exception {
    FTPSClient client = createFTPSClient();

    if (uri.getPort() > 0) {
        client.connect(uri.getHost(), uri.getPort());
    } else {/*from  www  .ja v  a  2s.  c  om*/
        client.connect(uri.getHost());
    }
    if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
        throw new IOException("Ftp connect failed: " + client.getReplyCode());
    }
    if (!client.login(uri.getUser(), uri.getPassword())) {
        throw new IOException("Ftp login failed: " + client.getReplyCode());
    }
    if (!client.setFileType(FTP.BINARY_FILE_TYPE)) {
        throw new IOException("Ftp error. Couldn't set BINARY transfer type: " + client.getReplyCode());
    }
    return client;
}