Search in sources :

Example 1 with PkgVersion

use of org.haiku.haikudepotserver.dataobjects.PkgVersion in project haikudepotserver by haiku.

the class PkgDownloadController method download.

@RequestMapping(value = { "/" + SEGMENT_PKGDOWNLOAD + PATH }, method = RequestMethod.GET)
public void download(HttpServletResponse response, @PathVariable(value = KEY_PKGNAME) String pkgName, @PathVariable(value = KEY_REPOSITORYCODE) String repositoryCode, @PathVariable(value = KEY_MAJOR) String major, @PathVariable(value = KEY_MINOR) String minor, @PathVariable(value = KEY_MICRO) String micro, @PathVariable(value = KEY_PRERELEASE) String prerelease, @PathVariable(value = KEY_REVISION) String revisionStr, @PathVariable(value = KEY_ARCHITECTURECODE) String architectureCode) throws IOException, RequestObjectNotFound {
    Preconditions.checkArgument(null != response, "the response is required");
    ObjectContext context = serverRuntime.newContext();
    Pkg pkg = Pkg.tryGetByName(context, pkgName).orElseThrow(() -> {
        LOGGER.info("unable to find the package; {}", pkgName);
        return new RequestObjectNotFound();
    });
    Repository repository = Repository.tryGetByCode(context, repositoryCode).orElseThrow(() -> {
        LOGGER.info("unable to find the repository; {}", repositoryCode);
        return new RequestObjectNotFound();
    });
    Architecture architecture = Architecture.tryGetByCode(context, architectureCode).orElseThrow(() -> {
        LOGGER.info("unable to find the architecture; {}", architectureCode);
        return new RequestObjectNotFound();
    });
    revisionStr = hyphenToNull(revisionStr);
    VersionCoordinates versionCoordinates = new VersionCoordinates(hyphenToNull(major), hyphenToNull(minor), hyphenToNull(micro), hyphenToNull(prerelease), null == revisionStr ? null : Integer.parseInt(revisionStr));
    PkgVersion pkgVersion = PkgVersion.getForPkg(context, pkg, repository, architecture, versionCoordinates).orElseThrow(() -> {
        LOGGER.info("unable to find the pkg version; {}, {}", pkgName, versionCoordinates);
        return new RequestObjectNotFound();
    });
    Optional<URL> urlOptional = pkgVersion.tryGetHpkgURL(ExposureType.EXTERNAL_FACING);
    if (urlOptional.isEmpty()) {
        LOGGER.info("unable to allow download of the hpkg data as no url was able to be generated");
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
    } else {
        URL url = urlOptional.get();
        if (ImmutableSet.of("http", "https").contains(url.getProtocol())) {
            response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
            response.setHeader(HttpHeaders.LOCATION, url.toString());
            response.setContentType(MediaType.PLAIN_TEXT_UTF_8.toString());
            PrintWriter writer = response.getWriter();
            writer.print(url.toString());
            writer.flush();
        } else {
            response.setContentType(MediaType.OCTET_STREAM.toString());
            response.setHeader(HttpHeaders.CONTENT_DISPOSITION, String.format("attachment; filename=\"%s\"", pkgVersion.getHpkgFilename()));
            try (InputStream inputStream = url.openStream()) {
                LOGGER.info("downloaded package version; {} - {}", pkg.getName(), pkgVersion);
                ByteStreams.copy(inputStream, response.getOutputStream());
            } catch (IOException ioe) {
                // logged without a stack trace because it happens fairly often that a robot will initiate the download and then drop it.
                LOGGER.error("unable to relay data to output stream from '{}'; {} -- {}", url.toString(), ioe.getClass().getSimpleName(), ioe.getMessage());
            }
        }
    }
}
Also used : Repository(org.haiku.haikudepotserver.dataobjects.Repository) Architecture(org.haiku.haikudepotserver.dataobjects.Architecture) VersionCoordinates(org.haiku.haikudepotserver.support.VersionCoordinates) InputStream(java.io.InputStream) PkgVersion(org.haiku.haikudepotserver.dataobjects.PkgVersion) ObjectContext(org.apache.cayenne.ObjectContext) IOException(java.io.IOException) Pkg(org.haiku.haikudepotserver.dataobjects.Pkg) URL(java.net.URL) PrintWriter(java.io.PrintWriter) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with PkgVersion

use of org.haiku.haikudepotserver.dataobjects.PkgVersion in project haikudepotserver by haiku.

the class PkgVersionPayloadLengthPopulationJobRunner method run.

@Override
public void run(JobService jobService, PkgVersionPayloadLengthPopulationJobSpecification specification) throws IOException {
    Preconditions.checkArgument(null != jobService);
    Preconditions.checkArgument(null != specification);
    ObjectContext context = serverRuntime.newContext();
    // we want to fetch the ObjectIds of PkgVersions that need to be handled.
    List<PkgVersion> pkgVersions = ObjectSelect.query(PkgVersion.class).where(PkgVersion.ACTIVE.isTrue()).and(PkgVersion.PKG.dot(Pkg.ACTIVE).isTrue()).and(PkgVersion.IS_LATEST.isTrue()).and(PkgVersion.PAYLOAD_LENGTH.isNull()).pageSize(50).select(context);
    LOGGER.info("did find {} package versions that need payload lengths to be populated", pkgVersions.size());
    for (int i = 0; i < pkgVersions.size(); i++) {
        PkgVersion pkgVersion = pkgVersions.get(i);
        Optional<URL> urlOptional = pkgVersion.tryGetHpkgURL(ExposureType.INTERNAL_FACING);
        if (urlOptional.isPresent()) {
            try {
                urlHelperService.tryGetPayloadLength(urlOptional.get()).filter(l -> l > 0L).ifPresent(l -> {
                    pkgVersion.setPayloadLength(l);
                    context.commitChanges();
                });
            } catch (IOException ioe) {
                LOGGER.error("unable to get the payload length for " + pkgVersion.toString(), ioe);
            }
        } else {
            LOGGER.info("unable to get the length of [{}] because no url" + "hpkg url was able to be obtained", pkgVersion);
        }
        jobService.setJobProgressPercent(specification.getGuid(), i * 100 / pkgVersions.size());
    }
}
Also used : ObjectContext(org.apache.cayenne.ObjectContext) Pkg(org.haiku.haikudepotserver.dataobjects.Pkg) Logger(org.slf4j.Logger) URL(java.net.URL) AbstractJobRunner(org.haiku.haikudepotserver.job.AbstractJobRunner) PkgVersionPayloadLengthPopulationJobSpecification(org.haiku.haikudepotserver.pkg.model.PkgVersionPayloadLengthPopulationJobSpecification) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) ExposureType(org.haiku.haikudepotserver.support.ExposureType) PkgVersion(org.haiku.haikudepotserver.dataobjects.PkgVersion) URLHelperService(org.haiku.haikudepotserver.support.URLHelperService) Component(org.springframework.stereotype.Component) List(java.util.List) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) ObjectSelect(org.apache.cayenne.query.ObjectSelect) JobService(org.haiku.haikudepotserver.job.model.JobService) ServerRuntime(org.apache.cayenne.configuration.server.ServerRuntime) PkgVersion(org.haiku.haikudepotserver.dataobjects.PkgVersion) ObjectContext(org.apache.cayenne.ObjectContext) IOException(java.io.IOException) URL(java.net.URL)

Example 3 with PkgVersion

use of org.haiku.haikudepotserver.dataobjects.PkgVersion in project haikudepotserver by haiku.

the class PkgImportServiceImpl method possiblyReconfigurePersistedPkgVersionToBeLatest.

private void possiblyReconfigurePersistedPkgVersionToBeLatest(ObjectContext objectContext, PkgVersion persistedLatestExistingPkgVersion, PkgVersion persistedPkgVersion) {
    if (null != persistedLatestExistingPkgVersion) {
        VersionCoordinatesComparator versionCoordinatesComparator = new VersionCoordinatesComparator();
        VersionCoordinates persistedPkgVersionCoords = persistedPkgVersion.toVersionCoordinates();
        VersionCoordinates persistedLatestExistingPkgVersionCoords = persistedLatestExistingPkgVersion.toVersionCoordinates();
        int c = versionCoordinatesComparator.compare(persistedPkgVersionCoords, persistedLatestExistingPkgVersionCoords);
        if (c > 0) {
            persistedPkgVersion.setIsLatest(true);
            persistedLatestExistingPkgVersion.setIsLatest(false);
        } else {
            boolean isRealArchitecture = !persistedPkgVersion.getArchitecture().getCode().equals(Architecture.CODE_SOURCE);
            if (0 == c) {
                if (isRealArchitecture) {
                    LOGGER.debug("imported a package version [{}] of [{}] which is the same as the existing [{}]", persistedPkgVersionCoords, persistedPkgVersion.getPkg().getName(), persistedLatestExistingPkgVersionCoords);
                }
            } else {
                if (isRealArchitecture) {
                    // [apl 3.dec.2016]
                    // If the package from the repository is older than the one that is presently marked as latest
                    // then a regression has occurred.  In this case make the imported one be the latest and mark
                    // the later ones as "inactive".
                    List<PkgVersion> pkgVersionsToDeactivate = PkgVersion.getForPkg(objectContext, persistedPkgVersion.getPkg(), persistedPkgVersion.getRepositorySource().getRepository(), false).stream().filter((pv) -> pv.getArchitecture().equals(persistedPkgVersion.getArchitecture())).filter((pv) -> versionCoordinatesComparator.compare(persistedPkgVersionCoords, pv.toVersionCoordinates()) < 0).collect(Collectors.toList());
                    LOGGER.warn("imported a package version {} of {} which is older or the same as the existing {}" + " -- will deactivate {} pkg versions after the imported one and make the" + " imported one as latest", persistedPkgVersionCoords, persistedPkgVersion.getPkg().getName(), persistedLatestExistingPkgVersionCoords, pkgVersionsToDeactivate.size());
                    for (PkgVersion pkgVersionToDeactivate : pkgVersionsToDeactivate) {
                        pkgVersionToDeactivate.setActive(false);
                        pkgVersionToDeactivate.setIsLatest(false);
                        LOGGER.info("deactivated {}", pkgVersionToDeactivate);
                    }
                    persistedPkgVersion.setIsLatest(true);
                }
            }
        }
    } else {
        persistedPkgVersion.setIsLatest(true);
    }
}
Also used : PkgIconService(org.haiku.haikudepotserver.pkg.model.PkgIconService) PkgService(org.haiku.haikudepotserver.pkg.model.PkgService) Iterables(com.google.common.collect.Iterables) ObjectContext(org.apache.cayenne.ObjectContext) URL(java.net.URL) BadPkgIconException(org.haiku.haikudepotserver.pkg.model.BadPkgIconException) LoggerFactory(org.slf4j.LoggerFactory) AttributeContext(org.haiku.pkg.AttributeContext) HpkgHelper(org.haiku.haikudepotserver.support.HpkgHelper) PkgUrlType(org.haiku.haikudepotserver.dataobjects.PkgUrlType) StringUtils(org.apache.commons.lang3.StringUtils) Strings(com.google.common.base.Strings) PkgVersion(org.haiku.haikudepotserver.dataobjects.PkgVersion) ImmutableList(com.google.common.collect.ImmutableList) Service(org.springframework.stereotype.Service) PkgVersionCopyright(org.haiku.haikudepotserver.dataobjects.PkgVersionCopyright) PkgImportService(org.haiku.haikudepotserver.pkg.model.PkgImportService) ByteSource(com.google.common.io.ByteSource) MediaType(org.haiku.haikudepotserver.dataobjects.MediaType) HpkgFileExtractor(org.haiku.pkg.HpkgFileExtractor) Pkg(org.haiku.haikudepotserver.dataobjects.Pkg) Logger(org.slf4j.Logger) RepositorySource(org.haiku.haikudepotserver.dataobjects.RepositorySource) NaturalLanguage(org.haiku.haikudepotserver.dataobjects.NaturalLanguage) Architecture(org.haiku.haikudepotserver.dataobjects.Architecture) AttributeId(org.haiku.pkg.model.AttributeId) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) ExposureType(org.haiku.haikudepotserver.support.ExposureType) File(java.io.File) UncheckedIOException(java.io.UncheckedIOException) URLHelperService(org.haiku.haikudepotserver.support.URLHelperService) List(java.util.List) Stream(java.util.stream.Stream) ObjectId(org.apache.cayenne.ObjectId) PkgVersionLicense(org.haiku.haikudepotserver.dataobjects.PkgVersionLicense) VersionCoordinates(org.haiku.haikudepotserver.support.VersionCoordinates) VersionCoordinatesComparator(org.haiku.haikudepotserver.support.VersionCoordinatesComparator) PkgSupplement(org.haiku.haikudepotserver.dataobjects.PkgSupplement) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) RandomStringUtils(org.apache.commons.lang3.RandomStringUtils) PkgLocalizationService(org.haiku.haikudepotserver.pkg.model.PkgLocalizationService) PkgVersionUrl(org.haiku.haikudepotserver.dataobjects.PkgVersionUrl) Collections(java.util.Collections) Attribute(org.haiku.pkg.model.Attribute) InputStream(java.io.InputStream) VersionCoordinates(org.haiku.haikudepotserver.support.VersionCoordinates) VersionCoordinatesComparator(org.haiku.haikudepotserver.support.VersionCoordinatesComparator) PkgVersion(org.haiku.haikudepotserver.dataobjects.PkgVersion)

Example 4 with PkgVersion

use of org.haiku.haikudepotserver.dataobjects.PkgVersion in project haikudepotserver by haiku.

the class PkgImportServiceImpl method importFrom.

@Override
public void importFrom(ObjectContext objectContext, ObjectId repositorySourceObjectId, org.haiku.pkg.model.Pkg pkg, boolean populateFromPayload) {
    Preconditions.checkArgument(null != pkg, "the package must be provided");
    Preconditions.checkArgument(null != repositorySourceObjectId, "the repository source is must be provided");
    RepositorySource repositorySource = RepositorySource.get(objectContext, repositorySourceObjectId);
    if (!repositorySource.getActive()) {
        throw new IllegalStateException("it is not possible to import from a repository source that is not active; " + repositorySource);
    }
    if (!repositorySource.getRepository().getActive()) {
        throw new IllegalStateException("it is not possible to import from a repository that is not active; " + repositorySource.getRepository());
    }
    // first, check to see if the package is there or not.
    Optional<Pkg> persistedPkgOptional = Pkg.tryGetByName(objectContext, pkg.getName());
    Pkg persistedPkg;
    Optional<PkgVersion> persistedLatestExistingPkgVersion = Optional.empty();
    Architecture architecture = Architecture.tryGetByCode(objectContext, pkg.getArchitecture().name().toLowerCase()).orElseThrow(IllegalStateException::new);
    PkgVersion persistedPkgVersion = null;
    if (persistedPkgOptional.isEmpty()) {
        persistedPkg = createPkg(objectContext, pkg.getName());
        pkgServiceImpl.ensurePkgProminence(objectContext, persistedPkg, repositorySource.getRepository());
        LOGGER.info("the package [{}] did not exist; will create", pkg.getName());
    } else {
        persistedPkg = persistedPkgOptional.get();
        pkgServiceImpl.ensurePkgProminence(objectContext, persistedPkg, repositorySource.getRepository());
        // if we know that the package exists then we should look for the version.
        persistedPkgVersion = PkgVersion.getForPkg(objectContext, persistedPkg, repositorySource.getRepository(), architecture, new VersionCoordinates(pkg.getVersion())).orElse(null);
        persistedLatestExistingPkgVersion = pkgServiceImpl.getLatestPkgVersionForPkg(objectContext, persistedPkg, repositorySource.getRepository(), Collections.singletonList(architecture));
    }
    if (null == persistedPkgVersion) {
        persistedPkgVersion = objectContext.newObject(PkgVersion.class);
        persistedPkgVersion.setMajor(pkg.getVersion().getMajor());
        persistedPkgVersion.setMinor(pkg.getVersion().getMinor());
        persistedPkgVersion.setMicro(pkg.getVersion().getMicro());
        persistedPkgVersion.setPreRelease(pkg.getVersion().getPreRelease());
        persistedPkgVersion.setRevision(pkg.getVersion().getRevision());
        persistedPkgVersion.setRepositorySource(repositorySource);
        persistedPkgVersion.setArchitecture(architecture);
        persistedPkgVersion.setPkg(persistedPkg);
        LOGGER.info("the version [{}] of package [{}] did not exist; will create", pkg.getVersion().toString(), pkg.getName());
    } else {
        LOGGER.debug("the version [{}] of package [{}] did exist; will re-configure necessary data", pkg.getVersion().toString(), pkg.getName());
    }
    persistedPkgVersion.setActive(Boolean.TRUE);
    importCopyrights(objectContext, pkg, persistedPkgVersion);
    importLicenses(objectContext, pkg, persistedPkgVersion);
    importUrls(objectContext, pkg, persistedPkgVersion);
    if (!Strings.isNullOrEmpty(pkg.getSummary()) || !Strings.isNullOrEmpty(pkg.getDescription())) {
        pkgLocalizationService.updatePkgVersionLocalization(objectContext, persistedPkgVersion, NaturalLanguage.getEnglish(objectContext), // not supported quite yet
        null, pkg.getSummary(), pkg.getDescription());
    }
    // now possibly switch the latest flag over to the new one from the old one.
    possiblyReconfigurePersistedPkgVersionToBeLatest(objectContext, persistedLatestExistingPkgVersion.orElse(null), persistedPkgVersion);
    if (populateFromPayload && shouldPopulateFromPayload(persistedPkgVersion)) {
        populateFromPayload(objectContext, persistedPkgVersion);
    }
    LOGGER.debug("have processed package {}", pkg);
}
Also used : Architecture(org.haiku.haikudepotserver.dataobjects.Architecture) VersionCoordinates(org.haiku.haikudepotserver.support.VersionCoordinates) RepositorySource(org.haiku.haikudepotserver.dataobjects.RepositorySource) PkgVersion(org.haiku.haikudepotserver.dataobjects.PkgVersion) Pkg(org.haiku.haikudepotserver.dataobjects.Pkg)

Example 5 with PkgVersion

use of org.haiku.haikudepotserver.dataobjects.PkgVersion in project haikudepotserver by haiku.

the class UserRatingOrchestrationServiceIT method createTestUserRatingPkgVersion.

private PkgVersion createTestUserRatingPkgVersion(ObjectContext context, RepositorySource repositorySource, Pkg pkg, Architecture architecture, Integer major, Integer minor, Integer micro, Integer revision, boolean isLatest) {
    PkgVersion pkgVersion = context.newObject(PkgVersion.class);
    pkgVersion.setIsLatest(isLatest);
    pkgVersion.setRepositorySource(repositorySource);
    pkgVersion.setRevision(revision);
    pkgVersion.setArchitecture(architecture);
    pkgVersion.setMajor(Integer.toString(major));
    pkgVersion.setMinor(Integer.toString(minor));
    pkgVersion.setMicro(Integer.toString(micro));
    pkgVersion.setPkg(pkg);
    return pkgVersion;
}
Also used : PkgVersion(org.haiku.haikudepotserver.dataobjects.PkgVersion)

Aggregations

PkgVersion (org.haiku.haikudepotserver.dataobjects.PkgVersion)10 ObjectContext (org.apache.cayenne.ObjectContext)8 Pkg (org.haiku.haikudepotserver.dataobjects.Pkg)8 Architecture (org.haiku.haikudepotserver.dataobjects.Architecture)5 VersionCoordinates (org.haiku.haikudepotserver.support.VersionCoordinates)5 List (java.util.List)4 Repository (org.haiku.haikudepotserver.dataobjects.Repository)4 Preconditions (com.google.common.base.Preconditions)3 IOException (java.io.IOException)3 URL (java.net.URL)3 Optional (java.util.Optional)3 AbstractIntegrationTest (org.haiku.haikudepotserver.AbstractIntegrationTest)3 RepositorySource (org.haiku.haikudepotserver.dataobjects.RepositorySource)3 Test (org.junit.jupiter.api.Test)3 File (java.io.File)2 InputStream (java.io.InputStream)2 PrintWriter (java.io.PrintWriter)2 Collections (java.util.Collections)2 Collectors (java.util.stream.Collectors)2 ObjectId (org.apache.cayenne.ObjectId)2