Search in sources :

Example 1 with CommandOperation

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;
}
Also used : LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) MapSolrParams(org.apache.solr.common.params.MapSolrParams) HashMap(java.util.HashMap) CommandOperation(org.apache.solr.common.util.CommandOperation) CompositeApi(org.apache.solr.api.V2HttpCall.CompositeApi) CompositeApi(org.apache.solr.api.V2HttpCall.CompositeApi) Api(org.apache.solr.api.Api)

Example 2 with CommandOperation

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());
}
Also used : CommandOperation(org.apache.solr.common.util.CommandOperation) StringReader(java.io.StringReader) Test(org.junit.Test)

Example 3 with CommandOperation

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());
}
Also used : CommandOperation(org.apache.solr.common.util.CommandOperation) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with CommandOperation

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);
            }
        }
    };
}
Also used : SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) CommandOperation(org.apache.solr.common.util.CommandOperation) SolrRequest(org.apache.solr.client.solrj.SolrRequest) SolrException(org.apache.solr.common.SolrException) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrParams(org.apache.solr.common.params.SolrParams) Api(org.apache.solr.api.Api) SolrException(org.apache.solr.common.SolrException)

Example 5 with CommandOperation

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");
}
Also used : ConfigEditablePlugin(org.apache.solr.security.ConfigEditablePlugin) CommandOperation(org.apache.solr.common.util.CommandOperation) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) SolrException(org.apache.solr.common.SolrException)

Aggregations

CommandOperation (org.apache.solr.common.util.CommandOperation)13 Map (java.util.Map)5 SolrException (org.apache.solr.common.SolrException)5 LinkedHashMap (java.util.LinkedHashMap)4 HashMap (java.util.HashMap)3 Api (org.apache.solr.api.Api)3 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)3 IOException (java.io.IOException)2 StringReader (java.io.StringReader)2 Collections.singletonList (java.util.Collections.singletonList)2 Collections.singletonMap (java.util.Collections.singletonMap)2 List (java.util.List)2 SolrParams (org.apache.solr.common.params.SolrParams)2 ValidatingJsonMap (org.apache.solr.common.util.ValidatingJsonMap)2 LocalSolrQueryRequest (org.apache.solr.request.LocalSolrQueryRequest)2 File (java.io.File)1 StringWriter (java.io.StringWriter)1 URL (java.net.URL)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ApiBag (org.apache.solr.api.ApiBag)1