Search in sources :

Example 11 with ErrorResultException

use of org.eclipse.openvsx.util.ErrorResultException in project openvsx by eclipse.

the class AdminService method getAdminStatisticsCsv.

@Transactional
public String getAdminStatisticsCsv(int year, int month) throws ErrorResultException {
    if (year < 0) {
        throw new ErrorResultException("Year can't be negative", HttpStatus.BAD_REQUEST);
    }
    if (month < 1 || month > 12) {
        throw new ErrorResultException("Month must be a value between 1 and 12", HttpStatus.BAD_REQUEST);
    }
    var now = LocalDateTime.now();
    if (year > now.getYear() || (year == now.getYear() && month > now.getMonthValue())) {
        throw new ErrorResultException("Combination of year and month lies in the future", HttpStatus.BAD_REQUEST);
    }
    var statistics = repositories.findAdminStatisticsByYearAndMonth(year, month);
    if (statistics == null) {
        LocalDateTime startInclusive;
        try {
            startInclusive = LocalDateTime.of(year, month, 1, 0, 0);
        } catch (DateTimeException e) {
            throw new ErrorResultException("Invalid month or year", HttpStatus.BAD_REQUEST);
        }
        var currentYearAndMonth = now.getYear() == year && now.getMonthValue() == month;
        var endExclusive = currentYearAndMonth ? now.truncatedTo(ChronoUnit.MINUTES) : startInclusive.plusMonths(1);
        var extensions = repositories.countActiveExtensions(endExclusive);
        var downloads = repositories.downloadsBetween(startInclusive, endExclusive);
        var downloadsTotal = repositories.downloadsUntil(endExclusive);
        var publishers = repositories.countActiveExtensionPublishers(endExclusive);
        var averageReviewsPerExtension = repositories.averageNumberOfActiveReviewsPerActiveExtension(endExclusive);
        var namespaceOwners = repositories.countPublishersThatClaimedNamespaceOwnership(endExclusive);
        var extensionsByRating = repositories.countActiveExtensionsGroupedByExtensionReviewRating(endExclusive);
        var publishersByExtensionsPublished = repositories.countActiveExtensionPublishersGroupedByExtensionsPublished(endExclusive);
        statistics = new AdminStatistics();
        statistics.setYear(year);
        statistics.setMonth(month);
        statistics.setExtensions(extensions);
        statistics.setDownloads(downloads);
        statistics.setDownloadsTotal(downloadsTotal);
        statistics.setPublishers(publishers);
        statistics.setAverageReviewsPerExtension(averageReviewsPerExtension);
        statistics.setNamespaceOwners(namespaceOwners);
        statistics.setExtensionsByRating(extensionsByRating);
        statistics.setPublishersByExtensionsPublished(publishersByExtensionsPublished);
        if (!currentYearAndMonth) {
            // archive statistics for quicker lookup next time
            entityManager.persist(statistics);
        }
    }
    return statistics.toCsv();
}
Also used : ErrorResultException(org.eclipse.openvsx.util.ErrorResultException) LocalDateTime(java.time.LocalDateTime) DateTimeException(java.time.DateTimeException) Transactional(javax.transaction.Transactional)

Example 12 with ErrorResultException

use of org.eclipse.openvsx.util.ErrorResultException in project openvsx by eclipse.

the class ExtensionProcessor method loadVsixManifest.

private void loadVsixManifest() {
    if (vsixManifest != null) {
        return;
    }
    readInputStream();
    // Read extension.vsixmanifest
    var bytes = ArchiveUtil.readEntry(zipFile, VSIX_MANIFEST);
    if (bytes == null)
        throw new ErrorResultException("Entry not found: " + VSIX_MANIFEST);
    try {
        var mapper = new XmlMapper();
        vsixManifest = mapper.readTree(bytes);
    } catch (JsonParseException exc) {
        throw new ErrorResultException("Invalid JSON format in " + VSIX_MANIFEST + ": " + exc.getMessage());
    } catch (IOException exc) {
        throw new RuntimeException(exc);
    }
}
Also used : ErrorResultException(org.eclipse.openvsx.util.ErrorResultException) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper)

Example 13 with ErrorResultException

use of org.eclipse.openvsx.util.ErrorResultException in project openvsx by eclipse.

the class ExtensionProcessor method readInputStream.

private void readInputStream() {
    if (zipFile != null) {
        return;
    }
    try {
        if (inputStream != null)
            content = ByteStreams.toByteArray(inputStream);
        if (content.length > MAX_CONTENT_SIZE)
            throw new ErrorResultException("The extension package exceeds the size limit of 512 MB.", HttpStatus.PAYLOAD_TOO_LARGE);
        var tempFile = File.createTempFile("extension_", ".vsix");
        Files.write(content, tempFile);
        zipFile = new ZipFile(tempFile);
    } catch (ZipException exc) {
        throw new ErrorResultException("Could not read zip file: " + exc.getMessage());
    } catch (EOFException exc) {
        throw new ErrorResultException("Could not read from input stream: " + exc.getMessage());
    } catch (IOException exc) {
        throw new RuntimeException(exc);
    }
}
Also used : ErrorResultException(org.eclipse.openvsx.util.ErrorResultException) ZipFile(java.util.zip.ZipFile) EOFException(java.io.EOFException) ZipException(java.util.zip.ZipException) IOException(java.io.IOException)

Example 14 with ErrorResultException

use of org.eclipse.openvsx.util.ErrorResultException in project openvsx by eclipse.

the class ExtensionProcessor method loadPackageJson.

private void loadPackageJson() {
    if (packageJson != null) {
        return;
    }
    readInputStream();
    // Read package.json
    var bytes = ArchiveUtil.readEntry(zipFile, PACKAGE_JSON);
    if (bytes == null)
        throw new ErrorResultException("Entry not found: " + PACKAGE_JSON);
    try {
        var mapper = new ObjectMapper();
        packageJson = mapper.readTree(bytes);
    } catch (JsonParseException exc) {
        throw new ErrorResultException("Invalid JSON format in " + PACKAGE_JSON + ": " + exc.getMessage());
    } catch (IOException exc) {
        throw new RuntimeException(exc);
    }
}
Also used : ErrorResultException(org.eclipse.openvsx.util.ErrorResultException) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 15 with ErrorResultException

use of org.eclipse.openvsx.util.ErrorResultException in project openvsx by eclipse.

the class AdminAPI method getExtension.

@GetMapping(path = "/admin/extension/{namespaceName}/{extensionName}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<ExtensionJson> getExtension(@PathVariable String namespaceName, @PathVariable String extensionName) {
    try {
        admins.checkAdminUser();
        var extension = repositories.findExtension(extensionName, namespaceName);
        if (extension == null) {
            var json = ExtensionJson.error("Extension not found: " + namespaceName + "." + extensionName);
            return new ResponseEntity<>(json, HttpStatus.NOT_FOUND);
        }
        ExtensionJson json;
        // Don't rely on the 'latest' relationship here because the extension might be inactive
        var extVersion = getLatestVersion(extension);
        if (extVersion == null) {
            json = new ExtensionJson();
            json.namespace = extension.getNamespace().getName();
            json.name = extension.getName();
            json.allVersions = Collections.emptyMap();
        } else {
            json = local.toExtensionVersionJson(extVersion, false);
        }
        json.active = extension.isActive();
        return ResponseEntity.ok(json);
    } catch (ErrorResultException exc) {
        return exc.toResponseEntity(ExtensionJson.class);
    }
}
Also used : ErrorResultException(org.eclipse.openvsx.util.ErrorResultException) ResponseEntity(org.springframework.http.ResponseEntity) ExtensionJson(org.eclipse.openvsx.json.ExtensionJson) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Aggregations

ErrorResultException (org.eclipse.openvsx.util.ErrorResultException)22 Transactional (javax.transaction.Transactional)5 HttpHeaders (org.springframework.http.HttpHeaders)5 RestClientException (org.springframework.web.client.RestClientException)5 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 IOException (java.io.IOException)3 Collectors (java.util.stream.Collectors)3 Extension (org.eclipse.openvsx.entities.Extension)3 ExtensionVersion (org.eclipse.openvsx.entities.ExtensionVersion)3 ExtensionJson (org.eclipse.openvsx.json.ExtensionJson)3 ResultJson (org.eclipse.openvsx.json.ResultJson)3 RepositoryService (org.eclipse.openvsx.repositories.RepositoryService)3 SearchUtilService (org.eclipse.openvsx.search.SearchUtilService)3 NotFoundException (org.eclipse.openvsx.util.NotFoundException)3 UrlUtil (org.eclipse.openvsx.util.UrlUtil)3 Autowired (org.springframework.beans.factory.annotation.Autowired)3 HttpStatus (org.springframework.http.HttpStatus)3 ResponseEntity (org.springframework.http.ResponseEntity)3 GetMapping (org.springframework.web.bind.annotation.GetMapping)3 ResponseStatusException (org.springframework.web.server.ResponseStatusException)3