use of com.b2international.commons.exceptions.BadRequestException in project snow-owl by b2ihealthcare.
the class AuthorizationHeaderVerifier method toUser.
/**
* Converts the given JWT access token to a {@link User} representation using the configured email and permission claims.
*
* @param jwt
* - the JWT to convert to a {@link User} object
* @return
* @throws BadRequestException
* - if either the configured email or permissions property is missing from the given JWT
*/
public User toUser(DecodedJWT jwt) {
final Claim emailClaim = jwt.getClaim(emailClaimProperty);
if (emailClaim == null || emailClaim.isNull()) {
throw new BadRequestException("'%s' JWT access token field is required for email access, but it was missing.", emailClaimProperty);
}
Claim permissionsClaim = jwt.getClaim(permissionsClaimProperty);
if (permissionsClaim == null || permissionsClaim.isNull()) {
throw new BadRequestException("'%s' JWT access token field is required for permissions access, but it was missing.", permissionsClaimProperty);
}
final Set<Permission> permissions = jwt.getClaim(permissionsClaimProperty).asList(String.class).stream().map(Permission::valueOf).collect(Collectors.toSet());
return new User(emailClaim.asString(), List.of(new Role("jwt_roles", permissions)));
}
use of com.b2international.commons.exceptions.BadRequestException in project snow-owl by b2ihealthcare.
the class AbstractRestService method extractSortFields.
/**
* Extract {@link SearchResourceRequest.Sort}s from the given list of sortKeys. The returned list maintains the same order as the input sortKey
* list.
*
* @param sortKeys
* @return
*/
protected final List<Sort> extractSortFields(List<String> sortKeys) {
if (CompareUtils.isEmpty(sortKeys)) {
return Collections.emptyList();
}
final List<Sort> result = Lists.newArrayList();
for (String sortKey : sortKeys) {
Matcher matcher = sortKeyPattern.matcher(sortKey);
if (matcher.matches()) {
String field = matcher.group(1);
String order = matcher.group(2);
result.add(SearchResourceRequest.SortField.of(field, !"desc".equals(order)));
} else {
throw new BadRequestException("Sort key '%s' is not supported, or incorrect sort field pattern.", sortKey);
}
}
return result;
}
use of com.b2international.commons.exceptions.BadRequestException in project snow-owl by b2ihealthcare.
the class SnomedConceptCreateRequest method checkParent.
private void checkParent(TransactionContext context) {
final SnomedRefSetType refSetType = refSetRequest.getRefSetType();
final String refSetTypeRootParent = SnomedRefSetUtil.getParentConceptId(refSetType);
final Set<String> parents = getParents();
if (!isValidParentage(context, refSetTypeRootParent, parents)) {
throw new BadRequestException("'%s' type reference sets should be subtype of '%s' concept.", refSetType, refSetTypeRootParent);
}
}
use of com.b2international.commons.exceptions.BadRequestException in project snow-owl by b2ihealthcare.
the class SnomedConcreteDomainMemberCreateDelegate method execute.
@Override
public String execute(SnomedReferenceSet refSet, TransactionContext context) {
checkRefSetType(refSet, SnomedRefSetType.CONCRETE_DATA_TYPE);
checkReferencedComponent(refSet);
checkNonEmptyProperty(SnomedRf2Headers.FIELD_VALUE);
checkNonEmptyProperty(SnomedRf2Headers.FIELD_RELATIONSHIP_GROUP);
checkComponentExists(refSet, context, SnomedRf2Headers.FIELD_MODULE_ID, getModuleId());
checkComponentExists(refSet, context, SnomedRf2Headers.FIELD_REFERENCED_COMPONENT_ID, getReferencedComponentId());
checkComponentExists(refSet, context, SnomedRf2Headers.FIELD_TYPE_ID);
checkComponentExists(refSet, context, SnomedRf2Headers.FIELD_CHARACTERISTIC_TYPE_ID);
DataType dataType = refSet.getDataType();
String value = getProperty(SnomedRf2Headers.FIELD_VALUE);
try {
SnomedRefSetUtil.deserializeValue(dataType, value);
} catch (IllegalArgumentException e) {
throw new BadRequestException("Couldn't deserialize value '%s' for data type '%s'.", value, dataType);
}
SnomedRefSetMemberIndexEntry member = SnomedComponents.newConcreteDomainReferenceSetMember().withId(getId()).withActive(isActive()).withCharacteristicTypeId(getComponentId(SnomedRf2Headers.FIELD_CHARACTERISTIC_TYPE_ID)).withGroup(getProperty(SnomedRf2Headers.FIELD_RELATIONSHIP_GROUP, Integer.class)).withModuleId(getModuleId()).withReferencedComponent(getReferencedComponentId()).withRefSet(getReferenceSetId()).withSerializedValue(getProperty(SnomedRf2Headers.FIELD_VALUE)).withTypeId(getComponentId(SnomedRf2Headers.FIELD_TYPE_ID)).addTo(context);
return member.getId();
}
use of com.b2international.commons.exceptions.BadRequestException in project snow-owl by b2ihealthcare.
the class NamespaceIdProvider method extractNamespaceIds.
default Map<String, String> extractNamespaceIds(BranchContext context, final Collection<String> namespaceConceptIds, boolean ignoreInvalidValues) {
final Set<String> mutableNamespaceConceptIds = Sets.newHashSet(namespaceConceptIds);
final Map<String, String> namespacesByNamespaceConceptId = new HashMap<>(mutableNamespaceConceptIds.size());
// Keep only valid SCTIDs passed in to the filter
if (ignoreInvalidValues) {
mutableNamespaceConceptIds.removeIf(id -> !SnomedIdentifiers.isValid(id));
} else {
final Set<String> invalidNamespaceConceptIds = mutableNamespaceConceptIds.stream().filter(id -> !SnomedIdentifiers.isValid(id)).collect(ImmutableSortedSet.toImmutableSortedSet(Comparator.naturalOrder()));
if (!invalidNamespaceConceptIds.isEmpty()) {
throw new BadRequestException("The following namespaceConceptId values are invalid SNOMED CT Concept identifiers, %s", invalidNamespaceConceptIds.toString());
}
}
/*
* The International core namespace concept will not have an FSN matching the pattern,
* so remove it from the set, and convert it to the empty namespace directly.
*/
if (mutableNamespaceConceptIds.remove(Concepts.CORE_NAMESPACE)) {
namespacesByNamespaceConceptId.put(Concepts.CORE_NAMESPACE, "");
}
// Find the FSN of namespace SCTIDs
SnomedRequests.prepareSearchDescription().filterByActive(true).filterByType(Concepts.FULLY_SPECIFIED_NAME).filterByConcepts(mutableNamespaceConceptIds).setFields(SnomedDescriptionIndexEntry.Fields.ID, SnomedDescriptionIndexEntry.Fields.CONCEPT_ID, SnomedDescriptionIndexEntry.Fields.TERM).setLimit(1000).stream(context).flatMap(SnomedDescriptions::stream).forEach(fsn -> {
// Extract namespace from description terms
final Matcher matcher = NAMESPACE_PATTERN.matcher(fsn.getTerm());
if (matcher.matches()) {
namespacesByNamespaceConceptId.put(fsn.getConceptId(), matcher.group(1));
}
});
return Map.copyOf(namespacesByNamespaceConceptId);
}
Aggregations