Skip to content

Commit

Permalink
#1657 snapshot version number retrieval optimizations added new deter…
Browse files Browse the repository at this point in the history
…mineSnapshotVersionFromResource method to Nexus2SearchResponse adjusted createDownloadLink method in AbstractSearchResponse to handle snapshot versions too
  • Loading branch information
jan-vcapgemini committed Jul 19, 2023
1 parent a3d03f3 commit 63cc92e
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public class MavenSearchRepositoryConstants {
/**
* Nexus2 SNAPSHOT repository link
*/
public static final String NEXUS2_SNAPSHOT_REPOSITORY_LINK = "service/local/artifact/maven/redirect";
public static final String NEXUS2_SNAPSHOT_REPOSITORY_LINK = "service/local/repositories/snapshots/content";

/**
* Nexus2 REST search API path
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ public static String retrieveJsonResponseWithAuthentication(String targetLink,
builder.writeTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS);
builder.retryOnConnectionFailure(true);

if (serverCredentials.getProxyAddress() != null && serverCredentials.getProxyPort() != 0) {
if (serverCredentials != null && serverCredentials.getProxyAddress() != null
&& serverCredentials.getProxyPort() != 0) {
buildProxy(serverCredentials, builder);
}

Expand All @@ -156,7 +157,8 @@ public static String retrieveJsonResponseWithAuthentication(String targetLink,

boolean usingBasicAuth = false;
// use basic authentication
if (serverCredentials.getUsername() != null && serverCredentials.getPassword() != null) {
if (serverCredentials != null && serverCredentials.getUsername() != null
&& serverCredentials.getPassword() != null) {
LOG.debug("Connecting to REST API using Basic Authentication.");
requestWithHeaders = basicUsernamePasswordAuthentication(targetLink, serverCredentials.getUsername(),
serverCredentials.getPassword(), requestWithHeaders);
Expand Down Expand Up @@ -239,22 +241,33 @@ public Request authenticate(Route route, Response response) throws IOException {
}

/**
* Creates a download link (concatenates maven repository link with groupId, artifact and version)
* Creates a download link (concatenates maven repository link with groupId, artifact and version).
*
* Can handle snapshot versions if provided
*
* @param mavenRepo link to the maven repository to use
* @param groupId for the download link
* @param artifactId for the download link
* @param version for the download link
* @param fileEnding file ending for the download link
* @param snapshotVersion String of snapshot version number
* @return concatenated download link
* @throws MalformedURLException if the URL was not valid
*/
protected static URL createDownloadLink(String mavenRepo, String groupId, String artifactId, String version,
String fileEnding) throws MalformedURLException {
String fileEnding, String snapshotVersion) throws MalformedURLException {

String parsedGroupId = groupId.replace(".", "/");
String downloadFile = artifactId + "-" + version + fileEnding;

// replace version with snapshot version
if (!snapshotVersion.isEmpty()) {
String versionWithoutSnapShot = version.replace("-SNAPSHOT", "");
downloadFile = artifactId + "-" + versionWithoutSnapShot + "-" + snapshotVersion + fileEnding;
}

String downloadLink = mavenRepo + "/" + parsedGroupId + "/" + artifactId + "/" + version + "/" + downloadFile;

URL url = new URL(downloadLink);
return url;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public List<URL> retrieveTemplateSetXmlDownloadURLs() throws MalformedURLExcepti
if (fileEnding.endsWith(ConfigurationConstants.TEMPLATE_SET_CONFIG_FILENAME)) {
downloadLinks.add(
AbstractSearchResponse.createDownloadLink(MavenSearchRepositoryConstants.MAVEN_REPOSITORY_DOWNLOAD_LINK,
doc.getGroup(), doc.getArtifact(), doc.getLatestVersion(), fileEnding));
doc.getGroup(), doc.getArtifact(), doc.getLatestVersion(), fileEnding, ""));
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -49,17 +51,23 @@ public List<URL> retrieveTemplateSetXmlDownloadURLs() throws MalformedURLExcepti
if (artifactLink.getClassifier() != null && artifactLink.getClassifier().equals("template-set")) {
// Check for full SNAPSHOT version link
if (item.getVersion().contains("-SNAPSHOT")) {
URL snapShotUrl = new URL(MavenSearchRepositoryConstants.NEXUS2_REPOSITORY_URL + "/"
+ MavenSearchRepositoryConstants.NEXUS2_SNAPSHOT_REPOSITORY_LINK + "?r=snapshots" + "&g="
+ item.getGroupId() + "&a=" + item.getArtifactId() + "&v=" + item.getVersion() + "&e="
+ artifactLink.getExtension() + "&c=" + artifactLink.getClassifier());
downloadLinks.add(snapShotUrl);

String fullSnapshotVersion = determineSnapshotVersionFromResource(item.getGroupId(), item.getArtifactId(),
item.getVersion());

URL snapshotUrl = AbstractSearchResponse.createDownloadLink(
MavenSearchRepositoryConstants.NEXUS2_REPOSITORY_URL + "/"
+ MavenSearchRepositoryConstants.NEXUS2_SNAPSHOT_REPOSITORY_LINK,
item.getGroupId(), item.getArtifactId(), item.getVersion(),
"-" + artifactLink.getClassifier() + "." + artifactLink.getExtension(), fullSnapshotVersion);

downloadLinks.add(snapshotUrl);
} else {
downloadLinks.add(AbstractSearchResponse.createDownloadLink(
MavenSearchRepositoryConstants.NEXUS2_REPOSITORY_URL + "/"
+ MavenSearchRepositoryConstants.NEXUS2_REPOSITORY_LINK,
item.getGroupId(), item.getArtifactId(), item.getVersion(),
"-" + artifactLink.getClassifier() + "." + artifactLink.getExtension()));
"-" + artifactLink.getClassifier() + "." + artifactLink.getExtension(), ""));
}
}
}
Expand All @@ -69,6 +77,32 @@ public List<URL> retrieveTemplateSetXmlDownloadURLs() throws MalformedURLExcepti
return removeDuplicatedDownloadURLs(downloadLinks);
}

/**
* Determines the full snapshot version number from resource xml
*
* @param groupId String of the group ID
* @param artifactId String of the artifact ID
* @param version String of the version
* @return String of full snapshot version number
*/
@JsonIgnore
public String determineSnapshotVersionFromResource(String groupId, String artifactId, String version) {

String parsedGroupId = groupId.replace(".", "/");
String snapshotUrl = MavenSearchRepositoryConstants.NEXUS2_REPOSITORY_URL + "/"
+ MavenSearchRepositoryConstants.NEXUS2_SNAPSHOT_REPOSITORY_LINK + "/" + parsedGroupId + "/" + artifactId + "/"
+ version;
String response = AbstractSearchResponse.retrieveJsonResponseWithAuthentication(snapshotUrl, getRepositoryType(),
null);
Pattern pattern = Pattern.compile("(\\d+\\.)?(\\d+\\.)?(\\*|\\d+)-(\\d+)-template-set\\.xml");
Matcher matcher = pattern.matcher(response);
String versionNumber = "";
if (matcher.find()) {
versionNumber = matcher.group();
}
return versionNumber.replace("-template-set.xml", "");
}

@Override
@JsonIgnore
public String retrieveJsonResponse(ServerCredentials serverCredentials, String groupId)
Expand Down

0 comments on commit 63cc92e

Please sign in to comment.