Search in sources :

Example 26 with BytesRestResponse

use of org.elasticsearch.rest.BytesRestResponse in project elasticsearch-changes-plugin by derryx.

the class ChangesAction method handleRequest.

@Override
protected void handleRequest(RestRequest request, RestChannel channel, Client client) throws Exception {
    logger.debug("Request");
    List<String> indices = Arrays.asList(splitIndices(request.param("index")));
    if (indices.isEmpty()) {
        indices = new ArrayList<String>(changes.keySet());
    }
    boolean wait = request.paramAsBoolean("wait", Boolean.FALSE);
    long timeout = request.paramAsLong("timeout", 15 * 60 * 1000);
    // Wait for trigger
    if (wait) {
        IndexChangeWatcher watcher = addWatcher(indices, timeout);
        boolean changeDetected = watcher.aquire();
        removeWatcher(indices);
        if (!changeDetected) {
            channel.sendResponse(new BytesRestResponse(RestStatus.NOT_FOUND, "No change detected during timeout interval"));
            return;
        }
        if (watcher.getChange() == null || watcher.getChange().getType() == null) {
            channel.sendResponse(new BytesRestResponse(RestStatus.NOT_FOUND, "No more shards available to trigger waiting watch"));
            return;
        }
    }
    XContentBuilder builder = createXContentBuilderForChanges(request, indices);
    channel.sendResponse(new BytesRestResponse(OK, builder));
}
Also used : IndexChangeWatcher(org.elasticsearch.plugins.changes.beans.IndexChangeWatcher) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 27 with BytesRestResponse

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

the class RestGetRepositoriesAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    final String[] repositories = request.paramAsStringArray("repository", Strings.EMPTY_ARRAY);
    GetRepositoriesRequest getRepositoriesRequest = getRepositoryRequest(repositories);
    getRepositoriesRequest.masterNodeTimeout(request.paramAsTime("master_timeout", getRepositoriesRequest.masterNodeTimeout()));
    getRepositoriesRequest.local(request.paramAsBoolean("local", getRepositoriesRequest.local()));
    settingsFilter.addFilterSettingParams(request);
    return channel -> client.admin().cluster().getRepositories(getRepositoriesRequest, new RestBuilderListener<GetRepositoriesResponse>(channel) {

        @Override
        public RestResponse buildResponse(GetRepositoriesResponse response, XContentBuilder builder) throws Exception {
            builder.startObject();
            for (RepositoryMetaData repositoryMetaData : response.repositories()) {
                RepositoriesMetaData.toXContent(repositoryMetaData, builder, request);
            }
            builder.endObject();
            return new BytesRestResponse(OK, builder);
        }
    });
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) RepositoriesMetaData(org.elasticsearch.cluster.metadata.RepositoriesMetaData) RepositoryMetaData(org.elasticsearch.cluster.metadata.RepositoryMetaData) SettingsFilter(org.elasticsearch.common.settings.SettingsFilter) GET(org.elasticsearch.rest.RestRequest.Method.GET) RestResponse(org.elasticsearch.rest.RestResponse) RestBuilderListener(org.elasticsearch.rest.action.RestBuilderListener) Set(java.util.Set) Requests.getRepositoryRequest(org.elasticsearch.client.Requests.getRepositoryRequest) IOException(java.io.IOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) RestController(org.elasticsearch.rest.RestController) GetRepositoriesResponse(org.elasticsearch.action.admin.cluster.repositories.get.GetRepositoriesResponse) Strings(org.elasticsearch.common.Strings) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) Settings(org.elasticsearch.common.settings.Settings) RestRequest(org.elasticsearch.rest.RestRequest) OK(org.elasticsearch.rest.RestStatus.OK) NodeClient(org.elasticsearch.client.node.NodeClient) GetRepositoriesRequest(org.elasticsearch.action.admin.cluster.repositories.get.GetRepositoriesRequest) RestResponse(org.elasticsearch.rest.RestResponse) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) RepositoryMetaData(org.elasticsearch.cluster.metadata.RepositoryMetaData) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) GetRepositoriesRequest(org.elasticsearch.action.admin.cluster.repositories.get.GetRepositoriesRequest) GetRepositoriesResponse(org.elasticsearch.action.admin.cluster.repositories.get.GetRepositoriesResponse) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) IOException(java.io.IOException)

Example 28 with BytesRestResponse

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

the class RestGetStoredScriptAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, NodeClient client) throws IOException {
    String id;
    String lang;
    // name ordering issues in the handlers' paths.
    if (request.param("id") == null) {
        id = request.param("lang");
        ;
        lang = null;
    } else {
        id = request.param("id");
        lang = request.param("lang");
    }
    if (lang != null) {
        deprecationLogger.deprecated("specifying lang [" + lang + "] as part of the url path is deprecated");
    }
    GetStoredScriptRequest getRequest = new GetStoredScriptRequest(id, lang);
    return channel -> client.admin().cluster().getStoredScript(getRequest, new RestBuilderListener<GetStoredScriptResponse>(channel) {

        @Override
        public RestResponse buildResponse(GetStoredScriptResponse response, XContentBuilder builder) throws Exception {
            builder.startObject();
            builder.field(_ID_PARSE_FIELD.getPreferredName(), id);
            if (lang != null) {
                builder.field(StoredScriptSource.LANG_PARSE_FIELD.getPreferredName(), lang);
            }
            StoredScriptSource source = response.getSource();
            boolean found = source != null;
            builder.field(FOUND_PARSE_FIELD.getPreferredName(), found);
            if (found) {
                if (lang == null) {
                    builder.startObject(StoredScriptSource.SCRIPT_PARSE_FIELD.getPreferredName());
                    builder.field(StoredScriptSource.LANG_PARSE_FIELD.getPreferredName(), source.getLang());
                    builder.field(StoredScriptSource.CODE_PARSE_FIELD.getPreferredName(), source.getCode());
                    if (source.getOptions().isEmpty() == false) {
                        builder.field(StoredScriptSource.OPTIONS_PARSE_FIELD.getPreferredName(), source.getOptions());
                    }
                    builder.endObject();
                } else {
                    builder.field(StoredScriptSource.SCRIPT_PARSE_FIELD.getPreferredName(), source.getCode());
                }
            }
            builder.endObject();
            return new BytesRestResponse(found ? RestStatus.OK : RestStatus.NOT_FOUND, builder);
        }
    });
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) GetStoredScriptRequest(org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptRequest) GET(org.elasticsearch.rest.RestRequest.Method.GET) RestResponse(org.elasticsearch.rest.RestResponse) RestBuilderListener(org.elasticsearch.rest.action.RestBuilderListener) IOException(java.io.IOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) RestController(org.elasticsearch.rest.RestController) GetStoredScriptResponse(org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptResponse) StoredScriptSource(org.elasticsearch.script.StoredScriptSource) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) Settings(org.elasticsearch.common.settings.Settings) RestStatus(org.elasticsearch.rest.RestStatus) RestRequest(org.elasticsearch.rest.RestRequest) NodeClient(org.elasticsearch.client.node.NodeClient) ParseField(org.elasticsearch.common.ParseField) RestResponse(org.elasticsearch.rest.RestResponse) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) GetStoredScriptRequest(org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptRequest) StoredScriptSource(org.elasticsearch.script.StoredScriptSource) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) IOException(java.io.IOException) GetStoredScriptResponse(org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptResponse)

Example 29 with BytesRestResponse

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

the class RestNodesHotThreadsAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    String[] nodesIds = Strings.splitStringByCommaToArray(request.param("nodeId"));
    NodesHotThreadsRequest nodesHotThreadsRequest = new NodesHotThreadsRequest(nodesIds);
    nodesHotThreadsRequest.threads(request.paramAsInt("threads", nodesHotThreadsRequest.threads()));
    nodesHotThreadsRequest.ignoreIdleThreads(request.paramAsBoolean("ignore_idle_threads", nodesHotThreadsRequest.ignoreIdleThreads()));
    nodesHotThreadsRequest.type(request.param("type", nodesHotThreadsRequest.type()));
    nodesHotThreadsRequest.interval(TimeValue.parseTimeValue(request.param("interval"), nodesHotThreadsRequest.interval(), "interval"));
    nodesHotThreadsRequest.snapshots(request.paramAsInt("snapshots", nodesHotThreadsRequest.snapshots()));
    nodesHotThreadsRequest.timeout(request.param("timeout"));
    return channel -> client.admin().cluster().nodesHotThreads(nodesHotThreadsRequest, new RestResponseListener<NodesHotThreadsResponse>(channel) {

        @Override
        public RestResponse buildResponse(NodesHotThreadsResponse response) throws Exception {
            StringBuilder sb = new StringBuilder();
            for (NodeHotThreads node : response.getNodes()) {
                sb.append("::: ").append(node.getNode().toString()).append("\n");
                Strings.spaceify(3, node.getHotThreads(), sb);
                sb.append('\n');
            }
            return new BytesRestResponse(RestStatus.OK, sb.toString());
        }
    });
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) RestResponse(org.elasticsearch.rest.RestResponse) IOException(java.io.IOException) RestController(org.elasticsearch.rest.RestController) Strings(org.elasticsearch.common.Strings) NodesHotThreadsResponse(org.elasticsearch.action.admin.cluster.node.hotthreads.NodesHotThreadsResponse) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) NodeHotThreads(org.elasticsearch.action.admin.cluster.node.hotthreads.NodeHotThreads) Settings(org.elasticsearch.common.settings.Settings) TimeValue(org.elasticsearch.common.unit.TimeValue) RestStatus(org.elasticsearch.rest.RestStatus) RestRequest(org.elasticsearch.rest.RestRequest) NodeClient(org.elasticsearch.client.node.NodeClient) NodesHotThreadsRequest(org.elasticsearch.action.admin.cluster.node.hotthreads.NodesHotThreadsRequest) RestResponseListener(org.elasticsearch.rest.action.RestResponseListener) NodesHotThreadsRequest(org.elasticsearch.action.admin.cluster.node.hotthreads.NodesHotThreadsRequest) RestResponse(org.elasticsearch.rest.RestResponse) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) NodeHotThreads(org.elasticsearch.action.admin.cluster.node.hotthreads.NodeHotThreads) NodesHotThreadsResponse(org.elasticsearch.action.admin.cluster.node.hotthreads.NodesHotThreadsResponse) IOException(java.io.IOException)

Example 30 with BytesRestResponse

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

the class RestClearIndicesCacheAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    ClearIndicesCacheRequest clearIndicesCacheRequest = new ClearIndicesCacheRequest(Strings.splitStringByCommaToArray(request.param("index")));
    clearIndicesCacheRequest.indicesOptions(IndicesOptions.fromRequest(request, clearIndicesCacheRequest.indicesOptions()));
    fromRequest(request, clearIndicesCacheRequest);
    return channel -> client.admin().indices().clearCache(clearIndicesCacheRequest, new RestBuilderListener<ClearIndicesCacheResponse>(channel) {

        @Override
        public RestResponse buildResponse(ClearIndicesCacheResponse response, XContentBuilder builder) throws Exception {
            builder.startObject();
            buildBroadcastShardsHeader(builder, request, response);
            builder.endObject();
            return new BytesRestResponse(OK, builder);
        }
    });
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) GET(org.elasticsearch.rest.RestRequest.Method.GET) RestResponse(org.elasticsearch.rest.RestResponse) RestBuilderListener(org.elasticsearch.rest.action.RestBuilderListener) IOException(java.io.IOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) RestController(org.elasticsearch.rest.RestController) ClearIndicesCacheResponse(org.elasticsearch.action.admin.indices.cache.clear.ClearIndicesCacheResponse) Strings(org.elasticsearch.common.Strings) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) POST(org.elasticsearch.rest.RestRequest.Method.POST) Settings(org.elasticsearch.common.settings.Settings) RestActions.buildBroadcastShardsHeader(org.elasticsearch.rest.action.RestActions.buildBroadcastShardsHeader) Map(java.util.Map) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) RestRequest(org.elasticsearch.rest.RestRequest) OK(org.elasticsearch.rest.RestStatus.OK) NodeClient(org.elasticsearch.client.node.NodeClient) ClearIndicesCacheRequest(org.elasticsearch.action.admin.indices.cache.clear.ClearIndicesCacheRequest) ParseField(org.elasticsearch.common.ParseField) ClearIndicesCacheResponse(org.elasticsearch.action.admin.indices.cache.clear.ClearIndicesCacheResponse) RestResponse(org.elasticsearch.rest.RestResponse) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) ClearIndicesCacheRequest(org.elasticsearch.action.admin.indices.cache.clear.ClearIndicesCacheRequest) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) IOException(java.io.IOException)

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