use of org.xwiki.extension.version.InvalidVersionRangeException in project xwiki-platform by xwiki.
the class ExtensionVersionsRESTResource method getExtensionVersions.
@GET
public ExtensionVersions getExtensionVersions(@PathParam("extensionId") String extensionId, @QueryParam(Resources.QPARAM_LIST_START) @DefaultValue("0") int offset, @QueryParam(Resources.QPARAM_LIST_NUMBER) @DefaultValue("-1") int number, @QueryParam(Resources.QPARAM_VERSIONS_RANGES) String ranges) throws QueryException, InvalidVersionRangeException {
Query query = createExtensionsSummariesQuery(null, "extension.id = :extensionId", 0, -1, true);
query.bindValue("extensionId", extensionId);
ExtensionVersions extensions = this.extensionObjectFactory.createExtensionVersions();
getExtensionSummaries(extensions.getExtensionVersionSummaries(), query);
// Filter by ranges
if (StringUtils.isNotBlank(ranges)) {
VersionConstraint constraint = this.extensionFactory.getVersionConstraint(ranges);
if (constraint.getVersion() != null) {
throw new InvalidVersionRangeException("Invalid ranges syntax [" + ranges + "]");
}
for (Iterator<ExtensionVersionSummary> it = extensions.getExtensionVersionSummaries().iterator(); it.hasNext(); ) {
if (!constraint.containsVersion(new DefaultVersion(it.next().getVersion()))) {
it.remove();
}
}
}
// Order by version
final Map<String, Version> versionCache = new HashMap<>();
Collections.sort(extensions.getExtensionVersionSummaries(), new Comparator<ExtensionVersionSummary>() {
@Override
public int compare(ExtensionVersionSummary o1, ExtensionVersionSummary o2) {
return toVersion(o1.getVersion()).compareTo(toVersion(o2.getVersion()));
}
private Version toVersion(String versionString) {
Version version = versionCache.get(versionString);
if (version == null) {
version = extensionFactory.getVersion(versionString);
versionCache.put(versionString, version);
}
return version;
}
});
extensions.setTotalHits(extensions.getExtensionVersionSummaries().size());
extensions.setOffset(offset);
if (offset > 0 || (number > -1 && offset + number < extensions.getExtensionVersionSummaries().size())) {
if (offset >= extensions.getExtensionVersionSummaries().size() || number == 0) {
extensions.getExtensionVersionSummaries().clear();
} else {
List<ExtensionVersionSummary> limited = new ArrayList<ExtensionVersionSummary>(extensions.getExtensionVersionSummaries());
extensions.getExtensionVersionSummaries().clear();
extensions.withExtensionVersionSummaries(limited.subList(offset < 0 ? 0 : offset, number < 0 ? extensions.getExtensionVersionSummaries().size() : offset + number));
}
}
return extensions;
}
Aggregations