Search in sources :

Example 6 with RestResponseListener

use of org.opensearch.rest.action.RestResponseListener in project OpenSearch by opensearch-project.

the class RestMasterAction method doCatRequest.

@Override
public RestChannelConsumer doCatRequest(final RestRequest request, final NodeClient client) {
    final ClusterStateRequest clusterStateRequest = new ClusterStateRequest();
    clusterStateRequest.clear().nodes(true);
    clusterStateRequest.local(request.paramAsBoolean("local", clusterStateRequest.local()));
    clusterStateRequest.masterNodeTimeout(request.paramAsTime("cluster_manager_timeout", clusterStateRequest.masterNodeTimeout()));
    parseDeprecatedMasterTimeoutParameter(clusterStateRequest, request, deprecationLogger, getName());
    return channel -> client.admin().cluster().state(clusterStateRequest, new RestResponseListener<ClusterStateResponse>(channel) {

        @Override
        public RestResponse buildResponse(final ClusterStateResponse clusterStateResponse) throws Exception {
            return RestTable.buildResponse(buildTable(request, clusterStateResponse), channel);
        }
    });
}
Also used : DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) NodeClient(org.opensearch.client.node.NodeClient) GET(org.opensearch.rest.RestRequest.Method.GET) RestRequest(org.opensearch.rest.RestRequest) ClusterStateResponse(org.opensearch.action.admin.cluster.state.ClusterStateResponse) Table(org.opensearch.common.Table) RestResponse(org.opensearch.rest.RestResponse) Collections.singletonList(java.util.Collections.singletonList) DeprecationLogger(org.opensearch.common.logging.DeprecationLogger) ClusterStateRequest(org.opensearch.action.admin.cluster.state.ClusterStateRequest) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) List(java.util.List) RestResponseListener(org.opensearch.rest.action.RestResponseListener) ClusterStateResponse(org.opensearch.action.admin.cluster.state.ClusterStateResponse) RestResponse(org.opensearch.rest.RestResponse) ClusterStateRequest(org.opensearch.action.admin.cluster.state.ClusterStateRequest)

Example 7 with RestResponseListener

use of org.opensearch.rest.action.RestResponseListener in project OpenSearch by opensearch-project.

the class RestPendingClusterTasksAction method doCatRequest.

@Override
public RestChannelConsumer doCatRequest(final RestRequest request, final NodeClient client) {
    PendingClusterTasksRequest pendingClusterTasksRequest = new PendingClusterTasksRequest();
    pendingClusterTasksRequest.masterNodeTimeout(request.paramAsTime("cluster_manager_timeout", pendingClusterTasksRequest.masterNodeTimeout()));
    parseDeprecatedMasterTimeoutParameter(pendingClusterTasksRequest, request, deprecationLogger, getName());
    pendingClusterTasksRequest.local(request.paramAsBoolean("local", pendingClusterTasksRequest.local()));
    return channel -> client.admin().cluster().pendingClusterTasks(pendingClusterTasksRequest, new RestResponseListener<PendingClusterTasksResponse>(channel) {

        @Override
        public RestResponse buildResponse(PendingClusterTasksResponse pendingClusterTasks) throws Exception {
            Table tab = buildTable(request, pendingClusterTasks);
            return RestTable.buildResponse(tab, channel);
        }
    });
}
Also used : DeprecationLogger(org.opensearch.common.logging.DeprecationLogger) PendingClusterTasksResponse(org.opensearch.action.admin.cluster.tasks.PendingClusterTasksResponse) List(java.util.List) NodeClient(org.opensearch.client.node.NodeClient) PendingClusterTasksRequest(org.opensearch.action.admin.cluster.tasks.PendingClusterTasksRequest) GET(org.opensearch.rest.RestRequest.Method.GET) RestRequest(org.opensearch.rest.RestRequest) Table(org.opensearch.common.Table) PendingClusterTask(org.opensearch.cluster.service.PendingClusterTask) RestResponse(org.opensearch.rest.RestResponse) RestResponseListener(org.opensearch.rest.action.RestResponseListener) Collections.singletonList(java.util.Collections.singletonList) Table(org.opensearch.common.Table) PendingClusterTasksResponse(org.opensearch.action.admin.cluster.tasks.PendingClusterTasksResponse) RestResponse(org.opensearch.rest.RestResponse) PendingClusterTasksRequest(org.opensearch.action.admin.cluster.tasks.PendingClusterTasksRequest)

Example 8 with RestResponseListener

use of org.opensearch.rest.action.RestResponseListener in project OpenSearch by opensearch-project.

the class RestFielddataAction method doCatRequest.

@Override
protected RestChannelConsumer doCatRequest(final RestRequest request, final NodeClient client) {
    final NodesStatsRequest nodesStatsRequest = new NodesStatsRequest("data:true");
    nodesStatsRequest.clear();
    nodesStatsRequest.indices(true);
    String[] fields = request.paramAsStringArray("fields", null);
    nodesStatsRequest.indices().fieldDataFields(fields == null ? new String[] { "*" } : fields);
    return channel -> client.admin().cluster().nodesStats(nodesStatsRequest, new RestResponseListener<NodesStatsResponse>(channel) {

        @Override
        public RestResponse buildResponse(NodesStatsResponse nodeStatses) throws Exception {
            return RestTable.buildResponse(buildTable(request, nodeStatses), channel);
        }
    });
}
Also used : NodesStatsRequest(org.opensearch.action.admin.cluster.node.stats.NodesStatsRequest) NodeClient(org.opensearch.client.node.NodeClient) Collections.unmodifiableList(java.util.Collections.unmodifiableList) GET(org.opensearch.rest.RestRequest.Method.GET) RestRequest(org.opensearch.rest.RestRequest) Table(org.opensearch.common.Table) ByteSizeValue(org.opensearch.common.unit.ByteSizeValue) RestResponse(org.opensearch.rest.RestResponse) List(java.util.List) Arrays.asList(java.util.Arrays.asList) NodesStatsResponse(org.opensearch.action.admin.cluster.node.stats.NodesStatsResponse) NodeStats(org.opensearch.action.admin.cluster.node.stats.NodeStats) NodesStatsRequest(org.opensearch.action.admin.cluster.node.stats.NodesStatsRequest) ObjectLongCursor(com.carrotsearch.hppc.cursors.ObjectLongCursor) RestResponseListener(org.opensearch.rest.action.RestResponseListener) NodesStatsResponse(org.opensearch.action.admin.cluster.node.stats.NodesStatsResponse) RestResponse(org.opensearch.rest.RestResponse)

Example 9 with RestResponseListener

use of org.opensearch.rest.action.RestResponseListener in project anomaly-detection by opensearch-project.

the class RestIndexAnomalyDetectorAction method indexAnomalyDetectorResponse.

private RestResponseListener<IndexAnomalyDetectorResponse> indexAnomalyDetectorResponse(RestChannel channel, RestRequest.Method method) {
    return new RestResponseListener<IndexAnomalyDetectorResponse>(channel) {

        @Override
        public RestResponse buildResponse(IndexAnomalyDetectorResponse response) throws Exception {
            RestStatus restStatus = RestStatus.CREATED;
            if (method == RestRequest.Method.PUT) {
                restStatus = RestStatus.OK;
            }
            BytesRestResponse bytesRestResponse = new BytesRestResponse(restStatus, response.toXContent(channel.newBuilder(), ToXContent.EMPTY_PARAMS));
            if (restStatus == RestStatus.CREATED) {
                String location = String.format(Locale.ROOT, "%s/%s", AnomalyDetectorPlugin.LEGACY_AD_BASE, response.getId());
                bytesRestResponse.addHeader("Location", location);
            }
            return bytesRestResponse;
        }
    };
}
Also used : RestStatus(org.opensearch.rest.RestStatus) RestResponseListener(org.opensearch.rest.action.RestResponseListener) BytesRestResponse(org.opensearch.rest.BytesRestResponse) IndexAnomalyDetectorResponse(org.opensearch.ad.transport.IndexAnomalyDetectorResponse)

Example 10 with RestResponseListener

use of org.opensearch.rest.action.RestResponseListener in project anomaly-detection by opensearch-project.

the class AbstractSearchAction method search.

protected RestResponseListener<SearchResponse> search(RestChannel channel) {
    return new RestResponseListener<SearchResponse>(channel) {

        @Override
        public RestResponse buildResponse(SearchResponse response) throws Exception {
            if (response.isTimedOut()) {
                return new BytesRestResponse(RestStatus.REQUEST_TIMEOUT, response.toString());
            }
            if (clazz == AnomalyDetector.class) {
                for (SearchHit hit : response.getHits()) {
                    XContentParser parser = XContentType.JSON.xContent().createParser(channel.request().getXContentRegistry(), LoggingDeprecationHandler.INSTANCE, hit.getSourceAsString());
                    ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser);
                    // write back id and version to anomaly detector object
                    ToXContentObject xContentObject = AnomalyDetector.parse(parser, hit.getId(), hit.getVersion());
                    XContentBuilder builder = xContentObject.toXContent(jsonBuilder(), EMPTY_PARAMS);
                    hit.sourceRef(BytesReference.bytes(builder));
                }
            }
            return new BytesRestResponse(RestStatus.OK, response.toXContent(channel.newBuilder(), EMPTY_PARAMS));
        }
    };
}
Also used : SearchHit(org.opensearch.search.SearchHit) RestResponseListener(org.opensearch.rest.action.RestResponseListener) BytesRestResponse(org.opensearch.rest.BytesRestResponse) XContentParser(org.opensearch.common.xcontent.XContentParser) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) SearchResponse(org.opensearch.action.search.SearchResponse) ToXContentObject(org.opensearch.common.xcontent.ToXContentObject)

Aggregations

RestResponseListener (org.opensearch.rest.action.RestResponseListener)20 List (java.util.List)18 NodeClient (org.opensearch.client.node.NodeClient)18 RestRequest (org.opensearch.rest.RestRequest)18 GET (org.opensearch.rest.RestRequest.Method.GET)18 RestResponse (org.opensearch.rest.RestResponse)18 Table (org.opensearch.common.Table)17 DeprecationLogger (org.opensearch.common.logging.DeprecationLogger)13 Arrays.asList (java.util.Arrays.asList)12 Collections.unmodifiableList (java.util.Collections.unmodifiableList)12 ClusterStateRequest (org.opensearch.action.admin.cluster.state.ClusterStateRequest)10 ClusterStateResponse (org.opensearch.action.admin.cluster.state.ClusterStateResponse)10 Strings (org.opensearch.common.Strings)9 Collections.singletonList (java.util.Collections.singletonList)6 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)6 DiscoveryNodes (org.opensearch.cluster.node.DiscoveryNodes)6 RestActionListener (org.opensearch.rest.action.RestActionListener)6 Locale (java.util.Locale)5 Map (java.util.Map)5 TimeValue (org.opensearch.common.unit.TimeValue)5