Search in sources :

Example 1 with Architecture

use of org.haiku.haikudepotserver.dataobjects.Architecture 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 Architecture

use of org.haiku.haikudepotserver.dataobjects.Architecture 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 3 with Architecture

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

the class RepositoryDumpExportJobRunner method createDumpRepository.

private DumpExportRepository createDumpRepository(Repository repository) {
    DumpExportRepository dumpRepository = new DumpExportRepository();
    dumpRepository.setCode(repository.getCode());
    dumpRepository.setName(repository.getName());
    dumpRepository.setDescription(repository.getDescription());
    dumpRepository.setInformationUrl(repository.getInformationUrl());
    dumpRepository.setRepositorySources(repository.getRepositorySources().stream().filter(_RepositorySource::getActive).filter(rs -> rs.tryGetPrimaryMirror().isPresent()).sorted(Comparator.comparing(_RepositorySource::getCode)).map((rs) -> {
        DumpExportRepositorySource dumpRepositorySource = new DumpExportRepositorySource();
        dumpRepositorySource.setCode(rs.getCode());
        dumpRepositorySource.setArchitectureCode(Optional.ofNullable(rs.getArchitecture()).map(Architecture::getCode).orElse(null));
        dumpRepositorySource.setIdentifier(rs.getIdentifier());
        dumpRepositorySource.setRepoInfoUrl(rs.getIdentifier());
        // ^^ deprecated; repoInfoUrl is replaced with identifier
        dumpRepositorySource.setExtraIdentifiers(rs.getExtraIdentifiers());
        dumpRepositorySource.setRepositorySourceMirrors(rs.getRepositorySourceMirrors().stream().filter(_RepositorySourceMirror::getActive).sorted(Comparator.comparing(_RepositorySourceMirror::getCode)).map(rsm -> {
            DumpExportRepositorySourceMirror dumpMirror = new DumpExportRepositorySourceMirror();
            dumpMirror.setBaseUrl(rsm.getBaseUrl());
            dumpMirror.setCountryCode(rsm.getCountry().getCode());
            dumpMirror.setDescription(rsm.getDescription());
            dumpMirror.setIsPrimary(rsm.getIsPrimary());
            return dumpMirror;
        }).collect(Collectors.toList()));
        return dumpRepositorySource;
    }).collect(Collectors.toList()));
    return dumpRepository;
}
Also used : DumpExportRepository(org.haiku.haikudepotserver.repository.model.dumpexport.DumpExportRepository) DumpExportRepositorySourceMirror(org.haiku.haikudepotserver.repository.model.dumpexport.DumpExportRepositorySourceMirror) org.haiku.haikudepotserver.dataobjects.auto._RepositorySource(org.haiku.haikudepotserver.dataobjects.auto._RepositorySource) Architecture(org.haiku.haikudepotserver.dataobjects.Architecture) org.haiku.haikudepotserver.dataobjects.auto._RepositorySourceMirror(org.haiku.haikudepotserver.dataobjects.auto._RepositorySourceMirror) DumpExportRepositorySource(org.haiku.haikudepotserver.repository.model.dumpexport.DumpExportRepositorySource)

Example 4 with Architecture

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

the class UserRatingOrchestrationServiceIT method createTestUserRatingData.

private UserRatingTestData createTestUserRatingData(ObjectContext context) {
    UserRatingTestData userRatingTestData = new UserRatingTestData();
    Repository repository = Repository.tryGetByCode(context, "testrepo").get();
    RepositorySource repositorySource = RepositorySource.tryGetByCode(context, "testreposrc_xyz").get();
    Architecture x86_64 = Architecture.tryGetByCode(context, "x86_64").get();
    Architecture x86_gcc2 = Architecture.tryGetByCode(context, "x86_gcc2").get();
    userRatingTestData.pkg = integrationTestSupportService.createPkg(context, "urtestpkg");
    pkgService.ensurePkgProminence(context, userRatingTestData.pkg, repository, Prominence.ORDERING_LAST);
    userRatingTestData.user1 = integrationTestSupportService.createBasicUser(context, "urtestuser1", "password");
    userRatingTestData.user2 = integrationTestSupportService.createBasicUser(context, "urtestuser2", "password");
    userRatingTestData.user3 = integrationTestSupportService.createBasicUser(context, "urtestuser3", "password");
    userRatingTestData.user4 = integrationTestSupportService.createBasicUser(context, "urtestuser4", "password");
    userRatingTestData.user5 = integrationTestSupportService.createBasicUser(context, "urtestuser5", "password");
    userRatingTestData.pkgVersion_0_0_9__x86_gcc2 = createTestUserRatingPkgVersion(context, repositorySource, userRatingTestData.pkg, x86_64, 0, 0, 9, null, false);
    userRatingTestData.pkgVersion_1_0_0__x86_gcc2 = createTestUserRatingPkgVersion(context, repositorySource, userRatingTestData.pkg, x86_64, 1, 0, 0, null, false);
    userRatingTestData.pkgVersion_1_0_1__x86_gcc2 = createTestUserRatingPkgVersion(context, repositorySource, userRatingTestData.pkg, x86_64, 1, 0, 1, null, false);
    userRatingTestData.pkgVersion_1_0_1_1__x86_gcc2 = createTestUserRatingPkgVersion(context, repositorySource, userRatingTestData.pkg, x86_64, 1, 0, 1, 1, false);
    userRatingTestData.pkgVersion_1_0_2__x86_gcc2 = createTestUserRatingPkgVersion(context, repositorySource, userRatingTestData.pkg, x86_64, 1, 0, 2, null, false);
    userRatingTestData.pkgVersion_1_0_2__x86_64 = createTestUserRatingPkgVersion(context, repositorySource, userRatingTestData.pkg, x86_gcc2, 1, 0, 2, null, false);
    return userRatingTestData;
}
Also used : Repository(org.haiku.haikudepotserver.dataobjects.Repository) Architecture(org.haiku.haikudepotserver.dataobjects.Architecture) RepositorySource(org.haiku.haikudepotserver.dataobjects.RepositorySource)

Example 5 with Architecture

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

the class RepositoryHpkrIngressJobRunner method runImportInfoForRepositorySource.

/**
 * <p>Each repository has a little &quot;repo.info&quot; file that resides next to the HPKR data.
 * This method will pull this in and process the data into the repository source.</p>
 */
private void runImportInfoForRepositorySource(ObjectContext mainContext, RepositorySource repositorySource) throws RepositoryHpkrIngressException {
    URL url = repositorySource.tryGetInternalFacingDownloadRepoInfoURL().orElseThrow(() -> new RepositoryHpkrIngressException("unable to download for [" + repositorySource.getCode() + "] as no download repo info url was available"));
    // now shift the URL's data into a temporary file and then process it.
    File temporaryFile = null;
    try {
        temporaryFile = File.createTempFile(repositorySource.getCode() + "__import", ".repo-info");
        LOGGER.info("will copy data for repository info [{}] ({}) to temporary file", repositorySource, url.toString());
        FileHelper.streamUrlDataToFile(url, temporaryFile, TIMEOUT_REPOSITORY_SOURCE_FETCH);
        try (InputStream inputStream = new FileInputStream(temporaryFile);
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charsets.UTF_8);
            BufferedReader reader = new BufferedReader(inputStreamReader)) {
            List<Parameter> parameters = DriverSettings.parse(reader);
            String identifierParameterValue = ObjectUtils.firstNonNull(tryGetParameterValue(parameters, PARAMETER_NAME_IDENTIFIER).orElse(null), tryGetParameterValue(parameters, PARAMETER_NAME_URL).orElse(null));
            if (StringUtils.isEmpty(identifierParameterValue)) {
                throw new DriverSettingsException("expected to find the parameter [" + PARAMETER_NAME_IDENTIFIER + "] or [" + PARAMETER_NAME_URL + "]");
            }
            if (!Objects.equals(identifierParameterValue, repositorySource.getIdentifier())) {
                LOGGER.info("updated the repo info identifier to [{}] for repository source [{}]", identifierParameterValue, repositorySource.getCode());
                repositorySource.setIdentifier(identifierParameterValue);
                repositorySource.getRepository().setModifyTimestamp();
                mainContext.commitChanges();
            }
            Optional<String> architectureCodeOptional = tryGetParameterValue(parameters, PARAMETER_ARCHITECTURE);
            if (architectureCodeOptional.isEmpty()) {
                throw new RepositoryHpkrIngressException("repository source [" + repositorySource.getCode() + "] has no architecture code");
            }
            Optional<Architecture> architectureOptional = Architecture.tryGetByCode(mainContext, architectureCodeOptional.get());
            if (architectureOptional.isEmpty()) {
                throw new RepositoryHpkrIngressException("repository source [" + repositorySource.getCode() + "] has unknown architecture code [" + architectureCodeOptional.get() + "]");
            }
            repositorySource.setArchitecture(architectureOptional.get());
        }
    } catch (IOException | DriverSettingsException e) {
        throw new RepositoryHpkrIngressException("a problem has arisen parsing or dealing with the 'repo.info' file", e);
    } finally {
        if (null != temporaryFile && temporaryFile.exists()) {
            if (!temporaryFile.delete()) {
                LOGGER.error("unable to delete the file; {}" + temporaryFile.getAbsolutePath());
            }
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) DriverSettingsException(org.haiku.driversettings.DriverSettingsException) Architecture(org.haiku.haikudepotserver.dataobjects.Architecture) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) URL(java.net.URL) FileInputStream(java.io.FileInputStream) RepositoryHpkrIngressException(org.haiku.haikudepotserver.repository.model.RepositoryHpkrIngressException) BufferedReader(java.io.BufferedReader) Parameter(org.haiku.driversettings.Parameter) File(java.io.File)

Aggregations

Architecture (org.haiku.haikudepotserver.dataobjects.Architecture)9 ObjectContext (org.apache.cayenne.ObjectContext)5 Repository (org.haiku.haikudepotserver.dataobjects.Repository)5 Pkg (org.haiku.haikudepotserver.dataobjects.Pkg)4 PkgVersion (org.haiku.haikudepotserver.dataobjects.PkgVersion)4 RepositorySource (org.haiku.haikudepotserver.dataobjects.RepositorySource)4 VersionCoordinates (org.haiku.haikudepotserver.support.VersionCoordinates)4 org.haiku.haikudepotserver.dataobjects.auto._RepositorySourceMirror (org.haiku.haikudepotserver.dataobjects.auto._RepositorySourceMirror)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 URL (java.net.URL)2 AbstractIntegrationTest (org.haiku.haikudepotserver.AbstractIntegrationTest)2 GetRepositorySourceResult (org.haiku.haikudepotserver.api1.model.repository.GetRepositorySourceResult)2 ObjectNotFoundException (org.haiku.haikudepotserver.api1.support.ObjectNotFoundException)2 RepositorySourceMirror (org.haiku.haikudepotserver.dataobjects.RepositorySourceMirror)2 Test (org.junit.jupiter.api.Test)2 Preconditions (com.google.common.base.Preconditions)1 Strings (com.google.common.base.Strings)1 AutoJsonRpcServiceImpl (com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImpl)1 BufferedReader (java.io.BufferedReader)1