Templates: Convert template uses to ScriptService.compileTemplate (elastic/x-pack-elasticsearch#1170)
This is the xpack side of elastic/elasticsearch#24280 Original commit: elastic/x-pack-elasticsearch@90b7b2c6b7
This commit is contained in:
parent
918f4fb962
commit
105b689ec0
|
@ -14,6 +14,7 @@ import org.elasticsearch.script.ExecutableScript;
|
||||||
import org.elasticsearch.script.Script;
|
import org.elasticsearch.script.Script;
|
||||||
import org.elasticsearch.script.ScriptService;
|
import org.elasticsearch.script.ScriptService;
|
||||||
import org.elasticsearch.script.ScriptType;
|
import org.elasticsearch.script.ScriptType;
|
||||||
|
import org.elasticsearch.template.CompiledTemplate;
|
||||||
import org.elasticsearch.xpack.watcher.Watcher;
|
import org.elasticsearch.xpack.watcher.Watcher;
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
@ -55,13 +56,8 @@ public class TextTemplateEngine extends AbstractComponent {
|
||||||
options.put(Script.CONTENT_TYPE_OPTION, mediaType);
|
options.put(Script.CONTENT_TYPE_OPTION, mediaType);
|
||||||
}
|
}
|
||||||
Script script = new Script(textTemplate.getType(), "mustache", template, options, mergedModel);
|
Script script = new Script(textTemplate.getType(), "mustache", template, options, mergedModel);
|
||||||
CompiledScript compiledScript = service.compile(script, Watcher.SCRIPT_CONTEXT);
|
CompiledTemplate compiledTemplate = service.compileTemplate(script, Watcher.SCRIPT_CONTEXT);
|
||||||
ExecutableScript executable = service.executable(compiledScript, model);
|
return compiledTemplate.run(model).utf8ToString();
|
||||||
Object result = executable.run();
|
|
||||||
if (result instanceof BytesReference) {
|
|
||||||
return ((BytesReference) result).utf8ToString();
|
|
||||||
}
|
|
||||||
return result.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private String trimContentType(TextTemplate textTemplate) {
|
private String trimContentType(TextTemplate textTemplate) {
|
||||||
|
|
|
@ -63,6 +63,7 @@ import org.elasticsearch.script.ExecutableScript;
|
||||||
import org.elasticsearch.script.Script;
|
import org.elasticsearch.script.Script;
|
||||||
import org.elasticsearch.script.ScriptContext;
|
import org.elasticsearch.script.ScriptContext;
|
||||||
import org.elasticsearch.script.ScriptService;
|
import org.elasticsearch.script.ScriptService;
|
||||||
|
import org.elasticsearch.template.CompiledTemplate;
|
||||||
import org.elasticsearch.xpack.security.authc.Authentication;
|
import org.elasticsearch.xpack.security.authc.Authentication;
|
||||||
import org.elasticsearch.xpack.security.authz.AuthorizationService;
|
import org.elasticsearch.xpack.security.authz.AuthorizationService;
|
||||||
import org.elasticsearch.xpack.security.authz.accesscontrol.DocumentSubsetReader.DocumentSubsetDirectoryReader;
|
import org.elasticsearch.xpack.security.authz.accesscontrol.DocumentSubsetReader.DocumentSubsetDirectoryReader;
|
||||||
|
@ -277,9 +278,8 @@ public class SecurityIndexSearcherWrapper extends IndexSearcherWrapper {
|
||||||
params.put("_user", userModel);
|
params.put("_user", userModel);
|
||||||
// Always enforce mustache script lang:
|
// Always enforce mustache script lang:
|
||||||
script = new Script(script.getType(), "mustache", script.getIdOrCode(), script.getOptions(), params);
|
script = new Script(script.getType(), "mustache", script.getIdOrCode(), script.getOptions(), params);
|
||||||
CompiledScript compiledScript = scriptService.compile(script, ScriptContext.Standard.SEARCH);
|
CompiledTemplate compiledTemplate = scriptService.compileTemplate(script, ScriptContext.Standard.SEARCH);
|
||||||
ExecutableScript executable = scriptService.executable(compiledScript, script.getParams());
|
return compiledTemplate.run(script.getParams());
|
||||||
return (BytesReference) executable.run();
|
|
||||||
} else {
|
} else {
|
||||||
return querySource;
|
return querySource;
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,6 +17,7 @@ import org.elasticsearch.script.CompiledScript;
|
||||||
import org.elasticsearch.script.Script;
|
import org.elasticsearch.script.Script;
|
||||||
import org.elasticsearch.script.ScriptService;
|
import org.elasticsearch.script.ScriptService;
|
||||||
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
||||||
|
import org.elasticsearch.template.CompiledTemplate;
|
||||||
import org.elasticsearch.xpack.watcher.Watcher;
|
import org.elasticsearch.xpack.watcher.Watcher;
|
||||||
import org.elasticsearch.xpack.watcher.execution.WatchExecutionContext;
|
import org.elasticsearch.xpack.watcher.execution.WatchExecutionContext;
|
||||||
import org.elasticsearch.xpack.watcher.support.Variables;
|
import org.elasticsearch.xpack.watcher.support.Variables;
|
||||||
|
@ -53,8 +54,8 @@ public class WatcherSearchTemplateService extends AbstractComponent {
|
||||||
}
|
}
|
||||||
// Templates are always of lang mustache:
|
// Templates are always of lang mustache:
|
||||||
Script template = new Script(source.getType(), "mustache", source.getIdOrCode(), source.getOptions(), watcherContextParams);
|
Script template = new Script(source.getType(), "mustache", source.getIdOrCode(), source.getOptions(), watcherContextParams);
|
||||||
CompiledScript compiledScript = scriptService.compile(template, Watcher.SCRIPT_CONTEXT);
|
CompiledTemplate compiledTemplate = scriptService.compileTemplate(template, Watcher.SCRIPT_CONTEXT);
|
||||||
return (BytesReference) scriptService.executable(compiledScript, template.getParams()).run();
|
return compiledTemplate.run(template.getParams());
|
||||||
}
|
}
|
||||||
|
|
||||||
public SearchRequest toSearchRequest(WatcherSearchTemplateRequest request) throws IOException {
|
public SearchRequest toSearchRequest(WatcherSearchTemplateRequest request) throws IOException {
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
*/
|
*/
|
||||||
package org.elasticsearch.xpack.common.text;
|
package org.elasticsearch.xpack.common.text;
|
||||||
|
|
||||||
|
import org.elasticsearch.common.bytes.BytesArray;
|
||||||
import org.elasticsearch.common.bytes.BytesReference;
|
import org.elasticsearch.common.bytes.BytesReference;
|
||||||
import org.elasticsearch.common.settings.Settings;
|
import org.elasticsearch.common.settings.Settings;
|
||||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||||
|
@ -15,6 +16,7 @@ import org.elasticsearch.script.ExecutableScript;
|
||||||
import org.elasticsearch.script.Script;
|
import org.elasticsearch.script.Script;
|
||||||
import org.elasticsearch.script.ScriptService;
|
import org.elasticsearch.script.ScriptService;
|
||||||
import org.elasticsearch.script.ScriptType;
|
import org.elasticsearch.script.ScriptType;
|
||||||
|
import org.elasticsearch.template.CompiledTemplate;
|
||||||
import org.elasticsearch.test.ESTestCase;
|
import org.elasticsearch.test.ESTestCase;
|
||||||
import org.elasticsearch.xpack.watcher.Watcher;
|
import org.elasticsearch.xpack.watcher.Watcher;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
|
@ -38,13 +40,11 @@ public class TextTemplateTests extends ESTestCase {
|
||||||
|
|
||||||
private ScriptService service;
|
private ScriptService service;
|
||||||
private TextTemplateEngine engine;
|
private TextTemplateEngine engine;
|
||||||
private ExecutableScript script;
|
|
||||||
private final String lang = "mustache";
|
private final String lang = "mustache";
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void init() throws Exception {
|
public void init() throws Exception {
|
||||||
service = mock(ScriptService.class);
|
service = mock(ScriptService.class);
|
||||||
script = mock(ExecutableScript.class);
|
|
||||||
engine = new TextTemplateEngine(Settings.EMPTY, service);
|
engine = new TextTemplateEngine(Settings.EMPTY, service);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,12 +57,10 @@ public class TextTemplateTests extends ESTestCase {
|
||||||
merged = unmodifiableMap(merged);
|
merged = unmodifiableMap(merged);
|
||||||
ScriptType type = randomFrom(ScriptType.values());
|
ScriptType type = randomFrom(ScriptType.values());
|
||||||
|
|
||||||
CompiledScript compiledScript = mock(CompiledScript.class);
|
CompiledTemplate compiledTemplate = templateParams -> new BytesArray("rendered_text");
|
||||||
when(service.compile(new Script(type, lang, templateText,
|
when(service.compileTemplate(new Script(type, lang, templateText,
|
||||||
type == ScriptType.INLINE ? Collections.singletonMap("content_type", "text/plain") : null,
|
type == ScriptType.INLINE ? Collections.singletonMap("content_type", "text/plain") : null,
|
||||||
merged), Watcher.SCRIPT_CONTEXT)).thenReturn(compiledScript);
|
merged), Watcher.SCRIPT_CONTEXT)).thenReturn(compiledTemplate);
|
||||||
when(service.executable(compiledScript, model)).thenReturn(script);
|
|
||||||
when(script.run()).thenReturn("rendered_text");
|
|
||||||
|
|
||||||
TextTemplate template = templateBuilder(type, templateText, params);
|
TextTemplate template = templateBuilder(type, templateText, params);
|
||||||
assertThat(engine.render(template, model), is("rendered_text"));
|
assertThat(engine.render(template, model), is("rendered_text"));
|
||||||
|
@ -74,12 +72,10 @@ public class TextTemplateTests extends ESTestCase {
|
||||||
Map<String, Object> model = singletonMap("key", "model_val");
|
Map<String, Object> model = singletonMap("key", "model_val");
|
||||||
ScriptType type = randomFrom(ScriptType.values());
|
ScriptType type = randomFrom(ScriptType.values());
|
||||||
|
|
||||||
CompiledScript compiledScript = mock(CompiledScript.class);
|
CompiledTemplate compiledTemplate = templateParams -> new BytesArray("rendered_text");
|
||||||
when(service.compile(new Script(type, lang, templateText,
|
when(service.compileTemplate(new Script(type, lang, templateText,
|
||||||
type == ScriptType.INLINE ? Collections.singletonMap("content_type", "text/plain") : null,
|
type == ScriptType.INLINE ? Collections.singletonMap("content_type", "text/plain") : null,
|
||||||
model), Watcher.SCRIPT_CONTEXT)).thenReturn(compiledScript);
|
model), Watcher.SCRIPT_CONTEXT)).thenReturn(compiledTemplate);
|
||||||
when(service.executable(compiledScript, model)).thenReturn(script);
|
|
||||||
when(script.run()).thenReturn("rendered_text");
|
|
||||||
|
|
||||||
TextTemplate template = templateBuilder(type, templateText, params);
|
TextTemplate template = templateBuilder(type, templateText, params);
|
||||||
assertThat(engine.render(template, model), is("rendered_text"));
|
assertThat(engine.render(template, model), is("rendered_text"));
|
||||||
|
@ -89,12 +85,10 @@ public class TextTemplateTests extends ESTestCase {
|
||||||
String templateText = "_template";
|
String templateText = "_template";
|
||||||
Map<String, Object> model = singletonMap("key", "model_val");
|
Map<String, Object> model = singletonMap("key", "model_val");
|
||||||
|
|
||||||
CompiledScript compiledScript = mock(CompiledScript.class);
|
CompiledTemplate compiledTemplate = templateParams -> new BytesArray("rendered_text");
|
||||||
when(service.compile(new Script(ScriptType.INLINE, lang, templateText,
|
when(service.compileTemplate(new Script(ScriptType.INLINE, lang, templateText,
|
||||||
Collections.singletonMap("content_type", "text/plain"), model), Watcher.SCRIPT_CONTEXT))
|
Collections.singletonMap("content_type", "text/plain"), model), Watcher.SCRIPT_CONTEXT))
|
||||||
.thenReturn(compiledScript);
|
.thenReturn(compiledTemplate);
|
||||||
when(service.executable(compiledScript, model)).thenReturn(script);
|
|
||||||
when(script.run()).thenReturn("rendered_text");
|
|
||||||
|
|
||||||
TextTemplate template = new TextTemplate(templateText);
|
TextTemplate template = new TextTemplate(templateText);
|
||||||
assertThat(engine.render(template, model), is("rendered_text"));
|
assertThat(engine.render(template, model), is("rendered_text"));
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
*/
|
*/
|
||||||
package org.elasticsearch.xpack.watcher.transform.script;
|
package org.elasticsearch.xpack.watcher.transform.script;
|
||||||
|
|
||||||
|
import org.elasticsearch.common.bytes.BytesArray;
|
||||||
import org.elasticsearch.common.settings.Settings;
|
import org.elasticsearch.common.settings.Settings;
|
||||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||||
import org.elasticsearch.common.xcontent.XContentParser;
|
import org.elasticsearch.common.xcontent.XContentParser;
|
||||||
|
@ -14,6 +15,7 @@ import org.elasticsearch.script.Script;
|
||||||
import org.elasticsearch.script.ScriptException;
|
import org.elasticsearch.script.ScriptException;
|
||||||
import org.elasticsearch.script.ScriptService;
|
import org.elasticsearch.script.ScriptService;
|
||||||
import org.elasticsearch.script.ScriptType;
|
import org.elasticsearch.script.ScriptType;
|
||||||
|
import org.elasticsearch.template.CompiledTemplate;
|
||||||
import org.elasticsearch.test.ESTestCase;
|
import org.elasticsearch.test.ESTestCase;
|
||||||
import org.elasticsearch.threadpool.TestThreadPool;
|
import org.elasticsearch.threadpool.TestThreadPool;
|
||||||
import org.elasticsearch.threadpool.ThreadPool;
|
import org.elasticsearch.threadpool.ThreadPool;
|
||||||
|
@ -169,7 +171,7 @@ public class ScriptTransformTests extends ESTestCase {
|
||||||
String errorMessage = "expected error message";
|
String errorMessage = "expected error message";
|
||||||
ScriptException scriptException = new ScriptException(errorMessage, new RuntimeException("foo"),
|
ScriptException scriptException = new ScriptException(errorMessage, new RuntimeException("foo"),
|
||||||
Collections.emptyList(), "whatever", "whatever");
|
Collections.emptyList(), "whatever", "whatever");
|
||||||
when(scriptService.compile(anyObject(), eq(Watcher.SCRIPT_CONTEXT))).thenThrow(scriptException);
|
when(scriptService.compileTemplate(anyObject(), eq(Watcher.SCRIPT_CONTEXT))).thenThrow(scriptException);
|
||||||
|
|
||||||
ScriptTransformFactory transformFactory = new ScriptTransformFactory(Settings.builder().build(), scriptService);
|
ScriptTransformFactory transformFactory = new ScriptTransformFactory(Settings.builder().build(), scriptService);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue