Search in sources :

Example 11 with SnowowlRuntimeException

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

the class IdentityPlugin method createRSAKeyProvider.

private RSAKeyProvider createRSAKeyProvider(IdentityConfiguration conf) throws MalformedURLException {
    final String privateKeyId;
    final RSAPrivateKey privateKey;
    // read private key if provided
    if (!Strings.isNullOrEmpty(conf.getSigningKey())) {
        privateKeyId = Hashing.goodFastHash(16).hashString(conf.getSigningKey(), Charsets.UTF_8).toString();
        privateKey = readPrivateKey(conf.getSigningKey());
    } else {
        privateKeyId = null;
        privateKey = null;
    }
    if (!Strings.isNullOrEmpty(conf.getJwksUrl())) {
        // prefer JSON Web Key Set provider URLs (if set) for token verification
        JwkProvider jwkProvider = new JwkProviderBuilder(new URL(conf.getJwksUrl())).cached(5, 24, TimeUnit.HOURS).rateLimited(10, 1, TimeUnit.MINUTES).build();
        return new RSAKeyProvider() {

            @Override
            public RSAPublicKey getPublicKeyById(String kid) {
                try {
                    return (RSAPublicKey) jwkProvider.get(kid).getPublicKey();
                } catch (JwkException e) {
                    throw new SnowowlRuntimeException(e.getMessage(), e);
                }
            }

            @Override
            public String getPrivateKeyId() {
                return privateKeyId;
            }

            @Override
            public RSAPrivateKey getPrivateKey() {
                return privateKey;
            }
        };
    } else if (!Strings.isNullOrEmpty(conf.getVerificationKey())) {
        // if JWKS is not set, then fall back to verification key if set
        RSAPublicKey publicKey = readPublicKey(conf.getVerificationKey());
        return new RSAKeyProvider() {

            @Override
            public RSAPublicKey getPublicKeyById(String kid) {
                return publicKey;
            }

            @Override
            public String getPrivateKeyId() {
                return privateKeyId;
            }

            @Override
            public RSAPrivateKey getPrivateKey() {
                return privateKey;
            }
        };
    } else {
        // if neither jwksUrl nor the verificationKey settings are configured then this not an RSA configuration (or an invalid configuration raised when creating the algorithm instance)
        return null;
    }
}
Also used : RSAKeyProvider(com.auth0.jwt.interfaces.RSAKeyProvider) RSAPublicKey(java.security.interfaces.RSAPublicKey) JwkProvider(com.auth0.jwk.JwkProvider) JwkProviderBuilder(com.auth0.jwk.JwkProviderBuilder) JwkException(com.auth0.jwk.JwkException) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) URL(java.net.URL) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException)

Example 12 with SnowowlRuntimeException

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

the class Rf2GlobalValidator method fetchComponentIds.

private <T extends SnomedComponentDocument> Set<String> fetchComponentIds(BranchContext context, final Set<String> componentIdsToFetch, Class<T> clazz) {
    if (!componentIdsToFetch.isEmpty()) {
        Set<String> existingComponentIds = newHashSet();
        for (List<String> ids : Iterables.partition(componentIdsToFetch, RAW_QUERY_PAGE_SIZE)) {
            try {
                Query<String> query = Query.select(String.class).from(clazz).fields(RevisionDocument.Fields.ID).where(RevisionDocument.Expressions.ids(ids)).limit(ids.size()).build();
                existingComponentIds.addAll(context.service(RevisionSearcher.class).search(query).getHits());
            } catch (IOException e) {
                throw new SnowowlRuntimeException(e);
            }
        }
        componentIdsToFetch.removeAll(existingComponentIds);
    }
    return componentIdsToFetch;
}
Also used : IOException(java.io.IOException) RevisionSearcher(com.b2international.index.revision.RevisionSearcher) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException)

Example 13 with SnowowlRuntimeException

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

the class Taxonomies method buildTaxonomy.

private static Taxonomy buildTaxonomy(RevisionSearcher searcher, SnomedOWLExpressionConverter expressionConverter, StagingArea staging, LongCollection conceptIds, String characteristicTypeId, boolean checkCycles) {
    try {
        Collection<Object[]> isaStatements = getStatements(searcher, conceptIds, characteristicTypeId, true);
        final TaxonomyGraph oldTaxonomy = new TaxonomyGraph(conceptIds.size(), isaStatements.size());
        oldTaxonomy.setCheckCycles(checkCycles);
        final TaxonomyGraph newTaxonomy = new TaxonomyGraph(conceptIds.size(), isaStatements.size());
        newTaxonomy.setCheckCycles(checkCycles);
        // populate nodes
        LongIterator conceptIdsIt = conceptIds.iterator();
        while (conceptIdsIt.hasNext()) {
            long nodeId = conceptIdsIt.next();
            if (IComponent.ROOT_IDL == nodeId) {
                continue;
            }
            oldTaxonomy.addNode(nodeId);
            newTaxonomy.addNode(nodeId);
        }
        // populate edges
        for (Object[] isaStatement : isaStatements) {
            oldTaxonomy.addEdge((String) isaStatement[0], (long) isaStatement[1], (long[]) isaStatement[2]);
            newTaxonomy.addEdge((String) isaStatement[0], (long) isaStatement[1], (long[]) isaStatement[2]);
        }
        isaStatements = null;
        oldTaxonomy.update();
        final TaxonomyGraphStatus status = updateTaxonomy(searcher, expressionConverter, staging, newTaxonomy, characteristicTypeId);
        final Set<String> newKeys = newTaxonomy.getEdgeIds();
        final Set<String> oldKeys = oldTaxonomy.getEdgeIds();
        // new edges
        final Set<String> newEdges = Sets.difference(newKeys, oldKeys);
        // changed edges
        final Set<String> changedEdges = Sets.newHashSet();
        for (String nextEdge : Sets.intersection(newKeys, oldKeys)) {
            Edges oldValue = oldTaxonomy.getEdge(nextEdge);
            Edges newValue = newTaxonomy.getEdge(nextEdge);
            if (!oldValue.equals(newValue)) {
                changedEdges.add(nextEdge);
            }
        }
        // detached edges
        final Set<String> detachedEdges = Sets.difference(oldKeys, newKeys);
        return new Taxonomy(newTaxonomy, oldTaxonomy, status, newEdges, changedEdges, detachedEdges);
    } catch (IOException e) {
        throw new SnowowlRuntimeException(e);
    }
}
Also used : IOException(java.io.IOException) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException) LongIterator(com.b2international.collections.longs.LongIterator)

Example 14 with SnowowlRuntimeException

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

the class DefaultAttachmentRegistry method upload.

@Override
public void upload(UUID id, InputStream in) throws AlreadyExistsException, BadRequestException {
    final File file = toFile(id);
    if (file.exists()) {
        throw new AlreadyExistsException("Zip File", id.toString());
    }
    final BufferedInputStream bin = new BufferedInputStream(in);
    try {
        new ByteSource() {

            @Override
            public InputStream openStream() throws IOException {
                return bin;
            }
        }.copyTo(Files.asByteSink(file));
    } catch (IOException e) {
        throw new SnowowlRuntimeException("Failed to upload attachment of " + id, e);
    }
}
Also used : AlreadyExistsException(com.b2international.commons.exceptions.AlreadyExistsException) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) ByteSource(com.google.common.io.ByteSource) IOException(java.io.IOException) File(java.io.File) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException)

Example 15 with SnowowlRuntimeException

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

the class ValidateRequest method doValidate.

private ValidationResult doValidate(BranchContext context, Writer index) throws IOException {
    final String branchPath = context.path();
    ValidationRuleSearchRequestBuilder req = ValidationRequests.rules().prepareSearch();
    TerminologyResource resource = context.service(TerminologyResource.class);
    ResourceURI resourceURI = resource.getResourceURI(branchPath);
    if (!CompareUtils.isEmpty(ruleIds)) {
        req.filterByIds(ruleIds);
    }
    final ValidationRules rules = req.all().build().execute(context);
    final ValidationThreadPool pool = context.service(ValidationThreadPool.class);
    final BlockingQueue<IssuesToPersist> issuesToPersistQueue = Queues.newLinkedBlockingDeque();
    final List<Promise<Object>> validationPromises = Lists.newArrayList();
    // evaluate selected rules
    for (ValidationRule rule : rules) {
        checkArgument(rule.getCheckType() != null, "CheckType is missing for rule " + rule.getId());
        final ValidationRuleEvaluator evaluator = ValidationRuleEvaluator.Registry.get(rule.getType());
        if (evaluator != null) {
            validationPromises.add(pool.submit(rule.getCheckType(), () -> {
                Stopwatch w = Stopwatch.createStarted();
                try {
                    LOG.info("Executing rule '{}'...", rule.getId());
                    final List<?> evaluationResponse = evaluator.eval(context, rule, ruleParameters);
                    issuesToPersistQueue.offer(new IssuesToPersist(rule.getId(), evaluationResponse));
                    LOG.info("Execution of rule '{}' successfully completed in '{}'.", rule.getId(), w);
                // TODO report successfully executed validation rule
                } catch (Exception e) {
                    // TODO report failed validation rule
                    LOG.error("Execution of rule '{}' failed after '{}'.", rule.getId(), w, e);
                }
            }));
        }
    }
    final Set<String> ruleIds = rules.stream().map(ValidationRule::getId).collect(Collectors.toSet());
    final Multimap<String, ComponentIdentifier> whiteListedEntries = fetchWhiteListEntries(context, ruleIds);
    final Promise<List<Object>> promise = Promise.all(validationPromises);
    while (!promise.isDone() || !issuesToPersistQueue.isEmpty()) {
        if (!issuesToPersistQueue.isEmpty()) {
            final Collection<IssuesToPersist> issuesToPersist = newArrayList();
            issuesToPersistQueue.drainTo(issuesToPersist);
            if (!issuesToPersist.isEmpty()) {
                final List<String> rulesToPersist = issuesToPersist.stream().map(itp -> itp.ruleId).collect(Collectors.toList());
                LOG.info("Persisting issues generated by rules '{}'...", rulesToPersist);
                // persist new issues generated by rules so far, extending them using the Issue Extension API
                int persistedIssues = 0;
                final Multimap<String, ValidationIssue> issuesToExtendWithDetailsByToolingId = HashMultimap.create();
                for (IssuesToPersist ruleIssues : Iterables.consumingIterable(issuesToPersist)) {
                    final String ruleId = ruleIssues.ruleId;
                    final List<ValidationIssue> existingRuleIssues = ValidationRequests.issues().prepareSearch().all().filterByResourceUri(resourceURI).filterByRule(ruleId).build().execute(context).getItems();
                    final Set<String> issueIdsToDelete = Sets.newHashSet();
                    final Map<ComponentIdentifier, ValidationIssue> existingIsssuesByComponentIdentifier = new HashMap<>();
                    for (ValidationIssue issue : existingRuleIssues) {
                        if (existingIsssuesByComponentIdentifier.containsKey(issue.getAffectedComponent())) {
                            issueIdsToDelete.add(issue.getId());
                        } else {
                            existingIsssuesByComponentIdentifier.put(issue.getAffectedComponent(), issue);
                        }
                    }
                    // remove all processed whitelist entries
                    final Collection<ComponentIdentifier> ruleWhiteListEntries = whiteListedEntries.removeAll(ruleId);
                    final String toolingId = rules.stream().filter(rule -> ruleId.equals(rule.getId())).findFirst().get().getToolingId();
                    for (ValidationIssueDetails issueDetails : ruleIssues.issueDetails) {
                        final ValidationIssue validationIssue;
                        ComponentIdentifier componentIdentifier = issueDetails.affectedComponentId;
                        if (!existingIsssuesByComponentIdentifier.containsKey(componentIdentifier)) {
                            validationIssue = new ValidationIssue(UUID.randomUUID().toString(), ruleId, ComponentURI.of(resourceURI, componentIdentifier), ruleWhiteListEntries.contains(componentIdentifier));
                        } else {
                            final ValidationIssue issueToCopy = existingIsssuesByComponentIdentifier.get(componentIdentifier);
                            validationIssue = new ValidationIssue(issueToCopy.getId(), issueToCopy.getRuleId(), ComponentURI.of(resourceURI, issueToCopy.getAffectedComponent()), ruleWhiteListEntries.contains(issueToCopy.getAffectedComponent()));
                            existingIsssuesByComponentIdentifier.remove(componentIdentifier);
                        }
                        validationIssue.setDetails(ValidationIssueDetails.HIGHLIGHT_DETAILS, issueDetails.stylingDetails);
                        issuesToExtendWithDetailsByToolingId.put(toolingId, validationIssue);
                        persistedIssues++;
                    }
                    existingRuleIssues.stream().filter(issue -> existingIsssuesByComponentIdentifier.containsKey(issue.getAffectedComponent())).forEach(issue -> issueIdsToDelete.add(issue.getId()));
                    if (!issueIdsToDelete.isEmpty()) {
                        index.removeAll(Collections.singletonMap(ValidationIssue.class, issueIdsToDelete));
                    }
                }
                for (String toolingId : issuesToExtendWithDetailsByToolingId.keySet()) {
                    final ValidationIssueDetailExtension extensions = context.service(ValidationIssueDetailExtensionProvider.class).getExtensions(toolingId);
                    final Collection<ValidationIssue> issues = issuesToExtendWithDetailsByToolingId.removeAll(toolingId);
                    extensions.extendIssues(context, issues, ruleParameters);
                    for (ValidationIssue issue : issues) {
                        index.put(issue);
                    }
                }
                index.commit();
                LOG.info("Persisted '{}' issues generated by rules '{}'.", persistedIssues, rulesToPersist);
            }
        } else {
            try {
                // wait at least number of rules * 50ms for the next responses
                Thread.sleep(Math.min(((long) ruleIds.size()) * 100, POLL_INTERVAL_MAX));
            } catch (InterruptedException e) {
                throw new SnowowlRuntimeException(e);
            }
        }
    }
    // TODO return ValidationResult object with status and new issue IDs as set
    return new ValidationResult(context.info().id(), context.path());
}
Also used : ValidationIssueDetailExtensionProvider(com.b2international.snowowl.core.validation.issue.ValidationIssueDetailExtensionProvider) java.util(java.util) TerminologyResource(com.b2international.snowowl.core.TerminologyResource) Stopwatch(com.google.common.base.Stopwatch) Promise(com.b2international.snowowl.core.events.util.Promise) LoggerFactory(org.slf4j.LoggerFactory) ValidationIssue(com.b2international.snowowl.core.validation.issue.ValidationIssue) ValidationIssueDetailExtension(com.b2international.snowowl.core.validation.issue.ValidationIssueDetailExtension) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) AccessControl(com.b2international.snowowl.core.authorization.AccessControl) Permission(com.b2international.snowowl.core.identity.Permission) CompareUtils(com.b2international.commons.CompareUtils) ValidationRuleSearchRequestBuilder(com.b2international.snowowl.core.validation.rule.ValidationRuleSearchRequestBuilder) com.google.common.collect(com.google.common.collect) ValidationRepository(com.b2international.snowowl.core.internal.validation.ValidationRepository) ResourceURI(com.b2international.snowowl.core.ResourceURI) ValidationRule(com.b2international.snowowl.core.validation.rule.ValidationRule) ValidationWhiteListSearchRequestBuilder(com.b2international.snowowl.core.validation.whitelist.ValidationWhiteListSearchRequestBuilder) ValidationThreadPool(com.b2international.snowowl.core.internal.validation.ValidationThreadPool) Logger(org.slf4j.Logger) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException) Request(com.b2international.snowowl.core.events.Request) ComponentURI(com.b2international.snowowl.core.uri.ComponentURI) IOException(java.io.IOException) BlockingQueue(java.util.concurrent.BlockingQueue) Collectors(java.util.stream.Collectors) Writer(com.b2international.index.Writer) ValidationRuleEvaluator(com.b2international.snowowl.core.validation.eval.ValidationRuleEvaluator) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ComponentIdentifier(com.b2international.snowowl.core.ComponentIdentifier) ValidationRules(com.b2international.snowowl.core.validation.rule.ValidationRules) BranchContext(com.b2international.snowowl.core.domain.BranchContext) ValidationRuleSearchRequestBuilder(com.b2international.snowowl.core.validation.rule.ValidationRuleSearchRequestBuilder) ValidationThreadPool(com.b2international.snowowl.core.internal.validation.ValidationThreadPool) Stopwatch(com.google.common.base.Stopwatch) ComponentIdentifier(com.b2international.snowowl.core.ComponentIdentifier) ValidationIssueDetailExtension(com.b2international.snowowl.core.validation.issue.ValidationIssueDetailExtension) TerminologyResource(com.b2international.snowowl.core.TerminologyResource) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ResourceURI(com.b2international.snowowl.core.ResourceURI) ValidationIssue(com.b2international.snowowl.core.validation.issue.ValidationIssue) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException) IOException(java.io.IOException) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException) ValidationRule(com.b2international.snowowl.core.validation.rule.ValidationRule) Promise(com.b2international.snowowl.core.events.util.Promise) ValidationRules(com.b2international.snowowl.core.validation.rule.ValidationRules) ValidationRuleEvaluator(com.b2international.snowowl.core.validation.eval.ValidationRuleEvaluator) ValidationIssueDetailExtensionProvider(com.b2international.snowowl.core.validation.issue.ValidationIssueDetailExtensionProvider)

Aggregations

SnowowlRuntimeException (com.b2international.snowowl.core.api.SnowowlRuntimeException)39 IOException (java.io.IOException)27 SctId (com.b2international.snowowl.snomed.cis.domain.SctId)7 Collection (java.util.Collection)7 HttpPost (org.apache.http.client.methods.HttpPost)6 BadRequestException (com.b2international.commons.exceptions.BadRequestException)5 RevisionSearcher (com.b2international.index.revision.RevisionSearcher)4 Promise (com.b2international.snowowl.core.events.util.Promise)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 List (java.util.List)4 TimeUnit (java.util.concurrent.TimeUnit)4 File (java.io.File)3 Path (java.nio.file.Path)3 NamingException (javax.naming.NamingException)3 InitialLdapContext (javax.naming.ldap.InitialLdapContext)3 HttpGet (org.apache.http.client.methods.HttpGet)3 Logger (org.slf4j.Logger)3 JwkException (com.auth0.jwk.JwkException)2 JwkProvider (com.auth0.jwk.JwkProvider)2 JwkProviderBuilder (com.auth0.jwk.JwkProviderBuilder)2