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;
}
Aggregations