use of org.apache.solr.common.util.CommandOperation in project lucene-solr by apache.
the class TestApiFramework method invoke.
private SolrQueryResponse invoke(PluginBag<SolrRequestHandler> reqHandlers, String path, String fullPath, SolrRequest.METHOD method, CoreContainer mockCC) {
HashMap<String, String> parts = new HashMap<>();
boolean containerHandlerLookup = mockCC.getRequestHandlers() == reqHandlers;
path = path == null ? fullPath : path;
Api api = null;
if (containerHandlerLookup) {
api = V2HttpCall.getApiInfo(reqHandlers, path, "GET", fullPath, parts);
} else {
api = V2HttpCall.getApiInfo(mockCC.getRequestHandlers(), fullPath, "GET", fullPath, parts);
if (api == null)
api = new CompositeApi(null);
if (api instanceof CompositeApi) {
CompositeApi compositeApi = (CompositeApi) api;
api = V2HttpCall.getApiInfo(reqHandlers, path, "GET", fullPath, parts);
compositeApi.add(api);
api = compositeApi;
}
}
SolrQueryResponse rsp = new SolrQueryResponse();
LocalSolrQueryRequest req = new LocalSolrQueryRequest(null, new MapSolrParams(new HashMap<>())) {
@Override
public List<CommandOperation> getCommands(boolean validateInput) {
return Collections.emptyList();
}
};
api.call(req, rsp);
return rsp;
}
use of org.apache.solr.common.util.CommandOperation in project lucene-solr by apache.
the class TestSchemaManager method testParsing.
@Test
public void testParsing() throws IOException {
String x = "{\n" + " 'add-field' : {\n" + " 'name':'a',\n" + " 'type': 'string',\n" + " 'stored':true,\n" + " 'indexed':false\n" + " },\n" + " 'add-field' : {\n" + " 'name':'b',\n" + " 'type': 'string',\n" + " 'stored':true,\n" + " 'indexed':false\n" + " }\n" + "\n" + "}";
List<CommandOperation> ops = CommandOperation.parse(new StringReader(json(x)));
assertEquals(2, ops.size());
assertTrue(CommandOperation.captureErrors(ops).isEmpty());
x = " {'add-field' : [{\n" + " 'name':'a1',\n" + " 'type': 'string',\n" + " 'stored':true,\n" + " 'indexed':false\n" + " },\n" + " {\n" + " 'name':'a2',\n" + " 'type': 'string',\n" + " 'stored':true,\n" + " 'indexed':true\n" + " }]\n" + " }";
ops = CommandOperation.parse(new StringReader(json(x)));
assertEquals(2, ops.size());
assertTrue(CommandOperation.captureErrors(ops).isEmpty());
}
use of org.apache.solr.common.util.CommandOperation in project lucene-solr by apache.
the class TestSha256AuthenticationProvider method testBasicAuthCommands.
public void testBasicAuthCommands() {
BasicAuthPlugin basicAuthPlugin = new BasicAuthPlugin();
basicAuthPlugin.init(Collections.emptyMap());
Map latestConf = new LinkedHashMap<>();
CommandOperation blockUnknown = new CommandOperation("set-property", singletonMap("blockUnknown", true));
basicAuthPlugin.edit(latestConf, Collections.singletonList(blockUnknown));
assertEquals(Boolean.TRUE, latestConf.get("blockUnknown"));
basicAuthPlugin.init(latestConf);
assertTrue(basicAuthPlugin.getBlockUnknown());
blockUnknown = new CommandOperation("set-property", singletonMap("blockUnknown", false));
basicAuthPlugin.edit(latestConf, Collections.singletonList(blockUnknown));
assertEquals(Boolean.FALSE, latestConf.get("blockUnknown"));
basicAuthPlugin.init(latestConf);
assertFalse(basicAuthPlugin.getBlockUnknown());
}
use of org.apache.solr.common.util.CommandOperation in project lucene-solr by apache.
the class BaseHandlerApiSupport method getApi.
private Api getApi(final V2EndPoint op) {
final BaseHandlerApiSupport apiHandler = this;
return new Api(ApiBag.getSpec(op.getSpecName())) {
@Override
public void call(SolrQueryRequest req, SolrQueryResponse rsp) {
SolrParams params = req.getParams();
SolrRequest.METHOD method = SolrRequest.METHOD.valueOf(req.getHttpMethod());
List<ApiCommand> commands = commandsMapping.get(method).get(op);
try {
if (method == POST) {
List<CommandOperation> cmds = req.getCommands(true);
if (cmds.size() > 1)
throw new SolrException(BAD_REQUEST, "Only one command is allowed");
CommandOperation c = cmds.size() == 0 ? null : cmds.get(0);
ApiCommand command = null;
String commandName = c == null ? null : c.name;
for (ApiCommand cmd : commands) {
if (Objects.equals(cmd.meta().getName(), commandName)) {
command = cmd;
break;
}
}
if (command == null) {
throw new SolrException(BAD_REQUEST, " no such command " + c);
}
wrapParams(req, c, command, false);
command.invoke(req, rsp, apiHandler);
} else {
if (commands == null || commands.isEmpty()) {
rsp.add("error", "No support for : " + method + " at :" + req.getPath());
return;
}
if (commands.size() > 1) {
for (ApiCommand command : commands) {
if (command.meta().getName().equals(req.getPath())) {
commands = Collections.singletonList(command);
break;
}
}
}
wrapParams(req, new CommandOperation("", Collections.EMPTY_MAP), commands.get(0), true);
commands.get(0).invoke(req, rsp, apiHandler);
}
} catch (SolrException e) {
throw e;
} catch (Exception e) {
throw new SolrException(BAD_REQUEST, e);
} finally {
req.setParams(params);
}
}
};
}
use of org.apache.solr.common.util.CommandOperation in project lucene-solr by apache.
the class SecurityConfHandler method doEdit.
private void doEdit(SolrQueryRequest req, SolrQueryResponse rsp, String path, final String key, final Object plugin) throws IOException {
ConfigEditablePlugin configEditablePlugin = null;
if (plugin == null) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "No " + key + " plugin configured");
}
if (plugin instanceof ConfigEditablePlugin) {
configEditablePlugin = (ConfigEditablePlugin) plugin;
} else {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, key + " plugin is not editable");
}
if (req.getContentStreams() == null) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "No contentStream");
}
List<CommandOperation> ops = CommandOperation.readCommands(req.getContentStreams(), rsp.getValues());
if (ops == null) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "No commands");
}
for (int count = 1; count <= 3; count++) {
SecurityConfig securityConfig = getSecurityConfig(true);
Map<String, Object> data = securityConfig.getData();
Map<String, Object> latestConf = (Map<String, Object>) data.get(key);
if (latestConf == null) {
throw new SolrException(SERVER_ERROR, "No configuration present for " + key);
}
List<CommandOperation> commandsCopy = CommandOperation.clone(ops);
Map<String, Object> out = configEditablePlugin.edit(Utils.getDeepCopy(latestConf, 4), commandsCopy);
if (out == null) {
List<Map> errs = CommandOperation.captureErrors(commandsCopy);
if (!errs.isEmpty()) {
rsp.add(CommandOperation.ERR_MSGS, errs);
return;
}
log.debug("No edits made");
return;
} else {
if (!Objects.equals(latestConf.get("class"), out.get("class"))) {
throw new SolrException(SERVER_ERROR, "class cannot be modified");
}
Map meta = getMapValue(out, "");
//encode the expected zkversion
meta.put("v", securityConfig.getVersion() + 1);
data.put(key, out);
if (persistConf(securityConfig)) {
securityConfEdited();
return;
}
}
log.debug("Security edit operation failed {} time(s)" + count);
}
throw new SolrException(SERVER_ERROR, "Failed to persist security config after 3 attempts. Giving up");
}
Aggregations