Search in sources :

Example 36 with JsonArray

use of com.bluenimble.platform.json.JsonArray in project serverless by bluenimble.

the class ArrayValidator method validate.

@Override
public Object validate(Api api, ApiConsumer consumer, ApiRequest request, DefaultApiServiceValidator validator, String name, String label, JsonObject spec, Object value) {
    JsonObject message = isRequired(validator, api, request.getLang(), label, spec, value);
    if (message != null) {
        return message;
    }
    if (value == null) {
        return null;
    }
    boolean updateRequest = false;
    JsonArray array = null;
    if (value instanceof JsonArray) {
        array = (JsonArray) value;
    } else {
        try {
            array = new JsonArray(String.valueOf(value));
        } catch (JsonException e) {
            return ValidationUtils.feedback(null, spec, Spec.Type, e.getMessage());
        }
        updateRequest = true;
    }
    if (array.isEmpty()) {
        return ValidationUtils.feedback(null, spec, null, validator.getMessage(api, request.getLang(), RequiredMessage, label));
    }
    String sType = Json.getString(spec, Spec.SType, MapValidator.Type);
    TypeValidator tValidator = validator.getValidator(sType);
    for (int i = 0; i < array.count(); i++) {
        Object feedback = tValidator.validate(api, consumer, request, validator, name, label + "->index " + i, spec, array.get(i));
        if (feedback != null) {
            return feedback;
        }
    }
    if (updateRequest) {
        request.set(name, array);
    }
    return null;
}
Also used : JsonArray(com.bluenimble.platform.json.JsonArray) JsonException(com.bluenimble.platform.json.JsonException) JsonObject(com.bluenimble.platform.json.JsonObject) JsonObject(com.bluenimble.platform.json.JsonObject)

Example 37 with JsonArray

use of com.bluenimble.platform.json.JsonArray in project serverless by bluenimble.

the class StringValidator method validate.

@Override
public Object validate(Api api, ApiConsumer consumer, ApiRequest request, DefaultApiServiceValidator validator, String name, String label, JsonObject spec, Object value) {
    Object message = isRequired(validator, api, request.getLang(), label, spec, value);
    if (message != null) {
        return message;
    }
    if (value == null) {
        return null;
    }
    JsonObject feedback = null;
    String sValue = String.valueOf(value);
    String displayValue = Json.getBoolean(spec, Spec.Secret, false) ? SecretValue : sValue;
    // validate length
    int min = Json.getInteger(spec, Spec.Min, 1);
    if (sValue.length() < min) {
        feedback = ValidationUtils.feedback(feedback, spec, Spec.Min, validator.getMessage(api, request.getLang(), MinMessage, label, String.valueOf(min), displayValue));
    }
    int max = Json.getInteger(spec, Spec.Max, Integer.MAX_VALUE);
    if (sValue.length() > max) {
        feedback = ValidationUtils.feedback(feedback, spec, Spec.Max, validator.getMessage(api, request.getLang(), MaxMessage, label, String.valueOf(max), displayValue));
    }
    if (feedback != null) {
        return feedback;
    }
    // validate vType
    String vType = Json.getString(spec, Spec.VType);
    if (!Lang.isNullOrEmpty(vType)) {
        TypeValidator vTypeValiator = validator.getVType(vType);
        if (vTypeValiator != null) {
            message = vTypeValiator.validate(api, consumer, request, validator, name, label, spec, sValue);
            if (message != null) {
                return message;
            }
        }
    }
    JsonArray lov = Json.getArray(spec, Spec.ListOfValues);
    if (lov == null) {
        return null;
    }
    if (!lov.contains(sValue)) {
        return ValidationUtils.feedback(feedback, spec, Spec.ListOfValues, validator.getMessage(api, request.getLang(), LovMessage, label, lov.join(Lang.COMMA), displayValue));
    }
    return null;
}
Also used : JsonArray(com.bluenimble.platform.json.JsonArray) JsonObject(com.bluenimble.platform.json.JsonObject) JsonObject(com.bluenimble.platform.json.JsonObject)

Example 38 with JsonArray

use of com.bluenimble.platform.json.JsonArray in project serverless by bluenimble.

the class BlueNimble method loadVariables.

private void loadVariables() {
    JsonObject oVars = Json.getObject(Config, CliSpec.Config.Variables);
    if (oVars == null) {
        oVars = new JsonObject();
        Config.set(CliSpec.Config.Variables, oVars);
    }
    if (!oVars.containsKey(DefaultVars.StorageProvider)) {
        oVars.set(DefaultVars.StorageProvider, "default");
    }
    if (!oVars.containsKey(DefaultVars.DatabaseProvider)) {
        oVars.set(DefaultVars.DatabaseProvider, "default");
    }
    if (!oVars.containsKey(DefaultVars.CacheProvider)) {
        oVars.set(DefaultVars.CacheProvider, "default");
    }
    if (!oVars.containsKey(DefaultVars.QueryAll)) {
        oVars.set(DefaultVars.QueryAll, new JsonObject().set("where", new JsonObject()));
    }
    if (!oVars.containsKey(DefaultVars.QueryCount)) {
        oVars.set(DefaultVars.QueryCount, new JsonObject().set("where", new JsonObject()).set("select", new JsonArray().set(null, "count(1)")));
    }
    if (!oVars.containsKey(DefaultVars.TemplateServices)) {
        oVars.set(DefaultVars.TemplateServices, "database/javascript");
    }
    if (!oVars.containsKey(DefaultVars.TemplateApi)) {
        oVars.set(DefaultVars.TemplateApi, "database/javascript");
    }
    if (!oVars.containsKey(DefaultVars.SpecLanguage)) {
        oVars.set(DefaultVars.SpecLanguage, SpecLangs.Json);
    }
    if (oVars.containsKey(DefaultVars.Paraphrase)) {
        try {
            super.setParaphrase(Json.getString(oVars, DefaultVars.Paraphrase), false);
        } catch (Exception e) {
            printer().error("Can't read user paraphrase. Cause: " + e.getMessage());
        }
    }
    if (!oVars.containsKey(DefaultVars.SslTrust)) {
        oVars.set(DefaultVars.SslTrust, "true");
    }
    if (!oVars.containsKey(DefaultVars.ApiSecurityEnabled)) {
        oVars.set(DefaultVars.ApiSecurityEnabled, "true");
    }
    if (!oVars.containsKey(DefaultVars.RemoteHeadersAccept)) {
        oVars.set(DefaultVars.RemoteHeadersAccept, ApiContentTypes.Json);
    }
    @SuppressWarnings("unchecked") Map<String, Object> vars = (Map<String, Object>) getContext(Tool.ROOT_CTX).get(ToolContext.VARS);
    for (Object key : oVars.keySet()) {
        vars.put(String.valueOf(key), oVars.get(key));
    }
}
Also used : JsonArray(com.bluenimble.platform.json.JsonArray) JsonObject(com.bluenimble.platform.json.JsonObject) JsonObject(com.bluenimble.platform.json.JsonObject) HashMap(java.util.HashMap) Map(java.util.Map) InstallI18nException(com.bluenimble.platform.cli.InstallI18nException) ToolStartupException(com.bluenimble.platform.cli.ToolStartupException) IOException(java.io.IOException)

Example 39 with JsonArray

use of com.bluenimble.platform.json.JsonArray in project serverless by bluenimble.

the class JsonDeleteHandler method execute.

@Override
public CommandResult execute(Tool tool, String... args) throws CommandExecutionException {
    if (args == null || args.length < 1) {
        throw new CommandExecutionException("json variable name required");
    }
    if (args.length < 2) {
        throw new CommandExecutionException("json property required");
    }
    String var = args[0];
    @SuppressWarnings("unchecked") Map<String, Object> vars = (Map<String, Object>) tool.getContext(Tool.ROOT_CTX).get(ToolContext.VARS);
    String prop = args[1];
    int indexOfDot = prop.indexOf(Lang.DOT);
    Object o = vars.get(var);
    if (o == null) {
        throw new CommandExecutionException("variable '" + var + "' not found");
    }
    if (!(o instanceof JsonObject) && !(o instanceof JsonArray)) {
        throw new CommandExecutionException("variable '" + var + "' isn't a valid json object or array");
    }
    if ((o instanceof JsonArray) && indexOfDot > 0) {
        throw new CommandExecutionException("property '" + prop + "' should be a valid integer since the json variable is an array");
    }
    if (indexOfDot <= 0) {
        if (o instanceof JsonObject) {
            ((JsonObject) o).remove(prop);
        } else {
            ((JsonArray) o).remove(Integer.parseInt(prop));
        }
        return new DefaultCommandResult(CommandResult.OK, o);
    }
    JsonObject json = (JsonObject) o;
    String[] path = Lang.split(prop, Lang.DOT);
    prop = path[path.length - 1];
    path = Lang.moveRight(path, 1);
    Object child = Json.find(json, path);
    if (child == null) {
        throw new CommandExecutionException(Lang.join(path, Lang.DOT) + " not found");
    }
    if (child instanceof JsonObject) {
        ((JsonObject) child).remove(prop);
        return new DefaultCommandResult(CommandResult.OK, json);
    } else if (child instanceof JsonArray) {
        int iProp = -1;
        try {
            iProp = Integer.valueOf(prop);
        } catch (Exception ex) {
        // ignore
        }
        JsonArray array = (JsonArray) child;
        if (iProp > -1 && array.count() > iProp) {
            ((JsonArray) child).remove(iProp);
            return new DefaultCommandResult(CommandResult.OK, json);
        }
    }
    return new DefaultCommandResult(CommandResult.OK, json);
}
Also used : JsonArray(com.bluenimble.platform.json.JsonArray) DefaultCommandResult(com.bluenimble.platform.cli.command.impls.DefaultCommandResult) JsonObject(com.bluenimble.platform.json.JsonObject) CommandExecutionException(com.bluenimble.platform.cli.command.CommandExecutionException) JsonObject(com.bluenimble.platform.json.JsonObject) Map(java.util.Map) CommandExecutionException(com.bluenimble.platform.cli.command.CommandExecutionException)

Example 40 with JsonArray

use of com.bluenimble.platform.json.JsonArray in project serverless by bluenimble.

the class JsonJoinHandler method execute.

@Override
public CommandResult execute(Tool tool, String... args) throws CommandExecutionException {
    if (args == null || args.length < 1) {
        throw new CommandExecutionException("json variable name required");
    }
    String sep = Lang.SPACE;
    if (args.length > 1) {
        sep = args[1];
    }
    String var = args[0];
    String prop = null;
    int indexOfSlash = var.indexOf(Lang.SLASH);
    if (indexOfSlash > 0) {
        prop = var.substring(indexOfSlash + 1);
        var = var.substring(0, indexOfSlash);
    }
    @SuppressWarnings("unchecked") Map<String, Object> vars = (Map<String, Object>) tool.getContext(Tool.ROOT_CTX).get(ToolContext.VARS);
    Object o = vars.get(var);
    if (o == null) {
        throw new CommandExecutionException("variable '" + var + "' not found");
    }
    if (!(o instanceof JsonObject)) {
        throw new CommandExecutionException("variable '" + var + "' isn't a valid json object");
    }
    JsonObject json = (JsonObject) o;
    Object a = json.find(prop, Lang.SLASH);
    if (!(a instanceof JsonArray)) {
        throw new CommandExecutionException("property '" + prop + "' isn't a valid array");
    }
    JsonArray array = (JsonArray) a;
    try {
        return new DefaultCommandResult(CommandResult.OK, join(array, sep));
    } catch (JsonException e) {
        throw new CommandExecutionException(e.getMessage(), e);
    }
}
Also used : JsonArray(com.bluenimble.platform.json.JsonArray) JsonException(com.bluenimble.platform.json.JsonException) DefaultCommandResult(com.bluenimble.platform.cli.command.impls.DefaultCommandResult) JsonObject(com.bluenimble.platform.json.JsonObject) CommandExecutionException(com.bluenimble.platform.cli.command.CommandExecutionException) JsonObject(com.bluenimble.platform.json.JsonObject) Map(java.util.Map)

Aggregations

JsonArray (com.bluenimble.platform.json.JsonArray)42 JsonObject (com.bluenimble.platform.json.JsonObject)37 DatabaseObject (com.bluenimble.platform.db.DatabaseObject)9 Map (java.util.Map)8 ApiServiceExecutionException (com.bluenimble.platform.api.ApiServiceExecutionException)6 ApiSpace (com.bluenimble.platform.api.ApiSpace)5 JsonApiOutput (com.bluenimble.platform.api.impls.JsonApiOutput)5 Database (com.bluenimble.platform.db.Database)5 IOException (java.io.IOException)5 ApiAccessDeniedException (com.bluenimble.platform.api.ApiAccessDeniedException)4 ApiAuthenticationException (com.bluenimble.platform.api.security.ApiAuthenticationException)4 CommandExecutionException (com.bluenimble.platform.cli.command.CommandExecutionException)4 DefaultCommandResult (com.bluenimble.platform.cli.command.impls.DefaultCommandResult)4 ApiService (com.bluenimble.platform.api.ApiService)3 DescribeOption (com.bluenimble.platform.api.DescribeOption)3 DatabaseException (com.bluenimble.platform.db.DatabaseException)3 JsonQuery (com.bluenimble.platform.db.query.impls.JsonQuery)3 KeyPair (com.bluenimble.platform.security.KeyPair)3 ArrayList (java.util.ArrayList)3 ApiOutput (com.bluenimble.platform.api.ApiOutput)2