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());
}
}
}
}
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());
}
}
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);
}
}
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);
}
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;
}
Aggregations