From e6f872df58e90b1bfe725d3714a1e1930001cb86 Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Thu, 21 Jul 2022 20:51:03 +0200 Subject: [PATCH 01/42] #1529 added first implementation of extensible artifact search REST API added new getMavenArtifactsByGroupId to MavenUtils which takes a repository type (e.g. maven, nexus, jfrog) and a groupId to search for added new SearchResponse tos to core-api utils added new MavenUtilTest class (tests for proper json string parsing and REST API requests) and resources --- .../devonfw/cobigen/api/util/MavenUtil.java | 24 +++ .../util/to/AbstractRESTSearchResponse.java | 111 ++++++++++ .../api/util/to/JfrogSearchResponse.java | 84 ++++++++ .../api/util/to/MavenSearchResponse.java | 190 ++++++++++++++++++ .../api/util/to/NexusSearchResponse.java | 101 ++++++++++ .../devonfw/cobigen/api/MavenUtilTest.java | 148 ++++++++++++++ .../unittest/MavenUtilTest/jfrogJsonTest.json | 9 + .../unittest/MavenUtilTest/mavenJsonTest.json | 118 +++++++++++ .../unittest/MavenUtilTest/nexusJsonTest.json | 42 ++++ 9 files changed, 827 insertions(+) create mode 100644 cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractRESTSearchResponse.java create mode 100644 cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/JfrogSearchResponse.java create mode 100644 cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/MavenSearchResponse.java create mode 100644 cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/NexusSearchResponse.java create mode 100644 cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java create mode 100644 cobigen/cobigen-core-api/src/test/resources/testdata/unittest/MavenUtilTest/jfrogJsonTest.json create mode 100644 cobigen/cobigen-core-api/src/test/resources/testdata/unittest/MavenUtilTest/mavenJsonTest.json create mode 100644 cobigen/cobigen-core-api/src/test/resources/testdata/unittest/MavenUtilTest/nexusJsonTest.json diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/MavenUtil.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/MavenUtil.java index a50d684117..c6bbf2db77 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/MavenUtil.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/MavenUtil.java @@ -8,6 +8,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.concurrent.ExecutionException; @@ -27,6 +28,7 @@ import com.devonfw.cobigen.api.constants.MavenConstants; import com.devonfw.cobigen.api.exception.CobiGenRuntimeException; +import com.devonfw.cobigen.api.util.to.AbstractRESTSearchResponse; import com.google.common.collect.Lists; import com.google.common.hash.Hashing; import com.google.common.io.ByteSource; @@ -340,4 +342,26 @@ public static Path getProjectRoot(Path inputFile, boolean topLevel) { LOG.debug("Project root could not be found."); return null; } + + /** + * Gets a list of download URLs by groupId from the specified repository search REST API + * + * @param repositoryType String of the type of the repository e.g. maven, jfrog, nexus + * @param groupId the groupId to search for + * + * @return List of artifact URLS + */ + public static List getMavenArtifactsByGroupId(String repositoryType, String groupId) { + + List artifactList = new ArrayList<>(); + + try { + artifactList = AbstractRESTSearchResponse.getArtifactDownloadLinks(repositoryType, groupId); + return artifactList; + } catch (IOException e) { + throw new CobiGenRuntimeException("Unable to get artifacts from " + repositoryType + " by groupId " + groupId, e); + } + + } + } diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractRESTSearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractRESTSearchResponse.java new file mode 100644 index 0000000000..803081c9d8 --- /dev/null +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractRESTSearchResponse.java @@ -0,0 +1,111 @@ +package com.devonfw.cobigen.api.util.to; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; + +import com.fasterxml.jackson.databind.ObjectMapper; + +import jakarta.ws.rs.client.Client; +import jakarta.ws.rs.client.ClientBuilder; +import jakarta.ws.rs.client.Invocation; +import jakarta.ws.rs.client.WebTarget; +import jakarta.ws.rs.core.MediaType; +import jakarta.ws.rs.core.Response; + +/** + * Handles the responses from various search REST API's + * + */ +public interface AbstractRESTSearchResponse { + + /** + * Creates a list of download links + * + * @return List of download links + * @throws MalformedURLException if an URL was not valid + */ + public List getDownloadURLs() throws MalformedURLException; + + /** + * Gets the json response + * + * @param repositoryUrl URL of the repository + * @param groupId to search for + * @return String of json response + * @throws IOException if the request did not return status 200 + */ + public String getJsonResponse(String repositoryUrl, String groupId) throws IOException; + + /** + * Gets the download links by given repository type + * + * @param repositoryType String of the type of the repository e.g. maven, jfrog, nexus + * @param groupId the groupId to search for + * + * @return List of download links + * @throws IOException if an error occurred + * + */ + public static List getArtifactDownloadLinks(String repositoryType, String groupId) throws IOException { + + List downloadLinks = new ArrayList<>(); + ObjectMapper mapper = new ObjectMapper(); + String jsonResponse = ""; + + if (repositoryType.equals("maven")) { + MavenSearchResponse response = new MavenSearchResponse(); + String mavenRepositoryURL = "https://search.maven.org"; + jsonResponse = response.getJsonResponse(mavenRepositoryURL, groupId); + response = mapper.readValue(jsonResponse, MavenSearchResponse.class); + downloadLinks = response.getDownloadURLs(); + return downloadLinks; + } + + if (repositoryType.equals("jfrog")) { + JfrogSearchResponse response = new JfrogSearchResponse(); + String jfrogRepositoryURL = ""; + jsonResponse = response.getJsonResponse(jfrogRepositoryURL, groupId); + response = mapper.readValue(jsonResponse, JfrogSearchResponse.class); + downloadLinks = response.getDownloadURLs(); + return downloadLinks; + } + + if (repositoryType.equals("nexus")) { + NexusSearchResponse response = new NexusSearchResponse(); + String nexusRepositoryURL = ""; + jsonResponse = response.getJsonResponse(nexusRepositoryURL, groupId); + response = mapper.readValue(jsonResponse, NexusSearchResponse.class); + downloadLinks = response.getDownloadURLs(); + return downloadLinks; + } + + return null; + } + + /** + * Gets a json response by given REST API target link + * + * @param targetLink link to get response from + * @return String of json response + * @throws IOException if the returned status code was not 200 OK + */ + public static String getJsonResponseStringByTargetLink(String targetLink) throws IOException { + + Client client = ClientBuilder.newClient(); + WebTarget target = client.target(targetLink); + Invocation.Builder request = target.request(MediaType.APPLICATION_JSON); + Response response = request.get(); + int status = response.getStatus(); + String jsonResponse = ""; + if (status == 200) { + jsonResponse = response.readEntity(String.class); + } else { + throw new IOException(String.valueOf(status)); + } + return jsonResponse; + } + +} \ No newline at end of file diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/JfrogSearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/JfrogSearchResponse.java new file mode 100644 index 0000000000..ec4b349ff8 --- /dev/null +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/JfrogSearchResponse.java @@ -0,0 +1,84 @@ +package com.devonfw.cobigen.api.util.to; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonProperty; + +class Result { + + @JsonProperty("uri") + private String uri; + + /** + * @return uri + */ + @JsonIgnore + public String getUri() { + + return this.uri; + } + +} + +/** + * Json model for jfrog Search REST API response + * + */ +public class JfrogSearchResponse implements AbstractRESTSearchResponse { + + /** Logger instance. */ + @JsonIgnore + private static final Logger LOG = LoggerFactory.getLogger(JfrogSearchResponse.class); + + @JsonProperty("results") + private List results; + + /** + * @return results + */ + @JsonIgnore + public List getResults() { + + return this.results; + } + + @Override + @JsonIgnore + public String getJsonResponse(String repositoryUrl, String groupId) throws IOException { + + String targetLink = repositoryUrl + "/" + "solrsearch/select?q=g:" + groupId + "&wt=json"; + LOG.info("Starting Jfrog Search REST API request with URL: {}.", targetLink); + + String jsonResponse; + + try { + jsonResponse = AbstractRESTSearchResponse.getJsonResponseStringByTargetLink(targetLink); + } catch (IOException e) { + LOG.error("Jfrog Search REST API request was not successful, status code was: {}!", e.getMessage()); + throw new IOException(); + } + + return jsonResponse; + } + + @Override + @JsonIgnore + public List getDownloadURLs() throws MalformedURLException { + + List downloadLinks = new ArrayList<>(); + + for (Result result : getResults()) { + downloadLinks.add(new URL(result.getUri())); + } + + return downloadLinks; + } +} diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/MavenSearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/MavenSearchResponse.java new file mode 100644 index 0000000000..1d25783b42 --- /dev/null +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/MavenSearchResponse.java @@ -0,0 +1,190 @@ +package com.devonfw.cobigen.api.util.to; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; + +@JsonIgnoreProperties(value = { "p", "timestamp", "versionCount", "text", "ec" }) +class Doc { + @JsonProperty("id") + private String id; + + @JsonProperty("g") + private String group; + + @JsonProperty("a") + private String artifact; + + @JsonProperty("latestVersion") + private String latestVersion; + + @JsonProperty("repositoryId") + private String repositoryId; + + /** + * @return id + */ + @JsonIgnore + public String getId() { + + return this.id; + } + + /** + * @return group + */ + @JsonIgnore + public String getGroup() { + + return this.group; + } + + /** + * @return artifact + */ + @JsonIgnore + public String getArtifact() { + + return this.artifact; + } + + /** + * @return latestVersion + */ + @JsonIgnore + public String getLatestVersion() { + + return this.latestVersion; + } + + /** + * @return repositoryId + */ + @JsonIgnore + public String getRepositoryId() { + + return this.repositoryId; + } + + /** + * Creates a download link (concatenates maven repository link with groupId, artifact and version) + * + * @param mavenRepo link to the maven repository to use + * @return concatenated download link + * @throws MalformedURLException if the URL was not valid + */ + @JsonIgnore + public URL createDownloadLink(String mavenRepo) throws MalformedURLException { + + String parsedGroupId = getGroup().replace(".", "/"); + String downloadFile = getArtifact() + "-" + getLatestVersion() + ".jar"; + String downloadLink = mavenRepo + "/" + parsedGroupId + "/" + getArtifact() + "/" + getLatestVersion() + "/" + + downloadFile; + URL url = new URL(downloadLink); + return url; + } + +} + +@JsonIgnoreProperties(value = { "start" }) +class MavenResponse { + + @JsonProperty("numFound") + private int numFound; + + @JsonProperty("docs") + private List docs; + + /** + * @return numFound + */ + @JsonIgnore + public int getNumFound() { + + return this.numFound; + } + + /** + * @return docs + */ + @JsonIgnore + public List getDocs() { + + return this.docs; + } + +} + +/** + * Json model for maven Search REST API response + * + */ +@JsonIgnoreProperties(value = { "responseHeader", "spellcheck" }) +public class MavenSearchResponse implements AbstractRESTSearchResponse { + + /** Logger instance. */ + @JsonIgnore + private static final Logger LOG = LoggerFactory.getLogger(MavenSearchResponse.class); + + @JsonProperty("response") + private MavenResponse response; + + /** + * @return response + */ + @JsonIgnore + public MavenResponse getResponse() { + + return this.response; + } + + @Override + @JsonIgnore + public String getJsonResponse(String repositoryUrl, String groupId) throws IOException { + + String targetLink = repositoryUrl + "/" + "solrsearch/select?q=g:" + groupId + "&wt=json"; + LOG.info("Starting Maven Search REST API request with URL: {}.", targetLink); + + int limitRows = 20; + if (limitRows > 0) { + targetLink += "&rows=" + limitRows; + LOG.info("Limiting Maven Search REST API request to: {} rows.", limitRows); + } + + String jsonResponse; + + try { + jsonResponse = AbstractRESTSearchResponse.getJsonResponseStringByTargetLink(targetLink); + } catch (IOException e) { + LOG.error("Maven Search REST API request was not successful, status code was: {}!", e.getMessage()); + throw new IOException(); + } + + return jsonResponse; + + } + + @Override + @JsonIgnore + public List getDownloadURLs() throws MalformedURLException { + + List downloadLinks = new ArrayList<>(); + List docs = getResponse().getDocs(); + + for (Doc doc : docs) { + downloadLinks.add(doc.createDownloadLink("https://repo1.maven.org/maven2")); + } + + return downloadLinks; + } + +} diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/NexusSearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/NexusSearchResponse.java new file mode 100644 index 0000000000..f592f4c04d --- /dev/null +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/NexusSearchResponse.java @@ -0,0 +1,101 @@ +package com.devonfw.cobigen.api.util.to; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; + +@JsonIgnoreProperties(value = { "id", "format", "checksum" }) +class Asset { + @JsonProperty("downloadUrl") + public String downloadUrl; + + @JsonProperty("path") + public String path; + + @JsonProperty("repository") + public String repository; + + @JsonProperty("latestVersion") + public String latestVersion; + + @JsonProperty("repositoryId") + public String repositoryId; +} + +@JsonIgnoreProperties(value = { "id", "format" }) +class Item { + + @JsonProperty("repository") + private String repository; + + @JsonProperty("group") + private String group; + + @JsonProperty("name") + private String name; + + @JsonProperty("version") + private String version; + + @JsonProperty("assets") + public List assets; +} + +/** + * Json model for nexus Search REST API response + * + */ +@JsonIgnoreProperties(value = { "continuationToken" }) +public class NexusSearchResponse implements AbstractRESTSearchResponse { + + /** Logger instance. */ + @JsonIgnore + private static final Logger LOG = LoggerFactory.getLogger(NexusSearchResponse.class); + + @JsonProperty("items") + private List items; + + @Override + @JsonIgnore + public List getDownloadURLs() throws MalformedURLException { + + List downloadLinks = new ArrayList<>(); + + for (Item item : this.items) { + for (Asset asset : item.assets) { + downloadLinks.add(new URL(asset.downloadUrl)); + } + + } + + return downloadLinks; + } + + @Override + @JsonIgnore + public String getJsonResponse(String repositoryUrl, String groupId) throws IOException { + + String targetLink = repositoryUrl + "/" + "service/rest/v1/search?repository=maven-central&group=" + groupId; + LOG.info("Starting Nexus Search REST API request with URL: {}.", targetLink); + + String jsonResponse; + + try { + jsonResponse = AbstractRESTSearchResponse.getJsonResponseStringByTargetLink(targetLink); + } catch (IOException e) { + LOG.error("Nexus Search REST API request was not successful, status code was: {}!", e.getMessage()); + throw new IOException(); + } + + return jsonResponse; + } +} diff --git a/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java b/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java new file mode 100644 index 0000000000..059bffb977 --- /dev/null +++ b/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java @@ -0,0 +1,148 @@ +package com.devonfw.cobigen.api; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.IOException; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.List; + +import org.junit.Test; + +import com.devonfw.cobigen.api.util.MavenUtil; +import com.devonfw.cobigen.api.util.to.JfrogSearchResponse; +import com.devonfw.cobigen.api.util.to.MavenSearchResponse; +import com.devonfw.cobigen.api.util.to.NexusSearchResponse; +import com.fasterxml.jackson.databind.ObjectMapper; + +/** + * Test class for maven utilities + */ +public class MavenUtilTest { + + /** Testdata root path */ + private static final String testdataRoot = "src/test/resources/testdata/unittest/MavenUtilTest"; + + /** + * Tests if maven json response can properly be parsed and converted to a list of download URLs + * + * @throws IOException + */ + @Test + public void testMavenParseDownloadLinks() throws IOException { + + ObjectMapper mapper = new ObjectMapper(); + MavenSearchResponse response = new MavenSearchResponse(); + + String jsonResponse = new String(Files.readAllBytes(Paths.get(testdataRoot).resolve("mavenJsonTest.json"))); + + response = mapper.readValue(jsonResponse, MavenSearchResponse.class); + List downloadLinks = response.getDownloadURLs(); + assertThat(downloadLinks).contains( + new URL("https://repo1.maven.org/maven2/com/google/inject/guice/5.1.0/guice-5.1.0.jar"), + new URL("https://repo1.maven.org/maven2/com/google/inject/guice-bom/5.1.0/guice-bom-5.1.0.jar"), + new URL("https://repo1.maven.org/maven2/com/google/inject/guice-parent/5.1.0/guice-parent-5.1.0.jar"), + new URL("https://repo1.maven.org/maven2/com/google/inject/jdk8-tests/5.0.1/jdk8-tests-5.0.1.jar")); + } + + /** + * Tests if nexus json response can properly be parsed and converted to a list of download URLs + * + * @throws IOException + */ + @Test + public void testNexusParseDownloadLinks() throws IOException { + + ObjectMapper mapper = new ObjectMapper(); + NexusSearchResponse response = new NexusSearchResponse(); + + String jsonResponse = new String(Files.readAllBytes(Paths.get(testdataRoot).resolve("nexusJsonTest.json"))); + + response = mapper.readValue(jsonResponse, NexusSearchResponse.class); + List downloadLinks = response.getDownloadURLs(); + assertThat(downloadLinks).contains(new URL( + "http://localhost:8081/repository/maven-central/org/osgi/org.osgi.core/4.3.1/org.osgi.core-4.3.1-sources.jar"), + new URL("http://localhost:8081/repository/maven-central/org/osgi/org.osgi.core/4.3.1/org.osgi.core-4.3.1.jar"), + new URL("http://localhost:8081/repository/maven-central/org/osgi/org.osgi.core/4.3.1/org.osgi.core-4.3.1.pom")); + } + + /** + * Tests if jfrog json response can properly be parsed and converted to a list of download URLs + * + * @throws IOException + */ + @Test + public void testJfrogParseDownloadLinks() throws IOException { + + ObjectMapper mapper = new ObjectMapper(); + JfrogSearchResponse response = new JfrogSearchResponse(); + + String jsonResponse = new String(Files.readAllBytes(Paths.get(testdataRoot).resolve("jfrogJsonTest.json"))); + + response = mapper.readValue(jsonResponse, JfrogSearchResponse.class); + List downloadLinks = response.getDownloadURLs(); + assertThat(downloadLinks).contains(new URL( + "http://localhost:8081/artifactory/api/storage/libs-release-local/org/acme/artifact/1.0/artifact-1.0-sources.jar"), + new URL( + "http://localhost:8081/artifactory/api/storage/libs-release-local/org/acme/artifactB/1.0/artifactB-1.0-sources.jar")); + } + + /** + * Tests if a request to maven search REST API returns a list of download URLs + * + * @throws IOException + */ + @Test + public void testMavenSearchRequestGetsValidDownloadLinks() throws IOException { + + List downloadList; + + downloadList = MavenUtil.getMavenArtifactsByGroupId("maven", "com.google.inject"); + + assertThat(downloadList).contains( + new URL("https://repo1.maven.org/maven2/com/google/inject/guice/5.1.0/guice-5.1.0.jar"), + new URL("https://repo1.maven.org/maven2/com/google/inject/guice-bom/5.1.0/guice-bom-5.1.0.jar"), + new URL("https://repo1.maven.org/maven2/com/google/inject/guice-parent/5.1.0/guice-parent-5.1.0.jar"), + new URL("https://repo1.maven.org/maven2/com/google/inject/jdk8-tests/5.0.1/jdk8-tests-5.0.1.jar")); + } + + /** + * Tests if a request to nexus search REST API returns a list of download URLs + * + * @throws IOException + */ + @Test + public void testNexusSearchRequestGetsValidDownloadLinks() throws IOException { + + List downloadList; + + downloadList = MavenUtil.getMavenArtifactsByGroupId("nexus", "com.google.inject"); + + assertThat(downloadList).contains( + new URL("https://repo1.maven.org/maven2/com/google/inject/guice/5.1.0/guice-5.1.0.jar"), + new URL("https://repo1.maven.org/maven2/com/google/inject/guice-bom/5.1.0/guice-bom-5.1.0.jar"), + new URL("https://repo1.maven.org/maven2/com/google/inject/guice-parent/5.1.0/guice-parent-5.1.0.jar"), + new URL("https://repo1.maven.org/maven2/com/google/inject/jdk8-tests/5.0.1/jdk8-tests-5.0.1.jar")); + } + + /** + * Tests if a request to nexus search REST API returns a list of download URLs + * + * @throws IOException + */ + @Test + public void testJfrogSearchRequestGetsValidDownloadLinks() throws IOException { + + List downloadList; + + downloadList = MavenUtil.getMavenArtifactsByGroupId("jfrog", "com.google.inject"); + + assertThat(downloadList).contains( + new URL("https://repo1.maven.org/maven2/com/google/inject/guice/5.1.0/guice-5.1.0.jar"), + new URL("https://repo1.maven.org/maven2/com/google/inject/guice-bom/5.1.0/guice-bom-5.1.0.jar"), + new URL("https://repo1.maven.org/maven2/com/google/inject/guice-parent/5.1.0/guice-parent-5.1.0.jar"), + new URL("https://repo1.maven.org/maven2/com/google/inject/jdk8-tests/5.0.1/jdk8-tests-5.0.1.jar")); + } + +} diff --git a/cobigen/cobigen-core-api/src/test/resources/testdata/unittest/MavenUtilTest/jfrogJsonTest.json b/cobigen/cobigen-core-api/src/test/resources/testdata/unittest/MavenUtilTest/jfrogJsonTest.json new file mode 100644 index 0000000000..d208ba0267 --- /dev/null +++ b/cobigen/cobigen-core-api/src/test/resources/testdata/unittest/MavenUtilTest/jfrogJsonTest.json @@ -0,0 +1,9 @@ +{ +"results": [ + { + "uri": "http://localhost:8081/artifactory/api/storage/libs-release-local/org/acme/artifact/1.0/artifact-1.0-sources.jar" + },{ + "uri": "http://localhost:8081/artifactory/api/storage/libs-release-local/org/acme/artifactB/1.0/artifactB-1.0-sources.jar" + } +] +} \ No newline at end of file diff --git a/cobigen/cobigen-core-api/src/test/resources/testdata/unittest/MavenUtilTest/mavenJsonTest.json b/cobigen/cobigen-core-api/src/test/resources/testdata/unittest/MavenUtilTest/mavenJsonTest.json new file mode 100644 index 0000000000..6401fe8466 --- /dev/null +++ b/cobigen/cobigen-core-api/src/test/resources/testdata/unittest/MavenUtilTest/mavenJsonTest.json @@ -0,0 +1,118 @@ +{ + "responseHeader":{ + "status":0, + "QTime":1, + "params":{ + "q":"g:com.google.inject", + "core":"", + "indent":"off", + "spellcheck":"true", + "fl":"id,g,a,latestVersion,p,ec,repositoryId,text,timestamp,versionCount", + "start":"", + "sort":"score desc,timestamp desc,g asc,a asc", + "spellcheck.count":"5", + "rows":"20", + "wt":"json", + "version":"2.2" + } + }, + "response":{ + "numFound":4, + "start":0, + "docs":[ + { + "id":"com.google.inject:guice", + "g":"com.google.inject", + "a":"guice", + "latestVersion":"5.1.0", + "repositoryId":"central", + "p":"jar", + "timestamp":1643061977000, + "versionCount":19, + "text":[ + "com.google.inject", + "guice", + "-javadoc.jar", + "-sources.jar", + "-test-sources.jar", + "-tests.jar", + ".jar", + "-classes.jar", + ".pom" + ], + "ec":[ + "-javadoc.jar", + "-sources.jar", + "-test-sources.jar", + "-tests.jar", + ".jar", + "-classes.jar", + ".pom" + ] + }, + { + "id":"com.google.inject:guice-bom", + "g":"com.google.inject", + "a":"guice-bom", + "latestVersion":"5.1.0", + "repositoryId":"central", + "p":"pom", + "timestamp":1643061926000, + "versionCount":10, + "text":[ + "com.google.inject", + "guice-bom", + ".pom" + ], + "ec":[ + ".pom" + ] + }, + { + "id":"com.google.inject:guice-parent", + "g":"com.google.inject", + "a":"guice-parent", + "latestVersion":"5.1.0", + "repositoryId":"central", + "p":"pom", + "timestamp":1643061923000, + "versionCount":19, + "text":[ + "com.google.inject", + "guice-parent", + ".pom" + ], + "ec":[ + ".pom" + ] + }, + { + "id":"com.google.inject:jdk8-tests", + "g":"com.google.inject", + "a":"jdk8-tests", + "latestVersion":"5.0.1", + "repositoryId":"central", + "p":"jar", + "timestamp":1614380739000, + "versionCount":7, + "text":[ + "com.google.inject", + "jdk8-tests", + ".jar", + "-tests.jar", + ".pom" + ], + "ec":[ + ".jar", + "-tests.jar", + ".pom" + ] + } + ] + }, + "spellcheck":{ + "suggestions":[ + + ] + } +} \ No newline at end of file diff --git a/cobigen/cobigen-core-api/src/test/resources/testdata/unittest/MavenUtilTest/nexusJsonTest.json b/cobigen/cobigen-core-api/src/test/resources/testdata/unittest/MavenUtilTest/nexusJsonTest.json new file mode 100644 index 0000000000..4488061d96 --- /dev/null +++ b/cobigen/cobigen-core-api/src/test/resources/testdata/unittest/MavenUtilTest/nexusJsonTest.json @@ -0,0 +1,42 @@ +{ + "items" : [ { + "id" : "bWF2ZW4tY2VudHJhbDoyZTQ3ZGRhMGYxYjU1NWUwNzE1OWRjOWY5ZGQzZmVmNA", + "repository" : "maven-central", + "format" : "maven2", + "group" : "org.osgi", + "name" : "org.osgi.core", + "version" : "4.3.1", + "assets" : [ { + "downloadUrl" : "http://localhost:8081/repository/maven-central/org/osgi/org.osgi.core/4.3.1/org.osgi.core-4.3.1-sources.jar", + "path" : "org/osgi/org.osgi.core/4.3.1/org.osgi.core-4.3.1-sources.jar", + "id" : "bWF2ZW4tY2VudHJhbDplMDE4OGVkMDcyOGZhNjhmNDExNzU2OGU1MjQ2NjZiYg", + "repository" : "maven-central", + "format" : "maven2", + "checksum" : { + "sha1" : "80bfafcf783988442b3a58318face1d2132db33d", + "md5" : "87ee0258b79dc852626b91818316b9c3" + } + }, { + "downloadUrl" : "http://localhost:8081/repository/maven-central/org/osgi/org.osgi.core/4.3.1/org.osgi.core-4.3.1.jar", + "path" : "org/osgi/org.osgi.core/4.3.1/org.osgi.core-4.3.1.jar", + "id" : "bWF2ZW4tY2VudHJhbDpkMDY0ODA0YThlZDVhZDZlNjhmZGU5MWNmM2NiZTgzMw", + "repository" : "maven-central", + "format" : "maven2", + "checksum" : { + "sha1" : "5458ffe2ba049e76c29f2df2dc3ffccddf8b839e", + "md5" : "8053bbc1b55d51f5abae005625209d08" + } + }, { + "downloadUrl" : "http://localhost:8081/repository/maven-central/org/osgi/org.osgi.core/4.3.1/org.osgi.core-4.3.1.pom", + "path" : "org/osgi/org.osgi.core/4.3.1/org.osgi.core-4.3.1.pom", + "id" : "bWF2ZW4tY2VudHJhbDo2NTRiYjdkMGE1OTIxMzg1OWZhMTVkMzNmYWU1ZmY3OA", + "repository" : "maven-central", + "format" : "maven2", + "checksum" : { + "sha1" : "79391fc69dd72ad1fd983d01b4572f93f644882b", + "md5" : "3d87a59bcdb4b131d9a63e87e0ed924a" + } + } ] + } ], + "continuationToken" : null +} \ No newline at end of file From 0628f8a65f5874617be82fd320b1b5f50a3ef9be Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Thu, 21 Jul 2022 21:04:51 +0200 Subject: [PATCH 02/42] #1529 added missing dependencies --- cobigen/cobigen-core-api/pom.xml | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/cobigen/cobigen-core-api/pom.xml b/cobigen/cobigen-core-api/pom.xml index 7f8555d058..bebcce3d78 100644 --- a/cobigen/cobigen-core-api/pom.xml +++ b/cobigen/cobigen-core-api/pom.xml @@ -27,7 +27,7 @@ com.google.guava guava - + org.zeroturnaround @@ -40,11 +40,30 @@ core-test test + commons-io commons-io test + + + org.glassfish.jersey.media + jersey-media-json-jackson + 3.0.5 + + + + org.glassfish.jersey.core + jersey-client + 3.0.5 + + + + org.glassfish.jersey.inject + jersey-hk2 + 3.0.5 + From 575dfbd549aad034fb9dd3daad6d706a7fbaee72 Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Thu, 28 Jul 2022 13:10:41 +0200 Subject: [PATCH 03/42] #1529 implemented requested changes added new RESTSearchResponseException replaced IOException with RESTSearchResponseException --- .../RESTSearchResponseException.java | 40 +++++++++++++++++++ .../devonfw/cobigen/api/util/MavenUtil.java | 10 ++--- .../util/to/AbstractRESTSearchResponse.java | 23 +++++++---- .../api/util/to/JfrogSearchResponse.java | 11 ++--- .../api/util/to/MavenSearchResponse.java | 11 ++--- .../api/util/to/NexusSearchResponse.java | 11 ++--- 6 files changed, 68 insertions(+), 38 deletions(-) create mode 100644 cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/exception/RESTSearchResponseException.java diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/exception/RESTSearchResponseException.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/exception/RESTSearchResponseException.java new file mode 100644 index 0000000000..546a967d13 --- /dev/null +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/exception/RESTSearchResponseException.java @@ -0,0 +1,40 @@ +package com.devonfw.cobigen.api.exception; + +/** Exception to indicate that the REST search API encountered a problem while accessing the server. */ +public class RESTSearchResponseException extends CobiGenRuntimeException { + + private static final long serialVersionUID = 1L; + + /** + * Creates a new {@link RESTSearchResponseException} with the given message + * + * @param msg error message of the exception + */ + public RESTSearchResponseException(String msg) { + + super(msg); + } + + /** + * Creates a new {@link RESTSearchResponseException} with the specified message and the causing {@link Throwable} + * + * @param message describing the exception + * @param cause the causing Throwable + */ + public RESTSearchResponseException(String message, Throwable cause) { + + super(message, cause); + } + + /** + * Creates a new {@link RESTSearchResponseException} with the specified message and the causing {@link Throwable} + * + * @param statusCode status code causing the {@link RESTSearchResponseException} or null if not available + * @param message describing the exception + */ + public RESTSearchResponseException(String message, String statusCode) { + + super((statusCode != null ? statusCode + ":\n" : "") + message); + } + +} diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/MavenUtil.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/MavenUtil.java index c6bbf2db77..2993e3fe03 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/MavenUtil.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/MavenUtil.java @@ -8,7 +8,6 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.concurrent.ExecutionException; @@ -28,7 +27,9 @@ import com.devonfw.cobigen.api.constants.MavenConstants; import com.devonfw.cobigen.api.exception.CobiGenRuntimeException; +import com.devonfw.cobigen.api.exception.RESTSearchResponseException; import com.devonfw.cobigen.api.util.to.AbstractRESTSearchResponse; +import com.fasterxml.jackson.core.JsonProcessingException; import com.google.common.collect.Lists; import com.google.common.hash.Hashing; import com.google.common.io.ByteSource; @@ -353,12 +354,9 @@ public static Path getProjectRoot(Path inputFile, boolean topLevel) { */ public static List getMavenArtifactsByGroupId(String repositoryType, String groupId) { - List artifactList = new ArrayList<>(); - try { - artifactList = AbstractRESTSearchResponse.getArtifactDownloadLinks(repositoryType, groupId); - return artifactList; - } catch (IOException e) { + return AbstractRESTSearchResponse.getArtifactDownloadLinks(repositoryType, groupId); + } catch (RESTSearchResponseException | JsonProcessingException | MalformedURLException e) { throw new CobiGenRuntimeException("Unable to get artifacts from " + repositoryType + " by groupId " + groupId, e); } diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractRESTSearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractRESTSearchResponse.java index 803081c9d8..4c2866e6c5 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractRESTSearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractRESTSearchResponse.java @@ -1,11 +1,13 @@ package com.devonfw.cobigen.api.util.to; -import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.List; +import com.devonfw.cobigen.api.exception.RESTSearchResponseException; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.ws.rs.client.Client; @@ -35,9 +37,9 @@ public interface AbstractRESTSearchResponse { * @param repositoryUrl URL of the repository * @param groupId to search for * @return String of json response - * @throws IOException if the request did not return status 200 + * @throws RESTSearchResponseException if the request did not return status 200 */ - public String getJsonResponse(String repositoryUrl, String groupId) throws IOException; + public String getJsonResponse(String repositoryUrl, String groupId) throws RESTSearchResponseException; /** * Gets the download links by given repository type @@ -46,10 +48,14 @@ public interface AbstractRESTSearchResponse { * @param groupId the groupId to search for * * @return List of download links - * @throws IOException if an error occurred + * @throws RESTSearchResponseException if an error occurred + * @throws JsonProcessingException + * @throws JsonMappingException + * @throws MalformedURLException * */ - public static List getArtifactDownloadLinks(String repositoryType, String groupId) throws IOException { + public static List getArtifactDownloadLinks(String repositoryType, String groupId) + throws RESTSearchResponseException, JsonMappingException, JsonProcessingException, MalformedURLException { List downloadLinks = new ArrayList<>(); ObjectMapper mapper = new ObjectMapper(); @@ -90,9 +96,9 @@ public static List getArtifactDownloadLinks(String repositoryType, String g * * @param targetLink link to get response from * @return String of json response - * @throws IOException if the returned status code was not 200 OK + * @throws RESTSearchResponseException if the returned status code was not 200 OK */ - public static String getJsonResponseStringByTargetLink(String targetLink) throws IOException { + public static String getJsonResponseStringByTargetLink(String targetLink) throws RESTSearchResponseException { Client client = ClientBuilder.newClient(); WebTarget target = client.target(targetLink); @@ -103,7 +109,8 @@ public static String getJsonResponseStringByTargetLink(String targetLink) throws if (status == 200) { jsonResponse = response.readEntity(String.class); } else { - throw new IOException(String.valueOf(status)); + throw new RESTSearchResponseException("The search REST API returned the unexpected status code", + String.valueOf(status)); } return jsonResponse; } diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/JfrogSearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/JfrogSearchResponse.java index ec4b349ff8..d6c2792fd8 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/JfrogSearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/JfrogSearchResponse.java @@ -1,6 +1,5 @@ package com.devonfw.cobigen.api.util.to; -import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; @@ -9,6 +8,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.devonfw.cobigen.api.exception.RESTSearchResponseException; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; @@ -52,19 +52,14 @@ public List getResults() { @Override @JsonIgnore - public String getJsonResponse(String repositoryUrl, String groupId) throws IOException { + public String getJsonResponse(String repositoryUrl, String groupId) throws RESTSearchResponseException { String targetLink = repositoryUrl + "/" + "solrsearch/select?q=g:" + groupId + "&wt=json"; LOG.info("Starting Jfrog Search REST API request with URL: {}.", targetLink); String jsonResponse; - try { - jsonResponse = AbstractRESTSearchResponse.getJsonResponseStringByTargetLink(targetLink); - } catch (IOException e) { - LOG.error("Jfrog Search REST API request was not successful, status code was: {}!", e.getMessage()); - throw new IOException(); - } + jsonResponse = AbstractRESTSearchResponse.getJsonResponseStringByTargetLink(targetLink); return jsonResponse; } diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/MavenSearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/MavenSearchResponse.java index 1d25783b42..a6a87d0b1b 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/MavenSearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/MavenSearchResponse.java @@ -1,6 +1,5 @@ package com.devonfw.cobigen.api.util.to; -import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; @@ -9,6 +8,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.devonfw.cobigen.api.exception.RESTSearchResponseException; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; @@ -149,7 +149,7 @@ public MavenResponse getResponse() { @Override @JsonIgnore - public String getJsonResponse(String repositoryUrl, String groupId) throws IOException { + public String getJsonResponse(String repositoryUrl, String groupId) throws RESTSearchResponseException { String targetLink = repositoryUrl + "/" + "solrsearch/select?q=g:" + groupId + "&wt=json"; LOG.info("Starting Maven Search REST API request with URL: {}.", targetLink); @@ -162,12 +162,7 @@ public String getJsonResponse(String repositoryUrl, String groupId) throws IOExc String jsonResponse; - try { - jsonResponse = AbstractRESTSearchResponse.getJsonResponseStringByTargetLink(targetLink); - } catch (IOException e) { - LOG.error("Maven Search REST API request was not successful, status code was: {}!", e.getMessage()); - throw new IOException(); - } + jsonResponse = AbstractRESTSearchResponse.getJsonResponseStringByTargetLink(targetLink); return jsonResponse; diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/NexusSearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/NexusSearchResponse.java index f592f4c04d..c9a56ad748 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/NexusSearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/NexusSearchResponse.java @@ -1,6 +1,5 @@ package com.devonfw.cobigen.api.util.to; -import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; @@ -9,6 +8,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.devonfw.cobigen.api.exception.RESTSearchResponseException; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; @@ -82,19 +82,14 @@ public List getDownloadURLs() throws MalformedURLException { @Override @JsonIgnore - public String getJsonResponse(String repositoryUrl, String groupId) throws IOException { + public String getJsonResponse(String repositoryUrl, String groupId) throws RESTSearchResponseException { String targetLink = repositoryUrl + "/" + "service/rest/v1/search?repository=maven-central&group=" + groupId; LOG.info("Starting Nexus Search REST API request with URL: {}.", targetLink); String jsonResponse; - try { - jsonResponse = AbstractRESTSearchResponse.getJsonResponseStringByTargetLink(targetLink); - } catch (IOException e) { - LOG.error("Nexus Search REST API request was not successful, status code was: {}!", e.getMessage()); - throw new IOException(); - } + jsonResponse = AbstractRESTSearchResponse.getJsonResponseStringByTargetLink(targetLink); return jsonResponse; } From e97a4159d10dda4aa378be5603e9a39071d8929d Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Thu, 28 Jul 2022 13:23:03 +0200 Subject: [PATCH 04/42] #1529 fixed jFrog API --- .../com/devonfw/cobigen/api/util/to/JfrogSearchResponse.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/JfrogSearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/JfrogSearchResponse.java index d6c2792fd8..b357d9d91f 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/JfrogSearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/JfrogSearchResponse.java @@ -54,7 +54,7 @@ public List getResults() { @JsonIgnore public String getJsonResponse(String repositoryUrl, String groupId) throws RESTSearchResponseException { - String targetLink = repositoryUrl + "/" + "solrsearch/select?q=g:" + groupId + "&wt=json"; + String targetLink = repositoryUrl + "/" + "api/search/gavc?g=" + groupId; LOG.info("Starting Jfrog Search REST API request with URL: {}.", targetLink); String jsonResponse; From c9e0340d4a53e3c5f11cb48524f10d762b83c346 Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Mon, 8 Aug 2022 11:42:16 +0200 Subject: [PATCH 05/42] #1529 implemented requested changes changed repositoryType from String to MavenSearchRepositoryType enum added new MavenSearchRepositoryType enum --- .../constants/MavenSearchRepositoryType.java | 23 +++++++++++++++++++ .../devonfw/cobigen/api/util/MavenUtil.java | 3 ++- .../util/to/AbstractRESTSearchResponse.java | 9 ++++---- .../devonfw/cobigen/api/MavenUtilTest.java | 7 +++--- 4 files changed, 34 insertions(+), 8 deletions(-) create mode 100644 cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/constants/MavenSearchRepositoryType.java diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/constants/MavenSearchRepositoryType.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/constants/MavenSearchRepositoryType.java new file mode 100644 index 0000000000..d00a5e0661 --- /dev/null +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/constants/MavenSearchRepositoryType.java @@ -0,0 +1,23 @@ +package com.devonfw.cobigen.api.constants; + +/** + * Maven search repository types + * + */ +public enum MavenSearchRepositoryType { + + /** + * Nexus search repository type + */ + nexus, + + /** + * Maven search repository type + */ + maven, + + /** + * Jfrog search repository type + */ + jfrog +} diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/MavenUtil.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/MavenUtil.java index 2993e3fe03..1680cdfc9e 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/MavenUtil.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/MavenUtil.java @@ -26,6 +26,7 @@ import org.zeroturnaround.exec.stream.slf4j.Slf4jStream; import com.devonfw.cobigen.api.constants.MavenConstants; +import com.devonfw.cobigen.api.constants.MavenSearchRepositoryType; import com.devonfw.cobigen.api.exception.CobiGenRuntimeException; import com.devonfw.cobigen.api.exception.RESTSearchResponseException; import com.devonfw.cobigen.api.util.to.AbstractRESTSearchResponse; @@ -352,7 +353,7 @@ public static Path getProjectRoot(Path inputFile, boolean topLevel) { * * @return List of artifact URLS */ - public static List getMavenArtifactsByGroupId(String repositoryType, String groupId) { + public static List getMavenArtifactsByGroupId(MavenSearchRepositoryType repositoryType, String groupId) { try { return AbstractRESTSearchResponse.getArtifactDownloadLinks(repositoryType, groupId); diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractRESTSearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractRESTSearchResponse.java index 4c2866e6c5..e1253cdf75 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractRESTSearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractRESTSearchResponse.java @@ -5,6 +5,7 @@ import java.util.ArrayList; import java.util.List; +import com.devonfw.cobigen.api.constants.MavenSearchRepositoryType; import com.devonfw.cobigen.api.exception.RESTSearchResponseException; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonMappingException; @@ -54,14 +55,14 @@ public interface AbstractRESTSearchResponse { * @throws MalformedURLException * */ - public static List getArtifactDownloadLinks(String repositoryType, String groupId) + public static List getArtifactDownloadLinks(MavenSearchRepositoryType repositoryType, String groupId) throws RESTSearchResponseException, JsonMappingException, JsonProcessingException, MalformedURLException { List downloadLinks = new ArrayList<>(); ObjectMapper mapper = new ObjectMapper(); String jsonResponse = ""; - if (repositoryType.equals("maven")) { + if (repositoryType == MavenSearchRepositoryType.maven) { MavenSearchResponse response = new MavenSearchResponse(); String mavenRepositoryURL = "https://search.maven.org"; jsonResponse = response.getJsonResponse(mavenRepositoryURL, groupId); @@ -70,7 +71,7 @@ public static List getArtifactDownloadLinks(String repositoryType, String g return downloadLinks; } - if (repositoryType.equals("jfrog")) { + if (repositoryType == MavenSearchRepositoryType.jfrog) { JfrogSearchResponse response = new JfrogSearchResponse(); String jfrogRepositoryURL = ""; jsonResponse = response.getJsonResponse(jfrogRepositoryURL, groupId); @@ -79,7 +80,7 @@ public static List getArtifactDownloadLinks(String repositoryType, String g return downloadLinks; } - if (repositoryType.equals("nexus")) { + if (repositoryType == MavenSearchRepositoryType.nexus) { NexusSearchResponse response = new NexusSearchResponse(); String nexusRepositoryURL = ""; jsonResponse = response.getJsonResponse(nexusRepositoryURL, groupId); diff --git a/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java b/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java index 059bffb977..a117a3ba06 100644 --- a/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java +++ b/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java @@ -10,6 +10,7 @@ import org.junit.Test; +import com.devonfw.cobigen.api.constants.MavenSearchRepositoryType; import com.devonfw.cobigen.api.util.MavenUtil; import com.devonfw.cobigen.api.util.to.JfrogSearchResponse; import com.devonfw.cobigen.api.util.to.MavenSearchResponse; @@ -98,7 +99,7 @@ public void testMavenSearchRequestGetsValidDownloadLinks() throws IOException { List downloadList; - downloadList = MavenUtil.getMavenArtifactsByGroupId("maven", "com.google.inject"); + downloadList = MavenUtil.getMavenArtifactsByGroupId(MavenSearchRepositoryType.maven, "com.google.inject"); assertThat(downloadList).contains( new URL("https://repo1.maven.org/maven2/com/google/inject/guice/5.1.0/guice-5.1.0.jar"), @@ -117,7 +118,7 @@ public void testNexusSearchRequestGetsValidDownloadLinks() throws IOException { List downloadList; - downloadList = MavenUtil.getMavenArtifactsByGroupId("nexus", "com.google.inject"); + downloadList = MavenUtil.getMavenArtifactsByGroupId(MavenSearchRepositoryType.nexus, "com.google.inject"); assertThat(downloadList).contains( new URL("https://repo1.maven.org/maven2/com/google/inject/guice/5.1.0/guice-5.1.0.jar"), @@ -136,7 +137,7 @@ public void testJfrogSearchRequestGetsValidDownloadLinks() throws IOException { List downloadList; - downloadList = MavenUtil.getMavenArtifactsByGroupId("jfrog", "com.google.inject"); + downloadList = MavenUtil.getMavenArtifactsByGroupId(MavenSearchRepositoryType.jfrog, "com.google.inject"); assertThat(downloadList).contains( new URL("https://repo1.maven.org/maven2/com/google/inject/guice/5.1.0/guice-5.1.0.jar"), From 8e947db840c7eac64260f6f8607df438e69ae18b Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Mon, 8 Aug 2022 11:49:44 +0200 Subject: [PATCH 06/42] #1529 implemented requested changes replaced fixed repository URLs with constants added new MavenSearchRepositoryConstants --- .../MavenSearchRepositoryConstants.java | 23 +++++++++++++++++++ .../util/to/AbstractRESTSearchResponse.java | 7 +++--- 2 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/constants/MavenSearchRepositoryConstants.java diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/constants/MavenSearchRepositoryConstants.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/constants/MavenSearchRepositoryConstants.java new file mode 100644 index 0000000000..1793656660 --- /dev/null +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/constants/MavenSearchRepositoryConstants.java @@ -0,0 +1,23 @@ +package com.devonfw.cobigen.api.constants; + +/** + * Maven search repository URLs + * + */ +public class MavenSearchRepositoryConstants { + + /** + * Maven repository URL + */ + public static String mavenRepositoryURL = "https://search.maven.org"; + + /** + * Nexus repository URL + */ + public static String nexusRepositoryURL = ""; + + /** + * Jfrog repository URL + */ + public static String jfrogRepositoryURL = ""; +} diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractRESTSearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractRESTSearchResponse.java index e1253cdf75..552eb44e46 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractRESTSearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractRESTSearchResponse.java @@ -5,6 +5,7 @@ import java.util.ArrayList; import java.util.List; +import com.devonfw.cobigen.api.constants.MavenSearchRepositoryConstants; import com.devonfw.cobigen.api.constants.MavenSearchRepositoryType; import com.devonfw.cobigen.api.exception.RESTSearchResponseException; import com.fasterxml.jackson.core.JsonProcessingException; @@ -64,7 +65,7 @@ public static List getArtifactDownloadLinks(MavenSearchRepositoryType repos if (repositoryType == MavenSearchRepositoryType.maven) { MavenSearchResponse response = new MavenSearchResponse(); - String mavenRepositoryURL = "https://search.maven.org"; + String mavenRepositoryURL = MavenSearchRepositoryConstants.mavenRepositoryURL; jsonResponse = response.getJsonResponse(mavenRepositoryURL, groupId); response = mapper.readValue(jsonResponse, MavenSearchResponse.class); downloadLinks = response.getDownloadURLs(); @@ -73,7 +74,7 @@ public static List getArtifactDownloadLinks(MavenSearchRepositoryType repos if (repositoryType == MavenSearchRepositoryType.jfrog) { JfrogSearchResponse response = new JfrogSearchResponse(); - String jfrogRepositoryURL = ""; + String jfrogRepositoryURL = MavenSearchRepositoryConstants.jfrogRepositoryURL; jsonResponse = response.getJsonResponse(jfrogRepositoryURL, groupId); response = mapper.readValue(jsonResponse, JfrogSearchResponse.class); downloadLinks = response.getDownloadURLs(); @@ -82,7 +83,7 @@ public static List getArtifactDownloadLinks(MavenSearchRepositoryType repos if (repositoryType == MavenSearchRepositoryType.nexus) { NexusSearchResponse response = new NexusSearchResponse(); - String nexusRepositoryURL = ""; + String nexusRepositoryURL = MavenSearchRepositoryConstants.nexusRepositoryURL; jsonResponse = response.getJsonResponse(nexusRepositoryURL, groupId); response = mapper.readValue(jsonResponse, NexusSearchResponse.class); downloadLinks = response.getDownloadURLs(); From eb78cade4202c3fe5cbb402b6d36b3f4b12c3d8b Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Mon, 8 Aug 2022 19:20:09 +0200 Subject: [PATCH 07/42] #1529 implemented requested changes converted MavenSearchResponseConstants to uppercase added MAVEN, NEXUS and JFROG TARGET_LINK constants added MAVEN_MAX_RESPONSE_ROWS constant added javadoc to all models (moved main model to top) renamed json response models --- .../MavenSearchRepositoryConstants.java | 27 ++- .../util/to/AbstractRESTSearchResponse.java | 6 +- .../api/util/to/JfrogSearchResponse.java | 52 +++--- .../api/util/to/MavenSearchResponse.java | 161 +++++++++++------- .../api/util/to/NexusSearchResponse.java | 125 +++++++++----- 5 files changed, 240 insertions(+), 131 deletions(-) diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/constants/MavenSearchRepositoryConstants.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/constants/MavenSearchRepositoryConstants.java index 1793656660..858ca5b548 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/constants/MavenSearchRepositoryConstants.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/constants/MavenSearchRepositoryConstants.java @@ -9,15 +9,36 @@ public class MavenSearchRepositoryConstants { /** * Maven repository URL */ - public static String mavenRepositoryURL = "https://search.maven.org"; + public static String MAVEN_REPOSITORY_URL = "https://search.maven.org"; + + /** + * Maven target link + */ + public static String MAVEN_TARGET_LINK = "solrsearch/select"; + + /** + * Maven maximum response rows + */ + public static int MAVEN_MAX_RESPONSE_ROWS = 20; /** * Nexus repository URL */ - public static String nexusRepositoryURL = ""; + public static String NEXUS_REPOSITORY_URL = ""; + + /** + * Nexus target link + */ + public static String NEXUS_TARGET_LINK = "service/rest/v1/search"; /** * Jfrog repository URL */ - public static String jfrogRepositoryURL = ""; + public static String JFROG_REPOSITORY_URL = ""; + + /** + * Jfrog target link + */ + public static String JFROG_TARGET_LINK = "api/search/gavc"; + } diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractRESTSearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractRESTSearchResponse.java index 552eb44e46..f5f2bf798f 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractRESTSearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractRESTSearchResponse.java @@ -65,7 +65,7 @@ public static List getArtifactDownloadLinks(MavenSearchRepositoryType repos if (repositoryType == MavenSearchRepositoryType.maven) { MavenSearchResponse response = new MavenSearchResponse(); - String mavenRepositoryURL = MavenSearchRepositoryConstants.mavenRepositoryURL; + String mavenRepositoryURL = MavenSearchRepositoryConstants.MAVEN_REPOSITORY_URL; jsonResponse = response.getJsonResponse(mavenRepositoryURL, groupId); response = mapper.readValue(jsonResponse, MavenSearchResponse.class); downloadLinks = response.getDownloadURLs(); @@ -74,7 +74,7 @@ public static List getArtifactDownloadLinks(MavenSearchRepositoryType repos if (repositoryType == MavenSearchRepositoryType.jfrog) { JfrogSearchResponse response = new JfrogSearchResponse(); - String jfrogRepositoryURL = MavenSearchRepositoryConstants.jfrogRepositoryURL; + String jfrogRepositoryURL = MavenSearchRepositoryConstants.JFROG_REPOSITORY_URL; jsonResponse = response.getJsonResponse(jfrogRepositoryURL, groupId); response = mapper.readValue(jsonResponse, JfrogSearchResponse.class); downloadLinks = response.getDownloadURLs(); @@ -83,7 +83,7 @@ public static List getArtifactDownloadLinks(MavenSearchRepositoryType repos if (repositoryType == MavenSearchRepositoryType.nexus) { NexusSearchResponse response = new NexusSearchResponse(); - String nexusRepositoryURL = MavenSearchRepositoryConstants.nexusRepositoryURL; + String nexusRepositoryURL = MavenSearchRepositoryConstants.NEXUS_REPOSITORY_URL; jsonResponse = response.getJsonResponse(nexusRepositoryURL, groupId); response = mapper.readValue(jsonResponse, NexusSearchResponse.class); downloadLinks = response.getDownloadURLs(); diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/JfrogSearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/JfrogSearchResponse.java index b357d9d91f..e61f600f25 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/JfrogSearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/JfrogSearchResponse.java @@ -8,26 +8,11 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.devonfw.cobigen.api.constants.MavenSearchRepositoryConstants; import com.devonfw.cobigen.api.exception.RESTSearchResponseException; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; -class Result { - - @JsonProperty("uri") - private String uri; - - /** - * @return uri - */ - @JsonIgnore - public String getUri() { - - return this.uri; - } - -} - /** * Json model for jfrog Search REST API response * @@ -38,14 +23,17 @@ public class JfrogSearchResponse implements AbstractRESTSearchResponse { @JsonIgnore private static final Logger LOG = LoggerFactory.getLogger(JfrogSearchResponse.class); + /** + * results + */ @JsonProperty("results") - private List results; + private List results; /** * @return results */ @JsonIgnore - public List getResults() { + public List getResults() { return this.results; } @@ -54,7 +42,7 @@ public List getResults() { @JsonIgnore public String getJsonResponse(String repositoryUrl, String groupId) throws RESTSearchResponseException { - String targetLink = repositoryUrl + "/" + "api/search/gavc?g=" + groupId; + String targetLink = repositoryUrl + "/" + MavenSearchRepositoryConstants.JFROG_TARGET_LINK + "?g=" + groupId; LOG.info("Starting Jfrog Search REST API request with URL: {}.", targetLink); String jsonResponse; @@ -70,10 +58,34 @@ public List getDownloadURLs() throws MalformedURLException { List downloadLinks = new ArrayList<>(); - for (Result result : getResults()) { + for (JfrogSearchResponseResult result : getResults()) { downloadLinks.add(new URL(result.getUri())); } return downloadLinks; } } + +/** + * + * Jfrog search response result model + * + */ +class JfrogSearchResponseResult { + + /** + * uri + */ + @JsonProperty("uri") + private String uri; + + /** + * @return uri + */ + @JsonIgnore + public String getUri() { + + return this.uri; + } + +} diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/MavenSearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/MavenSearchResponse.java index a6a87d0b1b..889b65b6d0 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/MavenSearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/MavenSearchResponse.java @@ -8,25 +8,107 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.devonfw.cobigen.api.constants.MavenSearchRepositoryConstants; import com.devonfw.cobigen.api.exception.RESTSearchResponseException; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; +/** + * Json model for maven Search REST API response + * + */ +@JsonIgnoreProperties(value = { "responseHeader", "spellcheck" }) +public class MavenSearchResponse implements AbstractRESTSearchResponse { + + /** Logger instance. */ + @JsonIgnore + private static final Logger LOG = LoggerFactory.getLogger(MavenSearchResponse.class); + + @JsonProperty("response") + private MavenSearchResponseResponse response; + + /** + * @return response + */ + @JsonIgnore + public MavenSearchResponseResponse getResponse() { + + return this.response; + } + + @Override + @JsonIgnore + public String getJsonResponse(String repositoryUrl, String groupId) throws RESTSearchResponseException { + + String targetLink = repositoryUrl + "/" + MavenSearchRepositoryConstants.MAVEN_TARGET_LINK + "?q=g:" + groupId + + "&wt=json"; + LOG.info("Starting Maven Search REST API request with URL: {}.", targetLink); + + int limitRows = MavenSearchRepositoryConstants.MAVEN_MAX_RESPONSE_ROWS; + if (limitRows > 0) { + targetLink += "&rows=" + limitRows; + LOG.info("Limiting Maven Search REST API request to: {} rows.", limitRows); + } + + String jsonResponse; + + jsonResponse = AbstractRESTSearchResponse.getJsonResponseStringByTargetLink(targetLink); + + return jsonResponse; + + } + + @Override + @JsonIgnore + public List getDownloadURLs() throws MalformedURLException { + + List downloadLinks = new ArrayList<>(); + List docs = getResponse().getDocs(); + + for (MavenSearchResponseDoc doc : docs) { + downloadLinks.add(doc.createDownloadLink("https://repo1.maven.org/maven2")); + } + + return downloadLinks; + } + +} + +/** + * + * Maven search response doc model + * + */ @JsonIgnoreProperties(value = { "p", "timestamp", "versionCount", "text", "ec" }) -class Doc { +class MavenSearchResponseDoc { + /** + * id + */ @JsonProperty("id") private String id; + /** + * group + */ @JsonProperty("g") private String group; + /** + * artifact + */ @JsonProperty("a") private String artifact; + /** + * latest version + */ @JsonProperty("latestVersion") private String latestVersion; + /** + * repository ID + */ @JsonProperty("repositoryId") private String repositoryId; @@ -95,14 +177,25 @@ public URL createDownloadLink(String mavenRepo) throws MalformedURLException { } +/** + * + * Maven search response model + * + */ @JsonIgnoreProperties(value = { "start" }) -class MavenResponse { +class MavenSearchResponseResponse { + /** + * found results + */ @JsonProperty("numFound") private int numFound; + /** + * docs + */ @JsonProperty("docs") - private List docs; + private List docs; /** * @return numFound @@ -117,69 +210,9 @@ public int getNumFound() { * @return docs */ @JsonIgnore - public List getDocs() { + public List getDocs() { return this.docs; } } - -/** - * Json model for maven Search REST API response - * - */ -@JsonIgnoreProperties(value = { "responseHeader", "spellcheck" }) -public class MavenSearchResponse implements AbstractRESTSearchResponse { - - /** Logger instance. */ - @JsonIgnore - private static final Logger LOG = LoggerFactory.getLogger(MavenSearchResponse.class); - - @JsonProperty("response") - private MavenResponse response; - - /** - * @return response - */ - @JsonIgnore - public MavenResponse getResponse() { - - return this.response; - } - - @Override - @JsonIgnore - public String getJsonResponse(String repositoryUrl, String groupId) throws RESTSearchResponseException { - - String targetLink = repositoryUrl + "/" + "solrsearch/select?q=g:" + groupId + "&wt=json"; - LOG.info("Starting Maven Search REST API request with URL: {}.", targetLink); - - int limitRows = 20; - if (limitRows > 0) { - targetLink += "&rows=" + limitRows; - LOG.info("Limiting Maven Search REST API request to: {} rows.", limitRows); - } - - String jsonResponse; - - jsonResponse = AbstractRESTSearchResponse.getJsonResponseStringByTargetLink(targetLink); - - return jsonResponse; - - } - - @Override - @JsonIgnore - public List getDownloadURLs() throws MalformedURLException { - - List downloadLinks = new ArrayList<>(); - List docs = getResponse().getDocs(); - - for (Doc doc : docs) { - downloadLinks.add(doc.createDownloadLink("https://repo1.maven.org/maven2")); - } - - return downloadLinks; - } - -} diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/NexusSearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/NexusSearchResponse.java index c9a56ad748..82b5e395d4 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/NexusSearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/NexusSearchResponse.java @@ -8,48 +8,12 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.devonfw.cobigen.api.constants.MavenSearchRepositoryConstants; import com.devonfw.cobigen.api.exception.RESTSearchResponseException; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; -@JsonIgnoreProperties(value = { "id", "format", "checksum" }) -class Asset { - @JsonProperty("downloadUrl") - public String downloadUrl; - - @JsonProperty("path") - public String path; - - @JsonProperty("repository") - public String repository; - - @JsonProperty("latestVersion") - public String latestVersion; - - @JsonProperty("repositoryId") - public String repositoryId; -} - -@JsonIgnoreProperties(value = { "id", "format" }) -class Item { - - @JsonProperty("repository") - private String repository; - - @JsonProperty("group") - private String group; - - @JsonProperty("name") - private String name; - - @JsonProperty("version") - private String version; - - @JsonProperty("assets") - public List assets; -} - /** * Json model for nexus Search REST API response * @@ -62,7 +26,7 @@ public class NexusSearchResponse implements AbstractRESTSearchResponse { private static final Logger LOG = LoggerFactory.getLogger(NexusSearchResponse.class); @JsonProperty("items") - private List items; + private List items; @Override @JsonIgnore @@ -70,8 +34,8 @@ public List getDownloadURLs() throws MalformedURLException { List downloadLinks = new ArrayList<>(); - for (Item item : this.items) { - for (Asset asset : item.assets) { + for (NexusSearchResponseItem item : this.items) { + for (NexusSearchResponseAsset asset : item.assets) { downloadLinks.add(new URL(asset.downloadUrl)); } @@ -84,7 +48,8 @@ public List getDownloadURLs() throws MalformedURLException { @JsonIgnore public String getJsonResponse(String repositoryUrl, String groupId) throws RESTSearchResponseException { - String targetLink = repositoryUrl + "/" + "service/rest/v1/search?repository=maven-central&group=" + groupId; + String targetLink = repositoryUrl + "/" + MavenSearchRepositoryConstants.NEXUS_TARGET_LINK + + "?repository=maven-central" + "&group=" + groupId; LOG.info("Starting Nexus Search REST API request with URL: {}.", targetLink); String jsonResponse; @@ -94,3 +59,81 @@ public String getJsonResponse(String repositoryUrl, String groupId) throws RESTS return jsonResponse; } } + +/** + * + * Nexus search response asset model + * + */ +@JsonIgnoreProperties(value = { "id", "format", "checksum" }) +class NexusSearchResponseAsset { + + /** + * downloadUrl + */ + @JsonProperty("downloadUrl") + public String downloadUrl; + + /** + * path + */ + @JsonProperty("path") + public String path; + + /** + * repository + */ + @JsonProperty("repository") + public String repository; + + /** + * latest version + */ + @JsonProperty("latestVersion") + public String latestVersion; + + /** + * repositoryId + */ + @JsonProperty("repositoryId") + public String repositoryId; +} + +/** + * + * Nexus search response item model + * + */ +@JsonIgnoreProperties(value = { "id", "format" }) +class NexusSearchResponseItem { + + /** + * repository + */ + @JsonProperty("repository") + private String repository; + + /** + * group + */ + @JsonProperty("group") + private String group; + + /** + * name + */ + @JsonProperty("name") + private String name; + + /** + * version + */ + @JsonProperty("version") + private String version; + + /** + * assets + */ + @JsonProperty("assets") + public List assets; +} From b1b6532f18816c7228fa8aa03c5c991c0f1022c0 Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Tue, 9 Aug 2022 16:41:42 +0200 Subject: [PATCH 08/42] #1529 adjusted nexus REST API adjusted nexus REST API to v2.0 adjusted nexus REST API tests and resources to v2.0 refactored createDownloadLink method (moved to AbstractRESTSearchResponse) added new constants for MAVEN_REPOSITORY_LINK, NEXUS_REPOSITORY_URL, NEXUS_REPOSITORY_URL, NEXUS_TARGET_LINK, NEXUS_DC_ID added ec param to MavenSearchResponse --- .../MavenSearchRepositoryConstants.java | 19 +- .../util/to/AbstractRESTSearchResponse.java | 27 ++- .../api/util/to/MavenSearchResponse.java | 43 ++--- .../api/util/to/NexusSearchResponse.java | 116 +++++++----- .../devonfw/cobigen/api/MavenUtilTest.java | 35 ++-- .../unittest/MavenUtilTest/nexusJsonTest.json | 169 ++++++++++++++---- 6 files changed, 286 insertions(+), 123 deletions(-) diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/constants/MavenSearchRepositoryConstants.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/constants/MavenSearchRepositoryConstants.java index 858ca5b548..c1146a562d 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/constants/MavenSearchRepositoryConstants.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/constants/MavenSearchRepositoryConstants.java @@ -11,6 +11,11 @@ public class MavenSearchRepositoryConstants { */ public static String MAVEN_REPOSITORY_URL = "https://search.maven.org"; + /** + * Maven repository download link + */ + public static String MAVEN_REPOSITORY_DOWNLOAD_LINK = "https://repo1.maven.org/maven2"; + /** * Maven target link */ @@ -24,12 +29,22 @@ public class MavenSearchRepositoryConstants { /** * Nexus repository URL */ - public static String NEXUS_REPOSITORY_URL = ""; + public static String NEXUS_REPOSITORY_URL = "https://s01.oss.sonatype.org"; + + /** + * Nexus repository link + */ + public static String NEXUS_REPOSITORY_LINK = "service/local/repositories/releases/content"; /** * Nexus target link */ - public static String NEXUS_TARGET_LINK = "service/rest/v1/search"; + public static String NEXUS_TARGET_LINK = "service/local/lucene/search"; + + /** + * Nexus connect ID + */ + public static String NEXUS_DC_ID = "1660043999867"; /** * Jfrog repository URL diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractRESTSearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractRESTSearchResponse.java index f5f2bf798f..f21611470e 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractRESTSearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractRESTSearchResponse.java @@ -51,9 +51,9 @@ public interface AbstractRESTSearchResponse { * * @return List of download links * @throws RESTSearchResponseException if an error occurred - * @throws JsonProcessingException - * @throws JsonMappingException - * @throws MalformedURLException + * @throws JsonProcessingException if the json processing was not possible + * @throws JsonMappingException if the json mapping was not possible + * @throws MalformedURLException if an URL was malformed * */ public static List getArtifactDownloadLinks(MavenSearchRepositoryType repositoryType, String groupId) @@ -117,4 +117,25 @@ public static String getJsonResponseStringByTargetLink(String targetLink) throws return jsonResponse; } + /** + * Creates a download link (concatenates maven repository link with groupId, artifact and version) + * + * @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 + * @return concatenated download link + * @throws MalformedURLException if the URL was not valid + */ + public static URL createDownloadLink(String mavenRepo, String groupId, String artifactId, String version, + String fileEnding) throws MalformedURLException { + + String parsedGroupId = groupId.replace(".", "/"); + String downloadFile = artifactId + "-" + version + fileEnding; + String downloadLink = mavenRepo + "/" + parsedGroupId + "/" + artifactId + "/" + version + "/" + downloadFile; + URL url = new URL(downloadLink); + return url; + } + } \ No newline at end of file diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/MavenSearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/MavenSearchResponse.java index 889b65b6d0..0717fe711c 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/MavenSearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/MavenSearchResponse.java @@ -67,7 +67,13 @@ public List getDownloadURLs() throws MalformedURLException { List docs = getResponse().getDocs(); for (MavenSearchResponseDoc doc : docs) { - downloadLinks.add(doc.createDownloadLink("https://repo1.maven.org/maven2")); + for (String fileEnding : doc.getEc()) { + String newFileEnding = fileEnding; + downloadLinks.add( + AbstractRESTSearchResponse.createDownloadLink(MavenSearchRepositoryConstants.MAVEN_REPOSITORY_DOWNLOAD_LINK, + doc.getGroup(), doc.getArtifact(), doc.getLatestVersion(), newFileEnding)); + } + } return downloadLinks; @@ -80,7 +86,7 @@ public List getDownloadURLs() throws MalformedURLException { * Maven search response doc model * */ -@JsonIgnoreProperties(value = { "p", "timestamp", "versionCount", "text", "ec" }) +@JsonIgnoreProperties(value = { "p", "timestamp", "versionCount", "text" }) class MavenSearchResponseDoc { /** * id @@ -112,6 +118,21 @@ class MavenSearchResponseDoc { @JsonProperty("repositoryId") private String repositoryId; + /** + * ec (file ending) + */ + @JsonProperty("ec") + private List ec; + + /** + * @return ec + */ + @JsonIgnore + public List getEc() { + + return this.ec; + } + /** * @return id */ @@ -157,24 +178,6 @@ public String getRepositoryId() { return this.repositoryId; } - /** - * Creates a download link (concatenates maven repository link with groupId, artifact and version) - * - * @param mavenRepo link to the maven repository to use - * @return concatenated download link - * @throws MalformedURLException if the URL was not valid - */ - @JsonIgnore - public URL createDownloadLink(String mavenRepo) throws MalformedURLException { - - String parsedGroupId = getGroup().replace(".", "/"); - String downloadFile = getArtifact() + "-" + getLatestVersion() + ".jar"; - String downloadLink = mavenRepo + "/" + parsedGroupId + "/" + getArtifact() + "/" + getLatestVersion() + "/" - + downloadFile; - URL url = new URL(downloadLink); - return url; - } - } /** diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/NexusSearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/NexusSearchResponse.java index 82b5e395d4..004b08993f 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/NexusSearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/NexusSearchResponse.java @@ -4,6 +4,7 @@ import java.net.URL; import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -18,15 +19,15 @@ * Json model for nexus Search REST API response * */ -@JsonIgnoreProperties(value = { "continuationToken" }) +@JsonIgnoreProperties(value = { "totalCount", "from", "count", "tooManyResults", "collapsed", "repoDetails" }) public class NexusSearchResponse implements AbstractRESTSearchResponse { /** Logger instance. */ @JsonIgnore private static final Logger LOG = LoggerFactory.getLogger(NexusSearchResponse.class); - @JsonProperty("items") - private List items; + @JsonProperty("data") + private List data; @Override @JsonIgnore @@ -34,22 +35,30 @@ public List getDownloadURLs() throws MalformedURLException { List downloadLinks = new ArrayList<>(); - for (NexusSearchResponseItem item : this.items) { - for (NexusSearchResponseAsset asset : item.assets) { - downloadLinks.add(new URL(asset.downloadUrl)); - } + for (NexusSearchResponseData item : this.data) { + for (NexusSearchResponseArtifactHits artifactHit : item.artifactHits) { + for (NexusSearchResponeArtifactLinks artifactLink : artifactHit.artifactLinks) { + downloadLinks.add(AbstractRESTSearchResponse.createDownloadLink( + MavenSearchRepositoryConstants.NEXUS_REPOSITORY_URL + "/" + + MavenSearchRepositoryConstants.NEXUS_REPOSITORY_LINK, + item.getGroupId(), item.getArtifactId(), item.getVersion(), "." + artifactLink.getExtension())); + } + } } - return downloadLinks; + // removes duplicates + List newDownloadList = downloadLinks.stream().distinct().collect(Collectors.toList()); + + return newDownloadList; } @Override @JsonIgnore public String getJsonResponse(String repositoryUrl, String groupId) throws RESTSearchResponseException { - String targetLink = repositoryUrl + "/" + MavenSearchRepositoryConstants.NEXUS_TARGET_LINK - + "?repository=maven-central" + "&group=" + groupId; + String targetLink = repositoryUrl + "/" + MavenSearchRepositoryConstants.NEXUS_TARGET_LINK + "?_dc=" + + MavenSearchRepositoryConstants.NEXUS_DC_ID + "&q=" + groupId; LOG.info("Starting Nexus Search REST API request with URL: {}.", targetLink); String jsonResponse; @@ -65,38 +74,32 @@ public String getJsonResponse(String repositoryUrl, String groupId) throws RESTS * Nexus search response asset model * */ -@JsonIgnoreProperties(value = { "id", "format", "checksum" }) -class NexusSearchResponseAsset { +@JsonIgnoreProperties(value = { "repositoryId" }) +class NexusSearchResponseArtifactHits { /** - * downloadUrl + * artifactLinks */ - @JsonProperty("downloadUrl") - public String downloadUrl; + @JsonProperty("artifactLinks") + public List artifactLinks; - /** - * path - */ - @JsonProperty("path") - public String path; +} - /** - * repository - */ - @JsonProperty("repository") - public String repository; +class NexusSearchResponeArtifactLinks { - /** - * latest version - */ - @JsonProperty("latestVersion") - public String latestVersion; + @JsonProperty("extension") + private String extension; /** - * repositoryId + * @return extension */ - @JsonProperty("repositoryId") - public String repositoryId; + public String getExtension() { + + return this.extension; + } + + @JsonProperty("classifier") + private String classifier; } /** @@ -104,26 +107,44 @@ class NexusSearchResponseAsset { * Nexus search response item model * */ -@JsonIgnoreProperties(value = { "id", "format" }) -class NexusSearchResponseItem { +@JsonIgnoreProperties(value = { "latestRelease", "latestReleaseRepositoryId", "highlightedFragment" }) +class NexusSearchResponseData { /** - * repository + * groupId */ - @JsonProperty("repository") - private String repository; + @JsonProperty("groupId") + private String groupId; /** - * group + * @return groupId */ - @JsonProperty("group") - private String group; + public String getGroupId() { + + return this.groupId; + } /** - * name + * @return artifactId */ - @JsonProperty("name") - private String name; + public String getArtifactId() { + + return this.artifactId; + } + + /** + * @return version + */ + public String getVersion() { + + return this.version; + } + + /** + * artifactId + */ + @JsonProperty("artifactId") + private String artifactId; /** * version @@ -132,8 +153,9 @@ class NexusSearchResponseItem { private String version; /** - * assets + * artifactHits */ - @JsonProperty("assets") - public List assets; + @JsonProperty("artifactHits") + public List artifactHits; + } diff --git a/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java b/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java index a117a3ba06..903d5faf7c 100644 --- a/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java +++ b/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java @@ -42,8 +42,8 @@ public void testMavenParseDownloadLinks() throws IOException { List downloadLinks = response.getDownloadURLs(); assertThat(downloadLinks).contains( new URL("https://repo1.maven.org/maven2/com/google/inject/guice/5.1.0/guice-5.1.0.jar"), - new URL("https://repo1.maven.org/maven2/com/google/inject/guice-bom/5.1.0/guice-bom-5.1.0.jar"), - new URL("https://repo1.maven.org/maven2/com/google/inject/guice-parent/5.1.0/guice-parent-5.1.0.jar"), + new URL("https://repo1.maven.org/maven2/com/google/inject/guice-bom/5.1.0/guice-bom-5.1.0.pom"), + new URL("https://repo1.maven.org/maven2/com/google/inject/guice-parent/5.1.0/guice-parent-5.1.0.pom"), new URL("https://repo1.maven.org/maven2/com/google/inject/jdk8-tests/5.0.1/jdk8-tests-5.0.1.jar")); } @@ -63,9 +63,21 @@ public void testNexusParseDownloadLinks() throws IOException { response = mapper.readValue(jsonResponse, NexusSearchResponse.class); List downloadLinks = response.getDownloadURLs(); assertThat(downloadLinks).contains(new URL( - "http://localhost:8081/repository/maven-central/org/osgi/org.osgi.core/4.3.1/org.osgi.core-4.3.1-sources.jar"), - new URL("http://localhost:8081/repository/maven-central/org/osgi/org.osgi.core/4.3.1/org.osgi.core-4.3.1.jar"), - new URL("http://localhost:8081/repository/maven-central/org/osgi/org.osgi.core/4.3.1/org.osgi.core-4.3.1.pom")); + "https://s01.oss.sonatype.org/service/local/repositories/releases/content/com/devonfw/cobigen/openapiplugin/2021.12.006/openapiplugin-2021.12.006.pom"), + new URL( + "https://s01.oss.sonatype.org/service/local/repositories/releases/content/com/devonfw/cobigen/openapiplugin/2021.12.006/openapiplugin-2021.12.006.jar"), + new URL( + "https://s01.oss.sonatype.org/service/local/repositories/releases/content/com/devonfw/cobigen/openapiplugin/2021.12.005/openapiplugin-2021.12.005.pom"), + new URL( + "https://s01.oss.sonatype.org/service/local/repositories/releases/content/com/devonfw/cobigen/openapiplugin/2021.12.005/openapiplugin-2021.12.005.jar"), + new URL( + "https://s01.oss.sonatype.org/service/local/repositories/releases/content/com/devonfw/cobigen/jsonplugin/2021.12.006/jsonplugin-2021.12.006.pom"), + new URL( + "https://s01.oss.sonatype.org/service/local/repositories/releases/content/com/devonfw/cobigen/jsonplugin/2021.12.006/jsonplugin-2021.12.006.jar"), + new URL( + "https://s01.oss.sonatype.org/service/local/repositories/releases/content/com/devonfw/cobigen/jsonplugin/2021.12.005/jsonplugin-2021.12.005.pom"), + new URL( + "https://s01.oss.sonatype.org/service/local/repositories/releases/content/com/devonfw/cobigen/jsonplugin/2021.12.005/jsonplugin-2021.12.005.jar")); } /** @@ -103,8 +115,8 @@ public void testMavenSearchRequestGetsValidDownloadLinks() throws IOException { assertThat(downloadList).contains( new URL("https://repo1.maven.org/maven2/com/google/inject/guice/5.1.0/guice-5.1.0.jar"), - new URL("https://repo1.maven.org/maven2/com/google/inject/guice-bom/5.1.0/guice-bom-5.1.0.jar"), - new URL("https://repo1.maven.org/maven2/com/google/inject/guice-parent/5.1.0/guice-parent-5.1.0.jar"), + new URL("https://repo1.maven.org/maven2/com/google/inject/guice-bom/5.1.0/guice-bom-5.1.0.pom"), + new URL("https://repo1.maven.org/maven2/com/google/inject/guice-parent/5.1.0/guice-parent-5.1.0.pom"), new URL("https://repo1.maven.org/maven2/com/google/inject/jdk8-tests/5.0.1/jdk8-tests-5.0.1.jar")); } @@ -118,13 +130,10 @@ public void testNexusSearchRequestGetsValidDownloadLinks() throws IOException { List downloadList; - downloadList = MavenUtil.getMavenArtifactsByGroupId(MavenSearchRepositoryType.nexus, "com.google.inject"); + downloadList = MavenUtil.getMavenArtifactsByGroupId(MavenSearchRepositoryType.nexus, "com.devonfw.cobigen"); - assertThat(downloadList).contains( - new URL("https://repo1.maven.org/maven2/com/google/inject/guice/5.1.0/guice-5.1.0.jar"), - new URL("https://repo1.maven.org/maven2/com/google/inject/guice-bom/5.1.0/guice-bom-5.1.0.jar"), - new URL("https://repo1.maven.org/maven2/com/google/inject/guice-parent/5.1.0/guice-parent-5.1.0.jar"), - new URL("https://repo1.maven.org/maven2/com/google/inject/jdk8-tests/5.0.1/jdk8-tests-5.0.1.jar")); + assertThat(downloadList).contains(new URL( + "https://s01.oss.sonatype.org/service/local/repositories/releases/content/com/devonfw/cobigen/openapiplugin/2021.12.006/openapiplugin-2021.12.006.jar")); } /** diff --git a/cobigen/cobigen-core-api/src/test/resources/testdata/unittest/MavenUtilTest/nexusJsonTest.json b/cobigen/cobigen-core-api/src/test/resources/testdata/unittest/MavenUtilTest/nexusJsonTest.json index 4488061d96..73b88a9376 100644 --- a/cobigen/cobigen-core-api/src/test/resources/testdata/unittest/MavenUtilTest/nexusJsonTest.json +++ b/cobigen/cobigen-core-api/src/test/resources/testdata/unittest/MavenUtilTest/nexusJsonTest.json @@ -1,42 +1,135 @@ { - "items" : [ { - "id" : "bWF2ZW4tY2VudHJhbDoyZTQ3ZGRhMGYxYjU1NWUwNzE1OWRjOWY5ZGQzZmVmNA", - "repository" : "maven-central", - "format" : "maven2", - "group" : "org.osgi", - "name" : "org.osgi.core", - "version" : "4.3.1", - "assets" : [ { - "downloadUrl" : "http://localhost:8081/repository/maven-central/org/osgi/org.osgi.core/4.3.1/org.osgi.core-4.3.1-sources.jar", - "path" : "org/osgi/org.osgi.core/4.3.1/org.osgi.core-4.3.1-sources.jar", - "id" : "bWF2ZW4tY2VudHJhbDplMDE4OGVkMDcyOGZhNjhmNDExNzU2OGU1MjQ2NjZiYg", - "repository" : "maven-central", - "format" : "maven2", - "checksum" : { - "sha1" : "80bfafcf783988442b3a58318face1d2132db33d", - "md5" : "87ee0258b79dc852626b91818316b9c3" + "totalCount":305, + "from":-1, + "count":-1, + "tooManyResults":false, + "collapsed":false, + "repoDetails":[ + { + "repositoryId":"releases", + "repositoryName":"Releases", + "repositoryContentClass":"maven2", + "repositoryKind":"hosted", + "repositoryPolicy":"RELEASE", + "repositoryURL":"https://s01.oss.sonatype.org/service/local/repositories/releases" } - }, { - "downloadUrl" : "http://localhost:8081/repository/maven-central/org/osgi/org.osgi.core/4.3.1/org.osgi.core-4.3.1.jar", - "path" : "org/osgi/org.osgi.core/4.3.1/org.osgi.core-4.3.1.jar", - "id" : "bWF2ZW4tY2VudHJhbDpkMDY0ODA0YThlZDVhZDZlNjhmZGU5MWNmM2NiZTgzMw", - "repository" : "maven-central", - "format" : "maven2", - "checksum" : { - "sha1" : "5458ffe2ba049e76c29f2df2dc3ffccddf8b839e", - "md5" : "8053bbc1b55d51f5abae005625209d08" + ], + "data":[ + { + "groupId":"com.devonfw.cobigen", + "artifactId":"openapiplugin", + "version":"2021.12.006", + "latestRelease":"2021.12.006", + "latestReleaseRepositoryId":"releases", + "highlightedFragment":"
Group ID
  • com<\/B>.devonfw<\/B>.cobigen<\/B><\/LI><\/UL><\/blockquote>", + "artifactHits":[ + { + "repositoryId":"releases", + "artifactLinks":[ + { + "extension":"pom" + }, + { + "extension":"jar" + }, + { + "classifier":"javadoc", + "extension":"jar" + }, + { + "classifier":"sources", + "extension":"jar" + } + ] + } + ] + }, + { + "groupId":"com.devonfw.cobigen", + "artifactId":"openapiplugin", + "version":"2021.12.005", + "latestRelease":"2021.12.006", + "latestReleaseRepositoryId":"releases", + "highlightedFragment":"
    Group ID
    • com<\/B>.devonfw<\/B>.cobigen<\/B><\/LI><\/UL><\/blockquote>", + "artifactHits":[ + { + "repositoryId":"releases", + "artifactLinks":[ + { + "extension":"pom" + }, + { + "extension":"jar" + }, + { + "classifier":"javadoc", + "extension":"jar" + }, + { + "classifier":"sources", + "extension":"jar" + } + ] + } + ] + }, + { + "groupId":"com.devonfw.cobigen", + "artifactId":"jsonplugin", + "version":"2021.12.006", + "latestRelease":"2021.12.006", + "latestReleaseRepositoryId":"releases", + "highlightedFragment":"
      Group ID
      • com<\/B>.devonfw<\/B>.cobigen<\/B><\/LI><\/UL><\/blockquote>", + "artifactHits":[ + { + "repositoryId":"releases", + "artifactLinks":[ + { + "extension":"pom" + }, + { + "extension":"jar" + }, + { + "classifier":"javadoc", + "extension":"jar" + }, + { + "classifier":"sources", + "extension":"jar" + } + ] + } + ] + }, + { + "groupId":"com.devonfw.cobigen", + "artifactId":"jsonplugin", + "version":"2021.12.005", + "latestRelease":"2021.12.006", + "latestReleaseRepositoryId":"releases", + "highlightedFragment":"
        Group ID
        • com<\/B>.devonfw<\/B>.cobigen<\/B><\/LI><\/UL><\/blockquote>", + "artifactHits":[ + { + "repositoryId":"releases", + "artifactLinks":[ + { + "extension":"pom" + }, + { + "classifier":"javadoc", + "extension":"jar" + }, + { + "extension":"jar" + }, + { + "classifier":"sources", + "extension":"jar" + } + ] + } + ] } - }, { - "downloadUrl" : "http://localhost:8081/repository/maven-central/org/osgi/org.osgi.core/4.3.1/org.osgi.core-4.3.1.pom", - "path" : "org/osgi/org.osgi.core/4.3.1/org.osgi.core-4.3.1.pom", - "id" : "bWF2ZW4tY2VudHJhbDo2NTRiYjdkMGE1OTIxMzg1OWZhMTVkMzNmYWU1ZmY3OA", - "repository" : "maven-central", - "format" : "maven2", - "checksum" : { - "sha1" : "79391fc69dd72ad1fd983d01b4572f93f644882b", - "md5" : "3d87a59bcdb4b131d9a63e87e0ed924a" - } - } ] - } ], - "continuationToken" : null + ] } \ No newline at end of file From de019bafc15fc97f002b0958469a0797ea81a4ff Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Tue, 9 Aug 2022 16:48:46 +0200 Subject: [PATCH 09/42] #1529 disabled jfrog REST API test --- .../src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java b/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java index 903d5faf7c..93001051eb 100644 --- a/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java +++ b/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java @@ -8,6 +8,7 @@ import java.nio.file.Paths; import java.util.List; +import org.junit.Ignore; import org.junit.Test; import com.devonfw.cobigen.api.constants.MavenSearchRepositoryType; @@ -137,11 +138,12 @@ public void testNexusSearchRequestGetsValidDownloadLinks() throws IOException { } /** - * Tests if a request to nexus search REST API returns a list of download URLs + * Tests if a request to jfrog search REST API returns a list of download URLs * * @throws IOException */ @Test + @Ignore // TODO: remove when jfrog URLs are clear public void testJfrogSearchRequestGetsValidDownloadLinks() throws IOException { List downloadList; From fc3049a6bf5a915d6078fa56b074c12b92d6b769 Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Fri, 12 Aug 2022 01:18:20 +0200 Subject: [PATCH 10/42] #1529 added optimizations + authentication added bearer token authentication added bearer token to jfrog renamed nexus to nexus2 repository type added nexus3 repository type added more tests for code coverage --- cobigen/cobigen-core-api/pom.xml | 6 + .../MavenSearchRepositoryConstants.java | 28 +++-- .../constants/MavenSearchRepositoryType.java | 9 +- .../devonfw/cobigen/api/util/MavenUtil.java | 23 +++- .../util/to/AbstractRESTSearchResponse.java | 98 +++++++++++---- .../api/util/to/JfrogSearchResponse.java | 10 +- .../api/util/to/MavenSearchResponse.java | 10 +- ...esponse.java => Nexus2SearchResponse.java} | 44 ++++--- .../api/util/to/Nexus3SearchResponse.java | 104 ++++++++++++++++ .../devonfw/cobigen/api/MavenUtilTest.java | 114 +++++++++++++++--- ...nexusJsonTest.json => nexus2JsonTest.json} | 0 .../MavenUtilTest/nexus3JsonTest.json | 42 +++++++ 12 files changed, 412 insertions(+), 76 deletions(-) rename cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/{NexusSearchResponse.java => Nexus2SearchResponse.java} (69%) create mode 100644 cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/Nexus3SearchResponse.java rename cobigen/cobigen-core-api/src/test/resources/testdata/unittest/MavenUtilTest/{nexusJsonTest.json => nexus2JsonTest.json} (100%) create mode 100644 cobigen/cobigen-core-api/src/test/resources/testdata/unittest/MavenUtilTest/nexus3JsonTest.json diff --git a/cobigen/cobigen-core-api/pom.xml b/cobigen/cobigen-core-api/pom.xml index bebcce3d78..50672772e4 100644 --- a/cobigen/cobigen-core-api/pom.xml +++ b/cobigen/cobigen-core-api/pom.xml @@ -64,6 +64,12 @@ jersey-hk2 3.0.5 + + + org.glassfish.jersey.security + oauth2-client + 3.0.5 + diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/constants/MavenSearchRepositoryConstants.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/constants/MavenSearchRepositoryConstants.java index c1146a562d..815fa7436e 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/constants/MavenSearchRepositoryConstants.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/constants/MavenSearchRepositoryConstants.java @@ -27,29 +27,39 @@ public class MavenSearchRepositoryConstants { public static int MAVEN_MAX_RESPONSE_ROWS = 20; /** - * Nexus repository URL + * Nexus2 repository URL */ - public static String NEXUS_REPOSITORY_URL = "https://s01.oss.sonatype.org"; + public static String NEXUS2_REPOSITORY_URL = "https://s01.oss.sonatype.org"; /** - * Nexus repository link + * Nexus2 repository link */ - public static String NEXUS_REPOSITORY_LINK = "service/local/repositories/releases/content"; + public static String NEXUS2_REPOSITORY_LINK = "service/local/repositories/releases/content"; /** - * Nexus target link + * Nexus2 target link */ - public static String NEXUS_TARGET_LINK = "service/local/lucene/search"; + public static String NEXUS2_TARGET_LINK = "service/local/lucene/search"; /** - * Nexus connect ID + * Nexus2 connect ID */ - public static String NEXUS_DC_ID = "1660043999867"; + public static String NEXUS2_DC_ID = "1660043999867"; + + /** + * Nexus3 target link + */ + public static String NEXUS3_TARGET_LINK = "service/rest/v1/search"; + + /** + * Nexus3 repository URL + */ + public static String NEXUS3_REPOSITORY_URL = ""; /** * Jfrog repository URL */ - public static String JFROG_REPOSITORY_URL = ""; + public static String JFROG_REPOSITORY_URL = "http://localhost:8082/artifactory"; /** * Jfrog target link diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/constants/MavenSearchRepositoryType.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/constants/MavenSearchRepositoryType.java index d00a5e0661..a25b5288f5 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/constants/MavenSearchRepositoryType.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/constants/MavenSearchRepositoryType.java @@ -7,9 +7,14 @@ public enum MavenSearchRepositoryType { /** - * Nexus search repository type + * Nexus2 search repository type */ - nexus, + nexus2, + + /** + * Nexus3 search repository type + */ + nexus3, /** * Maven search repository type diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/MavenUtil.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/MavenUtil.java index 1680cdfc9e..81499401fd 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/MavenUtil.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/MavenUtil.java @@ -348,15 +348,30 @@ public static Path getProjectRoot(Path inputFile, boolean topLevel) { /** * Gets a list of download URLs by groupId from the specified repository search REST API * - * @param repositoryType String of the type of the repository e.g. maven, jfrog, nexus + * @param repositoryType String of the type of the repository e.g. maven, jfrog, nexus2, nexus3 * @param groupId the groupId to search for - * - * @return List of artifact URLS + * @return List of artifact download URLS */ public static List getMavenArtifactsByGroupId(MavenSearchRepositoryType repositoryType, String groupId) { + return getMavenArtifactsByGroupId(repositoryType, groupId, null); + + } + + /** + * Gets a list of download URLs by groupId from the specified repository search REST API using authentication with + * bearer token + * + * @param repositoryType String of the type of the repository e.g. maven, jfrog, nexus2, nexus3 + * @param groupId the groupId to search for + * @param authToken bearer token to use for authentication + * @return List of artifact download URLS + */ + public static List getMavenArtifactsByGroupId(MavenSearchRepositoryType repositoryType, String groupId, + String authToken) { + try { - return AbstractRESTSearchResponse.getArtifactDownloadLinks(repositoryType, groupId); + return AbstractRESTSearchResponse.getArtifactDownloadLinks(repositoryType, groupId, authToken); } catch (RESTSearchResponseException | JsonProcessingException | MalformedURLException e) { throw new CobiGenRuntimeException("Unable to get artifacts from " + repositoryType + " by groupId " + groupId, e); } diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractRESTSearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractRESTSearchResponse.java index f21611470e..4418df01d7 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractRESTSearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractRESTSearchResponse.java @@ -2,9 +2,10 @@ import java.net.MalformedURLException; import java.net.URL; -import java.util.ArrayList; import java.util.List; +import org.glassfish.jersey.client.oauth2.OAuth2ClientSupport; + import com.devonfw.cobigen.api.constants.MavenSearchRepositoryConstants; import com.devonfw.cobigen.api.constants.MavenSearchRepositoryType; import com.devonfw.cobigen.api.exception.RESTSearchResponseException; @@ -12,10 +13,12 @@ import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; +import jakarta.ws.rs.ProcessingException; import jakarta.ws.rs.client.Client; import jakarta.ws.rs.client.ClientBuilder; import jakarta.ws.rs.client.Invocation; import jakarta.ws.rs.client.WebTarget; +import jakarta.ws.rs.core.Feature; import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.Response; @@ -43,12 +46,24 @@ public interface AbstractRESTSearchResponse { */ public String getJsonResponse(String repositoryUrl, String groupId) throws RESTSearchResponseException; + /** + * Gets the json response using bearer authentication token + * + * @param repositoryUrl URL of the repository + * @param groupId to search for + * @param authToken bearer token to use for authentication + * @return String of json response + * @throws RESTSearchResponseException if the request did not return status 200 + */ + public String getJsonResponse(String repositoryUrl, String groupId, String authToken) + throws RESTSearchResponseException; + /** * Gets the download links by given repository type * * @param repositoryType String of the type of the repository e.g. maven, jfrog, nexus * @param groupId the groupId to search for - * + * @param authToken bearer token to use for authentication * @return List of download links * @throws RESTSearchResponseException if an error occurred * @throws JsonProcessingException if the json processing was not possible @@ -56,62 +71,99 @@ public interface AbstractRESTSearchResponse { * @throws MalformedURLException if an URL was malformed * */ - public static List getArtifactDownloadLinks(MavenSearchRepositoryType repositoryType, String groupId) + public static List getArtifactDownloadLinks(MavenSearchRepositoryType repositoryType, String groupId, + String authToken) throws RESTSearchResponseException, JsonMappingException, JsonProcessingException, MalformedURLException { - List downloadLinks = new ArrayList<>(); ObjectMapper mapper = new ObjectMapper(); String jsonResponse = ""; if (repositoryType == MavenSearchRepositoryType.maven) { MavenSearchResponse response = new MavenSearchResponse(); String mavenRepositoryURL = MavenSearchRepositoryConstants.MAVEN_REPOSITORY_URL; - jsonResponse = response.getJsonResponse(mavenRepositoryURL, groupId); + jsonResponse = response.getJsonResponse(mavenRepositoryURL, groupId, null); response = mapper.readValue(jsonResponse, MavenSearchResponse.class); - downloadLinks = response.getDownloadURLs(); - return downloadLinks; + return response.getDownloadURLs(); } if (repositoryType == MavenSearchRepositoryType.jfrog) { JfrogSearchResponse response = new JfrogSearchResponse(); String jfrogRepositoryURL = MavenSearchRepositoryConstants.JFROG_REPOSITORY_URL; - jsonResponse = response.getJsonResponse(jfrogRepositoryURL, groupId); + jsonResponse = response.getJsonResponse(jfrogRepositoryURL, groupId, authToken); response = mapper.readValue(jsonResponse, JfrogSearchResponse.class); - downloadLinks = response.getDownloadURLs(); - return downloadLinks; + return response.getDownloadURLs(); } - if (repositoryType == MavenSearchRepositoryType.nexus) { - NexusSearchResponse response = new NexusSearchResponse(); - String nexusRepositoryURL = MavenSearchRepositoryConstants.NEXUS_REPOSITORY_URL; - jsonResponse = response.getJsonResponse(nexusRepositoryURL, groupId); - response = mapper.readValue(jsonResponse, NexusSearchResponse.class); - downloadLinks = response.getDownloadURLs(); - return downloadLinks; + if (repositoryType == MavenSearchRepositoryType.nexus2) { + Nexus2SearchResponse response = new Nexus2SearchResponse(); + String nexusRepositoryURL = MavenSearchRepositoryConstants.NEXUS2_REPOSITORY_URL; + jsonResponse = response.getJsonResponse(nexusRepositoryURL, groupId, null); + response = mapper.readValue(jsonResponse, Nexus2SearchResponse.class); + return response.getDownloadURLs(); + } + + if (repositoryType == MavenSearchRepositoryType.nexus3) { + Nexus3SearchResponse response = new Nexus3SearchResponse(); + String nexusRepositoryURL = MavenSearchRepositoryConstants.NEXUS3_REPOSITORY_URL; + jsonResponse = response.getJsonResponse(nexusRepositoryURL, groupId, null); + response = mapper.readValue(jsonResponse, Nexus3SearchResponse.class); + return response.getDownloadURLs(); } return null; } /** - * Gets a json response by given REST API target link + * Creates a @WebTarget with provided authentication token + * + * @param targetLink link to get response from + * @param token bearer token to use for authentication + * @return WebTarget to use as resource + */ + public static WebTarget bearerAuthenticationWithOAuth2AtClientLevel(String targetLink, String token) { + + Feature feature = OAuth2ClientSupport.feature(token); + Client client = ClientBuilder.newBuilder().register(feature).build(); + + WebTarget target = client.target(targetLink); + return target; + } + + /** + * Gets a json response by given REST API target link using bearer authentication token * * @param targetLink link to get response from + * @param authToken bearer token to use for authentication * @return String of json response * @throws RESTSearchResponseException if the returned status code was not 200 OK */ - public static String getJsonResponseStringByTargetLink(String targetLink) throws RESTSearchResponseException { + public static String getJsonResponseStringByTargetLink(String targetLink, String authToken) + throws RESTSearchResponseException { - Client client = ClientBuilder.newClient(); - WebTarget target = client.target(targetLink); + WebTarget target = null; + + if (authToken != null) { + target = bearerAuthenticationWithOAuth2AtClientLevel(targetLink, authToken); + } else { + Client client = ClientBuilder.newClient(); + target = client.target(targetLink); + } + + Response response = null; Invocation.Builder request = target.request(MediaType.APPLICATION_JSON); - Response response = request.get(); + try { + response = request.get(); + } catch (ProcessingException e) { + throw new RESTSearchResponseException("The search REST API was not able to process the target URL: " + targetLink, + e); + } + int status = response.getStatus(); String jsonResponse = ""; if (status == 200) { jsonResponse = response.readEntity(String.class); } else { - throw new RESTSearchResponseException("The search REST API returned the unexpected status code", + throw new RESTSearchResponseException("The search REST API returned the unexpected status code: ", String.valueOf(status)); } return jsonResponse; diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/JfrogSearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/JfrogSearchResponse.java index e61f600f25..b41ede60ff 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/JfrogSearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/JfrogSearchResponse.java @@ -42,12 +42,20 @@ public List getResults() { @JsonIgnore public String getJsonResponse(String repositoryUrl, String groupId) throws RESTSearchResponseException { + return getJsonResponse(repositoryUrl, groupId, null); + } + + @Override + @JsonIgnore + public String getJsonResponse(String repositoryUrl, String groupId, String authToken) + throws RESTSearchResponseException { + String targetLink = repositoryUrl + "/" + MavenSearchRepositoryConstants.JFROG_TARGET_LINK + "?g=" + groupId; LOG.info("Starting Jfrog Search REST API request with URL: {}.", targetLink); String jsonResponse; - jsonResponse = AbstractRESTSearchResponse.getJsonResponseStringByTargetLink(targetLink); + jsonResponse = AbstractRESTSearchResponse.getJsonResponseStringByTargetLink(targetLink, authToken); return jsonResponse; } diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/MavenSearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/MavenSearchResponse.java index 0717fe711c..b6b99aab4c 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/MavenSearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/MavenSearchResponse.java @@ -41,6 +41,14 @@ public MavenSearchResponseResponse getResponse() { @JsonIgnore public String getJsonResponse(String repositoryUrl, String groupId) throws RESTSearchResponseException { + return getJsonResponse(repositoryUrl, groupId, null); + } + + @Override + @JsonIgnore + public String getJsonResponse(String repositoryUrl, String groupId, String authToken) + throws RESTSearchResponseException { + String targetLink = repositoryUrl + "/" + MavenSearchRepositoryConstants.MAVEN_TARGET_LINK + "?q=g:" + groupId + "&wt=json"; LOG.info("Starting Maven Search REST API request with URL: {}.", targetLink); @@ -53,7 +61,7 @@ public String getJsonResponse(String repositoryUrl, String groupId) throws RESTS String jsonResponse; - jsonResponse = AbstractRESTSearchResponse.getJsonResponseStringByTargetLink(targetLink); + jsonResponse = AbstractRESTSearchResponse.getJsonResponseStringByTargetLink(targetLink, authToken); return jsonResponse; diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/NexusSearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/Nexus2SearchResponse.java similarity index 69% rename from cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/NexusSearchResponse.java rename to cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/Nexus2SearchResponse.java index 004b08993f..719094c9df 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/NexusSearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/Nexus2SearchResponse.java @@ -16,18 +16,18 @@ import com.fasterxml.jackson.annotation.JsonProperty; /** - * Json model for nexus Search REST API response + * Json model for nexus2 Search REST API response * */ @JsonIgnoreProperties(value = { "totalCount", "from", "count", "tooManyResults", "collapsed", "repoDetails" }) -public class NexusSearchResponse implements AbstractRESTSearchResponse { +public class Nexus2SearchResponse implements AbstractRESTSearchResponse { /** Logger instance. */ @JsonIgnore - private static final Logger LOG = LoggerFactory.getLogger(NexusSearchResponse.class); + private static final Logger LOG = LoggerFactory.getLogger(Nexus2SearchResponse.class); @JsonProperty("data") - private List data; + private List data; @Override @JsonIgnore @@ -35,12 +35,12 @@ public List getDownloadURLs() throws MalformedURLException { List downloadLinks = new ArrayList<>(); - for (NexusSearchResponseData item : this.data) { - for (NexusSearchResponseArtifactHits artifactHit : item.artifactHits) { - for (NexusSearchResponeArtifactLinks artifactLink : artifactHit.artifactLinks) { + for (Nexus2SearchResponseData item : this.data) { + for (Nexus2SearchResponseArtifactHits artifactHit : item.artifactHits) { + for (Nexus2SearchResponeArtifactLinks artifactLink : artifactHit.artifactLinks) { downloadLinks.add(AbstractRESTSearchResponse.createDownloadLink( - MavenSearchRepositoryConstants.NEXUS_REPOSITORY_URL + "/" - + MavenSearchRepositoryConstants.NEXUS_REPOSITORY_LINK, + MavenSearchRepositoryConstants.NEXUS2_REPOSITORY_URL + "/" + + MavenSearchRepositoryConstants.NEXUS2_REPOSITORY_LINK, item.getGroupId(), item.getArtifactId(), item.getVersion(), "." + artifactLink.getExtension())); } @@ -57,13 +57,21 @@ public List getDownloadURLs() throws MalformedURLException { @JsonIgnore public String getJsonResponse(String repositoryUrl, String groupId) throws RESTSearchResponseException { - String targetLink = repositoryUrl + "/" + MavenSearchRepositoryConstants.NEXUS_TARGET_LINK + "?_dc=" - + MavenSearchRepositoryConstants.NEXUS_DC_ID + "&q=" + groupId; + return getJsonResponse(repositoryUrl, groupId, null); + } + + @Override + @JsonIgnore + public String getJsonResponse(String repositoryUrl, String groupId, String authToken) + throws RESTSearchResponseException { + + String targetLink = repositoryUrl + "/" + MavenSearchRepositoryConstants.NEXUS2_TARGET_LINK + "?_dc=" + + MavenSearchRepositoryConstants.NEXUS2_DC_ID + "&q=" + groupId; LOG.info("Starting Nexus Search REST API request with URL: {}.", targetLink); String jsonResponse; - jsonResponse = AbstractRESTSearchResponse.getJsonResponseStringByTargetLink(targetLink); + jsonResponse = AbstractRESTSearchResponse.getJsonResponseStringByTargetLink(targetLink, authToken); return jsonResponse; } @@ -75,17 +83,17 @@ public String getJsonResponse(String repositoryUrl, String groupId) throws RESTS * */ @JsonIgnoreProperties(value = { "repositoryId" }) -class NexusSearchResponseArtifactHits { +class Nexus2SearchResponseArtifactHits { /** * artifactLinks */ @JsonProperty("artifactLinks") - public List artifactLinks; + public List artifactLinks; } -class NexusSearchResponeArtifactLinks { +class Nexus2SearchResponeArtifactLinks { @JsonProperty("extension") private String extension; @@ -104,11 +112,11 @@ public String getExtension() { /** * - * Nexus search response item model + * Nexus2 search response item model * */ @JsonIgnoreProperties(value = { "latestRelease", "latestReleaseRepositoryId", "highlightedFragment" }) -class NexusSearchResponseData { +class Nexus2SearchResponseData { /** * groupId @@ -156,6 +164,6 @@ public String getVersion() { * artifactHits */ @JsonProperty("artifactHits") - public List artifactHits; + public List artifactHits; } diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/Nexus3SearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/Nexus3SearchResponse.java new file mode 100644 index 0000000000..21e7d37da2 --- /dev/null +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/Nexus3SearchResponse.java @@ -0,0 +1,104 @@ +package com.devonfw.cobigen.api.util.to; + +import java.net.MalformedURLException; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.devonfw.cobigen.api.constants.MavenSearchRepositoryConstants; +import com.devonfw.cobigen.api.exception.RESTSearchResponseException; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * Json model for nexus3 Search REST API response + * + */ +@JsonIgnoreProperties(value = { "continuationToken" }) +public class Nexus3SearchResponse implements AbstractRESTSearchResponse { + + /** Logger instance. */ + @JsonIgnore + private static final Logger LOG = LoggerFactory.getLogger(Nexus3SearchResponse.class); + + @JsonProperty("items") + private List items; + + @Override + @JsonIgnore + public List getDownloadURLs() throws MalformedURLException { + + List downloadLinks = new ArrayList<>(); + + for (Nexus3SearchResponseItem item : this.items) { + for (Nexus3SearchResponseAsset asset : item.assets) { + downloadLinks.add(new URL(asset.downloadUrl)); + } + } + + // removes duplicates + List newDownloadList = downloadLinks.stream().distinct().collect(Collectors.toList()); + + return newDownloadList; + } + + @Override + @JsonIgnore + public String getJsonResponse(String repositoryUrl, String groupId) throws RESTSearchResponseException { + + return getJsonResponse(repositoryUrl, groupId, null); + } + + @Override + @JsonIgnore + public String getJsonResponse(String repositoryUrl, String groupId, String authToken) + throws RESTSearchResponseException { + + String targetLink = repositoryUrl + "/" + MavenSearchRepositoryConstants.NEXUS3_TARGET_LINK + + "?repository=maven-central" + "&group=" + groupId; + LOG.info("Starting Nexus Search REST API request with URL: {}.", targetLink); + + String jsonResponse; + + jsonResponse = AbstractRESTSearchResponse.getJsonResponseStringByTargetLink(targetLink, authToken); + + return jsonResponse; + } +} + +/** + * + * Nexus3 search response asset model + * + */ +@JsonIgnoreProperties(value = { "path", "id", "repository", "format", "checksum" }) +class Nexus3SearchResponseAsset { + + /** + * downloadUrl + */ + @JsonProperty("downloadUrl") + public String downloadUrl; + +} + +/** + * + * Nexus3 search response item model + * + */ +@JsonIgnoreProperties(value = { "id", "repository", "format", "group", "name", "version" }) +class Nexus3SearchResponseItem { + + /** + * artifactHits + */ + @JsonProperty("assets") + public List assets; + +} diff --git a/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java b/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java index 93001051eb..7db46f5a3c 100644 --- a/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java +++ b/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java @@ -12,10 +12,13 @@ import org.junit.Test; import com.devonfw.cobigen.api.constants.MavenSearchRepositoryType; +import com.devonfw.cobigen.api.exception.RESTSearchResponseException; import com.devonfw.cobigen.api.util.MavenUtil; +import com.devonfw.cobigen.api.util.to.AbstractRESTSearchResponse; import com.devonfw.cobigen.api.util.to.JfrogSearchResponse; import com.devonfw.cobigen.api.util.to.MavenSearchResponse; -import com.devonfw.cobigen.api.util.to.NexusSearchResponse; +import com.devonfw.cobigen.api.util.to.Nexus2SearchResponse; +import com.devonfw.cobigen.api.util.to.Nexus3SearchResponse; import com.fasterxml.jackson.databind.ObjectMapper; /** @@ -26,6 +29,43 @@ public class MavenUtilTest { /** Testdata root path */ private static final String testdataRoot = "src/test/resources/testdata/unittest/MavenUtilTest"; + /** + * Tests if a wrong repository type returns null + */ + @Test + public void testWrongRepositoryTypeReturnsNull() { + + assertThat(MavenUtil.getMavenArtifactsByGroupId(null, testdataRoot)).isNull(); + } + + /** + * Tests if an exception gets thrown when a faulty target link without a token was used + */ + @Test(expected = RESTSearchResponseException.class) + public void testWrongTargetLinkThrowsException() { + + AbstractRESTSearchResponse.getJsonResponseStringByTargetLink("this/is/not/a/link", null); + } + + /** + * Tests if an exception gets thrown when a faulty target link and a token was used + */ + @Test(expected = RESTSearchResponseException.class) + public void testWrongTargetLinkAndTokenThrowsException() { + + AbstractRESTSearchResponse.getJsonResponseStringByTargetLink("this/is/not/a/link", "thisisabadtoken"); + } + + /** + * Tests if an exception gets thrown when a status code was not 200 + */ + @Test(expected = RESTSearchResponseException.class) + public void testWrongResponseStatusCodeThrowsException() { + + AbstractRESTSearchResponse.getJsonResponseStringByTargetLink("https://search.maven.org/solrsearch/select?test", + null); + } + /** * Tests if maven json response can properly be parsed and converted to a list of download URLs * @@ -49,19 +89,19 @@ public void testMavenParseDownloadLinks() throws IOException { } /** - * Tests if nexus json response can properly be parsed and converted to a list of download URLs + * Tests if nexus2 json response can properly be parsed and converted to a list of download URLs * * @throws IOException */ @Test - public void testNexusParseDownloadLinks() throws IOException { + public void testNexus2ParseDownloadLinks() throws IOException { ObjectMapper mapper = new ObjectMapper(); - NexusSearchResponse response = new NexusSearchResponse(); + Nexus2SearchResponse response = new Nexus2SearchResponse(); - String jsonResponse = new String(Files.readAllBytes(Paths.get(testdataRoot).resolve("nexusJsonTest.json"))); + String jsonResponse = new String(Files.readAllBytes(Paths.get(testdataRoot).resolve("nexus2JsonTest.json"))); - response = mapper.readValue(jsonResponse, NexusSearchResponse.class); + response = mapper.readValue(jsonResponse, Nexus2SearchResponse.class); List downloadLinks = response.getDownloadURLs(); assertThat(downloadLinks).contains(new URL( "https://s01.oss.sonatype.org/service/local/repositories/releases/content/com/devonfw/cobigen/openapiplugin/2021.12.006/openapiplugin-2021.12.006.pom"), @@ -81,6 +121,27 @@ public void testNexusParseDownloadLinks() throws IOException { "https://s01.oss.sonatype.org/service/local/repositories/releases/content/com/devonfw/cobigen/jsonplugin/2021.12.005/jsonplugin-2021.12.005.jar")); } + /** + * Tests if nexus3 json response can properly be parsed and converted to a list of download URLs + * + * @throws IOException + */ + @Test + public void testNexus3ParseDownloadLinks() throws IOException { + + ObjectMapper mapper = new ObjectMapper(); + Nexus3SearchResponse response = new Nexus3SearchResponse(); + + String jsonResponse = new String(Files.readAllBytes(Paths.get(testdataRoot).resolve("nexus3JsonTest.json"))); + + response = mapper.readValue(jsonResponse, Nexus3SearchResponse.class); + List downloadLinks = response.getDownloadURLs(); + assertThat(downloadLinks).contains(new URL( + "http://localhost:8081/repository/maven-central/org/osgi/org.osgi.core/4.3.1/org.osgi.core-4.3.1-sources.jar"), + new URL("http://localhost:8081/repository/maven-central/org/osgi/org.osgi.core/4.3.1/org.osgi.core-4.3.1.jar"), + new URL("http://localhost:8081/repository/maven-central/org/osgi/org.osgi.core/4.3.1/org.osgi.core-4.3.1.pom")); + } + /** * Tests if jfrog json response can properly be parsed and converted to a list of download URLs * @@ -112,7 +173,7 @@ public void testMavenSearchRequestGetsValidDownloadLinks() throws IOException { List downloadList; - downloadList = MavenUtil.getMavenArtifactsByGroupId(MavenSearchRepositoryType.maven, "com.google.inject"); + downloadList = MavenUtil.getMavenArtifactsByGroupId(MavenSearchRepositoryType.maven, "com.google.inject", null); assertThat(downloadList).contains( new URL("https://repo1.maven.org/maven2/com/google/inject/guice/5.1.0/guice-5.1.0.jar"), @@ -122,39 +183,56 @@ public void testMavenSearchRequestGetsValidDownloadLinks() throws IOException { } /** - * Tests if a request to nexus search REST API returns a list of download URLs + * Tests if a request to nexus2 search REST API returns a list of download URLs * * @throws IOException */ @Test - public void testNexusSearchRequestGetsValidDownloadLinks() throws IOException { + public void testNexus2SearchRequestGetsValidDownloadLinks() throws IOException { List downloadList; - downloadList = MavenUtil.getMavenArtifactsByGroupId(MavenSearchRepositoryType.nexus, "com.devonfw.cobigen"); + downloadList = MavenUtil.getMavenArtifactsByGroupId(MavenSearchRepositoryType.nexus2, "com.devonfw.cobigen"); assertThat(downloadList).contains(new URL( "https://s01.oss.sonatype.org/service/local/repositories/releases/content/com/devonfw/cobigen/openapiplugin/2021.12.006/openapiplugin-2021.12.006.jar")); } + /** + * Tests if a request to nexus3 search REST API returns a list of download URLs + * + * @throws IOException + */ + @Test + @Ignore // TODO: remove when nexus3 URLs are testable + public void testNexus3SearchRequestGetsValidDownloadLinks() throws IOException { + + List downloadList; + + downloadList = MavenUtil.getMavenArtifactsByGroupId(MavenSearchRepositoryType.nexus3, "com.devonfw.cobigen"); + + assertThat(downloadList).contains(new URL( + "http://localhost:8081/repository/maven-central/org/osgi/org.osgi.core/4.3.1/org.osgi.core-4.3.1-sources.jar"), + new URL("http://localhost:8081/repository/maven-central/org/osgi/org.osgi.core/4.3.1/org.osgi.core-4.3.1.jar"), + new URL("http://localhost:8081/repository/maven-central/org/osgi/org.osgi.core/4.3.1/org.osgi.core-4.3.1.pom")); + } + /** * Tests if a request to jfrog search REST API returns a list of download URLs * * @throws IOException */ @Test - @Ignore // TODO: remove when jfrog URLs are clear + @Ignore // TODO: remove when jfrog URLs are testable public void testJfrogSearchRequestGetsValidDownloadLinks() throws IOException { List downloadList; - downloadList = MavenUtil.getMavenArtifactsByGroupId(MavenSearchRepositoryType.jfrog, "com.google.inject"); - - assertThat(downloadList).contains( - new URL("https://repo1.maven.org/maven2/com/google/inject/guice/5.1.0/guice-5.1.0.jar"), - new URL("https://repo1.maven.org/maven2/com/google/inject/guice-bom/5.1.0/guice-bom-5.1.0.jar"), - new URL("https://repo1.maven.org/maven2/com/google/inject/guice-parent/5.1.0/guice-parent-5.1.0.jar"), - new URL("https://repo1.maven.org/maven2/com/google/inject/jdk8-tests/5.0.1/jdk8-tests-5.0.1.jar")); + downloadList = MavenUtil.getMavenArtifactsByGroupId(MavenSearchRepositoryType.jfrog, "com.devonfw.cobigen", null); + assertThat(downloadList).contains(new URL( + "https://localjfrog.com/artifactory/api/storage/maven-remote-cache/com/devonfw/cobigen/cli-parent/2021.04.001-SNAPSHOT/cli-parent-2021.04.001-SNAPSHOT.pom"), + new URL( + "https://localjfrog.com/artifactory/api/storage/maven-remote-cache/com/devonfw/cobigen/cli-parent/2021.08.002-SNAPSHOT/cli-parent-2021.08.002-SNAPSHOT.pom")); } } diff --git a/cobigen/cobigen-core-api/src/test/resources/testdata/unittest/MavenUtilTest/nexusJsonTest.json b/cobigen/cobigen-core-api/src/test/resources/testdata/unittest/MavenUtilTest/nexus2JsonTest.json similarity index 100% rename from cobigen/cobigen-core-api/src/test/resources/testdata/unittest/MavenUtilTest/nexusJsonTest.json rename to cobigen/cobigen-core-api/src/test/resources/testdata/unittest/MavenUtilTest/nexus2JsonTest.json diff --git a/cobigen/cobigen-core-api/src/test/resources/testdata/unittest/MavenUtilTest/nexus3JsonTest.json b/cobigen/cobigen-core-api/src/test/resources/testdata/unittest/MavenUtilTest/nexus3JsonTest.json new file mode 100644 index 0000000000..4488061d96 --- /dev/null +++ b/cobigen/cobigen-core-api/src/test/resources/testdata/unittest/MavenUtilTest/nexus3JsonTest.json @@ -0,0 +1,42 @@ +{ + "items" : [ { + "id" : "bWF2ZW4tY2VudHJhbDoyZTQ3ZGRhMGYxYjU1NWUwNzE1OWRjOWY5ZGQzZmVmNA", + "repository" : "maven-central", + "format" : "maven2", + "group" : "org.osgi", + "name" : "org.osgi.core", + "version" : "4.3.1", + "assets" : [ { + "downloadUrl" : "http://localhost:8081/repository/maven-central/org/osgi/org.osgi.core/4.3.1/org.osgi.core-4.3.1-sources.jar", + "path" : "org/osgi/org.osgi.core/4.3.1/org.osgi.core-4.3.1-sources.jar", + "id" : "bWF2ZW4tY2VudHJhbDplMDE4OGVkMDcyOGZhNjhmNDExNzU2OGU1MjQ2NjZiYg", + "repository" : "maven-central", + "format" : "maven2", + "checksum" : { + "sha1" : "80bfafcf783988442b3a58318face1d2132db33d", + "md5" : "87ee0258b79dc852626b91818316b9c3" + } + }, { + "downloadUrl" : "http://localhost:8081/repository/maven-central/org/osgi/org.osgi.core/4.3.1/org.osgi.core-4.3.1.jar", + "path" : "org/osgi/org.osgi.core/4.3.1/org.osgi.core-4.3.1.jar", + "id" : "bWF2ZW4tY2VudHJhbDpkMDY0ODA0YThlZDVhZDZlNjhmZGU5MWNmM2NiZTgzMw", + "repository" : "maven-central", + "format" : "maven2", + "checksum" : { + "sha1" : "5458ffe2ba049e76c29f2df2dc3ffccddf8b839e", + "md5" : "8053bbc1b55d51f5abae005625209d08" + } + }, { + "downloadUrl" : "http://localhost:8081/repository/maven-central/org/osgi/org.osgi.core/4.3.1/org.osgi.core-4.3.1.pom", + "path" : "org/osgi/org.osgi.core/4.3.1/org.osgi.core-4.3.1.pom", + "id" : "bWF2ZW4tY2VudHJhbDo2NTRiYjdkMGE1OTIxMzg1OWZhMTVkMzNmYWU1ZmY3OA", + "repository" : "maven-central", + "format" : "maven2", + "checksum" : { + "sha1" : "79391fc69dd72ad1fd983d01b4572f93f644882b", + "md5" : "3d87a59bcdb4b131d9a63e87e0ed924a" + } + } ] + } ], + "continuationToken" : null +} \ No newline at end of file From 8a8f774ea580fd2aaddf21518d790e41218c492b Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Fri, 12 Aug 2022 16:34:02 +0200 Subject: [PATCH 11/42] #1529 applied factory pattern added new SearchResponse interface made all SearchResponse types inherit from SearchResponse added new SearchResponseFactory refactored getArtifactDownloadLinks method added new getAvailableSearchInterfaces method (used to register new search interfaces) added getRepositoryType to SearchResponse parent class (returns the type of repository as an enum) --- .../RESTSearchResponseException.java | 4 +- .../devonfw/cobigen/api/util/MavenUtil.java | 25 +--- .../api/util/to/JfrogSearchResponse.java | 13 ++- .../api/util/to/MavenSearchResponse.java | 19 ++- .../api/util/to/Nexus2SearchResponse.java | 15 ++- .../api/util/to/Nexus3SearchResponse.java | 13 ++- .../cobigen/api/util/to/SearchResponse.java | 50 ++++++++ ...sponse.java => SearchResponseFactory.java} | 110 +++++++----------- .../devonfw/cobigen/api/MavenUtilTest.java | 40 ++++--- 9 files changed, 164 insertions(+), 125 deletions(-) create mode 100644 cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponse.java rename cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/{AbstractRESTSearchResponse.java => SearchResponseFactory.java} (55%) diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/exception/RESTSearchResponseException.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/exception/RESTSearchResponseException.java index 546a967d13..7f7e030b0a 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/exception/RESTSearchResponseException.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/exception/RESTSearchResponseException.java @@ -29,12 +29,12 @@ public RESTSearchResponseException(String message, Throwable cause) { /** * Creates a new {@link RESTSearchResponseException} with the specified message and the causing {@link Throwable} * - * @param statusCode status code causing the {@link RESTSearchResponseException} or null if not available * @param message describing the exception + * @param statusCode status code causing the {@link RESTSearchResponseException} or null if not available */ public RESTSearchResponseException(String message, String statusCode) { - super((statusCode != null ? statusCode + ":\n" : "") + message); + super(message + statusCode); } } diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/MavenUtil.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/MavenUtil.java index 81499401fd..9bce532595 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/MavenUtil.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/MavenUtil.java @@ -26,10 +26,9 @@ import org.zeroturnaround.exec.stream.slf4j.Slf4jStream; import com.devonfw.cobigen.api.constants.MavenConstants; -import com.devonfw.cobigen.api.constants.MavenSearchRepositoryType; import com.devonfw.cobigen.api.exception.CobiGenRuntimeException; import com.devonfw.cobigen.api.exception.RESTSearchResponseException; -import com.devonfw.cobigen.api.util.to.AbstractRESTSearchResponse; +import com.devonfw.cobigen.api.util.to.SearchResponseFactory; import com.fasterxml.jackson.core.JsonProcessingException; import com.google.common.collect.Lists; import com.google.common.hash.Hashing; @@ -345,35 +344,21 @@ public static Path getProjectRoot(Path inputFile, boolean topLevel) { return null; } - /** - * Gets a list of download URLs by groupId from the specified repository search REST API - * - * @param repositoryType String of the type of the repository e.g. maven, jfrog, nexus2, nexus3 - * @param groupId the groupId to search for - * @return List of artifact download URLS - */ - public static List getMavenArtifactsByGroupId(MavenSearchRepositoryType repositoryType, String groupId) { - - return getMavenArtifactsByGroupId(repositoryType, groupId, null); - - } - /** * Gets a list of download URLs by groupId from the specified repository search REST API using authentication with * bearer token * - * @param repositoryType String of the type of the repository e.g. maven, jfrog, nexus2, nexus3 + * @param baseURL String of the repository server URL * @param groupId the groupId to search for * @param authToken bearer token to use for authentication * @return List of artifact download URLS */ - public static List getMavenArtifactsByGroupId(MavenSearchRepositoryType repositoryType, String groupId, - String authToken) { + public static List getMavenArtifactsByGroupId(String baseURL, String groupId, String authToken) { try { - return AbstractRESTSearchResponse.getArtifactDownloadLinks(repositoryType, groupId, authToken); + return SearchResponseFactory.getArtifactDownloadLinks(baseURL, groupId, authToken); } catch (RESTSearchResponseException | JsonProcessingException | MalformedURLException e) { - throw new CobiGenRuntimeException("Unable to get artifacts from " + repositoryType + " by groupId " + groupId, e); + throw new CobiGenRuntimeException("Unable to get artifacts from " + baseURL + " by groupId " + groupId, e); } } diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/JfrogSearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/JfrogSearchResponse.java index b41ede60ff..e6a45ad727 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/JfrogSearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/JfrogSearchResponse.java @@ -9,6 +9,7 @@ import org.slf4j.LoggerFactory; import com.devonfw.cobigen.api.constants.MavenSearchRepositoryConstants; +import com.devonfw.cobigen.api.constants.MavenSearchRepositoryType; import com.devonfw.cobigen.api.exception.RESTSearchResponseException; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; @@ -17,7 +18,7 @@ * Json model for jfrog Search REST API response * */ -public class JfrogSearchResponse implements AbstractRESTSearchResponse { +public class JfrogSearchResponse implements SearchResponse { /** Logger instance. */ @JsonIgnore @@ -51,11 +52,11 @@ public String getJsonResponse(String repositoryUrl, String groupId, String authT throws RESTSearchResponseException { String targetLink = repositoryUrl + "/" + MavenSearchRepositoryConstants.JFROG_TARGET_LINK + "?g=" + groupId; - LOG.info("Starting Jfrog Search REST API request with URL: {}.", targetLink); + LOG.info("Starting {} search REST API request with URL: {}.", getRepositoryType(), targetLink); String jsonResponse; - jsonResponse = AbstractRESTSearchResponse.getJsonResponseStringByTargetLink(targetLink, authToken); + jsonResponse = SearchResponseFactory.getJsonResponseStringByTargetLink(targetLink, authToken); return jsonResponse; } @@ -72,6 +73,12 @@ public List getDownloadURLs() throws MalformedURLException { return downloadLinks; } + + @Override + public MavenSearchRepositoryType getRepositoryType() { + + return MavenSearchRepositoryType.jfrog; + } } /** diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/MavenSearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/MavenSearchResponse.java index b6b99aab4c..031bec0c2f 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/MavenSearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/MavenSearchResponse.java @@ -9,6 +9,7 @@ import org.slf4j.LoggerFactory; import com.devonfw.cobigen.api.constants.MavenSearchRepositoryConstants; +import com.devonfw.cobigen.api.constants.MavenSearchRepositoryType; import com.devonfw.cobigen.api.exception.RESTSearchResponseException; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; @@ -19,7 +20,7 @@ * */ @JsonIgnoreProperties(value = { "responseHeader", "spellcheck" }) -public class MavenSearchResponse implements AbstractRESTSearchResponse { +public class MavenSearchResponse implements SearchResponse { /** Logger instance. */ @JsonIgnore @@ -51,17 +52,17 @@ public String getJsonResponse(String repositoryUrl, String groupId, String authT String targetLink = repositoryUrl + "/" + MavenSearchRepositoryConstants.MAVEN_TARGET_LINK + "?q=g:" + groupId + "&wt=json"; - LOG.info("Starting Maven Search REST API request with URL: {}.", targetLink); + LOG.info("Starting {} search REST API request with URL: {}.", getRepositoryType(), targetLink); int limitRows = MavenSearchRepositoryConstants.MAVEN_MAX_RESPONSE_ROWS; if (limitRows > 0) { targetLink += "&rows=" + limitRows; - LOG.info("Limiting Maven Search REST API request to: {} rows.", limitRows); + LOG.info("Limiting {} search REST API request to: {} rows.", getRepositoryType(), limitRows); } String jsonResponse; - jsonResponse = AbstractRESTSearchResponse.getJsonResponseStringByTargetLink(targetLink, authToken); + jsonResponse = SearchResponseFactory.getJsonResponseStringByTargetLink(targetLink, authToken); return jsonResponse; @@ -77,8 +78,8 @@ public List getDownloadURLs() throws MalformedURLException { for (MavenSearchResponseDoc doc : docs) { for (String fileEnding : doc.getEc()) { String newFileEnding = fileEnding; - downloadLinks.add( - AbstractRESTSearchResponse.createDownloadLink(MavenSearchRepositoryConstants.MAVEN_REPOSITORY_DOWNLOAD_LINK, + downloadLinks + .add(SearchResponseFactory.createDownloadLink(MavenSearchRepositoryConstants.MAVEN_REPOSITORY_DOWNLOAD_LINK, doc.getGroup(), doc.getArtifact(), doc.getLatestVersion(), newFileEnding)); } @@ -87,6 +88,12 @@ public List getDownloadURLs() throws MalformedURLException { return downloadLinks; } + @Override + public MavenSearchRepositoryType getRepositoryType() { + + return MavenSearchRepositoryType.maven; + } + } /** diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/Nexus2SearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/Nexus2SearchResponse.java index 719094c9df..7322113716 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/Nexus2SearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/Nexus2SearchResponse.java @@ -10,6 +10,7 @@ import org.slf4j.LoggerFactory; import com.devonfw.cobigen.api.constants.MavenSearchRepositoryConstants; +import com.devonfw.cobigen.api.constants.MavenSearchRepositoryType; import com.devonfw.cobigen.api.exception.RESTSearchResponseException; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; @@ -20,7 +21,7 @@ * */ @JsonIgnoreProperties(value = { "totalCount", "from", "count", "tooManyResults", "collapsed", "repoDetails" }) -public class Nexus2SearchResponse implements AbstractRESTSearchResponse { +public class Nexus2SearchResponse implements SearchResponse { /** Logger instance. */ @JsonIgnore @@ -38,7 +39,7 @@ public List getDownloadURLs() throws MalformedURLException { for (Nexus2SearchResponseData item : this.data) { for (Nexus2SearchResponseArtifactHits artifactHit : item.artifactHits) { for (Nexus2SearchResponeArtifactLinks artifactLink : artifactHit.artifactLinks) { - downloadLinks.add(AbstractRESTSearchResponse.createDownloadLink( + downloadLinks.add(SearchResponseFactory.createDownloadLink( MavenSearchRepositoryConstants.NEXUS2_REPOSITORY_URL + "/" + MavenSearchRepositoryConstants.NEXUS2_REPOSITORY_LINK, item.getGroupId(), item.getArtifactId(), item.getVersion(), "." + artifactLink.getExtension())); @@ -67,14 +68,20 @@ public String getJsonResponse(String repositoryUrl, String groupId, String authT String targetLink = repositoryUrl + "/" + MavenSearchRepositoryConstants.NEXUS2_TARGET_LINK + "?_dc=" + MavenSearchRepositoryConstants.NEXUS2_DC_ID + "&q=" + groupId; - LOG.info("Starting Nexus Search REST API request with URL: {}.", targetLink); + LOG.info("Starting {} search REST API request with URL: {}.", getRepositoryType(), targetLink); String jsonResponse; - jsonResponse = AbstractRESTSearchResponse.getJsonResponseStringByTargetLink(targetLink, authToken); + jsonResponse = SearchResponseFactory.getJsonResponseStringByTargetLink(targetLink, authToken); return jsonResponse; } + + @Override + public MavenSearchRepositoryType getRepositoryType() { + + return MavenSearchRepositoryType.nexus2; + } } /** diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/Nexus3SearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/Nexus3SearchResponse.java index 21e7d37da2..6193429c8c 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/Nexus3SearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/Nexus3SearchResponse.java @@ -10,6 +10,7 @@ import org.slf4j.LoggerFactory; import com.devonfw.cobigen.api.constants.MavenSearchRepositoryConstants; +import com.devonfw.cobigen.api.constants.MavenSearchRepositoryType; import com.devonfw.cobigen.api.exception.RESTSearchResponseException; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; @@ -20,7 +21,7 @@ * */ @JsonIgnoreProperties(value = { "continuationToken" }) -public class Nexus3SearchResponse implements AbstractRESTSearchResponse { +public class Nexus3SearchResponse implements SearchResponse { /** Logger instance. */ @JsonIgnore @@ -61,14 +62,20 @@ public String getJsonResponse(String repositoryUrl, String groupId, String authT String targetLink = repositoryUrl + "/" + MavenSearchRepositoryConstants.NEXUS3_TARGET_LINK + "?repository=maven-central" + "&group=" + groupId; - LOG.info("Starting Nexus Search REST API request with URL: {}.", targetLink); + LOG.info("Starting {} search REST API request with URL: {}.", getRepositoryType(), targetLink); String jsonResponse; - jsonResponse = AbstractRESTSearchResponse.getJsonResponseStringByTargetLink(targetLink, authToken); + jsonResponse = SearchResponseFactory.getJsonResponseStringByTargetLink(targetLink, authToken); return jsonResponse; } + + @Override + public MavenSearchRepositoryType getRepositoryType() { + + return MavenSearchRepositoryType.nexus3; + } } /** diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponse.java new file mode 100644 index 0000000000..ec73bf17c4 --- /dev/null +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponse.java @@ -0,0 +1,50 @@ +package com.devonfw.cobigen.api.util.to; + +import java.net.MalformedURLException; +import java.net.URL; +import java.util.List; + +import com.devonfw.cobigen.api.constants.MavenSearchRepositoryType; +import com.devonfw.cobigen.api.exception.RESTSearchResponseException; + +/** + * + * + */ +public interface SearchResponse { + + /** + * @return the {@link MavenSearchRepositoryType} type + */ + public MavenSearchRepositoryType getRepositoryType(); + + /** + * Creates a list of download links + * + * @return List of download links + * @throws MalformedURLException if an URL was not valid + */ + List getDownloadURLs() throws MalformedURLException; + + /** + * Gets the json response + * + * @param repositoryUrl URL of the repository + * @param groupId to search for + * @return String of json response + * @throws RESTSearchResponseException if the request did not return status 200 + */ + String getJsonResponse(String repositoryUrl, String groupId) throws RESTSearchResponseException; + + /** + * Gets the json response using bearer authentication token + * + * @param repositoryUrl URL of the repository + * @param groupId to search for + * @param authToken bearer token to use for authentication + * @return String of json response + * @throws RESTSearchResponseException if the request did not return status 200 + */ + String getJsonResponse(String repositoryUrl, String groupId, String authToken) throws RESTSearchResponseException; + +} \ No newline at end of file diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractRESTSearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseFactory.java similarity index 55% rename from cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractRESTSearchResponse.java rename to cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseFactory.java index 4418df01d7..1e83b444f2 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractRESTSearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseFactory.java @@ -2,12 +2,14 @@ import java.net.MalformedURLException; import java.net.URL; +import java.util.ArrayList; import java.util.List; import org.glassfish.jersey.client.oauth2.OAuth2ClientSupport; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; -import com.devonfw.cobigen.api.constants.MavenSearchRepositoryConstants; -import com.devonfw.cobigen.api.constants.MavenSearchRepositoryType; +import com.devonfw.cobigen.api.exception.CobiGenRuntimeException; import com.devonfw.cobigen.api.exception.RESTSearchResponseException; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonMappingException; @@ -26,42 +28,30 @@ * Handles the responses from various search REST API's * */ -public interface AbstractRESTSearchResponse { +public class SearchResponseFactory { - /** - * Creates a list of download links - * - * @return List of download links - * @throws MalformedURLException if an URL was not valid - */ - public List getDownloadURLs() throws MalformedURLException; + /** Logger instance. */ + private static final Logger LOG = LoggerFactory.getLogger(SearchResponseFactory.class); /** - * Gets the json response + * Gets a list of available search REST APIs (register a new search interface type here) * - * @param repositoryUrl URL of the repository - * @param groupId to search for - * @return String of json response - * @throws RESTSearchResponseException if the request did not return status 200 - */ - public String getJsonResponse(String repositoryUrl, String groupId) throws RESTSearchResponseException; - - /** - * Gets the json response using bearer authentication token - * - * @param repositoryUrl URL of the repository - * @param groupId to search for - * @param authToken bearer token to use for authentication - * @return String of json response - * @throws RESTSearchResponseException if the request did not return status 200 + * @return list of available {@link SearchResponse} */ - public String getJsonResponse(String repositoryUrl, String groupId, String authToken) - throws RESTSearchResponseException; + private static List getAvailableSearchInterfaces() { + + List availableSearchInterfaces = new ArrayList<>(); + availableSearchInterfaces.add(new MavenSearchResponse()); + availableSearchInterfaces.add(new JfrogSearchResponse()); + availableSearchInterfaces.add(new Nexus2SearchResponse()); + availableSearchInterfaces.add(new Nexus3SearchResponse()); + return availableSearchInterfaces; + } /** * Gets the download links by given repository type * - * @param repositoryType String of the type of the repository e.g. maven, jfrog, nexus + * @param baseURL String of the repository server URL * @param groupId the groupId to search for * @param authToken bearer token to use for authentication * @return List of download links @@ -71,46 +61,31 @@ public String getJsonResponse(String repositoryUrl, String groupId, String authT * @throws MalformedURLException if an URL was malformed * */ - public static List getArtifactDownloadLinks(MavenSearchRepositoryType repositoryType, String groupId, - String authToken) + public static List getArtifactDownloadLinks(String baseURL, String groupId, String authToken) throws RESTSearchResponseException, JsonMappingException, JsonProcessingException, MalformedURLException { ObjectMapper mapper = new ObjectMapper(); - String jsonResponse = ""; - - if (repositoryType == MavenSearchRepositoryType.maven) { - MavenSearchResponse response = new MavenSearchResponse(); - String mavenRepositoryURL = MavenSearchRepositoryConstants.MAVEN_REPOSITORY_URL; - jsonResponse = response.getJsonResponse(mavenRepositoryURL, groupId, null); - response = mapper.readValue(jsonResponse, MavenSearchResponse.class); - return response.getDownloadURLs(); - } - - if (repositoryType == MavenSearchRepositoryType.jfrog) { - JfrogSearchResponse response = new JfrogSearchResponse(); - String jfrogRepositoryURL = MavenSearchRepositoryConstants.JFROG_REPOSITORY_URL; - jsonResponse = response.getJsonResponse(jfrogRepositoryURL, groupId, authToken); - response = mapper.readValue(jsonResponse, JfrogSearchResponse.class); - return response.getDownloadURLs(); + List downloadLinks = null; + List availableSearchInterfaces = getAvailableSearchInterfaces(); + + for (SearchResponse searchResponse : availableSearchInterfaces) { + try { + LOG.debug("Trying to get a response from {} with server URL: {} ...", searchResponse.getRepositoryType(), + baseURL); + String jsonResponse = searchResponse.getJsonResponse(baseURL, groupId, authToken); + SearchResponse response = mapper.readValue(jsonResponse, searchResponse.getClass()); + return response.getDownloadURLs(); + } catch (RESTSearchResponseException e) { + LOG.debug("It was not possible to get a response from {} using the URL: {}.\n Following error occured:\n {}", + searchResponse.getRepositoryType(), baseURL, e.getMessage()); + } catch (ProcessingException e) { + String errorMsg = "The search REST API was not able to process the URL: " + baseURL; + LOG.error(errorMsg, e); + throw new CobiGenRuntimeException(errorMsg, e); + } } - if (repositoryType == MavenSearchRepositoryType.nexus2) { - Nexus2SearchResponse response = new Nexus2SearchResponse(); - String nexusRepositoryURL = MavenSearchRepositoryConstants.NEXUS2_REPOSITORY_URL; - jsonResponse = response.getJsonResponse(nexusRepositoryURL, groupId, null); - response = mapper.readValue(jsonResponse, Nexus2SearchResponse.class); - return response.getDownloadURLs(); - } - - if (repositoryType == MavenSearchRepositoryType.nexus3) { - Nexus3SearchResponse response = new Nexus3SearchResponse(); - String nexusRepositoryURL = MavenSearchRepositoryConstants.NEXUS3_REPOSITORY_URL; - jsonResponse = response.getJsonResponse(nexusRepositoryURL, groupId, null); - response = mapper.readValue(jsonResponse, Nexus3SearchResponse.class); - return response.getDownloadURLs(); - } - - return null; + return downloadLinks; } /** @@ -151,12 +126,7 @@ public static String getJsonResponseStringByTargetLink(String targetLink, String Response response = null; Invocation.Builder request = target.request(MediaType.APPLICATION_JSON); - try { - response = request.get(); - } catch (ProcessingException e) { - throw new RESTSearchResponseException("The search REST API was not able to process the target URL: " + targetLink, - e); - } + response = request.get(); int status = response.getStatus(); String jsonResponse = ""; diff --git a/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java b/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java index 7db46f5a3c..86f084de26 100644 --- a/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java +++ b/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java @@ -11,16 +11,19 @@ import org.junit.Ignore; import org.junit.Test; -import com.devonfw.cobigen.api.constants.MavenSearchRepositoryType; +import com.devonfw.cobigen.api.constants.MavenSearchRepositoryConstants; +import com.devonfw.cobigen.api.exception.CobiGenRuntimeException; import com.devonfw.cobigen.api.exception.RESTSearchResponseException; import com.devonfw.cobigen.api.util.MavenUtil; -import com.devonfw.cobigen.api.util.to.AbstractRESTSearchResponse; import com.devonfw.cobigen.api.util.to.JfrogSearchResponse; import com.devonfw.cobigen.api.util.to.MavenSearchResponse; import com.devonfw.cobigen.api.util.to.Nexus2SearchResponse; import com.devonfw.cobigen.api.util.to.Nexus3SearchResponse; +import com.devonfw.cobigen.api.util.to.SearchResponseFactory; import com.fasterxml.jackson.databind.ObjectMapper; +import jakarta.ws.rs.ProcessingException; + /** * Test class for maven utilities */ @@ -30,30 +33,30 @@ public class MavenUtilTest { private static final String testdataRoot = "src/test/resources/testdata/unittest/MavenUtilTest"; /** - * Tests if a wrong repository type returns null + * Tests if a wrong repository type throws {@link CobiGenRuntimeException} */ - @Test - public void testWrongRepositoryTypeReturnsNull() { + @Test(expected = CobiGenRuntimeException.class) + public void testWrongRepositoryTypeThrowsException() { - assertThat(MavenUtil.getMavenArtifactsByGroupId(null, testdataRoot)).isNull(); + assertThat(MavenUtil.getMavenArtifactsByGroupId("this/is/not/a/link", "test", null)); } /** * Tests if an exception gets thrown when a faulty target link without a token was used */ - @Test(expected = RESTSearchResponseException.class) + @Test(expected = ProcessingException.class) public void testWrongTargetLinkThrowsException() { - AbstractRESTSearchResponse.getJsonResponseStringByTargetLink("this/is/not/a/link", null); + SearchResponseFactory.getJsonResponseStringByTargetLink("this/is/not/a/link", null); } /** - * Tests if an exception gets thrown when a faulty target link and a token was used + * Tests if an exception gets thrown when a faulty target link and token was used */ - @Test(expected = RESTSearchResponseException.class) + @Test(expected = ProcessingException.class) public void testWrongTargetLinkAndTokenThrowsException() { - AbstractRESTSearchResponse.getJsonResponseStringByTargetLink("this/is/not/a/link", "thisisabadtoken"); + SearchResponseFactory.getJsonResponseStringByTargetLink("this/is/not/a/link", "thisisabadtoken"); } /** @@ -62,8 +65,7 @@ public void testWrongTargetLinkAndTokenThrowsException() { @Test(expected = RESTSearchResponseException.class) public void testWrongResponseStatusCodeThrowsException() { - AbstractRESTSearchResponse.getJsonResponseStringByTargetLink("https://search.maven.org/solrsearch/select?test", - null); + SearchResponseFactory.getJsonResponseStringByTargetLink("https://search.maven.org/solrsearch/select?test", null); } /** @@ -173,7 +175,8 @@ public void testMavenSearchRequestGetsValidDownloadLinks() throws IOException { List downloadList; - downloadList = MavenUtil.getMavenArtifactsByGroupId(MavenSearchRepositoryType.maven, "com.google.inject", null); + downloadList = MavenUtil.getMavenArtifactsByGroupId(MavenSearchRepositoryConstants.MAVEN_REPOSITORY_URL, + "com.google.inject", null); assertThat(downloadList).contains( new URL("https://repo1.maven.org/maven2/com/google/inject/guice/5.1.0/guice-5.1.0.jar"), @@ -192,7 +195,8 @@ public void testNexus2SearchRequestGetsValidDownloadLinks() throws IOException { List downloadList; - downloadList = MavenUtil.getMavenArtifactsByGroupId(MavenSearchRepositoryType.nexus2, "com.devonfw.cobigen"); + downloadList = MavenUtil.getMavenArtifactsByGroupId(MavenSearchRepositoryConstants.NEXUS2_REPOSITORY_URL, + "com.devonfw.cobigen", null); assertThat(downloadList).contains(new URL( "https://s01.oss.sonatype.org/service/local/repositories/releases/content/com/devonfw/cobigen/openapiplugin/2021.12.006/openapiplugin-2021.12.006.jar")); @@ -209,7 +213,8 @@ public void testNexus3SearchRequestGetsValidDownloadLinks() throws IOException { List downloadList; - downloadList = MavenUtil.getMavenArtifactsByGroupId(MavenSearchRepositoryType.nexus3, "com.devonfw.cobigen"); + downloadList = MavenUtil.getMavenArtifactsByGroupId(MavenSearchRepositoryConstants.NEXUS3_REPOSITORY_URL, + "com.devonfw.cobigen", null); assertThat(downloadList).contains(new URL( "http://localhost:8081/repository/maven-central/org/osgi/org.osgi.core/4.3.1/org.osgi.core-4.3.1-sources.jar"), @@ -228,7 +233,8 @@ public void testJfrogSearchRequestGetsValidDownloadLinks() throws IOException { List downloadList; - downloadList = MavenUtil.getMavenArtifactsByGroupId(MavenSearchRepositoryType.jfrog, "com.devonfw.cobigen", null); + downloadList = MavenUtil.getMavenArtifactsByGroupId(MavenSearchRepositoryConstants.JFROG_REPOSITORY_URL, + "com.devonfw.cobigen", null); assertThat(downloadList).contains(new URL( "https://localjfrog.com/artifactory/api/storage/maven-remote-cache/com/devonfw/cobigen/cli-parent/2021.04.001-SNAPSHOT/cli-parent-2021.04.001-SNAPSHOT.pom"), new URL( From 644c6adddc821087a7868279cb9598cbc43da8ce Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Mon, 15 Aug 2022 11:49:27 +0200 Subject: [PATCH 12/42] #1529 moved utility methods from factory class to util class added new SearchResponseUtil class --- .../api/util/to/JfrogSearchResponse.java | 2 +- .../api/util/to/MavenSearchResponse.java | 4 +- .../api/util/to/Nexus2SearchResponse.java | 4 +- .../api/util/to/Nexus3SearchResponse.java | 2 +- .../api/util/to/SearchResponseFactory.java | 86 +--------------- .../api/util/to/SearchResponseUtil.java | 97 +++++++++++++++++++ .../devonfw/cobigen/api/MavenUtilTest.java | 8 +- 7 files changed, 110 insertions(+), 93 deletions(-) create mode 100644 cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseUtil.java diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/JfrogSearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/JfrogSearchResponse.java index e6a45ad727..aa2d77732d 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/JfrogSearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/JfrogSearchResponse.java @@ -56,7 +56,7 @@ public String getJsonResponse(String repositoryUrl, String groupId, String authT String jsonResponse; - jsonResponse = SearchResponseFactory.getJsonResponseStringByTargetLink(targetLink, authToken); + jsonResponse = SearchResponseUtil.getJsonResponseStringByTargetLink(targetLink, authToken); return jsonResponse; } diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/MavenSearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/MavenSearchResponse.java index 031bec0c2f..b6b0a620c2 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/MavenSearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/MavenSearchResponse.java @@ -62,7 +62,7 @@ public String getJsonResponse(String repositoryUrl, String groupId, String authT String jsonResponse; - jsonResponse = SearchResponseFactory.getJsonResponseStringByTargetLink(targetLink, authToken); + jsonResponse = SearchResponseUtil.getJsonResponseStringByTargetLink(targetLink, authToken); return jsonResponse; @@ -79,7 +79,7 @@ public List getDownloadURLs() throws MalformedURLException { for (String fileEnding : doc.getEc()) { String newFileEnding = fileEnding; downloadLinks - .add(SearchResponseFactory.createDownloadLink(MavenSearchRepositoryConstants.MAVEN_REPOSITORY_DOWNLOAD_LINK, + .add(SearchResponseUtil.createDownloadLink(MavenSearchRepositoryConstants.MAVEN_REPOSITORY_DOWNLOAD_LINK, doc.getGroup(), doc.getArtifact(), doc.getLatestVersion(), newFileEnding)); } diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/Nexus2SearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/Nexus2SearchResponse.java index 7322113716..0a495fdcfe 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/Nexus2SearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/Nexus2SearchResponse.java @@ -39,7 +39,7 @@ public List getDownloadURLs() throws MalformedURLException { for (Nexus2SearchResponseData item : this.data) { for (Nexus2SearchResponseArtifactHits artifactHit : item.artifactHits) { for (Nexus2SearchResponeArtifactLinks artifactLink : artifactHit.artifactLinks) { - downloadLinks.add(SearchResponseFactory.createDownloadLink( + downloadLinks.add(SearchResponseUtil.createDownloadLink( MavenSearchRepositoryConstants.NEXUS2_REPOSITORY_URL + "/" + MavenSearchRepositoryConstants.NEXUS2_REPOSITORY_LINK, item.getGroupId(), item.getArtifactId(), item.getVersion(), "." + artifactLink.getExtension())); @@ -72,7 +72,7 @@ public String getJsonResponse(String repositoryUrl, String groupId, String authT String jsonResponse; - jsonResponse = SearchResponseFactory.getJsonResponseStringByTargetLink(targetLink, authToken); + jsonResponse = SearchResponseUtil.getJsonResponseStringByTargetLink(targetLink, authToken); return jsonResponse; } diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/Nexus3SearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/Nexus3SearchResponse.java index 6193429c8c..98c94ed9c7 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/Nexus3SearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/Nexus3SearchResponse.java @@ -66,7 +66,7 @@ public String getJsonResponse(String repositoryUrl, String groupId, String authT String jsonResponse; - jsonResponse = SearchResponseFactory.getJsonResponseStringByTargetLink(targetLink, authToken); + jsonResponse = SearchResponseUtil.getJsonResponseStringByTargetLink(targetLink, authToken); return jsonResponse; } diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseFactory.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseFactory.java index 1e83b444f2..9216ee0d2d 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseFactory.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseFactory.java @@ -5,7 +5,6 @@ import java.util.ArrayList; import java.util.List; -import org.glassfish.jersey.client.oauth2.OAuth2ClientSupport; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -16,13 +15,6 @@ import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.ws.rs.ProcessingException; -import jakarta.ws.rs.client.Client; -import jakarta.ws.rs.client.ClientBuilder; -import jakarta.ws.rs.client.Invocation; -import jakarta.ws.rs.client.WebTarget; -import jakarta.ws.rs.core.Feature; -import jakarta.ws.rs.core.MediaType; -import jakarta.ws.rs.core.Response; /** * Handles the responses from various search REST API's @@ -31,14 +23,14 @@ public class SearchResponseFactory { /** Logger instance. */ - private static final Logger LOG = LoggerFactory.getLogger(SearchResponseFactory.class); + static final Logger LOG = LoggerFactory.getLogger(SearchResponseFactory.class); /** * Gets a list of available search REST APIs (register a new search interface type here) * * @return list of available {@link SearchResponse} */ - private static List getAvailableSearchInterfaces() { + static List getAvailableSearchInterfaces() { List availableSearchInterfaces = new ArrayList<>(); availableSearchInterfaces.add(new MavenSearchResponse()); @@ -49,7 +41,7 @@ private static List getAvailableSearchInterfaces() { } /** - * Gets the download links by given repository type + * Gets the maven artifact download links by given base URL, groupId and optional authentication token * * @param baseURL String of the repository server URL * @param groupId the groupId to search for @@ -88,76 +80,4 @@ public static List getArtifactDownloadLinks(String baseURL, String groupId, return downloadLinks; } - /** - * Creates a @WebTarget with provided authentication token - * - * @param targetLink link to get response from - * @param token bearer token to use for authentication - * @return WebTarget to use as resource - */ - public static WebTarget bearerAuthenticationWithOAuth2AtClientLevel(String targetLink, String token) { - - Feature feature = OAuth2ClientSupport.feature(token); - Client client = ClientBuilder.newBuilder().register(feature).build(); - - WebTarget target = client.target(targetLink); - return target; - } - - /** - * Gets a json response by given REST API target link using bearer authentication token - * - * @param targetLink link to get response from - * @param authToken bearer token to use for authentication - * @return String of json response - * @throws RESTSearchResponseException if the returned status code was not 200 OK - */ - public static String getJsonResponseStringByTargetLink(String targetLink, String authToken) - throws RESTSearchResponseException { - - WebTarget target = null; - - if (authToken != null) { - target = bearerAuthenticationWithOAuth2AtClientLevel(targetLink, authToken); - } else { - Client client = ClientBuilder.newClient(); - target = client.target(targetLink); - } - - Response response = null; - Invocation.Builder request = target.request(MediaType.APPLICATION_JSON); - response = request.get(); - - int status = response.getStatus(); - String jsonResponse = ""; - if (status == 200) { - jsonResponse = response.readEntity(String.class); - } else { - throw new RESTSearchResponseException("The search REST API returned the unexpected status code: ", - String.valueOf(status)); - } - return jsonResponse; - } - - /** - * Creates a download link (concatenates maven repository link with groupId, artifact and version) - * - * @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 - * @return concatenated download link - * @throws MalformedURLException if the URL was not valid - */ - public static URL createDownloadLink(String mavenRepo, String groupId, String artifactId, String version, - String fileEnding) throws MalformedURLException { - - String parsedGroupId = groupId.replace(".", "/"); - String downloadFile = artifactId + "-" + version + fileEnding; - String downloadLink = mavenRepo + "/" + parsedGroupId + "/" + artifactId + "/" + version + "/" + downloadFile; - URL url = new URL(downloadLink); - return url; - } - } \ No newline at end of file diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseUtil.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseUtil.java new file mode 100644 index 0000000000..835ab794cf --- /dev/null +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseUtil.java @@ -0,0 +1,97 @@ +package com.devonfw.cobigen.api.util.to; + +import java.net.MalformedURLException; +import java.net.URL; + +import org.glassfish.jersey.client.oauth2.OAuth2ClientSupport; + +import com.devonfw.cobigen.api.exception.RESTSearchResponseException; + +import jakarta.ws.rs.client.Client; +import jakarta.ws.rs.client.ClientBuilder; +import jakarta.ws.rs.client.Invocation; +import jakarta.ws.rs.client.WebTarget; +import jakarta.ws.rs.core.Feature; +import jakarta.ws.rs.core.MediaType; +import jakarta.ws.rs.core.Response; + +/** + * + * Utility class for search REST API requests handling + * + */ +public class SearchResponseUtil { + + /** + * Creates a @WebTarget with provided authentication token + * + * @param targetLink link to get response from + * @param token bearer token to use for authentication + * @return WebTarget to use as resource + */ + public static WebTarget bearerAuthenticationWithOAuth2AtClientLevel(String targetLink, String token) { + + Feature feature = OAuth2ClientSupport.feature(token); + Client client = ClientBuilder.newBuilder().register(feature).build(); + + WebTarget target = client.target(targetLink); + return target; + } + + /** + * Gets a json response by given REST API target link using bearer authentication token + * + * @param targetLink link to get response from + * @param authToken bearer token to use for authentication + * @return String of json response + * @throws RESTSearchResponseException if the returned status code was not 200 OK + */ + public static String getJsonResponseStringByTargetLink(String targetLink, String authToken) + throws RESTSearchResponseException { + + WebTarget target = null; + + if (authToken != null) { + target = bearerAuthenticationWithOAuth2AtClientLevel(targetLink, authToken); + } else { + Client client = ClientBuilder.newClient(); + target = client.target(targetLink); + } + + Response response = null; + Invocation.Builder request = target.request(MediaType.APPLICATION_JSON); + response = request.get(); + + int status = response.getStatus(); + String jsonResponse = ""; + if (status == 200) { + jsonResponse = response.readEntity(String.class); + } else { + throw new RESTSearchResponseException("The search REST API returned the unexpected status code: ", + String.valueOf(status)); + } + return jsonResponse; + } + + /** + * Creates a download link (concatenates maven repository link with groupId, artifact and version) + * + * @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 + * @return concatenated download link + * @throws MalformedURLException if the URL was not valid + */ + public static URL createDownloadLink(String mavenRepo, String groupId, String artifactId, String version, + String fileEnding) throws MalformedURLException { + + String parsedGroupId = groupId.replace(".", "/"); + String downloadFile = artifactId + "-" + version + fileEnding; + String downloadLink = mavenRepo + "/" + parsedGroupId + "/" + artifactId + "/" + version + "/" + downloadFile; + URL url = new URL(downloadLink); + return url; + } + +} diff --git a/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java b/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java index 86f084de26..f2137a15b5 100644 --- a/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java +++ b/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java @@ -19,7 +19,7 @@ import com.devonfw.cobigen.api.util.to.MavenSearchResponse; import com.devonfw.cobigen.api.util.to.Nexus2SearchResponse; import com.devonfw.cobigen.api.util.to.Nexus3SearchResponse; -import com.devonfw.cobigen.api.util.to.SearchResponseFactory; +import com.devonfw.cobigen.api.util.to.SearchResponseUtil; import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.ws.rs.ProcessingException; @@ -47,7 +47,7 @@ public void testWrongRepositoryTypeThrowsException() { @Test(expected = ProcessingException.class) public void testWrongTargetLinkThrowsException() { - SearchResponseFactory.getJsonResponseStringByTargetLink("this/is/not/a/link", null); + SearchResponseUtil.getJsonResponseStringByTargetLink("this/is/not/a/link", null); } /** @@ -56,7 +56,7 @@ public void testWrongTargetLinkThrowsException() { @Test(expected = ProcessingException.class) public void testWrongTargetLinkAndTokenThrowsException() { - SearchResponseFactory.getJsonResponseStringByTargetLink("this/is/not/a/link", "thisisabadtoken"); + SearchResponseUtil.getJsonResponseStringByTargetLink("this/is/not/a/link", "thisisabadtoken"); } /** @@ -65,7 +65,7 @@ public void testWrongTargetLinkAndTokenThrowsException() { @Test(expected = RESTSearchResponseException.class) public void testWrongResponseStatusCodeThrowsException() { - SearchResponseFactory.getJsonResponseStringByTargetLink("https://search.maven.org/solrsearch/select?test", null); + SearchResponseUtil.getJsonResponseStringByTargetLink("https://search.maven.org/solrsearch/select?test", null); } /** From b2cca28db97aef1ddacc27b6ad8f67938b4a08e5 Mon Sep 17 00:00:00 2001 From: EduardKrieger Date: Wed, 17 Aug 2022 17:12:34 +0200 Subject: [PATCH 13/42] changed javadoc --- .../java/com/devonfw/cobigen/api/util/to/SearchResponse.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponse.java index ec73bf17c4..22c6efd5a0 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponse.java @@ -19,7 +19,7 @@ public interface SearchResponse { public MavenSearchRepositoryType getRepositoryType(); /** - * Creates a list of download links + * Creates a list of download URLs * * @return List of download links * @throws MalformedURLException if an URL was not valid From d5b3589615d16c6a01c35fb30514034fc5b19af7 Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Thu, 18 Aug 2022 00:31:44 +0200 Subject: [PATCH 14/42] #1529 implemented requested changes renamed RESTSearchResponse signature msg to message --- .../cobigen/api/exception/RESTSearchResponseException.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/exception/RESTSearchResponseException.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/exception/RESTSearchResponseException.java index 7f7e030b0a..3565fce23e 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/exception/RESTSearchResponseException.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/exception/RESTSearchResponseException.java @@ -8,11 +8,11 @@ public class RESTSearchResponseException extends CobiGenRuntimeException { /** * Creates a new {@link RESTSearchResponseException} with the given message * - * @param msg error message of the exception + * @param message error message of the exception */ - public RESTSearchResponseException(String msg) { + public RESTSearchResponseException(String message) { - super(msg); + super(message); } /** From b427129202c496c9b840f54510b452fc3cd47b25 Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Thu, 18 Aug 2022 00:34:36 +0200 Subject: [PATCH 15/42] #1529 implemented requested changes removed limitRows functionality removed NEXUS2_DC_ID from target link adjusted MavenSearchResponseConstants javadoc --- .../constants/MavenSearchRepositoryConstants.java | 13 +------------ .../cobigen/api/util/to/MavenSearchResponse.java | 6 ------ .../cobigen/api/util/to/Nexus2SearchResponse.java | 3 +-- 3 files changed, 2 insertions(+), 20 deletions(-) diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/constants/MavenSearchRepositoryConstants.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/constants/MavenSearchRepositoryConstants.java index 815fa7436e..0026115471 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/constants/MavenSearchRepositoryConstants.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/constants/MavenSearchRepositoryConstants.java @@ -1,8 +1,7 @@ package com.devonfw.cobigen.api.constants; /** - * Maven search repository URLs - * + * Constants needed for handling the maven search REST APIs */ public class MavenSearchRepositoryConstants { @@ -21,11 +20,6 @@ public class MavenSearchRepositoryConstants { */ public static String MAVEN_TARGET_LINK = "solrsearch/select"; - /** - * Maven maximum response rows - */ - public static int MAVEN_MAX_RESPONSE_ROWS = 20; - /** * Nexus2 repository URL */ @@ -41,11 +35,6 @@ public class MavenSearchRepositoryConstants { */ public static String NEXUS2_TARGET_LINK = "service/local/lucene/search"; - /** - * Nexus2 connect ID - */ - public static String NEXUS2_DC_ID = "1660043999867"; - /** * Nexus3 target link */ diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/MavenSearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/MavenSearchResponse.java index b6b0a620c2..d153133a36 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/MavenSearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/MavenSearchResponse.java @@ -54,12 +54,6 @@ public String getJsonResponse(String repositoryUrl, String groupId, String authT + "&wt=json"; LOG.info("Starting {} search REST API request with URL: {}.", getRepositoryType(), targetLink); - int limitRows = MavenSearchRepositoryConstants.MAVEN_MAX_RESPONSE_ROWS; - if (limitRows > 0) { - targetLink += "&rows=" + limitRows; - LOG.info("Limiting {} search REST API request to: {} rows.", getRepositoryType(), limitRows); - } - String jsonResponse; jsonResponse = SearchResponseUtil.getJsonResponseStringByTargetLink(targetLink, authToken); diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/Nexus2SearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/Nexus2SearchResponse.java index 0a495fdcfe..9d911a9de1 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/Nexus2SearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/Nexus2SearchResponse.java @@ -66,8 +66,7 @@ public String getJsonResponse(String repositoryUrl, String groupId) throws RESTS public String getJsonResponse(String repositoryUrl, String groupId, String authToken) throws RESTSearchResponseException { - String targetLink = repositoryUrl + "/" + MavenSearchRepositoryConstants.NEXUS2_TARGET_LINK + "?_dc=" - + MavenSearchRepositoryConstants.NEXUS2_DC_ID + "&q=" + groupId; + String targetLink = repositoryUrl + "/" + MavenSearchRepositoryConstants.NEXUS2_TARGET_LINK + "?q=" + groupId; LOG.info("Starting {} search REST API request with URL: {}.", getRepositoryType(), targetLink); String jsonResponse; From 77890ab61cb4a9121406ba6d5d2e2cac629ade83 Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Thu, 18 Aug 2022 01:07:18 +0200 Subject: [PATCH 16/42] #1529 implemented requested changes converted getAvailableSearchInterfaces method to final list of SearchResponses adjusted javadoc --- .../api/util/to/SearchResponseFactory.java | 27 ++++++------------- 1 file changed, 8 insertions(+), 19 deletions(-) diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseFactory.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseFactory.java index 9216ee0d2d..08b92bd83f 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseFactory.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseFactory.java @@ -2,7 +2,6 @@ import java.net.MalformedURLException; import java.net.URL; -import java.util.ArrayList; import java.util.List; import org.slf4j.Logger; @@ -13,12 +12,12 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.collect.Lists; import jakarta.ws.rs.ProcessingException; /** - * Handles the responses from various search REST API's - * + * Factory to create new instances of {@link SearchResponse} which handles the responses from various search REST APIs. */ public class SearchResponseFactory { @@ -26,19 +25,10 @@ public class SearchResponseFactory { static final Logger LOG = LoggerFactory.getLogger(SearchResponseFactory.class); /** - * Gets a list of available search REST APIs (register a new search interface type here) - * - * @return list of available {@link SearchResponse} + * List of available {@link SearchResponse} implementations (add new search REST API responses here) */ - static List getAvailableSearchInterfaces() { - - List availableSearchInterfaces = new ArrayList<>(); - availableSearchInterfaces.add(new MavenSearchResponse()); - availableSearchInterfaces.add(new JfrogSearchResponse()); - availableSearchInterfaces.add(new Nexus2SearchResponse()); - availableSearchInterfaces.add(new Nexus3SearchResponse()); - return availableSearchInterfaces; - } + private static final List SEARCH_RESPONSES = Lists.newArrayList(new MavenSearchResponse(), + new JfrogSearchResponse(), new Nexus2SearchResponse(), new Nexus3SearchResponse()); /** * Gets the maven artifact download links by given base URL, groupId and optional authentication token @@ -46,8 +36,8 @@ static List getAvailableSearchInterfaces() { * @param baseURL String of the repository server URL * @param groupId the groupId to search for * @param authToken bearer token to use for authentication - * @return List of download links - * @throws RESTSearchResponseException if an error occurred + * @return List of download URLs + * @throws RESTSearchResponseException if an error occurred while accessing the server * @throws JsonProcessingException if the json processing was not possible * @throws JsonMappingException if the json mapping was not possible * @throws MalformedURLException if an URL was malformed @@ -58,9 +48,8 @@ public static List getArtifactDownloadLinks(String baseURL, String groupId, ObjectMapper mapper = new ObjectMapper(); List downloadLinks = null; - List availableSearchInterfaces = getAvailableSearchInterfaces(); - for (SearchResponse searchResponse : availableSearchInterfaces) { + for (SearchResponse searchResponse : SEARCH_RESPONSES) { try { LOG.debug("Trying to get a response from {} with server URL: {} ...", searchResponse.getRepositoryType(), baseURL); From 3b1c998d73f86813f3f7f712914e765770ae535d Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Thu, 18 Aug 2022 01:07:50 +0200 Subject: [PATCH 17/42] #1529 added missing javadoc --- .../java/com/devonfw/cobigen/api/util/to/SearchResponse.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponse.java index 22c6efd5a0..358e52c3d3 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponse.java @@ -6,10 +6,11 @@ import com.devonfw.cobigen.api.constants.MavenSearchRepositoryType; import com.devonfw.cobigen.api.exception.RESTSearchResponseException; +import com.fasterxml.jackson.annotation.JsonProperty; /** - * - * + * This interface should be inherited for all maven REST search API responses to properly convert {@link JsonProperty} + * from responses to valid download URLs */ public interface SearchResponse { From aba2434728d005c7776e191f55d590a83dc80ac0 Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Thu, 18 Aug 2022 01:19:06 +0200 Subject: [PATCH 18/42] #1529 implemented requested changes added more detailed javadoc --- .../cobigen/api/constants/MavenSearchRepositoryType.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/constants/MavenSearchRepositoryType.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/constants/MavenSearchRepositoryType.java index a25b5288f5..e7049d00a5 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/constants/MavenSearchRepositoryType.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/constants/MavenSearchRepositoryType.java @@ -1,8 +1,8 @@ package com.devonfw.cobigen.api.constants; /** - * Maven search repository types - * + * Maven search repository types used to identify and name the available search REST API types (add new search + * repository types/versions here) */ public enum MavenSearchRepositoryType { From 5406713d817c3d8fa71934f5113fb9617c1cfe8b Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Wed, 24 Aug 2022 10:32:03 +0200 Subject: [PATCH 19/42] #1529 implemented requested changes converted RESTSearchResponseException name to PascalCase --- ...ion.java => RestSearchResponseException.java} | 16 ++++++++-------- .../com/devonfw/cobigen/api/util/MavenUtil.java | 4 ++-- .../cobigen/api/util/to/JfrogSearchResponse.java | 6 +++--- .../cobigen/api/util/to/MavenSearchResponse.java | 6 +++--- .../api/util/to/Nexus2SearchResponse.java | 6 +++--- .../api/util/to/Nexus3SearchResponse.java | 6 +++--- .../cobigen/api/util/to/SearchResponse.java | 10 +++++----- .../api/util/to/SearchResponseFactory.java | 8 ++++---- .../cobigen/api/util/to/SearchResponseUtil.java | 8 ++++---- .../com/devonfw/cobigen/api/MavenUtilTest.java | 4 ++-- 10 files changed, 37 insertions(+), 37 deletions(-) rename cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/exception/{RESTSearchResponseException.java => RestSearchResponseException.java} (59%) diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/exception/RESTSearchResponseException.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/exception/RestSearchResponseException.java similarity index 59% rename from cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/exception/RESTSearchResponseException.java rename to cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/exception/RestSearchResponseException.java index 3565fce23e..e4a1b6e208 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/exception/RESTSearchResponseException.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/exception/RestSearchResponseException.java @@ -1,38 +1,38 @@ package com.devonfw.cobigen.api.exception; /** Exception to indicate that the REST search API encountered a problem while accessing the server. */ -public class RESTSearchResponseException extends CobiGenRuntimeException { +public class RestSearchResponseException extends CobiGenRuntimeException { private static final long serialVersionUID = 1L; /** - * Creates a new {@link RESTSearchResponseException} with the given message + * Creates a new {@link RestSearchResponseException} with the given message * * @param message error message of the exception */ - public RESTSearchResponseException(String message) { + public RestSearchResponseException(String message) { super(message); } /** - * Creates a new {@link RESTSearchResponseException} with the specified message and the causing {@link Throwable} + * Creates a new {@link RestSearchResponseException} with the specified message and the causing {@link Throwable} * * @param message describing the exception * @param cause the causing Throwable */ - public RESTSearchResponseException(String message, Throwable cause) { + public RestSearchResponseException(String message, Throwable cause) { super(message, cause); } /** - * Creates a new {@link RESTSearchResponseException} with the specified message and the causing {@link Throwable} + * Creates a new {@link RestSearchResponseException} with the specified message and the causing {@link Throwable} * * @param message describing the exception - * @param statusCode status code causing the {@link RESTSearchResponseException} or null if not available + * @param statusCode status code causing the {@link RestSearchResponseException} or null if not available */ - public RESTSearchResponseException(String message, String statusCode) { + public RestSearchResponseException(String message, String statusCode) { super(message + statusCode); } diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/MavenUtil.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/MavenUtil.java index 9bce532595..d2570aa6d6 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/MavenUtil.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/MavenUtil.java @@ -27,7 +27,7 @@ import com.devonfw.cobigen.api.constants.MavenConstants; import com.devonfw.cobigen.api.exception.CobiGenRuntimeException; -import com.devonfw.cobigen.api.exception.RESTSearchResponseException; +import com.devonfw.cobigen.api.exception.RestSearchResponseException; import com.devonfw.cobigen.api.util.to.SearchResponseFactory; import com.fasterxml.jackson.core.JsonProcessingException; import com.google.common.collect.Lists; @@ -357,7 +357,7 @@ public static List getMavenArtifactsByGroupId(String baseURL, String groupI try { return SearchResponseFactory.getArtifactDownloadLinks(baseURL, groupId, authToken); - } catch (RESTSearchResponseException | JsonProcessingException | MalformedURLException e) { + } catch (RestSearchResponseException | JsonProcessingException | MalformedURLException e) { throw new CobiGenRuntimeException("Unable to get artifacts from " + baseURL + " by groupId " + groupId, e); } diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/JfrogSearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/JfrogSearchResponse.java index aa2d77732d..f4f06269e1 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/JfrogSearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/JfrogSearchResponse.java @@ -10,7 +10,7 @@ import com.devonfw.cobigen.api.constants.MavenSearchRepositoryConstants; import com.devonfw.cobigen.api.constants.MavenSearchRepositoryType; -import com.devonfw.cobigen.api.exception.RESTSearchResponseException; +import com.devonfw.cobigen.api.exception.RestSearchResponseException; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; @@ -41,7 +41,7 @@ public List getResults() { @Override @JsonIgnore - public String getJsonResponse(String repositoryUrl, String groupId) throws RESTSearchResponseException { + public String getJsonResponse(String repositoryUrl, String groupId) throws RestSearchResponseException { return getJsonResponse(repositoryUrl, groupId, null); } @@ -49,7 +49,7 @@ public String getJsonResponse(String repositoryUrl, String groupId) throws RESTS @Override @JsonIgnore public String getJsonResponse(String repositoryUrl, String groupId, String authToken) - throws RESTSearchResponseException { + throws RestSearchResponseException { String targetLink = repositoryUrl + "/" + MavenSearchRepositoryConstants.JFROG_TARGET_LINK + "?g=" + groupId; LOG.info("Starting {} search REST API request with URL: {}.", getRepositoryType(), targetLink); diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/MavenSearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/MavenSearchResponse.java index d153133a36..e342c8fb3a 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/MavenSearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/MavenSearchResponse.java @@ -10,7 +10,7 @@ import com.devonfw.cobigen.api.constants.MavenSearchRepositoryConstants; import com.devonfw.cobigen.api.constants.MavenSearchRepositoryType; -import com.devonfw.cobigen.api.exception.RESTSearchResponseException; +import com.devonfw.cobigen.api.exception.RestSearchResponseException; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; @@ -40,7 +40,7 @@ public MavenSearchResponseResponse getResponse() { @Override @JsonIgnore - public String getJsonResponse(String repositoryUrl, String groupId) throws RESTSearchResponseException { + public String getJsonResponse(String repositoryUrl, String groupId) throws RestSearchResponseException { return getJsonResponse(repositoryUrl, groupId, null); } @@ -48,7 +48,7 @@ public String getJsonResponse(String repositoryUrl, String groupId) throws RESTS @Override @JsonIgnore public String getJsonResponse(String repositoryUrl, String groupId, String authToken) - throws RESTSearchResponseException { + throws RestSearchResponseException { String targetLink = repositoryUrl + "/" + MavenSearchRepositoryConstants.MAVEN_TARGET_LINK + "?q=g:" + groupId + "&wt=json"; diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/Nexus2SearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/Nexus2SearchResponse.java index 9d911a9de1..4f585a7580 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/Nexus2SearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/Nexus2SearchResponse.java @@ -11,7 +11,7 @@ import com.devonfw.cobigen.api.constants.MavenSearchRepositoryConstants; import com.devonfw.cobigen.api.constants.MavenSearchRepositoryType; -import com.devonfw.cobigen.api.exception.RESTSearchResponseException; +import com.devonfw.cobigen.api.exception.RestSearchResponseException; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; @@ -56,7 +56,7 @@ public List getDownloadURLs() throws MalformedURLException { @Override @JsonIgnore - public String getJsonResponse(String repositoryUrl, String groupId) throws RESTSearchResponseException { + public String getJsonResponse(String repositoryUrl, String groupId) throws RestSearchResponseException { return getJsonResponse(repositoryUrl, groupId, null); } @@ -64,7 +64,7 @@ public String getJsonResponse(String repositoryUrl, String groupId) throws RESTS @Override @JsonIgnore public String getJsonResponse(String repositoryUrl, String groupId, String authToken) - throws RESTSearchResponseException { + throws RestSearchResponseException { String targetLink = repositoryUrl + "/" + MavenSearchRepositoryConstants.NEXUS2_TARGET_LINK + "?q=" + groupId; LOG.info("Starting {} search REST API request with URL: {}.", getRepositoryType(), targetLink); diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/Nexus3SearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/Nexus3SearchResponse.java index 98c94ed9c7..0cd87f5bf3 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/Nexus3SearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/Nexus3SearchResponse.java @@ -11,7 +11,7 @@ import com.devonfw.cobigen.api.constants.MavenSearchRepositoryConstants; import com.devonfw.cobigen.api.constants.MavenSearchRepositoryType; -import com.devonfw.cobigen.api.exception.RESTSearchResponseException; +import com.devonfw.cobigen.api.exception.RestSearchResponseException; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; @@ -50,7 +50,7 @@ public List getDownloadURLs() throws MalformedURLException { @Override @JsonIgnore - public String getJsonResponse(String repositoryUrl, String groupId) throws RESTSearchResponseException { + public String getJsonResponse(String repositoryUrl, String groupId) throws RestSearchResponseException { return getJsonResponse(repositoryUrl, groupId, null); } @@ -58,7 +58,7 @@ public String getJsonResponse(String repositoryUrl, String groupId) throws RESTS @Override @JsonIgnore public String getJsonResponse(String repositoryUrl, String groupId, String authToken) - throws RESTSearchResponseException { + throws RestSearchResponseException { String targetLink = repositoryUrl + "/" + MavenSearchRepositoryConstants.NEXUS3_TARGET_LINK + "?repository=maven-central" + "&group=" + groupId; diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponse.java index 358e52c3d3..2cf037f8a7 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponse.java @@ -5,7 +5,7 @@ import java.util.List; import com.devonfw.cobigen.api.constants.MavenSearchRepositoryType; -import com.devonfw.cobigen.api.exception.RESTSearchResponseException; +import com.devonfw.cobigen.api.exception.RestSearchResponseException; import com.fasterxml.jackson.annotation.JsonProperty; /** @@ -33,9 +33,9 @@ public interface SearchResponse { * @param repositoryUrl URL of the repository * @param groupId to search for * @return String of json response - * @throws RESTSearchResponseException if the request did not return status 200 + * @throws RestSearchResponseException if the request did not return status 200 */ - String getJsonResponse(String repositoryUrl, String groupId) throws RESTSearchResponseException; + String getJsonResponse(String repositoryUrl, String groupId) throws RestSearchResponseException; /** * Gets the json response using bearer authentication token @@ -44,8 +44,8 @@ public interface SearchResponse { * @param groupId to search for * @param authToken bearer token to use for authentication * @return String of json response - * @throws RESTSearchResponseException if the request did not return status 200 + * @throws RestSearchResponseException if the request did not return status 200 */ - String getJsonResponse(String repositoryUrl, String groupId, String authToken) throws RESTSearchResponseException; + String getJsonResponse(String repositoryUrl, String groupId, String authToken) throws RestSearchResponseException; } \ No newline at end of file diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseFactory.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseFactory.java index 08b92bd83f..1a2ab597e7 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseFactory.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseFactory.java @@ -8,7 +8,7 @@ import org.slf4j.LoggerFactory; import com.devonfw.cobigen.api.exception.CobiGenRuntimeException; -import com.devonfw.cobigen.api.exception.RESTSearchResponseException; +import com.devonfw.cobigen.api.exception.RestSearchResponseException; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; @@ -37,14 +37,14 @@ public class SearchResponseFactory { * @param groupId the groupId to search for * @param authToken bearer token to use for authentication * @return List of download URLs - * @throws RESTSearchResponseException if an error occurred while accessing the server + * @throws RestSearchResponseException if an error occurred while accessing the server * @throws JsonProcessingException if the json processing was not possible * @throws JsonMappingException if the json mapping was not possible * @throws MalformedURLException if an URL was malformed * */ public static List getArtifactDownloadLinks(String baseURL, String groupId, String authToken) - throws RESTSearchResponseException, JsonMappingException, JsonProcessingException, MalformedURLException { + throws RestSearchResponseException, JsonMappingException, JsonProcessingException, MalformedURLException { ObjectMapper mapper = new ObjectMapper(); List downloadLinks = null; @@ -56,7 +56,7 @@ public static List getArtifactDownloadLinks(String baseURL, String groupId, String jsonResponse = searchResponse.getJsonResponse(baseURL, groupId, authToken); SearchResponse response = mapper.readValue(jsonResponse, searchResponse.getClass()); return response.getDownloadURLs(); - } catch (RESTSearchResponseException e) { + } catch (RestSearchResponseException e) { LOG.debug("It was not possible to get a response from {} using the URL: {}.\n Following error occured:\n {}", searchResponse.getRepositoryType(), baseURL, e.getMessage()); } catch (ProcessingException e) { diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseUtil.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseUtil.java index 835ab794cf..c4dbe10aec 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseUtil.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseUtil.java @@ -5,7 +5,7 @@ import org.glassfish.jersey.client.oauth2.OAuth2ClientSupport; -import com.devonfw.cobigen.api.exception.RESTSearchResponseException; +import com.devonfw.cobigen.api.exception.RestSearchResponseException; import jakarta.ws.rs.client.Client; import jakarta.ws.rs.client.ClientBuilder; @@ -44,10 +44,10 @@ public static WebTarget bearerAuthenticationWithOAuth2AtClientLevel(String targe * @param targetLink link to get response from * @param authToken bearer token to use for authentication * @return String of json response - * @throws RESTSearchResponseException if the returned status code was not 200 OK + * @throws RestSearchResponseException if the returned status code was not 200 OK */ public static String getJsonResponseStringByTargetLink(String targetLink, String authToken) - throws RESTSearchResponseException { + throws RestSearchResponseException { WebTarget target = null; @@ -67,7 +67,7 @@ public static String getJsonResponseStringByTargetLink(String targetLink, String if (status == 200) { jsonResponse = response.readEntity(String.class); } else { - throw new RESTSearchResponseException("The search REST API returned the unexpected status code: ", + throw new RestSearchResponseException("The search REST API returned the unexpected status code: ", String.valueOf(status)); } return jsonResponse; diff --git a/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java b/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java index f2137a15b5..fd23f3746e 100644 --- a/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java +++ b/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java @@ -13,7 +13,7 @@ import com.devonfw.cobigen.api.constants.MavenSearchRepositoryConstants; import com.devonfw.cobigen.api.exception.CobiGenRuntimeException; -import com.devonfw.cobigen.api.exception.RESTSearchResponseException; +import com.devonfw.cobigen.api.exception.RestSearchResponseException; import com.devonfw.cobigen.api.util.MavenUtil; import com.devonfw.cobigen.api.util.to.JfrogSearchResponse; import com.devonfw.cobigen.api.util.to.MavenSearchResponse; @@ -62,7 +62,7 @@ public void testWrongTargetLinkAndTokenThrowsException() { /** * Tests if an exception gets thrown when a status code was not 200 */ - @Test(expected = RESTSearchResponseException.class) + @Test(expected = RestSearchResponseException.class) public void testWrongResponseStatusCodeThrowsException() { SearchResponseUtil.getJsonResponseStringByTargetLink("https://search.maven.org/solrsearch/select?test", null); From ff95670f8ee42a5e14a4d13cab42d3533f59427c Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Wed, 24 Aug 2022 10:34:19 +0200 Subject: [PATCH 20/42] #1529 implemented requested changes renamed getMavenArtifactsByGroupId to retrieveMavenArtifactsByGroupId --- .../java/com/devonfw/cobigen/api/util/MavenUtil.java | 6 +++--- .../java/com/devonfw/cobigen/api/MavenUtilTest.java | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/MavenUtil.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/MavenUtil.java index d2570aa6d6..17e675bb5c 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/MavenUtil.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/MavenUtil.java @@ -345,15 +345,15 @@ public static Path getProjectRoot(Path inputFile, boolean topLevel) { } /** - * Gets a list of download URLs by groupId from the specified repository search REST API using authentication with - * bearer token + * Retrieves a list of download URLs by groupId from the specified repository search REST API using authentication + * with bearer token * * @param baseURL String of the repository server URL * @param groupId the groupId to search for * @param authToken bearer token to use for authentication * @return List of artifact download URLS */ - public static List getMavenArtifactsByGroupId(String baseURL, String groupId, String authToken) { + public static List retrieveMavenArtifactsByGroupId(String baseURL, String groupId, String authToken) { try { return SearchResponseFactory.getArtifactDownloadLinks(baseURL, groupId, authToken); diff --git a/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java b/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java index fd23f3746e..cbfd6e2290 100644 --- a/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java +++ b/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java @@ -38,7 +38,7 @@ public class MavenUtilTest { @Test(expected = CobiGenRuntimeException.class) public void testWrongRepositoryTypeThrowsException() { - assertThat(MavenUtil.getMavenArtifactsByGroupId("this/is/not/a/link", "test", null)); + assertThat(MavenUtil.retrieveMavenArtifactsByGroupId("this/is/not/a/link", "test", null)); } /** @@ -175,7 +175,7 @@ public void testMavenSearchRequestGetsValidDownloadLinks() throws IOException { List downloadList; - downloadList = MavenUtil.getMavenArtifactsByGroupId(MavenSearchRepositoryConstants.MAVEN_REPOSITORY_URL, + downloadList = MavenUtil.retrieveMavenArtifactsByGroupId(MavenSearchRepositoryConstants.MAVEN_REPOSITORY_URL, "com.google.inject", null); assertThat(downloadList).contains( @@ -195,7 +195,7 @@ public void testNexus2SearchRequestGetsValidDownloadLinks() throws IOException { List downloadList; - downloadList = MavenUtil.getMavenArtifactsByGroupId(MavenSearchRepositoryConstants.NEXUS2_REPOSITORY_URL, + downloadList = MavenUtil.retrieveMavenArtifactsByGroupId(MavenSearchRepositoryConstants.NEXUS2_REPOSITORY_URL, "com.devonfw.cobigen", null); assertThat(downloadList).contains(new URL( @@ -213,7 +213,7 @@ public void testNexus3SearchRequestGetsValidDownloadLinks() throws IOException { List downloadList; - downloadList = MavenUtil.getMavenArtifactsByGroupId(MavenSearchRepositoryConstants.NEXUS3_REPOSITORY_URL, + downloadList = MavenUtil.retrieveMavenArtifactsByGroupId(MavenSearchRepositoryConstants.NEXUS3_REPOSITORY_URL, "com.devonfw.cobigen", null); assertThat(downloadList).contains(new URL( @@ -233,7 +233,7 @@ public void testJfrogSearchRequestGetsValidDownloadLinks() throws IOException { List downloadList; - downloadList = MavenUtil.getMavenArtifactsByGroupId(MavenSearchRepositoryConstants.JFROG_REPOSITORY_URL, + downloadList = MavenUtil.retrieveMavenArtifactsByGroupId(MavenSearchRepositoryConstants.JFROG_REPOSITORY_URL, "com.devonfw.cobigen", null); assertThat(downloadList).contains(new URL( "https://localjfrog.com/artifactory/api/storage/maven-remote-cache/com/devonfw/cobigen/cli-parent/2021.04.001-SNAPSHOT/cli-parent-2021.04.001-SNAPSHOT.pom"), From eac63f684c7c74817a40a5174ee724bea1c96a31 Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Wed, 24 Aug 2022 10:36:49 +0200 Subject: [PATCH 21/42] #1529 implemented requested changes renamed getArtifactDownloadLinks to searchArtifactDownloadLinks --- .../src/main/java/com/devonfw/cobigen/api/util/MavenUtil.java | 2 +- .../devonfw/cobigen/api/util/to/SearchResponseFactory.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/MavenUtil.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/MavenUtil.java index 17e675bb5c..aac364bebf 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/MavenUtil.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/MavenUtil.java @@ -356,7 +356,7 @@ public static Path getProjectRoot(Path inputFile, boolean topLevel) { public static List retrieveMavenArtifactsByGroupId(String baseURL, String groupId, String authToken) { try { - return SearchResponseFactory.getArtifactDownloadLinks(baseURL, groupId, authToken); + return SearchResponseFactory.searchArtifactDownloadLinks(baseURL, groupId, authToken); } catch (RestSearchResponseException | JsonProcessingException | MalformedURLException e) { throw new CobiGenRuntimeException("Unable to get artifacts from " + baseURL + " by groupId " + groupId, e); } diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseFactory.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseFactory.java index 1a2ab597e7..1cc559edcd 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseFactory.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseFactory.java @@ -31,7 +31,7 @@ public class SearchResponseFactory { new JfrogSearchResponse(), new Nexus2SearchResponse(), new Nexus3SearchResponse()); /** - * Gets the maven artifact download links by given base URL, groupId and optional authentication token + * Searches for the maven artifact download links by given base URL, groupId and optional authentication token * * @param baseURL String of the repository server URL * @param groupId the groupId to search for @@ -43,7 +43,7 @@ public class SearchResponseFactory { * @throws MalformedURLException if an URL was malformed * */ - public static List getArtifactDownloadLinks(String baseURL, String groupId, String authToken) + public static List searchArtifactDownloadLinks(String baseURL, String groupId, String authToken) throws RestSearchResponseException, JsonMappingException, JsonProcessingException, MalformedURLException { ObjectMapper mapper = new ObjectMapper(); From d1af594a980fd437ced1dd9afd18221af085604e Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Wed, 24 Aug 2022 10:54:48 +0200 Subject: [PATCH 22/42] #1529 implemented requested changes moved multi search response classes to separate packages --- .../api/util/to/SearchResponseFactory.java | 4 + .../to/{ => jfrog}/JfrogSearchResponse.java | 28 +--- .../to/jfrog/JfrogSearchResponseResult.java | 28 ++++ .../to/{ => maven}/MavenSearchResponse.java | 145 +----------------- .../util/to/maven/MavenSearchResponseDoc.java | 106 +++++++++++++ .../to/maven/MavenSearchResponseResponse.java | 47 ++++++ .../Nexus2SearchResponeArtifactLinks.java | 25 +++ .../to/{ => nexus2}/Nexus2SearchResponse.java | 95 +----------- .../Nexus2SearchResponseArtifactHits.java | 22 +++ .../to/nexus2/Nexus2SearchResponseData.java | 64 ++++++++ .../to/{ => nexus3}/Nexus3SearchResponse.java | 36 +---- .../to/nexus3/Nexus3SearchResponseAsset.java | 20 +++ .../to/nexus3/Nexus3SearchResponseItem.java | 22 +++ .../devonfw/cobigen/api/MavenUtilTest.java | 8 +- 14 files changed, 355 insertions(+), 295 deletions(-) rename cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/{ => jfrog}/JfrogSearchResponse.java (87%) create mode 100644 cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/jfrog/JfrogSearchResponseResult.java rename cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/{ => maven}/MavenSearchResponse.java (57%) create mode 100644 cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponseDoc.java create mode 100644 cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponseResponse.java create mode 100644 cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponeArtifactLinks.java rename cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/{ => nexus2}/Nexus2SearchResponse.java (65%) create mode 100644 cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponseArtifactHits.java create mode 100644 cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponseData.java rename cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/{ => nexus3}/Nexus3SearchResponse.java (79%) create mode 100644 cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponseAsset.java create mode 100644 cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponseItem.java diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseFactory.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseFactory.java index 1cc559edcd..cf57349c18 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseFactory.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseFactory.java @@ -9,6 +9,10 @@ import com.devonfw.cobigen.api.exception.CobiGenRuntimeException; import com.devonfw.cobigen.api.exception.RestSearchResponseException; +import com.devonfw.cobigen.api.util.to.jfrog.JfrogSearchResponse; +import com.devonfw.cobigen.api.util.to.maven.MavenSearchResponse; +import com.devonfw.cobigen.api.util.to.nexus2.Nexus2SearchResponse; +import com.devonfw.cobigen.api.util.to.nexus3.Nexus3SearchResponse; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/JfrogSearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/jfrog/JfrogSearchResponse.java similarity index 87% rename from cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/JfrogSearchResponse.java rename to cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/jfrog/JfrogSearchResponse.java index f4f06269e1..7cbf0f79a5 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/JfrogSearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/jfrog/JfrogSearchResponse.java @@ -1,4 +1,4 @@ -package com.devonfw.cobigen.api.util.to; +package com.devonfw.cobigen.api.util.to.jfrog; import java.net.MalformedURLException; import java.net.URL; @@ -11,6 +11,8 @@ import com.devonfw.cobigen.api.constants.MavenSearchRepositoryConstants; import com.devonfw.cobigen.api.constants.MavenSearchRepositoryType; import com.devonfw.cobigen.api.exception.RestSearchResponseException; +import com.devonfw.cobigen.api.util.to.SearchResponse; +import com.devonfw.cobigen.api.util.to.SearchResponseUtil; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; @@ -80,27 +82,3 @@ public MavenSearchRepositoryType getRepositoryType() { return MavenSearchRepositoryType.jfrog; } } - -/** - * - * Jfrog search response result model - * - */ -class JfrogSearchResponseResult { - - /** - * uri - */ - @JsonProperty("uri") - private String uri; - - /** - * @return uri - */ - @JsonIgnore - public String getUri() { - - return this.uri; - } - -} diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/jfrog/JfrogSearchResponseResult.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/jfrog/JfrogSearchResponseResult.java new file mode 100644 index 0000000000..4813a5836f --- /dev/null +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/jfrog/JfrogSearchResponseResult.java @@ -0,0 +1,28 @@ +package com.devonfw.cobigen.api.util.to.jfrog; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * + * Jfrog search response result model + * + */ +class JfrogSearchResponseResult { + + /** + * uri + */ + @JsonProperty("uri") + private String uri; + + /** + * @return uri + */ + @JsonIgnore + public String getUri() { + + return this.uri; + } + +} diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/MavenSearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponse.java similarity index 57% rename from cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/MavenSearchResponse.java rename to cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponse.java index e342c8fb3a..4d459f0204 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/MavenSearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponse.java @@ -1,4 +1,4 @@ -package com.devonfw.cobigen.api.util.to; +package com.devonfw.cobigen.api.util.to.maven; import java.net.MalformedURLException; import java.net.URL; @@ -11,6 +11,8 @@ import com.devonfw.cobigen.api.constants.MavenSearchRepositoryConstants; import com.devonfw.cobigen.api.constants.MavenSearchRepositoryType; import com.devonfw.cobigen.api.exception.RestSearchResponseException; +import com.devonfw.cobigen.api.util.to.SearchResponse; +import com.devonfw.cobigen.api.util.to.SearchResponseUtil; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; @@ -88,143 +90,4 @@ public MavenSearchRepositoryType getRepositoryType() { return MavenSearchRepositoryType.maven; } -} - -/** - * - * Maven search response doc model - * - */ -@JsonIgnoreProperties(value = { "p", "timestamp", "versionCount", "text" }) -class MavenSearchResponseDoc { - /** - * id - */ - @JsonProperty("id") - private String id; - - /** - * group - */ - @JsonProperty("g") - private String group; - - /** - * artifact - */ - @JsonProperty("a") - private String artifact; - - /** - * latest version - */ - @JsonProperty("latestVersion") - private String latestVersion; - - /** - * repository ID - */ - @JsonProperty("repositoryId") - private String repositoryId; - - /** - * ec (file ending) - */ - @JsonProperty("ec") - private List ec; - - /** - * @return ec - */ - @JsonIgnore - public List getEc() { - - return this.ec; - } - - /** - * @return id - */ - @JsonIgnore - public String getId() { - - return this.id; - } - - /** - * @return group - */ - @JsonIgnore - public String getGroup() { - - return this.group; - } - - /** - * @return artifact - */ - @JsonIgnore - public String getArtifact() { - - return this.artifact; - } - - /** - * @return latestVersion - */ - @JsonIgnore - public String getLatestVersion() { - - return this.latestVersion; - } - - /** - * @return repositoryId - */ - @JsonIgnore - public String getRepositoryId() { - - return this.repositoryId; - } - -} - -/** - * - * Maven search response model - * - */ -@JsonIgnoreProperties(value = { "start" }) -class MavenSearchResponseResponse { - - /** - * found results - */ - @JsonProperty("numFound") - private int numFound; - - /** - * docs - */ - @JsonProperty("docs") - private List docs; - - /** - * @return numFound - */ - @JsonIgnore - public int getNumFound() { - - return this.numFound; - } - - /** - * @return docs - */ - @JsonIgnore - public List getDocs() { - - return this.docs; - } - -} +} \ No newline at end of file diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponseDoc.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponseDoc.java new file mode 100644 index 0000000000..61781c1664 --- /dev/null +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponseDoc.java @@ -0,0 +1,106 @@ +package com.devonfw.cobigen.api.util.to.maven; + +import java.util.List; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * + * Maven search response doc model + * + */ +@JsonIgnoreProperties(value = { "p", "timestamp", "versionCount", "text" }) +class MavenSearchResponseDoc { + /** + * id + */ + @JsonProperty("id") + private String id; + + /** + * group + */ + @JsonProperty("g") + private String group; + + /** + * artifact + */ + @JsonProperty("a") + private String artifact; + + /** + * latest version + */ + @JsonProperty("latestVersion") + private String latestVersion; + + /** + * repository ID + */ + @JsonProperty("repositoryId") + private String repositoryId; + + /** + * ec (file ending) + */ + @JsonProperty("ec") + private List ec; + + /** + * @return ec + */ + @JsonIgnore + public List getEc() { + + return this.ec; + } + + /** + * @return id + */ + @JsonIgnore + public String getId() { + + return this.id; + } + + /** + * @return group + */ + @JsonIgnore + public String getGroup() { + + return this.group; + } + + /** + * @return artifact + */ + @JsonIgnore + public String getArtifact() { + + return this.artifact; + } + + /** + * @return latestVersion + */ + @JsonIgnore + public String getLatestVersion() { + + return this.latestVersion; + } + + /** + * @return repositoryId + */ + @JsonIgnore + public String getRepositoryId() { + + return this.repositoryId; + } + +} diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponseResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponseResponse.java new file mode 100644 index 0000000000..bcd58ce5c7 --- /dev/null +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponseResponse.java @@ -0,0 +1,47 @@ +package com.devonfw.cobigen.api.util.to.maven; + +import java.util.List; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * + * Maven search response model + * + */ +@JsonIgnoreProperties(value = { "start" }) +class MavenSearchResponseResponse { + + /** + * found results + */ + @JsonProperty("numFound") + private int numFound; + + /** + * docs + */ + @JsonProperty("docs") + private List docs; + + /** + * @return numFound + */ + @JsonIgnore + public int getNumFound() { + + return this.numFound; + } + + /** + * @return docs + */ + @JsonIgnore + public List getDocs() { + + return this.docs; + } + +} diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponeArtifactLinks.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponeArtifactLinks.java new file mode 100644 index 0000000000..462bc8c916 --- /dev/null +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponeArtifactLinks.java @@ -0,0 +1,25 @@ +package com.devonfw.cobigen.api.util.to.nexus2; + +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * + * Nexus 2 search response artifact links model + * + */ +class Nexus2SearchResponeArtifactLinks { + + @JsonProperty("extension") + private String extension; + + /** + * @return extension + */ + public String getExtension() { + + return this.extension; + } + + @JsonProperty("classifier") + private String classifier; +} diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/Nexus2SearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponse.java similarity index 65% rename from cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/Nexus2SearchResponse.java rename to cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponse.java index 4f585a7580..c07ffb9816 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/Nexus2SearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponse.java @@ -1,4 +1,4 @@ -package com.devonfw.cobigen.api.util.to; +package com.devonfw.cobigen.api.util.to.nexus2; import java.net.MalformedURLException; import java.net.URL; @@ -12,6 +12,8 @@ import com.devonfw.cobigen.api.constants.MavenSearchRepositoryConstants; import com.devonfw.cobigen.api.constants.MavenSearchRepositoryType; import com.devonfw.cobigen.api.exception.RestSearchResponseException; +import com.devonfw.cobigen.api.util.to.SearchResponse; +import com.devonfw.cobigen.api.util.to.SearchResponseUtil; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; @@ -82,94 +84,3 @@ public MavenSearchRepositoryType getRepositoryType() { return MavenSearchRepositoryType.nexus2; } } - -/** - * - * Nexus search response asset model - * - */ -@JsonIgnoreProperties(value = { "repositoryId" }) -class Nexus2SearchResponseArtifactHits { - - /** - * artifactLinks - */ - @JsonProperty("artifactLinks") - public List artifactLinks; - -} - -class Nexus2SearchResponeArtifactLinks { - - @JsonProperty("extension") - private String extension; - - /** - * @return extension - */ - public String getExtension() { - - return this.extension; - } - - @JsonProperty("classifier") - private String classifier; -} - -/** - * - * Nexus2 search response item model - * - */ -@JsonIgnoreProperties(value = { "latestRelease", "latestReleaseRepositoryId", "highlightedFragment" }) -class Nexus2SearchResponseData { - - /** - * groupId - */ - @JsonProperty("groupId") - private String groupId; - - /** - * @return groupId - */ - public String getGroupId() { - - return this.groupId; - } - - /** - * @return artifactId - */ - public String getArtifactId() { - - return this.artifactId; - } - - /** - * @return version - */ - public String getVersion() { - - return this.version; - } - - /** - * artifactId - */ - @JsonProperty("artifactId") - private String artifactId; - - /** - * version - */ - @JsonProperty("version") - private String version; - - /** - * artifactHits - */ - @JsonProperty("artifactHits") - public List artifactHits; - -} diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponseArtifactHits.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponseArtifactHits.java new file mode 100644 index 0000000000..0fd2af0413 --- /dev/null +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponseArtifactHits.java @@ -0,0 +1,22 @@ +package com.devonfw.cobigen.api.util.to.nexus2; + +import java.util.List; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * + * Nexus search response artifacthits model + * + */ +@JsonIgnoreProperties(value = { "repositoryId" }) +class Nexus2SearchResponseArtifactHits { + + /** + * artifactLinks + */ + @JsonProperty("artifactLinks") + public List artifactLinks; + +} diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponseData.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponseData.java new file mode 100644 index 0000000000..1389d1fa2b --- /dev/null +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponseData.java @@ -0,0 +1,64 @@ +package com.devonfw.cobigen.api.util.to.nexus2; + +import java.util.List; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * + * Nexus2 search response data model + * + */ +@JsonIgnoreProperties(value = { "latestRelease", "latestReleaseRepositoryId", "highlightedFragment" }) +class Nexus2SearchResponseData { + + /** + * groupId + */ + @JsonProperty("groupId") + private String groupId; + + /** + * @return groupId + */ + public String getGroupId() { + + return this.groupId; + } + + /** + * @return artifactId + */ + public String getArtifactId() { + + return this.artifactId; + } + + /** + * @return version + */ + public String getVersion() { + + return this.version; + } + + /** + * artifactId + */ + @JsonProperty("artifactId") + private String artifactId; + + /** + * version + */ + @JsonProperty("version") + private String version; + + /** + * artifactHits + */ + @JsonProperty("artifactHits") + public List artifactHits; + +} diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/Nexus3SearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponse.java similarity index 79% rename from cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/Nexus3SearchResponse.java rename to cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponse.java index 0cd87f5bf3..4cb8ac73df 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/Nexus3SearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponse.java @@ -1,4 +1,4 @@ -package com.devonfw.cobigen.api.util.to; +package com.devonfw.cobigen.api.util.to.nexus3; import java.net.MalformedURLException; import java.net.URL; @@ -12,6 +12,8 @@ import com.devonfw.cobigen.api.constants.MavenSearchRepositoryConstants; import com.devonfw.cobigen.api.constants.MavenSearchRepositoryType; import com.devonfw.cobigen.api.exception.RestSearchResponseException; +import com.devonfw.cobigen.api.util.to.SearchResponse; +import com.devonfw.cobigen.api.util.to.SearchResponseUtil; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; @@ -77,35 +79,3 @@ public MavenSearchRepositoryType getRepositoryType() { return MavenSearchRepositoryType.nexus3; } } - -/** - * - * Nexus3 search response asset model - * - */ -@JsonIgnoreProperties(value = { "path", "id", "repository", "format", "checksum" }) -class Nexus3SearchResponseAsset { - - /** - * downloadUrl - */ - @JsonProperty("downloadUrl") - public String downloadUrl; - -} - -/** - * - * Nexus3 search response item model - * - */ -@JsonIgnoreProperties(value = { "id", "repository", "format", "group", "name", "version" }) -class Nexus3SearchResponseItem { - - /** - * artifactHits - */ - @JsonProperty("assets") - public List assets; - -} diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponseAsset.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponseAsset.java new file mode 100644 index 0000000000..0604ae1b1d --- /dev/null +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponseAsset.java @@ -0,0 +1,20 @@ +package com.devonfw.cobigen.api.util.to.nexus3; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * + * Nexus3 search response asset model + * + */ +@JsonIgnoreProperties(value = { "path", "id", "repository", "format", "checksum" }) +class Nexus3SearchResponseAsset { + + /** + * downloadUrl + */ + @JsonProperty("downloadUrl") + public String downloadUrl; + +} diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponseItem.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponseItem.java new file mode 100644 index 0000000000..6add30ea07 --- /dev/null +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponseItem.java @@ -0,0 +1,22 @@ +package com.devonfw.cobigen.api.util.to.nexus3; + +import java.util.List; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * + * Nexus3 search response item model + * + */ +@JsonIgnoreProperties(value = { "id", "repository", "format", "group", "name", "version" }) +class Nexus3SearchResponseItem { + + /** + * artifactHits + */ + @JsonProperty("assets") + public List assets; + +} diff --git a/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java b/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java index cbfd6e2290..4622d791c3 100644 --- a/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java +++ b/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java @@ -15,11 +15,11 @@ import com.devonfw.cobigen.api.exception.CobiGenRuntimeException; import com.devonfw.cobigen.api.exception.RestSearchResponseException; import com.devonfw.cobigen.api.util.MavenUtil; -import com.devonfw.cobigen.api.util.to.JfrogSearchResponse; -import com.devonfw.cobigen.api.util.to.MavenSearchResponse; -import com.devonfw.cobigen.api.util.to.Nexus2SearchResponse; -import com.devonfw.cobigen.api.util.to.Nexus3SearchResponse; import com.devonfw.cobigen.api.util.to.SearchResponseUtil; +import com.devonfw.cobigen.api.util.to.jfrog.JfrogSearchResponse; +import com.devonfw.cobigen.api.util.to.maven.MavenSearchResponse; +import com.devonfw.cobigen.api.util.to.nexus2.Nexus2SearchResponse; +import com.devonfw.cobigen.api.util.to.nexus3.Nexus3SearchResponse; import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.ws.rs.ProcessingException; From f65539ea3222e8aa1e77328327a3889bdeb23950 Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Wed, 24 Aug 2022 11:03:07 +0200 Subject: [PATCH 23/42] #1529 implemented requested changes improved javadoc readability --- .../util/to/jfrog/JfrogSearchResponse.java | 4 +-- .../to/jfrog/JfrogSearchResponseResult.java | 4 +-- .../util/to/maven/MavenSearchResponseDoc.java | 25 ++++---------- .../to/maven/MavenSearchResponseResponse.java | 4 +-- .../util/to/nexus2/Nexus2SearchResponse.java | 1 + .../Nexus2SearchResponseArtifactHits.java | 4 +-- .../to/nexus2/Nexus2SearchResponseData.java | 34 +++++++------------ .../util/to/nexus3/Nexus3SearchResponse.java | 1 + .../to/nexus3/Nexus3SearchResponseAsset.java | 4 +-- .../to/nexus3/Nexus3SearchResponseItem.java | 4 +-- 10 files changed, 28 insertions(+), 57 deletions(-) diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/jfrog/JfrogSearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/jfrog/JfrogSearchResponse.java index 7cbf0f79a5..b51c0b2cab 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/jfrog/JfrogSearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/jfrog/JfrogSearchResponse.java @@ -26,9 +26,7 @@ public class JfrogSearchResponse implements SearchResponse { @JsonIgnore private static final Logger LOG = LoggerFactory.getLogger(JfrogSearchResponse.class); - /** - * results - */ + /** results */ @JsonProperty("results") private List results; diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/jfrog/JfrogSearchResponseResult.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/jfrog/JfrogSearchResponseResult.java index 4813a5836f..c880e12a66 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/jfrog/JfrogSearchResponseResult.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/jfrog/JfrogSearchResponseResult.java @@ -10,9 +10,7 @@ */ class JfrogSearchResponseResult { - /** - * uri - */ + /** uri */ @JsonProperty("uri") private String uri; diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponseDoc.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponseDoc.java index 61781c1664..70429f9389 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponseDoc.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponseDoc.java @@ -13,39 +13,28 @@ */ @JsonIgnoreProperties(value = { "p", "timestamp", "versionCount", "text" }) class MavenSearchResponseDoc { - /** - * id - */ + + /** id */ @JsonProperty("id") private String id; - /** - * group - */ + /** group */ @JsonProperty("g") private String group; - /** - * artifact - */ + /** artifact */ @JsonProperty("a") private String artifact; - /** - * latest version - */ + /** latest version */ @JsonProperty("latestVersion") private String latestVersion; - /** - * repository ID - */ + /** repository ID */ @JsonProperty("repositoryId") private String repositoryId; - /** - * ec (file ending) - */ + /** ec (file ending) */ @JsonProperty("ec") private List ec; diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponseResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponseResponse.java index bcd58ce5c7..5985249b7d 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponseResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponseResponse.java @@ -20,9 +20,7 @@ class MavenSearchResponseResponse { @JsonProperty("numFound") private int numFound; - /** - * docs - */ + /** docs */ @JsonProperty("docs") private List docs; diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponse.java index c07ffb9816..20766a2fff 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponse.java @@ -29,6 +29,7 @@ public class Nexus2SearchResponse implements SearchResponse { @JsonIgnore private static final Logger LOG = LoggerFactory.getLogger(Nexus2SearchResponse.class); + /** data */ @JsonProperty("data") private List data; diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponseArtifactHits.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponseArtifactHits.java index 0fd2af0413..295745435e 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponseArtifactHits.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponseArtifactHits.java @@ -13,9 +13,7 @@ @JsonIgnoreProperties(value = { "repositoryId" }) class Nexus2SearchResponseArtifactHits { - /** - * artifactLinks - */ + /** artifactLinks */ @JsonProperty("artifactLinks") public List artifactLinks; diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponseData.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponseData.java index 1389d1fa2b..e7659c574b 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponseData.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponseData.java @@ -13,12 +13,22 @@ @JsonIgnoreProperties(value = { "latestRelease", "latestReleaseRepositoryId", "highlightedFragment" }) class Nexus2SearchResponseData { - /** - * groupId - */ + /** groupId */ @JsonProperty("groupId") private String groupId; + /** artifactId */ + @JsonProperty("artifactId") + private String artifactId; + + /** version */ + @JsonProperty("version") + private String version; + + /** artifactHits */ + @JsonProperty("artifactHits") + public List artifactHits; + /** * @return groupId */ @@ -43,22 +53,4 @@ public String getVersion() { return this.version; } - /** - * artifactId - */ - @JsonProperty("artifactId") - private String artifactId; - - /** - * version - */ - @JsonProperty("version") - private String version; - - /** - * artifactHits - */ - @JsonProperty("artifactHits") - public List artifactHits; - } diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponse.java index 4cb8ac73df..80302260f3 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponse.java @@ -29,6 +29,7 @@ public class Nexus3SearchResponse implements SearchResponse { @JsonIgnore private static final Logger LOG = LoggerFactory.getLogger(Nexus3SearchResponse.class); + /** items */ @JsonProperty("items") private List items; diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponseAsset.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponseAsset.java index 0604ae1b1d..f7638f719b 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponseAsset.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponseAsset.java @@ -11,9 +11,7 @@ @JsonIgnoreProperties(value = { "path", "id", "repository", "format", "checksum" }) class Nexus3SearchResponseAsset { - /** - * downloadUrl - */ + /** downloadUrl */ @JsonProperty("downloadUrl") public String downloadUrl; diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponseItem.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponseItem.java index 6add30ea07..3f4e3e4ed2 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponseItem.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponseItem.java @@ -13,9 +13,7 @@ @JsonIgnoreProperties(value = { "id", "repository", "format", "group", "name", "version" }) class Nexus3SearchResponseItem { - /** - * artifactHits - */ + /** artifactHits */ @JsonProperty("assets") public List assets; From f9abab39fb7d5291794132656aaf300f9cf17135 Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Wed, 24 Aug 2022 12:56:15 +0200 Subject: [PATCH 24/42] #1529 implemented requested changes converted SearchResponse to AbstractSearchResponse refactored getJsonResponse added new retrieveJsonResponseWithAuthentication method to AbstractSearchResponse renamed getJsonResponse to retrieveJsonResponse --- .../api/util/to/AbstractSearchResponse.java | 79 +++++++++++++++++++ .../cobigen/api/util/to/SearchResponse.java | 51 ------------ .../api/util/to/SearchResponseFactory.java | 18 ++--- .../util/to/jfrog/JfrogSearchResponse.java | 21 +---- .../util/to/maven/MavenSearchResponse.java | 21 +---- .../util/to/nexus2/Nexus2SearchResponse.java | 20 +---- .../util/to/nexus3/Nexus3SearchResponse.java | 21 +---- 7 files changed, 104 insertions(+), 127 deletions(-) create mode 100644 cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractSearchResponse.java delete mode 100644 cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponse.java diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractSearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractSearchResponse.java new file mode 100644 index 0000000000..26ed77a60b --- /dev/null +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractSearchResponse.java @@ -0,0 +1,79 @@ +package com.devonfw.cobigen.api.util.to; + +import java.net.MalformedURLException; +import java.net.URL; +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.devonfw.cobigen.api.constants.MavenSearchRepositoryType; +import com.devonfw.cobigen.api.exception.RestSearchResponseException; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * This interface should be inherited for all maven REST search API responses to properly convert {@link JsonProperty} + * from responses to valid download URLs + */ +public abstract class AbstractSearchResponse { + + /** Logger instance. */ + @JsonIgnore + private static final Logger LOG = LoggerFactory.getLogger(AbstractSearchResponse.class); + + /** + * @return the {@link MavenSearchRepositoryType} type + */ + public abstract MavenSearchRepositoryType getRepositoryType(); + + /** + * Creates a list of download URLs + * + * @return List of download links + * @throws MalformedURLException if an URL was not valid + */ + public abstract List getDownloadURLs() throws MalformedURLException; + + /** + * Retrieves the json response from a repository URL and a group ID + * + * @param repositoryUrl URL of the repository + * @param groupId to search for + * @return String of json response + * @throws RestSearchResponseException if the request did not return status 200 + */ + public String retrieveJsonResponse(String repositoryUrl, String groupId) throws RestSearchResponseException { + + return retrieveJsonResponse(repositoryUrl, groupId, null); + } + + /** + * Retrieves the json response from a repository URL, a group ID and a bearer authentication token + * + * @param repositoryUrl URL of the repository + * @param groupId to search for + * @param authToken bearer token to use for authentication + * @return String of json response + * @throws RestSearchResponseException if the request did not return status 200 + */ + public abstract String retrieveJsonResponse(String repositoryUrl, String groupId, String authToken) + throws RestSearchResponseException; + + /** + * Retrieves the json response from the search API target link using bearer authentication token + * + * @param targetLink link to get response from + * @param authToken bearer token to use for authentication + * @return String of json response + * @throws RestSearchResponseException if the request did not return status 200 + */ + public String retrieveJsonResponseWithAuthenticationToken(String targetLink, String authToken) + throws RestSearchResponseException { + + LOG.info("Starting {} search REST API request with URL: {}.", getRepositoryType(), targetLink); + + return SearchResponseUtil.getJsonResponseStringByTargetLink(targetLink, authToken); + } + +} \ No newline at end of file diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponse.java deleted file mode 100644 index 2cf037f8a7..0000000000 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponse.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.devonfw.cobigen.api.util.to; - -import java.net.MalformedURLException; -import java.net.URL; -import java.util.List; - -import com.devonfw.cobigen.api.constants.MavenSearchRepositoryType; -import com.devonfw.cobigen.api.exception.RestSearchResponseException; -import com.fasterxml.jackson.annotation.JsonProperty; - -/** - * This interface should be inherited for all maven REST search API responses to properly convert {@link JsonProperty} - * from responses to valid download URLs - */ -public interface SearchResponse { - - /** - * @return the {@link MavenSearchRepositoryType} type - */ - public MavenSearchRepositoryType getRepositoryType(); - - /** - * Creates a list of download URLs - * - * @return List of download links - * @throws MalformedURLException if an URL was not valid - */ - List getDownloadURLs() throws MalformedURLException; - - /** - * Gets the json response - * - * @param repositoryUrl URL of the repository - * @param groupId to search for - * @return String of json response - * @throws RestSearchResponseException if the request did not return status 200 - */ - String getJsonResponse(String repositoryUrl, String groupId) throws RestSearchResponseException; - - /** - * Gets the json response using bearer authentication token - * - * @param repositoryUrl URL of the repository - * @param groupId to search for - * @param authToken bearer token to use for authentication - * @return String of json response - * @throws RestSearchResponseException if the request did not return status 200 - */ - String getJsonResponse(String repositoryUrl, String groupId, String authToken) throws RestSearchResponseException; - -} \ No newline at end of file diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseFactory.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseFactory.java index cf57349c18..dda3ec9636 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseFactory.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseFactory.java @@ -21,7 +21,7 @@ import jakarta.ws.rs.ProcessingException; /** - * Factory to create new instances of {@link SearchResponse} which handles the responses from various search REST APIs. + * Factory to create new instances of {@link AbstractSearchResponse} which handles the responses from various search REST APIs. */ public class SearchResponseFactory { @@ -29,9 +29,9 @@ public class SearchResponseFactory { static final Logger LOG = LoggerFactory.getLogger(SearchResponseFactory.class); /** - * List of available {@link SearchResponse} implementations (add new search REST API responses here) + * List of available {@link AbstractSearchResponse} implementations (add new search REST API responses here) */ - private static final List SEARCH_RESPONSES = Lists.newArrayList(new MavenSearchResponse(), + private static final List SEARCH_RESPONSES = Lists.newArrayList(new MavenSearchResponse(), new JfrogSearchResponse(), new Nexus2SearchResponse(), new Nexus3SearchResponse()); /** @@ -53,16 +53,16 @@ public static List searchArtifactDownloadLinks(String baseURL, String group ObjectMapper mapper = new ObjectMapper(); List downloadLinks = null; - for (SearchResponse searchResponse : SEARCH_RESPONSES) { + for (Object searchResponse : SEARCH_RESPONSES) { try { - LOG.debug("Trying to get a response from {} with server URL: {} ...", searchResponse.getRepositoryType(), - baseURL); - String jsonResponse = searchResponse.getJsonResponse(baseURL, groupId, authToken); - SearchResponse response = mapper.readValue(jsonResponse, searchResponse.getClass()); + LOG.debug("Trying to get a response from {} with server URL: {} ...", + ((AbstractSearchResponse) searchResponse).getRepositoryType(), baseURL); + String jsonResponse = ((AbstractSearchResponse) searchResponse).retrieveJsonResponse(baseURL, groupId, authToken); + AbstractSearchResponse response = (AbstractSearchResponse) mapper.readValue(jsonResponse, searchResponse.getClass()); return response.getDownloadURLs(); } catch (RestSearchResponseException e) { LOG.debug("It was not possible to get a response from {} using the URL: {}.\n Following error occured:\n {}", - searchResponse.getRepositoryType(), baseURL, e.getMessage()); + ((AbstractSearchResponse) searchResponse).getRepositoryType(), baseURL, e.getMessage()); } catch (ProcessingException e) { String errorMsg = "The search REST API was not able to process the URL: " + baseURL; LOG.error(errorMsg, e); diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/jfrog/JfrogSearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/jfrog/JfrogSearchResponse.java index b51c0b2cab..1b6da2f393 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/jfrog/JfrogSearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/jfrog/JfrogSearchResponse.java @@ -11,8 +11,7 @@ import com.devonfw.cobigen.api.constants.MavenSearchRepositoryConstants; import com.devonfw.cobigen.api.constants.MavenSearchRepositoryType; import com.devonfw.cobigen.api.exception.RestSearchResponseException; -import com.devonfw.cobigen.api.util.to.SearchResponse; -import com.devonfw.cobigen.api.util.to.SearchResponseUtil; +import com.devonfw.cobigen.api.util.to.AbstractSearchResponse; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; @@ -20,7 +19,7 @@ * Json model for jfrog Search REST API response * */ -public class JfrogSearchResponse implements SearchResponse { +public class JfrogSearchResponse extends AbstractSearchResponse { /** Logger instance. */ @JsonIgnore @@ -41,24 +40,12 @@ public List getResults() { @Override @JsonIgnore - public String getJsonResponse(String repositoryUrl, String groupId) throws RestSearchResponseException { - - return getJsonResponse(repositoryUrl, groupId, null); - } - - @Override - @JsonIgnore - public String getJsonResponse(String repositoryUrl, String groupId, String authToken) + public String retrieveJsonResponse(String repositoryUrl, String groupId, String authToken) throws RestSearchResponseException { String targetLink = repositoryUrl + "/" + MavenSearchRepositoryConstants.JFROG_TARGET_LINK + "?g=" + groupId; - LOG.info("Starting {} search REST API request with URL: {}.", getRepositoryType(), targetLink); - - String jsonResponse; - - jsonResponse = SearchResponseUtil.getJsonResponseStringByTargetLink(targetLink, authToken); - return jsonResponse; + return retrieveJsonResponseWithAuthenticationToken(targetLink, authToken); } @Override diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponse.java index 4d459f0204..9b703fac11 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponse.java @@ -11,7 +11,7 @@ import com.devonfw.cobigen.api.constants.MavenSearchRepositoryConstants; import com.devonfw.cobigen.api.constants.MavenSearchRepositoryType; import com.devonfw.cobigen.api.exception.RestSearchResponseException; -import com.devonfw.cobigen.api.util.to.SearchResponse; +import com.devonfw.cobigen.api.util.to.AbstractSearchResponse; import com.devonfw.cobigen.api.util.to.SearchResponseUtil; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; @@ -22,7 +22,7 @@ * */ @JsonIgnoreProperties(value = { "responseHeader", "spellcheck" }) -public class MavenSearchResponse implements SearchResponse { +public class MavenSearchResponse extends AbstractSearchResponse { /** Logger instance. */ @JsonIgnore @@ -42,26 +42,13 @@ public MavenSearchResponseResponse getResponse() { @Override @JsonIgnore - public String getJsonResponse(String repositoryUrl, String groupId) throws RestSearchResponseException { - - return getJsonResponse(repositoryUrl, groupId, null); - } - - @Override - @JsonIgnore - public String getJsonResponse(String repositoryUrl, String groupId, String authToken) + public String retrieveJsonResponse(String repositoryUrl, String groupId, String authToken) throws RestSearchResponseException { String targetLink = repositoryUrl + "/" + MavenSearchRepositoryConstants.MAVEN_TARGET_LINK + "?q=g:" + groupId + "&wt=json"; - LOG.info("Starting {} search REST API request with URL: {}.", getRepositoryType(), targetLink); - - String jsonResponse; - - jsonResponse = SearchResponseUtil.getJsonResponseStringByTargetLink(targetLink, authToken); - - return jsonResponse; + return retrieveJsonResponseWithAuthenticationToken(targetLink, authToken); } @Override diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponse.java index 20766a2fff..db4e617303 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponse.java @@ -12,7 +12,7 @@ import com.devonfw.cobigen.api.constants.MavenSearchRepositoryConstants; import com.devonfw.cobigen.api.constants.MavenSearchRepositoryType; import com.devonfw.cobigen.api.exception.RestSearchResponseException; -import com.devonfw.cobigen.api.util.to.SearchResponse; +import com.devonfw.cobigen.api.util.to.AbstractSearchResponse; import com.devonfw.cobigen.api.util.to.SearchResponseUtil; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; @@ -23,7 +23,7 @@ * */ @JsonIgnoreProperties(value = { "totalCount", "from", "count", "tooManyResults", "collapsed", "repoDetails" }) -public class Nexus2SearchResponse implements SearchResponse { +public class Nexus2SearchResponse extends AbstractSearchResponse { /** Logger instance. */ @JsonIgnore @@ -59,24 +59,12 @@ public List getDownloadURLs() throws MalformedURLException { @Override @JsonIgnore - public String getJsonResponse(String repositoryUrl, String groupId) throws RestSearchResponseException { - - return getJsonResponse(repositoryUrl, groupId, null); - } - - @Override - @JsonIgnore - public String getJsonResponse(String repositoryUrl, String groupId, String authToken) + public String retrieveJsonResponse(String repositoryUrl, String groupId, String authToken) throws RestSearchResponseException { String targetLink = repositoryUrl + "/" + MavenSearchRepositoryConstants.NEXUS2_TARGET_LINK + "?q=" + groupId; - LOG.info("Starting {} search REST API request with URL: {}.", getRepositoryType(), targetLink); - - String jsonResponse; - - jsonResponse = SearchResponseUtil.getJsonResponseStringByTargetLink(targetLink, authToken); - return jsonResponse; + return retrieveJsonResponseWithAuthenticationToken(targetLink, authToken); } @Override diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponse.java index 80302260f3..1a00381cfc 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponse.java @@ -12,8 +12,7 @@ import com.devonfw.cobigen.api.constants.MavenSearchRepositoryConstants; import com.devonfw.cobigen.api.constants.MavenSearchRepositoryType; import com.devonfw.cobigen.api.exception.RestSearchResponseException; -import com.devonfw.cobigen.api.util.to.SearchResponse; -import com.devonfw.cobigen.api.util.to.SearchResponseUtil; +import com.devonfw.cobigen.api.util.to.AbstractSearchResponse; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; @@ -23,7 +22,7 @@ * */ @JsonIgnoreProperties(value = { "continuationToken" }) -public class Nexus3SearchResponse implements SearchResponse { +public class Nexus3SearchResponse extends AbstractSearchResponse { /** Logger instance. */ @JsonIgnore @@ -53,25 +52,13 @@ public List getDownloadURLs() throws MalformedURLException { @Override @JsonIgnore - public String getJsonResponse(String repositoryUrl, String groupId) throws RestSearchResponseException { - - return getJsonResponse(repositoryUrl, groupId, null); - } - - @Override - @JsonIgnore - public String getJsonResponse(String repositoryUrl, String groupId, String authToken) + public String retrieveJsonResponse(String repositoryUrl, String groupId, String authToken) throws RestSearchResponseException { String targetLink = repositoryUrl + "/" + MavenSearchRepositoryConstants.NEXUS3_TARGET_LINK + "?repository=maven-central" + "&group=" + groupId; - LOG.info("Starting {} search REST API request with URL: {}.", getRepositoryType(), targetLink); - - String jsonResponse; - - jsonResponse = SearchResponseUtil.getJsonResponseStringByTargetLink(targetLink, authToken); - return jsonResponse; + return retrieveJsonResponseWithAuthenticationToken(targetLink, authToken); } @Override From f28296e20e1b862abe11ec4c91f44077b03ddb07 Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Wed, 24 Aug 2022 13:06:34 +0200 Subject: [PATCH 25/42] #1529 implemented requested changes refactored getDownloadURLs method added new removeDuplicatedDownloadURLs method to AbstractSearchResponse renamed getDownloadURLs to retrieveDownloadURLs --- .../api/util/to/AbstractSearchResponse.java | 15 ++++++++++++++- .../api/util/to/SearchResponseFactory.java | 2 +- .../api/util/to/jfrog/JfrogSearchResponse.java | 2 +- .../api/util/to/maven/MavenSearchResponse.java | 2 +- .../api/util/to/nexus2/Nexus2SearchResponse.java | 8 ++------ .../api/util/to/nexus3/Nexus3SearchResponse.java | 8 ++------ .../com/devonfw/cobigen/api/MavenUtilTest.java | 8 ++++---- 7 files changed, 25 insertions(+), 20 deletions(-) diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractSearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractSearchResponse.java index 26ed77a60b..1df3df3db8 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractSearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractSearchResponse.java @@ -3,6 +3,7 @@ import java.net.MalformedURLException; import java.net.URL; import java.util.List; +import java.util.stream.Collectors; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -33,7 +34,19 @@ public abstract class AbstractSearchResponse { * @return List of download links * @throws MalformedURLException if an URL was not valid */ - public abstract List getDownloadURLs() throws MalformedURLException; + public abstract List retrieveDownloadURLs() throws MalformedURLException; + + /** + * Removes duplicates from list of download URLs + * + * @param downloadUrls list of download URLs + * @return List of download links + * @throws MalformedURLException if an URL was not valid + */ + public List removeDuplicatedDownloadURLs(List downloadUrls) throws MalformedURLException { + + return downloadUrls.stream().distinct().collect(Collectors.toList()); + } /** * Retrieves the json response from a repository URL and a group ID diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseFactory.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseFactory.java index dda3ec9636..aa5ecafb0e 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseFactory.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseFactory.java @@ -59,7 +59,7 @@ public static List searchArtifactDownloadLinks(String baseURL, String group ((AbstractSearchResponse) searchResponse).getRepositoryType(), baseURL); String jsonResponse = ((AbstractSearchResponse) searchResponse).retrieveJsonResponse(baseURL, groupId, authToken); AbstractSearchResponse response = (AbstractSearchResponse) mapper.readValue(jsonResponse, searchResponse.getClass()); - return response.getDownloadURLs(); + return response.retrieveDownloadURLs(); } catch (RestSearchResponseException e) { LOG.debug("It was not possible to get a response from {} using the URL: {}.\n Following error occured:\n {}", ((AbstractSearchResponse) searchResponse).getRepositoryType(), baseURL, e.getMessage()); diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/jfrog/JfrogSearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/jfrog/JfrogSearchResponse.java index 1b6da2f393..445e4c5717 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/jfrog/JfrogSearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/jfrog/JfrogSearchResponse.java @@ -50,7 +50,7 @@ public String retrieveJsonResponse(String repositoryUrl, String groupId, String @Override @JsonIgnore - public List getDownloadURLs() throws MalformedURLException { + public List retrieveDownloadURLs() throws MalformedURLException { List downloadLinks = new ArrayList<>(); diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponse.java index 9b703fac11..182a480fc0 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponse.java @@ -53,7 +53,7 @@ public String retrieveJsonResponse(String repositoryUrl, String groupId, String @Override @JsonIgnore - public List getDownloadURLs() throws MalformedURLException { + public List retrieveDownloadURLs() throws MalformedURLException { List downloadLinks = new ArrayList<>(); List docs = getResponse().getDocs(); diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponse.java index db4e617303..b8d50b95bf 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponse.java @@ -4,7 +4,6 @@ import java.net.URL; import java.util.ArrayList; import java.util.List; -import java.util.stream.Collectors; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -35,7 +34,7 @@ public class Nexus2SearchResponse extends AbstractSearchResponse { @Override @JsonIgnore - public List getDownloadURLs() throws MalformedURLException { + public List retrieveDownloadURLs() throws MalformedURLException { List downloadLinks = new ArrayList<>(); @@ -51,10 +50,7 @@ public List getDownloadURLs() throws MalformedURLException { } } - // removes duplicates - List newDownloadList = downloadLinks.stream().distinct().collect(Collectors.toList()); - - return newDownloadList; + return removeDuplicatedDownloadURLs(downloadLinks); } @Override diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponse.java index 1a00381cfc..b31bf3e3e6 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponse.java @@ -4,7 +4,6 @@ import java.net.URL; import java.util.ArrayList; import java.util.List; -import java.util.stream.Collectors; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -34,7 +33,7 @@ public class Nexus3SearchResponse extends AbstractSearchResponse { @Override @JsonIgnore - public List getDownloadURLs() throws MalformedURLException { + public List retrieveDownloadURLs() throws MalformedURLException { List downloadLinks = new ArrayList<>(); @@ -44,10 +43,7 @@ public List getDownloadURLs() throws MalformedURLException { } } - // removes duplicates - List newDownloadList = downloadLinks.stream().distinct().collect(Collectors.toList()); - - return newDownloadList; + return removeDuplicatedDownloadURLs(downloadLinks); } @Override diff --git a/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java b/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java index 4622d791c3..152c5fecad 100644 --- a/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java +++ b/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java @@ -82,7 +82,7 @@ public void testMavenParseDownloadLinks() throws IOException { String jsonResponse = new String(Files.readAllBytes(Paths.get(testdataRoot).resolve("mavenJsonTest.json"))); response = mapper.readValue(jsonResponse, MavenSearchResponse.class); - List downloadLinks = response.getDownloadURLs(); + List downloadLinks = response.retrieveDownloadURLs(); assertThat(downloadLinks).contains( new URL("https://repo1.maven.org/maven2/com/google/inject/guice/5.1.0/guice-5.1.0.jar"), new URL("https://repo1.maven.org/maven2/com/google/inject/guice-bom/5.1.0/guice-bom-5.1.0.pom"), @@ -104,7 +104,7 @@ public void testNexus2ParseDownloadLinks() throws IOException { String jsonResponse = new String(Files.readAllBytes(Paths.get(testdataRoot).resolve("nexus2JsonTest.json"))); response = mapper.readValue(jsonResponse, Nexus2SearchResponse.class); - List downloadLinks = response.getDownloadURLs(); + List downloadLinks = response.retrieveDownloadURLs(); assertThat(downloadLinks).contains(new URL( "https://s01.oss.sonatype.org/service/local/repositories/releases/content/com/devonfw/cobigen/openapiplugin/2021.12.006/openapiplugin-2021.12.006.pom"), new URL( @@ -137,7 +137,7 @@ public void testNexus3ParseDownloadLinks() throws IOException { String jsonResponse = new String(Files.readAllBytes(Paths.get(testdataRoot).resolve("nexus3JsonTest.json"))); response = mapper.readValue(jsonResponse, Nexus3SearchResponse.class); - List downloadLinks = response.getDownloadURLs(); + List downloadLinks = response.retrieveDownloadURLs(); assertThat(downloadLinks).contains(new URL( "http://localhost:8081/repository/maven-central/org/osgi/org.osgi.core/4.3.1/org.osgi.core-4.3.1-sources.jar"), new URL("http://localhost:8081/repository/maven-central/org/osgi/org.osgi.core/4.3.1/org.osgi.core-4.3.1.jar"), @@ -158,7 +158,7 @@ public void testJfrogParseDownloadLinks() throws IOException { String jsonResponse = new String(Files.readAllBytes(Paths.get(testdataRoot).resolve("jfrogJsonTest.json"))); response = mapper.readValue(jsonResponse, JfrogSearchResponse.class); - List downloadLinks = response.getDownloadURLs(); + List downloadLinks = response.retrieveDownloadURLs(); assertThat(downloadLinks).contains(new URL( "http://localhost:8081/artifactory/api/storage/libs-release-local/org/acme/artifact/1.0/artifact-1.0-sources.jar"), new URL( From dccffb7b7dff7dc81a308bf794577b20f11c8c97 Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Wed, 24 Aug 2022 13:21:18 +0200 Subject: [PATCH 26/42] #1529 implemented requested changes refactored SearchResponseUtil methods moved SearchResponseUtil methods to AbstractSearchResponse removed SearchResponseUtil class --- .../api/util/to/AbstractSearchResponse.java | 83 +++++++++++++++- .../api/util/to/SearchResponseUtil.java | 97 ------------------- .../util/to/maven/MavenSearchResponse.java | 3 +- .../util/to/nexus2/Nexus2SearchResponse.java | 3 +- .../devonfw/cobigen/api/MavenUtilTest.java | 8 +- 5 files changed, 88 insertions(+), 106 deletions(-) delete mode 100644 cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseUtil.java diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractSearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractSearchResponse.java index 1df3df3db8..053e56c42d 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractSearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractSearchResponse.java @@ -5,6 +5,7 @@ import java.util.List; import java.util.stream.Collectors; +import org.glassfish.jersey.client.oauth2.OAuth2ClientSupport; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -13,6 +14,14 @@ import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; +import jakarta.ws.rs.client.Client; +import jakarta.ws.rs.client.ClientBuilder; +import jakarta.ws.rs.client.Invocation; +import jakarta.ws.rs.client.WebTarget; +import jakarta.ws.rs.core.Feature; +import jakarta.ws.rs.core.MediaType; +import jakarta.ws.rs.core.Response; + /** * This interface should be inherited for all maven REST search API responses to properly convert {@link JsonProperty} * from responses to valid download URLs @@ -86,7 +95,79 @@ public String retrieveJsonResponseWithAuthenticationToken(String targetLink, Str LOG.info("Starting {} search REST API request with URL: {}.", getRepositoryType(), targetLink); - return SearchResponseUtil.getJsonResponseStringByTargetLink(targetLink, authToken); + return getJsonResponseStringByTargetLink(targetLink, authToken); + } + + /** + * Creates a @WebTarget with provided authentication token + * + * @param targetLink link to get response from + * @param token bearer token to use for authentication + * @return WebTarget to use as resource + */ + private static WebTarget bearerAuthenticationWithOAuth2AtClientLevel(String targetLink, String token) { + + Feature feature = OAuth2ClientSupport.feature(token); + Client client = ClientBuilder.newBuilder().register(feature).build(); + + WebTarget target = client.target(targetLink); + return target; + } + + /** + * Gets a json response by given REST API target link using bearer authentication token + * + * @param targetLink link to get response from + * @param authToken bearer token to use for authentication + * @return String of json response + * @throws RestSearchResponseException if the returned status code was not 200 OK + */ + public static String getJsonResponseStringByTargetLink(String targetLink, String authToken) + throws RestSearchResponseException { + + WebTarget target = null; + + if (authToken != null) { + target = bearerAuthenticationWithOAuth2AtClientLevel(targetLink, authToken); + } else { + Client client = ClientBuilder.newClient(); + target = client.target(targetLink); + } + + Response response = null; + Invocation.Builder request = target.request(MediaType.APPLICATION_JSON); + response = request.get(); + + int status = response.getStatus(); + String jsonResponse = ""; + if (status == 200) { + jsonResponse = response.readEntity(String.class); + } else { + throw new RestSearchResponseException("The search REST API returned the unexpected status code: ", + String.valueOf(status)); + } + return jsonResponse; + } + + /** + * Creates a download link (concatenates maven repository link with groupId, artifact and version) + * + * @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 + * @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 parsedGroupId = groupId.replace(".", "/"); + String downloadFile = artifactId + "-" + version + fileEnding; + String downloadLink = mavenRepo + "/" + parsedGroupId + "/" + artifactId + "/" + version + "/" + downloadFile; + URL url = new URL(downloadLink); + return url; } } \ No newline at end of file diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseUtil.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseUtil.java deleted file mode 100644 index c4dbe10aec..0000000000 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseUtil.java +++ /dev/null @@ -1,97 +0,0 @@ -package com.devonfw.cobigen.api.util.to; - -import java.net.MalformedURLException; -import java.net.URL; - -import org.glassfish.jersey.client.oauth2.OAuth2ClientSupport; - -import com.devonfw.cobigen.api.exception.RestSearchResponseException; - -import jakarta.ws.rs.client.Client; -import jakarta.ws.rs.client.ClientBuilder; -import jakarta.ws.rs.client.Invocation; -import jakarta.ws.rs.client.WebTarget; -import jakarta.ws.rs.core.Feature; -import jakarta.ws.rs.core.MediaType; -import jakarta.ws.rs.core.Response; - -/** - * - * Utility class for search REST API requests handling - * - */ -public class SearchResponseUtil { - - /** - * Creates a @WebTarget with provided authentication token - * - * @param targetLink link to get response from - * @param token bearer token to use for authentication - * @return WebTarget to use as resource - */ - public static WebTarget bearerAuthenticationWithOAuth2AtClientLevel(String targetLink, String token) { - - Feature feature = OAuth2ClientSupport.feature(token); - Client client = ClientBuilder.newBuilder().register(feature).build(); - - WebTarget target = client.target(targetLink); - return target; - } - - /** - * Gets a json response by given REST API target link using bearer authentication token - * - * @param targetLink link to get response from - * @param authToken bearer token to use for authentication - * @return String of json response - * @throws RestSearchResponseException if the returned status code was not 200 OK - */ - public static String getJsonResponseStringByTargetLink(String targetLink, String authToken) - throws RestSearchResponseException { - - WebTarget target = null; - - if (authToken != null) { - target = bearerAuthenticationWithOAuth2AtClientLevel(targetLink, authToken); - } else { - Client client = ClientBuilder.newClient(); - target = client.target(targetLink); - } - - Response response = null; - Invocation.Builder request = target.request(MediaType.APPLICATION_JSON); - response = request.get(); - - int status = response.getStatus(); - String jsonResponse = ""; - if (status == 200) { - jsonResponse = response.readEntity(String.class); - } else { - throw new RestSearchResponseException("The search REST API returned the unexpected status code: ", - String.valueOf(status)); - } - return jsonResponse; - } - - /** - * Creates a download link (concatenates maven repository link with groupId, artifact and version) - * - * @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 - * @return concatenated download link - * @throws MalformedURLException if the URL was not valid - */ - public static URL createDownloadLink(String mavenRepo, String groupId, String artifactId, String version, - String fileEnding) throws MalformedURLException { - - String parsedGroupId = groupId.replace(".", "/"); - String downloadFile = artifactId + "-" + version + fileEnding; - String downloadLink = mavenRepo + "/" + parsedGroupId + "/" + artifactId + "/" + version + "/" + downloadFile; - URL url = new URL(downloadLink); - return url; - } - -} diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponse.java index 182a480fc0..bdd1103ee0 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponse.java @@ -12,7 +12,6 @@ import com.devonfw.cobigen.api.constants.MavenSearchRepositoryType; import com.devonfw.cobigen.api.exception.RestSearchResponseException; import com.devonfw.cobigen.api.util.to.AbstractSearchResponse; -import com.devonfw.cobigen.api.util.to.SearchResponseUtil; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; @@ -62,7 +61,7 @@ public List retrieveDownloadURLs() throws MalformedURLException { for (String fileEnding : doc.getEc()) { String newFileEnding = fileEnding; downloadLinks - .add(SearchResponseUtil.createDownloadLink(MavenSearchRepositoryConstants.MAVEN_REPOSITORY_DOWNLOAD_LINK, + .add(AbstractSearchResponse.createDownloadLink(MavenSearchRepositoryConstants.MAVEN_REPOSITORY_DOWNLOAD_LINK, doc.getGroup(), doc.getArtifact(), doc.getLatestVersion(), newFileEnding)); } diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponse.java index b8d50b95bf..3131b3bf5b 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponse.java @@ -12,7 +12,6 @@ import com.devonfw.cobigen.api.constants.MavenSearchRepositoryType; import com.devonfw.cobigen.api.exception.RestSearchResponseException; import com.devonfw.cobigen.api.util.to.AbstractSearchResponse; -import com.devonfw.cobigen.api.util.to.SearchResponseUtil; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; @@ -41,7 +40,7 @@ public List retrieveDownloadURLs() throws MalformedURLException { for (Nexus2SearchResponseData item : this.data) { for (Nexus2SearchResponseArtifactHits artifactHit : item.artifactHits) { for (Nexus2SearchResponeArtifactLinks artifactLink : artifactHit.artifactLinks) { - downloadLinks.add(SearchResponseUtil.createDownloadLink( + downloadLinks.add(AbstractSearchResponse.createDownloadLink( MavenSearchRepositoryConstants.NEXUS2_REPOSITORY_URL + "/" + MavenSearchRepositoryConstants.NEXUS2_REPOSITORY_LINK, item.getGroupId(), item.getArtifactId(), item.getVersion(), "." + artifactLink.getExtension())); diff --git a/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java b/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java index 152c5fecad..79fd89d27b 100644 --- a/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java +++ b/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java @@ -15,7 +15,7 @@ import com.devonfw.cobigen.api.exception.CobiGenRuntimeException; import com.devonfw.cobigen.api.exception.RestSearchResponseException; import com.devonfw.cobigen.api.util.MavenUtil; -import com.devonfw.cobigen.api.util.to.SearchResponseUtil; +import com.devonfw.cobigen.api.util.to.AbstractSearchResponse; import com.devonfw.cobigen.api.util.to.jfrog.JfrogSearchResponse; import com.devonfw.cobigen.api.util.to.maven.MavenSearchResponse; import com.devonfw.cobigen.api.util.to.nexus2.Nexus2SearchResponse; @@ -47,7 +47,7 @@ public void testWrongRepositoryTypeThrowsException() { @Test(expected = ProcessingException.class) public void testWrongTargetLinkThrowsException() { - SearchResponseUtil.getJsonResponseStringByTargetLink("this/is/not/a/link", null); + AbstractSearchResponse.getJsonResponseStringByTargetLink("this/is/not/a/link", null); } /** @@ -56,7 +56,7 @@ public void testWrongTargetLinkThrowsException() { @Test(expected = ProcessingException.class) public void testWrongTargetLinkAndTokenThrowsException() { - SearchResponseUtil.getJsonResponseStringByTargetLink("this/is/not/a/link", "thisisabadtoken"); + AbstractSearchResponse.getJsonResponseStringByTargetLink("this/is/not/a/link", "thisisabadtoken"); } /** @@ -65,7 +65,7 @@ public void testWrongTargetLinkAndTokenThrowsException() { @Test(expected = RestSearchResponseException.class) public void testWrongResponseStatusCodeThrowsException() { - SearchResponseUtil.getJsonResponseStringByTargetLink("https://search.maven.org/solrsearch/select?test", null); + AbstractSearchResponse.getJsonResponseStringByTargetLink("https://search.maven.org/solrsearch/select?test", null); } /** From a939d5604d06d3b4eba6779fa7de2a8a9809949d Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Wed, 24 Aug 2022 13:40:33 +0200 Subject: [PATCH 27/42] #1529 implemented requested changes removed throw CobiGenRuntimeExceptions to ensure that an error with the API won't stop CobiGen execution replaced CobiGenRuntimeException in MavenUtil with error log and a return of null converted testWrongRepositoryTypeThrowsException to testRetrieveMavenArtifactsWithInvalidLinkReturnsNull replaced throw exception with error log message --- .../com/devonfw/cobigen/api/util/MavenUtil.java | 6 ++++-- .../api/util/to/SearchResponseFactory.java | 16 ++++++++-------- .../com/devonfw/cobigen/api/MavenUtilTest.java | 8 +++----- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/MavenUtil.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/MavenUtil.java index aac364bebf..a30d78d7c0 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/MavenUtil.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/MavenUtil.java @@ -351,14 +351,16 @@ public static Path getProjectRoot(Path inputFile, boolean topLevel) { * @param baseURL String of the repository server URL * @param groupId the groupId to search for * @param authToken bearer token to use for authentication - * @return List of artifact download URLS + * @return List of artifact download URLS or null if an error occurred */ public static List retrieveMavenArtifactsByGroupId(String baseURL, String groupId, String authToken) { try { return SearchResponseFactory.searchArtifactDownloadLinks(baseURL, groupId, authToken); } catch (RestSearchResponseException | JsonProcessingException | MalformedURLException e) { - throw new CobiGenRuntimeException("Unable to get artifacts from " + baseURL + " by groupId " + groupId, e); + LOG.error("Unable to get artifacts from " + baseURL + " by groupId " + groupId, e); + // TODO: Handle Eclipse, CLI and MavenPlugin here (f.e. with a new Exception) + return null; } } diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseFactory.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseFactory.java index aa5ecafb0e..8ac5590e3c 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseFactory.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseFactory.java @@ -7,7 +7,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.devonfw.cobigen.api.exception.CobiGenRuntimeException; import com.devonfw.cobigen.api.exception.RestSearchResponseException; import com.devonfw.cobigen.api.util.to.jfrog.JfrogSearchResponse; import com.devonfw.cobigen.api.util.to.maven.MavenSearchResponse; @@ -21,7 +20,8 @@ import jakarta.ws.rs.ProcessingException; /** - * Factory to create new instances of {@link AbstractSearchResponse} which handles the responses from various search REST APIs. + * Factory to create new instances of {@link AbstractSearchResponse} which handles the responses from various search + * REST APIs. */ public class SearchResponseFactory { @@ -57,16 +57,16 @@ public static List searchArtifactDownloadLinks(String baseURL, String group try { LOG.debug("Trying to get a response from {} with server URL: {} ...", ((AbstractSearchResponse) searchResponse).getRepositoryType(), baseURL); - String jsonResponse = ((AbstractSearchResponse) searchResponse).retrieveJsonResponse(baseURL, groupId, authToken); - AbstractSearchResponse response = (AbstractSearchResponse) mapper.readValue(jsonResponse, searchResponse.getClass()); + String jsonResponse = ((AbstractSearchResponse) searchResponse).retrieveJsonResponse(baseURL, groupId, + authToken); + AbstractSearchResponse response = (AbstractSearchResponse) mapper.readValue(jsonResponse, + searchResponse.getClass()); return response.retrieveDownloadURLs(); } catch (RestSearchResponseException e) { - LOG.debug("It was not possible to get a response from {} using the URL: {}.\n Following error occured:\n {}", + LOG.error("It was not possible to get a response from {} using the URL: {}.\n Following error occured:\n {}", ((AbstractSearchResponse) searchResponse).getRepositoryType(), baseURL, e.getMessage()); } catch (ProcessingException e) { - String errorMsg = "The search REST API was not able to process the URL: " + baseURL; - LOG.error(errorMsg, e); - throw new CobiGenRuntimeException(errorMsg, e); + LOG.error("The search REST API was not able to process the URL: {}", baseURL, e); } } diff --git a/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java b/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java index 79fd89d27b..bbf9a6d555 100644 --- a/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java +++ b/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java @@ -12,7 +12,6 @@ import org.junit.Test; import com.devonfw.cobigen.api.constants.MavenSearchRepositoryConstants; -import com.devonfw.cobigen.api.exception.CobiGenRuntimeException; import com.devonfw.cobigen.api.exception.RestSearchResponseException; import com.devonfw.cobigen.api.util.MavenUtil; import com.devonfw.cobigen.api.util.to.AbstractSearchResponse; @@ -33,12 +32,11 @@ public class MavenUtilTest { private static final String testdataRoot = "src/test/resources/testdata/unittest/MavenUtilTest"; /** - * Tests if a wrong repository type throws {@link CobiGenRuntimeException} + * Tests if retrieving maven artifacts with an invalid link returns null */ - @Test(expected = CobiGenRuntimeException.class) - public void testWrongRepositoryTypeThrowsException() { + public void testRetrieveMavenArtifactsWithInvalidLinkReturnsNull() { - assertThat(MavenUtil.retrieveMavenArtifactsByGroupId("this/is/not/a/link", "test", null)); + assertThat(MavenUtil.retrieveMavenArtifactsByGroupId("this/is/not/a/link", "test", null)).isNull(); } /** From 82a8727eb0ef162e00cc1643c4b235c7e27029a5 Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Wed, 24 Aug 2022 13:56:09 +0200 Subject: [PATCH 28/42] #1529 implemented requested changes refactored getJsonResponseStringByTargetLink replaced getJsonResponseStringByTargetLink with retrieveJsonResponseWithAuthenticationToken added MavenSearchRepositoryType to retrieveJsonResponseWithAuthenticationToken --- .../api/util/to/AbstractSearchResponse.java | 25 +++++-------------- .../util/to/jfrog/JfrogSearchResponse.java | 2 +- .../util/to/maven/MavenSearchResponse.java | 6 ++--- .../util/to/nexus2/Nexus2SearchResponse.java | 2 +- .../util/to/nexus3/Nexus3SearchResponse.java | 2 +- .../devonfw/cobigen/api/MavenUtilTest.java | 7 +++--- 6 files changed, 16 insertions(+), 28 deletions(-) diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractSearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractSearchResponse.java index 053e56c42d..6e6d0bf482 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractSearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractSearchResponse.java @@ -82,22 +82,6 @@ public String retrieveJsonResponse(String repositoryUrl, String groupId) throws public abstract String retrieveJsonResponse(String repositoryUrl, String groupId, String authToken) throws RestSearchResponseException; - /** - * Retrieves the json response from the search API target link using bearer authentication token - * - * @param targetLink link to get response from - * @param authToken bearer token to use for authentication - * @return String of json response - * @throws RestSearchResponseException if the request did not return status 200 - */ - public String retrieveJsonResponseWithAuthenticationToken(String targetLink, String authToken) - throws RestSearchResponseException { - - LOG.info("Starting {} search REST API request with URL: {}.", getRepositoryType(), targetLink); - - return getJsonResponseStringByTargetLink(targetLink, authToken); - } - /** * Creates a @WebTarget with provided authentication token * @@ -115,15 +99,18 @@ private static WebTarget bearerAuthenticationWithOAuth2AtClientLevel(String targ } /** - * Gets a json response by given REST API target link using bearer authentication token + * Retrieves a json response by given REST API target link using bearer authentication token * * @param targetLink link to get response from * @param authToken bearer token to use for authentication + * @param searchRepositoryType the type of the search repository * @return String of json response * @throws RestSearchResponseException if the returned status code was not 200 OK */ - public static String getJsonResponseStringByTargetLink(String targetLink, String authToken) - throws RestSearchResponseException { + public static String retrieveJsonResponseWithAuthenticationToken(String targetLink, String authToken, + MavenSearchRepositoryType searchRepositoryType) throws RestSearchResponseException { + + LOG.info("Starting {} search REST API request with URL: {}.", searchRepositoryType, targetLink); WebTarget target = null; diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/jfrog/JfrogSearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/jfrog/JfrogSearchResponse.java index 445e4c5717..1b388fca87 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/jfrog/JfrogSearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/jfrog/JfrogSearchResponse.java @@ -45,7 +45,7 @@ public String retrieveJsonResponse(String repositoryUrl, String groupId, String String targetLink = repositoryUrl + "/" + MavenSearchRepositoryConstants.JFROG_TARGET_LINK + "?g=" + groupId; - return retrieveJsonResponseWithAuthenticationToken(targetLink, authToken); + return retrieveJsonResponseWithAuthenticationToken(targetLink, authToken, getRepositoryType()); } @Override diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponse.java index bdd1103ee0..82f4bbaa04 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponse.java @@ -47,7 +47,7 @@ public String retrieveJsonResponse(String repositoryUrl, String groupId, String String targetLink = repositoryUrl + "/" + MavenSearchRepositoryConstants.MAVEN_TARGET_LINK + "?q=g:" + groupId + "&wt=json"; - return retrieveJsonResponseWithAuthenticationToken(targetLink, authToken); + return retrieveJsonResponseWithAuthenticationToken(targetLink, authToken, getRepositoryType()); } @Override @@ -60,8 +60,8 @@ public List retrieveDownloadURLs() throws MalformedURLException { for (MavenSearchResponseDoc doc : docs) { for (String fileEnding : doc.getEc()) { String newFileEnding = fileEnding; - downloadLinks - .add(AbstractSearchResponse.createDownloadLink(MavenSearchRepositoryConstants.MAVEN_REPOSITORY_DOWNLOAD_LINK, + downloadLinks.add( + AbstractSearchResponse.createDownloadLink(MavenSearchRepositoryConstants.MAVEN_REPOSITORY_DOWNLOAD_LINK, doc.getGroup(), doc.getArtifact(), doc.getLatestVersion(), newFileEnding)); } diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponse.java index 3131b3bf5b..5667e732f8 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponse.java @@ -59,7 +59,7 @@ public String retrieveJsonResponse(String repositoryUrl, String groupId, String String targetLink = repositoryUrl + "/" + MavenSearchRepositoryConstants.NEXUS2_TARGET_LINK + "?q=" + groupId; - return retrieveJsonResponseWithAuthenticationToken(targetLink, authToken); + return retrieveJsonResponseWithAuthenticationToken(targetLink, authToken, getRepositoryType()); } @Override diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponse.java index b31bf3e3e6..d2f242d493 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponse.java @@ -54,7 +54,7 @@ public String retrieveJsonResponse(String repositoryUrl, String groupId, String String targetLink = repositoryUrl + "/" + MavenSearchRepositoryConstants.NEXUS3_TARGET_LINK + "?repository=maven-central" + "&group=" + groupId; - return retrieveJsonResponseWithAuthenticationToken(targetLink, authToken); + return retrieveJsonResponseWithAuthenticationToken(targetLink, authToken, getRepositoryType()); } @Override diff --git a/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java b/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java index bbf9a6d555..06feef8fb8 100644 --- a/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java +++ b/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java @@ -45,7 +45,7 @@ public void testRetrieveMavenArtifactsWithInvalidLinkReturnsNull() { @Test(expected = ProcessingException.class) public void testWrongTargetLinkThrowsException() { - AbstractSearchResponse.getJsonResponseStringByTargetLink("this/is/not/a/link", null); + AbstractSearchResponse.retrieveJsonResponseWithAuthenticationToken("this/is/not/a/link", null, null); } /** @@ -54,7 +54,7 @@ public void testWrongTargetLinkThrowsException() { @Test(expected = ProcessingException.class) public void testWrongTargetLinkAndTokenThrowsException() { - AbstractSearchResponse.getJsonResponseStringByTargetLink("this/is/not/a/link", "thisisabadtoken"); + AbstractSearchResponse.retrieveJsonResponseWithAuthenticationToken("this/is/not/a/link", "thisisabadtoken", null); } /** @@ -63,7 +63,8 @@ public void testWrongTargetLinkAndTokenThrowsException() { @Test(expected = RestSearchResponseException.class) public void testWrongResponseStatusCodeThrowsException() { - AbstractSearchResponse.getJsonResponseStringByTargetLink("https://search.maven.org/solrsearch/select?test", null); + AbstractSearchResponse + .retrieveJsonResponseWithAuthenticationToken("https://search.maven.org/solrsearch/select?test", null, null); } /** From 28dfb4e93cea6a6bfb1f27c56e00346a1fdd78a8 Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Wed, 24 Aug 2022 14:01:24 +0200 Subject: [PATCH 29/42] #1529 implemented requested changes replaced fixed ignored json properties with ignore unknown param --- .../devonfw/cobigen/api/util/to/maven/MavenSearchResponse.java | 2 +- .../cobigen/api/util/to/maven/MavenSearchResponseDoc.java | 2 +- .../cobigen/api/util/to/maven/MavenSearchResponseResponse.java | 2 +- .../api/util/to/nexus2/Nexus2SearchResponeArtifactLinks.java | 2 ++ .../cobigen/api/util/to/nexus2/Nexus2SearchResponse.java | 2 +- .../api/util/to/nexus2/Nexus2SearchResponseArtifactHits.java | 2 +- .../cobigen/api/util/to/nexus2/Nexus2SearchResponseData.java | 2 +- .../cobigen/api/util/to/nexus3/Nexus3SearchResponse.java | 2 +- .../cobigen/api/util/to/nexus3/Nexus3SearchResponseAsset.java | 2 +- .../cobigen/api/util/to/nexus3/Nexus3SearchResponseItem.java | 2 +- 10 files changed, 11 insertions(+), 9 deletions(-) diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponse.java index 82f4bbaa04..99606dbd1f 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponse.java @@ -20,7 +20,7 @@ * Json model for maven Search REST API response * */ -@JsonIgnoreProperties(value = { "responseHeader", "spellcheck" }) +@JsonIgnoreProperties(ignoreUnknown = true) public class MavenSearchResponse extends AbstractSearchResponse { /** Logger instance. */ diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponseDoc.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponseDoc.java index 70429f9389..8d5d7b2eb4 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponseDoc.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponseDoc.java @@ -11,7 +11,7 @@ * Maven search response doc model * */ -@JsonIgnoreProperties(value = { "p", "timestamp", "versionCount", "text" }) +@JsonIgnoreProperties(ignoreUnknown = true) class MavenSearchResponseDoc { /** id */ diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponseResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponseResponse.java index 5985249b7d..ec30cb7487 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponseResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponseResponse.java @@ -11,7 +11,7 @@ * Maven search response model * */ -@JsonIgnoreProperties(value = { "start" }) +@JsonIgnoreProperties(ignoreUnknown = true) class MavenSearchResponseResponse { /** diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponeArtifactLinks.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponeArtifactLinks.java index 462bc8c916..9016e1a95f 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponeArtifactLinks.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponeArtifactLinks.java @@ -1,5 +1,6 @@ package com.devonfw.cobigen.api.util.to.nexus2; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; /** @@ -7,6 +8,7 @@ * Nexus 2 search response artifact links model * */ +@JsonIgnoreProperties(ignoreUnknown = true) class Nexus2SearchResponeArtifactLinks { @JsonProperty("extension") diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponse.java index 5667e732f8..abbe4ff2d5 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponse.java @@ -20,7 +20,7 @@ * Json model for nexus2 Search REST API response * */ -@JsonIgnoreProperties(value = { "totalCount", "from", "count", "tooManyResults", "collapsed", "repoDetails" }) +@JsonIgnoreProperties(ignoreUnknown = true) public class Nexus2SearchResponse extends AbstractSearchResponse { /** Logger instance. */ diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponseArtifactHits.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponseArtifactHits.java index 295745435e..54fa1a5190 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponseArtifactHits.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponseArtifactHits.java @@ -10,7 +10,7 @@ * Nexus search response artifacthits model * */ -@JsonIgnoreProperties(value = { "repositoryId" }) +@JsonIgnoreProperties(ignoreUnknown = true) class Nexus2SearchResponseArtifactHits { /** artifactLinks */ diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponseData.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponseData.java index e7659c574b..b87dc24c17 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponseData.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponseData.java @@ -10,7 +10,7 @@ * Nexus2 search response data model * */ -@JsonIgnoreProperties(value = { "latestRelease", "latestReleaseRepositoryId", "highlightedFragment" }) +@JsonIgnoreProperties(ignoreUnknown = true) class Nexus2SearchResponseData { /** groupId */ diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponse.java index d2f242d493..74ffbd5b7a 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponse.java @@ -20,7 +20,7 @@ * Json model for nexus3 Search REST API response * */ -@JsonIgnoreProperties(value = { "continuationToken" }) +@JsonIgnoreProperties(ignoreUnknown = true) public class Nexus3SearchResponse extends AbstractSearchResponse { /** Logger instance. */ diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponseAsset.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponseAsset.java index f7638f719b..7ed9689f79 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponseAsset.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponseAsset.java @@ -8,7 +8,7 @@ * Nexus3 search response asset model * */ -@JsonIgnoreProperties(value = { "path", "id", "repository", "format", "checksum" }) +@JsonIgnoreProperties(ignoreUnknown = true) class Nexus3SearchResponseAsset { /** downloadUrl */ diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponseItem.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponseItem.java index 3f4e3e4ed2..6fab4b864c 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponseItem.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponseItem.java @@ -10,7 +10,7 @@ * Nexus3 search response item model * */ -@JsonIgnoreProperties(value = { "id", "repository", "format", "group", "name", "version" }) +@JsonIgnoreProperties(ignoreUnknown = true) class Nexus3SearchResponseItem { /** artifactHits */ From ef6c9cd91a4ab8ae91b6df0dc07f0612dccf5a0d Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Thu, 1 Sep 2022 12:27:40 +0200 Subject: [PATCH 30/42] #1529 added WireMock to tests made ignored tests functional with WireMock added WireMock stubs for each case updated jersey from 3.0.5 to 3.0.7 added wiremock-standalone 2.27.2 (used older version because of conflicts with jackson) --- cobigen/cobigen-core-api/pom.xml | 15 ++- .../devonfw/cobigen/api/MavenUtilTest.java | 115 +++++++++++++++--- 2 files changed, 109 insertions(+), 21 deletions(-) diff --git a/cobigen/cobigen-core-api/pom.xml b/cobigen/cobigen-core-api/pom.xml index 50672772e4..91cbf45342 100644 --- a/cobigen/cobigen-core-api/pom.xml +++ b/cobigen/cobigen-core-api/pom.xml @@ -50,25 +50,32 @@ org.glassfish.jersey.media jersey-media-json-jackson - 3.0.5 + 3.0.7 org.glassfish.jersey.core jersey-client - 3.0.5 + 3.0.7 org.glassfish.jersey.inject jersey-hk2 - 3.0.5 + 3.0.7 org.glassfish.jersey.security oauth2-client - 3.0.5 + 3.0.7 + + + + com.github.tomakehurst + wiremock-standalone + 2.27.2 + test diff --git a/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java b/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java index 06feef8fb8..9d58839f32 100644 --- a/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java +++ b/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java @@ -1,5 +1,9 @@ package com.devonfw.cobigen.api; +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; +import static com.github.tomakehurst.wiremock.client.WireMock.get; +import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching; +import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; import static org.assertj.core.api.Assertions.assertThat; import java.io.IOException; @@ -8,7 +12,7 @@ import java.nio.file.Paths; import java.util.List; -import org.junit.Ignore; +import org.junit.Rule; import org.junit.Test; import com.devonfw.cobigen.api.constants.MavenSearchRepositoryConstants; @@ -20,6 +24,7 @@ import com.devonfw.cobigen.api.util.to.nexus2.Nexus2SearchResponse; import com.devonfw.cobigen.api.util.to.nexus3.Nexus3SearchResponse; import com.fasterxml.jackson.databind.ObjectMapper; +import com.github.tomakehurst.wiremock.junit.WireMockRule; import jakarta.ws.rs.ProcessingException; @@ -28,6 +33,12 @@ */ public class MavenUtilTest { + /** + * WireMock rule to initialize + */ + @Rule + public WireMockRule wireMockRule = new WireMockRule(options().disableRequestJournal()); + /** Testdata root path */ private static final String testdataRoot = "src/test/resources/testdata/unittest/MavenUtilTest"; @@ -70,18 +81,22 @@ public void testWrongResponseStatusCodeThrowsException() { /** * Tests if maven json response can properly be parsed and converted to a list of download URLs * - * @throws IOException + * @throws IOException if an error occurred while reading the test json file */ @Test public void testMavenParseDownloadLinks() throws IOException { + // given ObjectMapper mapper = new ObjectMapper(); MavenSearchResponse response = new MavenSearchResponse(); String jsonResponse = new String(Files.readAllBytes(Paths.get(testdataRoot).resolve("mavenJsonTest.json"))); response = mapper.readValue(jsonResponse, MavenSearchResponse.class); + // when List downloadLinks = response.retrieveDownloadURLs(); + + // then assertThat(downloadLinks).contains( new URL("https://repo1.maven.org/maven2/com/google/inject/guice/5.1.0/guice-5.1.0.jar"), new URL("https://repo1.maven.org/maven2/com/google/inject/guice-bom/5.1.0/guice-bom-5.1.0.pom"), @@ -92,18 +107,23 @@ public void testMavenParseDownloadLinks() throws IOException { /** * Tests if nexus2 json response can properly be parsed and converted to a list of download URLs * - * @throws IOException + * @throws IOException if an error occurred while reading the test json file */ @Test public void testNexus2ParseDownloadLinks() throws IOException { + // given ObjectMapper mapper = new ObjectMapper(); Nexus2SearchResponse response = new Nexus2SearchResponse(); String jsonResponse = new String(Files.readAllBytes(Paths.get(testdataRoot).resolve("nexus2JsonTest.json"))); response = mapper.readValue(jsonResponse, Nexus2SearchResponse.class); + + // when List downloadLinks = response.retrieveDownloadURLs(); + + // then assertThat(downloadLinks).contains(new URL( "https://s01.oss.sonatype.org/service/local/repositories/releases/content/com/devonfw/cobigen/openapiplugin/2021.12.006/openapiplugin-2021.12.006.pom"), new URL( @@ -125,18 +145,23 @@ public void testNexus2ParseDownloadLinks() throws IOException { /** * Tests if nexus3 json response can properly be parsed and converted to a list of download URLs * - * @throws IOException + * @throws IOException if an error occurred while reading the test json file */ @Test public void testNexus3ParseDownloadLinks() throws IOException { + // given ObjectMapper mapper = new ObjectMapper(); Nexus3SearchResponse response = new Nexus3SearchResponse(); String jsonResponse = new String(Files.readAllBytes(Paths.get(testdataRoot).resolve("nexus3JsonTest.json"))); response = mapper.readValue(jsonResponse, Nexus3SearchResponse.class); + + // when List downloadLinks = response.retrieveDownloadURLs(); + + // then assertThat(downloadLinks).contains(new URL( "http://localhost:8081/repository/maven-central/org/osgi/org.osgi.core/4.3.1/org.osgi.core-4.3.1-sources.jar"), new URL("http://localhost:8081/repository/maven-central/org/osgi/org.osgi.core/4.3.1/org.osgi.core-4.3.1.jar"), @@ -146,18 +171,22 @@ public void testNexus3ParseDownloadLinks() throws IOException { /** * Tests if jfrog json response can properly be parsed and converted to a list of download URLs * - * @throws IOException + * @throws IOException if an error occurred while reading the test json file */ @Test public void testJfrogParseDownloadLinks() throws IOException { + // given ObjectMapper mapper = new ObjectMapper(); JfrogSearchResponse response = new JfrogSearchResponse(); String jsonResponse = new String(Files.readAllBytes(Paths.get(testdataRoot).resolve("jfrogJsonTest.json"))); + // when response = mapper.readValue(jsonResponse, JfrogSearchResponse.class); List downloadLinks = response.retrieveDownloadURLs(); + + // then assertThat(downloadLinks).contains(new URL( "http://localhost:8081/artifactory/api/storage/libs-release-local/org/acme/artifact/1.0/artifact-1.0-sources.jar"), new URL( @@ -167,16 +196,30 @@ public void testJfrogParseDownloadLinks() throws IOException { /** * Tests if a request to maven search REST API returns a list of download URLs * - * @throws IOException + * @throws IOException if an error occurred while reading the test json file */ @Test public void testMavenSearchRequestGetsValidDownloadLinks() throws IOException { + // given List downloadList; + this.wireMockRule.stubFor(get(urlMatching("/artifactory/solrsearch/.*")).willReturn( + aResponse().withStatus(202).withBody(Files.readString(Paths.get(testdataRoot).resolve("mavenJsonTest.json"))))); + + this.wireMockRule + .stubFor(get(urlMatching("/artifactory/api/search/gavc.*")).willReturn(aResponse().withStatus(404))); + + this.wireMockRule + .stubFor(get(urlMatching("/service/local/lucene/search/.*")).willReturn(aResponse().withStatus(404))); + + this.wireMockRule.stubFor(get(urlMatching("/service/rest/v1/search.*")).willReturn(aResponse().withStatus(404))); + + // when downloadList = MavenUtil.retrieveMavenArtifactsByGroupId(MavenSearchRepositoryConstants.MAVEN_REPOSITORY_URL, "com.google.inject", null); + // then assertThat(downloadList).contains( new URL("https://repo1.maven.org/maven2/com/google/inject/guice/5.1.0/guice-5.1.0.jar"), new URL("https://repo1.maven.org/maven2/com/google/inject/guice-bom/5.1.0/guice-bom-5.1.0.pom"), @@ -187,16 +230,29 @@ public void testMavenSearchRequestGetsValidDownloadLinks() throws IOException { /** * Tests if a request to nexus2 search REST API returns a list of download URLs * - * @throws IOException + * @throws IOException if an error occurred while reading the test json file */ @Test public void testNexus2SearchRequestGetsValidDownloadLinks() throws IOException { + // given List downloadList; + this.wireMockRule.stubFor(get(urlMatching("/artifactory/solrsearch/.*")).willReturn(aResponse().withStatus(404))); + + this.wireMockRule + .stubFor(get(urlMatching("/artifactory/api/search/gavc.*")).willReturn(aResponse().withStatus(404))); + + this.wireMockRule.stubFor(get(urlMatching("/service/local/lucene/search/.*")).willReturn(aResponse().withStatus(200) + .withBody(Files.readString(Paths.get(testdataRoot).resolve("nexus3JsonTest.json"))))); + + this.wireMockRule.stubFor(get(urlMatching("/service/rest/v1/search.*")).willReturn(aResponse().withStatus(404))); + + // when downloadList = MavenUtil.retrieveMavenArtifactsByGroupId(MavenSearchRepositoryConstants.NEXUS2_REPOSITORY_URL, "com.devonfw.cobigen", null); + // then assertThat(downloadList).contains(new URL( "https://s01.oss.sonatype.org/service/local/repositories/releases/content/com/devonfw/cobigen/openapiplugin/2021.12.006/openapiplugin-2021.12.006.jar")); } @@ -204,17 +260,29 @@ public void testNexus2SearchRequestGetsValidDownloadLinks() throws IOException { /** * Tests if a request to nexus3 search REST API returns a list of download URLs * - * @throws IOException + * @throws IOException if an error occurred while reading the test json file */ @Test - @Ignore // TODO: remove when nexus3 URLs are testable public void testNexus3SearchRequestGetsValidDownloadLinks() throws IOException { + // given List downloadList; - downloadList = MavenUtil.retrieveMavenArtifactsByGroupId(MavenSearchRepositoryConstants.NEXUS3_REPOSITORY_URL, - "com.devonfw.cobigen", null); + this.wireMockRule.stubFor(get(urlMatching("/artifactory/solrsearch/.*")).willReturn(aResponse().withStatus(404))); + + this.wireMockRule + .stubFor(get(urlMatching("/artifactory/api/search/gavc.*")).willReturn(aResponse().withStatus(404))); + this.wireMockRule + .stubFor(get(urlMatching("/service/local/lucene/search/.*")).willReturn(aResponse().withStatus(404))); + + this.wireMockRule.stubFor(get(urlMatching("/service/rest/v1/search.*")).willReturn(aResponse().withStatus(200) + .withBody(Files.readString(Paths.get(testdataRoot).resolve("nexus3JsonTest.json"))))); + + // when + downloadList = MavenUtil.retrieveMavenArtifactsByGroupId("http://localhost:8080", "com.devonfw.cobigen", null); + + // then assertThat(downloadList).contains(new URL( "http://localhost:8081/repository/maven-central/org/osgi/org.osgi.core/4.3.1/org.osgi.core-4.3.1-sources.jar"), new URL("http://localhost:8081/repository/maven-central/org/osgi/org.osgi.core/4.3.1/org.osgi.core-4.3.1.jar"), @@ -224,20 +292,33 @@ public void testNexus3SearchRequestGetsValidDownloadLinks() throws IOException { /** * Tests if a request to jfrog search REST API returns a list of download URLs * - * @throws IOException + * @throws IOException if an error occurred while reading the test json file */ @Test - @Ignore // TODO: remove when jfrog URLs are testable public void testJfrogSearchRequestGetsValidDownloadLinks() throws IOException { + // given List downloadList; - downloadList = MavenUtil.retrieveMavenArtifactsByGroupId(MavenSearchRepositoryConstants.JFROG_REPOSITORY_URL, - "com.devonfw.cobigen", null); + this.wireMockRule.stubFor(get(urlMatching("/artifactory/solrsearch/.*")).willReturn(aResponse().withStatus(404))); + + this.wireMockRule.stubFor(get(urlMatching("/artifactory/api/search/gavc.*")).willReturn( + aResponse().withStatus(200).withBody(Files.readString(Paths.get(testdataRoot).resolve("jfrogJsonTest.json"))))); + + this.wireMockRule + .stubFor(get(urlMatching("/service/local/lucene/search/.*")).willReturn(aResponse().withStatus(404))); + + this.wireMockRule.stubFor(get(urlMatching("/service/rest/v1/search.*")).willReturn(aResponse().withStatus(404))); + + // when + downloadList = MavenUtil.retrieveMavenArtifactsByGroupId("http://localhost:8080/artifactory", "com.devonfw.cobigen", + null); + + // then assertThat(downloadList).contains(new URL( - "https://localjfrog.com/artifactory/api/storage/maven-remote-cache/com/devonfw/cobigen/cli-parent/2021.04.001-SNAPSHOT/cli-parent-2021.04.001-SNAPSHOT.pom"), + "http://localhost:8081/artifactory/api/storage/libs-release-local/org/acme/artifact/1.0/artifact-1.0-sources.jar"), new URL( - "https://localjfrog.com/artifactory/api/storage/maven-remote-cache/com/devonfw/cobigen/cli-parent/2021.08.002-SNAPSHOT/cli-parent-2021.08.002-SNAPSHOT.pom")); + "http://localhost:8081/artifactory/api/storage/libs-release-local/org/acme/artifactB/1.0/artifactB-1.0-sources.jar")); } } From e91f1f542c03cc6fb0a760a1c3faf88e7ee32dfd Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Thu, 1 Sep 2022 12:28:50 +0200 Subject: [PATCH 31/42] #1529 reduced WireMock logging added logback-test.xml (sets WireMock to WARN log level) --- .../src/test/resources/logback-test.xml | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 cobigen/cobigen-core-api/src/test/resources/logback-test.xml diff --git a/cobigen/cobigen-core-api/src/test/resources/logback-test.xml b/cobigen/cobigen-core-api/src/test/resources/logback-test.xml new file mode 100644 index 0000000000..7a043e0457 --- /dev/null +++ b/cobigen/cobigen-core-api/src/test/resources/logback-test.xml @@ -0,0 +1,17 @@ + + + + + + %d{HH:mm:ss.SSS} [%thread] [C:%X{correlationId}] - %-5level - %logger{36} - %msg%n + + + + + + + + + + + \ No newline at end of file From 314586d7d5e785aea9b0e1b48e3101c8726c7b64 Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Thu, 1 Sep 2022 12:29:57 +0200 Subject: [PATCH 32/42] #1529 updated jackson updated jackson-databind from 2.13.2.2 to 2.13.3 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index d3f74992d9..599e64c995 100644 --- a/pom.xml +++ b/pom.xml @@ -127,7 +127,7 @@ com.fasterxml.jackson.core jackson-databind - 2.13.2.2 + 2.13.3 com.google.code.gson From 702691342c1622eab25ab57ba63ecb2dd49673be Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Thu, 1 Sep 2022 12:43:35 +0200 Subject: [PATCH 33/42] #1529 fixed FileUtils issue replaced readString with readAllBytes --- .../java/com/devonfw/cobigen/api/MavenUtilTest.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java b/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java index 9d58839f32..4942cd52e1 100644 --- a/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java +++ b/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java @@ -204,8 +204,8 @@ public void testMavenSearchRequestGetsValidDownloadLinks() throws IOException { // given List downloadList; - this.wireMockRule.stubFor(get(urlMatching("/artifactory/solrsearch/.*")).willReturn( - aResponse().withStatus(202).withBody(Files.readString(Paths.get(testdataRoot).resolve("mavenJsonTest.json"))))); + this.wireMockRule.stubFor(get(urlMatching("/artifactory/solrsearch/.*")).willReturn(aResponse().withStatus(202) + .withBody(Files.readAllBytes(Paths.get(testdataRoot).resolve("mavenJsonTest.json"))))); this.wireMockRule .stubFor(get(urlMatching("/artifactory/api/search/gavc.*")).willReturn(aResponse().withStatus(404))); @@ -244,7 +244,7 @@ public void testNexus2SearchRequestGetsValidDownloadLinks() throws IOException { .stubFor(get(urlMatching("/artifactory/api/search/gavc.*")).willReturn(aResponse().withStatus(404))); this.wireMockRule.stubFor(get(urlMatching("/service/local/lucene/search/.*")).willReturn(aResponse().withStatus(200) - .withBody(Files.readString(Paths.get(testdataRoot).resolve("nexus3JsonTest.json"))))); + .withBody(Files.readAllBytes(Paths.get(testdataRoot).resolve("nexus3JsonTest.json"))))); this.wireMockRule.stubFor(get(urlMatching("/service/rest/v1/search.*")).willReturn(aResponse().withStatus(404))); @@ -277,7 +277,7 @@ public void testNexus3SearchRequestGetsValidDownloadLinks() throws IOException { .stubFor(get(urlMatching("/service/local/lucene/search/.*")).willReturn(aResponse().withStatus(404))); this.wireMockRule.stubFor(get(urlMatching("/service/rest/v1/search.*")).willReturn(aResponse().withStatus(200) - .withBody(Files.readString(Paths.get(testdataRoot).resolve("nexus3JsonTest.json"))))); + .withBody(Files.readAllBytes(Paths.get(testdataRoot).resolve("nexus3JsonTest.json"))))); // when downloadList = MavenUtil.retrieveMavenArtifactsByGroupId("http://localhost:8080", "com.devonfw.cobigen", null); @@ -302,8 +302,8 @@ public void testJfrogSearchRequestGetsValidDownloadLinks() throws IOException { this.wireMockRule.stubFor(get(urlMatching("/artifactory/solrsearch/.*")).willReturn(aResponse().withStatus(404))); - this.wireMockRule.stubFor(get(urlMatching("/artifactory/api/search/gavc.*")).willReturn( - aResponse().withStatus(200).withBody(Files.readString(Paths.get(testdataRoot).resolve("jfrogJsonTest.json"))))); + this.wireMockRule.stubFor(get(urlMatching("/artifactory/api/search/gavc.*")).willReturn(aResponse().withStatus(200) + .withBody(Files.readAllBytes(Paths.get(testdataRoot).resolve("jfrogJsonTest.json"))))); this.wireMockRule .stubFor(get(urlMatching("/service/local/lucene/search/.*")).willReturn(aResponse().withStatus(404))); From 4eee9e08fb68338bc25187cdfd52800dc2c3c5b2 Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Fri, 9 Sep 2022 13:45:31 +0200 Subject: [PATCH 34/42] #1529 implemented requested changes renamed baseURL to baseUrl removed LOG concatenation --- .../java/com/devonfw/cobigen/api/util/MavenUtil.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/MavenUtil.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/MavenUtil.java index a30d78d7c0..69f96c4e50 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/MavenUtil.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/MavenUtil.java @@ -348,17 +348,18 @@ public static Path getProjectRoot(Path inputFile, boolean topLevel) { * Retrieves a list of download URLs by groupId from the specified repository search REST API using authentication * with bearer token * - * @param baseURL String of the repository server URL + * @param baseUrl String of the repository server URL * @param groupId the groupId to search for * @param authToken bearer token to use for authentication * @return List of artifact download URLS or null if an error occurred */ - public static List retrieveMavenArtifactsByGroupId(String baseURL, String groupId, String authToken) { + public static List retrieveMavenArtifactsByGroupId(String baseUrl, String groupId, String authToken) { try { - return SearchResponseFactory.searchArtifactDownloadLinks(baseURL, groupId, authToken); + + return SearchResponseFactory.searchArtifactDownloadLinks(baseUrl, groupId, authToken); } catch (RestSearchResponseException | JsonProcessingException | MalformedURLException e) { - LOG.error("Unable to get artifacts from " + baseURL + " by groupId " + groupId, e); + LOG.error("Unable to get artifacts from {} by groupId {}", baseUrl, groupId, e); // TODO: Handle Eclipse, CLI and MavenPlugin here (f.e. with a new Exception) return null; } From c4139801b6c263e33fdf170657b00843856db4dd Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Fri, 9 Sep 2022 13:47:51 +0200 Subject: [PATCH 35/42] #1529 implemented requested changes removed ProcessingException from SearchResponseFactory --- .../devonfw/cobigen/api/util/to/SearchResponseFactory.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseFactory.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseFactory.java index 8ac5590e3c..4fd420d474 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseFactory.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/SearchResponseFactory.java @@ -17,8 +17,6 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.collect.Lists; -import jakarta.ws.rs.ProcessingException; - /** * Factory to create new instances of {@link AbstractSearchResponse} which handles the responses from various search * REST APIs. @@ -65,8 +63,6 @@ public static List searchArtifactDownloadLinks(String baseURL, String group } catch (RestSearchResponseException e) { LOG.error("It was not possible to get a response from {} using the URL: {}.\n Following error occured:\n {}", ((AbstractSearchResponse) searchResponse).getRepositoryType(), baseURL, e.getMessage()); - } catch (ProcessingException e) { - LOG.error("The search REST API was not able to process the URL: {}", baseURL, e); } } From 1b26cf9a7b08a047a0e5bbf34494f07e0c8beac5 Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Fri, 9 Sep 2022 13:48:57 +0200 Subject: [PATCH 36/42] #1529 implemented requested changes added artifactory path to jfrog target link constant --- .../cobigen/api/constants/MavenSearchRepositoryConstants.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/constants/MavenSearchRepositoryConstants.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/constants/MavenSearchRepositoryConstants.java index 0026115471..5fa96b47ad 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/constants/MavenSearchRepositoryConstants.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/constants/MavenSearchRepositoryConstants.java @@ -53,6 +53,6 @@ public class MavenSearchRepositoryConstants { /** * Jfrog target link */ - public static String JFROG_TARGET_LINK = "api/search/gavc"; + public static String JFROG_TARGET_LINK = "artifactory/api/search/gavc"; } From a76fc14b2d4b01f5ccfeca0f06e19854534aba20 Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Fri, 9 Sep 2022 15:23:34 +0200 Subject: [PATCH 37/42] #1529 removed jersey dependencies replaced jersey with OkHttpClient restricted MavenUtilTests (added check of messages) added jackson-databind to core-api dependencies added okhttp to core-api dependencies --- cobigen/cobigen-core-api/pom.xml | 26 +++---- .../api/util/to/AbstractSearchResponse.java | 69 ++++++++++--------- .../devonfw/cobigen/api/MavenUtilTest.java | 50 ++++++++------ 3 files changed, 73 insertions(+), 72 deletions(-) diff --git a/cobigen/cobigen-core-api/pom.xml b/cobigen/cobigen-core-api/pom.xml index 91cbf45342..d3db948b3d 100644 --- a/cobigen/cobigen-core-api/pom.xml +++ b/cobigen/cobigen-core-api/pom.xml @@ -47,30 +47,20 @@ test + - org.glassfish.jersey.media - jersey-media-json-jackson - 3.0.7 + com.fasterxml.jackson.core + jackson-databind + - org.glassfish.jersey.core - jersey-client - 3.0.7 - - - - org.glassfish.jersey.inject - jersey-hk2 - 3.0.7 - - - - org.glassfish.jersey.security - oauth2-client - 3.0.7 + com.squareup.okhttp3 + okhttp + 4.9.1 + com.github.tomakehurst wiremock-standalone diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractSearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractSearchResponse.java index 6e6d0bf482..e6320c8036 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractSearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractSearchResponse.java @@ -1,11 +1,12 @@ package com.devonfw.cobigen.api.util.to; +import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.util.List; +import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; -import org.glassfish.jersey.client.oauth2.OAuth2ClientSupport; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -14,13 +15,9 @@ import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; -import jakarta.ws.rs.client.Client; -import jakarta.ws.rs.client.ClientBuilder; -import jakarta.ws.rs.client.Invocation; -import jakarta.ws.rs.client.WebTarget; -import jakarta.ws.rs.core.Feature; -import jakarta.ws.rs.core.MediaType; -import jakarta.ws.rs.core.Response; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; /** * This interface should be inherited for all maven REST search API responses to properly convert {@link JsonProperty} @@ -87,15 +84,12 @@ public abstract String retrieveJsonResponse(String repositoryUrl, String groupId * * @param targetLink link to get response from * @param token bearer token to use for authentication - * @return WebTarget to use as resource + * @return Request to use as resource */ - private static WebTarget bearerAuthenticationWithOAuth2AtClientLevel(String targetLink, String token) { + private static Request bearerAuthenticationWithOAuth2AtClientLevel(String targetLink, String token) { - Feature feature = OAuth2ClientSupport.feature(token); - Client client = ClientBuilder.newBuilder().register(feature).build(); + return new Request.Builder().url(targetLink).addHeader("Authorization", "Bearer " + token).build(); - WebTarget target = client.target(targetLink); - return target; } /** @@ -112,28 +106,39 @@ public static String retrieveJsonResponseWithAuthenticationToken(String targetLi LOG.info("Starting {} search REST API request with URL: {}.", searchRepositoryType, targetLink); - WebTarget target = null; + OkHttpClient httpClient = new OkHttpClient().newBuilder().connectTimeout(10, TimeUnit.SECONDS) + .readTimeout(30, TimeUnit.SECONDS).callTimeout(30, TimeUnit.SECONDS).writeTimeout(30, TimeUnit.SECONDS) + .retryOnConnectionFailure(true).build(); + String jsonResponse = ""; - if (authToken != null) { - target = bearerAuthenticationWithOAuth2AtClientLevel(targetLink, authToken); - } else { - Client client = ClientBuilder.newClient(); - target = client.target(targetLink); + try { + Response response = null; + + if (authToken != null) { + response = httpClient.newCall(bearerAuthenticationWithOAuth2AtClientLevel(targetLink, authToken)).execute(); + } else { + response = httpClient.newCall(new Request.Builder().url(targetLink).get().build()).execute(); + } + + int status = response.code(); + + if (response != null) { + if (status == 200 || status == 201 || status == 204) { + jsonResponse = response.body().string(); + } else { + throw new RestSearchResponseException("The search REST API returned the unexpected status code: ", + String.valueOf(response.code())); + } + } + + } catch (IOException e) { + throw new RestSearchResponseException("Unable to send or receive the message from the service", e); + } catch (IllegalArgumentException e) { + throw new RestSearchResponseException("The target URL was faulty.", e); } - Response response = null; - Invocation.Builder request = target.request(MediaType.APPLICATION_JSON); - response = request.get(); - - int status = response.getStatus(); - String jsonResponse = ""; - if (status == 200) { - jsonResponse = response.readEntity(String.class); - } else { - throw new RestSearchResponseException("The search REST API returned the unexpected status code: ", - String.valueOf(status)); - } return jsonResponse; + } /** diff --git a/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java b/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java index 4942cd52e1..9674c14c13 100644 --- a/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java +++ b/cobigen/cobigen-core-api/src/test/java/com/devonfw/cobigen/api/MavenUtilTest.java @@ -15,7 +15,6 @@ import org.junit.Rule; import org.junit.Test; -import com.devonfw.cobigen.api.constants.MavenSearchRepositoryConstants; import com.devonfw.cobigen.api.exception.RestSearchResponseException; import com.devonfw.cobigen.api.util.MavenUtil; import com.devonfw.cobigen.api.util.to.AbstractSearchResponse; @@ -26,8 +25,6 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.github.tomakehurst.wiremock.junit.WireMockRule; -import jakarta.ws.rs.ProcessingException; - /** * Test class for maven utilities */ @@ -51,31 +48,43 @@ public void testRetrieveMavenArtifactsWithInvalidLinkReturnsNull() { } /** - * Tests if an exception gets thrown when a faulty target link without a token was used + * Tests if a {@link RestSearchResponseException} gets thrown when a faulty target link without a token was used */ - @Test(expected = ProcessingException.class) + @Test public void testWrongTargetLinkThrowsException() { - AbstractSearchResponse.retrieveJsonResponseWithAuthenticationToken("this/is/not/a/link", null, null); + try { + AbstractSearchResponse.retrieveJsonResponseWithAuthenticationToken("this/is/not/a/link", null, null); + } catch (RestSearchResponseException e) { + assertThat(e).hasMessage("The target URL was faulty."); + } } /** * Tests if an exception gets thrown when a faulty target link and token was used */ - @Test(expected = ProcessingException.class) + @Test public void testWrongTargetLinkAndTokenThrowsException() { - AbstractSearchResponse.retrieveJsonResponseWithAuthenticationToken("this/is/not/a/link", "thisisabadtoken", null); + try { + AbstractSearchResponse.retrieveJsonResponseWithAuthenticationToken("this/is/not/a/link", "thisisabadtoken", null); + } catch (RestSearchResponseException e) { + assertThat(e).hasMessage("The target URL was faulty."); + } } /** - * Tests if an exception gets thrown when a status code was not 200 + * Tests if a {@link RestSearchResponseException} gets thrown when a status code was not 200 but 400 instead */ - @Test(expected = RestSearchResponseException.class) + @Test public void testWrongResponseStatusCodeThrowsException() { - AbstractSearchResponse - .retrieveJsonResponseWithAuthenticationToken("https://search.maven.org/solrsearch/select?test", null, null); + try { + AbstractSearchResponse + .retrieveJsonResponseWithAuthenticationToken("https://search.maven.org/solrsearch/select?test", null, null); + } catch (RestSearchResponseException e) { + assertThat(e).hasMessage("The search REST API returned the unexpected status code: 400"); + } } /** @@ -204,7 +213,7 @@ public void testMavenSearchRequestGetsValidDownloadLinks() throws IOException { // given List downloadList; - this.wireMockRule.stubFor(get(urlMatching("/artifactory/solrsearch/.*")).willReturn(aResponse().withStatus(202) + this.wireMockRule.stubFor(get(urlMatching("/solrsearch/select.*")).willReturn(aResponse().withStatus(200) .withBody(Files.readAllBytes(Paths.get(testdataRoot).resolve("mavenJsonTest.json"))))); this.wireMockRule @@ -216,8 +225,7 @@ public void testMavenSearchRequestGetsValidDownloadLinks() throws IOException { this.wireMockRule.stubFor(get(urlMatching("/service/rest/v1/search.*")).willReturn(aResponse().withStatus(404))); // when - downloadList = MavenUtil.retrieveMavenArtifactsByGroupId(MavenSearchRepositoryConstants.MAVEN_REPOSITORY_URL, - "com.google.inject", null); + downloadList = MavenUtil.retrieveMavenArtifactsByGroupId("http://localhost:8080", "com.google.inject", null); // then assertThat(downloadList).contains( @@ -238,19 +246,18 @@ public void testNexus2SearchRequestGetsValidDownloadLinks() throws IOException { // given List downloadList; - this.wireMockRule.stubFor(get(urlMatching("/artifactory/solrsearch/.*")).willReturn(aResponse().withStatus(404))); + this.wireMockRule.stubFor(get(urlMatching("/artifactory/solrsearch.*")).willReturn(aResponse().withStatus(404))); this.wireMockRule .stubFor(get(urlMatching("/artifactory/api/search/gavc.*")).willReturn(aResponse().withStatus(404))); - this.wireMockRule.stubFor(get(urlMatching("/service/local/lucene/search/.*")).willReturn(aResponse().withStatus(200) - .withBody(Files.readAllBytes(Paths.get(testdataRoot).resolve("nexus3JsonTest.json"))))); + this.wireMockRule.stubFor(get(urlMatching("/service/local/lucene/search.*")).willReturn(aResponse().withStatus(200) + .withBody(Files.readAllBytes(Paths.get(testdataRoot).resolve("nexus2JsonTest.json"))))); this.wireMockRule.stubFor(get(urlMatching("/service/rest/v1/search.*")).willReturn(aResponse().withStatus(404))); // when - downloadList = MavenUtil.retrieveMavenArtifactsByGroupId(MavenSearchRepositoryConstants.NEXUS2_REPOSITORY_URL, - "com.devonfw.cobigen", null); + downloadList = MavenUtil.retrieveMavenArtifactsByGroupId("http://localhost:8080", "com.devonfw.cobigen", null); // then assertThat(downloadList).contains(new URL( @@ -311,8 +318,7 @@ public void testJfrogSearchRequestGetsValidDownloadLinks() throws IOException { this.wireMockRule.stubFor(get(urlMatching("/service/rest/v1/search.*")).willReturn(aResponse().withStatus(404))); // when - downloadList = MavenUtil.retrieveMavenArtifactsByGroupId("http://localhost:8080/artifactory", "com.devonfw.cobigen", - null); + downloadList = MavenUtil.retrieveMavenArtifactsByGroupId("http://localhost:8080", "com.devonfw.cobigen", null); // then assertThat(downloadList).contains(new URL( From 9d3787154b7379bc5331249826d5b588c7c55336 Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Fri, 9 Sep 2022 15:37:02 +0200 Subject: [PATCH 38/42] #1529 updated okhttp changed okhttp version from 4.9.1 to 4.10.0 --- cobigen/cobigen-core-api/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cobigen/cobigen-core-api/pom.xml b/cobigen/cobigen-core-api/pom.xml index d3db948b3d..735747debb 100644 --- a/cobigen/cobigen-core-api/pom.xml +++ b/cobigen/cobigen-core-api/pom.xml @@ -57,7 +57,7 @@ com.squareup.okhttp3 okhttp - 4.9.1 + 4.10.0 From 341ae3434f52f3b897df164a866b5c8571aff3a4 Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Fri, 9 Sep 2022 15:51:33 +0200 Subject: [PATCH 39/42] #1529 added okhttp version to root pom added latest okhttp dependeny to root pom.xml removed fixed okhttp versions from core-api and core-externalprocess-api --- cobigen/cobigen-core-api/pom.xml | 1 - cobigen/core-externalprocess-api/pom.xml | 1 - pom.xml | 5 +++++ 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/cobigen/cobigen-core-api/pom.xml b/cobigen/cobigen-core-api/pom.xml index 735747debb..380b6e4345 100644 --- a/cobigen/cobigen-core-api/pom.xml +++ b/cobigen/cobigen-core-api/pom.xml @@ -57,7 +57,6 @@ com.squareup.okhttp3 okhttp - 4.10.0 diff --git a/cobigen/core-externalprocess-api/pom.xml b/cobigen/core-externalprocess-api/pom.xml index 1e1c2958d6..5c6c5c7caa 100644 --- a/cobigen/core-externalprocess-api/pom.xml +++ b/cobigen/core-externalprocess-api/pom.xml @@ -25,7 +25,6 @@ com.squareup.okhttp3 okhttp - 4.9.1 com.google.code.gson diff --git a/pom.xml b/pom.xml index 599e64c995..61cf76e8ed 100644 --- a/pom.xml +++ b/pom.xml @@ -134,6 +134,11 @@ gson 2.8.6 + + com.squareup.okhttp3 + okhttp + 4.10.0 + jaxen jaxen From bd76bcaebb1ffefaebf1e39eb49d1e5960f80181 Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Fri, 9 Sep 2022 18:21:35 +0200 Subject: [PATCH 40/42] #1529 added okio added transitive okio dependency --- cobigen/core-externalprocess-api/pom.xml | 4 ++++ pom.xml | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/cobigen/core-externalprocess-api/pom.xml b/cobigen/core-externalprocess-api/pom.xml index 5c6c5c7caa..392ee5c6a6 100644 --- a/cobigen/core-externalprocess-api/pom.xml +++ b/cobigen/core-externalprocess-api/pom.xml @@ -26,6 +26,10 @@ com.squareup.okhttp3 okhttp + + com.squareup.okio + okio + com.google.code.gson gson diff --git a/pom.xml b/pom.xml index 61cf76e8ed..e1fcaac515 100644 --- a/pom.xml +++ b/pom.xml @@ -139,6 +139,11 @@ okhttp 4.10.0 + + com.squareup.okio + okio + 3.2.0 + jaxen jaxen From 63346f0a30c933689489420f73529ce224b5def6 Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Wed, 14 Sep 2022 13:53:28 +0200 Subject: [PATCH 41/42] #1529 implemented requested changes Changed enum values to uppercase --- .../cobigen/api/constants/MavenSearchRepositoryType.java | 8 ++++---- .../cobigen/api/util/to/jfrog/JfrogSearchResponse.java | 2 +- .../cobigen/api/util/to/maven/MavenSearchResponse.java | 2 +- .../cobigen/api/util/to/nexus2/Nexus2SearchResponse.java | 2 +- .../cobigen/api/util/to/nexus3/Nexus3SearchResponse.java | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/constants/MavenSearchRepositoryType.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/constants/MavenSearchRepositoryType.java index e7049d00a5..e5c8733d5f 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/constants/MavenSearchRepositoryType.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/constants/MavenSearchRepositoryType.java @@ -9,20 +9,20 @@ public enum MavenSearchRepositoryType { /** * Nexus2 search repository type */ - nexus2, + NEXUS2, /** * Nexus3 search repository type */ - nexus3, + NEXUS3, /** * Maven search repository type */ - maven, + MAVEN, /** * Jfrog search repository type */ - jfrog + JFROG } diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/jfrog/JfrogSearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/jfrog/JfrogSearchResponse.java index 1b388fca87..19b8f646c3 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/jfrog/JfrogSearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/jfrog/JfrogSearchResponse.java @@ -64,6 +64,6 @@ public List retrieveDownloadURLs() throws MalformedURLException { @Override public MavenSearchRepositoryType getRepositoryType() { - return MavenSearchRepositoryType.jfrog; + return MavenSearchRepositoryType.JFROG; } } diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponse.java index 99606dbd1f..d320b23a21 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/maven/MavenSearchResponse.java @@ -73,7 +73,7 @@ public List retrieveDownloadURLs() throws MalformedURLException { @Override public MavenSearchRepositoryType getRepositoryType() { - return MavenSearchRepositoryType.maven; + return MavenSearchRepositoryType.MAVEN; } } \ No newline at end of file diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponse.java index abbe4ff2d5..3bd9ada1ca 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus2/Nexus2SearchResponse.java @@ -65,6 +65,6 @@ public String retrieveJsonResponse(String repositoryUrl, String groupId, String @Override public MavenSearchRepositoryType getRepositoryType() { - return MavenSearchRepositoryType.nexus2; + return MavenSearchRepositoryType.NEXUS2; } } diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponse.java index 74ffbd5b7a..4a31f6eca8 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/nexus3/Nexus3SearchResponse.java @@ -60,6 +60,6 @@ public String retrieveJsonResponse(String repositoryUrl, String groupId, String @Override public MavenSearchRepositoryType getRepositoryType() { - return MavenSearchRepositoryType.nexus3; + return MavenSearchRepositoryType.NEXUS3; } } From f86f3aaf0fcb4256b918ed317695325e2528cb87 Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Wed, 14 Sep 2022 16:40:30 +0200 Subject: [PATCH 42/42] #1529 implemented requested changes moved status into not null if condition --- .../devonfw/cobigen/api/util/to/AbstractSearchResponse.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractSearchResponse.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractSearchResponse.java index e6320c8036..eb00dba93a 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractSearchResponse.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/to/AbstractSearchResponse.java @@ -120,9 +120,8 @@ public static String retrieveJsonResponseWithAuthenticationToken(String targetLi response = httpClient.newCall(new Request.Builder().url(targetLink).get().build()).execute(); } - int status = response.code(); - if (response != null) { + int status = response.code(); if (status == 200 || status == 201 || status == 204) { jsonResponse = response.body().string(); } else {