public File download()

in src/main/java/org/apache/maven/shared/io/download/DefaultDownloadManager.java [77:173]


    public File download(String url, List<TransferListener> transferListeners, MessageHolder messageHolder)
            throws DownloadFailedException {
        File downloaded = (File) cache.get(url);

        if (downloaded != null && downloaded.exists()) {
            messageHolder.addMessage("Using cached download: " + downloaded.getAbsolutePath());

            return downloaded;
        }

        URL sourceUrl;
        try {
            sourceUrl = new URL(url);
        } catch (MalformedURLException e) {
            throw new DownloadFailedException(url, "Download failed due to invalid URL.", e);
        }

        Wagon wagon = null;

        // Retrieve the correct Wagon instance used to download the remote archive
        try {
            wagon = wagonManager.getWagon(sourceUrl.getProtocol());
        } catch (UnsupportedProtocolException e) {
            throw new DownloadFailedException(url, "Download failed", e);
        }

        messageHolder.addMessage("Using wagon: " + wagon + " to download: " + url);

        try {
            // create the landing file in /tmp for the downloaded source archive
            downloaded = Files.createTempFile("download-", null).toFile();

            // delete when the JVM exits, to avoid polluting the temp dir...
            downloaded.deleteOnExit();
        } catch (IOException e) {
            throw new DownloadFailedException(url, "Failed to create temporary file target for download.", e);
        }

        messageHolder.addMessage("Download target is: " + downloaded.getAbsolutePath());

        // split the download URL into base URL and remote path for connecting, then retrieving.
        String remotePath = sourceUrl.getPath();
        String baseUrl = url.substring(0, url.length() - remotePath.length());

        for (Iterator<TransferListener> it = transferListeners.iterator(); it.hasNext(); ) {
            wagon.addTransferListener(it.next());
        }

        // connect to the remote site, and retrieve the archive. Note the separate methods in which
        // base URL and remote path are used.
        Repository repo = new Repository(sourceUrl.getHost(), baseUrl);

        messageHolder.addMessage("Connecting to: " + repo.getHost() + "(baseUrl: " + repo.getUrl() + ")");

        try {
            wagon.connect(
                    repo,
                    wagonManager.getAuthenticationInfo(repo.getId()),
                    wagonManager.getProxy(sourceUrl.getProtocol()));
        } catch (ConnectionException e) {
            throw new DownloadFailedException(url, "Download failed", e);
        } catch (AuthenticationException e) {
            throw new DownloadFailedException(url, "Download failed", e);
        }

        messageHolder.addMessage("Getting: " + remotePath);

        try {
            wagon.get(remotePath, downloaded);

            // cache this for later download requests to the same instance...
            cache.put(url, downloaded);

            return downloaded;
        } catch (TransferFailedException e) {
            throw new DownloadFailedException(url, "Download failed", e);
        } catch (ResourceDoesNotExistException e) {
            throw new DownloadFailedException(url, "Download failed", e);
        } catch (AuthorizationException e) {
            throw new DownloadFailedException(url, "Download failed", e);
        } finally {
            // ensure the Wagon instance is closed out properly.
            if (wagon != null) {
                try {
                    messageHolder.addMessage("Disconnecting.");

                    wagon.disconnect();
                } catch (ConnectionException e) {
                    messageHolder.addMessage("Failed to disconnect wagon for: " + url, e);
                }

                for (Iterator<TransferListener> it = transferListeners.iterator(); it.hasNext(); ) {
                    wagon.removeTransferListener(it.next());
                }
            }
        }
    }