Search in sources :

Example 1 with RestResponse

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

the class RestClusterAllocationExplainAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    ClusterAllocationExplainRequest req;
    if (request.hasContentOrSourceParam() == false) {
        // Empty request signals "explain the first unassigned shard you find"
        req = new ClusterAllocationExplainRequest();
    } else {
        try (XContentParser parser = request.contentOrSourceParamParser()) {
            req = ClusterAllocationExplainRequest.parse(parser);
        }
    }
    req.includeYesDecisions(request.paramAsBoolean("include_yes_decisions", false));
    req.includeDiskInfo(request.paramAsBoolean("include_disk_info", false));
    return channel -> client.admin().cluster().allocationExplain(req, new RestBuilderListener<ClusterAllocationExplainResponse>(channel) {

        @Override
        public RestResponse buildResponse(ClusterAllocationExplainResponse response, XContentBuilder builder) throws IOException {
            response.getExplanation().toXContent(builder, ToXContent.EMPTY_PARAMS);
            return new BytesRestResponse(RestStatus.OK, builder);
        }
    });
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) ClusterAllocationExplainResponse(org.elasticsearch.action.admin.cluster.allocation.ClusterAllocationExplainResponse) RestResponse(org.elasticsearch.rest.RestResponse) RestBuilderListener(org.elasticsearch.rest.action.RestBuilderListener) ToXContent(org.elasticsearch.common.xcontent.ToXContent) IOException(java.io.IOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) RestController(org.elasticsearch.rest.RestController) XContentParser(org.elasticsearch.common.xcontent.XContentParser) 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) ClusterAllocationExplainRequest(org.elasticsearch.action.admin.cluster.allocation.ClusterAllocationExplainRequest) ClusterAllocationExplainRequest(org.elasticsearch.action.admin.cluster.allocation.ClusterAllocationExplainRequest) ClusterAllocationExplainResponse(org.elasticsearch.action.admin.cluster.allocation.ClusterAllocationExplainResponse) RestResponse(org.elasticsearch.rest.RestResponse) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) IOException(java.io.IOException) XContentParser(org.elasticsearch.common.xcontent.XContentParser) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 2 with RestResponse

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

the class RestClusterGetSettingsAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    ClusterStateRequest clusterStateRequest = Requests.clusterStateRequest().routingTable(false).nodes(false);
    final boolean renderDefaults = request.paramAsBoolean("include_defaults", false);
    clusterStateRequest.local(request.paramAsBoolean("local", clusterStateRequest.local()));
    return channel -> client.admin().cluster().state(clusterStateRequest, new RestBuilderListener<ClusterStateResponse>(channel) {

        @Override
        public RestResponse buildResponse(ClusterStateResponse response, XContentBuilder builder) throws Exception {
            return new BytesRestResponse(RestStatus.OK, renderResponse(response.getState(), renderDefaults, builder, request));
        }
    });
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) SettingsFilter(org.elasticsearch.common.settings.SettingsFilter) RestResponse(org.elasticsearch.rest.RestResponse) RestBuilderListener(org.elasticsearch.rest.action.RestBuilderListener) ToXContent(org.elasticsearch.common.xcontent.ToXContent) Set(java.util.Set) IOException(java.io.IOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) RestController(org.elasticsearch.rest.RestController) ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) ClusterState(org.elasticsearch.cluster.ClusterState) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) Settings(org.elasticsearch.common.settings.Settings) RestStatus(org.elasticsearch.rest.RestStatus) RestRequest(org.elasticsearch.rest.RestRequest) ClusterStateRequest(org.elasticsearch.action.admin.cluster.state.ClusterStateRequest) NodeClient(org.elasticsearch.client.node.NodeClient) Requests(org.elasticsearch.client.Requests) ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) RestResponse(org.elasticsearch.rest.RestResponse) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) ClusterStateRequest(org.elasticsearch.action.admin.cluster.state.ClusterStateRequest) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) IOException(java.io.IOException)

Example 3 with RestResponse

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

the class RestClusterStateAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    final ClusterStateRequest clusterStateRequest = Requests.clusterStateRequest();
    clusterStateRequest.indicesOptions(IndicesOptions.fromRequest(request, clusterStateRequest.indicesOptions()));
    clusterStateRequest.local(request.paramAsBoolean("local", clusterStateRequest.local()));
    clusterStateRequest.masterNodeTimeout(request.paramAsTime("master_timeout", clusterStateRequest.masterNodeTimeout()));
    final String[] indices = Strings.splitStringByCommaToArray(request.param("indices", "_all"));
    boolean isAllIndicesOnly = indices.length == 1 && "_all".equals(indices[0]);
    if (!isAllIndicesOnly) {
        clusterStateRequest.indices(indices);
    }
    if (request.hasParam("metric")) {
        EnumSet<ClusterState.Metric> metrics = ClusterState.Metric.parseString(request.param("metric"), true);
        // do not ask for what we do not need.
        clusterStateRequest.nodes(metrics.contains(ClusterState.Metric.NODES) || metrics.contains(ClusterState.Metric.MASTER_NODE));
        /*
             * there is no distinction in Java api between routing_table and routing_nodes, it's the same info set over the wire, one single
             * flag to ask for it
             */
        clusterStateRequest.routingTable(metrics.contains(ClusterState.Metric.ROUTING_TABLE) || metrics.contains(ClusterState.Metric.ROUTING_NODES));
        clusterStateRequest.metaData(metrics.contains(ClusterState.Metric.METADATA));
        clusterStateRequest.blocks(metrics.contains(ClusterState.Metric.BLOCKS));
        clusterStateRequest.customs(metrics.contains(ClusterState.Metric.CUSTOMS));
    }
    settingsFilter.addFilterSettingParams(request);
    return channel -> client.admin().cluster().state(clusterStateRequest, new RestBuilderListener<ClusterStateResponse>(channel) {

        @Override
        public RestResponse buildResponse(ClusterStateResponse response, XContentBuilder builder) throws Exception {
            builder.startObject();
            builder.field(Fields.CLUSTER_NAME, response.getClusterName().value());
            builder.byteSizeField(Fields.CLUSTER_STATE_SIZE_IN_BYTES, Fields.CLUSTER_STATE_SIZE, response.getTotalCompressedSize());
            response.getState().toXContent(builder, request);
            builder.endObject();
            return new BytesRestResponse(RestStatus.OK, builder);
        }
    });
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) SettingsFilter(org.elasticsearch.common.settings.SettingsFilter) RestResponse(org.elasticsearch.rest.RestResponse) RestBuilderListener(org.elasticsearch.rest.action.RestBuilderListener) Set(java.util.Set) IOException(java.io.IOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) RestController(org.elasticsearch.rest.RestController) Strings(org.elasticsearch.common.Strings) ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) HashSet(java.util.HashSet) ClusterState(org.elasticsearch.cluster.ClusterState) Settings(org.elasticsearch.common.settings.Settings) RestStatus(org.elasticsearch.rest.RestStatus) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) RestRequest(org.elasticsearch.rest.RestRequest) ClusterStateRequest(org.elasticsearch.action.admin.cluster.state.ClusterStateRequest) NodeClient(org.elasticsearch.client.node.NodeClient) Requests(org.elasticsearch.client.Requests) Collections(java.util.Collections) EnumSet(java.util.EnumSet) ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) RestResponse(org.elasticsearch.rest.RestResponse) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) ClusterStateRequest(org.elasticsearch.action.admin.cluster.state.ClusterStateRequest) IOException(java.io.IOException) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 4 with RestResponse

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

the class RestFieldStatsAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    if (request.hasContentOrSourceParam() && request.hasParam("fields")) {
        throw new IllegalArgumentException("can't specify a request body and [fields] request parameter, " + "either specify a request body or the [fields] request parameter");
    }
    final FieldStatsRequest fieldStatsRequest = new FieldStatsRequest();
    fieldStatsRequest.indices(Strings.splitStringByCommaToArray(request.param("index")));
    fieldStatsRequest.indicesOptions(IndicesOptions.fromRequest(request, fieldStatsRequest.indicesOptions()));
    fieldStatsRequest.level(request.param("level", FieldStatsRequest.DEFAULT_LEVEL));
    if (request.hasContentOrSourceParam()) {
        try (XContentParser parser = request.contentOrSourceParamParser()) {
            fieldStatsRequest.source(parser);
        }
    } else {
        fieldStatsRequest.setFields(Strings.splitStringByCommaToArray(request.param("fields")));
    }
    return channel -> client.fieldStats(fieldStatsRequest, new RestBuilderListener<FieldStatsResponse>(channel) {

        @Override
        public RestResponse buildResponse(FieldStatsResponse response, XContentBuilder builder) throws Exception {
            builder.startObject();
            buildBroadcastShardsHeader(builder, request, response);
            builder.startObject("indices");
            for (Map.Entry<String, Map<String, FieldStats>> entry1 : response.getIndicesMergedFieldStats().entrySet()) {
                builder.startObject(entry1.getKey());
                builder.startObject("fields");
                for (Map.Entry<String, FieldStats> entry2 : entry1.getValue().entrySet()) {
                    builder.field(entry2.getKey());
                    entry2.getValue().toXContent(builder, request);
                }
                builder.endObject();
                builder.endObject();
            }
            builder.endObject();
            if (response.getConflicts().size() > 0) {
                builder.startObject("conflicts");
                for (Map.Entry<String, String> entry : response.getConflicts().entrySet()) {
                    builder.field(entry.getKey(), entry.getValue());
                }
                builder.endObject();
            }
            builder.endObject();
            return new BytesRestResponse(RestStatus.OK, builder);
        }
    });
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) GET(org.elasticsearch.rest.RestRequest.Method.GET) FieldStatsRequest(org.elasticsearch.action.fieldstats.FieldStatsRequest) RestResponse(org.elasticsearch.rest.RestResponse) IOException(java.io.IOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) RestController(org.elasticsearch.rest.RestController) Strings(org.elasticsearch.common.Strings) XContentParser(org.elasticsearch.common.xcontent.XContentParser) 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) RestStatus(org.elasticsearch.rest.RestStatus) Map(java.util.Map) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) RestRequest(org.elasticsearch.rest.RestRequest) NodeClient(org.elasticsearch.client.node.NodeClient) FieldStats(org.elasticsearch.action.fieldstats.FieldStats) FieldStatsResponse(org.elasticsearch.action.fieldstats.FieldStatsResponse) RestResponse(org.elasticsearch.rest.RestResponse) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) FieldStatsResponse(org.elasticsearch.action.fieldstats.FieldStatsResponse) IOException(java.io.IOException) FieldStats(org.elasticsearch.action.fieldstats.FieldStats) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) XContentParser(org.elasticsearch.common.xcontent.XContentParser) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) FieldStatsRequest(org.elasticsearch.action.fieldstats.FieldStatsRequest)

Example 5 with RestResponse

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

the class RestStatusToXContentListener method buildResponse.

@Override
public RestResponse buildResponse(Response response, XContentBuilder builder) throws Exception {
    //would be nice if we could make default methods final
    assert response.isFragment() == false;
    response.toXContent(builder, channel.request());
    RestResponse restResponse = new BytesRestResponse(response.status(), builder);
    if (RestStatus.CREATED == restResponse.status()) {
        final String location = extractLocation.apply(response);
        if (location != null) {
            restResponse.addHeader("Location", location);
        }
    }
    return restResponse;
}
Also used : BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) RestResponse(org.elasticsearch.rest.RestResponse) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse)

Aggregations

RestResponse (org.elasticsearch.rest.RestResponse)51 NodeClient (org.elasticsearch.client.node.NodeClient)48 Settings (org.elasticsearch.common.settings.Settings)48 RestController (org.elasticsearch.rest.RestController)48 RestRequest (org.elasticsearch.rest.RestRequest)48 GET (org.elasticsearch.rest.RestRequest.Method.GET)40 Strings (org.elasticsearch.common.Strings)33 IOException (java.io.IOException)31 BytesRestResponse (org.elasticsearch.rest.BytesRestResponse)31 BaseRestHandler (org.elasticsearch.rest.BaseRestHandler)30 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)27 RestBuilderListener (org.elasticsearch.rest.action.RestBuilderListener)25 IndicesOptions (org.elasticsearch.action.support.IndicesOptions)23 OK (org.elasticsearch.rest.RestStatus.OK)20 RestResponseListener (org.elasticsearch.rest.action.RestResponseListener)20 Table (org.elasticsearch.common.Table)17 ClusterStateRequest (org.elasticsearch.action.admin.cluster.state.ClusterStateRequest)12 ClusterStateResponse (org.elasticsearch.action.admin.cluster.state.ClusterStateResponse)12 RestActions.buildBroadcastShardsHeader (org.elasticsearch.rest.action.RestActions.buildBroadcastShardsHeader)11 RestStatus (org.elasticsearch.rest.RestStatus)10