Search in sources :

Example 1 with Repository

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

the class RepositoryApiImpl method updateRepository.

@Override
public UpdateRepositoryResult updateRepository(UpdateRepositoryRequest updateRepositoryRequest) {
    Preconditions.checkNotNull(updateRepositoryRequest);
    final ObjectContext context = serverRuntime.newContext();
    Repository repository = getRepositoryOrThrow(context, updateRepositoryRequest.code);
    if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), repository, Permission.REPOSITORY_EDIT)) {
        throw new AccessDeniedException("unable to edit the repository [" + repository + "]");
    }
    for (UpdateRepositoryRequest.Filter filter : updateRepositoryRequest.filter) {
        switch(filter) {
            case ACTIVE:
                if (null == updateRepositoryRequest.active) {
                    throw new IllegalStateException("the active flag must be supplied");
                }
                if (repository.getActive() != updateRepositoryRequest.active) {
                    repository.setActive(updateRepositoryRequest.active);
                    LOGGER.info("did set the active flag on repository {} to {}", updateRepositoryRequest.code, updateRepositoryRequest.active);
                }
                if (!updateRepositoryRequest.active) {
                    for (RepositorySource repositorySource : repository.getRepositorySources()) {
                        if (repositorySource.getActive()) {
                            repositorySource.setActive(false);
                            LOGGER.info("did set the active flag on the repository source {} to false", repositorySource.getCode());
                        }
                    }
                }
                break;
            case NAME:
                if (null == updateRepositoryRequest.name) {
                    throw new IllegalStateException("the name must be supplied to update the repository");
                }
                String name = updateRepositoryRequest.name.trim();
                if (0 == name.length()) {
                    throw new ValidationException(new ValidationFailure(Repository.NAME.getName(), "invalid"));
                }
                repository.setName(name);
                break;
            case INFORMATIONURL:
                repository.setInformationUrl(updateRepositoryRequest.informationUrl);
                LOGGER.info("did set the information url on repository {} to {}", updateRepositoryRequest.code, updateRepositoryRequest.informationUrl);
                break;
            case PASSWORD:
                if (StringUtils.isBlank(updateRepositoryRequest.passwordClear)) {
                    repository.setPasswordSalt(null);
                    repository.setPasswordHash(null);
                    LOGGER.info("cleared the password for repository [{}]", repository);
                } else {
                    repositoryService.setPassword(repository, updateRepositoryRequest.passwordClear);
                    LOGGER.info("did update the repository [{}] password", repository);
                }
                break;
            default:
                throw new IllegalStateException("unhandled filter for updating a repository");
        }
    }
    if (context.hasChanges()) {
        context.commitChanges();
    } else {
        LOGGER.info("update repository {} with no changes made", updateRepositoryRequest.code);
    }
    return new UpdateRepositoryResult();
}
Also used : UpdateRepositoryRequest(org.haiku.haikudepotserver.api1.model.repository.UpdateRepositoryRequest) Repository(org.haiku.haikudepotserver.dataobjects.Repository) AccessDeniedException(org.springframework.security.access.AccessDeniedException) UpdateRepositoryResult(org.haiku.haikudepotserver.api1.model.repository.UpdateRepositoryResult) ValidationException(org.haiku.haikudepotserver.api1.support.ValidationException) RepositorySource(org.haiku.haikudepotserver.dataobjects.RepositorySource) ObjectContext(org.apache.cayenne.ObjectContext) ValidationFailure(org.haiku.haikudepotserver.api1.support.ValidationFailure)

Example 2 with Repository

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

the class RepositoryApiImpl method createRepositorySource.

@Override
public CreateRepositorySourceResult createRepositorySource(CreateRepositorySourceRequest request) {
    Preconditions.checkArgument(null != request, "the request must be supplied");
    Preconditions.checkArgument(!Strings.isNullOrEmpty(request.code), "the code for the new repository source must be supplied");
    Preconditions.checkArgument(!Strings.isNullOrEmpty(request.repositoryCode), "the repository for the new repository source must be identified");
    final ObjectContext context = serverRuntime.newContext();
    Repository repository = getRepositoryOrThrow(context, request.repositoryCode);
    if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), repository, Permission.REPOSITORY_EDIT)) {
        throw new AccessDeniedException("unable to edit the repository [" + repository + "]");
    }
    Optional<RepositorySource> existingRepositorySourceOptional = RepositorySource.tryGetByCode(context, request.code);
    if (existingRepositorySourceOptional.isPresent()) {
        throw new ValidationException(new ValidationFailure(RepositorySource.CODE.getName(), "unique"));
    }
    RepositorySource repositorySource = context.newObject(RepositorySource.class);
    repositorySource.setRepository(repository);
    repositorySource.setCode(request.code);
    repository.setModifyTimestamp();
    context.commitChanges();
    LOGGER.info("did create a new repository source '{}' on the repository '{}'", repositorySource, repository);
    return new CreateRepositorySourceResult();
}
Also used : Repository(org.haiku.haikudepotserver.dataobjects.Repository) AccessDeniedException(org.springframework.security.access.AccessDeniedException) ValidationException(org.haiku.haikudepotserver.api1.support.ValidationException) RepositorySource(org.haiku.haikudepotserver.dataobjects.RepositorySource) CreateRepositorySourceResult(org.haiku.haikudepotserver.api1.model.repository.CreateRepositorySourceResult) ObjectContext(org.apache.cayenne.ObjectContext) ValidationFailure(org.haiku.haikudepotserver.api1.support.ValidationFailure)

Example 3 with Repository

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

the class RepositoryApiImpl method createRepository.

@Override
public CreateRepositoryResult createRepository(CreateRepositoryRequest createRepositoryRequest) {
    Preconditions.checkNotNull(createRepositoryRequest);
    final ObjectContext context = serverRuntime.newContext();
    if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), null, Permission.REPOSITORY_ADD)) {
        throw new AccessDeniedException("unable to add a repository");
    }
    if (Strings.isNullOrEmpty(createRepositoryRequest.code)) {
        throw new ValidationException(new ValidationFailure(Repository.CODE.getName(), "required"));
    }
    // check to see if there is an existing repository with the same code; non-unique.
    {
        Optional<Repository> repositoryOptional = Repository.tryGetByCode(context, createRepositoryRequest.code);
        if (repositoryOptional.isPresent()) {
            throw new ValidationException(new ValidationFailure(Repository.CODE.getName(), "unique"));
        }
    }
    Repository repository = context.newObject(Repository.class);
    repository.setCode(createRepositoryRequest.code);
    repository.setName(createRepositoryRequest.name);
    repository.setInformationUrl(createRepositoryRequest.informationUrl);
    context.commitChanges();
    return new CreateRepositoryResult();
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) Repository(org.haiku.haikudepotserver.dataobjects.Repository) ValidationException(org.haiku.haikudepotserver.api1.support.ValidationException) Optional(java.util.Optional) ObjectContext(org.apache.cayenne.ObjectContext) CreateRepositoryResult(org.haiku.haikudepotserver.api1.model.repository.CreateRepositoryResult) ValidationFailure(org.haiku.haikudepotserver.api1.support.ValidationFailure)

Example 4 with Repository

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

the class RepositoryController method importRepositorySource.

/**
 * <p>Instructs HDS to import repository data for a repository source of
 * a repository.</p>
 */
@RequestMapping(value = "{" + KEY_REPOSITORYCODE + "}/" + SEGMENT_SOURCE + "/{" + KEY_REPOSITORYSOURCECODE + "}/" + SEGMENT_IMPORT, method = RequestMethod.POST)
public ResponseEntity<String> importRepositorySource(@PathVariable(value = KEY_REPOSITORYCODE) String repositoryCode, @PathVariable(value = KEY_REPOSITORYSOURCECODE) String repositorySourceCode) {
    ObjectContext context = serverRuntime.newContext();
    Optional<Repository> repositoryOptional = Repository.tryGetByCode(context, repositoryCode);
    if (repositoryOptional.isEmpty()) {
        return new ResponseEntity<>("repository not found", HttpStatus.NOT_FOUND);
    }
    Optional<RepositorySource> repositorySourceOptional = RepositorySource.tryGetByCode(context, repositorySourceCode);
    if (repositorySourceOptional.isEmpty() || !repositoryOptional.get().equals(repositorySourceOptional.get().getRepository())) {
        return new ResponseEntity<>("repository source not found", HttpStatus.NOT_FOUND);
    }
    if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), repositoryOptional.get(), Permission.REPOSITORY_IMPORT)) {
        throw new AccessDeniedException("unable to import repository [" + repositoryOptional.get() + "]");
    }
    jobService.submit(new RepositoryHpkrIngressJobSpecification(repositoryCode, Collections.singleton(repositorySourceCode)), JobSnapshot.COALESCE_STATUSES_QUEUED);
    return ResponseEntity.ok("repository source import submitted");
}
Also used : Repository(org.haiku.haikudepotserver.dataobjects.Repository) ResponseEntity(org.springframework.http.ResponseEntity) AccessDeniedException(org.springframework.security.access.AccessDeniedException) RepositoryHpkrIngressJobSpecification(org.haiku.haikudepotserver.repository.model.RepositoryHpkrIngressJobSpecification) RepositorySource(org.haiku.haikudepotserver.dataobjects.RepositorySource) ObjectContext(org.apache.cayenne.ObjectContext) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with Repository

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

Aggregations

Repository (org.haiku.haikudepotserver.dataobjects.Repository)30 ObjectContext (org.apache.cayenne.ObjectContext)26 List (java.util.List)9 RepositorySource (org.haiku.haikudepotserver.dataobjects.RepositorySource)9 AccessDeniedException (org.springframework.security.access.AccessDeniedException)9 Optional (java.util.Optional)8 Architecture (org.haiku.haikudepotserver.dataobjects.Architecture)8 ServerRuntime (org.apache.cayenne.configuration.server.ServerRuntime)7 AbstractIntegrationTest (org.haiku.haikudepotserver.AbstractIntegrationTest)7 RepositoryHpkrIngressJobSpecification (org.haiku.haikudepotserver.repository.model.RepositoryHpkrIngressJobSpecification)7 Test (org.junit.jupiter.api.Test)7 Logger (org.slf4j.Logger)7 LoggerFactory (org.slf4j.LoggerFactory)7 Preconditions (com.google.common.base.Preconditions)6 UpdateRepositoryRequest (org.haiku.haikudepotserver.api1.model.repository.UpdateRepositoryRequest)6 Pkg (org.haiku.haikudepotserver.dataobjects.Pkg)6 Component (org.springframework.stereotype.Component)6 ObjectId (org.apache.cayenne.ObjectId)5 StringUtils (org.apache.commons.lang3.StringUtils)5 OutputStream (java.io.OutputStream)4