Search in sources :

Example 31 with ObjectNotFoundException

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

the class JobApiImpl method getJob.

@Override
public GetJobResult getJob(GetJobRequest request) {
    Preconditions.checkArgument(null != request);
    Preconditions.checkArgument(!Strings.isNullOrEmpty(request.guid));
    final ObjectContext context = serverRuntime.newContext();
    JobSnapshot job = jobService.tryGetJob(request.guid).orElseThrow(() -> new ObjectNotFoundException(JobSnapshot.class.getSimpleName(), request.guid));
    if (Strings.isNullOrEmpty(job.getOwnerUserNickname())) {
        if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), null, Permission.JOBS_VIEW)) {
            throw new AccessDeniedException("attempt to access jobs view");
        }
    } else {
        User ownerUser = User.tryGetByNickname(context, job.getOwnerUserNickname()).orElseThrow(() -> new ObjectNotFoundException(User.class.getSimpleName(), job.getOwnerUserNickname()));
        if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), ownerUser, Permission.USER_VIEWJOBS)) {
            throw new AccessDeniedException("attempt to access jobs view for [" + job + "]");
        }
    }
    // now output the result.
    GetJobResult result = new GetJobResult();
    result.guid = job.getGuid();
    result.jobStatus = JobStatus.valueOf(job.getStatus().name());
    result.jobTypeCode = job.getJobTypeCode();
    result.ownerUserNickname = job.getOwnerUserNickname();
    result.startTimestamp = null == job.getStartTimestamp() ? null : job.getStartTimestamp().getTime();
    result.finishTimestamp = null == job.getFinishTimestamp() ? null : job.getFinishTimestamp().getTime();
    result.queuedTimestamp = null == job.getQueuedTimestamp() ? null : job.getQueuedTimestamp().getTime();
    result.failTimestamp = null == job.getFailTimestamp() ? null : job.getFailTimestamp().getTime();
    result.cancelTimestamp = null == job.getCancelTimestamp() ? null : job.getCancelTimestamp().getTime();
    result.progressPercent = job.getProgressPercent();
    result.generatedDatas = new ArrayList<>();
    for (String guid : job.getGeneratedDataGuids()) {
        JobData jobData = jobService.tryGetData(guid).orElseThrow(() -> new ObjectNotFoundException(JobData.class.getSimpleName(), guid));
        GetJobResult.JobData resultJobData = new GetJobResult.JobData();
        resultJobData.useCode = jobData.getUseCode();
        resultJobData.guid = jobData.getGuid();
        resultJobData.mediaTypeCode = jobData.getMediaTypeCode();
        resultJobData.filename = jobService.deriveDataFilename(guid);
        result.generatedDatas.add(resultJobData);
    }
    return result;
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) User(org.haiku.haikudepotserver.dataobjects.User) ObjectNotFoundException(org.haiku.haikudepotserver.api1.support.ObjectNotFoundException) JobSnapshot(org.haiku.haikudepotserver.job.model.JobSnapshot) ObjectContext(org.apache.cayenne.ObjectContext) JobData(org.haiku.haikudepotserver.job.model.JobData)

Example 32 with ObjectNotFoundException

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

the class JobApiImpl method searchJobs.

@Override
public SearchJobsResult searchJobs(SearchJobsRequest request) {
    Preconditions.checkArgument(null != request);
    final SearchJobsResult result = new SearchJobsResult();
    final ObjectContext context = serverRuntime.newContext();
    User ownerUser = null;
    if (Strings.isNullOrEmpty(request.ownerUserNickname)) {
        if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), null, Permission.JOBS_VIEW)) {
            throw new AccessDeniedException("attempt to access jobs view");
        }
    } else {
        ownerUser = User.tryGetByNickname(context, request.ownerUserNickname).orElseThrow(() -> new ObjectNotFoundException(User.class.getSimpleName(), request.ownerUserNickname));
        if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), ownerUser, Permission.USER_VIEWJOBS)) {
            throw new AccessDeniedException("attempt to access jobs view for [" + ownerUser + "]");
        }
    }
    if (null != request.statuses && request.statuses.isEmpty()) {
        result.items = Collections.emptyList();
        result.total = 0L;
    } else {
        Set<JobSnapshot.Status> statuses = null;
        if (null != request.statuses) {
            statuses = request.statuses.stream().map(s -> JobSnapshot.Status.valueOf(s.name())).collect(Collectors.toSet());
        }
        result.total = (long) jobService.totalJobs(ownerUser, statuses);
        result.items = jobService.findJobs(ownerUser, statuses, null == request.offset ? 0 : request.offset, null == request.limit ? Integer.MAX_VALUE : request.limit).stream().map(js -> {
            SearchJobsResult.Job resultJob = new SearchJobsResult.Job();
            resultJob.guid = js.getGuid();
            resultJob.jobStatus = JobStatus.valueOf(js.getStatus().name());
            resultJob.jobTypeCode = js.getJobTypeCode();
            resultJob.ownerUserNickname = js.getOwnerUserNickname();
            resultJob.startTimestamp = null == js.getStartTimestamp() ? null : js.getStartTimestamp().getTime();
            resultJob.finishTimestamp = null == js.getFinishTimestamp() ? null : js.getFinishTimestamp().getTime();
            resultJob.queuedTimestamp = null == js.getQueuedTimestamp() ? null : js.getQueuedTimestamp().getTime();
            resultJob.failTimestamp = null == js.getFailTimestamp() ? null : js.getFailTimestamp().getTime();
            resultJob.cancelTimestamp = null == js.getCancelTimestamp() ? null : js.getCancelTimestamp().getTime();
            resultJob.progressPercent = js.getProgressPercent();
            return resultJob;
        }).collect(Collectors.toList());
        LOGGER.info("search for jobs found {} results", result.items.size());
    }
    return result;
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) User(org.haiku.haikudepotserver.dataobjects.User) ObjectNotFoundException(org.haiku.haikudepotserver.api1.support.ObjectNotFoundException) ObjectContext(org.apache.cayenne.ObjectContext)

Example 33 with ObjectNotFoundException

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

the class PkgApiImpl method getPkg.

@Override
public GetPkgResult getPkg(GetPkgRequest request) {
    Preconditions.checkNotNull(request);
    Preconditions.checkState(!Strings.isNullOrEmpty(request.name), "request pkg name is required");
    Preconditions.checkNotNull(request.versionType);
    Preconditions.checkState(!Strings.isNullOrEmpty(request.naturalLanguageCode));
    Preconditions.checkArgument(EnumSet.of(PkgVersionType.NONE, PkgVersionType.ALL).contains(request.versionType) || !Strings.isNullOrEmpty(request.repositoryCode), "the repository code should be supplied of the version request is not ALL or NONE");
    final ObjectContext context = serverRuntime.newContext();
    Optional<Architecture> architectureOptional = Optional.empty();
    if (!Strings.isNullOrEmpty(request.architectureCode)) {
        architectureOptional = Architecture.tryGetByCode(context, request.architectureCode);
    }
    Pkg pkg = getPkg(context, request.name);
    Repository repository = null;
    if (!Strings.isNullOrEmpty(request.repositoryCode)) {
        repository = getRepository(context, request.repositoryCode);
    }
    final NaturalLanguage naturalLanguage = getNaturalLanguage(context, request.naturalLanguageCode);
    GetPkgResult result = new GetPkgResult();
    result.name = pkg.getName();
    result.modifyTimestamp = pkg.getModifyTimestamp().getTime();
    result.vanityLinkUrl = pkgService.createVanityLinkUrl(pkg);
    result.hasChangelog = pkg.getPkgSupplement().getPkgChangelog().isPresent();
    result.pkgCategoryCodes = pkg.getPkgSupplement().getPkgPkgCategories().stream().map(ppc -> ppc.getPkgCategory().getCode()).collect(Collectors.toList());
    if (null != repository) {
        Optional<PkgUserRatingAggregate> userRatingAggregate = pkg.getPkgUserRatingAggregate(repository);
        if (userRatingAggregate.isPresent()) {
            result.derivedRating = userRatingAggregate.get().getDerivedRating();
            result.derivedRatingSampleSize = userRatingAggregate.get().getDerivedRatingSampleSize();
        }
    }
    if (null != repository) {
        result.prominenceOrdering = pkg.getPkgProminence(repository).map(pp -> pp.getProminence().getOrdering()).orElse(null);
    }
    switch(request.versionType) {
        case ALL:
            {
                List<PkgVersion> allVersions;
                if (null == repository) {
                    // active only
                    allVersions = PkgVersion.getForPkg(context, pkg, false);
                } else {
                    // active only
                    allVersions = PkgVersion.getForPkg(context, pkg, repository, false);
                }
                if (architectureOptional.isPresent()) {
                    final Architecture a = architectureOptional.get();
                    allVersions = allVersions.stream().filter(v -> v.getArchitecture().equals(a)).collect(Collectors.toList());
                }
                // now sort those.
                final VersionCoordinatesComparator vcc = new VersionCoordinatesComparator();
                Collections.sort(allVersions, (pv1, pv2) -> ComparisonChain.start().compare(pv1.getArchitecture().getCode(), pv2.getArchitecture().getCode()).compare(pv1.toVersionCoordinates(), pv2.toVersionCoordinates(), vcc).result());
                result.versions = allVersions.stream().map(v -> createGetPkgResultPkgVersion(context, v, naturalLanguage)).collect(Collectors.toList());
            }
            break;
        case SPECIFIC:
            {
                if (architectureOptional.isEmpty()) {
                    throw new IllegalStateException("the specified architecture was not able to be found; " + request.architectureCode);
                }
                VersionCoordinates coordinates = new VersionCoordinates(request.major, request.minor, request.micro, request.preRelease, request.revision);
                PkgVersion pkgVersion = PkgVersion.getForPkg(context, pkg, repository, architectureOptional.get(), coordinates).filter(_PkgVersion::getActive).orElseThrow(() -> new ObjectNotFoundException(PkgVersion.class.getSimpleName(), ""));
                if (null != request.incrementViewCounter && request.incrementViewCounter) {
                    incrementCounter(pkgVersion);
                }
                result.versions = Collections.singletonList(createGetPkgResultPkgVersion(context, pkgVersion, naturalLanguage));
            }
            break;
        case LATEST:
            {
                if (architectureOptional.isEmpty()) {
                    throw new IllegalStateException("the specified architecture was not able to be found; " + request.architectureCode);
                }
                PkgVersion pkgVersion = pkgService.getLatestPkgVersionForPkg(context, pkg, repository, ImmutableList.of(architectureOptional.get(), Architecture.tryGetByCode(context, Architecture.CODE_ANY).get(), Architecture.tryGetByCode(context, Architecture.CODE_SOURCE).get())).orElseThrow(() -> new ObjectNotFoundException(PkgVersion.class.getSimpleName(), request.name));
                if (null != request.incrementViewCounter && request.incrementViewCounter) {
                    incrementCounter(pkgVersion);
                }
                result.versions = Collections.singletonList(createGetPkgResultPkgVersion(context, pkgVersion, naturalLanguage));
            }
            break;
        case // no version is actually required.
        NONE:
            break;
        default:
            throw new IllegalStateException("unhandled version type in request");
    }
    return result;
}
Also used : PkgVersionUrl(org.haiku.haikudepotserver.api1.model.pkg.PkgVersionUrl) ObjectContext(org.apache.cayenne.ObjectContext) java.util(java.util) Permission(org.haiku.haikudepotserver.security.model.Permission) org.haiku.haikudepotserver.support(org.haiku.haikudepotserver.support) PkgIcon(org.haiku.haikudepotserver.api1.model.pkg.PkgIcon) FixedPkgLocalizationLookupServiceImpl(org.haiku.haikudepotserver.pkg.FixedPkgLocalizationLookupServiceImpl) LoggerFactory(org.slf4j.LoggerFactory) PkgLocalization(org.haiku.haikudepotserver.dataobjects.PkgLocalization) BadPkgIconException(org.haiku.haikudepotserver.api1.support.BadPkgIconException) StringUtils(org.apache.commons.lang3.StringUtils) Strings(com.google.common.base.Strings) ImmutableList(com.google.common.collect.ImmutableList) org.haiku.haikudepotserver.dataobjects.auto._PkgVersion(org.haiku.haikudepotserver.dataobjects.auto._PkgVersion) ByteArrayInputStream(java.io.ByteArrayInputStream) PkgVersionLocalization(org.haiku.haikudepotserver.dataobjects.PkgVersionLocalization) org.haiku.haikudepotserver.pkg.model(org.haiku.haikudepotserver.pkg.model) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) org.haiku.haikudepotserver.api1.model.pkg(org.haiku.haikudepotserver.api1.model.pkg) Logger(org.slf4j.Logger) AutoJsonRpcServiceImpl(com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImpl) PermissionEvaluator(org.haiku.haikudepotserver.security.PermissionEvaluator) IOException(java.io.IOException) AccessDeniedException(org.springframework.security.access.AccessDeniedException) org.haiku.haikudepotserver.dataobjects.auto._PkgUserRatingAggregate(org.haiku.haikudepotserver.dataobjects.auto._PkgUserRatingAggregate) ComparisonChain(com.google.common.collect.ComparisonChain) Collectors(java.util.stream.Collectors) PkgVersionType(org.haiku.haikudepotserver.api1.model.PkgVersionType) PkgScreenshot(org.haiku.haikudepotserver.dataobjects.PkgScreenshot) org.haiku.haikudepotserver.dataobjects(org.haiku.haikudepotserver.dataobjects) TimeUnit(java.util.concurrent.TimeUnit) Component(org.springframework.stereotype.Component) Stream(java.util.stream.Stream) Preconditions(com.google.common.base.Preconditions) CacheBuilder(com.google.common.cache.CacheBuilder) Cache(com.google.common.cache.Cache) ServerRuntime(org.apache.cayenne.configuration.server.ServerRuntime) ObjectNotFoundException(org.haiku.haikudepotserver.api1.support.ObjectNotFoundException) org.haiku.haikudepotserver.dataobjects.auto._PkgUserRatingAggregate(org.haiku.haikudepotserver.dataobjects.auto._PkgUserRatingAggregate) ObjectNotFoundException(org.haiku.haikudepotserver.api1.support.ObjectNotFoundException) org.haiku.haikudepotserver.dataobjects.auto._PkgVersion(org.haiku.haikudepotserver.dataobjects.auto._PkgVersion) org.haiku.haikudepotserver.dataobjects.auto._PkgVersion(org.haiku.haikudepotserver.dataobjects.auto._PkgVersion) ImmutableList(com.google.common.collect.ImmutableList) 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