Search in sources :

Example 1 with JsonException

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

the class MapValidator method validate.

@SuppressWarnings("unchecked")
@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;
    Map<String, Object> object = null;
    if (value instanceof JsonObject) {
        object = (Map<String, Object>) value;
    } else if (value instanceof InputStream) {
        try {
            object = new JsonObject(Json.load((InputStream) value));
        } catch (Exception e) {
            return ValidationUtils.feedback(null, spec, Spec.Type, e.getMessage());
        }
        updateRequest = true;
    } else {
        try {
            object = new JsonObject(String.valueOf(value));
        } catch (JsonException e) {
            return ValidationUtils.feedback(null, spec, Spec.Type, e.getMessage());
        }
        updateRequest = true;
    }
    if (object.isEmpty() && Json.getBoolean(spec, Spec.Required, true)) {
        return ValidationUtils.feedback(null, spec, null, validator.getMessage(api, request.getLang(), RequiredMessage, label));
    }
    try {
        validator.validate(api, spec, consumer, request, object);
    } catch (ApiServiceValidatorException e) {
        return e.getFeedback();
    }
    if (updateRequest) {
        request.set(name, object, ApiRequest.Scope.Parameter);
    }
    return null;
}
Also used : JsonException(com.bluenimble.platform.json.JsonException) InputStream(java.io.InputStream) JsonObject(com.bluenimble.platform.json.JsonObject) JsonObject(com.bluenimble.platform.json.JsonObject) JsonException(com.bluenimble.platform.json.JsonException) ApiServiceValidatorException(com.bluenimble.platform.api.validation.ApiServiceValidatorException) ApiServiceValidatorException(com.bluenimble.platform.api.validation.ApiServiceValidatorException)

Example 2 with JsonException

use of com.bluenimble.platform.json.JsonException 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 3 with JsonException

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

the class JsonCreateHandler method execute.

@Override
public CommandResult execute(Tool tool, String... args) throws CommandExecutionException {
    if (args == null || args.length < 1) {
        throw new CommandExecutionException("json variable name require");
    }
    String var = args[0];
    @SuppressWarnings("unchecked") Map<String, Object> vars = (Map<String, Object>) tool.getContext(Tool.ROOT_CTX).get(ToolContext.VARS);
    JsonObject json = null;
    if (args.length > 1) {
        try {
            json = new JsonObject(args[1]);
        } catch (JsonException e) {
            throw new CommandExecutionException(e.getMessage(), e);
        }
    } else {
        json = new JsonObject();
    }
    vars.put(var, json);
    return new DefaultCommandResult(CommandResult.OK, json);
}
Also used : 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)

Example 4 with JsonException

use of com.bluenimble.platform.json.JsonException 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

JsonException (com.bluenimble.platform.json.JsonException)4 JsonObject (com.bluenimble.platform.json.JsonObject)4 CommandExecutionException (com.bluenimble.platform.cli.command.CommandExecutionException)2 DefaultCommandResult (com.bluenimble.platform.cli.command.impls.DefaultCommandResult)2 JsonArray (com.bluenimble.platform.json.JsonArray)2 Map (java.util.Map)2 ApiServiceValidatorException (com.bluenimble.platform.api.validation.ApiServiceValidatorException)1 InputStream (java.io.InputStream)1