Search in sources :

Example 46 with BytesRestResponse

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

the class TestDeprecationHeaderRestAction method prepareRequest.

// List<String> casts
@SuppressWarnings("unchecked")
@Override
public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
    final List<String> settings;
    try (XContentParser parser = request.contentParser()) {
        final Map<String, Object> source = parser.map();
        if (source.containsKey("deprecated_settings")) {
            deprecationLogger.deprecated(DEPRECATED_USAGE);
            settings = (List<String>) source.get("deprecated_settings");
        } else {
            settings = (List<String>) source.get("settings");
        }
    }
    return channel -> {
        final XContentBuilder builder = channel.newBuilder();
        builder.startObject().startArray("settings");
        for (String setting : settings) {
            builder.startObject().field(setting, SETTINGS.get(setting).getRaw(this.settings)).endObject();
        }
        builder.endArray().endObject();
        channel.sendResponse(new BytesRestResponse(RestStatus.OK, builder));
    };
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) Setting(org.elasticsearch.common.settings.Setting) IOException(java.io.IOException) HashMap(java.util.HashMap) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) RestController(org.elasticsearch.rest.RestController) XContentParser(org.elasticsearch.common.xcontent.XContentParser) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) List(java.util.List) Settings(org.elasticsearch.common.settings.Settings) RestStatus(org.elasticsearch.rest.RestStatus) Map(java.util.Map) RestRequest(org.elasticsearch.rest.RestRequest) NodeClient(org.elasticsearch.client.node.NodeClient) Collections(java.util.Collections) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) XContentParser(org.elasticsearch.common.xcontent.XContentParser) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 47 with BytesRestResponse

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

the class TestResponseHeaderRestAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
    if ("password".equals(request.header("Secret"))) {
        RestResponse response = new BytesRestResponse(RestStatus.OK, "Access granted");
        response.addHeader("Secret", "granted");
        return channel -> channel.sendResponse(response);
    } else {
        RestResponse response = new BytesRestResponse(RestStatus.UNAUTHORIZED, "Access denied");
        response.addHeader("Secret", "required");
        return channel -> channel.sendResponse(response);
    }
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) Settings(org.elasticsearch.common.settings.Settings) RestResponse(org.elasticsearch.rest.RestResponse) RestStatus(org.elasticsearch.rest.RestStatus) RestRequest(org.elasticsearch.rest.RestRequest) NodeClient(org.elasticsearch.client.node.NodeClient) IOException(java.io.IOException) RestController(org.elasticsearch.rest.RestController) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) RestResponse(org.elasticsearch.rest.RestResponse) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse)

Aggregations

BytesRestResponse (org.elasticsearch.rest.BytesRestResponse)47 RestRequest (org.elasticsearch.rest.RestRequest)38 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)35 IOException (java.io.IOException)33 Settings (org.elasticsearch.common.settings.Settings)33 NodeClient (org.elasticsearch.client.node.NodeClient)32 RestController (org.elasticsearch.rest.RestController)32 BaseRestHandler (org.elasticsearch.rest.BaseRestHandler)31 RestResponse (org.elasticsearch.rest.RestResponse)31 RestBuilderListener (org.elasticsearch.rest.action.RestBuilderListener)25 Strings (org.elasticsearch.common.Strings)24 GET (org.elasticsearch.rest.RestRequest.Method.GET)24 IndicesOptions (org.elasticsearch.action.support.IndicesOptions)21 OK (org.elasticsearch.rest.RestStatus.OK)20 RestStatus (org.elasticsearch.rest.RestStatus)15 POST (org.elasticsearch.rest.RestRequest.Method.POST)11 RestActions.buildBroadcastShardsHeader (org.elasticsearch.rest.action.RestActions.buildBroadcastShardsHeader)11 Map (java.util.Map)7 Set (java.util.Set)5 SettingsFilter (org.elasticsearch.common.settings.SettingsFilter)5