From 63cc92eae364eb403453143dba352a4a4db7ea19 Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Thu, 30 Mar 2023 16:50:24 +0200 Subject: [PATCH] #1657 snapshot version number retrieval optimizations added new determineSnapshotVersionFromResource method to Nexus2SearchResponse adjusted createDownloadLink method in AbstractSearchResponse to handle snapshot versions too --- .../MavenSearchRepositoryConstants.java | 2 +- .../to/model/AbstractSearchResponse.java | 21 +++++++-- .../to/model/maven/MavenSearchResponse.java | 2 +- .../to/model/nexus2/Nexus2SearchResponse.java | 46 ++++++++++++++++--- 4 files changed, 59 insertions(+), 12 deletions(-) diff --git a/cobigen/core-artifact-retriever/src/main/java/com/devonfw/cobigen/retriever/mavensearch/constants/MavenSearchRepositoryConstants.java b/cobigen/core-artifact-retriever/src/main/java/com/devonfw/cobigen/retriever/mavensearch/constants/MavenSearchRepositoryConstants.java index 2e27d5aed..206595697 100644 --- a/cobigen/core-artifact-retriever/src/main/java/com/devonfw/cobigen/retriever/mavensearch/constants/MavenSearchRepositoryConstants.java +++ b/cobigen/core-artifact-retriever/src/main/java/com/devonfw/cobigen/retriever/mavensearch/constants/MavenSearchRepositoryConstants.java @@ -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 diff --git a/cobigen/core-artifact-retriever/src/main/java/com/devonfw/cobigen/retriever/mavensearch/to/model/AbstractSearchResponse.java b/cobigen/core-artifact-retriever/src/main/java/com/devonfw/cobigen/retriever/mavensearch/to/model/AbstractSearchResponse.java index d44a3fdd8..74da6291b 100644 --- a/cobigen/core-artifact-retriever/src/main/java/com/devonfw/cobigen/retriever/mavensearch/to/model/AbstractSearchResponse.java +++ b/cobigen/core-artifact-retriever/src/main/java/com/devonfw/cobigen/retriever/mavensearch/to/model/AbstractSearchResponse.java @@ -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); } @@ -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); @@ -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; } diff --git a/cobigen/core-artifact-retriever/src/main/java/com/devonfw/cobigen/retriever/mavensearch/to/model/maven/MavenSearchResponse.java b/cobigen/core-artifact-retriever/src/main/java/com/devonfw/cobigen/retriever/mavensearch/to/model/maven/MavenSearchResponse.java index 7f95107b6..d4112ad0f 100644 --- a/cobigen/core-artifact-retriever/src/main/java/com/devonfw/cobigen/retriever/mavensearch/to/model/maven/MavenSearchResponse.java +++ b/cobigen/core-artifact-retriever/src/main/java/com/devonfw/cobigen/retriever/mavensearch/to/model/maven/MavenSearchResponse.java @@ -68,7 +68,7 @@ public List 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, "")); } } diff --git a/cobigen/core-artifact-retriever/src/main/java/com/devonfw/cobigen/retriever/mavensearch/to/model/nexus2/Nexus2SearchResponse.java b/cobigen/core-artifact-retriever/src/main/java/com/devonfw/cobigen/retriever/mavensearch/to/model/nexus2/Nexus2SearchResponse.java index 90736ec8d..dbb8abdd1 100644 --- a/cobigen/core-artifact-retriever/src/main/java/com/devonfw/cobigen/retriever/mavensearch/to/model/nexus2/Nexus2SearchResponse.java +++ b/cobigen/core-artifact-retriever/src/main/java/com/devonfw/cobigen/retriever/mavensearch/to/model/nexus2/Nexus2SearchResponse.java @@ -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; @@ -49,17 +51,23 @@ public List 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(), "")); } } } @@ -69,6 +77,32 @@ public List 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)