Skip to content

Commit

Permalink
For Nexus3 the Classifier selection was not working as expected. Now …
Browse files Browse the repository at this point in the history
…its possible to filter the results or retrieve the full list of classififers.
  • Loading branch information
Watermeyer, Stephan committed Jul 7, 2022
1 parent 30c7592 commit 0b45c2b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
public abstract class RESTfulParameterBuilder {

public static final String PACKAGING_ALL = "*";

public static final String CLASSIFIER_ALL = "*";

// Always order the results by semantic version
private static final String DEFAULT_SORTORDER = "version";
Expand Down Expand Up @@ -68,13 +70,28 @@ public MultivaluedMap<String, String> create(final String pRepositoryId, final S
requestParams.putSingle(getPackaging(), pPackaging);
}
if (pClassifier != null) {
boolean retrieveAllClassifiers = false;

// FIXME: There is of course a better way how to do it...
final List<String> query = new ArrayList<String>();
for (String current : pClassifier.getValid())
for (String current : pClassifier.getValid()) {
query.add(current);


if(RESTfulParameterBuilder.CLASSIFIER_ALL.equals(current)) {
retrieveAllClassifiers = true;
}
}

if (!query.isEmpty())
if (retrieveAllClassifiers || pClassifier.getInvalid().isEmpty() == false) {
// in this case the parameter "maven.classifier" must not be present in the REST call to retrieve all classifiers.
// if there is a "invalid" selection, we need to retrieve all elements to filter the "invalid" entries after retrieval.
} else if (!query.isEmpty()) {
requestParams.put(getClassifier(), query);
} else {
// In case the classifier has not been given, we send it to Nexus3 API to retrieve only the default artifacts like JARs.
requestParams.putSingle(getClassifier(), "");
}
}

if (!StringUtils.isEmpty(continuationToken)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public Set<String> callService(final String pRepositoryId, final String pGroupId
// So we get the token from the request which should be null.
token = parsedJsonResult.getContinuationToken();
} else {
Set<String> currentResult = parseResponse(parsedJsonResult);
Set<String> currentResult = parseAndFilterResponse(parsedJsonResult, pClassifier);
retVal.addAll(currentResult);

// control the loop and maybe query again
Expand All @@ -97,16 +97,32 @@ public Set<String> callService(final String pRepositoryId, final String pGroupId
* artifacts can be retrieved.
*
* @param pJsonResult the JSON response of the Nexus3 API.
* @param pClassifier
* @return a unique list of URLs that are matching the search criteria, sorted
* by the order of the Nexus3 service.
*/
Set<String> parseResponse(final Nexus3RestResponse pJsonResult) {
Set<String> parseAndFilterResponse(final Nexus3RestResponse pJsonResult, final ValidAndInvalidClassifier pClassifier) {
// Use a Map instead of a List to filter duplicated entries and also linked to
// keep the order of response
final Set<String> retVal = new LinkedHashSet<>();

for (Item current : pJsonResult.getItems()) {
retVal.add(current.getDownloadUrl());
boolean mustApplyInvalidFilter = pClassifier.getInvalid().isEmpty() == false;
boolean addItemToResult = true;

for (final Item current : pJsonResult.getItems()) {
if(mustApplyInvalidFilter) {
addItemToResult = true;
for(final String currentInvalidClassifier : pClassifier.getInvalid()) {
// Example: "https://xxx/repository/r/com/a/b/c/log-uploader/1.0.56/log-uploader-1.0.56-javadoc.jar",
if(current.getDownloadUrl().contains("-" + currentInvalidClassifier+ ".")) {
addItemToResult = false;
}
}
}

if(addItemToResult) {
retVal.add(current.getDownloadUrl());
}
}
return retVal;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div>
The Maven Artifact classifier, i.E. "sources". By default leave empty.
You can specifiy a list of valid or invalid classifier separated by Comma, i.E. "sources,!jar"
The Maven Artifact classifier, i.E. "sources". By default leave empty to get the default artifact.
You can specifiy a list of valid or invalid classifier separated by Comma, i.E. "sources,!jar" or a "*" to retrieve all classifiers.
</div>

0 comments on commit 0b45c2b

Please sign in to comment.