Search in sources :

Example 61 with RestRequest

use of org.elasticsearch.rest.RestRequest in project elasticsearch by elastic.

the class ActionModuleTests method testPluginCanRegisterRestHandler.

public void testPluginCanRegisterRestHandler() {
    class FakeHandler implements RestHandler {

        FakeHandler(RestController restController) {
            restController.registerHandler(Method.GET, "/_dummy", this);
        }

        @Override
        public void handleRequest(RestRequest request, RestChannel channel, NodeClient client) throws Exception {
        }
    }
    ActionPlugin registersFakeHandler = new ActionPlugin() {

        @Override
        public List<RestHandler> getRestHandlers(Settings settings, RestController restController, ClusterSettings clusterSettings, IndexScopedSettings indexScopedSettings, SettingsFilter settingsFilter, IndexNameExpressionResolver indexNameExpressionResolver, Supplier<DiscoveryNodes> nodesInCluster) {
            return singletonList(new FakeHandler(restController));
        }
    };
    SettingsModule settings = new SettingsModule(Settings.EMPTY);
    ThreadPool threadPool = new TestThreadPool(getTestName());
    try {
        ActionModule actionModule = new ActionModule(false, settings.getSettings(), new IndexNameExpressionResolver(Settings.EMPTY), settings.getIndexScopedSettings(), settings.getClusterSettings(), settings.getSettingsFilter(), threadPool, singletonList(registersFakeHandler), null, null);
        actionModule.initRestHandlers(null);
        // At this point the easiest way to confirm that a handler is loaded is to try to register another one on top of it and to fail
        Exception e = expectThrows(IllegalArgumentException.class, () -> actionModule.getRestController().registerHandler(Method.GET, "/_dummy", null));
        assertThat(e.getMessage(), startsWith("Path [/_dummy] already has a value [" + FakeHandler.class.getName()));
    } finally {
        threadPool.shutdown();
    }
}
Also used : NodeClient(org.elasticsearch.client.node.NodeClient) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) IndexScopedSettings(org.elasticsearch.common.settings.IndexScopedSettings) ActionPlugin(org.elasticsearch.plugins.ActionPlugin) ThreadPool(org.elasticsearch.threadpool.ThreadPool) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) RestController(org.elasticsearch.rest.RestController) RestChannel(org.elasticsearch.rest.RestChannel) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) IOException(java.io.IOException) SettingsFilter(org.elasticsearch.common.settings.SettingsFilter) RestRequest(org.elasticsearch.rest.RestRequest) RestHandler(org.elasticsearch.rest.RestHandler) SettingsModule(org.elasticsearch.common.settings.SettingsModule) Supplier(java.util.function.Supplier) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver) Settings(org.elasticsearch.common.settings.Settings) IndexScopedSettings(org.elasticsearch.common.settings.IndexScopedSettings) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings)

Example 62 with RestRequest

use of org.elasticsearch.rest.RestRequest in project elasticsearch by elastic.

the class RestDeleteSearchTemplateAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
    String id = request.param("id");
    DeleteStoredScriptRequest deleteStoredScriptRequest = new DeleteStoredScriptRequest(id, Script.DEFAULT_TEMPLATE_LANG);
    return channel -> client.admin().cluster().deleteStoredScript(deleteStoredScriptRequest, new AcknowledgedRestListener<>(channel));
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) Script(org.elasticsearch.script.Script) Settings(org.elasticsearch.common.settings.Settings) DELETE(org.elasticsearch.rest.RestRequest.Method.DELETE) RestRequest(org.elasticsearch.rest.RestRequest) DeleteStoredScriptRequest(org.elasticsearch.action.admin.cluster.storedscripts.DeleteStoredScriptRequest) NodeClient(org.elasticsearch.client.node.NodeClient) IOException(java.io.IOException) RestController(org.elasticsearch.rest.RestController) AcknowledgedRestListener(org.elasticsearch.rest.action.AcknowledgedRestListener) DeleteStoredScriptRequest(org.elasticsearch.action.admin.cluster.storedscripts.DeleteStoredScriptRequest)

Example 63 with RestRequest

use of org.elasticsearch.rest.RestRequest in project elasticsearch by elastic.

the class RestGetSearchTemplateAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, NodeClient client) throws IOException {
    String id = request.param("id");
    GetStoredScriptRequest getRequest = new GetStoredScriptRequest(id, Script.DEFAULT_TEMPLATE_LANG);
    return channel -> client.admin().cluster().getStoredScript(getRequest, new RestBuilderListener<GetStoredScriptResponse>(channel) {

        @Override
        public RestResponse buildResponse(GetStoredScriptResponse response, XContentBuilder builder) throws Exception {
            builder.startObject();
            builder.field(_ID_PARSE_FIELD.getPreferredName(), id);
            builder.field(StoredScriptSource.LANG_PARSE_FIELD.getPreferredName(), Script.DEFAULT_TEMPLATE_LANG);
            StoredScriptSource source = response.getSource();
            boolean found = source != null;
            builder.field(FOUND_PARSE_FIELD.getPreferredName(), found);
            if (found) {
                builder.field(StoredScriptSource.TEMPLATE_PARSE_FIELD.getPreferredName(), source.getCode());
            }
            builder.endObject();
            return new BytesRestResponse(found ? RestStatus.OK : RestStatus.NOT_FOUND, builder);
        }
    });
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) GetStoredScriptRequest(org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptRequest) Script(org.elasticsearch.script.Script) GET(org.elasticsearch.rest.RestRequest.Method.GET) RestResponse(org.elasticsearch.rest.RestResponse) RestBuilderListener(org.elasticsearch.rest.action.RestBuilderListener) IOException(java.io.IOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) RestController(org.elasticsearch.rest.RestController) GetStoredScriptResponse(org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptResponse) StoredScriptSource(org.elasticsearch.script.StoredScriptSource) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) Settings(org.elasticsearch.common.settings.Settings) RestStatus(org.elasticsearch.rest.RestStatus) RestRequest(org.elasticsearch.rest.RestRequest) NodeClient(org.elasticsearch.client.node.NodeClient) ParseField(org.elasticsearch.common.ParseField) RestResponse(org.elasticsearch.rest.RestResponse) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) GetStoredScriptRequest(org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptRequest) StoredScriptSource(org.elasticsearch.script.StoredScriptSource) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) IOException(java.io.IOException) GetStoredScriptResponse(org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptResponse)

Example 64 with RestRequest

use of org.elasticsearch.rest.RestRequest in project elasticsearch by elastic.

the class MultiSearchTemplateRequestTests method testParseWithCarriageReturn.

public void testParseWithCarriageReturn() throws Exception {
    final String content = "{\"index\":[\"test0\", \"test1\"], \"request_cache\": true}\r\n" + "{\"inline\": {\"query\" : {\"match_{{template}}\" :{}}}, \"params\": {\"template\": \"all\" } }\r\n";
    RestRequest restRequest = new FakeRestRequest.Builder(xContentRegistry()).withContent(new BytesArray(content), XContentType.JSON).build();
    MultiSearchTemplateRequest request = RestMultiSearchTemplateAction.parseRequest(restRequest, true);
    assertThat(request.requests().size(), equalTo(1));
    assertThat(request.requests().get(0).getRequest().indices()[0], equalTo("test0"));
    assertThat(request.requests().get(0).getRequest().indices()[1], equalTo("test1"));
    assertThat(request.requests().get(0).getRequest().indices(), arrayContaining("test0", "test1"));
    assertThat(request.requests().get(0).getRequest().requestCache(), equalTo(true));
    assertThat(request.requests().get(0).getRequest().preference(), nullValue());
    assertNotNull(request.requests().get(0).getScript());
    assertEquals(ScriptType.INLINE, request.requests().get(0).getScriptType());
    assertEquals("{\"query\":{\"match_{{template}}\":{}}}", request.requests().get(0).getScript());
    assertEquals(1, request.requests().get(0).getScriptParams().size());
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) RestRequest(org.elasticsearch.rest.RestRequest) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest)

Example 65 with RestRequest

use of org.elasticsearch.rest.RestRequest in project elasticsearch by elastic.

the class RestRethrottleAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    RethrottleRequest internalRequest = new RethrottleRequest();
    internalRequest.setTaskId(new TaskId(request.param("taskId")));
    Float requestsPerSecond = AbstractBaseReindexRestHandler.parseRequestsPerSecond(request);
    if (requestsPerSecond == null) {
        throw new IllegalArgumentException("requests_per_second is a required parameter");
    }
    internalRequest.setRequestsPerSecond(requestsPerSecond);
    final String groupBy = request.param("group_by", "nodes");
    return channel -> client.execute(RethrottleAction.INSTANCE, internalRequest, listTasksResponseListener(nodesInCluster, groupBy, channel));
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) POST(org.elasticsearch.rest.RestRequest.Method.POST) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) Settings(org.elasticsearch.common.settings.Settings) RestListTasksAction.listTasksResponseListener(org.elasticsearch.rest.action.admin.cluster.RestListTasksAction.listTasksResponseListener) RestRequest(org.elasticsearch.rest.RestRequest) NodeClient(org.elasticsearch.client.node.NodeClient) IOException(java.io.IOException) RestController(org.elasticsearch.rest.RestController) TaskId(org.elasticsearch.tasks.TaskId) Supplier(java.util.function.Supplier) TaskId(org.elasticsearch.tasks.TaskId)

Aggregations

RestRequest (org.elasticsearch.rest.RestRequest)133 Settings (org.elasticsearch.common.settings.Settings)110 RestController (org.elasticsearch.rest.RestController)110 NodeClient (org.elasticsearch.client.node.NodeClient)108 IOException (java.io.IOException)91 BaseRestHandler (org.elasticsearch.rest.BaseRestHandler)88 Strings (org.elasticsearch.common.Strings)61 GET (org.elasticsearch.rest.RestRequest.Method.GET)57 RestResponse (org.elasticsearch.rest.RestResponse)50 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)41 BytesRestResponse (org.elasticsearch.rest.BytesRestResponse)41 POST (org.elasticsearch.rest.RestRequest.Method.POST)34 IndicesOptions (org.elasticsearch.action.support.IndicesOptions)30 RestBuilderListener (org.elasticsearch.rest.action.RestBuilderListener)27 OK (org.elasticsearch.rest.RestStatus.OK)23 AcknowledgedRestListener (org.elasticsearch.rest.action.AcknowledgedRestListener)23 FakeRestRequest (org.elasticsearch.test.rest.FakeRestRequest)21 RestResponseListener (org.elasticsearch.rest.action.RestResponseListener)20 Table (org.elasticsearch.common.Table)19 HashMap (java.util.HashMap)17