public CommandOperation getCopy() { return new CommandOperation(name, commandData); }
public static List<Map> captureErrors(List<CommandOperation> ops) { List<Map> errors = new ArrayList<>(); for (CommandOperation op : ops) { if (op.hasError()) { errors.add(op.errorDetails()); } } return errors; }
public String getStr(String key, String def) { if (ROOT_OBJ.equals(key)) { Object obj = getRootPrimitive(); return obj == def ? null : String.valueOf(obj); } Object o = getMapVal(key); return o == null ? def : String.valueOf(o); }
/** * Get all the values from the metadata for the command * without the specified keys */ public Map<String, Object> getValuesExcluding(String... keys) { getMapVal(null); if (hasError()) return emptyMap();//just to verify the type is Map @SuppressWarnings("unchecked") LinkedHashMap<String, Object> cp = new LinkedHashMap<>((Map<String, Object>) commandData); if (keys == null) return cp; for (String key : keys) { cp.remove(key); } return cp; }
public Integer getInt(String name, Integer def) { Object o = getVal(name); if (o == null) return def; if (o instanceof Number) { Number number = (Number) o; return number.intValue(); } else { try { return Integer.parseInt(o.toString()); } catch (NumberFormatException e) { addError(StrUtils.formatString("{0} is not a valid integer", name)); return null; } } }
/** * Get a required field. If missing it adds to the errors */ public String getStr(String key) { if (ROOT_OBJ.equals(key)) { Object obj = getRootPrimitive(); if (obj == null) { errors.add(StrUtils.formatString(REQD, name)); } return obj == null ? null : String.valueOf(obj); } String s = getStr(key, null); if (s == null) errors.add(StrUtils.formatString(REQD, key)); return s; }
/** * Read commands from request streams * * @param streams the streams * @param resp solr query response * @param singletonCommands , commands that cannot be repeated * @return parsed list of commands * @throws IOException if there is an error while parsing the stream */ public static List<CommandOperation> readCommands(Iterable<ContentStream> streams, NamedList resp, Set<String> singletonCommands) throws IOException { if (streams == null) { throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "missing content stream"); } ArrayList<CommandOperation> ops = new ArrayList<>(); for (ContentStream stream : streams) { if ("application/javabin".equals(stream.getContentType())) { ops.addAll(parse(stream.getStream(), singletonCommands)); } else { ops.addAll(parse(stream.getReader(), singletonCommands)); } } List<Map> errList = CommandOperation.captureErrors(ops); if (!errList.isEmpty()) { resp.add(CommandOperation.ERR_MSGS, errList); return null; } return ops; }
private Object getMapVal(String key) { if ("".equals(key)) { if (commandData instanceof Map) { addError("value of the command is an object should be primitive"); } return commandData; } if (commandData instanceof Map) { Map metaData = (Map) commandData; return metaData.get(key); } else { String msg = " value has to be an object for operation :" + name; if (!errors.contains(msg)) errors.add(msg); return null; } }
public Object getVal(String key) { return getMapVal(key); }
public boolean getBoolean(String key, boolean def) { String v = getStr(key, null); return v == null ? def : Boolean.parseBoolean(v); }
public static List<CommandOperation> clone(List<CommandOperation> ops) { List<CommandOperation> opsCopy = new ArrayList<>(ops.size()); for (CommandOperation op : ops) opsCopy.add(op.getCopy()); return opsCopy; }
private static Collection<String> getParamNames_(CommandOperation op, CommandMeta command) { Object o = op.getCommandData(); if (o instanceof Map) { Map map = (Map) o; List<String> result = new ArrayList<>(); collectKeyNames(map, result, ""); return result; } else { return Collections.emptySet(); } }
/** * Get collection of values for a key. If only one val is present a * single value collection is returned */ public List<String> getStrs(String key, List<String> def) { Object v = null; if (ROOT_OBJ.equals(key)) { v = getRootPrimitive(); } else { v = getMapVal(key); } if (v == null) { return def; } else { if (v instanceof List) { ArrayList<String> l = new ArrayList<>(); for (Object o : (List) v) { l.add(String.valueOf(o)); } if (l.isEmpty()) return def; return l; } else { return singletonList(String.valueOf(v)); } } }
public Map<String, Object> getDataMap() { if (commandData instanceof Map) { //noinspection unchecked return (Map<String, Object>) commandData; } addError(StrUtils.formatString("The command ''{0}'' should have the values as a json object '{'key:val'}' format but is ''{1}''", name, commandData)); return Collections.emptyMap(); }
public void unknownOperation() { addError(formatString("Unknown operation ''{0}'' ", name)); }
@Override public Object put(Object key, Object value) { List vals = null; if (value instanceof List && !singletonCommands.contains(key)) { vals = (List) value; } else { vals = Collections.singletonList(value); } for (Object val : vals) { operations.add(new CommandOperation(String.valueOf(key), val)); } return null; } };
for (Object o : list) { if (!(o instanceof Map)) { operations.add(new CommandOperation(String.valueOf(key), list)); break; } else { operations.add(new CommandOperation(String.valueOf(key), o)); operations.add(new CommandOperation(String.valueOf(key), val));