private ConfigOverlay applyRequestHandler(CommandOperation op, ConfigOverlay overlay) { String name = op.getStr(NAME); op.getStr(CLASS_NAME); op.getMap(PluginInfo.DEFAULTS, null); op.getMap(PluginInfo.INVARIANTS, null); op.getMap(PluginInfo.APPENDS, null); if (op.hasError()) return overlay; if (CREATE_REQHANDLER.equals(op.name)) { if (overlay.getReqHandlers().containsKey(name)) { op.addError( MessageFormat.format( " ''{0}'' already exists . Do an ''{1}'' , if you want to change it ", name, UPDATE_REQHANDLER)); return overlay; } else { return overlay.addReqHandler(op.getDataMap()); } } else if (UPDATE_REQHANDLER.equals(op.name)) { if (!overlay.getReqHandlers().containsKey(name)) { op.addError( MessageFormat.format( " ''{0}'' does not exist . Do an ''{1}'' , if you want to create it ", name, CREATE_REQHANDLER)); return overlay; } else { return overlay.addReqHandler(op.getDataMap()); } } return overlay; }
private ConfigOverlay applySetUserProp(CommandOperation op, ConfigOverlay overlay) { Map<String, Object> m = op.getDataMap(); if (op.hasError()) return overlay; for (Map.Entry<String, Object> e : m.entrySet()) { String name = e.getKey(); Object val = e.getValue(); overlay = overlay.setUserProperty(name, val); } return overlay; }
private ConfigOverlay applyDeleteHandler(CommandOperation op, ConfigOverlay overlay) { String name = op.getStr(CommandOperation.ROOT_OBJ); if (op.hasError()) return overlay; if (overlay.getReqHandlers().containsKey(name)) { return overlay.deleteHandler(name); } else { op.addError(MessageFormat.format("NO such requestHandler ''{0}'' ", name)); return overlay; } }
private ConfigOverlay applyUnsetUserProp(CommandOperation op, ConfigOverlay overlay) { List<String> name = op.getStrs(CommandOperation.ROOT_OBJ); if (op.hasError()) return overlay; for (String o : name) { if (!overlay.getUserProps().containsKey(o)) { op.addError(format("No such property ''{0}''", name)); } else { overlay = overlay.unsetUserProperty(o); } } return overlay; }
private ConfigOverlay applyUnset(CommandOperation op, ConfigOverlay overlay) { List<String> name = op.getStrs(CommandOperation.ROOT_OBJ); if (op.hasError()) return overlay; for (String o : name) { if (!ConfigOverlay.isEditableProp(o, false, null)) { op.addError(format(NOT_EDITABLE, name)); } else { overlay = overlay.unsetProperty(o); } } return overlay; }
private ConfigOverlay applySetProp(CommandOperation op, ConfigOverlay overlay) { Map<String, Object> m = op.getDataMap(); if (op.hasError()) return overlay; for (Map.Entry<String, Object> e : m.entrySet()) { String name = e.getKey(); Object val = e.getValue(); if (!ConfigOverlay.isEditableProp(name, false, null)) { op.addError(format(NOT_EDITABLE, name)); continue; } overlay = overlay.setProperty(name, val); } return overlay; }
private void handlePOST() throws IOException { Iterable<ContentStream> streams = req.getContentStreams(); if (streams == null) { throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "missing content stream"); } ArrayList<CommandOperation> ops = new ArrayList<>(); for (ContentStream stream : streams) ops.addAll(CommandOperation.parse(stream.getReader())); List<Map> errList = CommandOperation.captureErrors(ops); if (!errList.isEmpty()) { resp.add(CommandOperation.ERR_MSGS, errList); return; } try { for (; ; ) { ArrayList<CommandOperation> opsCopy = new ArrayList<>(ops.size()); for (CommandOperation op : ops) opsCopy.add(op.getCopy()); try { if (parts.size() > 1 && RequestParams.NAME.equals(parts.get(1))) { RequestParams params = RequestParams.getFreshRequestParams( req.getCore().getResourceLoader(), req.getCore().getSolrConfig().getRequestParams()); handleParams(opsCopy, params); } else { ConfigOverlay overlay = SolrConfig.getConfigOverlay(req.getCore().getResourceLoader()); handleCommands(opsCopy, overlay); } break; // succeeded . so no need to go over the loop again } catch (ZkController.ResourceModifiedInZkException e) { // retry log.info( "Race condition, the node is modified in ZK by someone else " + e.getMessage()); } } } catch (Exception e) { resp.setException(e); resp.add(CommandOperation.ERR_MSGS, singletonList(SchemaManager.getErrorStr(e))); } }
private void handleCommands(List<CommandOperation> ops, ConfigOverlay overlay) throws IOException { for (CommandOperation op : ops) { switch (op.name) { case SET_PROPERTY: overlay = applySetProp(op, overlay); break; case UNSET_PROPERTY: overlay = applyUnset(op, overlay); break; case SET_USER_PROPERTY: overlay = applySetUserProp(op, overlay); break; case UNSET_USER_PROPERTY: overlay = applyUnsetUserProp(op, overlay); break; case UPDATE_REQHANDLER: case CREATE_REQHANDLER: overlay = applyRequestHandler(op, overlay); break; case DELETE_REQHANDLER: overlay = applyDeleteHandler(op, overlay); break; } } List errs = CommandOperation.captureErrors(ops); if (!errs.isEmpty()) { resp.add(CommandOperation.ERR_MSGS, errs); return; } SolrResourceLoader loader = req.getCore().getResourceLoader(); if (loader instanceof ZkSolrResourceLoader) { ZkController.persistConfigResourceToZooKeeper( loader, overlay.getZnodeVersion(), ConfigOverlay.RESOURCE_NAME, overlay.toByteArray(), true); } else { SolrResourceLoader.persistConfLocally( loader, ConfigOverlay.RESOURCE_NAME, overlay.toByteArray()); req.getCore().getCoreDescriptor().getCoreContainer().reload(req.getCore().getName()); } }
private void handleParams(ArrayList<CommandOperation> ops, RequestParams params) { for (CommandOperation op : ops) { switch (op.name) { case CREATE: case UPDATE: { Map<String, Object> map = op.getDataMap(); if (op.hasError()) break; for (Map.Entry<String, Object> entry : map.entrySet()) { Map val = null; String key = entry.getKey(); if (key == null || key.trim().isEmpty()) { op.addError("null key "); continue; } key = key.trim(); if (!validName(key)) { op.addError( MessageFormat.format( "''{0}'' name should only have chars [a-zA-Z_-.0-9] ", key)); continue; } try { val = (Map) entry.getValue(); } catch (Exception e1) { op.addError("invalid params for key : " + key); continue; } if (val.containsKey("")) { op.addError("Empty keys are not allowed in params"); continue; } MapSolrParams old = params.getParams(key); if (op.name.equals(UPDATE)) { LinkedHashMap m = new LinkedHashMap(old.getMap()); m.putAll(val); val = m; } params = params.setParams(key, val); } break; } case "delete": { List<String> name = op.getStrs(CommandOperation.ROOT_OBJ); if (op.hasError()) break; for (String s : name) { if (params.getParams(s) == null) { op.addError( MessageFormat.format("can't delete . No such params ''{0}'' exist", s)); } params = params.setParams(s, null); } } } } List errs = CommandOperation.captureErrors(ops); if (!errs.isEmpty()) { resp.add(CommandOperation.ERR_MSGS, errs); return; } SolrResourceLoader loader = req.getCore().getResourceLoader(); if (loader instanceof ZkSolrResourceLoader) { ZkController.persistConfigResourceToZooKeeper( loader, params.getZnodeVersion(), RequestParams.RESOURCE, params.toByteArray(), true); } else { SolrResourceLoader.persistConfLocally( loader, ConfigOverlay.RESOURCE_NAME, params.toByteArray()); req.getCore().getSolrConfig().refreshRequestParams(); } }