Search in sources :

Example 76 with RestRequest

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

the class RestNodesStatsAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    String[] nodesIds = Strings.splitStringByCommaToArray(request.param("nodeId"));
    Set<String> metrics = Strings.splitStringByCommaToSet(request.param("metric", "_all"));
    NodesStatsRequest nodesStatsRequest = new NodesStatsRequest(nodesIds);
    nodesStatsRequest.timeout(request.param("timeout"));
    if (metrics.size() == 1 && metrics.contains("_all")) {
        if (request.hasParam("index_metric")) {
            throw new IllegalArgumentException(String.format(Locale.ROOT, "request [%s] contains index metrics [%s] but all stats requested", request.path(), request.param("index_metric")));
        }
        nodesStatsRequest.all();
        nodesStatsRequest.indices(CommonStatsFlags.ALL);
    } else if (metrics.contains("_all")) {
        throw new IllegalArgumentException(String.format(Locale.ROOT, "request [%s] contains _all and individual metrics [%s]", request.path(), request.param("metric")));
    } else {
        nodesStatsRequest.clear();
        // use a sorted set so the unrecognized parameters appear in a reliable sorted order
        final Set<String> invalidMetrics = new TreeSet<>();
        for (final String metric : metrics) {
            final Consumer<NodesStatsRequest> handler = METRICS.get(metric);
            if (handler != null) {
                handler.accept(nodesStatsRequest);
            } else {
                invalidMetrics.add(metric);
            }
        }
        if (!invalidMetrics.isEmpty()) {
            throw new IllegalArgumentException(unrecognized(request, invalidMetrics, METRICS.keySet(), "metric"));
        }
        // check for index specific metrics
        if (metrics.contains("indices")) {
            Set<String> indexMetrics = Strings.splitStringByCommaToSet(request.param("index_metric", "_all"));
            if (indexMetrics.size() == 1 && indexMetrics.contains("_all")) {
                nodesStatsRequest.indices(CommonStatsFlags.ALL);
            } else {
                CommonStatsFlags flags = new CommonStatsFlags();
                flags.clear();
                // use a sorted set so the unrecognized parameters appear in a reliable sorted order
                final Set<String> invalidIndexMetrics = new TreeSet<>();
                for (final String indexMetric : indexMetrics) {
                    final Consumer<CommonStatsFlags> handler = FLAGS.get(indexMetric);
                    if (handler != null) {
                        handler.accept(flags);
                    } else {
                        invalidIndexMetrics.add(indexMetric);
                    }
                }
                if (!invalidIndexMetrics.isEmpty()) {
                    throw new IllegalArgumentException(unrecognized(request, invalidIndexMetrics, FLAGS.keySet(), "index metric"));
                }
                nodesStatsRequest.indices(flags);
            }
        } else if (request.hasParam("index_metric")) {
            throw new IllegalArgumentException(String.format(Locale.ROOT, "request [%s] contains index metrics [%s] but indices stats not requested", request.path(), request.param("index_metric")));
        }
    }
    if (nodesStatsRequest.indices().isSet(Flag.FieldData) && (request.hasParam("fields") || request.hasParam("fielddata_fields"))) {
        nodesStatsRequest.indices().fieldDataFields(request.paramAsStringArray("fielddata_fields", request.paramAsStringArray("fields", null)));
    }
    if (nodesStatsRequest.indices().isSet(Flag.Completion) && (request.hasParam("fields") || request.hasParam("completion_fields"))) {
        nodesStatsRequest.indices().completionDataFields(request.paramAsStringArray("completion_fields", request.paramAsStringArray("fields", null)));
    }
    if (nodesStatsRequest.indices().isSet(Flag.Search) && (request.hasParam("groups"))) {
        nodesStatsRequest.indices().groups(request.paramAsStringArray("groups", null));
    }
    if (nodesStatsRequest.indices().isSet(Flag.Indexing) && (request.hasParam("types"))) {
        nodesStatsRequest.indices().types(request.paramAsStringArray("types", null));
    }
    if (nodesStatsRequest.indices().isSet(Flag.Segments)) {
        nodesStatsRequest.indices().includeSegmentFileSizes(request.paramAsBoolean("include_segment_file_sizes", false));
    }
    return channel -> client.admin().cluster().nodesStats(nodesStatsRequest, new NodesResponseRestListener<>(channel));
}
Also used : NodesStatsRequest(org.elasticsearch.action.admin.cluster.node.stats.NodesStatsRequest) BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) GET(org.elasticsearch.rest.RestRequest.Method.GET) Set(java.util.Set) IOException(java.io.IOException) HashMap(java.util.HashMap) RestController(org.elasticsearch.rest.RestController) Flag(org.elasticsearch.action.admin.indices.stats.CommonStatsFlags.Flag) NodesStatsRequest(org.elasticsearch.action.admin.cluster.node.stats.NodesStatsRequest) TreeSet(java.util.TreeSet) Strings(org.elasticsearch.common.Strings) Consumer(java.util.function.Consumer) NodesResponseRestListener(org.elasticsearch.rest.action.RestActions.NodesResponseRestListener) Settings(org.elasticsearch.common.settings.Settings) Locale(java.util.Locale) Map(java.util.Map) RestRequest(org.elasticsearch.rest.RestRequest) CommonStatsFlags(org.elasticsearch.action.admin.indices.stats.CommonStatsFlags) NodeClient(org.elasticsearch.client.node.NodeClient) Collections(java.util.Collections) Set(java.util.Set) TreeSet(java.util.TreeSet) Consumer(java.util.function.Consumer) CommonStatsFlags(org.elasticsearch.action.admin.indices.stats.CommonStatsFlags)

Example 77 with RestRequest

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

the class RestPendingClusterTasksAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    PendingClusterTasksRequest pendingClusterTasksRequest = new PendingClusterTasksRequest();
    pendingClusterTasksRequest.masterNodeTimeout(request.paramAsTime("master_timeout", pendingClusterTasksRequest.masterNodeTimeout()));
    pendingClusterTasksRequest.local(request.paramAsBoolean("local", pendingClusterTasksRequest.local()));
    return channel -> client.admin().cluster().pendingClusterTasks(pendingClusterTasksRequest, new RestToXContentListener<>(channel));
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) PendingClusterTasksRequest(org.elasticsearch.action.admin.cluster.tasks.PendingClusterTasksRequest) Settings(org.elasticsearch.common.settings.Settings) RestToXContentListener(org.elasticsearch.rest.action.RestToXContentListener) RestRequest(org.elasticsearch.rest.RestRequest) NodeClient(org.elasticsearch.client.node.NodeClient) IOException(java.io.IOException) RestController(org.elasticsearch.rest.RestController) PendingClusterTasksRequest(org.elasticsearch.action.admin.cluster.tasks.PendingClusterTasksRequest)

Example 78 with RestRequest

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

the class RestPutRepositoryAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    PutRepositoryRequest putRepositoryRequest = putRepositoryRequest(request.param("repository"));
    try (XContentParser parser = request.contentParser()) {
        putRepositoryRequest.source(parser.mapOrdered());
    }
    putRepositoryRequest.verify(request.paramAsBoolean("verify", true));
    putRepositoryRequest.masterNodeTimeout(request.paramAsTime("master_timeout", putRepositoryRequest.masterNodeTimeout()));
    putRepositoryRequest.timeout(request.paramAsTime("timeout", putRepositoryRequest.timeout()));
    return channel -> client.admin().cluster().putRepository(putRepositoryRequest, new AcknowledgedRestListener<>(channel));
}
Also used : XContentParser(org.elasticsearch.common.xcontent.XContentParser) BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) PutRepositoryRequest(org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryRequest) POST(org.elasticsearch.rest.RestRequest.Method.POST) Settings(org.elasticsearch.common.settings.Settings) Requests.putRepositoryRequest(org.elasticsearch.client.Requests.putRepositoryRequest) RestRequest(org.elasticsearch.rest.RestRequest) NodeClient(org.elasticsearch.client.node.NodeClient) IOException(java.io.IOException) RestController(org.elasticsearch.rest.RestController) PUT(org.elasticsearch.rest.RestRequest.Method.PUT) AcknowledgedRestListener(org.elasticsearch.rest.action.AcknowledgedRestListener) PutRepositoryRequest(org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryRequest) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 79 with RestRequest

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

the class RestPutStoredScriptAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
    String id = request.param("id");
    String lang = request.param("lang");
    // name ordering issues in the handlers' paths.
    if (id == null) {
        id = lang;
        lang = null;
    }
    BytesReference content = request.content();
    if (lang != null) {
        deprecationLogger.deprecated("specifying lang [" + lang + "] as part of the url path is deprecated, use request content instead");
    }
    PutStoredScriptRequest putRequest = new PutStoredScriptRequest(id, lang, content, request.getXContentType());
    return channel -> client.admin().cluster().putStoredScript(putRequest, new AcknowledgedRestListener<>(channel));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) POST(org.elasticsearch.rest.RestRequest.Method.POST) PutStoredScriptRequest(org.elasticsearch.action.admin.cluster.storedscripts.PutStoredScriptRequest) Settings(org.elasticsearch.common.settings.Settings) RestRequest(org.elasticsearch.rest.RestRequest) NodeClient(org.elasticsearch.client.node.NodeClient) IOException(java.io.IOException) RestController(org.elasticsearch.rest.RestController) BytesReference(org.elasticsearch.common.bytes.BytesReference) PUT(org.elasticsearch.rest.RestRequest.Method.PUT) AcknowledgedRestListener(org.elasticsearch.rest.action.AcknowledgedRestListener) PutStoredScriptRequest(org.elasticsearch.action.admin.cluster.storedscripts.PutStoredScriptRequest)

Example 80 with RestRequest

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

the class RestClearIndicesCacheAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    ClearIndicesCacheRequest clearIndicesCacheRequest = new ClearIndicesCacheRequest(Strings.splitStringByCommaToArray(request.param("index")));
    clearIndicesCacheRequest.indicesOptions(IndicesOptions.fromRequest(request, clearIndicesCacheRequest.indicesOptions()));
    fromRequest(request, clearIndicesCacheRequest);
    return channel -> client.admin().indices().clearCache(clearIndicesCacheRequest, new RestBuilderListener<ClearIndicesCacheResponse>(channel) {

        @Override
        public RestResponse buildResponse(ClearIndicesCacheResponse response, XContentBuilder builder) throws Exception {
            builder.startObject();
            buildBroadcastShardsHeader(builder, request, response);
            builder.endObject();
            return new BytesRestResponse(OK, builder);
        }
    });
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) 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) ClearIndicesCacheResponse(org.elasticsearch.action.admin.indices.cache.clear.ClearIndicesCacheResponse) Strings(org.elasticsearch.common.Strings) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) POST(org.elasticsearch.rest.RestRequest.Method.POST) Settings(org.elasticsearch.common.settings.Settings) RestActions.buildBroadcastShardsHeader(org.elasticsearch.rest.action.RestActions.buildBroadcastShardsHeader) Map(java.util.Map) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) RestRequest(org.elasticsearch.rest.RestRequest) OK(org.elasticsearch.rest.RestStatus.OK) NodeClient(org.elasticsearch.client.node.NodeClient) ClearIndicesCacheRequest(org.elasticsearch.action.admin.indices.cache.clear.ClearIndicesCacheRequest) ParseField(org.elasticsearch.common.ParseField) ClearIndicesCacheResponse(org.elasticsearch.action.admin.indices.cache.clear.ClearIndicesCacheResponse) RestResponse(org.elasticsearch.rest.RestResponse) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) ClearIndicesCacheRequest(org.elasticsearch.action.admin.indices.cache.clear.ClearIndicesCacheRequest) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) IOException(java.io.IOException)

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