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;
}
}
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;
}
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);
}
}
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);
}
}
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());
}
Aggregations