Example usage for org.apache.commons.net.ftp FTP BINARY_FILE_TYPE

List of usage examples for org.apache.commons.net.ftp FTP BINARY_FILE_TYPE

Introduction

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

Prototype

int BINARY_FILE_TYPE

To view the source code for org.apache.commons.net.ftp FTP BINARY_FILE_TYPE.

Click Source Link

Document

A constant used to indicate the file(s) being transfered should be treated as a binary image, i.e., no translations should be performed.

Usage

From source file:ubic.gemma.core.loader.expression.geo.fetcher.RawDataFetcher.java

/**
 * @param identifier The url for the supplementary file.
 * @return local files/*from  w w w.j ava 2s.co m*/
 */
@Override
public Collection<LocalFile> fetch(String identifier) {
    try {
        if (this.ftpClient == null || !this.ftpClient.isConnected())
            this.ftpClient = (new GeoUtil()).connect(FTP.BINARY_FILE_TYPE);
        assert this.ftpClient != null;
        File newDir = this.mkdir(identifier);
        newDir = new File(newDir, "rawDataFiles");
        if (!newDir.canRead() && !newDir.mkdir())
            throw new IOException("Could not create the raw data subdirectory");
        final String outputFileName = this.formLocalFilePath(identifier, newDir);
        final String seekFile = this.formRemoteFilePath(identifier);
        try {
            NetUtils.checkForFile(this.ftpClient, seekFile);
        } catch (FileNotFoundException e) {
            // that's okay, just return.
            AbstractFetcher.log.info(
                    "There is apparently no raw data archive for " + identifier + "(sought: " + seekFile + ")");
            EntityUtils.deleteFile(newDir);
            this.ftpClient.disconnect(); // important to do this!
            return null;
        }
        if (this.ftpClient == null || !this.ftpClient.isConnected()) {
            throw new IOException("Lost FTP connection");
        }
        long expectedSize = this.getExpectedSize(seekFile);
        FutureTask<Boolean> future = this.defineTask(outputFileName, seekFile);
        Collection<LocalFile> result = this.doTask(future, expectedSize, seekFile, outputFileName);

        if (result == null || result.isEmpty()) {
            throw new IOException("Files were not obtained, or download was cancelled.");
        }

        log.info(result.size() + " files obtained from archive");

        return result;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

}

From source file:ubic.gemma.core.loader.util.fetcher.FtpFetcher.java

protected Collection<LocalFile> fetch(String identifier, String seekFile) {
    File existingFile = null;//  w w  w .j a  va2s. c  o m
    try {
        File newDir = mkdir(identifier);
        String outputFileName = formLocalFilePath(identifier, newDir);

        existingFile = new File(outputFileName);
        //            if ( this.avoidDownload || ( existingFile.canRead() && allowUseExisting ) ) {
        //                // log.info( outputFileName + " already exists." );
        //            }

        if (ftpClient == null || !ftpClient.isConnected()) {
            ftpClient = this.getNetDataSourceUtil().connect(FTP.BINARY_FILE_TYPE);
            assert ftpClient != null; // otherwise should have gotten an exception from connect()
        }

        long expectedSize = getExpectedSize(seekFile);

        FutureTask<Boolean> future = this.defineTask(outputFileName, seekFile);
        return this.doTask(future, expectedSize, seekFile, outputFileName);
    } catch (UnknownHostException e) {
        if (force || !allowUseExisting || existingFile == null)
            throw new RuntimeException(e);

        if (!avoidDownload)
            throw new RuntimeException(e);

        log.warn("Could not connect to " + this.getNetDataSourceUtil().getHost() + " to check size of "
                + seekFile + ", using existing file");
        return getExistingFile(existingFile, seekFile);
    } catch (IOException e) {

        /*
         * Note: this block can trigger if you cancel.
         */

        if (force || !allowUseExisting || existingFile == null) {
            /*
             * Printing to log here because runtime error does not deliver message when passed through
             * java.util.concurrent.FutureTask (only throws InterruptedException and ExecutionException)
             */
            log.error("Runtime exception thrown: " + e.getMessage() + ". \n Stack trace follows:", e);
            throw new RuntimeException(
                    "Cancelled, or couldn't fetch " + seekFile
                            + ", make sure the file exists on the remote server and permissions are granted.",
                    e);

        }

        if (Thread.currentThread().isInterrupted()) {
            throw new CancellationException();
        }

        log.warn("Cancelled, or couldn't fetch " + seekFile
                + ", make sure the file exists on the remote server.," + e + ", using existing file");
        return getExistingFile(existingFile, seekFile);

    } finally {
        try {
            if (ftpClient != null && ftpClient.isConnected())
                ftpClient.disconnect();
        } catch (IOException e) {
            //noinspection ThrowFromFinallyBlock
            throw new RuntimeException("Could not disconnect: " + e.getMessage());
        }
    }
}

From source file:ubic.gemma.loader.expression.geo.fetcher.RawDataFetcher.java

/**
 * @param identifier//  w  w  w  .ja v  a 2s .  c o m
 * @return true if the files exist.
 */
public boolean checkForFile(String identifier) {
    try {
        // FIXME this needs to deal with the URL.
        if (this.ftpClient == null || !this.ftpClient.isConnected())
            this.ftpClient = (new GeoUtil()).connect(FTP.BINARY_FILE_TYPE);
        assert this.ftpClient != null;
        final String seekFile = formRemoteFilePath(identifier);
        try {
            NetUtils.checkForFile(this.ftpClient, seekFile);
            return true;
        } catch (FileNotFoundException e) {
            this.ftpClient.disconnect(); // important to do this!
            return false;
        }
    } catch (Exception e) {
        return false;
    }
}

From source file:ubic.gemma.loader.expression.geo.fetcher.RawDataFetcher.java

/**
 * @param identifier The url for the supplementary file.
 * @see ubic.gemma.loader.loaderutils.Fetcher#fetch(java.lang.String)
 *//*from   w  w  w . j  av a2  s  . co  m*/
@Override
public Collection<LocalFile> fetch(String identifier) {
    try {
        // FIXME this needs to deal with the URL.
        if (this.ftpClient == null || !this.ftpClient.isConnected())
            this.ftpClient = (new GeoUtil()).connect(FTP.BINARY_FILE_TYPE);
        assert this.ftpClient != null;
        File newDir = mkdir(identifier);
        newDir = new File(newDir, "rawDataFiles");
        if (!newDir.canRead() && !newDir.mkdir())
            throw new IOException("Could not create the raw data subdirectory");
        final String outputFileName = formLocalFilePath(identifier, newDir);
        final String seekFile = formRemoteFilePath(identifier);
        try {
            NetUtils.checkForFile(this.ftpClient, seekFile);
        } catch (FileNotFoundException e) {
            // that's okay, just return.
            log.info(
                    "There is apparently no raw data archive for " + identifier + "(sought: " + seekFile + ")");
            newDir.delete(); // nothing there.
            this.ftpClient.disconnect(); // important to do this!
            return null;
        }
        if (this.ftpClient == null || !this.ftpClient.isConnected()) {
            throw new IOException("Lost FTP connection");
        }
        long expectedSize = this.getExpectedSize(seekFile);
        FutureTask<Boolean> future = defineTask(outputFileName, seekFile);
        Collection<LocalFile> result = doTask(future, expectedSize, seekFile, outputFileName);

        if (result == null || result.isEmpty()) {
            throw new IOException("Files were not obtained, or download was cancelled.");
        }

        return result;
    } catch (SocketException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

}

From source file:ubic.gemma.loader.util.fetcher.FtpFetcher.java

/**
 * @param identifier/*w w  w .  j  ava 2s .co m*/
 * @param seekFile
 * @return
 */
protected Collection<LocalFile> fetch(String identifier, String seekFile) {
    File existingFile = null;
    try {
        File newDir = mkdir(identifier);
        String outputFileName = formLocalFilePath(identifier, newDir);

        existingFile = new File(outputFileName);
        if (this.avoidDownload || (existingFile.canRead() && allowUseExisting)) {
            // log.info( outputFileName + " already exists." );
        }

        if (ftpClient == null || !ftpClient.isConnected()) {
            ftpClient = this.getNetDataSourceUtil().connect(FTP.BINARY_FILE_TYPE);
            assert ftpClient != null; // otherwise should have gotten an exception from connect()
        }

        long expectedSize = getExpectedSize(seekFile);

        FutureTask<Boolean> future = this.defineTask(outputFileName, seekFile);
        Collection<LocalFile> result = this.doTask(future, expectedSize, seekFile, outputFileName);
        return result;
    } catch (UnknownHostException e) {
        if (force || !allowUseExisting || existingFile == null)
            throw new RuntimeException(e);

        if (!avoidDownload)
            throw new RuntimeException(e);

        log.warn("Could not connect to " + this.getNetDataSourceUtil().getHost() + " to check size of "
                + seekFile + ", using existing file");
        Collection<LocalFile> fallback = getExistingFile(existingFile, seekFile);
        return fallback;
    } catch (IOException e) {

        /*
         * Note: this block can trigger if you cancel.
         */

        if (force || !allowUseExisting || existingFile == null) {
            /*
             * Printing to log here because runtime error does not deliver message when passed through
             * java.util.concurrent.FutureTask (only throws InterruptedException and ExecutionException)
             */
            log.error("Runtime exception thrown: " + e.getMessage() + ". \n Stack trace follows:", e);
            throw new RuntimeException(
                    "Cancelled, or couldn't fetch " + seekFile
                            + ", make sure the file exists on the remote server and permissions are granted.",
                    e);

        }

        if (Thread.currentThread().isInterrupted()) {
            throw new CancellationException();
        }

        log.warn("Cancelled, or couldn't fetch " + seekFile
                + ", make sure the file exists on the remote server.," + e + ", using existing file");
        Collection<LocalFile> fallback = getExistingFile(existingFile, seekFile);
        return fallback;

    } finally {
        try {
            if (ftpClient != null && ftpClient.isConnected())
                ftpClient.disconnect();
        } catch (IOException e) {
            throw new RuntimeException("Could not disconnect: " + e.getMessage());
        }
    }
}

From source file:ubicrypt.core.provider.ftp.FTProvider.java

private Observable<FTPClient> connect() {
    return Observable.<FTPClient>create(subscriber -> {
        final FTPClient client = new FTPClient();
        try {/*ww w . j av a2 s.  co m*/
            client.connect(conf.getHost(), getConf().getPort() == -1 ? 21 : getConf().getPort());
            final int reply = client.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                log.error("FTP server refused connection:" + client.getReplyString());
                if (client.isConnected()) {
                    client.disconnect();
                }
                subscriber.onError(
                        new RuntimeException("FTP server refused connection:" + client.getReplyString()));
                return;
            }
            if (!getConf().isAnonymous()) {
                if (!client.login(getConf().getUsername(), new String(getConf().getPassword()))) {
                    client.disconnect();
                    log.warn("FTP wrong credentials:" + client.getReplyString());
                    subscriber.onError(new RuntimeException("FTP wrong credentials"));
                }
            }
            client.setFileType(FTP.BINARY_FILE_TYPE);
            client.setBufferSize(1 << 64);
            client.enterLocalPassiveMode();
            client.setControlKeepAliveTimeout(60 * 60); //1h
            if (!isEmpty(conf.getFolder())) {
                final String directory = startsWith("/", conf.getFolder()) ? conf.getFolder()
                        : "/" + conf.getFolder();
                if (!client.changeWorkingDirectory(directory)) {
                    if (!client.makeDirectory(directory)) {
                        disconnect(client);
                        subscriber.onError(new ProviderException(showServerReply(client)));
                        return;
                    }
                    if (!client.changeWorkingDirectory(directory)) {
                        disconnect(client);
                        subscriber.onError(new ProviderException(showServerReply(client)));
                        return;
                    }
                }
            }
            subscriber.onNext(client);
            subscriber.onCompleted();
        } catch (final IOException e) {
            disconnect(client);
            subscriber.onError(e);
        }
    }).subscribeOn(Schedulers.io());
}

From source file:ucar.unidata.idv.ui.ImageGenerator.java

/**
 * Do an FTP put of the given bytes//from  w  w  w . ja v  a 2  s .  c  o m
 *
 * @param server server
 * @param userName user name on server
 * @param password password on server
 * @param destination Where to put the bytes
 * @param bytes The bytes
 *
 * @throws Exception On badness
 */
public static void ftpPut(String server, String userName, String password, String destination, byte[] bytes)
        throws Exception {
    FTPClient f = new FTPClient();

    f.connect(server);
    f.login(userName, password);
    f.setFileType(FTP.BINARY_FILE_TYPE);
    f.enterLocalPassiveMode();
    checkFtp(f, "Connecting to ftp server");
    f.storeFile(destination, new ByteArrayInputStream(bytes));
    checkFtp(f, "Storing file");
    f.logout();
    f.disconnect();
}

From source file:uk.ac.bbsrc.tgac.miso.core.util.TransmissionUtils.java

public static FTPClient ftpConnect(String host, String username, String password) throws IOException {
    FTPClient ftp = new FTPClient();
    try {/*from  www .j  a v  a2 s .  c  o m*/

        ftp.connect(host);
        log.debug("Trying " + host);
        log.debug(ftp.getReplyString());
        int reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            throw new IOException("FTP server refused connection: " + reply);
        } else {
            log.info("Connected");
        }

        ftp.login(username, password);
        ftp.setFileType(FTP.BINARY_FILE_TYPE);
        ftp.enterLocalPassiveMode();
    } catch (NoRouteToHostException e) {
        throw new IOException("Couldn't connect to printer: " + e.getMessage(), e);
    } catch (UnknownHostException e) {
        throw new IOException("Couldn't connect to printer: " + e.getMessage(), e);
    }
    return ftp;
}

From source file:uk.ac.bbsrc.tgac.miso.core.util.TransmissionUtils.java

public static FTPClient ftpConnect(String host, int port, String username, String password) throws IOException {
    FTPClient ftp = new FTPClient();
    try {//from ww w.j  a v a2 s .c o  m
        ftp.connect(host, port);
        log.debug("Trying " + host + ":" + port);
        log.debug(ftp.getReplyString());
        int reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            throw new IOException("FTP server refused connection: " + reply);
        } else {
            log.debug("Connected");
        }

        ftp.login(username, password);
        ftp.setFileType(FTP.BINARY_FILE_TYPE);
        ftp.enterLocalPassiveMode();
    } catch (NoRouteToHostException e) {
        throw new IOException("Couldn't connect to printer: " + e.getMessage(), e);
    } catch (UnknownHostException e) {
        throw new IOException("Couldn't connect to printer: " + e.getMessage(), e);
    }
    return ftp;
}

From source file:uk.co.marcoratto.net.ClientFTP.java

/** Use Binary mode for file transfers */
public boolean setFileTypeBINARY() throws ClientFTPException {
    boolean reply = false;
    try {//w  ww . j a v  a  2s . co  m
        reply = ftp.setFileType(FTP.BINARY_FILE_TYPE);
        this.printReplyStrings();
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
        throw new ClientFTPException(e);
    }
    return reply;
}