2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Ericsson Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.brms.api.nexus;
25 import javax.ws.rs.core.UriBuilder;
28 * The Class NexusRestSearchParameters is used to specify the parameters of a search on Nexus.
29 * The parameters are used to return the search part of the URL for the Maven search.
31 public class NexusRestSearchParameters {
32 // The REST end point for Nexus Lucene searches
33 private static final String NEXUS_LUCENE_SEARCH_PATH = "service/local/lucene/search";
35 // REST search query parameter names
36 private static final String KEYWORD_QUERY_PARAM = "q";
37 private static final String GROUP_ID_QUERY_PARAM = "g";
38 private static final String ARTIFACT_ID_QUERY_PARAM = "a";
39 private static final String VERSION_QUERY_PARAM = "v";
40 private static final String PACKAGING_TYPE_QUERY_PARAM = "p";
41 private static final String CLASSIFIER_QUERY_PARAM = "c";
42 private static final String CLASS_NAME_QUERY_PARAM = "cn";
43 private static final String CHECKSUM_QUERY_PARAM = "sha1";
44 private static final String FROM_QUERY_PARAM = "from";
45 private static final String COUNT_QUERY_PARAM = "count";
46 private static final String REPOSITORY_ID_QUERY_PARAM = "repositoryId";
48 private enum SearchType {
49 KEYWORD, // Search using a keyword
50 FILTER, // Search using a group ID, artifact ID, version, packaging type, and/or classifier filter
51 CLASS_NAME, // Search for a class name
52 CHECKSUM // Search for artifacts matching a certain checksum
55 // The type of search to perform
56 private SearchType searchType = null;
59 private String keyword;
60 private String groupId;
61 private String artifactId;
62 private String version;
63 private String packagingType;
64 private String classifier;
65 private String className;
66 private String checksum;
69 private String repositoryId;
72 private int from = -1;
73 private int count = -1;
76 * Specify searching using a keyword.
78 * @param keyword The keyword to search for
79 * @throws NexusRestWrapperException on invalid keywords
81 public void useKeywordSearch(final String keyword) throws NexusRestWrapperException {
82 if (isNullOrBlank(keyword)) {
83 throw new NexusRestWrapperException("keyword must be specified for Nexus keyword searches");
86 searchType = SearchType.KEYWORD;
87 this.keyword = keyword;
91 * Specify searching using a filter.
93 * @param groupId The group ID to filter on
94 * @param artifactId The artifact ID to filter on
95 * @param version The version to filter on
96 * @param packagingType The packaging type to filter on
97 * @param classifier The classifier to filter on
98 * @throws NexusRestWrapperException on invalid filters
100 public void useFilterSearch(final String groupId, final String artifactId, final String version,
101 final String packagingType, final String classifier) throws NexusRestWrapperException {
102 if (isNullOrBlank(groupId) && isNullOrBlank(artifactId) && isNullOrBlank(version)
103 && isNullOrBlank(packagingType) && isNullOrBlank(classifier)) {
104 throw new NexusRestWrapperException(
105 "at least one filter parameter must be specified for Nexus keyword searches");
108 searchType = SearchType.FILTER;
109 this.groupId = groupId;
110 this.artifactId = artifactId;
111 this.version = version;
112 this.packagingType = packagingType;
113 this.classifier = classifier;
117 * Specify searching using a class name.
119 * @param className The class name to search for
120 * @throws NexusRestWrapperException on invalid className
122 public void useClassNameSearch(final String className) throws NexusRestWrapperException {
123 if (isNullOrBlank(className)) {
124 throw new NexusRestWrapperException("className must be specified for Nexus keyword searches");
127 searchType = SearchType.CLASS_NAME;
128 this.className = className;
132 * Specify searching using a checksum.
134 * @param checksum The checksum to search for
135 * @throws NexusRestWrapperException on invalid checksum
137 public void useChecksumSearch(final String checksum) throws NexusRestWrapperException {
138 if (isNullOrBlank(checksum)) {
139 throw new NexusRestWrapperException("checksum must be specified for Nexus keyword searches");
142 searchType = SearchType.CHECKSUM;
143 this.checksum = checksum;
147 * Search on a specific repository.
149 * @param repositoryId The repository to search
150 * @return this object to allow chaining of methods
151 * @throws NexusRestWrapperException on invalid repositoryId
153 public NexusRestSearchParameters setRepositoryId(String repositoryId) throws NexusRestWrapperException {
154 if (isNullOrBlank(repositoryId)) {
155 throw new NexusRestWrapperException("repositoryId must be specified for Nexus keyword searches");
158 this.repositoryId = repositoryId;
163 * Return the search results from this result number.
165 * @param from The number of the first result to return
166 * @return this object to allow chaining of methods
167 * @throws NexusRestWrapperException on invalid from value
169 public NexusRestSearchParameters setFrom(int from) throws NexusRestWrapperException {
171 throw new NexusRestWrapperException("from cannot be less than 0 for Nexus keyword searches");
179 * Return the specified number of search results.
181 * @param count The number of results to return
182 * @return this object to allow chaining of methods
183 * @throws NexusRestWrapperException on invalid count value
185 public NexusRestSearchParameters setCount(int count) throws NexusRestWrapperException {
187 throw new NexusRestWrapperException("count cannot be less than 1 for Nexus keyword searches");
195 * Compose the URI for the search to the Nexus server.
197 * @param nexusServerUrl the URL of the server on which to search
198 * @return the search URI
199 * @throws NexusRestWrapperException on search URL composition exceptions
201 public URI getSearchUri(final String nexusServerUrl) throws NexusRestWrapperException {
202 if (isNullOrBlank(nexusServerUrl)) {
203 throw new NexusRestWrapperException("nexusServerUrl must be specified for Nexus keyword searches");
206 // Use a URI builder to build up the search URI
207 UriBuilder uriBuilder = UriBuilder
208 .fromPath(nexusServerUrl)
209 .path(NEXUS_LUCENE_SEARCH_PATH);
211 switch (searchType) {
213 getKeywordSearchUri(uriBuilder);
217 getFitlerSearchUri(uriBuilder);
221 getClassNameSearchUri(uriBuilder);
225 getChecksumSearchUri(uriBuilder);
229 throw new NexusRestWrapperException("search parameters have not been specified for the NExus search");
232 // Add the repository ID query parameter is required
233 if (null != repositoryId) {
234 uriBuilder.queryParam(REPOSITORY_ID_QUERY_PARAM, repositoryId);
237 // Add the from and count values if required
239 uriBuilder.queryParam(FROM_QUERY_PARAM, from);
242 uriBuilder.queryParam(COUNT_QUERY_PARAM, count);
245 return uriBuilder.build();
249 * Compose the query parameters for a keyword search.
250 * @param uriBuilder The builder to add query parameters to
252 private void getKeywordSearchUri(UriBuilder uriBuilder) {
253 uriBuilder.queryParam(KEYWORD_QUERY_PARAM, keyword);
257 * Compose the query parameters for a filter search.
258 * @param uriBuilder The builder to add query parameters to
260 private void getFitlerSearchUri(UriBuilder uriBuilder) {
261 if (null != groupId) {
262 uriBuilder.queryParam(GROUP_ID_QUERY_PARAM, groupId);
264 if (null != artifactId) {
265 uriBuilder.queryParam(ARTIFACT_ID_QUERY_PARAM, artifactId);
267 if (null != version) {
268 uriBuilder.queryParam(VERSION_QUERY_PARAM, version);
270 if (null != packagingType) {
271 uriBuilder.queryParam(PACKAGING_TYPE_QUERY_PARAM, packagingType);
273 if (null != classifier) {
274 uriBuilder.queryParam(CLASSIFIER_QUERY_PARAM, classifier);
279 * Compose the query parameters for a class name search.
280 * @param uriBuilder The builder to add query parameters to
282 private void getClassNameSearchUri(UriBuilder uriBuilder) {
283 uriBuilder.queryParam(CLASS_NAME_QUERY_PARAM, className);
287 * Compose the query parameters for a checksum search.
288 * @param uriBuilder The builder to add query parameters to
290 private void getChecksumSearchUri(UriBuilder uriBuilder) {
291 uriBuilder.queryParam(CHECKSUM_QUERY_PARAM, checksum);
294 public SearchType getSearchType() {
298 public String getKeyword() {
302 public String getGroupId() {
306 public String getArtifactId() {
310 public String getVersion() {
314 public String getPackagingType() {
315 return packagingType;
318 public String getClassifier() {
322 public String getClassName() {
326 public String getChecksum() {
330 public String getRepositoryId() {
334 public int getFrom() {
338 public int getCount() {
343 * Check if a string is null or all white space.
345 private boolean isNullOrBlank(final String parameter) {
346 return null == parameter || parameter.trim().isEmpty();
350 public String toString() {
351 return "NexusRestSearchParameters [searchType=" + searchType + ", keyword=" + keyword + ", groupId=" + groupId
352 + ", artifactId=" + artifactId + ", version=" + version + ", packagingType=" + packagingType
353 + ", classifier=" + classifier + ", className=" + className + ", checksum=" + checksum
354 + ", repositoryId=" + repositoryId + ", from=" + from + ", count=" + count + "]";