summaryrefslogtreecommitdiff
path: root/core/src/main/java/org/elasticsearch/script/Script.java
blob: 05da7138d1977f1ecdb938d2996ed73f077e9fc9 (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
 * 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.script;

import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.ParseFieldMatcher;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.script.ScriptService.ScriptType;

import java.io.IOException;
import java.util.Map;
import java.util.Objects;

/**
 * Script holds all the parameters necessary to compile or find in cache and then execute a script.
 */
public final class Script implements ToXContent, Writeable {

    public static final String DEFAULT_SCRIPT_LANG = "painless";

    private String script;
    private ScriptType type;
    @Nullable private String lang;
    @Nullable private Map<String, Object> params;
    @Nullable private XContentType contentType;

    /**
     * Constructor for simple inline script. The script will have no lang or params set.
     *
     * @param script The inline script to execute.
     */
    public Script(String script) {
        this(script, ScriptType.INLINE, null, null);
    }

    public Script(String script, ScriptType type, String lang, @Nullable Map<String, ?> params) {
        this(script, type, lang, params, null);
    }

    /**
     * Constructor for Script.
     *
     * @param script        The cache key of the script to be compiled/executed. For inline scripts this is the actual
     *                      script source code. For indexed scripts this is the id used in the request. For on file
     *                      scripts this is the file name.
     * @param type          The type of script -- dynamic, stored, or file.
     * @param lang          The language of the script to be compiled/executed.
     * @param params        The map of parameters the script will be executed with.
     * @param contentType   The {@link XContentType} of the script. Only relevant for inline scripts that have not been
     *                      defined as a plain string, but as json or yaml content. This class needs this information
     *                      when serializing the script back to xcontent.
     */
    @SuppressWarnings("unchecked")
    public Script(String script, ScriptType type, String lang, @Nullable Map<String, ?> params,
                  @Nullable XContentType  contentType) {
        if (contentType != null && type != ScriptType.INLINE) {
            throw new IllegalArgumentException("The parameter contentType only makes sense for inline scripts");
        }
        this.script = Objects.requireNonNull(script);
        this.type = Objects.requireNonNull(type);
        this.lang = lang == null ? DEFAULT_SCRIPT_LANG : lang;
        this.params = (Map<String, Object>) params;
        this.contentType = contentType;
    }

    public Script(StreamInput in) throws IOException {
        script = in.readString();
        if (in.readBoolean()) {
            type = ScriptType.readFrom(in);
        }
        lang = in.readOptionalString();
        params = in.readMap();
        if (in.readBoolean()) {
            contentType = XContentType.readFrom(in);
        }
    }

    @Override
    public void writeTo(StreamOutput out) throws IOException {
        out.writeString(script);
        boolean hasType = type != null;
        out.writeBoolean(hasType);
        if (hasType) {
            ScriptType.writeTo(type, out);
        }
        out.writeOptionalString(lang);
        out.writeMap(params);
        boolean hasContentType = contentType != null;
        out.writeBoolean(hasContentType);
        if (hasContentType) {
            XContentType.writeTo(contentType, out);
        }
    }

    /**
     * Method for getting the script.
     * @return The cache key of the script to be compiled/executed.  For dynamic scripts this is the actual
     *         script source code.  For indexed scripts this is the id used in the request.  For on disk scripts
     *         this is the file name.
     */
    public String getScript() {
        return script;
    }

    /**
     * Method for getting the type.
     *
     * @return The type of script -- inline, stored, or file.
     */
    public ScriptType getType() {
        return type;
    }

    /**
     * Method for getting language.
     *
     * @return The language of the script to be compiled/executed.
     */
    public String getLang() {
        return lang;
    }

    /**
     * Method for getting the parameters.
     *
     * @return The map of parameters the script will be executed with.
     */
    public Map<String, Object> getParams() {
        return params;
    }

    /**
     * @return The content type of the script if it is an inline script and the script has been defined as json
     *         or yaml content instead of a plain string.
     */
    public XContentType getContentType() {
        return contentType;
    }

    @Override
    public XContentBuilder toXContent(XContentBuilder builder, Params builderParams) throws IOException {
        if (type == null) {
            return builder.value(script);
        }
        builder.startObject();
        if (type == ScriptType.INLINE && contentType != null && builder.contentType() == contentType) {
            builder.rawField(type.getParseField().getPreferredName(), new BytesArray(script));
        } else {
            builder.field(type.getParseField().getPreferredName(), script);
        }
        if (lang != null) {
            builder.field(ScriptField.LANG.getPreferredName(), lang);
        }
        if (params != null) {
            builder.field(ScriptField.PARAMS.getPreferredName(), params);
        }
        builder.endObject();
        return builder;
    }

    public static Script parse(XContentParser parser, ParseFieldMatcher parseFieldMatcher) throws IOException {
        return parse(parser, parseFieldMatcher, null);
    }

    public static Script parse(XContentParser parser, ParseFieldMatcher parseFieldMatcher, @Nullable String lang) throws IOException {
        XContentParser.Token token = parser.currentToken();
        // If the parser hasn't yet been pushed to the first token, do it now
        if (token == null) {
            token = parser.nextToken();
        }
        if (token == XContentParser.Token.VALUE_STRING) {
            return new Script(parser.text(), ScriptType.INLINE, lang, null);
        }
        if (token != XContentParser.Token.START_OBJECT) {
            throw new ElasticsearchParseException("expected a string value or an object, but found [{}] instead", token);
        }
        String script = null;
        ScriptType type = null;
        Map<String, Object> params = null;
        XContentType contentType = null;
        String cfn = null;
        while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
            if (token == XContentParser.Token.FIELD_NAME) {
                cfn = parser.currentName();
            } else if (parseFieldMatcher.match(cfn, ScriptType.INLINE.getParseField())) {
                type = ScriptType.INLINE;
                if (parser.currentToken() == XContentParser.Token.START_OBJECT) {
                    contentType = parser.contentType();
                    XContentBuilder builder = XContentFactory.contentBuilder(contentType);
                    script = builder.copyCurrentStructure(parser).bytes().utf8ToString();
                } else {
                    script = parser.text();
                }
            } else if (parseFieldMatcher.match(cfn, ScriptType.FILE.getParseField())) {
                type = ScriptType.FILE;
                if (token == XContentParser.Token.VALUE_STRING) {
                    script = parser.text();
                } else {
                    throw new ElasticsearchParseException("expected a string value for field [{}], but found [{}]", cfn, token);
                }
            } else if (parseFieldMatcher.match(cfn, ScriptType.STORED.getParseField())) {
                type = ScriptType.STORED;
                if (token == XContentParser.Token.VALUE_STRING) {
                    script = parser.text();
                } else {
                    throw new ElasticsearchParseException("expected a string value for field [{}], but found [{}]", cfn, token);
                }
            } else if (parseFieldMatcher.match(cfn, ScriptField.LANG)) {
                if (token == XContentParser.Token.VALUE_STRING) {
                    lang = parser.text();
                } else {
                    throw new ElasticsearchParseException("expected a string value for field [{}], but found [{}]", cfn, token);
                }
            } else if (parseFieldMatcher.match(cfn, ScriptField.PARAMS)) {
                if (token == XContentParser.Token.START_OBJECT) {
                    params = parser.map();
                } else {
                    throw new ElasticsearchParseException("expected an object for field [{}], but found [{}]", cfn, token);
                }
            } else {
                throw new ElasticsearchParseException("unexpected field [{}]", cfn);
            }
        }
        if (script == null) {
            throw new ElasticsearchParseException("expected one of [{}], [{}] or [{}] fields, but found none",
                    ScriptType.INLINE.getParseField() .getPreferredName(), ScriptType.FILE.getParseField().getPreferredName(),
                    ScriptType.STORED.getParseField() .getPreferredName());
        }
        return new Script(script, type, lang, params, contentType);
    }

    @Override
    public int hashCode() {
        return Objects.hash(lang, params, script, type, contentType);
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null) return false;
        if (getClass() != obj.getClass()) return false;
        Script other = (Script) obj;

        return Objects.equals(lang, other.lang) &&
                Objects.equals(params, other.params) &&
                Objects.equals(script, other.script) &&
                Objects.equals(type, other.type) &&
                Objects.equals(contentType, other.contentType);
    }

    @Override
    public String toString() {
        return "[script: " + script + ", type: " + type.getParseField().getPreferredName() + ", lang: "
                + lang + ", params: " + params + "]";
    }

    public interface ScriptField {
        ParseField SCRIPT = new ParseField("script");
        ParseField LANG = new ParseField("lang");
        ParseField PARAMS = new ParseField("params");
    }

}