Example #1
0
        public void DownloadPackage(
            string packageId,
            IVersion version,
            string feedId,
            Uri feedUri,
            ICredentials feedCredentials,
            bool forcePackageDownload,
            int maxDownloadAttempts,
            TimeSpan downloadAttemptBackoff,
            out string downloadedTo,
            out string hash,
            out long size)
        {
            var cacheDirectory = PackageDownloaderUtils.GetPackageRoot(feedId);

            downloadedTo = null;
            if (!forcePackageDownload)
            {
                Log.Info("Attempting to get from cache");
                try
                {
                    downloadedTo = SourceFromCache(
                        packageId,
                        version,
                        cacheDirectory);
                }
                catch (Exception ex)
                {
                    Log.Info("SourceFromCache() failed");
                    Log.Info("Exception starts");
                    Log.Info(ex.ToString());
                    Log.Info(ex.StackTrace);
                    Log.Info("Exception ends");
                }
            }

            if (downloadedTo == null)
            {
                downloadedTo = DownloadPackage(
                    packageId,
                    version,
                    feedUri,
                    feedCredentials,
                    cacheDirectory,
                    maxDownloadAttempts,
                    downloadAttemptBackoff);
            }
            else
            {
                Log.VerboseFormat("Package was found in cache. No need to download. Using file: '{0}'", downloadedTo);
            }

            size = fileSystem.GetFileSize(downloadedTo);
            hash = downloadedTo
                   .Map(path => FunctionalExtensions.Using(
                            () => fileSystem.OpenFile(path, FileAccess.Read),
                            stream => HashCalculator.Hash(stream)));
        }
Example #2
0
        /// <summary>
        /// Actually download the maven file.
        /// </summary>
        /// <returns>The path to the downloaded file</returns>
        string DownloadArtifact(
            MavenPackageID mavenGavFirst,
            string packageId,
            IVersion version,
            Uri feedUri,
            ICredentials feedCredentials,
            string cacheDirectory,
            int maxDownloadAttempts,
            TimeSpan downloadAttemptBackoff,
            XmlDocument snapshotMetadata)
        {
            Guard.NotNull(mavenGavFirst, "mavenGavFirst can not be null");
            Guard.NotNullOrWhiteSpace(packageId, "packageId can not be null");
            Guard.NotNull(version, "version can not be null");
            Guard.NotNullOrWhiteSpace(cacheDirectory, "cacheDirectory can not be null");
            Guard.NotNull(feedUri, "feedUri can not be null");

            for (var retry = 0; retry < maxDownloadAttempts; ++retry)
            {
                try
                {
                    return(GetFilePathToDownloadPackageTo(
                               cacheDirectory,
                               packageId,
                               version.ToString(),
                               mavenGavFirst.Packaging)
                           .Tee(path => feedUri.ToString().TrimEnd('/')
                                .Map(uri => uri + (snapshotMetadata == null ?
                                                   mavenGavFirst.DefaultArtifactPath :
                                                   mavenGavFirst.SnapshotArtifactPath(MetadataParser.GetLatestSnapshotRelease(
                                                                                          snapshotMetadata,
                                                                                          mavenGavFirst.Packaging,
                                                                                          mavenGavFirst.Version))))
                                .Map(uri => FunctionalExtensions.Using(
                                         () => new WebClient(),
                                         client => client
                                         .Tee(c => c.Credentials = feedCredentials)
                                         .Tee(c => c.DownloadFile(uri, path))))
                                ));
                }
                catch
                {
                    Thread.Sleep(downloadAttemptBackoff);
                }
            }

            throw new MavenDownloadException("Failed to download the Maven artifact");
        }
Example #3
0
        /// <summary>
        /// Attempt to get the snapshot maven-metadata.xml file, which we will need to use to build up
        /// the filenames of snapshot versions.
        /// </summary>
        /// <returns>The snapshot maven-metadata.xml file if it exists, and a null result otherwise</returns>
        XmlDocument GetSnapshotMetadata(
            MavenPackageID mavenPackageID,
            Uri feedUri,
            ICredentials feedCredentials,
            int maxDownloadAttempts,
            TimeSpan downloadAttemptBackoff)
        {
            for (var retry = 0; retry < maxDownloadAttempts; ++retry)
            {
                try
                {
                    var metadataResponse = (feedUri.ToString().TrimEnd('/') + mavenPackageID.GroupVersionMetadataPath)
                                           .ToEnumerable()
                                           .Select(uri => WebRequest.Create(uri).Tee(request => request.Credentials = feedCredentials))
                                           .Select(request => request.GetResponse())
                                           .Select(response => response as HttpWebResponse)
                                           .First(response => response.IsSuccessStatusCode() || (int)response.StatusCode == 404);

                    if (metadataResponse.IsSuccessStatusCode())
                    {
                        return(FunctionalExtensions.Using(
                                   () => metadataResponse.GetResponseStream(),
                                   stream => new XmlDocument().Tee(doc => doc.Load(stream))));
                    }

                    return(null);
                }
                catch (WebException ex)
                {
                    if (ex.Response is HttpWebResponse response)
                    {
                        if ((int)(response.StatusCode) == 404)
                        {
                            return(null);
                        }
                    }

                    Thread.Sleep(downloadAttemptBackoff);
                }
                catch
                {
                    Thread.Sleep(downloadAttemptBackoff);
                }
            }

            throw new MavenDownloadException("Failed to download the Maven artifact");
        }