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;
}
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;
}
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);
}
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);
}
}
Aggregations