Search in sources :

Example 1 with OK

use of org.opensearch.rest.RestStatus.OK in project OpenSearch by opensearch-project.

the class RestGetIndexTemplateAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    final String[] names = Strings.splitStringByCommaToArray(request.param("name"));
    final GetIndexTemplatesRequest getIndexTemplatesRequest = new GetIndexTemplatesRequest(names);
    getIndexTemplatesRequest.local(request.paramAsBoolean("local", getIndexTemplatesRequest.local()));
    getIndexTemplatesRequest.masterNodeTimeout(request.paramAsTime("master_timeout", getIndexTemplatesRequest.masterNodeTimeout()));
    final boolean implicitAll = getIndexTemplatesRequest.names().length == 0;
    return channel -> client.admin().indices().getTemplates(getIndexTemplatesRequest, new RestToXContentListener<GetIndexTemplatesResponse>(channel) {

        @Override
        protected RestStatus getStatus(final GetIndexTemplatesResponse response) {
            final boolean templateExists = response.getIndexTemplates().isEmpty() == false;
            return (templateExists || implicitAll) ? OK : NOT_FOUND;
        }
    });
}
Also used : OK(org.opensearch.rest.RestStatus.OK) NodeClient(org.opensearch.client.node.NodeClient) Collections.unmodifiableList(java.util.Collections.unmodifiableList) GET(org.opensearch.rest.RestRequest.Method.GET) RestRequest(org.opensearch.rest.RestRequest) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) IOException(java.io.IOException) RestStatus(org.opensearch.rest.RestStatus) Strings(org.opensearch.common.Strings) GetIndexTemplatesRequest(org.opensearch.action.admin.indices.template.get.GetIndexTemplatesRequest) NOT_FOUND(org.opensearch.rest.RestStatus.NOT_FOUND) List(java.util.List) RestToXContentListener(org.opensearch.rest.action.RestToXContentListener) Arrays.asList(java.util.Arrays.asList) GetIndexTemplatesResponse(org.opensearch.action.admin.indices.template.get.GetIndexTemplatesResponse) BaseRestHandler(org.opensearch.rest.BaseRestHandler) HEAD(org.opensearch.rest.RestRequest.Method.HEAD) RestStatus(org.opensearch.rest.RestStatus) GetIndexTemplatesResponse(org.opensearch.action.admin.indices.template.get.GetIndexTemplatesResponse) GetIndexTemplatesRequest(org.opensearch.action.admin.indices.template.get.GetIndexTemplatesRequest)

Example 2 with OK

use of org.opensearch.rest.RestStatus.OK in project OpenSearch by opensearch-project.

the class RestIndicesShardStoresAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    IndicesShardStoresRequest indicesShardStoresRequest = new IndicesShardStoresRequest(Strings.splitStringByCommaToArray(request.param("index")));
    if (request.hasParam("status")) {
        indicesShardStoresRequest.shardStatuses(Strings.splitStringByCommaToArray(request.param("status")));
    }
    indicesShardStoresRequest.indicesOptions(IndicesOptions.fromRequest(request, indicesShardStoresRequest.indicesOptions()));
    return channel -> client.admin().indices().shardStores(indicesShardStoresRequest, new RestBuilderListener<IndicesShardStoresResponse>(channel) {

        @Override
        public RestResponse buildResponse(IndicesShardStoresResponse response, XContentBuilder builder) throws Exception {
            builder.startObject();
            response.toXContent(builder, request);
            builder.endObject();
            return new BytesRestResponse(OK, builder);
        }
    });
}
Also used : IndicesShardStoresResponse(org.opensearch.action.admin.indices.shards.IndicesShardStoresResponse) OK(org.opensearch.rest.RestStatus.OK) NodeClient(org.opensearch.client.node.NodeClient) Collections.unmodifiableList(java.util.Collections.unmodifiableList) GET(org.opensearch.rest.RestRequest.Method.GET) RestRequest(org.opensearch.rest.RestRequest) IOException(java.io.IOException) IndicesOptions(org.opensearch.action.support.IndicesOptions) IndicesShardStoresAction(org.opensearch.action.admin.indices.shards.IndicesShardStoresAction) BytesRestResponse(org.opensearch.rest.BytesRestResponse) RestResponse(org.opensearch.rest.RestResponse) Strings(org.opensearch.common.Strings) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) IndicesShardStoresRequest(org.opensearch.action.admin.indices.shards.IndicesShardStoresRequest) RestBuilderListener(org.opensearch.rest.action.RestBuilderListener) List(java.util.List) Arrays.asList(java.util.Arrays.asList) BaseRestHandler(org.opensearch.rest.BaseRestHandler) IndicesShardStoresRequest(org.opensearch.action.admin.indices.shards.IndicesShardStoresRequest) IndicesShardStoresResponse(org.opensearch.action.admin.indices.shards.IndicesShardStoresResponse) BytesRestResponse(org.opensearch.rest.BytesRestResponse) RestResponse(org.opensearch.rest.RestResponse) BytesRestResponse(org.opensearch.rest.BytesRestResponse) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) IOException(java.io.IOException)

Example 3 with OK

use of org.opensearch.rest.RestStatus.OK in project OpenSearch by opensearch-project.

the class RestGetAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    GetRequest getRequest = new GetRequest(request.param("index"), request.param("id"));
    getRequest.refresh(request.paramAsBoolean("refresh", getRequest.refresh()));
    getRequest.routing(request.param("routing"));
    getRequest.preference(request.param("preference"));
    getRequest.realtime(request.paramAsBoolean("realtime", getRequest.realtime()));
    if (request.param("fields") != null) {
        throw new IllegalArgumentException("the parameter [fields] is no longer supported, " + "please use [stored_fields] to retrieve stored fields or [_source] to load the field from _source");
    }
    final String fieldsParam = request.param("stored_fields");
    if (fieldsParam != null) {
        final String[] fields = Strings.splitStringByCommaToArray(fieldsParam);
        if (fields != null) {
            getRequest.storedFields(fields);
        }
    }
    getRequest.version(RestActions.parseVersion(request));
    getRequest.versionType(VersionType.fromString(request.param("version_type"), getRequest.versionType()));
    getRequest.fetchSourceContext(FetchSourceContext.parseFromRestRequest(request));
    return channel -> client.get(getRequest, new RestToXContentListener<GetResponse>(channel) {

        @Override
        protected RestStatus getStatus(final GetResponse response) {
            return response.isExists() ? OK : NOT_FOUND;
        }
    });
}
Also used : OK(org.opensearch.rest.RestStatus.OK) NodeClient(org.opensearch.client.node.NodeClient) Collections.unmodifiableList(java.util.Collections.unmodifiableList) GET(org.opensearch.rest.RestRequest.Method.GET) RestRequest(org.opensearch.rest.RestRequest) GetRequest(org.opensearch.action.get.GetRequest) IOException(java.io.IOException) RestStatus(org.opensearch.rest.RestStatus) Strings(org.opensearch.common.Strings) NOT_FOUND(org.opensearch.rest.RestStatus.NOT_FOUND) VersionType(org.opensearch.index.VersionType) List(java.util.List) RestActions(org.opensearch.rest.action.RestActions) RestToXContentListener(org.opensearch.rest.action.RestToXContentListener) Arrays.asList(java.util.Arrays.asList) BaseRestHandler(org.opensearch.rest.BaseRestHandler) HEAD(org.opensearch.rest.RestRequest.Method.HEAD) GetResponse(org.opensearch.action.get.GetResponse) FetchSourceContext(org.opensearch.search.fetch.subphase.FetchSourceContext) RestStatus(org.opensearch.rest.RestStatus) GetRequest(org.opensearch.action.get.GetRequest) GetResponse(org.opensearch.action.get.GetResponse)

Example 4 with OK

use of org.opensearch.rest.RestStatus.OK in project OpenSearch by opensearch-project.

the class RestGetFieldMappingAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    final String[] indices = Strings.splitStringByCommaToArray(request.param("index"));
    final String[] fields = Strings.splitStringByCommaToArray(request.param("fields"));
    GetFieldMappingsRequest getMappingsRequest = new GetFieldMappingsRequest();
    getMappingsRequest.indices(indices).fields(fields).includeDefaults(request.paramAsBoolean("include_defaults", false));
    getMappingsRequest.indicesOptions(IndicesOptions.fromRequest(request, getMappingsRequest.indicesOptions()));
    if (request.hasParam("local")) {
        deprecationLogger.deprecate("get_field_mapping_local", "Use [local] in get field mapping requests is deprecated. " + "The parameter will be removed in the next major version");
    }
    getMappingsRequest.local(request.paramAsBoolean("local", getMappingsRequest.local()));
    return channel -> client.admin().indices().getFieldMappings(getMappingsRequest, new RestBuilderListener<GetFieldMappingsResponse>(channel) {

        @Override
        public RestResponse buildResponse(GetFieldMappingsResponse response, XContentBuilder builder) throws Exception {
            Map<String, Map<String, Map<String, FieldMappingMetadata>>> mappingsByIndex = response.mappings();
            boolean isPossibleSingleFieldRequest = indices.length == 1 && fields.length == 1;
            if (isPossibleSingleFieldRequest && isFieldMappingMissingField(mappingsByIndex)) {
                return new BytesRestResponse(OK, builder.startObject().endObject());
            }
            RestStatus status = OK;
            if (mappingsByIndex.isEmpty() && fields.length > 0) {
                status = NOT_FOUND;
            }
            response.toXContent(builder, request);
            return new BytesRestResponse(status, builder);
        }
    });
}
Also used : OK(org.opensearch.rest.RestStatus.OK) NodeClient(org.opensearch.client.node.NodeClient) Collections.unmodifiableList(java.util.Collections.unmodifiableList) GET(org.opensearch.rest.RestRequest.Method.GET) RestRequest(org.opensearch.rest.RestRequest) GetFieldMappingsRequest(org.opensearch.action.admin.indices.mapping.get.GetFieldMappingsRequest) IOException(java.io.IOException) FieldMappingMetadata(org.opensearch.action.admin.indices.mapping.get.GetFieldMappingsResponse.FieldMappingMetadata) IndicesOptions(org.opensearch.action.support.IndicesOptions) RestStatus(org.opensearch.rest.RestStatus) BytesRestResponse(org.opensearch.rest.BytesRestResponse) RestResponse(org.opensearch.rest.RestResponse) Strings(org.opensearch.common.Strings) DeprecationLogger(org.opensearch.common.logging.DeprecationLogger) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) NOT_FOUND(org.opensearch.rest.RestStatus.NOT_FOUND) RestBuilderListener(org.opensearch.rest.action.RestBuilderListener) List(java.util.List) Logger(org.apache.logging.log4j.Logger) GetFieldMappingsResponse(org.opensearch.action.admin.indices.mapping.get.GetFieldMappingsResponse) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) BaseRestHandler(org.opensearch.rest.BaseRestHandler) LogManager(org.apache.logging.log4j.LogManager) BytesRestResponse(org.opensearch.rest.BytesRestResponse) RestResponse(org.opensearch.rest.RestResponse) GetFieldMappingsRequest(org.opensearch.action.admin.indices.mapping.get.GetFieldMappingsRequest) GetFieldMappingsResponse(org.opensearch.action.admin.indices.mapping.get.GetFieldMappingsResponse) IOException(java.io.IOException) RestStatus(org.opensearch.rest.RestStatus) BytesRestResponse(org.opensearch.rest.BytesRestResponse) Map(java.util.Map) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Aggregations

IOException (java.io.IOException)4 Arrays.asList (java.util.Arrays.asList)4 Collections.unmodifiableList (java.util.Collections.unmodifiableList)4 List (java.util.List)4 NodeClient (org.opensearch.client.node.NodeClient)4 Strings (org.opensearch.common.Strings)4 BaseRestHandler (org.opensearch.rest.BaseRestHandler)4 RestRequest (org.opensearch.rest.RestRequest)4 GET (org.opensearch.rest.RestRequest.Method.GET)4 OK (org.opensearch.rest.RestStatus.OK)4 RestStatus (org.opensearch.rest.RestStatus)3 NOT_FOUND (org.opensearch.rest.RestStatus.NOT_FOUND)3 IndicesOptions (org.opensearch.action.support.IndicesOptions)2 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)2 BytesRestResponse (org.opensearch.rest.BytesRestResponse)2 HEAD (org.opensearch.rest.RestRequest.Method.HEAD)2 RestResponse (org.opensearch.rest.RestResponse)2 RestBuilderListener (org.opensearch.rest.action.RestBuilderListener)2 RestToXContentListener (org.opensearch.rest.action.RestToXContentListener)2 Map (java.util.Map)1