Search in sources :

Example 11 with ServiceProvider

use of com.b2international.snowowl.core.ServiceProvider in project snow-owl by b2ihealthcare.

the class DefaultResourceURIPathResolver method resolveWithVersion.

@Override
public PathWithVersion resolveWithVersion(ServiceProvider context, ResourceURI uriToResolve, Resource resource) {
    if (resource instanceof TerminologyResource) {
        TerminologyResource terminologyResource = (TerminologyResource) resource;
        if (uriToResolve.isHead()) {
            // use code system working branch directly when HEAD is specified
            final String workingBranchPath = terminologyResource.getBranchPath() + uriToResolve.getTimestampPart();
            return new PathWithVersion(workingBranchPath);
        }
        // prevent running version search if path does not look like a versionId (single path segment)
        final String relativeBranchPath = terminologyResource.getRelativeBranchPath(uriToResolve.getPath());
        if (uriToResolve.getPath().contains(Branch.SEPARATOR)) {
            final String absoluteBranchPath = relativeBranchPath + uriToResolve.getTimestampPart();
            return new PathWithVersion(absoluteBranchPath);
        }
        VersionSearchRequestBuilder versionSearch = ResourceRequests.prepareSearchVersion().one().filterByResource(terminologyResource.getResourceURI());
        if (uriToResolve.isLatest()) {
            // fetch the latest resource version if LATEST is specified in the URI
            versionSearch.sortBy(SearchResourceRequest.Sort.fieldDesc(VersionDocument.Fields.EFFECTIVE_TIME));
        } else {
            // try to fetch the path as exact version if not the special LATEST is specified in the URI
            versionSearch.filterByVersionId(uriToResolve.getPath());
        }
        // determine the final branch path, if based on the version search we find a version, then use that, otherwise use the defined path as relative branch of the code system working branch
        Versions versions = versionSearch.buildAsync().getRequest().execute(context);
        return versions.first().map(v -> {
            final String versionBranchPath = v.getBranchPath() + uriToResolve.getTimestampPart();
            final ResourceURI versionResourceURI = v.getVersionResourceURI().withTimestampPart(uriToResolve.getTimestampPart());
            return new PathWithVersion(versionBranchPath, versionResourceURI);
        }).orElseGet(() -> {
            if (uriToResolve.isLatest() || !allowBranches) {
                throw new BadRequestException("No Resource version is present in '%s'. Explicit '%s' can be used to retrieve the latest work in progress version of the Resource.", terminologyResource.getId(), terminologyResource.getId());
            }
            return new PathWithVersion(relativeBranchPath);
        });
    }
    return new PathWithVersion("");
}
Also used : VersionSearchRequestBuilder(com.b2international.snowowl.core.request.version.VersionSearchRequestBuilder) BadRequestException(com.b2international.commons.exceptions.BadRequestException) TerminologyResource(com.b2international.snowowl.core.TerminologyResource) ResourceRequests(com.b2international.snowowl.core.request.ResourceRequests) VersionSearchRequestBuilder(com.b2international.snowowl.core.request.version.VersionSearchRequestBuilder) Set(java.util.Set) Collectors(java.util.stream.Collectors) Branch(com.b2international.snowowl.core.branch.Branch) SearchResourceRequest(com.b2international.snowowl.core.request.SearchResourceRequest) Resource(com.b2international.snowowl.core.Resource) List(java.util.List) VersionDocument(com.b2international.snowowl.core.version.VersionDocument) Map(java.util.Map) ServiceProvider(com.b2international.snowowl.core.ServiceProvider) CompareUtils(com.b2international.commons.CompareUtils) Collections(java.util.Collections) Versions(com.b2international.snowowl.core.version.Versions) ResourceURI(com.b2international.snowowl.core.ResourceURI) ResourceURI(com.b2international.snowowl.core.ResourceURI) Versions(com.b2international.snowowl.core.version.Versions) TerminologyResource(com.b2international.snowowl.core.TerminologyResource) BadRequestException(com.b2international.commons.exceptions.BadRequestException)

Example 12 with ServiceProvider

use of com.b2international.snowowl.core.ServiceProvider in project snow-owl by b2ihealthcare.

the class FhirValidateCodeRequest method doExecute.

@Override
public ValidateCodeResult doExecute(ServiceProvider context, CodeSystem codeSystem) {
    Set<Coding> codings = collectCodingsToValidate(request);
    Map<String, Coding> codingsById = codings.stream().collect(Collectors.toMap(Coding::getCodeValue, c -> c));
    // extract locales from the request
    Map<String, Concept> conceptsById = CodeSystemRequests.prepareSearchConcepts().setLimit(codingsById.keySet().size()).filterByCodeSystemUri(codeSystem.getResourceURI()).filterByIds(codingsById.keySet()).setLocales(extractLocales(request.getDisplayLanguage())).buildAsync().execute(context).stream().collect(Collectors.toMap(Concept::getId, c -> c));
    // check if both Maps have the same keys and report if not
    Set<String> missingConceptIds = Sets.difference(codingsById.keySet(), conceptsById.keySet());
    if (!missingConceptIds.isEmpty()) {
        return ValidateCodeResult.builder().result(false).message(String.format("Could not find code%s '%s'.", missingConceptIds.size() == 1 ? "" : "s", ImmutableSortedSet.copyOf(missingConceptIds))).build();
    }
    // XXX it would be great to have support for multiple messages/validation results in a single request
    for (String id : codingsById.keySet()) {
        // check display if provided
        Coding providedCoding = codingsById.get(id);
        if (providedCoding.getDisplay() != null) {
            Concept concept = conceptsById.get(id);
            if (!providedCoding.getDisplay().equals(concept.getTerm())) {
                return ValidateCodeResult.builder().result(false).display(concept.getTerm()).message(String.format("Incorrect display '%s' for code '%s'.", providedCoding.getDisplay(), providedCoding.getCodeValue())).build();
            }
        }
    }
    return ValidateCodeResult.builder().result(true).build();
}
Also used : ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) ValidateCodeRequest(com.b2international.snowowl.fhir.core.model.codesystem.ValidateCodeRequest) Set(java.util.Set) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) HashSet(java.util.HashSet) Valid(javax.validation.Valid) CodeSystem(com.b2international.snowowl.fhir.core.model.codesystem.CodeSystem) Concept(com.b2international.snowowl.core.domain.Concept) Map(java.util.Map) ServiceProvider(com.b2international.snowowl.core.ServiceProvider) JsonUnwrapped(com.fasterxml.jackson.annotation.JsonUnwrapped) ValidateCodeResult(com.b2international.snowowl.fhir.core.model.ValidateCodeResult) CodeSystemRequests(com.b2international.snowowl.core.codesystem.CodeSystemRequests) Coding(com.b2international.snowowl.fhir.core.model.dt.Coding) CodeableConcept(com.b2international.snowowl.fhir.core.model.dt.CodeableConcept) Concept(com.b2international.snowowl.core.domain.Concept) CodeableConcept(com.b2international.snowowl.fhir.core.model.dt.CodeableConcept) Coding(com.b2international.snowowl.fhir.core.model.dt.Coding)

Example 13 with ServiceProvider

use of com.b2international.snowowl.core.ServiceProvider in project snow-owl by b2ihealthcare.

the class ApiRequestHandler method handle.

@Override
public final void handle(IMessage message) {
    try {
        final Request<ServiceProvider, ?> req = message.body(Request.class, classLoader);
        final ResponseHeaders responseHeaders = new ResponseHeaders();
        final ServiceProvider executionContext = context.inject().bind(RequestHeaders.class, new RequestHeaders(message.headers())).bind(ResponseHeaders.class, responseHeaders).build();
        // monitor each request execution
        final Object body = new MonitoredRequest<>(// authorize each request execution
        new AuthorizedRequest<>(// rate limit all requests
        new RateLimitingRequest<>(// actual request
        req))).execute(executionContext);
        if (body == null) {
            LoggerFactory.getLogger(ApiRequestHandler.class).error("No response was returned from request: " + req.getClass());
        }
        message.reply(body, responseHeaders.headers());
    } catch (WrappedException e) {
        message.fail(e.getCause());
    } catch (ApiException e) {
        message.fail(e);
    } catch (Throwable e) {
        LoggerFactory.getLogger(ApiRequestHandler.class).error("Unexpected error when executing request:", e);
        message.fail(e);
    }
}
Also used : WrappedException(org.eclipse.emf.common.util.WrappedException) AuthorizedRequest(com.b2international.snowowl.core.authorization.AuthorizedRequest) ServiceProvider(com.b2international.snowowl.core.ServiceProvider) ResponseHeaders(com.b2international.snowowl.core.events.util.ResponseHeaders) RequestHeaders(com.b2international.snowowl.core.events.util.RequestHeaders) ApiException(com.b2international.commons.exceptions.ApiException)

Example 14 with ServiceProvider

use of com.b2international.snowowl.core.ServiceProvider in project snow-owl by b2ihealthcare.

the class BaseResourceSearchRequest method addSecurityFilter.

/**
 * Configures security filters to allow access to certain resources only. This method is no-op if the given {@link ServiceProvider context}'s {@link User} is an administrator or has read access to everything.
 *
 * @param context - the context where user information will be extracted
 * @param queryBuilder - the query builder to append the clauses to
 */
protected final void addSecurityFilter(ServiceProvider context, ExpressionBuilder queryBuilder) {
    final User user = context.service(User.class);
    if (user.isAdministrator() || user.hasPermission(Permission.requireAll(Permission.OPERATION_BROWSE, Permission.ALL))) {
        return;
    }
    // extract read permissions
    final List<Permission> readPermissions = user.getPermissions().stream().filter(p -> Permission.ALL.equals(p.getOperation()) || Permission.OPERATION_BROWSE.equals(p.getOperation())).collect(Collectors.toList());
    final Set<String> exactResourceIds = readPermissions.stream().flatMap(p -> p.getResources().stream()).filter(resource -> !resource.endsWith("*")).collect(Collectors.toSet());
    final Set<String> resourceIdPrefixes = readPermissions.stream().flatMap(p -> p.getResources().stream()).filter(resource -> resource.endsWith("*")).map(resource -> resource.substring(0, resource.length() - 1)).collect(Collectors.toSet());
    if (!exactResourceIds.isEmpty() || !resourceIdPrefixes.isEmpty()) {
        context.log().info("Restricting user '{}' to resources exact: '{}', prefix: '{}'.", user.getUsername(), ImmutableSortedSet.copyOf(exactResourceIds), ImmutableSortedSet.copyOf(resourceIdPrefixes));
        ExpressionBuilder bool = Expressions.builder();
        // the permissions give access to either
        if (!exactResourceIds.isEmpty()) {
            // explicit IDs
            bool.should(ResourceDocument.Expressions.ids(exactResourceIds));
            // or the permitted resources are bundles which give access to all resources within it (recursively)
            bool.should(ResourceDocument.Expressions.bundleIds(exactResourceIds));
            bool.should(ResourceDocument.Expressions.bundleAncestorIds(exactResourceIds));
        }
        if (!resourceIdPrefixes.isEmpty()) {
            // partial IDs, prefixes
            bool.should(ResourceDocument.Expressions.idPrefixes(resourceIdPrefixes));
            // or the permitted resources are bundle ID prefixes which give access to all resources within it (recursively)
            bool.should(ResourceDocument.Expressions.bundleIdPrefixes(resourceIdPrefixes));
            bool.should(ResourceDocument.Expressions.bundleAncestorIdPrefixes(resourceIdPrefixes));
        }
        queryBuilder.filter(bool.build());
    } else {
        throw new NoResultException();
    }
}
Also used : ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) RepositoryContext(com.b2international.snowowl.core.domain.RepositoryContext) Collection(java.util.Collection) Set(java.util.Set) Collectors(java.util.stream.Collectors) ResourceDocument(com.b2international.snowowl.core.internal.ResourceDocument) List(java.util.List) Expressions(com.b2international.index.query.Expressions) ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder) ServiceProvider(com.b2international.snowowl.core.ServiceProvider) Expression(com.b2international.index.query.Expression) Permission(com.b2international.snowowl.core.identity.Permission) User(com.b2international.snowowl.core.identity.User) User(com.b2international.snowowl.core.identity.User) Permission(com.b2international.snowowl.core.identity.Permission) ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder)

Example 15 with ServiceProvider

use of com.b2international.snowowl.core.ServiceProvider in project snow-owl by b2ihealthcare.

the class ValueSetMemberSearchRequest method doExecute.

@Override
protected ValueSetMembers doExecute(ServiceProvider context) throws IOException {
    final int limit = limit();
    Options options = Options.builder().putAll(options()).put(MemberSearchRequestEvaluator.OptionKey.AFTER, searchAfter()).put(MemberSearchRequestEvaluator.OptionKey.LIMIT, limit).put(MemberSearchRequestEvaluator.OptionKey.LOCALES, locales()).put(SearchResourceRequest.OptionKey.SORT_BY, sortBy()).build();
    // extract all ValueSetMemberSearchRequestEvaluator from all connected toolings and determine which ones can handle this request
    List<ValueSetMembers> evaluatedMembers = context.service(RepositoryManager.class).repositories().stream().flatMap(repository -> {
        ValueSetMemberSearchRequestEvaluator evaluator = repository.service(ValueSetMemberSearchRequestEvaluator.class);
        Set<ResourceURI> targets = evaluator.evaluateSearchTargetResources(context, options);
        return targets.stream().map(uri -> {
            return evaluator.evaluate(uri, context, options);
        });
    }).collect(Collectors.toList());
    // calculate grand total
    int total = 0;
    for (ValueSetMembers evaluatedMember : evaluatedMembers) {
        total += evaluatedMember.getTotal();
    }
    return new ValueSetMembers(// TODO add manual sorting here if multiple resources have been fetched
    evaluatedMembers.stream().flatMap(ValueSetMembers::stream).limit(limit).collect(Collectors.toList()), null, /* not supported across resources, TODO support it when a single ValueSet is being fetched */
    limit, total);
}
Also used : List(java.util.List) Options(com.b2international.commons.options.Options) RepositoryManager(com.b2international.snowowl.core.RepositoryManager) ServiceProvider(com.b2international.snowowl.core.ServiceProvider) Set(java.util.Set) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) ValueSetMembers(com.b2international.snowowl.core.domain.ValueSetMembers) ResourceURI(com.b2international.snowowl.core.ResourceURI) Options(com.b2international.commons.options.Options) Set(java.util.Set) ValueSetMembers(com.b2international.snowowl.core.domain.ValueSetMembers)

Aggregations

ServiceProvider (com.b2international.snowowl.core.ServiceProvider)16 List (java.util.List)9 Collectors (java.util.stream.Collectors)9 Set (java.util.Set)8 IOException (java.io.IOException)6 ResourceURI (com.b2international.snowowl.core.ResourceURI)5 Map (java.util.Map)5 Options (com.b2international.commons.options.Options)4 BadRequestException (com.b2international.commons.exceptions.BadRequestException)3 RepositoryManager (com.b2international.snowowl.core.RepositoryManager)3 CodeSystemRequests (com.b2international.snowowl.core.codesystem.CodeSystemRequests)3 Request (com.b2international.snowowl.core.events.Request)3 User (com.b2international.snowowl.core.identity.User)3 ApiException (com.b2international.commons.exceptions.ApiException)2 Branch (com.b2international.snowowl.core.branch.Branch)2 Concept (com.b2international.snowowl.core.domain.Concept)2 ConceptMapMappings (com.b2international.snowowl.core.domain.ConceptMapMappings)2 RequestHeaders (com.b2international.snowowl.core.events.util.RequestHeaders)2 Permission (com.b2international.snowowl.core.identity.Permission)2 IEventBus (com.b2international.snowowl.eventbus.IEventBus)2