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