32
32
import org .elasticsearch .common .io .stream .StreamInput ;
33
33
import org .elasticsearch .common .io .stream .StreamOutput ;
34
34
import org .elasticsearch .common .io .stream .Writeable ;
35
+ import org .elasticsearch .common .logging .DeprecationLogger ;
36
+ import org .elasticsearch .common .logging .Loggers ;
35
37
import org .elasticsearch .common .xcontent .LoggingDeprecationHandler ;
36
38
import org .elasticsearch .common .xcontent .NamedXContentRegistry ;
37
39
import org .elasticsearch .common .xcontent .ObjectParser ;
57
59
*/
58
60
public class StoredScriptSource extends AbstractDiffable <StoredScriptSource > implements Writeable , ToXContentObject {
59
61
62
+ /**
63
+ * Standard deprecation logger for used to deprecate allowance of empty templates.
64
+ */
65
+ private static final DeprecationLogger DEPRECATION_LOGGER = new DeprecationLogger (Loggers .getLogger (StoredScriptSource .class ));
66
+
60
67
/**
61
68
* Standard {@link ParseField} for outer level of stored script source.
62
69
*/
@@ -109,7 +116,7 @@ private void setLang(String lang) {
109
116
private void setSource (XContentParser parser ) {
110
117
try {
111
118
if (parser .currentToken () == Token .START_OBJECT ) {
112
- //this is really for search templates, that need to be converted to json format
119
+ // this is really for search templates, that need to be converted to json format
113
120
XContentBuilder builder = XContentFactory .jsonBuilder ();
114
121
source = Strings .toString (builder .copyCurrentStructure (parser ));
115
122
options .put (Script .CONTENT_TYPE_OPTION , XContentType .JSON .mediaType ());
@@ -131,18 +138,38 @@ private void setOptions(Map<String, String> options) {
131
138
132
139
/**
133
140
* Validates the parameters and creates an {@link StoredScriptSource}.
141
+ *
142
+ * @param ignoreEmpty Specify as {@code true} to ignoreEmpty the empty source check.
143
+ * This allow empty templates to be loaded for backwards compatibility.
144
+ * This allow empty templates to be loaded for backwards compatibility.
134
145
*/
135
- private StoredScriptSource build () {
146
+ private StoredScriptSource build (boolean ignoreEmpty ) {
136
147
if (lang == null ) {
137
148
throw new IllegalArgumentException ("must specify lang for stored script" );
138
149
} else if (lang .isEmpty ()) {
139
150
throw new IllegalArgumentException ("lang cannot be empty" );
140
151
}
141
152
142
153
if (source == null ) {
143
- throw new IllegalArgumentException ("must specify source for stored script" );
154
+ if (ignoreEmpty || Script .DEFAULT_TEMPLATE_LANG .equals (lang )) {
155
+ if (Script .DEFAULT_TEMPLATE_LANG .equals (lang )) {
156
+ DEPRECATION_LOGGER .deprecated ("empty templates should no longer be used" );
157
+ } else {
158
+ DEPRECATION_LOGGER .deprecated ("empty scripts should no longer be used" );
159
+ }
160
+ } else {
161
+ throw new IllegalArgumentException ("must specify source for stored script" );
162
+ }
144
163
} else if (source .isEmpty ()) {
145
- throw new IllegalArgumentException ("source cannot be empty" );
164
+ if (ignoreEmpty || Script .DEFAULT_TEMPLATE_LANG .equals (lang )) {
165
+ if (Script .DEFAULT_TEMPLATE_LANG .equals (lang )) {
166
+ DEPRECATION_LOGGER .deprecated ("empty templates should no longer be used" );
167
+ } else {
168
+ DEPRECATION_LOGGER .deprecated ("empty scripts should no longer be used" );
169
+ }
170
+ } else {
171
+ throw new IllegalArgumentException ("source cannot be empty" );
172
+ }
146
173
}
147
174
148
175
if (options .size () > 1 || options .size () == 1 && options .get (Script .CONTENT_TYPE_OPTION ) == null ) {
@@ -257,6 +284,8 @@ public static StoredScriptSource parse(BytesReference content, XContentType xCon
257
284
token = parser .nextToken ();
258
285
259
286
if (token == Token .END_OBJECT ) {
287
+ DEPRECATION_LOGGER .deprecated ("empty templates should no longer be used" );
288
+
260
289
return new StoredScriptSource (Script .DEFAULT_TEMPLATE_LANG , "" , Collections .emptyMap ());
261
290
}
262
291
@@ -271,7 +300,7 @@ public static StoredScriptSource parse(BytesReference content, XContentType xCon
271
300
token = parser .nextToken ();
272
301
273
302
if (token == Token .START_OBJECT ) {
274
- return PARSER .apply (parser , null ).build ();
303
+ return PARSER .apply (parser , null ).build (false );
275
304
} else {
276
305
throw new ParsingException (parser .getTokenLocation (), "unexpected token [" + token + "], expected [{, <source>]" );
277
306
}
@@ -280,7 +309,13 @@ public static StoredScriptSource parse(BytesReference content, XContentType xCon
280
309
token = parser .nextToken ();
281
310
282
311
if (token == Token .VALUE_STRING ) {
283
- return new StoredScriptSource (Script .DEFAULT_TEMPLATE_LANG , parser .text (), Collections .emptyMap ());
312
+ String source = parser .text ();
313
+
314
+ if (source == null || source .isEmpty ()) {
315
+ DEPRECATION_LOGGER .deprecated ("empty templates should no longer be used" );
316
+ }
317
+
318
+ return new StoredScriptSource (Script .DEFAULT_TEMPLATE_LANG , source , Collections .emptyMap ());
284
319
}
285
320
}
286
321
@@ -293,7 +328,13 @@ public static StoredScriptSource parse(BytesReference content, XContentType xCon
293
328
builder .copyCurrentStructure (parser );
294
329
}
295
330
296
- return new StoredScriptSource (Script .DEFAULT_TEMPLATE_LANG , Strings .toString (builder ), Collections .emptyMap ());
331
+ String source = Strings .toString (builder );
332
+
333
+ if (source == null || source .isEmpty ()) {
334
+ DEPRECATION_LOGGER .deprecated ("empty templates should no longer be used" );
335
+ }
336
+
337
+ return new StoredScriptSource (Script .DEFAULT_TEMPLATE_LANG , source , Collections .emptyMap ());
297
338
}
298
339
}
299
340
} catch (IOException ioe ) {
@@ -320,9 +361,12 @@ public static StoredScriptSource parse(BytesReference content, XContentType xCon
320
361
*
321
362
* Note that the "source" parameter can also handle template parsing including from
322
363
* a complex JSON object.
364
+ *
365
+ * @param ignoreEmpty Specify as {@code true} to ignoreEmpty the empty source check.
366
+ * This allows empty templates to be loaded for backwards compatibility.
323
367
*/
324
- public static StoredScriptSource fromXContent (XContentParser parser ) {
325
- return PARSER .apply (parser , null ).build ();
368
+ public static StoredScriptSource fromXContent (XContentParser parser , boolean ignoreEmpty ) {
369
+ return PARSER .apply (parser , null ).build (ignoreEmpty );
326
370
}
327
371
328
372
/**
0 commit comments