Implement Sonar Nexus REST interface directly
[policy/engine.git] / BRMSGateway / src / main / java / org / onap / policy / brms / api / nexus / NexusRestSearchParameters.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.policy.brms.api.nexus;
22
23 import java.net.URI;
24
25 import javax.ws.rs.core.UriBuilder;
26
27 /**
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.
30  */
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";
34
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";
47
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
53     }
54
55     // The type of search to perform
56     private SearchType searchType = null;
57
58     // Search filters
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;
67
68     // Repository filter
69     private String repositoryId;
70
71     // Scope filters
72     private int from = -1;
73     private int count = -1;
74
75     /**
76      * Specify searching using a keyword.
77      * 
78      * @param keyword The keyword to search for
79      * @throws NexusRestWrapperException on invalid keywords
80      */
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");
84         }
85
86         searchType = SearchType.KEYWORD;
87         this.keyword = keyword;
88     }
89
90     /**
91      * Specify searching using a filter.
92      * 
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
99      */
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");
106         }
107
108         searchType = SearchType.FILTER;
109         this.groupId = groupId;
110         this.artifactId = artifactId;
111         this.version = version;
112         this.packagingType = packagingType;
113         this.classifier = classifier;
114     }
115
116     /**
117      * Specify searching using a class name.
118      * 
119      * @param className The class name to search for
120      * @throws NexusRestWrapperException on invalid className
121      */
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");
125         }
126
127         searchType = SearchType.CLASS_NAME;
128         this.className = className;
129     }
130
131     /**
132      * Specify searching using a checksum.
133      * 
134      * @param checksum The checksum to search for
135      * @throws NexusRestWrapperException on invalid checksum
136      */
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");
140         }
141
142         searchType = SearchType.CHECKSUM;
143         this.checksum = checksum;
144     }
145
146     /**
147      * Search on a specific repository.
148      * 
149      * @param repositoryId The repository to search
150      * @return this object to allow chaining of methods
151      * @throws NexusRestWrapperException on invalid repositoryId
152      */
153     public NexusRestSearchParameters setRepositoryId(String repositoryId) throws NexusRestWrapperException {
154         if (isNullOrBlank(repositoryId)) {
155             throw new NexusRestWrapperException("repositoryId must be specified for Nexus keyword searches");
156         }
157
158         this.repositoryId = repositoryId;
159         return this;
160     }
161
162     /**
163      * Return the search results from this result number.
164      * 
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
168      */
169     public NexusRestSearchParameters setFrom(int from) throws NexusRestWrapperException {
170         if (from < 0) {
171             throw new NexusRestWrapperException("from cannot be less than 0 for Nexus keyword searches");
172         }
173
174         this.from = from;
175         return this;
176     }
177
178     /**
179      * Return the specified number of search results.
180      * 
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
184      */
185     public NexusRestSearchParameters setCount(int count) throws NexusRestWrapperException {
186         if (count < 1) {
187             throw new NexusRestWrapperException("count cannot be less than 1 for Nexus keyword searches");
188         }
189
190         this.count = count;
191         return this;
192     }
193
194     /**
195      * Compose the URI for the search to the Nexus server.
196      * 
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
200      */
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");
204         }
205
206         // Use a URI builder to build up the search URI
207         UriBuilder uriBuilder = UriBuilder
208                 .fromPath(nexusServerUrl)
209                 .path(NEXUS_LUCENE_SEARCH_PATH);
210
211         switch (searchType) {
212             case KEYWORD:
213                 getKeywordSearchUri(uriBuilder);
214                 break;
215
216             case FILTER:
217                 getFitlerSearchUri(uriBuilder);
218                 break;
219
220             case CLASS_NAME:
221                 getClassNameSearchUri(uriBuilder);
222                 break;
223
224             case CHECKSUM:
225                 getChecksumSearchUri(uriBuilder);
226                 break;
227
228             default:
229                 throw new NexusRestWrapperException("search parameters have not been specified for the NExus search");
230         }
231
232         // Add the repository ID query parameter is required
233         if (null != repositoryId) {
234             uriBuilder.queryParam(REPOSITORY_ID_QUERY_PARAM, repositoryId);
235         }
236
237         // Add the from and count values if required
238         if (from >= 0) {
239             uriBuilder.queryParam(FROM_QUERY_PARAM, from);
240         }
241         if (count >= 0) {
242             uriBuilder.queryParam(COUNT_QUERY_PARAM, count);
243         }
244
245         return uriBuilder.build();
246     }
247
248     /**
249      * Compose the query parameters for a keyword search.
250      * @param uriBuilder The builder to add query parameters to
251      */
252     private void getKeywordSearchUri(UriBuilder uriBuilder) {
253         uriBuilder.queryParam(KEYWORD_QUERY_PARAM, keyword);
254     }
255
256     /**
257      * Compose the query parameters for a filter search.
258      * @param uriBuilder The builder to add query parameters to
259      */
260     private void getFitlerSearchUri(UriBuilder uriBuilder) {
261         if (null != groupId) {
262             uriBuilder.queryParam(GROUP_ID_QUERY_PARAM, groupId);
263         }
264         if (null != artifactId) {
265             uriBuilder.queryParam(ARTIFACT_ID_QUERY_PARAM, artifactId);
266         }
267         if (null != version) {
268             uriBuilder.queryParam(VERSION_QUERY_PARAM, version);
269         }
270         if (null != packagingType) {
271             uriBuilder.queryParam(PACKAGING_TYPE_QUERY_PARAM, packagingType);
272         }
273         if (null != classifier) {
274             uriBuilder.queryParam(CLASSIFIER_QUERY_PARAM, classifier);
275         }
276     }
277
278     /**
279      * Compose the query parameters for a class name search.
280      * @param uriBuilder The builder to add query parameters to
281      */
282     private void getClassNameSearchUri(UriBuilder uriBuilder) {
283         uriBuilder.queryParam(CLASS_NAME_QUERY_PARAM, className);
284     }
285
286     /**
287      * Compose the query parameters for a checksum search.
288      * @param uriBuilder The builder to add query parameters to
289      */
290     private void getChecksumSearchUri(UriBuilder uriBuilder) {
291         uriBuilder.queryParam(CHECKSUM_QUERY_PARAM, checksum);
292     }
293
294     public SearchType getSearchType() {
295         return searchType;
296     }
297
298     public String getKeyword() {
299         return keyword;
300     }
301
302     public String getGroupId() {
303         return groupId;
304     }
305
306     public String getArtifactId() {
307         return artifactId;
308     }
309
310     public String getVersion() {
311         return version;
312     }
313
314     public String getPackagingType() {
315         return packagingType;
316     }
317
318     public String getClassifier() {
319         return classifier;
320     }
321
322     public String getClassName() {
323         return className;
324     }
325
326     public String getChecksum() {
327         return checksum;
328     }
329
330     public String getRepositoryId() {
331         return repositoryId;
332     }
333
334     public int getFrom() {
335         return from;
336     }
337
338     public int getCount() {
339         return count;
340     }
341
342     /**
343      * Check if a string is null or all white space.
344      */
345     private boolean isNullOrBlank(final String parameter) {
346         return null == parameter || parameter.trim().isEmpty();
347     }
348
349     @Override
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 + "]";
355     }
356 }