/// <summary> /// Performs the actual HTTP head request to check for the presence of a maven artifact with a /// given extension. /// </summary> /// <returns>true if the package exists, and false otherwise</returns> bool MavenPackageExists(MavenPackageID mavenGavParser, Uri feedUri, ICredentials feedCredentials, XmlDocument snapshotMetadata) { return(feedUri.ToString().TrimEnd('/') .Map(uri => uri + (snapshotMetadata == null ? mavenGavParser.DefaultArtifactPath : mavenGavParser.SnapshotArtifactPath(MetadataParser.GetLatestSnapshotRelease( snapshotMetadata, mavenGavParser.Packaging, mavenGavParser.Version)))) .Map(uri => { try { return WebRequest.Create(uri) .Tee(c => c.Method = "HEAD") .Tee(c => c.Credentials = feedCredentials) .GetResponse() .Map(response => response as HttpWebResponse) .Map(response => (int)response.StatusCode >= 200 && (int)response.StatusCode <= 299); } catch { return false; } })); }
/// <summary> /// Performs the actual HTTP head request to check for the presence of a maven artifact with a /// given extension. /// </summary> /// <returns>true if the package exists, and false otherwise</returns> bool MavenPackageExists(MavenPackageID mavenGavParser, Uri feedUri, ICredentials feedCredentials, XmlDocument snapshotMetadata) { var uri = feedUri.ToString().TrimEnd('/') + (snapshotMetadata == null ? mavenGavParser.DefaultArtifactPath : mavenGavParser.SnapshotArtifactPath( GetLatestSnapshotRelease( snapshotMetadata, mavenGavParser.Packaging, mavenGavParser.Version))); try { var req = WebRequest.Create(uri); req.Method = "HEAD"; req.Credentials = feedCredentials; using (var response = (HttpWebResponse)req.GetResponse()) { return((int)response.StatusCode >= 200 && (int)response.StatusCode <= 299); } } catch { return(false); } }
/// <summary> /// Actually download the maven file. /// </summary> /// <returns>The path to the downloaded file</returns> PackagePhysicalFileMetadata 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"); var localDownloadName = Path.Combine(cacheDirectory, PackageName.ToCachedFileName(packageId, version, "." + mavenGavFirst.Packaging)); var downloadUrl = feedUri.ToString().TrimEnd('/') + (snapshotMetadata == null ? mavenGavFirst.DefaultArtifactPath : mavenGavFirst.SnapshotArtifactPath(GetLatestSnapshotRelease( snapshotMetadata, mavenGavFirst.Packaging, mavenGavFirst.Classifier, mavenGavFirst.Version))); for (var retry = 0; retry < maxDownloadAttempts; ++retry) { try { Log.Verbose($"Downloading Attempt {downloadUrl} TO {localDownloadName}"); using (var client = new WebClient { Credentials = feedCredentials }) { client.DownloadFile(downloadUrl, localDownloadName); var packagePhysicalFileMetadata = PackagePhysicalFileMetadata.Build(localDownloadName); return(packagePhysicalFileMetadata ?? throw new CommandException($"Unable to retrieve metadata for package {packageId}, version {version}")); } } catch (Exception ex) { if ((retry + 1) == maxDownloadAttempts) { throw new MavenDownloadException("Failed to download the Maven artifact.\r\nLast Exception Message: " + ex.Message); } Thread.Sleep(downloadAttemptBackoff); } } throw new MavenDownloadException("Failed to download the Maven artifact"); }
/// <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"); }