/** * Returns true if the value of the key is a BsonArray, returns false if the document does not contain the key. * * @param key the key * @return true if the value of the key is a BsonArray, returns false if the document does not contain the key. */ public boolean isArray(final Object key) { if (!containsKey(key)) { return false; } return get(key).isArray(); }
@Override public Boolean apply(final BsonDocument result, final ServerAddress serverAddress) { return result.get("users").isArray() && !result.getArray("users").isEmpty(); } };
/** * @param responseContent the responseContent to set */ public void setResponseContent(BsonValue responseContent) { if (responseContent != null && !(responseContent.isDocument() || responseContent.isArray())) { throw new IllegalArgumentException("response content must be " + "either an object or an array"); } this.responseContent = responseContent; }
/** * @param content the content to set */ public void setContent(BsonValue content) { if (content != null && !(content.isDocument() || content.isArray())) { throw new IllegalArgumentException("content must be " + "either an object or an array"); } this.content = content; }
private BsonArray getAggregationMetadata(BsonDocument contentToTransform) { List<Optional<BsonValue>> ___aggrs = JsonUtils .getPropsFromPath(contentToTransform, "$." + AbstractAggregationOperation.AGGREGATIONS_ELEMENT_NAME); if (___aggrs == null || ___aggrs.isEmpty()) { return null; } Optional<BsonValue> __aggrs = ___aggrs.get(0); if (__aggrs == null || !__aggrs.isPresent()) { return null; } BsonValue _aggrs = __aggrs.get(); if (_aggrs.isArray()) { return _aggrs.asArray(); } else { return null; } } }
public static boolean isBulkRequest(RequestContext context) { return context.getType() == RequestContext.TYPE.BULK_DOCUMENTS || context.getContent().isArray(); }
/** * checks if the aggregation variable start with $ this is not allowed since * the client would be able to modify the aggregation stages * * @param aVars RequestContext.getAggregationVars() */ public static void checkAggregationVariables(BsonValue aVars) throws SecurityException { if (aVars == null) { return; } if (aVars.isDocument()) { BsonDocument _obj = aVars.asDocument(); _obj.forEach((key, value) -> { if (key.startsWith("$")) { throw new SecurityException( "aggregation variables cannot include operators"); } if (value.isDocument() || value.isArray()) { checkAggregationVariables(value); } }); } else if (aVars.isArray()) { aVars.asArray().getValues().stream() .filter(el -> (el.isDocument() || el.isArray())) .forEachOrdered(AbstractAggregationOperation::checkAggregationVariables); } }
@Override public Boolean apply(final BsonDocument result, final ServerAddress serverAddress) { return result.get("users").isArray() && !result.getArray("users").isEmpty(); } };
public static boolean doesRequestUsesDotNotation(BsonValue content) { if (content.isDocument()) { BsonDocument obj = content.asDocument(); return obj.keySet().stream().anyMatch(key -> { return key.contains("."); }); } else if (content.isArray()) { BsonArray objs = content.asArray(); return objs.stream().anyMatch(obj -> { if (obj.isDocument()) { return doesRequestUsesDotNotation(obj); } else { return true; } }); } else { return true; } }
/** * * @param exchange * @param context * @throws Exception */ @Override public void handleRequest( HttpServerExchange exchange, RequestContext context) throws Exception { BsonValue content = context.getContent(); if (content != null && content.isArray()) { nextBulk.handleRequest(exchange, context); } else { nextNormal.handleRequest(exchange, context); } } }
@Override public boolean check( HttpServerExchange exchange, RequestContext context, BsonDocument contentToCheck, BsonValue args) { if (args.isArray()) { BsonArray conditions = filterMissingOptionalAndNullNullableConditions( args.asArray(), contentToCheck); return applyConditions(conditions, contentToCheck, context); } else { context.addWarning( "checker wrong definition: args property must be " + "an arrary of string property names."); return true; } }
public static List<RequestTransformer> getFromJson(BsonDocument props) throws InvalidMetadataException { BsonValue _rts = props.get(RTS_ELEMENT_NAME); if (_rts == null || !_rts.isArray()) { throw new InvalidMetadataException((_rts == null ? "missing '" : "invalid '") + RTS_ELEMENT_NAME + "' element; it must be an array"); } BsonArray rts = _rts.asArray(); List<RequestTransformer> ret = new ArrayList<>(); for (BsonValue o : rts.getValues()) { if (o.isDocument()) { ret.add(getSingleFromJson(o.asDocument())); } else { throw new InvalidMetadataException("invalid '" + RTS_ELEMENT_NAME + "'. Array elements must be json objects"); } } return ret; }
public static List<HookMetadata> getFromJson(BsonDocument props) throws InvalidMetadataException { BsonValue _scs = getProps(props); if (_scs == null || !_scs.isArray()) { throw new InvalidMetadataException( (_scs == null ? "missing '" : "invalid '") + ROOT_KEY + "' element. it must be a json array"); } BsonArray scs = _scs.asArray(); List<HookMetadata> ret = new ArrayList<>(); for (BsonValue o : scs) { if (o.isDocument()) { ret.add(getSingleFromJson(o.asDocument())); } else { throw new InvalidMetadataException("invalid '" + ROOT_KEY + "'. Array elements must be json objects"); } } return ret; }
public static List<RequestChecker> getFromJson(BsonDocument props) throws InvalidMetadataException { BsonValue _scs = getProps(props); if (_scs == null || !_scs.isArray()) { throw new InvalidMetadataException( (_scs == null ? "missing '" : "invalid '") + ROOT_KEY + "' element. it must be a json array"); } BsonArray scs = _scs.asArray(); List<RequestChecker> ret = new ArrayList<>(); for (BsonValue o : scs.getValues()) { if (o.isDocument()) { ret.add(getSingleFromJson(o.asDocument())); } else { throw new InvalidMetadataException( "invalid '" + ROOT_KEY + "'. Array elements must be json objects"); } } return ret; }
public static boolean doesRequestUsesUpdateOperators(BsonValue content) { if (content.isDocument()) { return JsonUtils.containsUpdateOperators(content.asDocument()); } else if (content.isArray()) { BsonArray objs = content.asArray(); return objs.stream().allMatch(obj -> { if (obj.isDocument()) { return doesRequestUsesUpdateOperators(obj); } else { return true; } }); } else { return true; } }
/** * @see https://docs.mongodb.com/manual/reference/operator/update/ * * @param ignoreCurrentDate true to ignore $currentDate * @param json * @return true if json contains update operators */ public static boolean containsUpdateOperators(BsonValue json, boolean ignoreCurrentDate) { if (json == null) { return false; } else if (json.isDocument()) { return _containsUpdateOperators(json.asDocument(), ignoreCurrentDate); } else if (json.isArray()) { return json.asArray() .stream() .filter(el -> el.isDocument()) .anyMatch(element -> _containsUpdateOperators(element.asDocument(), ignoreCurrentDate)); } else { return false; } }
@Override void applyGlobalTransformers(HttpServerExchange exchange, RequestContext context) { // execture global request tranformers getGlobalTransformers().stream() .filter(gt -> doesGlobalTransformerAppy(gt, exchange, context)) .forEachOrdered(gt -> { if (context.getContent() == null || context.getContent().isDocument()) { gt.transform( exchange, context, context.getContent()); } else if (context.getContent().isArray()) { context.getContent().asArray().forEach(doc -> { gt.transform( exchange, context, doc); }); } }); }
private BsonArray unflattenArray(BsonArray array) { BsonArray unflattenArray = new BsonArray(); for (BsonValue value : array) { if (value.isArray()) { unflattenArray.add(unflattenArray(value.asArray())); } else if (value.isDocument()) { unflattenArray.add(new JsonUnflattener(value.asDocument()) .withSeparator(separator).unflatten()); } else { unflattenArray.add(value); } } return unflattenArray; }
private static JsonValue mapBsonValueToJsonValue(@Nullable final BsonValue bsonValue, final Function<String, String> jsonKeyNameReviser) { final JsonValue result; if (bsonValue == null || bsonValue.isNull()) { result = JsonFactory.nullLiteral(); } else if (bsonValue.isString()) { result = JsonFactory.newValue(bsonValue.asString().getValue()); } else if (bsonValue.isNumber()) { result = mapBsonNumberToJsonNumber(bsonValue.asNumber()); } else if (bsonValue.isDocument()) { result = mapBsonDocumentToJsonObject(bsonValue.asDocument(), jsonKeyNameReviser); } else if (bsonValue.isArray()) { result = mapBsonArrayToJsonArray(bsonValue.asArray(), jsonKeyNameReviser); } else if (bsonValue.isBoolean()) { result = JsonFactory.newValue(bsonValue.asBoolean().getValue()); } else if (bsonValue.isTimestamp()) { final Instant instant = Instant.ofEpochSecond(bsonValue.asTimestamp().getTime()); result = JsonFactory.newValue(instant.toString()); } else { result = JsonFactory.nullLiteral(); } return result; }
private void setUnflattenedValue(BsonDocument flattened, String key, BsonValue currentVal, String objKey, Integer aryIdx) { BsonValue val = flattened.get(key); if (objKey != null) { if (val.isArray()) { BsonValue jsonArray = new BsonArray(); for (BsonValue arrayVal : val.asArray()) { jsonArray.asArray().add(newJsonUnflattener( arrayVal).unflatten()); } currentVal.asDocument().put(objKey, jsonArray); } else { currentVal.asDocument().put(objKey, val); } } else { // aryIdx != null assureJsonArraySize(currentVal.asArray(), aryIdx); currentVal.asArray().set(aryIdx, val); } }