Search in sources :

Example 1 with AppDownloadException

use of org.cytoscape.app.internal.exception.AppDownloadException in project cytoscape-impl by cytoscape.

the class WebQuerier method downloadApp.

/**
 * Given the unique app name used by the app store, query the app store for the
 * download URL and download the app to the given directory.
 *
 * If a file with the same name exists in the directory, it is overwritten.
 *
 * @param appName The unique app name used by the app store
 * @param version The desired version, or <code>null</code> to obtain the latest release
 * @param directory The directory used to store the downloaded file
 * @param taskMonitor
 */
public File downloadApp(WebApp webApp, String version, File directory, DownloadStatus status) throws AppDownloadException {
    List<WebApp.Release> compatibleReleases = getCompatibleReleases(webApp);
    if (compatibleReleases.size() > 0) {
        WebApp.Release releaseToDownload = null;
        if (version != null) {
            for (WebApp.Release compatibleRelease : compatibleReleases) {
                // Check if the desired version is found in the list of available versions
                if (compatibleRelease.getReleaseVersion().matches("(^\\s*|.*,)\\s*" + version + "\\s*(\\s*$|,.*)")) {
                    releaseToDownload = compatibleRelease;
                }
            }
            if (releaseToDownload == null) {
                throw new AppDownloadException("No release with the requested version " + version + " was found for the requested app " + webApp.getFullName());
            }
        } else {
            releaseToDownload = compatibleReleases.get(compatibleReleases.size() - 1);
        }
        URL downloadUrl = null;
        try {
            downloadUrl = new URL(currentAppStoreUrl + releaseToDownload.getRelativeUrl());
        } catch (MalformedURLException e) {
            throw new AppDownloadException("Unable to obtain URL for version " + version + " of the release for " + webApp.getFullName());
        }
        if (downloadUrl != null) {
            try {
                // Prepare to download
                URLConnection connection = streamUtil.getURLConnection(downloadUrl);
                InputStream inputStream = connection.getInputStream();
                long contentLength = connection.getContentLength();
                ReadableByteChannel readableByteChannel = Channels.newChannel(inputStream);
                File outputFile;
                try {
                    // Replace spaces with underscores
                    String outputFileBasename = webApp.getName().replaceAll("\\s", "_");
                    // Append version information
                    outputFileBasename += "-v" + releaseToDownload.getReleaseVersion();
                    // Strip disallowed characters
                    outputFileBasename = OUTPUT_FILENAME_DISALLOWED_CHARACTERS.matcher(outputFileBasename).replaceAll("");
                    // Append extension
                    outputFileBasename += ".jar";
                    // Output file has same name as app, but spaces and slashes are replaced with hyphens
                    outputFile = new File(directory.getCanonicalPath() + File.separator + outputFileBasename);
                    if (outputFile.exists()) {
                        outputFile.delete();
                    }
                    outputFile.createNewFile();
                    FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
                    try {
                        FileChannel fileChannel = fileOutputStream.getChannel();
                        long currentDownloadPosition = 0;
                        long bytesTransferred;
                        TaskMonitor taskMonitor = status.getTaskMonitor();
                        do {
                            bytesTransferred = fileChannel.transferFrom(readableByteChannel, currentDownloadPosition, 1 << 14);
                            if (status.isCanceled()) {
                                outputFile.delete();
                                return null;
                            }
                            currentDownloadPosition += bytesTransferred;
                            if (contentLength > 0) {
                                double progress = (double) currentDownloadPosition / contentLength;
                                taskMonitor.setProgress(progress);
                            }
                        } while (bytesTransferred > 0);
                    } finally {
                        fileOutputStream.close();
                    }
                } finally {
                    readableByteChannel.close();
                }
                return outputFile;
            } catch (IOException e) {
                throw new AppDownloadException("Error while downloading app " + webApp.getFullName() + ", " + e.getMessage());
            }
        }
    } else {
        throw new AppDownloadException("No available releases were found for the app " + webApp.getFullName() + ".");
    }
    return null;
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) MalformedURLException(java.net.MalformedURLException) Release(org.cytoscape.app.internal.net.WebApp.Release) AppDownloadException(org.cytoscape.app.internal.exception.AppDownloadException) InputStream(java.io.InputStream) FileChannel(java.nio.channels.FileChannel) IOException(java.io.IOException) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) TaskMonitor(org.cytoscape.work.TaskMonitor) FileOutputStream(java.io.FileOutputStream) File(java.io.File) Release(org.cytoscape.app.internal.net.WebApp.Release)

Aggregations

File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 URLConnection (java.net.URLConnection)1 FileChannel (java.nio.channels.FileChannel)1 ReadableByteChannel (java.nio.channels.ReadableByteChannel)1 AppDownloadException (org.cytoscape.app.internal.exception.AppDownloadException)1 Release (org.cytoscape.app.internal.net.WebApp.Release)1 TaskMonitor (org.cytoscape.work.TaskMonitor)1