Search in sources :

Example 1 with Namespace

use of org.eclipse.openvsx.entities.Namespace in project openvsx by eclipse.

the class LocalRegistryService method createNamespace.

@Transactional(rollbackOn = ErrorResultException.class)
public ResultJson createNamespace(NamespaceJson json, String tokenValue) {
    var namespaceIssue = validator.validateNamespace(json.name);
    if (namespaceIssue.isPresent()) {
        throw new ErrorResultException(namespaceIssue.get().toString());
    }
    var token = users.useAccessToken(tokenValue);
    if (token == null) {
        throw new ErrorResultException("Invalid access token.");
    }
    eclipse.checkPublisherAgreement(token.getUser());
    var namespace = repositories.findNamespace(json.name);
    if (namespace != null) {
        throw new ErrorResultException("Namespace already exists: " + namespace.getName());
    }
    // Create the requested namespace
    namespace = new Namespace();
    namespace.setName(json.name);
    entityManager.persist(namespace);
    // Assign the requesting user as contributor
    var membership = new NamespaceMembership();
    membership.setNamespace(namespace);
    membership.setUser(token.getUser());
    membership.setRole(NamespaceMembership.ROLE_CONTRIBUTOR);
    entityManager.persist(membership);
    return ResultJson.success("Created namespace " + namespace.getName());
}
Also used : ErrorResultException(org.eclipse.openvsx.util.ErrorResultException) NamespaceMembership(org.eclipse.openvsx.entities.NamespaceMembership) Namespace(org.eclipse.openvsx.entities.Namespace) Transactional(javax.transaction.Transactional)

Example 2 with Namespace

use of org.eclipse.openvsx.entities.Namespace in project openvsx by eclipse.

the class LocalRegistryService method query.

@Override
public QueryResultJson query(QueryParamJson param) {
    if (!Strings.isNullOrEmpty(param.extensionId)) {
        var split = param.extensionId.split("\\.");
        if (split.length != 2 || split[0].isEmpty() || split[1].isEmpty())
            throw new ErrorResultException("The 'extensionId' parameter must have the format 'namespace.extension'.");
        if (!Strings.isNullOrEmpty(param.namespaceName) && !param.namespaceName.equals(split[0]))
            throw new ErrorResultException("Conflicting parameters 'extensionId' and 'namespaceName'");
        if (!Strings.isNullOrEmpty(param.extensionName) && !param.extensionName.equals(split[1]))
            throw new ErrorResultException("Conflicting parameters 'extensionId' and 'extensionName'");
        param.namespaceName = split[0];
        param.extensionName = split[1];
    }
    List<ExtensionVersionDTO> extensionVersions = new ArrayList<>();
    // Add extension by UUID (public_id)
    if (!Strings.isNullOrEmpty(param.extensionUuid)) {
        extensionVersions.addAll(repositories.findActiveExtensionVersionDTOsByExtensionPublicId(param.extensionUuid));
    }
    // Add extensions by namespace UUID (public_id)
    if (!Strings.isNullOrEmpty(param.namespaceUuid)) {
        extensionVersions.addAll(repositories.findActiveExtensionVersionDTOsByNamespacePublicId(param.namespaceUuid));
    }
    // Add a specific version of an extension
    if (!Strings.isNullOrEmpty(param.namespaceName) && !Strings.isNullOrEmpty(param.extensionName) && !Strings.isNullOrEmpty(param.extensionVersion) && !param.includeAllVersions) {
        var extensionVersion = repositories.findActiveExtensionVersionDTOByVersion(param.extensionVersion, param.extensionName, param.namespaceName);
        if (extensionVersion != null) {
            extensionVersions.add(extensionVersion);
        }
    // Add extension by namespace and name
    } else if (!Strings.isNullOrEmpty(param.namespaceName) && !Strings.isNullOrEmpty(param.extensionName)) {
        extensionVersions.addAll(repositories.findActiveExtensionVersionDTOsByExtensionName(param.extensionName, param.namespaceName));
    // Add extensions by namespace
    } else if (!Strings.isNullOrEmpty(param.namespaceName)) {
        extensionVersions.addAll(repositories.findActiveExtensionVersionDTOsByNamespaceName(param.namespaceName));
    // Add extensions by name
    } else if (!Strings.isNullOrEmpty(param.extensionName)) {
        extensionVersions.addAll(repositories.findActiveExtensionVersionDTOsByExtensionName(param.extensionName));
    }
    extensionVersions = extensionVersions.stream().collect(Collectors.groupingBy(ExtensionVersionDTO::getId)).values().stream().map(l -> l.get(0)).filter(ev -> addToResult(ev, param)).collect(Collectors.toList());
    if (Strings.isNullOrEmpty(param.extensionVersion) && !param.includeAllVersions) {
        var latestIds = extensionVersions.stream().map(ExtensionVersionDTO::getExtension).map(ExtensionDTO::getLatestId).filter(Objects::nonNull).collect(Collectors.toList());
        extensionVersions = extensionVersions.stream().filter(ev -> latestIds.contains(ev.getId())).collect(Collectors.toList());
    }
    var extensionIds = extensionVersions.stream().map(ExtensionVersionDTO::getExtensionId).collect(Collectors.toSet());
    var reviewCounts = getReviewCounts(extensionIds);
    var versionStrings = getVersionStrings(extensionIds);
    var previewsByExtensionId = getPreviews(extensionIds);
    var fileResourcesByExtensionVersionId = getFileResources(extensionVersions);
    var membershipsByNamespaceId = getMemberships(extensionVersions);
    var result = new QueryResultJson();
    result.extensions = extensionVersions.stream().sorted(getExtensionVersionComparator()).map(ev -> {
        var reviewCount = reviewCounts.getOrDefault(ev.getExtensionId(), 0);
        var preview = previewsByExtensionId.get(ev.getExtensionId());
        var versions = versionStrings.get(ev.getExtensionId());
        var fileResources = fileResourcesByExtensionVersionId.getOrDefault(ev.getId(), Collections.emptyList());
        return toExtensionVersionJson(ev, reviewCount, preview, versions, fileResources, membershipsByNamespaceId);
    }).collect(Collectors.toList());
    return result;
}
Also used : ErrorResultException(org.eclipse.openvsx.util.ErrorResultException) Autowired(org.springframework.beans.factory.annotation.Autowired) QueryParamJson(org.eclipse.openvsx.json.QueryParamJson) CacheControl(org.springframework.http.CacheControl) ExtensionSearch(org.eclipse.openvsx.search.ExtensionSearch) ISearchService(org.eclipse.openvsx.search.ISearchService) FileResource(org.eclipse.openvsx.entities.FileResource) Namespace(org.eclipse.openvsx.entities.Namespace) ReviewJson(org.eclipse.openvsx.json.ReviewJson) ExtensionJson(org.eclipse.openvsx.json.ExtensionJson) SearchUtilService(org.eclipse.openvsx.search.SearchUtilService) ResponseStatusException(org.springframework.web.server.ResponseStatusException) Transactional(javax.transaction.Transactional) PageRequest(org.springframework.data.domain.PageRequest) ExtensionReview(org.eclipse.openvsx.entities.ExtensionReview) Collectors(java.util.stream.Collectors) Stream(java.util.stream.Stream) Maps(org.apache.jena.ext.com.google.common.collect.Maps) org.eclipse.openvsx.dto(org.eclipse.openvsx.dto) CollectionUtil(org.eclipse.openvsx.util.CollectionUtil) QueryResultJson(org.eclipse.openvsx.json.QueryResultJson) NamespaceMembership(org.eclipse.openvsx.entities.NamespaceMembership) java.util(java.util) Iterables(com.google.common.collect.Iterables) ExtensionVersion(org.eclipse.openvsx.entities.ExtensionVersion) ReviewListJson(org.eclipse.openvsx.json.ReviewListJson) ErrorResultException(org.eclipse.openvsx.util.ErrorResultException) UrlUtil(org.eclipse.openvsx.util.UrlUtil) TimeUtil(org.eclipse.openvsx.util.TimeUtil) Strings(com.google.common.base.Strings) Lists(com.google.common.collect.Lists) NamespaceJson(org.eclipse.openvsx.json.NamespaceJson) NotFoundException(org.eclipse.openvsx.util.NotFoundException) SearchHit(org.springframework.data.elasticsearch.core.SearchHit) UrlUtil.createApiUrl(org.eclipse.openvsx.util.UrlUtil.createApiUrl) Extension(org.eclipse.openvsx.entities.Extension) SearchHits(org.springframework.data.elasticsearch.core.SearchHits) SemanticVersion(org.eclipse.openvsx.util.SemanticVersion) EntityManager(javax.persistence.EntityManager) ResultJson(org.eclipse.openvsx.json.ResultJson) TimeUnit(java.util.concurrent.TimeUnit) SearchResultJson(org.eclipse.openvsx.json.SearchResultJson) RepositoryService(org.eclipse.openvsx.repositories.RepositoryService) HttpStatus(org.springframework.http.HttpStatus) Component(org.springframework.stereotype.Component) SearchEntryJson(org.eclipse.openvsx.json.SearchEntryJson) ResponseEntity(org.springframework.http.ResponseEntity) EclipseService(org.eclipse.openvsx.eclipse.EclipseService) InputStream(java.io.InputStream) StorageUtilService(org.eclipse.openvsx.storage.StorageUtilService) QueryResultJson(org.eclipse.openvsx.json.QueryResultJson)

Example 3 with Namespace

use of org.eclipse.openvsx.entities.Namespace in project openvsx by eclipse.

the class RegistryAPITest method mockForPublish.

private void mockForPublish(String mode) {
    var token = mockAccessToken();
    if (mode.equals("invalid")) {
        token.setActive(false);
    }
    var namespace = new Namespace();
    namespace.setName("foo");
    Mockito.when(repositories.findNamespace("foo")).thenReturn(namespace);
    if (mode.equals("existing")) {
        var extension = new Extension();
        extension.setName("bar");
        var extVersion = new ExtensionVersion();
        extVersion.setExtension(extension);
        extVersion.setVersion("1");
        extVersion.setActive(true);
        Mockito.when(repositories.findExtension("bar", namespace)).thenReturn(extension);
        Mockito.when(repositories.findVersion("1", extension)).thenReturn(extVersion);
    }
    Mockito.when(repositories.countActiveReviews(any(Extension.class))).thenReturn(0l);
    Mockito.when(repositories.findVersions(any(Extension.class))).thenReturn(Streamable.empty());
    Mockito.when(repositories.findActiveVersions(any(Extension.class))).thenReturn(Streamable.empty());
    Mockito.when(repositories.getVersionStrings(any(Extension.class))).thenReturn(Streamable.empty());
    Mockito.when(repositories.getActiveVersionStrings(any(Extension.class))).thenReturn(Streamable.empty());
    Mockito.when(repositories.findFilesByType(any(ExtensionVersion.class), anyCollection())).thenReturn(Streamable.empty());
    if (mode.equals("owner")) {
        var ownerMem = new NamespaceMembership();
        ownerMem.setUser(token.getUser());
        ownerMem.setNamespace(namespace);
        ownerMem.setRole(NamespaceMembership.ROLE_OWNER);
        Mockito.when(repositories.findMemberships(namespace, NamespaceMembership.ROLE_OWNER)).thenReturn(Streamable.of(ownerMem));
        Mockito.when(repositories.countMemberships(namespace, NamespaceMembership.ROLE_OWNER)).thenReturn(1l);
        Mockito.when(repositories.findMembership(token.getUser(), namespace)).thenReturn(ownerMem);
        Mockito.when(repositories.countMemberships(token.getUser(), namespace)).thenReturn(1l);
    } else if (mode.equals("contributor") || mode.equals("sole-contributor") || mode.equals("existing")) {
        var contribMem = new NamespaceMembership();
        contribMem.setUser(token.getUser());
        contribMem.setNamespace(namespace);
        contribMem.setRole(NamespaceMembership.ROLE_CONTRIBUTOR);
        Mockito.when(repositories.findMembership(token.getUser(), namespace)).thenReturn(contribMem);
        Mockito.when(repositories.countMemberships(token.getUser(), namespace)).thenReturn(1l);
        if (mode.equals("contributor")) {
            var otherUser = new UserData();
            otherUser.setLoginName("other_user");
            var ownerMem = new NamespaceMembership();
            ownerMem.setUser(otherUser);
            ownerMem.setNamespace(namespace);
            ownerMem.setRole(NamespaceMembership.ROLE_OWNER);
            Mockito.when(repositories.findMemberships(namespace, NamespaceMembership.ROLE_OWNER)).thenReturn(Streamable.of(ownerMem));
            Mockito.when(repositories.countMemberships(namespace, NamespaceMembership.ROLE_OWNER)).thenReturn(1l);
        } else {
            Mockito.when(repositories.findMemberships(namespace, NamespaceMembership.ROLE_OWNER)).thenReturn(Streamable.empty());
            Mockito.when(repositories.countMemberships(namespace, NamespaceMembership.ROLE_OWNER)).thenReturn(0l);
        }
    } else if (mode.equals("privileged") || mode.equals("unrelated")) {
        var otherUser = new UserData();
        otherUser.setLoginName("other_user");
        var ownerMem = new NamespaceMembership();
        ownerMem.setUser(otherUser);
        ownerMem.setNamespace(namespace);
        ownerMem.setRole(NamespaceMembership.ROLE_OWNER);
        Mockito.when(repositories.findMemberships(namespace, NamespaceMembership.ROLE_OWNER)).thenReturn(Streamable.of(ownerMem));
        Mockito.when(repositories.countMemberships(namespace, NamespaceMembership.ROLE_OWNER)).thenReturn(1l);
        if (mode.equals("privileged")) {
            token.getUser().setRole(UserData.ROLE_PRIVILEGED);
        }
    } else {
        Mockito.when(repositories.findMemberships(namespace, NamespaceMembership.ROLE_OWNER)).thenReturn(Streamable.empty());
        Mockito.when(repositories.countMemberships(namespace, NamespaceMembership.ROLE_OWNER)).thenReturn(0l);
    }
}
Also used : Extension(org.eclipse.openvsx.entities.Extension) UserData(org.eclipse.openvsx.entities.UserData) NamespaceMembership(org.eclipse.openvsx.entities.NamespaceMembership) Namespace(org.eclipse.openvsx.entities.Namespace) ExtensionVersion(org.eclipse.openvsx.entities.ExtensionVersion)

Aggregations

Namespace (org.eclipse.openvsx.entities.Namespace)3 NamespaceMembership (org.eclipse.openvsx.entities.NamespaceMembership)3 Transactional (javax.transaction.Transactional)2 Extension (org.eclipse.openvsx.entities.Extension)2 ExtensionVersion (org.eclipse.openvsx.entities.ExtensionVersion)2 ErrorResultException (org.eclipse.openvsx.util.ErrorResultException)2 Strings (com.google.common.base.Strings)1 Iterables (com.google.common.collect.Iterables)1 Lists (com.google.common.collect.Lists)1 InputStream (java.io.InputStream)1 java.util (java.util)1 TimeUnit (java.util.concurrent.TimeUnit)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 EntityManager (javax.persistence.EntityManager)1 Maps (org.apache.jena.ext.com.google.common.collect.Maps)1 org.eclipse.openvsx.dto (org.eclipse.openvsx.dto)1 EclipseService (org.eclipse.openvsx.eclipse.EclipseService)1 ExtensionReview (org.eclipse.openvsx.entities.ExtensionReview)1 FileResource (org.eclipse.openvsx.entities.FileResource)1