Search in sources :

Example 86 with RestRequest

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

the class RestClusterRerouteAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    ClusterRerouteRequest clusterRerouteRequest = createRequest(request);
    // by default, return everything but metadata
    final String metric = request.param("metric");
    if (metric == null) {
        request.params().put("metric", DEFAULT_METRICS);
    }
    return channel -> client.admin().cluster().reroute(clusterRerouteRequest, new AcknowledgedRestListener<ClusterRerouteResponse>(channel) {

        @Override
        protected void addCustomFields(XContentBuilder builder, ClusterRerouteResponse response) throws IOException {
            builder.startObject("state");
            settingsFilter.addFilterSettingParams(request);
            response.getState().toXContent(builder, request);
            builder.endObject();
            if (clusterRerouteRequest.explain()) {
                assert response.getExplanations() != null;
                response.getExplanations().toXContent(builder, ToXContent.EMPTY_PARAMS);
            }
        }
    });
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) SettingsFilter(org.elasticsearch.common.settings.SettingsFilter) ClusterRerouteRequest(org.elasticsearch.action.admin.cluster.reroute.ClusterRerouteRequest) 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) ClusterRerouteResponse(org.elasticsearch.action.admin.cluster.reroute.ClusterRerouteResponse) Strings(org.elasticsearch.common.Strings) HashSet(java.util.HashSet) ClusterState(org.elasticsearch.cluster.ClusterState) Settings(org.elasticsearch.common.settings.Settings) AllocationCommands(org.elasticsearch.cluster.routing.allocation.command.AllocationCommands) ValueType(org.elasticsearch.common.xcontent.ObjectParser.ValueType) ObjectParser(org.elasticsearch.common.xcontent.ObjectParser) RestRequest(org.elasticsearch.rest.RestRequest) NodeClient(org.elasticsearch.client.node.NodeClient) Requests(org.elasticsearch.client.Requests) ParseField(org.elasticsearch.common.ParseField) Collections(java.util.Collections) EnumSet(java.util.EnumSet) AcknowledgedRestListener(org.elasticsearch.rest.action.AcknowledgedRestListener) ClusterRerouteRequest(org.elasticsearch.action.admin.cluster.reroute.ClusterRerouteRequest) IOException(java.io.IOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) ClusterRerouteResponse(org.elasticsearch.action.admin.cluster.reroute.ClusterRerouteResponse)

Example 87 with RestRequest

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

the class RestClusterUpdateSettingsAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    final ClusterUpdateSettingsRequest clusterUpdateSettingsRequest = Requests.clusterUpdateSettingsRequest();
    clusterUpdateSettingsRequest.timeout(request.paramAsTime("timeout", clusterUpdateSettingsRequest.timeout()));
    clusterUpdateSettingsRequest.masterNodeTimeout(request.paramAsTime("master_timeout", clusterUpdateSettingsRequest.masterNodeTimeout()));
    Map<String, Object> source;
    try (XContentParser parser = request.contentParser()) {
        source = parser.map();
    }
    if (source.containsKey("transient")) {
        clusterUpdateSettingsRequest.transientSettings((Map) source.get("transient"));
    }
    if (source.containsKey("persistent")) {
        clusterUpdateSettingsRequest.persistentSettings((Map) source.get("persistent"));
    }
    return channel -> client.admin().cluster().updateSettings(clusterUpdateSettingsRequest, new AcknowledgedRestListener<ClusterUpdateSettingsResponse>(channel) {

        @Override
        protected void addCustomFields(XContentBuilder builder, ClusterUpdateSettingsResponse response) throws IOException {
            builder.startObject("persistent");
            response.getPersistentSettings().toXContent(builder, request);
            builder.endObject();
            builder.startObject("transient");
            response.getTransientSettings().toXContent(builder, request);
            builder.endObject();
        }
    });
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) Set(java.util.Set) IOException(java.io.IOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) RestController(org.elasticsearch.rest.RestController) XContentParser(org.elasticsearch.common.xcontent.XContentParser) ClusterUpdateSettingsRequest(org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest) Settings(org.elasticsearch.common.settings.Settings) Map(java.util.Map) RestRequest(org.elasticsearch.rest.RestRequest) NodeClient(org.elasticsearch.client.node.NodeClient) ClusterUpdateSettingsResponse(org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsResponse) Requests(org.elasticsearch.client.Requests) AcknowledgedRestListener(org.elasticsearch.rest.action.AcknowledgedRestListener) IOException(java.io.IOException) ClusterUpdateSettingsResponse(org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsResponse) ClusterUpdateSettingsRequest(org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest) XContentParser(org.elasticsearch.common.xcontent.XContentParser) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 88 with RestRequest

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

the class RestCreateSnapshotAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    CreateSnapshotRequest createSnapshotRequest = createSnapshotRequest(request.param("repository"), request.param("snapshot"));
    request.applyContentParser(p -> createSnapshotRequest.source(p.mapOrdered()));
    createSnapshotRequest.masterNodeTimeout(request.paramAsTime("master_timeout", createSnapshotRequest.masterNodeTimeout()));
    createSnapshotRequest.waitForCompletion(request.paramAsBoolean("wait_for_completion", false));
    return channel -> client.admin().cluster().createSnapshot(createSnapshotRequest, new RestToXContentListener<>(channel));
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) Requests.createSnapshotRequest(org.elasticsearch.client.Requests.createSnapshotRequest) POST(org.elasticsearch.rest.RestRequest.Method.POST) 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) CreateSnapshotRequest(org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotRequest) PUT(org.elasticsearch.rest.RestRequest.Method.PUT) CreateSnapshotRequest(org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotRequest)

Example 89 with RestRequest

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

the class SettingsFilterTests method testFiltering.

private void testFiltering(Settings source, Settings filtered, String... patterns) throws IOException {
    SettingsFilter settingsFilter = new SettingsFilter(Settings.EMPTY, Arrays.asList(patterns));
    // Test using direct filtering
    Settings filteredSettings = settingsFilter.filter(source);
    assertThat(filteredSettings.getAsMap().entrySet(), equalTo(filtered.getAsMap().entrySet()));
    // Test using toXContent filtering
    RestRequest request = new FakeRestRequest();
    settingsFilter.addFilterSettingParams(request);
    XContentBuilder xContentBuilder = XContentBuilder.builder(JsonXContent.jsonXContent);
    xContentBuilder.startObject();
    source.toXContent(xContentBuilder, request);
    xContentBuilder.endObject();
    String filteredSettingsString = xContentBuilder.string();
    filteredSettings = Settings.builder().loadFromSource(filteredSettingsString, xContentBuilder.contentType()).build();
    assertThat(filteredSettings.getAsMap().entrySet(), equalTo(filtered.getAsMap().entrySet()));
}
Also used : RestRequest(org.elasticsearch.rest.RestRequest) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 90 with RestRequest

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

the class RestMainActionTests method testHeadResponse.

public void testHeadResponse() throws Exception {
    final String nodeName = "node1";
    final ClusterName clusterName = new ClusterName("cluster1");
    final String clusterUUID = randomAsciiOfLengthBetween(10, 20);
    final boolean available = randomBoolean();
    final RestStatus expectedStatus = available ? RestStatus.OK : RestStatus.SERVICE_UNAVAILABLE;
    final Version version = Version.CURRENT;
    final Build build = Build.CURRENT;
    final MainResponse mainResponse = new MainResponse(nodeName, version, clusterName, clusterUUID, build, available);
    XContentBuilder builder = JsonXContent.contentBuilder();
    RestRequest restRequest = new FakeRestRequest() {

        @Override
        public Method method() {
            return Method.HEAD;
        }
    };
    BytesRestResponse response = RestMainAction.convertMainResponse(mainResponse, restRequest, builder);
    assertNotNull(response);
    assertEquals(expectedStatus, response.status());
// the empty responses are handled in the HTTP layer so we do
// not assert on them here
}
Also used : FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) RestRequest(org.elasticsearch.rest.RestRequest) RestStatus(org.elasticsearch.rest.RestStatus) Version(org.elasticsearch.Version) MainResponse(org.elasticsearch.action.main.MainResponse) Build(org.elasticsearch.Build) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) ClusterName(org.elasticsearch.cluster.ClusterName) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

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