Search in sources :

Example 1 with BadRequestException

use of com.b2international.commons.exceptions.BadRequestException in project snow-owl by b2ihealthcare.

the class EsDocumentSearcher method fromSearchAfterToken.

private Object[] fromSearchAfterToken(final String searchAfterToken) throws BadRequestException {
    if (Strings.isNullOrEmpty(searchAfterToken)) {
        return null;
    }
    final byte[] decodedToken = Base64.getUrlDecoder().decode(searchAfterToken);
    try (final DataInputStream dis = new DataInputStream(new ByteArrayInputStream(decodedToken))) {
        JavaBinCodec codec = new JavaBinCodec();
        List<?> obj = (List<?>) codec.unmarshal(dis);
        codec.close();
        return obj.toArray();
    } catch (final IOException e) {
        throw new FormattedRuntimeException("Couldn't decode searchAfter token.", e);
    } catch (final IllegalArgumentException e) {
        throw new BadRequestException("Invalid 'searchAfter' parameter value '%s'", searchAfterToken);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) FormattedRuntimeException(com.b2international.commons.exceptions.FormattedRuntimeException) BadRequestException(com.b2international.commons.exceptions.BadRequestException) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) JavaBinCodec(org.apache.solr.common.util.JavaBinCodec)

Example 2 with BadRequestException

use of com.b2international.commons.exceptions.BadRequestException in project snow-owl by b2ihealthcare.

the class DefaultSnomedIdentifierServiceRegressionTest method issue_SO_1945_testItemIdPoolExhausted.

@Test
public void issue_SO_1945_testItemIdPoolExhausted() throws Exception {
    final Provider<Index> storeProvider = Providers.of(store);
    final ItemIdGenerationStrategy idGenerationStrategy = new CyclingItemIdGenerationStrategy("1000", "1001");
    final SnomedIdentifierConfiguration config = new SnomedIdentifierConfiguration();
    config.setMaxIdGenerationAttempts(10);
    final ISnomedIdentifierService identifiers = new DefaultSnomedIdentifierService(storeProvider, idGenerationStrategy, config);
    final String first = Iterables.getOnlyElement(identifiers.generate(INT_NAMESPACE, ComponentCategory.CONCEPT, 1));
    assertThat(first).startsWith("1000");
    final String second = Iterables.getOnlyElement(identifiers.generate(INT_NAMESPACE, ComponentCategory.CONCEPT, 1));
    assertThat(second).startsWith("1001");
    /*
		 * The third attempt should generate itemId 1000 again,
		 * but that is already generated and no more itemIds are available
		 * therefore it will try to generate 1001, and that fails too, 
		 * rinse and repeat until maxIdGenerationAttempts are made
		 */
    try {
        identifiers.generate(INT_NAMESPACE, ComponentCategory.CONCEPT, 1);
    } catch (final BadRequestException e) {
        assertThat(e.getMessage()).isEqualTo(String.format("Couldn't generate 1 identifiers [CONCEPT, INT] in maximum (%s) number of attempts", config.getMaxIdGenerationAttempts()));
    }
}
Also used : SequentialItemIdGenerationStrategy(com.b2international.snowowl.snomed.cis.gen.SequentialItemIdGenerationStrategy) ItemIdGenerationStrategy(com.b2international.snowowl.snomed.cis.gen.ItemIdGenerationStrategy) DefaultSnomedIdentifierService(com.b2international.snowowl.snomed.cis.memory.DefaultSnomedIdentifierService) BadRequestException(com.b2international.commons.exceptions.BadRequestException) Index(com.b2international.index.Index) ISnomedIdentifierService(com.b2international.snowowl.snomed.cis.ISnomedIdentifierService) SnomedIdentifierConfiguration(com.b2international.snowowl.snomed.cis.SnomedIdentifierConfiguration) Test(org.junit.Test)

Example 3 with BadRequestException

use of com.b2international.commons.exceptions.BadRequestException in project snow-owl by b2ihealthcare.

the class BaseResourceCreateRequest method execute.

@Override
public final String execute(TransactionContext context) {
    // validate ID before use, IDs sometimes being used as branch paths, so must be a valid branch path
    try {
        BranchNameValidator.DEFAULT.checkName(id);
    } catch (BadRequestException e) {
        throw new BadRequestException(e.getMessage().replace("Branch name", getClass().getSimpleName().replace("CreateRequest", ".id")));
    }
    // validate URL format
    getResourceURLSchemaSupport(context).validate(url);
    // id checked against all resources
    final boolean existingId = ResourceRequests.prepareSearch().setLimit(0).filterById(getId()).build().execute(context).getTotal() > 0;
    if (existingId) {
        throw new AlreadyExistsException("Resource", getId());
    }
    // url checked against all resources
    final boolean existingUrl = ResourceRequests.prepareSearch().setLimit(0).filterByUrl(getUrl()).build().execute(context).getTotal() > 0;
    if (existingUrl) {
        throw new AlreadyExistsException("Resource", ResourceDocument.Fields.URL, getUrl());
    }
    preExecute(context);
    final List<String> bundleAncestorIds;
    if (IComponent.ROOT_ID.equals(bundleId)) {
        // "-1" is the only key that will show up both as the parent and as an ancestor
        bundleAncestorIds = List.of(IComponent.ROOT_ID);
    } else {
        final Bundles bundles = ResourceRequests.bundles().prepareSearch().filterById(bundleId).one().build().execute(context);
        if (bundles.getTotal() == 0) {
            throw new NotFoundException("Bundle parent", bundleId).toBadRequestException();
        }
        final Bundle bundleParent = bundles.first().get();
        bundleAncestorIds = bundleParent.getResourcePathSegments();
    }
    context.add(createResourceDocument(context, bundleAncestorIds));
    return id;
}
Also used : AlreadyExistsException(com.b2international.commons.exceptions.AlreadyExistsException) Bundle(com.b2international.snowowl.core.bundle.Bundle) BadRequestException(com.b2international.commons.exceptions.BadRequestException) NotFoundException(com.b2international.commons.exceptions.NotFoundException) Bundles(com.b2international.snowowl.core.bundle.Bundles)

Example 4 with BadRequestException

use of com.b2international.commons.exceptions.BadRequestException in project snow-owl by b2ihealthcare.

the class BaseResourceUpdateRequest method execute.

@Override
public final Boolean execute(TransactionContext context) {
    if (resource == null) {
        resource = context.lookup(componentId(), ResourceDocument.class);
    }
    final ResourceDocument.Builder updated = ResourceDocument.builder(resource);
    boolean changed = false;
    changed |= updateSpecializedProperties(context, resource, updated);
    // url checked against all resources
    if (url != null && !url.equals(resource.getUrl())) {
        if (url.isBlank()) {
            throw new BadRequestException("Resource.url should not be empty string");
        }
        final boolean existingUrl = ResourceRequests.prepareSearch().setLimit(0).filterByUrl(url).build().execute(context).getTotal() > 0;
        if (existingUrl) {
            throw new AlreadyExistsException("Resource", ResourceDocument.Fields.URL, url);
        }
        changed |= updateProperty(url, resource::getUrl, updated::url);
    }
    changed |= updateBundle(context, resource.getId(), resource.getBundleId(), updated);
    changed |= updateProperty(title, resource::getTitle, updated::title);
    changed |= updateProperty(language, resource::getLanguage, updated::language);
    changed |= updateProperty(description, resource::getDescription, updated::description);
    changed |= updateProperty(status, resource::getStatus, updated::status);
    changed |= updateProperty(copyright, resource::getCopyright, updated::copyright);
    changed |= updateProperty(owner, resource::getOwner, updated::owner);
    changed |= updateProperty(contact, resource::getContact, updated::contact);
    changed |= updateProperty(usage, resource::getUsage, updated::usage);
    changed |= updateProperty(purpose, resource::getPurpose, updated::purpose);
    if (changed) {
        // make sure we null out the updatedAt property we before update
        updated.updatedAt(null);
        context.update(resource, updated.build());
    }
    return changed;
}
Also used : Builder(com.b2international.snowowl.core.internal.ResourceDocument.Builder) AlreadyExistsException(com.b2international.commons.exceptions.AlreadyExistsException) ResourceDocument(com.b2international.snowowl.core.internal.ResourceDocument) BadRequestException(com.b2international.commons.exceptions.BadRequestException)

Example 5 with BadRequestException

use of com.b2international.commons.exceptions.BadRequestException in project snow-owl by b2ihealthcare.

the class ExpandParser method parse.

public static Options parse(final String expand) {
    String jsonizedOptionPart = String.format("{%s}", expand.replace("(", ":{").replace(')', '}'));
    try {
        JsonParser parser = JSON_FACTORY.createParser(jsonizedOptionPart);
        parser.setCodec(new ObjectMapper());
        Map<String, Object> source = parser.<Map<String, Object>>readValueAs(new TypeReference<Map<String, Object>>() {
        });
        return OptionsBuilder.newBuilder().putAll(source).build();
    } catch (JsonParseException e) {
        throw new BadRequestException("Expansion parameter %s is malformed.", expand);
    } catch (IOException e) {
        throw new SnowowlRuntimeException("Caught I/O exception while reading expansion parameters.", e);
    }
}
Also used : BadRequestException(com.b2international.commons.exceptions.BadRequestException) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException) JsonParser(com.fasterxml.jackson.core.JsonParser)

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