use of com.yahoo.elide.core.exceptions.BadRequestException in project elide by yahoo.
the class PersistentResource method loadRecords.
/**
* Load a collection from the datastore.
*
* @param projection the projection to load
* @param requestScope the request scope
* @param ids a list of object identifiers to optionally load. Can be empty.
* @return a filtered collection of resources loaded from the datastore.
*/
public static Observable<PersistentResource> loadRecords(EntityProjection projection, List<String> ids, RequestScope requestScope) {
Type<?> loadClass = projection.getType();
Pagination pagination = projection.getPagination();
Sorting sorting = projection.getSorting();
FilterExpression filterExpression = projection.getFilterExpression();
EntityDictionary dictionary = requestScope.getDictionary();
DataStoreTransaction tx = requestScope.getTransaction();
if (shouldSkipCollection(loadClass, ReadPermission.class, requestScope, projection.getRequestedFields())) {
if (ids.isEmpty()) {
return Observable.empty();
}
throw new InvalidObjectIdentifierException(ids.toString(), dictionary.getJsonAliasFor(loadClass));
}
Set<String> requestedFields = projection.getRequestedFields();
if (pagination != null && !pagination.isDefaultInstance() && !CanPaginateVisitor.canPaginate(loadClass, dictionary, requestScope, requestedFields)) {
throw new BadRequestException(String.format("Cannot paginate %s", dictionary.getJsonAliasFor(loadClass)));
}
Set<PersistentResource> newResources = new LinkedHashSet<>();
if (!ids.isEmpty()) {
String typeAlias = dictionary.getJsonAliasFor(loadClass);
newResources = requestScope.getNewPersistentResources().stream().filter(resource -> typeAlias.equals(resource.getTypeName()) && ids.contains(resource.getUUID().orElse(""))).collect(Collectors.toSet());
FilterExpression idExpression = buildIdFilterExpression(ids, loadClass, dictionary, requestScope);
// Combine filters if necessary
filterExpression = Optional.ofNullable(filterExpression).map(fe -> (FilterExpression) new AndFilterExpression(idExpression, fe)).orElse(idExpression);
}
Optional<FilterExpression> permissionFilter = getPermissionFilterExpression(loadClass, requestScope, requestedFields);
if (permissionFilter.isPresent()) {
if (filterExpression != null) {
filterExpression = new AndFilterExpression(filterExpression, permissionFilter.get());
} else {
filterExpression = permissionFilter.get();
}
}
EntityProjection modifiedProjection = projection.copyOf().filterExpression(filterExpression).sorting(sorting).pagination(pagination).build();
Observable<PersistentResource> existingResources = filter(ReadPermission.class, Optional.ofNullable(modifiedProjection.getFilterExpression()), projection.getRequestedFields(), Observable.fromIterable(new PersistentResourceSet(tx.loadObjects(modifiedProjection, requestScope), requestScope)));
// TODO: Sort again in memory now that two sets are glommed together?
Observable<PersistentResource> allResources = Observable.fromIterable(newResources).mergeWith(existingResources);
Set<String> foundIds = new HashSet<>();
allResources = allResources.doOnNext((resource) -> {
String id = (String) resource.getUUID().orElseGet(resource::getId);
if (ids.contains(id)) {
foundIds.add(id);
}
});
allResources = allResources.doOnComplete(() -> {
Set<String> missedIds = Sets.difference(new HashSet<>(ids), foundIds);
if (!missedIds.isEmpty()) {
throw new InvalidObjectIdentifierException(missedIds.toString(), dictionary.getJsonAliasFor(loadClass));
}
});
return allResources;
}
use of com.yahoo.elide.core.exceptions.BadRequestException in project elide by yahoo.
the class Elide method get.
/**
* Handle GET.
*
* @param baseUrlEndPoint base URL with prefix endpoint
* @param path the path
* @param queryParams the query params
* @param requestHeaders the request headers
* @param opaqueUser the opaque user
* @param apiVersion the API version
* @param requestId the request ID
* @return Elide response object
*/
public ElideResponse get(String baseUrlEndPoint, String path, MultivaluedMap<String, String> queryParams, Map<String, List<String>> requestHeaders, User opaqueUser, String apiVersion, UUID requestId) {
if (elideSettings.isStrictQueryParams()) {
try {
verifyQueryParams(queryParams);
} catch (BadRequestException e) {
return buildErrorResponse(e, false);
}
}
return handleRequest(true, opaqueUser, dataStore::beginReadTransaction, requestId, (tx, user) -> {
JsonApiDocument jsonApiDoc = new JsonApiDocument();
RequestScope requestScope = new RequestScope(baseUrlEndPoint, path, apiVersion, jsonApiDoc, tx, user, queryParams, requestHeaders, requestId, elideSettings);
requestScope.setEntityProjection(new EntityProjectionMaker(elideSettings.getDictionary(), requestScope).parsePath(path));
BaseVisitor visitor = new GetVisitor(requestScope);
return visit(path, requestScope, visitor);
});
}
use of com.yahoo.elide.core.exceptions.BadRequestException in project elide by yahoo.
the class MetricRatio method make.
@Override
public MetricProjection make(Metric metric, String alias, Map<String, Argument> arguments) {
Argument numerator = arguments.get("numerator");
Argument denominator = arguments.get("denominator");
if (numerator == null || denominator == null) {
throw new BadRequestException("'numerator' and 'denominator' arguments are required for " + metric.getName());
}
return SQLMetricProjection.builder().alias(alias).arguments(arguments).name(metric.getName()).expression("{{" + numerator.getValue() + "}} / {{" + denominator.getValue() + "}}").valueType(metric.getValueType()).columnType(metric.getColumnType()).build();
}
use of com.yahoo.elide.core.exceptions.BadRequestException in project elide by yahoo.
the class SubscriptionDataFetcherTest method testErrorBeforeStream.
@Test
void testErrorBeforeStream() {
Book book1 = new Book();
book1.setTitle("Book 1");
book1.setId(1);
Book book2 = new Book();
book2.setTitle("Book 2");
book2.setId(2);
when(dataStoreTransaction.loadObjects(any(), any())).thenThrow(new BadRequestException("Bad Request"));
List<String> responses = List.of("null");
List<String> errors = List.of("Bad Request");
String graphQLRequest = "subscription {book(topic:ADDED) {id title}}";
assertSubscriptionEquals(graphQLRequest, responses, errors);
}
use of com.yahoo.elide.core.exceptions.BadRequestException in project elide by yahoo.
the class MapEntryContainer method processFetch.
@Override
public Object processFetch(Environment context) {
NonEntityDictionary nonEntityDictionary = context.nonEntityDictionary;
String fieldName = context.field.getName();
Object returnObject;
if (KEY.equalsIgnoreCase(fieldName)) {
returnObject = entry.getKey();
} else if (VALUE.equalsIgnoreCase(fieldName)) {
returnObject = entry.getValue();
} else {
throw new BadRequestException("Invalid field: '" + fieldName + "'. Maps only contain fields 'key' and 'value'");
}
if (nonEntityDictionary.hasBinding(EntityDictionary.getType(returnObject))) {
return new NonEntityContainer(returnObject);
}
return returnObject;
}
Aggregations