|
19 | 19 |
|
20 | 20 | package org.elasticsearch.ingest.common;
|
21 | 21 |
|
22 |
| -import org.apache.logging.log4j.Logger; |
23 |
| -import org.elasticsearch.common.Strings; |
24 |
| -import org.elasticsearch.common.logging.DeprecationLogger; |
25 |
| -import org.elasticsearch.common.logging.ESLoggerFactory; |
| 22 | +import com.fasterxml.jackson.core.JsonFactory; |
| 23 | + |
| 24 | +import org.elasticsearch.common.xcontent.NamedXContentRegistry; |
| 25 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 26 | +import org.elasticsearch.common.xcontent.json.JsonXContent; |
| 27 | +import org.elasticsearch.common.xcontent.json.JsonXContentParser; |
26 | 28 | import org.elasticsearch.ingest.AbstractProcessor;
|
27 | 29 | import org.elasticsearch.ingest.IngestDocument;
|
28 | 30 | import org.elasticsearch.ingest.Processor;
|
|
31 | 33 | import org.elasticsearch.script.ScriptException;
|
32 | 34 | import org.elasticsearch.script.ScriptService;
|
33 | 35 |
|
| 36 | +import java.util.Arrays; |
34 | 37 | import java.util.Map;
|
35 | 38 |
|
36 |
| -import static java.util.Collections.emptyMap; |
37 |
| -import static org.elasticsearch.common.Strings.hasLength; |
38 | 39 | import static org.elasticsearch.ingest.ConfigurationUtils.newConfigurationException;
|
39 |
| -import static org.elasticsearch.ingest.ConfigurationUtils.readOptionalMap; |
40 |
| -import static org.elasticsearch.ingest.ConfigurationUtils.readOptionalStringProperty; |
41 |
| -import static org.elasticsearch.script.ScriptType.INLINE; |
42 |
| -import static org.elasticsearch.script.ScriptType.STORED; |
43 | 40 |
|
44 | 41 | /**
|
45 | 42 | * Processor that evaluates a script with an ingest document in its context.
|
46 | 43 | */
|
47 | 44 | public final class ScriptProcessor extends AbstractProcessor {
|
48 | 45 |
|
49 | 46 | public static final String TYPE = "script";
|
| 47 | + private static final JsonFactory JSON_FACTORY = new JsonFactory(); |
50 | 48 |
|
51 | 49 | private final Script script;
|
52 | 50 | private final ScriptService scriptService;
|
@@ -87,66 +85,27 @@ Script getScript() {
|
87 | 85 | }
|
88 | 86 |
|
89 | 87 | public static final class Factory implements Processor.Factory {
|
90 |
| - private final Logger logger = ESLoggerFactory.getLogger(Factory.class); |
91 |
| - private final DeprecationLogger deprecationLogger = new DeprecationLogger(logger); |
92 |
| - |
93 | 88 | private final ScriptService scriptService;
|
94 | 89 |
|
95 | 90 | public Factory(ScriptService scriptService) {
|
96 | 91 | this.scriptService = scriptService;
|
97 | 92 | }
|
98 | 93 |
|
99 | 94 | @Override
|
100 |
| - @SuppressWarnings("unchecked") |
101 | 95 | public ScriptProcessor create(Map<String, Processor.Factory> registry, String processorTag,
|
102 | 96 | Map<String, Object> config) throws Exception {
|
103 |
| - String lang = readOptionalStringProperty(TYPE, processorTag, config, "lang"); |
104 |
| - String source = readOptionalStringProperty(TYPE, processorTag, config, "source"); |
105 |
| - String id = readOptionalStringProperty(TYPE, processorTag, config, "id"); |
106 |
| - Map<String, ?> params = readOptionalMap(TYPE, processorTag, config, "params"); |
107 |
| - |
108 |
| - if (source == null) { |
109 |
| - source = readOptionalStringProperty(TYPE, processorTag, config, "inline"); |
110 |
| - if (source != null) { |
111 |
| - deprecationLogger.deprecated("Specifying script source with [inline] is deprecated, use [source] instead."); |
112 |
| - } |
113 |
| - } |
114 |
| - |
115 |
| - boolean containsNoScript = !hasLength(id) && !hasLength(source); |
116 |
| - if (containsNoScript) { |
117 |
| - throw newConfigurationException(TYPE, processorTag, null, "Need [id] or [source] parameter to refer to scripts"); |
118 |
| - } |
119 |
| - |
120 |
| - boolean moreThanOneConfigured = Strings.hasLength(id) && Strings.hasLength(source); |
121 |
| - if (moreThanOneConfigured) { |
122 |
| - throw newConfigurationException(TYPE, processorTag, null, "Only one of [id] or [source] may be configured"); |
123 |
| - } |
124 |
| - |
125 |
| - if (lang == null) { |
126 |
| - lang = Script.DEFAULT_SCRIPT_LANG; |
127 |
| - } |
| 97 | + XContentBuilder builder = XContentBuilder.builder(JsonXContent.jsonXContent).map(config); |
| 98 | + JsonXContentParser parser = new JsonXContentParser(NamedXContentRegistry.EMPTY, |
| 99 | + JSON_FACTORY.createParser(builder.bytes().streamInput())); |
| 100 | + Script script = Script.parse(parser); |
128 | 101 |
|
129 |
| - if (params == null) { |
130 |
| - params = emptyMap(); |
131 |
| - } |
132 |
| - |
133 |
| - final Script script; |
134 |
| - String scriptPropertyUsed; |
135 |
| - if (Strings.hasLength(source)) { |
136 |
| - script = new Script(INLINE, lang, source, (Map<String, Object>)params); |
137 |
| - scriptPropertyUsed = "source"; |
138 |
| - } else if (Strings.hasLength(id)) { |
139 |
| - script = new Script(STORED, null, id, (Map<String, Object>)params); |
140 |
| - scriptPropertyUsed = "id"; |
141 |
| - } else { |
142 |
| - throw newConfigurationException(TYPE, processorTag, null, "Could not initialize script"); |
143 |
| - } |
| 102 | + Arrays.asList("id", "source", "inline", "lang", "params", "options").forEach(config::remove); |
144 | 103 |
|
145 | 104 | // verify script is able to be compiled before successfully creating processor.
|
146 | 105 | try {
|
147 | 106 | scriptService.compile(script, ExecutableScript.INGEST_CONTEXT);
|
148 | 107 | } catch (ScriptException e) {
|
149 |
| - throw newConfigurationException(TYPE, processorTag, scriptPropertyUsed, e); |
| 108 | + throw newConfigurationException(TYPE, processorTag, null, e); |
150 | 109 | }
|
151 | 110 |
|
152 | 111 | return new ScriptProcessor(processorTag, script, scriptService);
|
|
0 commit comments