use of com.yahoo.elide.core.RequestScope in project elide by yahoo.
the class BaseState method constructPatchResponse.
/**
* Construct PATCH response.
*
* @param record a resource that has been updated
* @param stateContext a state that contains reference to request scope where we can get status code for update
* @return a supplier of PATH response
*/
protected static Supplier<Pair<Integer, JsonNode>> constructPatchResponse(PersistentResource record, StateContext stateContext) {
RequestScope requestScope = stateContext.getRequestScope();
int updateStatusCode = requestScope.getUpdateStatusCode();
return () -> Pair.of(updateStatusCode, updateStatusCode == HttpStatus.SC_NO_CONTENT ? null : getResponseBody(record, requestScope));
}
use of com.yahoo.elide.core.RequestScope in project elide by yahoo.
the class RelationshipTerminalState method handleGet.
@Override
public Supplier<Pair<Integer, JsonNode>> handleGet(StateContext state) {
JsonApiDocument doc = new JsonApiDocument();
RequestScope requestScope = state.getRequestScope();
JsonApiMapper mapper = requestScope.getMapper();
MultivaluedMap<String, String> queryParams = requestScope.getQueryParams();
Map<String, Relationship> relationships = record.toResource(parentProjection).getRelationships();
if (relationships != null && relationships.containsKey(relationshipName)) {
Relationship relationship = relationships.get(relationshipName);
// Handle valid relationship
// Set data
Data<Resource> data = relationship.getData();
doc.setData(data);
// Run include processor
DocumentProcessor includedProcessor = new IncludedProcessor();
includedProcessor.execute(doc, record, queryParams);
return () -> Pair.of(HttpStatus.SC_OK, mapper.toJsonObject(doc));
}
// Handle no data for relationship
if (relationshipType.isToMany()) {
doc.setData(new Data<>(new ArrayList<>()));
} else if (relationshipType.isToOne()) {
doc.setData(new Data<>((Resource) null));
} else {
throw new IllegalStateException("Failed to GET a relationship; relationship is neither toMany nor toOne");
}
return () -> Pair.of(HttpStatus.SC_OK, mapper.toJsonObject(doc));
}
use of com.yahoo.elide.core.RequestScope in project elide by yahoo.
the class PermissionExpressionBuilder method buildUserCheckEntityAndAnyFieldExpression.
/**
* Build an expression that strictly evaluates UserCheck's and ignores other checks for an entity.
* expression = (entityRule AND (field1Rule OR field2Rule ... OR fieldNRule))
* <p>
* NOTE: This method returns _NO_ commit checks.
*
* @param resourceClass Resource class
* @param annotationClass Annotation class
* @param scope Request scope
* @param <A> type parameter
* @return User check expression to evaluate
*/
public <A extends Annotation> Expression buildUserCheckEntityAndAnyFieldExpression(final Type<?> resourceClass, final Class<A> annotationClass, Set<String> requestedFields, final RequestScope scope) {
final Function<Check, Expression> leafBuilderFn = (check) -> new CheckExpression(check, null, scope, null, cache);
ParseTree classPermissions = entityDictionary.getPermissionsForClass(resourceClass, annotationClass);
Expression entityExpression = normalizedExpressionFromParseTree(classPermissions, leafBuilderFn);
Expression anyFieldExpression = buildAnyFieldOnlyExpression(new PermissionCondition(annotationClass, resourceClass), leafBuilderFn, requestedFields);
if (entityExpression == null) {
return anyFieldExpression;
}
return new AndExpression(entityExpression, anyFieldExpression);
}
use of com.yahoo.elide.core.RequestScope in project elide by yahoo.
the class LifeCycleTest method failElidePatchExtensionCreate.
@Test
public void failElidePatchExtensionCreate() throws Exception {
DataStore store = mock(DataStore.class);
DataStoreTransaction tx = mock(DataStoreTransaction.class);
FieldTestModel mockModel = mock(FieldTestModel.class);
Elide elide = getElide(store, dictionary, MOCK_AUDIT_LOGGER);
String body = "[{\"op\": \"add\",\"path\": \"/testModel\",\"value\":{" + "\"type\":\"testModel\",\"attributes\": {\"field\":\"Foo\"}}}]";
RequestScope scope = buildRequestScope(dictionary, tx);
when(store.beginTransaction()).thenReturn(tx);
when(tx.createNewObject(ClassType.of(FieldTestModel.class), scope)).thenReturn(mockModel);
String contentType = JSONAPI_CONTENT_TYPE_WITH_JSON_PATCH_EXTENSION;
ElideResponse response = elide.patch(baseUrl, contentType, contentType, "/", body, null, NO_VERSION);
assertEquals(HttpStatus.SC_BAD_REQUEST, response.getResponseCode());
assertEquals("[{\"errors\":[{\"detail\":\"Bad Request Body'Patch extension requires all objects to have an assigned ID (temporary or permanent) when assigning relationships.'\",\"status\":\"400\"}]}]", response.getBody());
verify(mockModel, never()).classCallback(eq(CREATE), eq(PRESECURITY));
verify(mockModel, never()).classCallback(eq(CREATE), eq(PREFLUSH));
verify(mockModel, never()).classCallback(eq(CREATE), eq(PRECOMMIT));
verify(mockModel, never()).classCallback(eq(CREATE), eq(POSTCOMMIT));
verify(mockModel, never()).classCallback(eq(UPDATE), any());
verify(mockModel, never()).classCallback(eq(DELETE), any());
verify(mockModel, never()).classAllFieldsCallback(any(), any());
verify(mockModel, never()).classAllFieldsCallback(eq(CREATE), eq(PRECOMMIT));
verify(mockModel, never()).attributeCallback(eq(CREATE), eq(PRESECURITY), any());
verify(mockModel, never()).attributeCallback(eq(CREATE), eq(PREFLUSH), any());
verify(mockModel, never()).attributeCallback(eq(CREATE), eq(PRECOMMIT), any());
verify(mockModel, never()).attributeCallback(eq(CREATE), eq(POSTCOMMIT), any());
verify(mockModel, never()).attributeCallback(eq(UPDATE), any(), any());
verify(mockModel, never()).attributeCallback(eq(DELETE), any(), any());
verify(mockModel, never()).relationCallback(eq(CREATE), eq(PRESECURITY), any());
verify(mockModel, never()).relationCallback(eq(CREATE), eq(PREFLUSH), any());
verify(mockModel, never()).relationCallback(eq(CREATE), eq(PRECOMMIT), any());
verify(mockModel, never()).relationCallback(eq(CREATE), eq(POSTCOMMIT), any());
verify(mockModel, never()).relationCallback(eq(UPDATE), any(), any());
verify(mockModel, never()).relationCallback(eq(DELETE), any(), any());
verify(tx, never()).preCommit(any());
verify(tx, never()).createObject(eq(mockModel), isA(RequestScope.class));
verify(tx, never()).flush(isA(RequestScope.class));
verify(tx, never()).commit(isA(RequestScope.class));
verify(tx).close();
}
use of com.yahoo.elide.core.RequestScope in project elide by yahoo.
the class EntityProjectionMakerTest method testRootCollectionWithTypedFilter.
@Test
public void testRootCollectionWithTypedFilter() {
MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>();
queryParams.add("filter[book]", "genre=='Science Fiction'");
String path = "/book";
RequestScope scope = new TestRequestScope(dictionary, path, queryParams);
FilterExpression expression = new InPredicate(new Path(Book.class, dictionary, "genre"), "Science Fiction");
EntityProjectionMaker maker = new EntityProjectionMaker(dictionary, scope);
EntityProjection expected = EntityProjection.builder().type(Book.class).attribute(Attribute.builder().name("title").type(String.class).build()).attribute(Attribute.builder().name("awards").type(Collection.class).build()).attribute(Attribute.builder().name("genre").type(String.class).build()).attribute(Attribute.builder().name("language").type(String.class).build()).attribute(Attribute.builder().name("publishDate").type(long.class).build()).attribute(Attribute.builder().name("authorTypes").type(Collection.class).build()).attribute(Attribute.builder().name("price").type(Price.class).build()).filterExpression(expression).relationship("authors", EntityProjection.builder().type(Author.class).build()).relationship("publisher", EntityProjection.builder().type(Publisher.class).build()).relationship("editor", EntityProjection.builder().type(Editor.class).build()).pagination(PaginationImpl.getDefaultPagination(ClassType.of(Book.class))).build();
EntityProjection actual = maker.parsePath(path);
projectionEquals(expected, actual);
}
Aggregations