Search in sources :

Example 41 with BytesRestResponse

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

the class RestGetSourceAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    final GetRequest getRequest = new GetRequest(request.param("index"), request.param("type"), request.param("id"));
    getRequest.operationThreaded(true);
    getRequest.refresh(request.paramAsBoolean("refresh", getRequest.refresh()));
    getRequest.routing(request.param("routing"));
    getRequest.parent(request.param("parent"));
    getRequest.preference(request.param("preference"));
    getRequest.realtime(request.paramAsBoolean("realtime", getRequest.realtime()));
    getRequest.fetchSourceContext(FetchSourceContext.parseFromRestRequest(request));
    return channel -> {
        if (getRequest.fetchSourceContext() != null && !getRequest.fetchSourceContext().fetchSource()) {
            final ActionRequestValidationException validationError = new ActionRequestValidationException();
            validationError.addValidationError("fetching source can not be disabled");
            channel.sendResponse(new BytesRestResponse(channel, validationError));
        } else {
            client.get(getRequest, new RestResponseListener<GetResponse>(channel) {

                @Override
                public RestResponse buildResponse(final GetResponse response) throws Exception {
                    final XContentBuilder builder = channel.newBuilder(request.getXContentType(), false);
                    if (response.isSourceEmpty()) {
                        return new BytesRestResponse(NOT_FOUND, builder);
                    } else {
                        builder.rawValue(response.getSourceInternal());
                        return new BytesRestResponse(OK, builder);
                    }
                }
            });
        }
    };
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) GetResponse(org.elasticsearch.action.get.GetResponse) GetRequest(org.elasticsearch.action.get.GetRequest) GET(org.elasticsearch.rest.RestRequest.Method.GET) RestResponse(org.elasticsearch.rest.RestResponse) IOException(java.io.IOException) NOT_FOUND(org.elasticsearch.rest.RestStatus.NOT_FOUND) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) RestController(org.elasticsearch.rest.RestController) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) Settings(org.elasticsearch.common.settings.Settings) ActionRequestValidationException(org.elasticsearch.action.ActionRequestValidationException) RestRequest(org.elasticsearch.rest.RestRequest) OK(org.elasticsearch.rest.RestStatus.OK) NodeClient(org.elasticsearch.client.node.NodeClient) HEAD(org.elasticsearch.rest.RestRequest.Method.HEAD) RestResponseListener(org.elasticsearch.rest.action.RestResponseListener) FetchSourceContext(org.elasticsearch.search.fetch.subphase.FetchSourceContext) ActionRequestValidationException(org.elasticsearch.action.ActionRequestValidationException) RestResponseListener(org.elasticsearch.rest.action.RestResponseListener) GetRequest(org.elasticsearch.action.get.GetRequest) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) GetResponse(org.elasticsearch.action.get.GetResponse) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 42 with BytesRestResponse

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

the class RestTable method buildTextPlainResponse.

public static RestResponse buildTextPlainResponse(Table table, RestChannel channel) throws IOException {
    RestRequest request = channel.request();
    boolean verbose = request.paramAsBoolean("v", false);
    List<DisplayHeader> headers = buildDisplayHeaders(table, request);
    int[] width = buildWidths(table, request, verbose, headers);
    BytesStreamOutput bytesOut = channel.bytesOutput();
    UTF8StreamWriter out = new UTF8StreamWriter().setOutput(bytesOut);
    int lastHeader = headers.size() - 1;
    if (verbose) {
        for (int col = 0; col < headers.size(); col++) {
            DisplayHeader header = headers.get(col);
            boolean isLastColumn = col == lastHeader;
            pad(new Table.Cell(header.display, table.findHeaderByName(header.name)), width[col], request, out, isLastColumn);
            if (!isLastColumn) {
                out.append(" ");
            }
        }
        out.append("\n");
    }
    List<Integer> rowOrder = getRowOrder(table, request);
    for (Integer row : rowOrder) {
        for (int col = 0; col < headers.size(); col++) {
            DisplayHeader header = headers.get(col);
            boolean isLastColumn = col == lastHeader;
            pad(table.getAsMap().get(header.name).get(row), width[col], request, out, isLastColumn);
            if (!isLastColumn) {
                out.append(" ");
            }
        }
        out.append("\n");
    }
    out.close();
    return new BytesRestResponse(RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, bytesOut.bytes());
}
Also used : Table(org.elasticsearch.common.Table) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) RestRequest(org.elasticsearch.rest.RestRequest) UTF8StreamWriter(org.elasticsearch.common.io.UTF8StreamWriter) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse)

Example 43 with BytesRestResponse

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

the class RestRecoveryAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    final RecoveryRequest recoveryRequest = new RecoveryRequest(Strings.splitStringByCommaToArray(request.param("index")));
    recoveryRequest.detailed(request.paramAsBoolean("detailed", false));
    recoveryRequest.activeOnly(request.paramAsBoolean("active_only", false));
    recoveryRequest.indicesOptions(IndicesOptions.fromRequest(request, recoveryRequest.indicesOptions()));
    return channel -> client.admin().indices().recoveries(recoveryRequest, new RestBuilderListener<RecoveryResponse>(channel) {

        @Override
        public RestResponse buildResponse(RecoveryResponse response, XContentBuilder builder) throws Exception {
            response.detailed(recoveryRequest.detailed());
            builder.startObject();
            response.toXContent(builder, request);
            builder.endObject();
            return new BytesRestResponse(OK, builder);
        }
    });
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) RecoveryResponse(org.elasticsearch.action.admin.indices.recovery.RecoveryResponse) GET(org.elasticsearch.rest.RestRequest.Method.GET) RestResponse(org.elasticsearch.rest.RestResponse) RecoveryRequest(org.elasticsearch.action.admin.indices.recovery.RecoveryRequest) RestBuilderListener(org.elasticsearch.rest.action.RestBuilderListener) IOException(java.io.IOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) RestController(org.elasticsearch.rest.RestController) Strings(org.elasticsearch.common.Strings) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) Settings(org.elasticsearch.common.settings.Settings) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) RestRequest(org.elasticsearch.rest.RestRequest) OK(org.elasticsearch.rest.RestStatus.OK) NodeClient(org.elasticsearch.client.node.NodeClient) RecoveryRequest(org.elasticsearch.action.admin.indices.recovery.RecoveryRequest) RestResponse(org.elasticsearch.rest.RestResponse) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) IOException(java.io.IOException) RecoveryResponse(org.elasticsearch.action.admin.indices.recovery.RecoveryResponse)

Example 44 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 45 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