@Override public boolean contains(final CharSequence key) { requireNonNull(key, "The key or pointer to check the existence of a value for must not be null!"); final boolean result; final JsonPointer pointer = JsonFactory.newPointer(key); if (1 >= pointer.getLevelCount()) { result = pointer.getRoot().map(this::containsKey).orElse(false); } else { result = pointer.getRoot() .flatMap(this::getValueForKey) .map(jsonValue -> !jsonValue.isObject() || jsonValue.asObject().contains(pointer.nextLevel())) // Recursion .orElse(false); } return result; }
private static JsonObject getPayload(final JsonObject sourceJsonObject) { final JsonObject result; final Optional<JsonValue> payloadJsonOptional = sourceJsonObject.getValue(JSON_PAYLOAD); if (payloadJsonOptional.isPresent()) { final JsonValue payloadJson = payloadJsonOptional.get(); if (!payloadJson.isObject()) { final String msgPattern = "Value <{0}> for <{1}> was not of type <{2}>!"; final String simpleName = JSON_PAYLOAD.getValueType().getSimpleName(); final String msg = MessageFormat.format(msgPattern, payloadJson, JSON_PAYLOAD.getPointer(), simpleName); throw new DittoJsonException(new IllegalArgumentException(msg)); } else { result = payloadJson.asObject(); } } else { result = JsonFactory.newObject(); } return result; }
@Override public boolean contains(final CharSequence key) { requireNonNull(key, "The key or pointer to check the existence of a value for must not be null!"); final boolean result; final JsonPointer pointer = JsonPointer.of(key); if (1 >= pointer.getLevelCount()) { result = pointer.getRoot().map(this::containsKey).orElse(false); } else { result = pointer.getRoot() .flatMap(this::getValueForKey) .map(jsonValue -> !jsonValue.isObject() || jsonValue.asObject().contains(pointer.nextLevel())) // Recursion .orElse(false); } return result; }
@Override public S isObject() { isNotNull(); assertThat(actual.isObject()) .overridingErrorMessage("Expected JSON value <%s> to be an object but it was not.", actual) .isTrue(); return myself; }
private static Collection<ResourcePermissions> createEntriesForAttribute(final JsonPointer attributePointer, final JsonValue attributeValue, final Enforcer policyEnforcer) { final Collection<ResourcePermissions> result = new HashSet<>(3); if (attributeValue.isObject() && !attributeValue.isNull()) { final JsonObject jsonObject = attributeValue.asObject(); // Recursion! jsonObject.forEach( subField -> result.addAll(createEntriesForAttribute(attributePointer.addLeaf(subField.getKey()), subField.getValue(), policyEnforcer))); } else { result.add(AttributeResourcePermissions.getInstance(attributePointer, attributeValue, policyEnforcer)); } return result; }
@Override public S isNotObject() { isNotNull(); assertThat(actual.isObject()) .overridingErrorMessage("Expected JSON value <%s> not to be an object but it was.", actual) .isFalse(); return myself; }
private static Collection<ResourcePermissions> createEntriesForFeatureProperty(final CharSequence featureId, final JsonPointer propertyPointer, final JsonValue propertyValue, final Enforcer policyEnforcer) { final Collection<ResourcePermissions> result = new HashSet<>(3); if (propertyValue.isObject() && !propertyValue.isNull()) { final JsonObject propertyValueJsonObject = propertyValue.asObject(); // Recursion! propertyValueJsonObject.forEach(subField -> result.addAll( createEntriesForFeatureProperty(featureId, propertyPointer.addLeaf(subField.getKey()), subField.getValue(), policyEnforcer))); } else { result.add(FeaturePropertyResourcePermissions.getInstance(featureId, propertyPointer, propertyValue, policyEnforcer)); } return result; }
private static Collection<ResourcePermissions> createEntriesForAttribute(final JsonPointer attributePointer, final JsonValue attributeValue, final Enforcer policyEnforcer) { final Collection<ResourcePermissions> result = new HashSet<>(3); if (attributeValue.isObject() && !attributeValue.isNull()) { final JsonObject jsonObject = attributeValue.asObject(); // Recursion! jsonObject.forEach( subField -> result.addAll(createEntriesForAttribute(attributePointer.addLeaf(subField.getKey()), subField.getValue(), policyEnforcer))); } else { result.add(AttributeResourcePermissions.getInstance(attributePointer, attributeValue, policyEnforcer)); } return result; }
private static Collection<ResourcePermissions> createEntriesForFeatureProperty(final CharSequence featureId, final JsonPointer propertyPointer, final JsonValue propertyValue, final Enforcer policyEnforcer) { final Collection<ResourcePermissions> result = new HashSet<>(3); if (propertyValue.isObject() && !propertyValue.isNull()) { final JsonObject propertyValueJsonObject = propertyValue.asObject(); // Recursion! propertyValueJsonObject.forEach(subField -> result.addAll( createEntriesForFeatureProperty(featureId, propertyPointer.addLeaf(subField.getKey()), subField.getValue(), policyEnforcer))); } else { result.add(FeaturePropertyResourcePermissions.getInstance(featureId, propertyPointer, propertyValue, policyEnforcer)); } return result; }
/** * Collect list of individual property changes */ private static void collectChanges(List target, String thingId, String featureId, JsonPointer path, JsonValue value) { if (value.isObject()) { // on Object recursively collect all individual properties with concatenated property path JsonObject obj = value.asObject(); obj.forEach(c -> { collectChanges(target, thingId, featureId, path.addLeaf(c.getKey()), c.getValue()); }); } else { target.add(new History(thingId, featureId, path, value, LocalDateTime.now())); } }
@Nullable private JsonValue getViewForJsonValueOrNull(final JsonValue jsonValue, final Set<String> subjectIds, final Permissions permissions) { final JsonValue result; if (jsonValue.isObject()) { result = getViewForJsonObjectOrNull(jsonValue.asObject(), subjectIds, permissions); } else if (jsonValue.isArray()) { result = getViewForJsonArrayOrNull(jsonValue.asArray(), subjectIds, permissions); } else if (grantRevokeIndex.hasPermissions(subjectIds, permissions)) { result = jsonValue; } else { result = null; } return result; }
private static boolean isJsonValueIllegal(final JsonValue entity) { final boolean result; if (entity.isArray()) { result = entity.asArray().stream().anyMatch(CommandWithOptionalEntityValidator::isJsonValueIllegal); } else if (entity.isObject()) { result = entity.asObject().stream().anyMatch(CommandWithOptionalEntityValidator::isJsonFieldIllegal); } else if (entity.isString()) { result = isStringIllegal(entity.asString()); } else { result = false; } return result; }
private static List<PointerAndValue> collectFlatPointers(final JsonPointer createdPointer, final JsonField field, final List<PointerAndValue> flattenedFields) { final JsonValue fieldValue = field.getValue(); if (fieldValue.isObject()) { final JsonObject jsonObject = fieldValue.asObject(); if (!jsonObject.isEmpty()) { jsonObject.forEach(jsonField -> collectFlatPointers(createdPointer.addLeaf(jsonField.getKey()), jsonField, flattenedFields)); } else { flattenedFields.add(new PointerAndValue(createdPointer, fieldValue)); } } else { flattenedFields.add(new PointerAndValue(createdPointer, fieldValue)); } return flattenedFields; }
private static List<Document> toFlatAttributesList(final String path, final JsonValue value, final List<Document> flatAttributes) { if (value.isString()) { flatAttributes.add(createFlatSubDocument(path, value.asString())); } else if (value.isBoolean()) { flatAttributes.add(createFlatSubDocument(path, value.asBoolean())); } else if (value.isNumber()) { try { flatAttributes.add(createFlatSubDocument(path, value.asLong())); } catch (final NumberFormatException e) { flatAttributes.add(createFlatSubDocument(path, value.asDouble())); } } else if (value.isNull()) { flatAttributes.add(createFlatSubDocument(path, null)); } else if (value.isObject()) { handleObject(path, value, flatAttributes); } return flatAttributes; }
private static JsonObjectBuilder setFieldInHierarchy(final ImmutableJsonObjectBuilder target, final JsonPointer pointer, final JsonField field) { if (1 >= pointer.getLevelCount()) { return target.set(field); } final JsonKey rootKey = pointer.getRoot().orElse(ROOT_KEY); final ImmutableJsonObjectBuilder newTarget = newInstance(); final JsonField rootJsonField = target.fields.get(rootKey.toString()); if (null != rootJsonField) { final JsonValue rootValue = rootJsonField.getValue(); if (rootValue.isObject() && !rootValue.isNull()) { newTarget.setAll(rootValue.asObject()); } } // let the recursion begin >:-( final JsonObject jsonObject = setFieldInHierarchy(newTarget, pointer.nextLevel(), field).build(); return target.set(rootKey, jsonObject); }
private void addInternalAttributes(final String path, final JsonValue jsonValue) { if (jsonValue == null || jsonValue.isNull()) { attributeInternally(path, null); } else if (jsonValue.isString()) { attributeInternally(path, jsonValue.asString()); } else if (jsonValue.isBoolean()) { attributeInternally(path, jsonValue.asBoolean()); } else if (jsonValue.isNumber()) { addNumberAttribute(path, jsonValue); } else if (jsonValue.isObject()) { jsonValue.asObject().getKeys().forEach(key -> // addInternalAttributes(path + PersistenceConstants.SLASH + key, jsonValue.asObject().getValue(key).orElse(null))); } }
private void addInternalFeatures(final String path, final JsonValue jsonValue, final String featureId) { if ((jsonValue == null) || jsonValue.isNull()) { featureInternally(path, null, featureId); } else if (jsonValue.isString()) { featureInternally(path, jsonValue.asString(), featureId); } else if (jsonValue.isBoolean()) { featureInternally(path, jsonValue.asBoolean(), featureId); } else if (jsonValue.isNumber()) { addNumberFeature(path, jsonValue, featureId); } else if (jsonValue.isObject()) { jsonValue.asObject().getKeys().forEach(name -> // addInternalFeatures(path + "/" + name, jsonValue.asObject().getValue(name).orElse(null), featureId)); } }
private void addInternalAttributes(final String path, final JsonValue jsonValue) { if ((jsonValue == null) || jsonValue.isNull()) { attributeInternally(path, null); } else if (jsonValue.isString()) { attributeInternally(path, jsonValue.asString()); } else if (jsonValue.isBoolean()) { attributeInternally(path, jsonValue.asBoolean()); } else if (jsonValue.isNumber()) { addNumberAttribute(path, jsonValue); } else if (jsonValue.isObject()) { jsonValue.asObject().getKeys().forEach(key -> // addInternalAttributes(path + PersistenceConstants.SLASH + key, jsonValue.asObject().getValue(key).orElse(null))); } }
private void addInternalFeatures(final String path, final JsonValue jsonValue, final String featureId) { if ((jsonValue == null) || jsonValue.isNull()) { featureInternally(path, null, featureId); } else if (jsonValue.isString()) { featureInternally(path, jsonValue.asString(), featureId); } else if (jsonValue.isBoolean()) { featureInternally(path, jsonValue.asBoolean(), featureId); } else if (jsonValue.isNumber()) { addNumberFeature(path, jsonValue, featureId); } else if (jsonValue.isObject()) { jsonValue.asObject().getKeys().forEach(name -> // addInternalFeatures(path + "/" + name, jsonValue.asObject().getValue(name).orElse(null), featureId)); } }