/** * Get a list of all fields including both relationships and attributes. * * @param entity entity * @return List of all fields. */ public List<String> getAllFields(Object entity) { return getAllFields(entity.getClass()); }
/** * Get a list of all fields including both relationships and attributes. * * @param entity entity * @return List of all fields. */ public List<String> getAllFields(Object entity) { return getAllFields(entity.getClass()); }
/** * Retrieve fields from an object containing a particular type. * * @param targetClass Class to search for fields * @param targetType Type of fields to find * @return Set containing field names */ public Set<String> getFieldsOfType(Class<?> targetClass, Class<?> targetType) { HashSet<String> fields = new HashSet<>(); for (String field : getAllFields(targetClass)) { if (getParameterizedType(targetClass, field).equals(targetType)) { fields.add(field); } } return fields; }
/** * Retrieve fields from an object containing a particular type. * * @param targetClass Class to search for fields * @param targetType Type of fields to find * @return Set containing field names */ public Set<String> getFieldsOfType(Class<?> targetClass, Class<?> targetType) { HashSet<String> fields = new HashSet<>(); for (String field : getAllFields(targetClass)) { if (getParameterizedType(targetClass, field).equals(targetType)) { fields.add(field); } } return fields; }
List<String> fields = entityDictionary.getAllFields(forType).stream() .filter(field -> sparseFields == null || sparseFields.contains(field)) .collect(Collectors.toList());
List<String> fields = entityDictionary.getAllFields(forType).stream() .filter(field -> sparseFields == null || sparseFields.contains(field)) .collect(Collectors.toList());
List<String> fields = dictionary.getAllFields(resourceClass); String resourceName = dictionary.getJsonAliasFor(resourceClass); Set<String> requestedFields = scope.getSparseFields().getOrDefault(resourceName, Collections.EMPTY_SET);
List<String> fields = dictionary.getAllFields(resourceClass); String resourceName = dictionary.getJsonAliasFor(resourceClass); Set<String> requestedFields = scope.getSparseFields().getOrDefault(resourceName, Collections.EMPTY_SET);
/** * Build an expression representing any field on an entity. * * @param condition The condition which triggered this permission expression check * @param checkFn check function * @param scope RequestScope * @return Expressions */ private Expression buildAnyFieldExpression(final PermissionCondition condition, final Function<Check, Expression> checkFn, final RequestScope scope) { Class<?> resourceClass = condition.getEntityClass(); Class<? extends Annotation> annotationClass = condition.getPermission(); ParseTree classPermissions = entityDictionary.getPermissionsForClass(resourceClass, annotationClass); Expression entityExpression = expressionFromParseTree(classPermissions, checkFn); OrExpression allFieldsExpression = new OrExpression(FAILURE, null); List<String> fields = entityDictionary.getAllFields(resourceClass); Set<String> sparseFields = scope.getSparseFields().get(entityDictionary.getJsonAliasFor(resourceClass)); for (String field : fields) { if (sparseFields != null && !sparseFields.contains(field)) { continue; } ParseTree fieldPermissions = entityDictionary.getPermissionsForField(resourceClass, field, annotationClass); Expression fieldExpression = expressionFromParseTree(fieldPermissions, checkFn); allFieldsExpression = new OrExpression(allFieldsExpression, fieldExpression); } return new AnyFieldExpression(condition, entityExpression, allFieldsExpression); }
/** * Build an expression representing any field on an entity. * * @param condition The condition which triggered this permission expression check * @param checkFn check function * @param scope RequestScope * @return Expressions */ private Expression buildAnyFieldExpression(final PermissionCondition condition, final Function<Check, Expression> checkFn, final RequestScope scope) { Class<?> resourceClass = condition.getEntityClass(); Class<? extends Annotation> annotationClass = condition.getPermission(); ParseTree classPermissions = entityDictionary.getPermissionsForClass(resourceClass, annotationClass); Expression entityExpression = expressionFromParseTree(classPermissions, checkFn); OrExpression allFieldsExpression = new OrExpression(FAILURE, null); List<String> fields = entityDictionary.getAllFields(resourceClass); Set<String> sparseFields = scope.getSparseFields().get(entityDictionary.getJsonAliasFor(resourceClass)); for (String field : fields) { if (sparseFields != null && !sparseFields.contains(field)) { continue; } ParseTree fieldPermissions = entityDictionary.getPermissionsForField(resourceClass, field, annotationClass); Expression fieldExpression = expressionFromParseTree(fieldPermissions, checkFn); allFieldsExpression = new OrExpression(allFieldsExpression, fieldExpression); } return new AnyFieldExpression(condition, entityExpression, allFieldsExpression); }
/** * @return the JSON-API 'field' query parameter for some GET operations. */ private Parameter getSparseFieldsParameter() { String typeName = dictionary.getJsonAliasFor(type); List<String> fieldNames = dictionary.getAllFields(type); return new QueryParameter() .type("array") .name("fields[" + typeName + "]") .description("Selects the set of " + typeName + " fields that should be returned in the result.") .items(new StringProperty()._enum(fieldNames)) .collectionFormat("csv"); }