summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorliamfallon <liam.fallon@ericsson.com>2018-04-24 15:29:41 +0100
committerliamfallon <liam.fallon@ericsson.com>2018-04-24 15:32:06 +0100
commit30b44d3e752d44f8f870594a05464c9e1c5158b6 (patch)
treef6f1f1f76650dbde5c8416594d3ee36ab7734155
parentab8bc20cabdbdd104a69da321d69d49662e53b92 (diff)
Implement Sonar Nexus REST interface directly
The BRMS gateway used the obsolete nexus-rest-client-java library to query the Nexus repository. This change removes that dependency and implements an interface directly to Nexus servers using the Nexus REST interface. This change is being submitted to ensure that the security issues with the plugin are resolved. The unit test for the new code will be implemented in a later submission. Issue-ID: POLICY-700 Change-Id: Ia1044e432d370034fe13f42cf7ed55d75e21187a Signed-off-by: liamfallon <liam.fallon@ericsson.com>
-rw-r--r--BRMSGateway/pom.xml19
-rw-r--r--BRMSGateway/src/main/java/org/onap/policy/brms/api/BrmsPush.java45
-rw-r--r--BRMSGateway/src/main/java/org/onap/policy/brms/api/nexus/NexusRestSearchParameters.java356
-rw-r--r--BRMSGateway/src/main/java/org/onap/policy/brms/api/nexus/NexusRestWrapper.java233
-rw-r--r--BRMSGateway/src/main/java/org/onap/policy/brms/api/nexus/NexusRestWrapperException.java42
-rw-r--r--BRMSGateway/src/main/java/org/onap/policy/brms/api/nexus/pojo/NexusArtifact.java96
-rw-r--r--BRMSGateway/src/main/java/org/onap/policy/brms/api/nexus/pojo/NexusArtifactHit.java46
-rw-r--r--BRMSGateway/src/main/java/org/onap/policy/brms/api/nexus/pojo/NexusArtifactLink.java44
-rw-r--r--BRMSGateway/src/main/java/org/onap/policy/brms/api/nexus/pojo/NexusRepository.java68
-rw-r--r--BRMSGateway/src/main/java/org/onap/policy/brms/api/nexus/pojo/NexusSearchResult.java72
-rw-r--r--BRMSGateway/src/test/java/org/onap/policy/brms/api/nexus/NexusRestWrapperTest.java52
11 files changed, 1029 insertions, 44 deletions
diff --git a/BRMSGateway/pom.xml b/BRMSGateway/pom.xml
index 82442e10d..29e9289bf 100644
--- a/BRMSGateway/pom.xml
+++ b/BRMSGateway/pom.xml
@@ -60,25 +60,6 @@
<artifactId>integrity-monitor</artifactId>
<version>${project.version}</version>
</dependency>
- <!-- CLM security fix - force use of commons-collections 3.2.2. Remove
- this if a new version of nexus-rest-client-java is upgraded to not use velocity
- (and then subsequently commons-collections v3.1 -->
- <dependency>
- <groupId>commons-collections</groupId>
- <artifactId>commons-collections</artifactId>
- <version>3.2.2</version>
- </dependency>
- <dependency>
- <groupId>org.sonatype.nexus</groupId>
- <artifactId>nexus-rest-client-java</artifactId>
- <version>2.3.1-01</version>
- <exclusions>
- <exclusion>
- <groupId>commons-collections</groupId>
- <artifactId>commons-collections</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
diff --git a/BRMSGateway/src/main/java/org/onap/policy/brms/api/BrmsPush.java b/BRMSGateway/src/main/java/org/onap/policy/brms/api/BrmsPush.java
index 5a7b25ec3..5e8046e22 100644
--- a/BRMSGateway/src/main/java/org/onap/policy/brms/api/BrmsPush.java
+++ b/BRMSGateway/src/main/java/org/onap/policy/brms/api/BrmsPush.java
@@ -58,6 +58,7 @@ import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;
+import javax.ws.rs.ProcessingException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringEscapeUtils;
@@ -76,6 +77,10 @@ import org.codehaus.plexus.util.WriterFactory;
import org.eclipse.persistence.config.PersistenceUnitProperties;
import org.onap.policy.api.PEDependency;
import org.onap.policy.api.PolicyException;
+import org.onap.policy.brms.api.nexus.NexusRestSearchParameters;
+import org.onap.policy.brms.api.nexus.NexusRestWrapper;
+import org.onap.policy.brms.api.nexus.NexusRestWrapperException;
+import org.onap.policy.brms.api.nexus.pojo.NexusArtifact;
import org.onap.policy.brms.entity.BrmsGroupInfo;
import org.onap.policy.brms.entity.BrmsPolicyInfo;
import org.onap.policy.brms.entity.DependencyInfo;
@@ -89,11 +94,6 @@ import org.onap.policy.utils.BackUpMonitor;
import org.onap.policy.utils.BusPublisher;
import org.onap.policy.utils.PolicyUtils;
import org.onap.policy.xacml.api.XACMLErrorConstants;
-import org.sonatype.nexus.client.NexusClient;
-import org.sonatype.nexus.client.NexusClientException;
-import org.sonatype.nexus.client.NexusConnectionException;
-import org.sonatype.nexus.client.rest.NexusRestClient;
-import org.sonatype.nexus.rest.model.NexusArtifact;
/**
* BRMSPush: Application responsible to push policies to the BRMS PDP Policy Repository (PR).
@@ -102,7 +102,6 @@ import org.sonatype.nexus.rest.model.NexusArtifact;
* @version 1.0
*/
-@SuppressWarnings("deprecation")
public class BrmsPush {
private static final String GROUP_NAMES = "groupNames";
private static final String DROOLS_APPS_TEMPLATE_GROUP =
@@ -587,7 +586,7 @@ public class BrmsPush {
URL website;
final String fileName = "rule.jar";
try {
- website = new URL(artifact.getResourceURI());
+ website = new URL(artifact.getUrlPath() + ".jar");
try (ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream(fileName)) {
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
@@ -685,34 +684,30 @@ public class BrmsPush {
}
private List<NexusArtifact> getArtifactFromNexus(final String selectedName, final String version) {
- final NexusClient client = new NexusRestClient();
+ NexusRestWrapper restWrapper = null;
int index = 0;
boolean flag = false;
while (index < repUrlList.size()) {
try {
final String repUrl = repUrlList.get(0);
- client.connect(repUrl.substring(0, repUrl.indexOf(repUrl.split(":[0-9]+\\/nexus")[1])), repUserName,
- repPassword);
- final NexusArtifact template = new NexusArtifact();
- template.setGroupId(getGroupId(selectedName));
- template.setArtifactId(getArtifactId(selectedName));
- if (version != null) {
- template.setVersion(version);
- }
- final List<NexusArtifact> resultList = client.searchByGAV(template);
+ restWrapper =
+ new NexusRestWrapper(repUrl.substring(0, repUrl.indexOf(repUrl.split(":[0-9]+\\/nexus")[1])),
+ repUserName, repPassword);
+ final NexusRestSearchParameters searchParameters = new NexusRestSearchParameters();
+ searchParameters.useFilterSearch(
+ getGroupId(selectedName), getArtifactId(selectedName), version, null, null);
+
+ final List<NexusArtifact> resultList = restWrapper.findArtifact(searchParameters).getArtifactList();
if (resultList != null) {
flag = true;
return resultList;
}
- } catch (NexusClientException | NexusConnectionException | NullPointerException e) {
+ } catch (NexusRestWrapperException | ProcessingException e) {
LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Connection to remote Nexus has failed. "
+ e.getMessage(), e);
- } finally {
- try {
- client.disconnect();
- } catch (NexusClientException | NexusConnectionException e) {
- LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "failed to disconnect Connection from Nexus."
- + e.getMessage(), e);
+ } finally {
+ if (null != restWrapper) {
+ restWrapper.close();
}
if (!flag) {
Collections.rotate(repUrlList, -1);
@@ -937,7 +932,7 @@ public class BrmsPush {
pomWriter.write(writer, model);
} catch (final Exception e) {
LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error while creating POM for " + getArtifactId(name)
- + e.getMessage(), e);
+ + e.getMessage(), e);
} finally {
IOUtil.close(writer);
}
diff --git a/BRMSGateway/src/main/java/org/onap/policy/brms/api/nexus/NexusRestSearchParameters.java b/BRMSGateway/src/main/java/org/onap/policy/brms/api/nexus/NexusRestSearchParameters.java
new file mode 100644
index 000000000..eec37392e
--- /dev/null
+++ b/BRMSGateway/src/main/java/org/onap/policy/brms/api/nexus/NexusRestSearchParameters.java
@@ -0,0 +1,356 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP Policy Engine
+ * ================================================================================
+ * Copyright (C) 2018 Ericsson Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.brms.api.nexus;
+
+import java.net.URI;
+
+import javax.ws.rs.core.UriBuilder;
+
+/**
+ * The Class NexusRestSearchParameters is used to specify the parameters of a search on Nexus.
+ * The parameters are used to return the search part of the URL for the Maven search.
+ */
+public class NexusRestSearchParameters {
+ // The REST end point for Nexus Lucene searches
+ private static final String NEXUS_LUCENE_SEARCH_PATH = "service/local/lucene/search";
+
+ // REST search query parameter names
+ private static final String KEYWORD_QUERY_PARAM = "q";
+ private static final String GROUP_ID_QUERY_PARAM = "g";
+ private static final String ARTIFACT_ID_QUERY_PARAM = "a";
+ private static final String VERSION_QUERY_PARAM = "v";
+ private static final String PACKAGING_TYPE_QUERY_PARAM = "p";
+ private static final String CLASSIFIER_QUERY_PARAM = "c";
+ private static final String CLASS_NAME_QUERY_PARAM = "cn";
+ private static final String CHECKSUM_QUERY_PARAM = "sha1";
+ private static final String FROM_QUERY_PARAM = "from";
+ private static final String COUNT_QUERY_PARAM = "count";
+ private static final String REPOSITORY_ID_QUERY_PARAM = "repositoryId";
+
+ private enum SearchType {
+ KEYWORD, // Search using a keyword
+ FILTER, // Search using a group ID, artifact ID, version, packaging type, and/or classifier filter
+ CLASS_NAME, // Search for a class name
+ CHECKSUM // Search for artifacts matching a certain checksum
+ }
+
+ // The type of search to perform
+ private SearchType searchType = null;
+
+ // Search filters
+ private String keyword;
+ private String groupId;
+ private String artifactId;
+ private String version;
+ private String packagingType;
+ private String classifier;
+ private String className;
+ private String checksum;
+
+ // Repository filter
+ private String repositoryId;
+
+ // Scope filters
+ private int from = -1;
+ private int count = -1;
+
+ /**
+ * Specify searching using a keyword.
+ *
+ * @param keyword The keyword to search for
+ * @throws NexusRestWrapperException on invalid keywords
+ */
+ public void useKeywordSearch(final String keyword) throws NexusRestWrapperException {
+ if (isNullOrBlank(keyword)) {
+ throw new NexusRestWrapperException("keyword must be specified for Nexus keyword searches");
+ }
+
+ searchType = SearchType.KEYWORD;
+ this.keyword = keyword;
+ }
+
+ /**
+ * Specify searching using a filter.
+ *
+ * @param groupId The group ID to filter on
+ * @param artifactId The artifact ID to filter on
+ * @param version The version to filter on
+ * @param packagingType The packaging type to filter on
+ * @param classifier The classifier to filter on
+ * @throws NexusRestWrapperException on invalid filters
+ */
+ public void useFilterSearch(final String groupId, final String artifactId, final String version,
+ final String packagingType, final String classifier) throws NexusRestWrapperException {
+ if (isNullOrBlank(groupId) && isNullOrBlank(artifactId) && isNullOrBlank(version)
+ && isNullOrBlank(packagingType) && isNullOrBlank(classifier)) {
+ throw new NexusRestWrapperException(
+ "at least one filter parameter must be specified for Nexus keyword searches");
+ }
+
+ searchType = SearchType.FILTER;
+ this.groupId = groupId;
+ this.artifactId = artifactId;
+ this.version = version;
+ this.packagingType = packagingType;
+ this.classifier = classifier;
+ }
+
+ /**
+ * Specify searching using a class name.
+ *
+ * @param className The class name to search for
+ * @throws NexusRestWrapperException on invalid className
+ */
+ public void useClassNameSearch(final String className) throws NexusRestWrapperException {
+ if (isNullOrBlank(className)) {
+ throw new NexusRestWrapperException("className must be specified for Nexus keyword searches");
+ }
+
+ searchType = SearchType.CLASS_NAME;
+ this.className = className;
+ }
+
+ /**
+ * Specify searching using a checksum.
+ *
+ * @param checksum The checksum to search for
+ * @throws NexusRestWrapperException on invalid checksum
+ */
+ public void useChecksumSearch(final String checksum) throws NexusRestWrapperException {
+ if (isNullOrBlank(checksum)) {
+ throw new NexusRestWrapperException("checksum must be specified for Nexus keyword searches");
+ }
+
+ searchType = SearchType.CHECKSUM;
+ this.checksum = checksum;
+ }
+
+ /**
+ * Search on a specific repository.
+ *
+ * @param repositoryId The repository to search
+ * @return this object to allow chaining of methods
+ * @throws NexusRestWrapperException on invalid repositoryId
+ */
+ public NexusRestSearchParameters setRepositoryId(String repositoryId) throws NexusRestWrapperException {
+ if (isNullOrBlank(repositoryId)) {
+ throw new NexusRestWrapperException("repositoryId must be specified for Nexus keyword searches");
+ }
+
+ this.repositoryId = repositoryId;
+ return this;
+ }
+
+ /**
+ * Return the search results from this result number.
+ *
+ * @param from The number of the first result to return
+ * @return this object to allow chaining of methods
+ * @throws NexusRestWrapperException on invalid from value
+ */
+ public NexusRestSearchParameters setFrom(int from) throws NexusRestWrapperException {
+ if (from < 0) {
+ throw new NexusRestWrapperException("from cannot be less than 0 for Nexus keyword searches");
+ }
+
+ this.from = from;
+ return this;
+ }
+
+ /**
+ * Return the specified number of search results.
+ *
+ * @param count The number of results to return
+ * @return this object to allow chaining of methods
+ * @throws NexusRestWrapperException on invalid count value
+ */
+ public NexusRestSearchParameters setCount(int count) throws NexusRestWrapperException {
+ if (count < 1) {
+ throw new NexusRestWrapperException("count cannot be less than 1 for Nexus keyword searches");
+ }
+
+ this.count = count;
+ return this;
+ }
+
+ /**
+ * Compose the URI for the search to the Nexus server.
+ *
+ * @param nexusServerUrl the URL of the server on which to search
+ * @return the search URI
+ * @throws NexusRestWrapperException on search URL composition exceptions
+ */
+ public URI getSearchUri(final String nexusServerUrl) throws NexusRestWrapperException {
+ if (isNullOrBlank(nexusServerUrl)) {
+ throw new NexusRestWrapperException("nexusServerUrl must be specified for Nexus keyword searches");
+ }
+
+ // Use a URI builder to build up the search URI
+ UriBuilder uriBuilder = UriBuilder
+ .fromPath(nexusServerUrl)
+ .path(NEXUS_LUCENE_SEARCH_PATH);
+
+ switch (searchType) {
+ case KEYWORD:
+ getKeywordSearchUri(uriBuilder);
+ break;
+
+ case FILTER:
+ getFitlerSearchUri(uriBuilder);
+ break;
+
+ case CLASS_NAME:
+ getClassNameSearchUri(uriBuilder);
+ break;
+
+ case CHECKSUM:
+ getChecksumSearchUri(uriBuilder);
+ break;
+
+ default:
+ throw new NexusRestWrapperException("search parameters have not been specified for the NExus search");
+ }
+
+ // Add the repository ID query parameter is required
+ if (null != repositoryId) {
+ uriBuilder.queryParam(REPOSITORY_ID_QUERY_PARAM, repositoryId);
+ }
+
+ // Add the from and count values if required
+ if (from >= 0) {
+ uriBuilder.queryParam(FROM_QUERY_PARAM, from);
+ }
+ if (count >= 0) {
+ uriBuilder.queryParam(COUNT_QUERY_PARAM, count);
+ }
+
+ return uriBuilder.build();
+ }
+
+ /**
+ * Compose the query parameters for a keyword search.
+ * @param uriBuilder The builder to add query parameters to
+ */
+ private void getKeywordSearchUri(UriBuilder uriBuilder) {
+ uriBuilder.queryParam(KEYWORD_QUERY_PARAM, keyword);
+ }
+
+ /**
+ * Compose the query parameters for a filter search.
+ * @param uriBuilder The builder to add query parameters to
+ */
+ private void getFitlerSearchUri(UriBuilder uriBuilder) {
+ if (null != groupId) {
+ uriBuilder.queryParam(GROUP_ID_QUERY_PARAM, groupId);
+ }
+ if (null != artifactId) {
+ uriBuilder.queryParam(ARTIFACT_ID_QUERY_PARAM, artifactId);
+ }
+ if (null != version) {
+ uriBuilder.queryParam(VERSION_QUERY_PARAM, version);
+ }
+ if (null != packagingType) {
+ uriBuilder.queryParam(PACKAGING_TYPE_QUERY_PARAM, packagingType);
+ }
+ if (null != classifier) {
+ uriBuilder.queryParam(CLASSIFIER_QUERY_PARAM, classifier);
+ }
+ }
+
+ /**
+ * Compose the query parameters for a class name search.
+ * @param uriBuilder The builder to add query parameters to
+ */
+ private void getClassNameSearchUri(UriBuilder uriBuilder) {
+ uriBuilder.queryParam(CLASS_NAME_QUERY_PARAM, className);
+ }
+
+ /**
+ * Compose the query parameters for a checksum search.
+ * @param uriBuilder The builder to add query parameters to
+ */
+ private void getChecksumSearchUri(UriBuilder uriBuilder) {
+ uriBuilder.queryParam(CHECKSUM_QUERY_PARAM, checksum);
+ }
+
+ public SearchType getSearchType() {
+ return searchType;
+ }
+
+ public String getKeyword() {
+ return keyword;
+ }
+
+ public String getGroupId() {
+ return groupId;
+ }
+
+ public String getArtifactId() {
+ return artifactId;
+ }
+
+ public String getVersion() {
+ return version;
+ }
+
+ public String getPackagingType() {
+ return packagingType;
+ }
+
+ public String getClassifier() {
+ return classifier;
+ }
+
+ public String getClassName() {
+ return className;
+ }
+
+ public String getChecksum() {
+ return checksum;
+ }
+
+ public String getRepositoryId() {
+ return repositoryId;
+ }
+
+ public int getFrom() {
+ return from;
+ }
+
+ public int getCount() {
+ return count;
+ }
+
+ /**
+ * Check if a string is null or all white space.
+ */
+ private boolean isNullOrBlank(final String parameter) {
+ return null == parameter || parameter.trim().isEmpty();
+ }
+
+ @Override
+ public String toString() {
+ return "NexusRestSearchParameters [searchType=" + searchType + ", keyword=" + keyword + ", groupId=" + groupId
+ + ", artifactId=" + artifactId + ", version=" + version + ", packagingType=" + packagingType
+ + ", classifier=" + classifier + ", className=" + className + ", checksum=" + checksum
+ + ", repositoryId=" + repositoryId + ", from=" + from + ", count=" + count + "]";
+ }
+}
diff --git a/BRMSGateway/src/main/java/org/onap/policy/brms/api/nexus/NexusRestWrapper.java b/BRMSGateway/src/main/java/org/onap/policy/brms/api/nexus/NexusRestWrapper.java
new file mode 100644
index 000000000..afa3e3794
--- /dev/null
+++ b/BRMSGateway/src/main/java/org/onap/policy/brms/api/nexus/NexusRestWrapper.java
@@ -0,0 +1,233 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP Policy Engine
+ * ================================================================================
+ * Copyright (C) 2018 Ericsson Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.brms.api.nexus;
+
+import com.google.gson.Gson;
+
+import java.net.URI;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.ws.rs.client.Client;
+import javax.ws.rs.client.ClientBuilder;
+import javax.ws.rs.client.Invocation.Builder;
+import javax.ws.rs.core.Response;
+
+import org.onap.policy.brms.api.nexus.pojo.NexusArtifact;
+import org.onap.policy.brms.api.nexus.pojo.NexusRepository;
+import org.onap.policy.brms.api.nexus.pojo.NexusSearchResult;
+import org.onap.policy.common.logging.flexlogger.FlexLogger;
+import org.onap.policy.common.logging.flexlogger.Logger;
+
+/**
+ * The Class NexusRestWrapper provides a Java API to a Nexus repository, wrapping the Nexus REST interface.
+ */
+public class NexusRestWrapper {
+ private static final Logger LOGGER = FlexLogger.getLogger(NexusRestWrapper.class.getName());
+
+ // A web client for issuing REST requests to the Nexus server
+ private final Client client;
+
+ // The URL of the Nexus server
+ private final String nexusServerUrl;
+
+ // Credentials for Nexus server logins
+ private String nexusUser;
+ private String nexusPassword;
+
+ /**
+ * Instantiates a new Nexus REST agent.
+ *
+ * @param nexusServerUrl the URL of the Nexus server as a string
+ * @throws NexusRestWrapperException on errors on the Nexus server URL
+ */
+ public NexusRestWrapper(final String nexusServerUrl) throws NexusRestWrapperException {
+ LOGGER.trace("new NexusRestWrapper: nexusServerUrl=" + nexusServerUrl);
+
+ if (isNullOrBlank(nexusServerUrl)) {
+ throw new NexusRestWrapperException("nexusServerUrl must be specified for the Nexus server");
+ }
+
+ this.nexusServerUrl = nexusServerUrl;
+
+ // Create a client for RST calls towards the Nexus server
+ client = ClientBuilder.newClient();
+ }
+
+ /**
+ * Instantiates a new Nexus REST agent with credentials.
+ *
+ * @param nexusServerUrl the URL of the Nexus server as a string
+ * @param nexusUser the Nexus userid
+ * @param nexusPassword the Nexus password
+ * @throws NexusRestWrapperException on parameter exceptions
+ */
+ public NexusRestWrapper(final String nexusServerUrl, final String nexusUser, final String nexusPassword)
+ throws NexusRestWrapperException {
+ LOGGER.trace("new NexusRestWrapper: nexusServerUrl=" + nexusServerUrl);
+
+ if (isNullOrBlank(nexusServerUrl)) {
+ throw new NexusRestWrapperException("nexusServerUrl must be specified for the Nexus server");
+ }
+
+ if (isNullOrBlank(nexusUser) || isNullOrBlank(nexusPassword)) {
+ throw new NexusRestWrapperException("nexuusUser and nexusPassword must both be specified");
+ }
+
+ this.nexusServerUrl = nexusServerUrl;
+ this.nexusUser = nexusUser;
+ this.nexusPassword = nexusPassword;
+
+ // Create a client for RST calls towards the Nexus server
+ client = ClientBuilder.newClient();
+
+ LOGGER.trace("NexusRestWrapper created: nexusServerUrl=" + nexusServerUrl);
+ }
+
+ /**
+ * Close the REST client.
+ */
+ public void close() {
+ LOGGER.trace("NexusRestWrapper closing: url=" + nexusServerUrl);
+
+ // Close the web client
+ client.close();
+
+ LOGGER.trace("NexusRestWrapper closed: url=" + nexusServerUrl);
+ }
+
+ /**
+ * Find an artifact in the Nexus server.
+ *
+ * @param searchParameters
+ * the search parameters to use for the search
+ * @return the list of artifacts found that match the requested artifact
+ * @throws NexusRestWrapperException
+ * Exceptions accessing the Nexus server
+ */
+ public NexusSearchResult findArtifact(final NexusRestSearchParameters searchParameters)
+ throws NexusRestWrapperException {
+
+ LOGGER.trace("new search with search parameters: " + searchParameters);
+
+ // Issue the REST request to perform the search
+ URI searchUri = searchParameters.getSearchUri(nexusServerUrl);
+
+ LOGGER.debug("search URI is: " + searchUri.toString());
+
+ // Compose the REST request
+ Builder requestBuilder = client.target(searchUri).request("application/json");
+ getAuthorizationHeader(requestBuilder);
+
+ // Issue the REST request
+ Response response = requestBuilder.get();
+
+ LOGGER.debug("search response is: " + response.toString());
+
+ // Check the HTTP response code for the search
+ if (Response.Status.OK.getStatusCode() != response.getStatus()) {
+ LOGGER.warn("search to URI " + searchUri.toString() + "failed, response was: " + response.toString());
+ throw new NexusRestWrapperException("query to Nexus failed with message: " + response.toString());
+ }
+
+ try {
+ // Get the JSON string with the the search result
+ String responseString = response.readEntity(String.class);
+
+ // Parse the returned JSON into result POJOs
+ NexusSearchResult searchResult = new Gson().fromJson(responseString, NexusSearchResult.class);
+
+ // We now need to expand the release and snapshot URL paths for each artifact
+ expandArtifactUrlPaths(searchResult);
+
+ return searchResult;
+ } catch (Exception e) {
+ LOGGER.warn("processing of result from search to URI " + searchUri
+ + " failed with message " + e.getMessage());
+ throw new NexusRestWrapperException(
+ "processing of result from query to Nexus failed with message: " + e.getMessage(), e);
+ }
+ }
+
+ /**
+ * Get the authorisation header for the user name and password.
+ * @param requestBuilder the request builder to add authorization to
+ * @return the authorisation header
+ */
+ private Builder getAuthorizationHeader(Builder requestBuilder) {
+ if (null != nexusUser && null != nexusPassword) {
+ String userPassString = nexusUser + ":" + nexusPassword;
+ requestBuilder.header("Authorization", "Basic "
+ + java.util.Base64.getEncoder().encodeToString(userPassString.getBytes()));
+ }
+
+ return requestBuilder;
+ }
+
+ /**
+ * Use the Repository URLs in the search result to create a release and snapshot URL path for each artifact.
+ * @param searchResult the results of a Nexus server search
+ */
+ private void expandArtifactUrlPaths(NexusSearchResult searchResult) {
+ // Create a map of repositories for doing lookups
+ Map<String, NexusRepository> repositoryMap = new HashMap<>();
+
+ for (NexusRepository repository : searchResult.getRepoDetailsList()) {
+ repositoryMap.put(repository.getRepositoryId(), repository);
+ }
+
+ for (NexusArtifact artifact : searchResult.getArtifactList()) {
+ artifact.setUrlPath(composeArtifactUrlPath(repositoryMap, artifact));
+ }
+ }
+
+ /**
+ * Compose an artifact URL path using the repository and artifact details for the artifact.
+ * @param repositoryMap the available repositories
+ * @param artifact the artifact
+ * @return the URL path
+ */
+ private String composeArtifactUrlPath(Map<String, NexusRepository> repositoryMap, NexusArtifact artifact) {
+ // We always have one hit
+ NexusRepository repository = repositoryMap.get(artifact.getArtifactHits().get(0).getRepositoryId());
+
+ return new StringBuilder()
+ .append(repository.getRepositoryUrl())
+ .append("/content/")
+ .append(artifact.getGroupId().replace('.', '/'))
+ .append('/')
+ .append(artifact.getArtifactId())
+ .append('/')
+ .append(artifact.getVersion())
+ .append('/')
+ .append(artifact.getArtifactId())
+ .append('-')
+ .append(artifact.getVersion())
+ .toString();
+ }
+
+ /**
+ * Check if a string is null or all white space.
+ */
+ private boolean isNullOrBlank(final String parameter) {
+ return null == parameter || parameter.trim().isEmpty();
+ }
+}
diff --git a/BRMSGateway/src/main/java/org/onap/policy/brms/api/nexus/NexusRestWrapperException.java b/BRMSGateway/src/main/java/org/onap/policy/brms/api/nexus/NexusRestWrapperException.java
new file mode 100644
index 000000000..40f02aef2
--- /dev/null
+++ b/BRMSGateway/src/main/java/org/onap/policy/brms/api/nexus/NexusRestWrapperException.java
@@ -0,0 +1,42 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP Policy Engine
+ * ================================================================================
+ * Copyright (C) 2018 Ericsson Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.brms.api.nexus;
+
+public class NexusRestWrapperException extends Exception {
+ private static final long serialVersionUID = -3321271612434193960L;
+
+ /**
+ * Constructor, message only.
+ * @param message the exception message
+ */
+ public NexusRestWrapperException(String message) {
+ super(message);
+ }
+
+ /**
+ * Constructor, message and nested exception.
+ * @param message the exception message
+ * @param throwable the nested exception
+ */
+ public NexusRestWrapperException(final String message, final Throwable throwable) {
+ super(message, throwable);
+ }
+}
diff --git a/BRMSGateway/src/main/java/org/onap/policy/brms/api/nexus/pojo/NexusArtifact.java b/BRMSGateway/src/main/java/org/onap/policy/brms/api/nexus/pojo/NexusArtifact.java
new file mode 100644
index 000000000..5c770b9ef
--- /dev/null
+++ b/BRMSGateway/src/main/java/org/onap/policy/brms/api/nexus/pojo/NexusArtifact.java
@@ -0,0 +1,96 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP Policy Engine
+ * ================================================================================
+ * Copyright (C) 2018 Ericsson Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.brms.api.nexus.pojo;
+
+import java.util.List;
+
+/**
+ * The Class NexusArtifact is a POJO that holds information on an Artifact in a Maven repository. It is populated from
+ * the JSON response to a query on the repository. See:
+ * {@linktourl https://repository.sonatype.org/nexus-indexer-lucene-plugin/default/docs/path__lucene_search.html}
+ */
+public class NexusArtifact {
+ private String groupId;
+ private String artifactId;
+ private String version;
+ private String highlightedFragment;
+ private String latestRelease;
+ private String latestReleaseRepositoryId;
+ private String latestSnapshot;
+ private String latestSnapshotRepositoryId;
+ private List<NexusArtifactHit> artifactHits;
+
+ // Path to the repository, added by wrapper after search is completed
+ private String urlPath;
+
+ public String getGroupId() {
+ return groupId;
+ }
+
+ public String getArtifactId() {
+ return artifactId;
+ }
+
+ public String getVersion() {
+ return version;
+ }
+
+ public String getHighlightedFragment() {
+ return highlightedFragment;
+ }
+
+ public String getLatestRelease() {
+ return latestRelease;
+ }
+
+ public String getLatestReleaseRepositoryId() {
+ return latestReleaseRepositoryId;
+ }
+
+ public String getLatestSnapshot() {
+ return latestSnapshot;
+ }
+
+ public String getLatestSnapshotRepositoryId() {
+ return latestSnapshotRepositoryId;
+ }
+
+ public List<NexusArtifactHit> getArtifactHits() {
+ return artifactHits;
+ }
+
+ public String getUrlPath() {
+ return urlPath;
+ }
+
+ public void setUrlPath(final String urlPath) {
+ this.urlPath = urlPath;
+ }
+
+ @Override
+ public String toString() {
+ return "NexusArtifact [groupId=" + groupId + ", artifactId=" + artifactId + ", version=" + version
+ + ", highlightedFragment=" + highlightedFragment + ", latestRelease=" + latestRelease
+ + ", latestReleaseRepositoryId=" + latestReleaseRepositoryId + ", latestSnapshot=" + latestSnapshot
+ + ", latestSnapshotRepositoryId=" + latestSnapshotRepositoryId + ", artifactHits=" + artifactHits
+ + ", urlPath=" + urlPath + "]";
+ }
+}
diff --git a/BRMSGateway/src/main/java/org/onap/policy/brms/api/nexus/pojo/NexusArtifactHit.java b/BRMSGateway/src/main/java/org/onap/policy/brms/api/nexus/pojo/NexusArtifactHit.java
new file mode 100644
index 000000000..0d173e036
--- /dev/null
+++ b/BRMSGateway/src/main/java/org/onap/policy/brms/api/nexus/pojo/NexusArtifactHit.java
@@ -0,0 +1,46 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP Policy Engine
+ * ================================================================================
+ * Copyright (C) 2018 Ericsson Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.brms.api.nexus.pojo;
+
+import java.util.List;
+
+/**
+ * The Class NexusArtifactHit is a POJO that holds information on the occurrences of an Artifact in a Maven repository.
+ * It is populated from the JSON response to a query on the repository. See:
+ * {@linktourl https://repository.sonatype.org/nexus-indexer-lucene-plugin/default/docs/path__lucene_search.html}
+ */
+public class NexusArtifactHit {
+ private List<NexusArtifactLink> artifactLinks;
+ private String repositoryId;
+
+ public List<NexusArtifactLink> getArtifactLinks() {
+ return artifactLinks;
+ }
+
+ public String getRepositoryId() {
+ return repositoryId;
+ }
+
+ @Override
+ public String toString() {
+ return "NexusArtifactHit [artifactLinks=" + artifactLinks + ", repositoryId=" + repositoryId + "]";
+ }
+}
diff --git a/BRMSGateway/src/main/java/org/onap/policy/brms/api/nexus/pojo/NexusArtifactLink.java b/BRMSGateway/src/main/java/org/onap/policy/brms/api/nexus/pojo/NexusArtifactLink.java
new file mode 100644
index 000000000..ac6da6a72
--- /dev/null
+++ b/BRMSGateway/src/main/java/org/onap/policy/brms/api/nexus/pojo/NexusArtifactLink.java
@@ -0,0 +1,44 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP Policy Engine
+ * ================================================================================
+ * Copyright (C) 2018 Ericsson Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.brms.api.nexus.pojo;
+
+/**
+ * The Class NexusArtifactHit is a POJO that holds a link to an occurrence of an Artifact in a Maven repository.
+ * It is populated from the JSON response to a query on the repository. See:
+ * {@linktourl https://repository.sonatype.org/nexus-indexer-lucene-plugin/default/docs/path__lucene_search.html}
+ */
+public class NexusArtifactLink {
+ private String classifier;
+ private String extension;
+
+ public String getClassifier() {
+ return classifier;
+ }
+
+ public String getExtension() {
+ return extension;
+ }
+
+ @Override
+ public String toString() {
+ return "NexusArtifactLink [classifier=" + classifier + ", extension=" + extension + "]";
+ }
+}
diff --git a/BRMSGateway/src/main/java/org/onap/policy/brms/api/nexus/pojo/NexusRepository.java b/BRMSGateway/src/main/java/org/onap/policy/brms/api/nexus/pojo/NexusRepository.java
new file mode 100644
index 000000000..e2bc48e15
--- /dev/null
+++ b/BRMSGateway/src/main/java/org/onap/policy/brms/api/nexus/pojo/NexusRepository.java
@@ -0,0 +1,68 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP Policy Engine
+ * ================================================================================
+ * Copyright (C) 2018 Ericsson Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.brms.api.nexus.pojo;
+
+import com.google.gson.annotations.SerializedName;
+
+/**
+ * The Class NexusRepository is a POJO that holds information on a repository in Maven.
+ * It is populated directly from the JSON returned from a search in Maven.
+ */
+public class NexusRepository {
+ private String repositoryContentClass;
+ private String repositoryId;
+ private String repositoryKind;
+ private String repositoryName;
+ private String repositoryPolicy;
+ @SerializedName("repositoryURL")
+ private String repositoryUrl;
+
+ public String getRepositoryContentClass() {
+ return repositoryContentClass;
+ }
+
+ public String getRepositoryId() {
+ return repositoryId;
+ }
+
+ public String getRepositoryKind() {
+ return repositoryKind;
+ }
+
+ public String getRepositoryName() {
+ return repositoryName;
+ }
+
+ public String getRepositoryPolicy() {
+ return repositoryPolicy;
+ }
+
+ public String getRepositoryUrl() {
+ return repositoryUrl;
+ }
+
+ @Override
+ public String toString() {
+ return "NexusRepository [repositoryContentClass=" + repositoryContentClass + ", repositoryId=" + repositoryId
+ + ", repositoryKind=" + repositoryKind + ", repositoryName=" + repositoryName + ", repositoryPolicy="
+ + repositoryPolicy + ", repositoryUrl=" + repositoryUrl + "]";
+ }
+}
diff --git a/BRMSGateway/src/main/java/org/onap/policy/brms/api/nexus/pojo/NexusSearchResult.java b/BRMSGateway/src/main/java/org/onap/policy/brms/api/nexus/pojo/NexusSearchResult.java
new file mode 100644
index 000000000..d71d2c7d8
--- /dev/null
+++ b/BRMSGateway/src/main/java/org/onap/policy/brms/api/nexus/pojo/NexusSearchResult.java
@@ -0,0 +1,72 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP Policy Engine
+ * ================================================================================
+ * Copyright (C) 2018 Ericsson Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.brms.api.nexus.pojo;
+
+import java.util.List;
+
+/**
+ * The Class NexusSearchResult holds the result of a search in Nexus. It is populated directly from the JSON returned by
+ * the Nexus search.
+ */
+public class NexusSearchResult {
+ private int totalCount;
+ private int from;
+ private int count;
+ private boolean tooManyResults;
+ private boolean collapsed;
+ private List<NexusRepository> repoDetails;
+ private List<NexusArtifact> data;
+
+ public int getTotalCount() {
+ return totalCount;
+ }
+
+ public int getFrom() {
+ return from;
+ }
+
+ public int getCount() {
+ return count;
+ }
+
+ public boolean isTooManyResults() {
+ return tooManyResults;
+ }
+
+ public boolean isCollapsed() {
+ return collapsed;
+ }
+
+ public List<NexusRepository> getRepoDetailsList() {
+ return repoDetails;
+ }
+
+ public List<NexusArtifact> getArtifactList() {
+ return data;
+ }
+
+ @Override
+ public String toString() {
+ return "NexusSearchResult [totalCount=" + totalCount + ", from=" + from + ", count=" + count
+ + ", tooManyResults=" + tooManyResults + ", collapsed=" + collapsed + ", repoDetailsList="
+ + repoDetails + ", artifactList=" + data + "]";
+ }
+}
diff --git a/BRMSGateway/src/test/java/org/onap/policy/brms/api/nexus/NexusRestWrapperTest.java b/BRMSGateway/src/test/java/org/onap/policy/brms/api/nexus/NexusRestWrapperTest.java
new file mode 100644
index 000000000..489014e52
--- /dev/null
+++ b/BRMSGateway/src/test/java/org/onap/policy/brms/api/nexus/NexusRestWrapperTest.java
@@ -0,0 +1,52 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP Policy Engine
+ * ================================================================================
+ * Copyright (C) 2018 Ericsson Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.brms.api.nexus;
+
+import static org.junit.Assert.assertNotNull;
+
+import java.util.List;
+
+import org.junit.Test;
+import org.onap.policy.brms.api.nexus.NexusRestSearchParameters;
+import org.onap.policy.brms.api.nexus.NexusRestWrapper;
+import org.onap.policy.brms.api.nexus.NexusRestWrapperException;
+import org.onap.policy.brms.api.nexus.pojo.NexusArtifact;
+
+public class NexusRestWrapperTest {
+
+ @Test
+ public void test() throws NexusRestWrapperException {
+ NexusRestWrapper wrapper = new NexusRestWrapper("https://nexus.onap.org");
+
+ NexusRestSearchParameters searchParameters = new NexusRestSearchParameters();
+ searchParameters.useFilterSearch("org.onap.policy.engine", "BRMSGateway", null, null, null);
+
+ List<NexusArtifact> foundArtifactList = wrapper.findArtifact(searchParameters).getArtifactList();
+
+ assertNotNull(foundArtifactList);
+
+ for (NexusArtifact artifact: foundArtifactList) {
+ System.out.println(artifact.getUrlPath());
+ }
+
+ wrapper.close();
+ }
+}