Search in sources :

Example 1 with JsonSchemaValidator

use of org.apache.solr.util.JsonSchemaValidator in project lucene-solr by apache.

the class ApiBag method getCommandOperations.

public static List<CommandOperation> getCommandOperations(Reader reader, Map<String, JsonSchemaValidator> validators, boolean validate) {
    List<CommandOperation> parsedCommands = null;
    try {
        parsedCommands = CommandOperation.parse(reader);
    } catch (IOException e) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e);
    }
    if (validators == null || !validate) {
        // no validation possible because we do not have a spec
        return parsedCommands;
    }
    List<CommandOperation> commandsCopy = CommandOperation.clone(parsedCommands);
    for (CommandOperation cmd : commandsCopy) {
        JsonSchemaValidator validator = validators.get(cmd.name);
        if (validator == null) {
            cmd.addError(formatString("Unknown operation ''{0}'' available ops are ''{1}''", cmd.name, validators.keySet()));
            continue;
        } else {
            List<String> errs = validator.validateJson(cmd.getCommandData());
            if (errs != null)
                for (String err : errs) cmd.addError(err);
        }
    }
    List<Map> errs = CommandOperation.captureErrors(commandsCopy);
    if (!errs.isEmpty()) {
        throw new ExceptionWithErrObject(SolrException.ErrorCode.BAD_REQUEST, "Error in command payload", errs);
    }
    return commandsCopy;
}
Also used : CommandOperation(org.apache.solr.common.util.CommandOperation) IOException(java.io.IOException) StrUtils.formatString(org.apache.solr.common.util.StrUtils.formatString) HashMap(java.util.HashMap) Map(java.util.Map) ValidatingJsonMap(org.apache.solr.common.util.ValidatingJsonMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SolrException(org.apache.solr.common.SolrException) JsonSchemaValidator(org.apache.solr.util.JsonSchemaValidator)

Aggregations

IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 SolrException (org.apache.solr.common.SolrException)1 CommandOperation (org.apache.solr.common.util.CommandOperation)1 StrUtils.formatString (org.apache.solr.common.util.StrUtils.formatString)1 ValidatingJsonMap (org.apache.solr.common.util.ValidatingJsonMap)1 JsonSchemaValidator (org.apache.solr.util.JsonSchemaValidator)1