summaryrefslogtreecommitdiff
path: root/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/ScriptProcessor.java
blob: 2801b24c53489482088769ae326e358d69af4450 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 * Licensed to Elasticsearch under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch licenses this file to you under
 * the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.elasticsearch.ingest.common;

import org.elasticsearch.common.Strings;
import org.elasticsearch.ingest.AbstractProcessor;
import org.elasticsearch.ingest.IngestDocument;
import org.elasticsearch.ingest.Processor;
import org.elasticsearch.script.ExecutableScript;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptContext;
import org.elasticsearch.script.ScriptException;
import org.elasticsearch.script.ScriptService;

import java.util.Map;

import static java.util.Collections.emptyMap;
import static org.elasticsearch.common.Strings.hasLength;
import static org.elasticsearch.ingest.ConfigurationUtils.newConfigurationException;
import static org.elasticsearch.ingest.ConfigurationUtils.readOptionalMap;
import static org.elasticsearch.ingest.ConfigurationUtils.readOptionalStringProperty;
import static org.elasticsearch.script.ScriptType.INLINE;
import static org.elasticsearch.script.ScriptType.STORED;

/**
 * Processor that evaluates a script with an ingest document in its context.
 */
public final class ScriptProcessor extends AbstractProcessor {

    public static final String TYPE = "script";

    private final Script script;
    private final ScriptService scriptService;

    /**
     * Processor that evaluates a script with an ingest document in its context
     *
     * @param tag The processor's tag.
     * @param script The {@link Script} to execute.
     * @param scriptService The {@link ScriptService} used to execute the script.
     */
    ScriptProcessor(String tag, Script script, ScriptService scriptService)  {
        super(tag);
        this.script = script;
        this.scriptService = scriptService;
    }

    /**
     * Executes the script with the Ingest document in context.
     *
     * @param document The Ingest document passed into the script context under the "ctx" object.
     */
    @Override
    public void execute(IngestDocument document) {
        ExecutableScript.Compiled compiledScript = scriptService.compile(script, ScriptContext.INGEST);
        ExecutableScript executableScript = compiledScript.newInstance(script.getParams());
        executableScript.setNextVar("ctx",  document.getSourceAndMetadata());
        executableScript.run();
    }

    @Override
    public String getType() {
        return TYPE;
    }

    Script getScript() {
        return script;
    }

    public static final class Factory implements Processor.Factory {

        private final ScriptService scriptService;

        public Factory(ScriptService scriptService) {
            this.scriptService = scriptService;
        }

        @Override
        @SuppressWarnings("unchecked")
        public ScriptProcessor create(Map<String, Processor.Factory> registry, String processorTag,
                                      Map<String, Object> config) throws Exception {
            String lang = readOptionalStringProperty(TYPE, processorTag, config, "lang");
            String inline = readOptionalStringProperty(TYPE, processorTag, config, "inline");
            String id = readOptionalStringProperty(TYPE, processorTag, config, "id");
            Map<String, ?> params = readOptionalMap(TYPE, processorTag, config, "params");

            boolean containsNoScript = !hasLength(id) && !hasLength(inline);
            if (containsNoScript) {
                throw newConfigurationException(TYPE, processorTag, null, "Need [id] or [inline] parameter to refer to scripts");
            }

            boolean moreThanOneConfigured = Strings.hasLength(id) && Strings.hasLength(inline);
            if (moreThanOneConfigured) {
                throw newConfigurationException(TYPE, processorTag, null, "Only one of [id] or [inline] may be configured");
            }

            if (lang == null) {
                lang = Script.DEFAULT_SCRIPT_LANG;
            }

            if (params == null) {
                params = emptyMap();
            }

            final Script script;
            String scriptPropertyUsed;
            if (Strings.hasLength(inline)) {
                script = new Script(INLINE, lang, inline, (Map<String, Object>)params);
                scriptPropertyUsed = "inline";
            } else if (Strings.hasLength(id)) {
                script = new Script(STORED, lang, id, (Map<String, Object>)params);
                scriptPropertyUsed = "id";
            } else {
                throw newConfigurationException(TYPE, processorTag, null, "Could not initialize script");
            }

            // verify script is able to be compiled before successfully creating processor.
            try {
                scriptService.compile(script, ScriptContext.INGEST);
            } catch (ScriptException e) {
                throw newConfigurationException(TYPE, processorTag, scriptPropertyUsed, e);
            }

            return new ScriptProcessor(processorTag, script, scriptService);
        }
    }
}