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();
}
}
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));
}
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);
}
});
}
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());
}
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));
}
Aggregations