Search in sources :

Example 11 with BadRequestException

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)));
}
Also used : BadRequestException(com.b2international.commons.exceptions.BadRequestException) Claim(com.auth0.jwt.interfaces.Claim)

Example 12 with BadRequestException

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;
}
Also used : Matcher(java.util.regex.Matcher) Sort(com.b2international.snowowl.core.request.SearchResourceRequest.Sort) BadRequestException(com.b2international.commons.exceptions.BadRequestException)

Example 13 with BadRequestException

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);
    }
}
Also used : BadRequestException(com.b2international.commons.exceptions.BadRequestException) SnomedRefSetType(com.b2international.snowowl.snomed.core.domain.refset.SnomedRefSetType)

Example 14 with BadRequestException

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();
}
Also used : SnomedRefSetMemberIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry) DataType(com.b2international.snowowl.snomed.core.domain.refset.DataType) BadRequestException(com.b2international.commons.exceptions.BadRequestException)

Example 15 with BadRequestException

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);
}
Also used : BadRequestException(com.b2international.commons.exceptions.BadRequestException) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) java.util(java.util) Matcher(java.util.regex.Matcher) SnomedDescriptions(com.b2international.snowowl.snomed.core.domain.SnomedDescriptions) SnomedDescriptionIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedDescriptionIndexEntry) Pattern(java.util.regex.Pattern) SnomedIdentifiers(com.b2international.snowowl.snomed.cis.SnomedIdentifiers) SnomedTerminologyComponentConstants(com.b2international.snowowl.snomed.common.SnomedTerminologyComponentConstants) BranchContext(com.b2international.snowowl.core.domain.BranchContext) Concepts(com.b2international.snowowl.snomed.common.SnomedConstants.Concepts) Sets(com.google.common.collect.Sets) Matcher(java.util.regex.Matcher) BadRequestException(com.b2international.commons.exceptions.BadRequestException)

Aggregations

BadRequestException (com.b2international.commons.exceptions.BadRequestException)41 IOException (java.io.IOException)8 Collectors (java.util.stream.Collectors)7 ResourceURI (com.b2international.snowowl.core.ResourceURI)6 BranchContext (com.b2international.snowowl.core.domain.BranchContext)6 Request (com.b2international.snowowl.core.events.Request)6 List (java.util.List)6 Map (java.util.Map)6 Options (com.b2international.commons.options.Options)5 Branch (com.b2international.snowowl.core.branch.Branch)5 Set (java.util.Set)5 CompareUtils (com.b2international.commons.CompareUtils)4 ExpressionBuilder (com.b2international.index.query.Expressions.ExpressionBuilder)4 SnowowlRuntimeException (com.b2international.snowowl.core.api.SnowowlRuntimeException)4 SnomedRefSetType (com.b2international.snowowl.snomed.core.domain.refset.SnomedRefSetType)4 Collection (java.util.Collection)4 AlreadyExistsException (com.b2international.commons.exceptions.AlreadyExistsException)3 ConflictException (com.b2international.commons.exceptions.ConflictException)3 NotFoundException (com.b2international.commons.exceptions.NotFoundException)3 ExtendedLocale (com.b2international.commons.http.ExtendedLocale)3