Search in sources :

Example 16 with RestResponseListener

use of org.elasticsearch.rest.action.RestResponseListener 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 17 with RestResponseListener

use of org.elasticsearch.rest.action.RestResponseListener in project elasticsearch by elastic.

the class RestRecoveryAction method doCatRequest.

@Override
public RestChannelConsumer doCatRequest(final RestRequest request, final NodeClient client) {
    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 RestResponseListener<RecoveryResponse>(channel) {

        @Override
        public RestResponse buildResponse(final RecoveryResponse response) throws Exception {
            return RestTable.buildResponse(buildRecoveryTable(request, response), channel);
        }
    });
}
Also used : 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) SnapshotRecoverySource(org.elasticsearch.cluster.routing.RecoverySource.SnapshotRecoverySource) Table(org.elasticsearch.common.Table) RestController(org.elasticsearch.rest.RestController) Strings(org.elasticsearch.common.Strings) CollectionUtil(org.apache.lucene.util.CollectionUtil) RecoverySource(org.elasticsearch.cluster.routing.RecoverySource) List(java.util.List) Settings(org.elasticsearch.common.settings.Settings) RecoveryState(org.elasticsearch.indices.recovery.RecoveryState) Locale(java.util.Locale) TimeValue(org.elasticsearch.common.unit.TimeValue) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) RestRequest(org.elasticsearch.rest.RestRequest) NodeClient(org.elasticsearch.client.node.NodeClient) Comparator(java.util.Comparator) RestResponseListener(org.elasticsearch.rest.action.RestResponseListener) RecoveryRequest(org.elasticsearch.action.admin.indices.recovery.RecoveryRequest) RestResponse(org.elasticsearch.rest.RestResponse) RecoveryResponse(org.elasticsearch.action.admin.indices.recovery.RecoveryResponse)

Example 18 with RestResponseListener

use of org.elasticsearch.rest.action.RestResponseListener in project elasticsearch by elastic.

the class RestRepositoriesAction method doCatRequest.

@Override
protected RestChannelConsumer doCatRequest(RestRequest request, NodeClient client) {
    GetRepositoriesRequest getRepositoriesRequest = new GetRepositoriesRequest();
    getRepositoriesRequest.local(request.paramAsBoolean("local", getRepositoriesRequest.local()));
    getRepositoriesRequest.masterNodeTimeout(request.paramAsTime("master_timeout", getRepositoriesRequest.masterNodeTimeout()));
    return channel -> client.admin().cluster().getRepositories(getRepositoriesRequest, new RestResponseListener<GetRepositoriesResponse>(channel) {

        @Override
        public RestResponse buildResponse(GetRepositoriesResponse getRepositoriesResponse) throws Exception {
            return RestTable.buildResponse(buildTable(request, getRepositoriesResponse), channel);
        }
    });
}
Also used : RepositoryMetaData(org.elasticsearch.cluster.metadata.RepositoryMetaData) Settings(org.elasticsearch.common.settings.Settings) GET(org.elasticsearch.rest.RestRequest.Method.GET) RestResponse(org.elasticsearch.rest.RestResponse) RestRequest(org.elasticsearch.rest.RestRequest) NodeClient(org.elasticsearch.client.node.NodeClient) Table(org.elasticsearch.common.Table) RestController(org.elasticsearch.rest.RestController) GetRepositoriesRequest(org.elasticsearch.action.admin.cluster.repositories.get.GetRepositoriesRequest) GetRepositoriesResponse(org.elasticsearch.action.admin.cluster.repositories.get.GetRepositoriesResponse) RestResponseListener(org.elasticsearch.rest.action.RestResponseListener) RestResponse(org.elasticsearch.rest.RestResponse) GetRepositoriesRequest(org.elasticsearch.action.admin.cluster.repositories.get.GetRepositoriesRequest) GetRepositoriesResponse(org.elasticsearch.action.admin.cluster.repositories.get.GetRepositoriesResponse)

Example 19 with RestResponseListener

use of org.elasticsearch.rest.action.RestResponseListener in project elasticsearch by elastic.

the class RestSegmentsAction method doCatRequest.

@Override
protected RestChannelConsumer doCatRequest(final RestRequest request, final NodeClient client) {
    final String[] indices = Strings.splitStringByCommaToArray(request.param("index"));
    final ClusterStateRequest clusterStateRequest = new ClusterStateRequest();
    clusterStateRequest.local(request.paramAsBoolean("local", clusterStateRequest.local()));
    clusterStateRequest.masterNodeTimeout(request.paramAsTime("master_timeout", clusterStateRequest.masterNodeTimeout()));
    clusterStateRequest.clear().nodes(true).routingTable(true).indices(indices);
    return channel -> client.admin().cluster().state(clusterStateRequest, new RestActionListener<ClusterStateResponse>(channel) {

        @Override
        public void processResponse(final ClusterStateResponse clusterStateResponse) {
            final IndicesSegmentsRequest indicesSegmentsRequest = new IndicesSegmentsRequest();
            indicesSegmentsRequest.indices(indices);
            client.admin().indices().segments(indicesSegmentsRequest, new RestResponseListener<IndicesSegmentResponse>(channel) {

                @Override
                public RestResponse buildResponse(final IndicesSegmentResponse indicesSegmentResponse) throws Exception {
                    final Map<String, IndexSegments> indicesSegments = indicesSegmentResponse.getIndices();
                    Table tab = buildTable(request, clusterStateResponse, indicesSegments);
                    return RestTable.buildResponse(tab, channel);
                }
            });
        }
    });
}
Also used : IndicesSegmentResponse(org.elasticsearch.action.admin.indices.segments.IndicesSegmentResponse) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) GET(org.elasticsearch.rest.RestRequest.Method.GET) IndicesSegmentsRequest(org.elasticsearch.action.admin.indices.segments.IndicesSegmentsRequest) RestResponse(org.elasticsearch.rest.RestResponse) ShardSegments(org.elasticsearch.action.admin.indices.segments.ShardSegments) Table(org.elasticsearch.common.Table) RestController(org.elasticsearch.rest.RestController) Strings(org.elasticsearch.common.Strings) ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) Segment(org.elasticsearch.index.engine.Segment) List(java.util.List) Settings(org.elasticsearch.common.settings.Settings) IndexSegments(org.elasticsearch.action.admin.indices.segments.IndexSegments) Map(java.util.Map) RestRequest(org.elasticsearch.rest.RestRequest) ClusterStateRequest(org.elasticsearch.action.admin.cluster.state.ClusterStateRequest) NodeClient(org.elasticsearch.client.node.NodeClient) IndexShardSegments(org.elasticsearch.action.admin.indices.segments.IndexShardSegments) RestActionListener(org.elasticsearch.rest.action.RestActionListener) RestResponseListener(org.elasticsearch.rest.action.RestResponseListener) Table(org.elasticsearch.common.Table) ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) RestResponseListener(org.elasticsearch.rest.action.RestResponseListener) ClusterStateRequest(org.elasticsearch.action.admin.cluster.state.ClusterStateRequest) IndicesSegmentResponse(org.elasticsearch.action.admin.indices.segments.IndicesSegmentResponse) IndicesSegmentsRequest(org.elasticsearch.action.admin.indices.segments.IndicesSegmentsRequest) IndexSegments(org.elasticsearch.action.admin.indices.segments.IndexSegments)

Example 20 with RestResponseListener

use of org.elasticsearch.rest.action.RestResponseListener in project elasticsearch by elastic.

the class RestSnapshotAction method doCatRequest.

@Override
protected RestChannelConsumer doCatRequest(final RestRequest request, NodeClient client) {
    GetSnapshotsRequest getSnapshotsRequest = new GetSnapshotsRequest().repository(request.param("repository")).snapshots(new String[] { GetSnapshotsRequest.ALL_SNAPSHOTS });
    getSnapshotsRequest.ignoreUnavailable(request.paramAsBoolean("ignore_unavailable", getSnapshotsRequest.ignoreUnavailable()));
    getSnapshotsRequest.masterNodeTimeout(request.paramAsTime("master_timeout", getSnapshotsRequest.masterNodeTimeout()));
    return channel -> client.admin().cluster().getSnapshots(getSnapshotsRequest, new RestResponseListener<GetSnapshotsResponse>(channel) {

        @Override
        public RestResponse buildResponse(GetSnapshotsResponse getSnapshotsResponse) throws Exception {
            return RestTable.buildResponse(buildTable(request, getSnapshotsResponse), channel);
        }
    });
}
Also used : DateTimeFormat(org.joda.time.format.DateTimeFormat) GET(org.elasticsearch.rest.RestRequest.Method.GET) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) RestResponse(org.elasticsearch.rest.RestResponse) Table(org.elasticsearch.common.Table) RestController(org.elasticsearch.rest.RestController) GetSnapshotsRequest(org.elasticsearch.action.admin.cluster.snapshots.get.GetSnapshotsRequest) SnapshotInfo(org.elasticsearch.snapshots.SnapshotInfo) TimeUnit(java.util.concurrent.TimeUnit) Settings(org.elasticsearch.common.settings.Settings) TimeValue(org.elasticsearch.common.unit.TimeValue) RestRequest(org.elasticsearch.rest.RestRequest) NodeClient(org.elasticsearch.client.node.NodeClient) SnapshotState(org.elasticsearch.snapshots.SnapshotState) GetSnapshotsResponse(org.elasticsearch.action.admin.cluster.snapshots.get.GetSnapshotsResponse) RestResponseListener(org.elasticsearch.rest.action.RestResponseListener) GetSnapshotsResponse(org.elasticsearch.action.admin.cluster.snapshots.get.GetSnapshotsResponse) GetSnapshotsRequest(org.elasticsearch.action.admin.cluster.snapshots.get.GetSnapshotsRequest) RestResponse(org.elasticsearch.rest.RestResponse)

Aggregations

NodeClient (org.elasticsearch.client.node.NodeClient)20 Settings (org.elasticsearch.common.settings.Settings)20 RestController (org.elasticsearch.rest.RestController)20 RestRequest (org.elasticsearch.rest.RestRequest)20 RestResponse (org.elasticsearch.rest.RestResponse)20 RestResponseListener (org.elasticsearch.rest.action.RestResponseListener)20 GET (org.elasticsearch.rest.RestRequest.Method.GET)18 Table (org.elasticsearch.common.Table)17 Strings (org.elasticsearch.common.Strings)11 ClusterStateRequest (org.elasticsearch.action.admin.cluster.state.ClusterStateRequest)10 ClusterStateResponse (org.elasticsearch.action.admin.cluster.state.ClusterStateResponse)10 RestActionListener (org.elasticsearch.rest.action.RestActionListener)8 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)6 DiscoveryNodes (org.elasticsearch.cluster.node.DiscoveryNodes)6 IOException (java.io.IOException)4 Locale (java.util.Locale)4 NodeInfo (org.elasticsearch.action.admin.cluster.node.info.NodeInfo)4 NodesInfoRequest (org.elasticsearch.action.admin.cluster.node.info.NodesInfoRequest)4 NodesInfoResponse (org.elasticsearch.action.admin.cluster.node.info.NodesInfoResponse)4 NodeStats (org.elasticsearch.action.admin.cluster.node.stats.NodeStats)4