Search in sources :

Example 21 with ObjectNotFoundException

use of org.haiku.haikudepotserver.api1.support.ObjectNotFoundException in project haikudepotserver by haiku.

the class RepositoryApiImpl method updateRepositorySourceMirror.

@Override
public UpdateRepositorySourceMirrorResult updateRepositorySourceMirror(UpdateRepositorySourceMirrorRequest request) {
    Preconditions.checkArgument(null != request, "the request must be supplied");
    Preconditions.checkArgument(!Strings.isNullOrEmpty(request.code), "the code for the mirror to update");
    final ObjectContext context = serverRuntime.newContext();
    RepositorySourceMirror repositorySourceMirror = RepositorySourceMirror.tryGetByCode(context, request.code).orElseThrow(() -> new ObjectNotFoundException(RepositorySourceMirror.class.getSimpleName(), request.code));
    if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), repositorySourceMirror.getRepositorySource().getRepository(), Permission.REPOSITORY_EDIT)) {
        throw new AccessDeniedException("the repository [" + repositorySourceMirror.getRepositorySource().getRepository() + "] is unable to be edited");
    }
    for (UpdateRepositorySourceMirrorRequest.Filter filter : CollectionUtils.emptyIfNull(request.filter)) {
        switch(filter) {
            case ACTIVE:
                if (repositorySourceMirror.getIsPrimary()) {
                    throw new ValidationException(new ValidationFailure(RepositorySourceMirror.ACTIVE.getName(), "confict"));
                }
                repositorySourceMirror.setActive(null != request.active && request.active);
                break;
            case BASE_URL:
                if (StringUtils.isBlank(request.baseUrl)) {
                    throw new ValidationException(new ValidationFailure(RepositorySourceMirror.BASE_URL.getName(), "required"));
                }
                if (!repositorySourceMirror.getBaseUrl().equals(request.baseUrl)) {
                    if (tryGetRepositorySourceMirrorObjectIdForBaseUrl(repositorySourceMirror.getRepositorySource().getCode(), request.baseUrl).isPresent()) {
                        throw new ValidationException(new ValidationFailure(RepositorySourceMirror.BASE_URL.getName(), "unique"));
                    }
                    repositorySourceMirror.setBaseUrl(request.baseUrl);
                }
                break;
            case COUNTRY:
                Country country = Country.tryGetByCode(context, request.countryCode).orElseThrow(() -> new ObjectNotFoundException(Country.class.getSimpleName(), request.countryCode));
                repositorySourceMirror.setCountry(country);
                break;
            case IS_PRIMARY:
                boolean isPrimary = null != request.isPrimary && request.isPrimary;
                if (isPrimary != repositorySourceMirror.getIsPrimary()) {
                    if (isPrimary) {
                        // in this case, the former primary should loose it's primary
                        // status so that it can be swapped to this one.
                        repositorySourceMirror.getRepositorySource().getPrimaryMirror().setIsPrimary(false);
                        repositorySourceMirror.setIsPrimary(true);
                    } else {
                        throw new ValidationException(new ValidationFailure(RepositorySourceMirror.IS_PRIMARY.getName(), "confict"));
                    }
                }
                break;
            case DESCRIPTION:
                repositorySourceMirror.setDescription(StringUtils.trimToNull(request.description));
                break;
            default:
                throw new IllegalStateException("unknown change filter for mirror [" + filter + "]");
        }
    }
    if (context.hasChanges()) {
        repositorySourceMirror.getRepositorySource().getRepository().setModifyTimestamp();
    }
    context.commitChanges();
    LOGGER.info("did update mirror [{}]", repositorySourceMirror.getCode());
    return new UpdateRepositorySourceMirrorResult();
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) org.haiku.haikudepotserver.dataobjects.auto._RepositorySourceMirror(org.haiku.haikudepotserver.dataobjects.auto._RepositorySourceMirror) RepositorySourceMirror(org.haiku.haikudepotserver.dataobjects.RepositorySourceMirror) ValidationException(org.haiku.haikudepotserver.api1.support.ValidationException) ObjectNotFoundException(org.haiku.haikudepotserver.api1.support.ObjectNotFoundException) UpdateRepositorySourceMirrorRequest(org.haiku.haikudepotserver.api1.model.repository.UpdateRepositorySourceMirrorRequest) UpdateRepositorySourceMirrorResult(org.haiku.haikudepotserver.api1.model.repository.UpdateRepositorySourceMirrorResult) Country(org.haiku.haikudepotserver.dataobjects.Country) ObjectContext(org.apache.cayenne.ObjectContext) ValidationFailure(org.haiku.haikudepotserver.api1.support.ValidationFailure)

Example 22 with ObjectNotFoundException

use of org.haiku.haikudepotserver.api1.support.ObjectNotFoundException in project haikudepotserver by haiku.

the class RepositoryApiImpl method removeRepositorySourceMirror.

@Override
public RemoveRepositorySourceMirrorResult removeRepositorySourceMirror(RemoveRepositorySourceMirrorRequest request) {
    Preconditions.checkArgument(null != request, "the request is required");
    Preconditions.checkArgument(StringUtils.isNotBlank(request.code), "the code is required on the request");
    final ObjectContext context = serverRuntime.newContext();
    RepositorySourceMirror repositorySourceMirror = RepositorySourceMirror.tryGetByCode(context, request.code).orElseThrow(() -> new ObjectNotFoundException(RepositorySourceMirror.class.getSimpleName(), request.code));
    if (repositorySourceMirror.getIsPrimary()) {
        throw new IllegalStateException("unable to remove the primary mirror");
    }
    repositorySourceMirror.getRepositorySource().getRepository().setModifyTimestamp();
    context.deleteObject(repositorySourceMirror);
    context.commitChanges();
    LOGGER.info("did remote the repository source mirror [{}]", request.code);
    return new RemoveRepositorySourceMirrorResult();
}
Also used : RemoveRepositorySourceMirrorResult(org.haiku.haikudepotserver.api1.model.repository.RemoveRepositorySourceMirrorResult) org.haiku.haikudepotserver.dataobjects.auto._RepositorySourceMirror(org.haiku.haikudepotserver.dataobjects.auto._RepositorySourceMirror) RepositorySourceMirror(org.haiku.haikudepotserver.dataobjects.RepositorySourceMirror) ObjectNotFoundException(org.haiku.haikudepotserver.api1.support.ObjectNotFoundException) ObjectContext(org.apache.cayenne.ObjectContext)

Example 23 with ObjectNotFoundException

use of org.haiku.haikudepotserver.api1.support.ObjectNotFoundException in project haikudepotserver by haiku.

the class RepositoryApiImpl method triggerImportRepository.

// note; no integration test for this one.
@Override
public TriggerImportRepositoryResult triggerImportRepository(TriggerImportRepositoryRequest triggerImportRepositoryRequest) {
    Preconditions.checkNotNull(triggerImportRepositoryRequest);
    Preconditions.checkState(!Strings.isNullOrEmpty(triggerImportRepositoryRequest.repositoryCode));
    Preconditions.checkArgument(null == triggerImportRepositoryRequest.repositorySourceCodes || !triggerImportRepositoryRequest.repositorySourceCodes.isEmpty(), "bad repository sources");
    final ObjectContext context = serverRuntime.newContext();
    Optional<Repository> repositoryOptional = Repository.tryGetByCode(context, triggerImportRepositoryRequest.repositoryCode);
    if (repositoryOptional.isEmpty()) {
        throw new ObjectNotFoundException(Repository.class.getSimpleName(), triggerImportRepositoryRequest.repositoryCode);
    }
    Repository repository = repositoryOptional.get();
    if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), repository, Permission.REPOSITORY_IMPORT)) {
        throw new AccessDeniedException("attempt to trigger repository import for [" + repository + "]");
    }
    Set<RepositorySource> repositorySources = null;
    if (null != triggerImportRepositoryRequest.repositorySourceCodes) {
        repositorySources = new HashSet<>();
        for (String repositorySourceCode : triggerImportRepositoryRequest.repositorySourceCodes) {
            repositorySources.add(repository.getRepositorySources().stream().filter(rs -> rs.getCode().equals(repositorySourceCode)).collect(SingleCollector.optional()).orElseThrow(() -> new ObjectNotFoundException(RepositorySource.class.getSimpleName(), repositorySourceCode)));
        }
    }
    jobService.submit(new RepositoryHpkrIngressJobSpecification(repository.getCode(), null == repositorySources ? null : repositorySources.stream().map(RepositorySource::getCode).collect(Collectors.toSet())), JobSnapshot.COALESCE_STATUSES_QUEUED);
    return new TriggerImportRepositoryResult();
}
Also used : GetRepositorySourceMirrorResult(org.haiku.haikudepotserver.api1.model.repository.GetRepositorySourceMirrorResult) CreateRepositoryResult(org.haiku.haikudepotserver.api1.model.repository.CreateRepositoryResult) CreateRepositorySourceRequest(org.haiku.haikudepotserver.api1.model.repository.CreateRepositorySourceRequest) RemoveRepositorySourceMirrorResult(org.haiku.haikudepotserver.api1.model.repository.RemoveRepositorySourceMirrorResult) LoggerFactory(org.slf4j.LoggerFactory) RepositorySourceExtraIdentifier(org.haiku.haikudepotserver.dataobjects.RepositorySourceExtraIdentifier) StringUtils(org.apache.commons.lang3.StringUtils) RepositoryService(org.haiku.haikudepotserver.repository.model.RepositoryService) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) UpdateRepositorySourceMirrorResult(org.haiku.haikudepotserver.api1.model.repository.UpdateRepositorySourceMirrorResult) org.haiku.haikudepotserver.dataobjects.auto._RepositorySourceMirror(org.haiku.haikudepotserver.dataobjects.auto._RepositorySourceMirror) PkgSearchSpecification(org.haiku.haikudepotserver.pkg.model.PkgSearchSpecification) SingleCollector(org.haiku.haikudepotserver.support.SingleCollector) RepositorySource(org.haiku.haikudepotserver.dataobjects.RepositorySource) RepositorySourceMirror(org.haiku.haikudepotserver.dataobjects.RepositorySourceMirror) Set(java.util.Set) SearchRepositoriesResult(org.haiku.haikudepotserver.api1.model.repository.SearchRepositoriesResult) UUID(java.util.UUID) GetRepositoriesResult(org.haiku.haikudepotserver.api1.model.repository.GetRepositoriesResult) UpdateRepositorySourceResult(org.haiku.haikudepotserver.api1.model.repository.UpdateRepositorySourceResult) Collectors(java.util.stream.Collectors) GetRepositorySourceMirrorRequest(org.haiku.haikudepotserver.api1.model.repository.GetRepositorySourceMirrorRequest) List(java.util.List) GetRepositoryRequest(org.haiku.haikudepotserver.api1.model.repository.GetRepositoryRequest) UpdateRepositoryRequest(org.haiku.haikudepotserver.api1.model.repository.UpdateRepositoryRequest) Repository(org.haiku.haikudepotserver.dataobjects.Repository) Optional(java.util.Optional) Country(org.haiku.haikudepotserver.dataobjects.Country) ObjectNotFoundException(org.haiku.haikudepotserver.api1.support.ObjectNotFoundException) UpdateRepositoryResult(org.haiku.haikudepotserver.api1.model.repository.UpdateRepositoryResult) TriggerImportRepositoryRequest(org.haiku.haikudepotserver.api1.model.repository.TriggerImportRepositoryRequest) ObjectContext(org.apache.cayenne.ObjectContext) Permission(org.haiku.haikudepotserver.security.model.Permission) ValidationException(org.haiku.haikudepotserver.api1.support.ValidationException) JobSnapshot(org.haiku.haikudepotserver.job.model.JobSnapshot) TriggerImportRepositoryResult(org.haiku.haikudepotserver.api1.model.repository.TriggerImportRepositoryResult) BooleanUtils(org.apache.commons.lang3.BooleanUtils) GetRepositorySourceResult(org.haiku.haikudepotserver.api1.model.repository.GetRepositorySourceResult) PersistentObject(org.apache.cayenne.PersistentObject) CreateRepositorySourceResult(org.haiku.haikudepotserver.api1.model.repository.CreateRepositorySourceResult) CollectionUtils(org.apache.commons.collections4.CollectionUtils) RemoveRepositorySourceMirrorRequest(org.haiku.haikudepotserver.api1.model.repository.RemoveRepositorySourceMirrorRequest) GetRepositoriesRequest(org.haiku.haikudepotserver.api1.model.repository.GetRepositoriesRequest) HashSet(java.util.HashSet) Strings(com.google.common.base.Strings) SetUtils(org.apache.commons.collections4.SetUtils) SearchRepositoriesRequest(org.haiku.haikudepotserver.api1.model.repository.SearchRepositoriesRequest) JobService(org.haiku.haikudepotserver.job.model.JobService) GetRepositoryResult(org.haiku.haikudepotserver.api1.model.repository.GetRepositoryResult) Logger(org.slf4j.Logger) UpdateRepositorySourceRequest(org.haiku.haikudepotserver.api1.model.repository.UpdateRepositorySourceRequest) AutoJsonRpcServiceImpl(com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImpl) Architecture(org.haiku.haikudepotserver.dataobjects.Architecture) AccessDeniedException(org.springframework.security.access.AccessDeniedException) PermissionEvaluator(org.springframework.security.access.PermissionEvaluator) CreateRepositorySourceMirrorRequest(org.haiku.haikudepotserver.api1.model.repository.CreateRepositorySourceMirrorRequest) RepositoryHpkrIngressJobSpecification(org.haiku.haikudepotserver.repository.model.RepositoryHpkrIngressJobSpecification) CreateRepositorySourceMirrorResult(org.haiku.haikudepotserver.api1.model.repository.CreateRepositorySourceMirrorResult) Component(org.springframework.stereotype.Component) CreateRepositoryRequest(org.haiku.haikudepotserver.api1.model.repository.CreateRepositoryRequest) ObjectId(org.apache.cayenne.ObjectId) UpdateRepositorySourceMirrorRequest(org.haiku.haikudepotserver.api1.model.repository.UpdateRepositorySourceMirrorRequest) Preconditions(com.google.common.base.Preconditions) RepositorySearchSpecification(org.haiku.haikudepotserver.repository.model.RepositorySearchSpecification) ValidationFailure(org.haiku.haikudepotserver.api1.support.ValidationFailure) ServerRuntime(org.apache.cayenne.configuration.server.ServerRuntime) Collections(java.util.Collections) GetRepositorySourceRequest(org.haiku.haikudepotserver.api1.model.repository.GetRepositorySourceRequest) Repository(org.haiku.haikudepotserver.dataobjects.Repository) AccessDeniedException(org.springframework.security.access.AccessDeniedException) RepositoryHpkrIngressJobSpecification(org.haiku.haikudepotserver.repository.model.RepositoryHpkrIngressJobSpecification) ObjectNotFoundException(org.haiku.haikudepotserver.api1.support.ObjectNotFoundException) RepositorySource(org.haiku.haikudepotserver.dataobjects.RepositorySource) ObjectContext(org.apache.cayenne.ObjectContext) TriggerImportRepositoryResult(org.haiku.haikudepotserver.api1.model.repository.TriggerImportRepositoryResult)

Example 24 with ObjectNotFoundException

use of org.haiku.haikudepotserver.api1.support.ObjectNotFoundException in project haikudepotserver by haiku.

the class RepositoryApiImpl method getRepositorySource.

@Override
public GetRepositorySourceResult getRepositorySource(GetRepositorySourceRequest request) {
    Preconditions.checkArgument(null != request);
    Preconditions.checkArgument(!Strings.isNullOrEmpty(request.code));
    final ObjectContext context = serverRuntime.newContext();
    Optional<RepositorySource> repositorySourceOptional = RepositorySource.tryGetByCode(context, request.code);
    if (repositorySourceOptional.isEmpty()) {
        throw new ObjectNotFoundException(RepositorySource.class.getSimpleName(), request.code);
    }
    RepositorySource repositorySource = repositorySourceOptional.get();
    GetRepositorySourceResult result = new GetRepositorySourceResult();
    result.active = repositorySource.getActive();
    result.code = repositorySource.getCode();
    result.repositoryCode = repositorySource.getRepository().getCode();
    result.identifier = repositorySource.getIdentifier();
    result.architectureCode = Optional.ofNullable(repositorySource.getArchitecture()).map(Architecture::getCode).orElse(null);
    if (null != repositorySource.getLastImportTimestamp()) {
        result.lastImportTimestamp = repositorySource.getLastImportTimestamp().getTime();
    }
    result.extraIdentifiers = repositorySource.getExtraIdentifiers();
    result.repositorySourceMirrors = repositorySource.getRepositorySourceMirrors().stream().filter(m -> m.getActive() || BooleanUtils.isTrue(request.includeInactiveRepositorySourceMirrors)).sorted().map(rsm -> {
        GetRepositorySourceResult.RepositorySourceMirror mirror = new GetRepositorySourceResult.RepositorySourceMirror();
        mirror.active = rsm.getActive();
        mirror.baseUrl = rsm.getBaseUrl();
        mirror.countryCode = rsm.getCountry().getCode();
        mirror.isPrimary = rsm.getIsPrimary();
        mirror.code = rsm.getCode();
        return mirror;
    }).collect(Collectors.toList());
    if (permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), repositorySource.getRepository(), Permission.REPOSITORY_EDIT)) {
        result.forcedInternalBaseUrl = repositorySourceOptional.get().getForcedInternalBaseUrl();
    }
    return result;
}
Also used : org.haiku.haikudepotserver.dataobjects.auto._RepositorySourceMirror(org.haiku.haikudepotserver.dataobjects.auto._RepositorySourceMirror) RepositorySourceMirror(org.haiku.haikudepotserver.dataobjects.RepositorySourceMirror) Architecture(org.haiku.haikudepotserver.dataobjects.Architecture) ObjectNotFoundException(org.haiku.haikudepotserver.api1.support.ObjectNotFoundException) RepositorySource(org.haiku.haikudepotserver.dataobjects.RepositorySource) ObjectContext(org.apache.cayenne.ObjectContext) GetRepositorySourceResult(org.haiku.haikudepotserver.api1.model.repository.GetRepositorySourceResult)

Example 25 with ObjectNotFoundException

use of org.haiku.haikudepotserver.api1.support.ObjectNotFoundException in project haikudepotserver by haiku.

the class UserRatingApiImpl method getUserRatingByUserAndPkgVersion.

@Override
public GetUserRatingByUserAndPkgVersionResult getUserRatingByUserAndPkgVersion(GetUserRatingByUserAndPkgVersionRequest request) {
    Preconditions.checkNotNull(request);
    Preconditions.checkState(!Strings.isNullOrEmpty(request.pkgName), "the package name must be supplied");
    Preconditions.checkState(!Strings.isNullOrEmpty(request.userNickname), "the user nickname must be supplied");
    Preconditions.checkState(!Strings.isNullOrEmpty(request.pkgVersionArchitectureCode), "the pkg version architecture code must be supplied");
    Preconditions.checkState(!Strings.isNullOrEmpty(request.pkgVersionMajor), "the package version major code must be supplied");
    Preconditions.checkArgument(!Strings.isNullOrEmpty(request.repositoryCode), "the repository code should be supplied");
    final ObjectContext context = serverRuntime.newContext();
    Architecture architecture = getArchitecture(context, request.pkgVersionArchitectureCode);
    User user = User.tryGetByNickname(context, request.userNickname).orElseThrow(() -> new ObjectNotFoundException(User.class.getSimpleName(), request.userNickname));
    Pkg pkg = Pkg.tryGetByName(context, request.pkgName).orElseThrow(() -> new ObjectNotFoundException(Pkg.class.getSimpleName(), request.pkgName));
    Repository repository = getRepository(context, request.repositoryCode);
    VersionCoordinates versionCoordinates = new VersionCoordinates(request.pkgVersionMajor, request.pkgVersionMinor, request.pkgVersionMicro, request.pkgVersionPreRelease, request.pkgVersionRevision);
    Optional<PkgVersion> pkgVersionOptional = PkgVersion.getForPkg(context, pkg, repository, architecture, versionCoordinates);
    if (pkgVersionOptional.isEmpty() || !pkgVersionOptional.get().getActive()) {
        throw new ObjectNotFoundException(PkgVersion.class.getSimpleName(), request.pkgName + "@" + versionCoordinates.toString());
    }
    Optional<UserRating> userRatingOptional = UserRating.getByUserAndPkgVersion(context, user, pkgVersionOptional.get());
    if (userRatingOptional.isEmpty()) {
        throw new ObjectNotFoundException(UserRating.class.getSimpleName(), "");
    }
    GetUserRatingByUserAndPkgVersionResult result = new GetUserRatingByUserAndPkgVersionResult();
    fillAbstractGetUserRatingResult(userRatingOptional.get(), result);
    return result;
}
Also used : VersionCoordinates(org.haiku.haikudepotserver.support.VersionCoordinates) ObjectNotFoundException(org.haiku.haikudepotserver.api1.support.ObjectNotFoundException) org.haiku.haikudepotserver.dataobjects.auto._PkgVersion(org.haiku.haikudepotserver.dataobjects.auto._PkgVersion) ObjectContext(org.apache.cayenne.ObjectContext)

Aggregations

ObjectNotFoundException (org.haiku.haikudepotserver.api1.support.ObjectNotFoundException)32 ObjectContext (org.apache.cayenne.ObjectContext)28 AccessDeniedException (org.springframework.security.access.AccessDeniedException)17 org.haiku.haikudepotserver.dataobjects.auto._PkgVersion (org.haiku.haikudepotserver.dataobjects.auto._PkgVersion)7 RepositorySourceMirror (org.haiku.haikudepotserver.dataobjects.RepositorySourceMirror)6 org.haiku.haikudepotserver.dataobjects.auto._RepositorySourceMirror (org.haiku.haikudepotserver.dataobjects.auto._RepositorySourceMirror)6 User (org.haiku.haikudepotserver.dataobjects.User)5 Preconditions (com.google.common.base.Preconditions)3 Strings (com.google.common.base.Strings)3 AutoJsonRpcServiceImpl (com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImpl)3 Collectors (java.util.stream.Collectors)3 ServerRuntime (org.apache.cayenne.configuration.server.ServerRuntime)3 StringUtils (org.apache.commons.lang3.StringUtils)3 ValidationException (org.haiku.haikudepotserver.api1.support.ValidationException)3 ValidationFailure (org.haiku.haikudepotserver.api1.support.ValidationFailure)3 Country (org.haiku.haikudepotserver.dataobjects.Country)3 PkgScreenshot (org.haiku.haikudepotserver.dataobjects.PkgScreenshot)3 RepositorySource (org.haiku.haikudepotserver.dataobjects.RepositorySource)3 org.haiku.haikudepotserver.dataobjects.auto._User (org.haiku.haikudepotserver.dataobjects.auto._User)3 Logger (org.slf4j.Logger)3