Search in sources :

Example 1 with InvalidVersionRangeException

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;
}
Also used : VersionConstraint(org.xwiki.extension.version.VersionConstraint) Query(org.xwiki.query.Query) HashMap(java.util.HashMap) DefaultVersion(org.xwiki.extension.version.internal.DefaultVersion) ArrayList(java.util.ArrayList) InvalidVersionRangeException(org.xwiki.extension.version.InvalidVersionRangeException) ExtensionVersionSummary(org.xwiki.extension.repository.xwiki.model.jaxb.ExtensionVersionSummary) Version(org.xwiki.extension.version.Version) DefaultVersion(org.xwiki.extension.version.internal.DefaultVersion) ExtensionVersions(org.xwiki.extension.repository.xwiki.model.jaxb.ExtensionVersions) GET(javax.ws.rs.GET)

Aggregations

ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 GET (javax.ws.rs.GET)1 ExtensionVersionSummary (org.xwiki.extension.repository.xwiki.model.jaxb.ExtensionVersionSummary)1 ExtensionVersions (org.xwiki.extension.repository.xwiki.model.jaxb.ExtensionVersions)1 InvalidVersionRangeException (org.xwiki.extension.version.InvalidVersionRangeException)1 Version (org.xwiki.extension.version.Version)1 VersionConstraint (org.xwiki.extension.version.VersionConstraint)1 DefaultVersion (org.xwiki.extension.version.internal.DefaultVersion)1 Query (org.xwiki.query.Query)1