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 /** The type of searches that can be performed. */
49 public enum SearchType {
50 KEYWORD, /** Search using a keyword. */
51 FILTER, /** Search using a group ID, artifact ID, version, packaging type, and/or classifier filter. */
52 CLASS_NAME, /** Search for a class name. */
53 CHECKSUM /** Search for artifacts matching a certain checksum. */
56 // The type of search to perform
57 private SearchType searchType = null;
60 private String keyword;
61 private String groupId;
62 private String artifactId;
63 private String version;
64 private String packagingType;
65 private String classifier;
66 private String className;
67 private String checksum;
70 private String repositoryId;
73 private int from = -1;
74 private int count = -1;
77 * Specify searching using a keyword.
79 * @param keyword The keyword to search for
80 * @return this object to allow chaining of methods
81 * @throws NexusRestWrapperException on invalid keywords
83 public NexusRestSearchParameters useKeywordSearch(final String keyword) throws NexusRestWrapperException {
84 clearSearchParameters();
86 if (isNullOrBlank(keyword)) {
87 throw new NexusRestWrapperException("keyword must be specified for Nexus keyword searches");
90 searchType = SearchType.KEYWORD;
91 this.keyword = keyword;
96 * Specify searching using a filter.
98 * @param groupId The group ID to filter on
99 * @param artifactId The artifact ID to filter on
100 * @param version The version to filter on
101 * @param packagingType The packaging type to filter on
102 * @param classifier The classifier to filter on
103 * @return this object to allow chaining of methods
104 * @throws NexusRestWrapperException on invalid filters
106 public NexusRestSearchParameters useFilterSearch(final String groupId, final String artifactId,
107 final String version, final String packagingType, final String classifier)
108 throws NexusRestWrapperException {
109 clearSearchParameters();
111 if (isNullOrBlank(groupId) && isNullOrBlank(artifactId) && isNullOrBlank(version)
112 && isNullOrBlank(packagingType) && isNullOrBlank(classifier)) {
113 throw new NexusRestWrapperException(
114 "at least one filter parameter must be specified for Nexus filter searches");
117 searchType = SearchType.FILTER;
118 this.groupId = groupId;
119 this.artifactId = artifactId;
120 this.version = version;
121 this.packagingType = packagingType;
122 this.classifier = classifier;
127 * Specify searching using a class name.
129 * @param className The class name to search for
130 * @return this object to allow chaining of methods
131 * @throws NexusRestWrapperException on invalid className
133 public NexusRestSearchParameters useClassNameSearch(final String className) throws NexusRestWrapperException {
134 clearSearchParameters();
136 if (isNullOrBlank(className)) {
137 throw new NexusRestWrapperException("className must be specified for Nexus class name searches");
140 searchType = SearchType.CLASS_NAME;
141 this.className = className;
146 * Specify searching using a checksum.
148 * @param checksum The checksum to search for
149 * @return this object to allow chaining of methods
150 * @throws NexusRestWrapperException on invalid checksum
152 public NexusRestSearchParameters useChecksumSearch(final String checksum) throws NexusRestWrapperException {
153 clearSearchParameters();
155 if (isNullOrBlank(checksum)) {
156 throw new NexusRestWrapperException("checksum must be specified for Nexus checksum searches");
159 searchType = SearchType.CHECKSUM;
160 this.checksum = checksum;
165 * Search on a specific repository.
167 * @param repositoryId The repository to search
168 * @return this object to allow chaining of methods
169 * @throws NexusRestWrapperException on invalid repositoryId
171 public NexusRestSearchParameters setRepositoryId(String repositoryId) throws NexusRestWrapperException {
172 if (isNullOrBlank(repositoryId)) {
173 throw new NexusRestWrapperException("a repositoryId must be specified");
176 this.repositoryId = repositoryId;
181 * Return the search results from this result number.
183 * @param from The number of the first result to return
184 * @return this object to allow chaining of methods
185 * @throws NexusRestWrapperException on invalid from value
187 public NexusRestSearchParameters setFrom(int from) throws NexusRestWrapperException {
189 throw new NexusRestWrapperException("from cannot be less than 0 when from is specified");
197 * Return the specified number of search results.
199 * @param count The number of results to return
200 * @return this object to allow chaining of methods
201 * @throws NexusRestWrapperException on invalid count value
203 public NexusRestSearchParameters setCount(int count) throws NexusRestWrapperException {
205 throw new NexusRestWrapperException("count cannot be less than 1 when count is specified");
213 * Compose the URI for the search to the Nexus server.
215 * @param nexusServerUrl the URL of the server on which to search
216 * @return the search URI
217 * @throws NexusRestWrapperException on search URL composition exceptions
219 public URI getSearchUri(final String nexusServerUrl) throws NexusRestWrapperException {
220 if (isNullOrBlank(nexusServerUrl)) {
221 throw new NexusRestWrapperException("nexusServerUrl must be specified for the search URI");
224 if (searchType == null) {
225 throw new NexusRestWrapperException("search parameters have not been set");
228 // Use a URI builder to build up the search URI
229 UriBuilder uriBuilder = UriBuilder
230 .fromPath(nexusServerUrl)
231 .path(NEXUS_LUCENE_SEARCH_PATH);
233 switch (searchType) {
235 getKeywordSearchUri(uriBuilder);
239 getFitlerSearchUri(uriBuilder);
243 getClassNameSearchUri(uriBuilder);
247 getChecksumSearchUri(uriBuilder);
251 throw new NexusRestWrapperException("search parameters have not been specified for the Nexus search");
254 // Add the repository ID query parameter is required
255 if (null != repositoryId) {
256 uriBuilder.queryParam(REPOSITORY_ID_QUERY_PARAM, repositoryId);
259 // Add the from and count values if required
261 uriBuilder.queryParam(FROM_QUERY_PARAM, from);
264 uriBuilder.queryParam(COUNT_QUERY_PARAM, count);
267 return uriBuilder.build();
271 * Compose the query parameters for a keyword search.
272 * @param uriBuilder The builder to add query parameters to
274 private void getKeywordSearchUri(UriBuilder uriBuilder) {
275 uriBuilder.queryParam(KEYWORD_QUERY_PARAM, keyword);
279 * Compose the query parameters for a filter search.
280 * @param uriBuilder The builder to add query parameters to
282 private void getFitlerSearchUri(UriBuilder uriBuilder) {
283 if (!isNullOrBlank(groupId)) {
284 uriBuilder.queryParam(GROUP_ID_QUERY_PARAM, groupId);
286 if (!isNullOrBlank(artifactId)) {
287 uriBuilder.queryParam(ARTIFACT_ID_QUERY_PARAM, artifactId);
289 if (!isNullOrBlank(version)) {
290 uriBuilder.queryParam(VERSION_QUERY_PARAM, version);
292 if (!isNullOrBlank(packagingType)) {
293 uriBuilder.queryParam(PACKAGING_TYPE_QUERY_PARAM, packagingType);
295 if (!isNullOrBlank(classifier)) {
296 uriBuilder.queryParam(CLASSIFIER_QUERY_PARAM, classifier);
301 * Compose the query parameters for a class name search.
302 * @param uriBuilder The builder to add query parameters to
304 private void getClassNameSearchUri(UriBuilder uriBuilder) {
305 uriBuilder.queryParam(CLASS_NAME_QUERY_PARAM, className);
309 * Compose the query parameters for a checksum search.
310 * @param uriBuilder The builder to add query parameters to
312 private void getChecksumSearchUri(UriBuilder uriBuilder) {
313 uriBuilder.queryParam(CHECKSUM_QUERY_PARAM, checksum);
316 public SearchType getSearchType() {
320 public String getKeyword() {
324 public String getGroupId() {
328 public String getArtifactId() {
332 public String getVersion() {
336 public String getPackagingType() {
337 return packagingType;
340 public String getClassifier() {
344 public String getClassName() {
348 public String getChecksum() {
352 public String getRepositoryId() {
356 public int getFrom() {
360 public int getCount() {
365 * Check if a string is null or all white space.
367 private boolean isNullOrBlank(final String parameter) {
368 return null == parameter || parameter.trim().isEmpty();
372 * Clear all search parameters.
375 private void clearSearchParameters() {
382 packagingType = null;
395 public String toString() {
396 return "NexusRestSearchParameters [searchType=" + searchType + ", keyword=" + keyword + ", groupId=" + groupId
397 + ", artifactId=" + artifactId + ", version=" + version + ", packagingType=" + packagingType
398 + ", classifier=" + classifier + ", className=" + className + ", checksum=" + checksum
399 + ", repositoryId=" + repositoryId + ", from=" + from + ", count=" + count + "]";