private Supplier<Pair<Integer, JsonNode>> handleRequest(StateContext state, BiFunction<Data<Resource>, RequestScope, Boolean> handler) { Data<Resource> data = state.getJsonApiDocument().getData(); handler.apply(data, state.getRequestScope()); // TODO: figure out if we've made modifications that differ from those requested by client return () -> Pair.of(HttpStatus.SC_NO_CONTENT, null); }
@Override public Supplier<Pair<Integer, JsonNode>> handleGet(StateContext state) { ObjectMapper mapper = state.getRequestScope().getMapper().getObjectMapper(); return () -> Pair.of(HttpStatus.SC_OK, getResponseBody(record, state.getRequestScope())); }
@Override public Supplier<Pair<Integer, JsonNode>> handleGet(StateContext state) { ObjectMapper mapper = state.getRequestScope().getMapper().getObjectMapper(); return () -> Pair.of(HttpStatus.SC_OK, getResponseBody(record, state.getRequestScope())); }
private Supplier<Pair<Integer, JsonNode>> handleRequest(StateContext state, BiFunction<Data<Resource>, RequestScope, Boolean> handler) { Data<Resource> data = state.getJsonApiDocument().getData(); handler.apply(data, state.getRequestScope()); // TODO: figure out if we've made modifications that differ from those requested by client return () -> Pair.of(HttpStatus.SC_NO_CONTENT, null); }
/** * 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) ); }
/** * 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) ); }
@Override public Supplier<Pair<Integer, JsonNode>> handlePost(StateContext state) { RequestScope requestScope = state.getRequestScope(); JsonApiMapper mapper = requestScope.getMapper(); newObject = createObject(requestScope); parent.ifPresent(persistentResource -> persistentResource.addRelation(relationName.get(), newObject)); return () -> { JsonApiDocument returnDoc = new JsonApiDocument(); returnDoc.setData(new Data(newObject.toResource())); JsonNode responseBody = mapper.getObjectMapper().convertValue(returnDoc, JsonNode.class); return Pair.of(HttpStatus.SC_CREATED, responseBody); }; }
@Override public Supplier<Pair<Integer, JsonNode>> handlePost(StateContext state) { RequestScope requestScope = state.getRequestScope(); JsonApiMapper mapper = requestScope.getMapper(); newObject = createObject(requestScope); parent.ifPresent(persistentResource -> persistentResource.addRelation(relationName.get(), newObject)); return () -> { JsonApiDocument returnDoc = new JsonApiDocument(); returnDoc.setData(new Data(newObject.toResource())); JsonNode responseBody = mapper.getObjectMapper().convertValue(returnDoc, JsonNode.class); return Pair.of(HttpStatus.SC_CREATED, responseBody); }; }
private PersistentResource<?> entityRecord(StateContext state, EntityContext entity) { String entityName = entity.term().getText(); String id = entity.id().getText(); EntityDictionary dictionary = state.getRequestScope().getDictionary(); Class<?> entityClass = dictionary.getEntityClass(entityName); if (entityClass == null || !dictionary.isRoot(entityClass)) { throw new InvalidCollectionException(entityName); } return PersistentResource.loadRecord(entityClass, id, state.getRequestScope()); } }
@Override public void handle(StateContext state, RootCollectionLoadEntitiesContext ctx) { String entityName = ctx.term().getText(); EntityDictionary dictionary = state.getRequestScope().getDictionary(); Class<?> entityClass = dictionary.getEntityClass(entityName); if (entityClass == null || !dictionary.isRoot(entityClass)) { throw new InvalidCollectionException(entityName); } state.setState(new CollectionTerminalState(entityClass, Optional.empty(), Optional.empty())); }
@Override public void handle(StateContext state, RootCollectionLoadEntitiesContext ctx) { String entityName = ctx.term().getText(); EntityDictionary dictionary = state.getRequestScope().getDictionary(); Class<?> entityClass = dictionary.getEntityClass(entityName); if (entityClass == null || !dictionary.isRoot(entityClass)) { throw new InvalidCollectionException(entityName); } state.setState(new CollectionTerminalState(entityClass, Optional.empty(), Optional.empty())); }
private PersistentResource<?> entityRecord(StateContext state, EntityContext entity) { String entityName = entity.term().getText(); String id = entity.id().getText(); EntityDictionary dictionary = state.getRequestScope().getDictionary(); Class<?> entityClass = dictionary.getEntityClass(entityName); if (entityClass == null || !dictionary.isRoot(entityClass)) { throw new InvalidCollectionException(entityName); } return PersistentResource.loadRecord(entityClass, id, state.getRequestScope()); } }
@Override public void handle(StateContext state, RootCollectionRelationshipContext ctx) { PersistentResource record = entityRecord(state, ctx.entity()); String relationName = ctx.relationship().term().getText(); try { Optional<FilterExpression> filterExpression = state.getRequestScope().getExpressionForRelation(record, relationName); record.getRelationCheckedFiltered(relationName, filterExpression, Optional.empty(), Optional.empty()); } catch (InvalidAttributeException e) { throw new InvalidCollectionException(relationName); } state.setState(new RelationshipTerminalState(record, relationName)); }
@Override public void handle(StateContext state, RootCollectionRelationshipContext ctx) { PersistentResource record = entityRecord(state, ctx.entity()); String relationName = ctx.relationship().term().getText(); try { Optional<FilterExpression> filterExpression = state.getRequestScope().getExpressionForRelation(record, relationName); record.getRelationCheckedFiltered(relationName, filterExpression, Optional.empty(), Optional.empty()); } catch (InvalidAttributeException e) { throw new InvalidCollectionException(relationName); } state.setState(new RelationshipTerminalState(record, relationName)); }
@Override public Supplier<Pair<Integer, JsonNode>> handleGet(StateContext state) { JsonApiDocument jsonApiDocument = new JsonApiDocument(); RequestScope requestScope = state.getRequestScope(); Optional<MultivaluedMap<String, String>> queryParams = requestScope.getQueryParams();
@Override public Supplier<Pair<Integer, JsonNode>> handleGet(StateContext state) { JsonApiDocument doc = new JsonApiDocument(); RequestScope requestScope = state.getRequestScope(); JsonApiMapper mapper = requestScope.getMapper(); Optional<MultivaluedMap<String, String>> queryParams = requestScope.getQueryParams();
@Override public void handle(StateContext state, SubCollectionRelationshipContext ctx) { String id = ctx.entity().id().getText(); String subCollection = ctx.entity().term().getText(); PersistentResource childRecord; try { childRecord = resource.getRelation(subCollection, id); } catch (InvalidAttributeException e) { throw new InvalidCollectionException(subCollection); } String relationName = ctx.relationship().term().getText(); try { Optional<FilterExpression> filterExpression = state.getRequestScope().getExpressionForRelation(resource, subCollection); childRecord.getRelationCheckedFiltered(relationName, filterExpression, Optional.empty(), Optional.empty()); } catch (InvalidAttributeException e) { throw new InvalidCollectionException(relationName); } state.setState(new RelationshipTerminalState(childRecord, relationName)); } }
@Override public void handle(StateContext state, SubCollectionRelationshipContext ctx) { String id = ctx.entity().id().getText(); String subCollection = ctx.entity().term().getText(); PersistentResource childRecord; try { childRecord = resource.getRelation(subCollection, id); } catch (InvalidAttributeException e) { throw new InvalidCollectionException(subCollection); } String relationName = ctx.relationship().term().getText(); try { Optional<FilterExpression> filterExpression = state.getRequestScope().getExpressionForRelation(resource, subCollection); childRecord.getRelationCheckedFiltered(relationName, filterExpression, Optional.empty(), Optional.empty()); } catch (InvalidAttributeException e) { throw new InvalidCollectionException(relationName); } state.setState(new RelationshipTerminalState(childRecord, relationName)); } }
@Override public Supplier<Pair<Integer, JsonNode>> handlePatch(StateContext state) { JsonApiDocument jsonApiDocument = state.getJsonApiDocument(); Data<Resource> data = jsonApiDocument.getData(); if (data == null) { throw new InvalidEntityBodyException("Expected data but found null"); } if (!data.isToOne()) { throw new InvalidEntityBodyException("Expected single element but found list"); } Resource resource = data.getSingleValue(); if (!record.matchesId(resource.getId())) { throw new InvalidEntityBodyException("Id in response body does not match requested id to update from path"); } patch(resource, state.getRequestScope()); return constructPatchResponse(record, state); }
@Override public Supplier<Pair<Integer, JsonNode>> handlePatch(StateContext state) { JsonApiDocument jsonApiDocument = state.getJsonApiDocument(); Data<Resource> data = jsonApiDocument.getData(); if (data == null) { throw new InvalidEntityBodyException("Expected data but found null"); } if (!data.isToOne()) { throw new InvalidEntityBodyException("Expected single element but found list"); } Resource resource = data.getSingleValue(); if (!record.matchesId(resource.getId())) { throw new InvalidEntityBodyException("Id in response body does not match requested id to update from path"); } patch(resource, state.getRequestScope()); return constructPatchResponse(record, state); }