Search in sources :

Example 1 with FileDownloadException

use of org.monarchinitiative.loinc2hpo.exception.FileDownloadException in project loinc2hpo by monarch-initiative.

the class FileDownloader method copyURLToFileThroughURL.

/**
 * Copy contents of a URL to a file using the {@link URL} class.
 *
 * This works for the HTTP and the HTTPS protocol and for FTP through a proxy. For plain FTP, we need to use the
 * passive mode.
 */
private boolean copyURLToFileThroughURL(URL src, File dest) throws FileDownloadException {
    setProxyProperties();
    // actually copy the file
    BufferedInputStream in = null;
    FileOutputStream out = null;
    try {
        URLConnection connection = src.openConnection();
        final int fileSize = connection.getContentLength();
        in = new BufferedInputStream(connection.getInputStream());
        out = new FileOutputStream(dest);
        ProgressBar pb = null;
        if (fileSize != -1)
            pb = new ProgressBar(0, fileSize, options.printProgressBar);
        else
            logger.info("(server did not tell us the file size, no progress bar)");
        // Download file.
        byte[] buffer = new byte[128 * 1024];
        int readCount;
        long pos = 0;
        if (pb != null)
            pb.print(pos);
        while ((readCount = in.read(buffer)) > 0) {
            out.write(buffer, 0, readCount);
            pos += readCount;
            if (pb != null)
                pb.print(pos);
        }
        in.close();
        out.close();
        if (pb != null && pos != pb.getMax())
            pb.print(fileSize);
    } catch (IOException e) {
        throw new FileDownloadException("ERROR: Problem downloading file: " + e.getMessage());
    }
    return true;
}
Also used : URLConnection(java.net.URLConnection) FileDownloadException(org.monarchinitiative.loinc2hpo.exception.FileDownloadException)

Example 2 with FileDownloadException

use of org.monarchinitiative.loinc2hpo.exception.FileDownloadException in project loinc2hpo by monarch-initiative.

the class FileDownloader method copyURLToFileWithFTP.

private boolean copyURLToFileWithFTP(URL src, File dest) throws FileDownloadException {
    final FTPClient ftp = new FTPClient();
    // passive mode for firewalls
    ftp.enterLocalPassiveMode();
    try {
        if (src.getPort() != -1)
            ftp.connect(src.getHost(), src.getPort());
        else
            ftp.connect(src.getHost());
        if (!ftp.login("anonymous", "anonymous@example.com"))
            throw new IOException("Could not login with anonymous:anonymous@example.com");
        if (!ftp.isConnected())
            logger.error("Weird, not connected!");
    } catch (SocketException e) {
        throw new FileDownloadException("ERROR: problem connecting when downloading file.", e);
    } catch (IOException e) {
        throw new FileDownloadException("ERROR: problem connecting when downloading file.", e);
    }
    try {
        // binary file transfer
        ftp.setFileType(FTP.BINARY_FILE_TYPE);
    } catch (IOException e) {
        try {
            ftp.logout();
        } catch (IOException e1) {
        // swallow, nothing we can do about it
        }
        try {
            ftp.disconnect();
        } catch (IOException e1) {
        // swallow, nothing we can do about it
        }
        throw new FileDownloadException("ERROR: could not use binary transfer.", e);
    }
    InputStream in = null;
    OutputStream out = null;
    try {
        final String parentDir = new File(src.getPath()).getParent().substring(1);
        final String fileName = new File(src.getPath()).getName();
        if (!ftp.changeWorkingDirectory(parentDir))
            throw new FileNotFoundException("Could not change directory to " + parentDir);
        // Try to get file size.
        FTPFile[] files = ftp.listFiles(fileName);
        long fileSize = -1;
        for (int i = 0; i < files.length; ++i) if (files[i].getName().equals(fileName))
            fileSize = files[i].getSize();
        ftp.pwd();
        ProgressBar pb = null;
        if (fileSize != -1)
            pb = new ProgressBar(0, fileSize, options.printProgressBar);
        else
            logger.info("(server did not tell us the file size, no progress bar)");
        // Download file.
        in = ftp.retrieveFileStream(fileName);
        if (in == null)
            throw new FileNotFoundException("Could not open connection for file " + fileName);
        out = new FileOutputStream(dest);
        BufferedInputStream inBf = new BufferedInputStream(in);
        byte[] buffer = new byte[128 * 1024];
        int readCount;
        long pos = 0;
        if (pb != null)
            pb.print(pos);
        while ((readCount = inBf.read(buffer)) > 0) {
            out.write(buffer, 0, readCount);
            pos += readCount;
            if (pb != null)
                pb.print(pos);
        }
        in.close();
        out.close();
        if (pb != null && pos != pb.getMax())
            pb.print(fileSize);
    // if (!ftp.completePendingCommand())
    // throw new IOException("Could not finish download!");
    } catch (FileNotFoundException e) {
        dest.delete();
        try {
            ftp.logout();
        } catch (IOException e1) {
        // swallow, nothing we can do about it
        }
        try {
            ftp.disconnect();
        } catch (IOException e1) {
        // swallow, nothing we can do about it
        }
        throw new FileDownloadException("ERROR: problem downloading file.", e);
    } catch (IOException e) {
        dest.delete();
        try {
            ftp.logout();
        } catch (IOException e1) {
        // swallow, nothing we can do about it
        }
        try {
            ftp.disconnect();
        } catch (IOException e1) {
        // swallow, nothing we can do about it
        }
        throw new FileDownloadException("ERROR: problem downloading file.", e);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
            // swallow, nothing we can do
            }
        }
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
            // swallow, nothing we can do
            }
        }
    // if (ftp != null) {
    // try {
    // ftp.completePendingCommand();
    // } catch (IOException e) {
    // // swallow, nothing we can do
    // }
    // }
    }
    return false;
}
Also used : SocketException(java.net.SocketException) FTPFile(org.apache.commons.net.ftp.FTPFile) FTPClient(org.apache.commons.net.ftp.FTPClient) FTPFile(org.apache.commons.net.ftp.FTPFile) FileDownloadException(org.monarchinitiative.loinc2hpo.exception.FileDownloadException)

Example 3 with FileDownloadException

use of org.monarchinitiative.loinc2hpo.exception.FileDownloadException in project loinc2hpo by monarch-initiative.

the class DownloadCommand method downloadHpOntologyIfNeeded.

private void downloadHpOntologyIfNeeded() {
    File f = new File(String.format("%s%shp.obo", downloadDirectory, File.separator));
    if (f.exists()) {
        logger.trace(String.format("Cowardly refusing to download hp.obo since we found it at %s", f.getAbsolutePath()));
        return;
    }
    FileDownloader downloader = new FileDownloader();
    try {
        URL url = new URL(HP_OBO);
        logger.debug("Created url from " + HP_OBO + ": " + url.toString());
        downloader.copyURLToFile(url, new File(f.getAbsolutePath()));
    } catch (MalformedURLException e) {
        logger.error("Malformed URL for hp.obo");
        logger.error(e, e);
    } catch (FileDownloadException e) {
        logger.error("Error downloading hp.obo");
        logger.error(e, e);
    }
    logger.trace(String.format("Successfully downloaded hp.obo file at %s", f.getAbsolutePath()));
}
Also used : MalformedURLException(java.net.MalformedURLException) FileDownloader(org.monarchinitiative.loinc2hpo.io.FileDownloader) File(java.io.File) URL(java.net.URL) FileDownloadException(org.monarchinitiative.loinc2hpo.exception.FileDownloadException)

Aggregations

FileDownloadException (org.monarchinitiative.loinc2hpo.exception.FileDownloadException)3 File (java.io.File)1 MalformedURLException (java.net.MalformedURLException)1 SocketException (java.net.SocketException)1 URL (java.net.URL)1 URLConnection (java.net.URLConnection)1 FTPClient (org.apache.commons.net.ftp.FTPClient)1 FTPFile (org.apache.commons.net.ftp.FTPFile)1 FileDownloader (org.monarchinitiative.loinc2hpo.io.FileDownloader)1