Search in sources :

Example 1 with GetStoredScriptRequest

use of org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptRequest in project elasticsearch by elastic.

the class RestGetSearchTemplateAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, NodeClient client) throws IOException {
    String id = request.param("id");
    GetStoredScriptRequest getRequest = new GetStoredScriptRequest(id, Script.DEFAULT_TEMPLATE_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);
            builder.field(StoredScriptSource.LANG_PARSE_FIELD.getPreferredName(), Script.DEFAULT_TEMPLATE_LANG);
            StoredScriptSource source = response.getSource();
            boolean found = source != null;
            builder.field(FOUND_PARSE_FIELD.getPreferredName(), found);
            if (found) {
                builder.field(StoredScriptSource.TEMPLATE_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) Script(org.elasticsearch.script.Script) 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 2 with GetStoredScriptRequest

use of org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptRequest in project elasticsearch by elastic.

the class GetStoredScriptRequestTests method testGetIndexedScriptRequestSerialization.

public void testGetIndexedScriptRequestSerialization() throws IOException {
    GetStoredScriptRequest request = new GetStoredScriptRequest("lang", "id");
    BytesStreamOutput out = new BytesStreamOutput();
    out.setVersion(randomVersion(random()));
    request.writeTo(out);
    StreamInput in = out.bytes().streamInput();
    in.setVersion(out.getVersion());
    GetStoredScriptRequest request2 = new GetStoredScriptRequest();
    request2.readFrom(in);
    assertThat(request2.id(), equalTo(request.id()));
    assertThat(request2.lang(), equalTo(request.lang()));
}
Also used : StreamInput(org.elasticsearch.common.io.stream.StreamInput) GetStoredScriptRequest(org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptRequest) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 3 with GetStoredScriptRequest

use of org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptRequest 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 4 with GetStoredScriptRequest

use of org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptRequest in project elasticsearch by elastic.

the class ScriptServiceTests method testGetStoredScript.

public void testGetStoredScript() throws Exception {
    buildScriptService(Settings.EMPTY);
    ClusterState cs = ClusterState.builder(new ClusterName("_name")).metaData(MetaData.builder().putCustom(ScriptMetaData.TYPE, new ScriptMetaData.Builder(null).storeScript("_id", StoredScriptSource.parse("_lang", new BytesArray("{\"script\":\"abc\"}"), XContentType.JSON)).build())).build();
    assertEquals("abc", scriptService.getStoredScript(cs, new GetStoredScriptRequest("_id", "_lang")).getCode());
    assertNull(scriptService.getStoredScript(cs, new GetStoredScriptRequest("_id2", "_lang")));
    cs = ClusterState.builder(new ClusterName("_name")).build();
    assertNull(scriptService.getStoredScript(cs, new GetStoredScriptRequest("_id", "_lang")));
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) BytesArray(org.elasticsearch.common.bytes.BytesArray) ClusterName(org.elasticsearch.cluster.ClusterName) GetStoredScriptRequest(org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptRequest)

Aggregations

GetStoredScriptRequest (org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptRequest)4 IOException (java.io.IOException)2 GetStoredScriptResponse (org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptResponse)2 NodeClient (org.elasticsearch.client.node.NodeClient)2 ParseField (org.elasticsearch.common.ParseField)2 Settings (org.elasticsearch.common.settings.Settings)2 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)2 BaseRestHandler (org.elasticsearch.rest.BaseRestHandler)2 BytesRestResponse (org.elasticsearch.rest.BytesRestResponse)2 RestController (org.elasticsearch.rest.RestController)2 RestRequest (org.elasticsearch.rest.RestRequest)2 GET (org.elasticsearch.rest.RestRequest.Method.GET)2 RestResponse (org.elasticsearch.rest.RestResponse)2 RestStatus (org.elasticsearch.rest.RestStatus)2 RestBuilderListener (org.elasticsearch.rest.action.RestBuilderListener)2 StoredScriptSource (org.elasticsearch.script.StoredScriptSource)2 ClusterName (org.elasticsearch.cluster.ClusterName)1 ClusterState (org.elasticsearch.cluster.ClusterState)1 BytesArray (org.elasticsearch.common.bytes.BytesArray)1 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)1