Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import com.github.mustachejava.codes.DefaultMustache;
import com.github.mustachejava.codes.IterableCode;
import com.github.mustachejava.codes.WriteCode;
import org.apache.lucene.search.highlight.DefaultEncoder;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentType;
Expand All @@ -51,30 +50,31 @@
import java.util.regex.Pattern;

public class CustomMustacheFactory extends DefaultMustacheFactory {
static final String V7_JSON_MEDIA_TYPE_WITH_CHARSET = "application/json; charset=UTF-8";
static final String JSON_MEDIA_TYPE_WITH_CHARSET = "application/json;charset=utf-8";
static final String JSON_MEDIA_TYPE = "application/json";
static final String PLAIN_TEXT_MEDIA_TYPE = "text/plain";
static final String X_WWW_FORM_URLENCODED_MEDIA_TYPE = "application/x-www-form-urlencoded";

static final String JSON_MIME_TYPE_WITH_CHARSET = "application/json;charset=utf-8";
static final String JSON_MIME_TYPE = "application/json";
static final String PLAIN_TEXT_MIME_TYPE = "text/plain";
static final String X_WWW_FORM_URLENCODED_MIME_TYPE = "application/x-www-form-urlencoded";

private static final String DEFAULT_MIME_TYPE = JSON_MIME_TYPE;
private static final String DEFAULT_MEDIA_TYPE = JSON_MEDIA_TYPE;

private static final Map<String, Supplier<Encoder>> ENCODERS = Map.of(
JSON_MIME_TYPE_WITH_CHARSET, JsonEscapeEncoder::new,
JSON_MIME_TYPE, JsonEscapeEncoder::new,
PLAIN_TEXT_MIME_TYPE, DefaultEncoder::new,
X_WWW_FORM_URLENCODED_MIME_TYPE, UrlEncoder::new);
V7_JSON_MEDIA_TYPE_WITH_CHARSET, JsonEscapeEncoder::new,
JSON_MEDIA_TYPE_WITH_CHARSET, JsonEscapeEncoder::new,
JSON_MEDIA_TYPE, JsonEscapeEncoder::new,
PLAIN_TEXT_MEDIA_TYPE, DefaultEncoder::new,
X_WWW_FORM_URLENCODED_MEDIA_TYPE, UrlEncoder::new);

private final Encoder encoder;

public CustomMustacheFactory(String mimeType) {
public CustomMustacheFactory(String mediaType) {
super();
setObjectHandler(new CustomReflectionObjectHandler());
this.encoder = createEncoder(mimeType);
this.encoder = createEncoder(mediaType);
}

public CustomMustacheFactory() {
this(DEFAULT_MIME_TYPE);
this(DEFAULT_MEDIA_TYPE);
}

@Override
Expand All @@ -86,10 +86,10 @@ public void encode(String value, Writer writer) {
}
}

static Encoder createEncoder(String mimeType) {
final Supplier<Encoder> supplier = ENCODERS.get(mimeType);
static Encoder createEncoder(String mediaType) {
final Supplier<Encoder> supplier = ENCODERS.get(mediaType);
if (supplier == null) {
throw new IllegalArgumentException("No encoder found for MIME type [" + mimeType + "]");
throw new IllegalArgumentException("No encoder found for media type [" + mediaType + "]");
}
return supplier.get();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@

import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonMap;
import static org.elasticsearch.script.mustache.CustomMustacheFactory.JSON_MIME_TYPE;
import static org.elasticsearch.script.mustache.CustomMustacheFactory.PLAIN_TEXT_MIME_TYPE;
import static org.elasticsearch.script.mustache.CustomMustacheFactory.X_WWW_FORM_URLENCODED_MIME_TYPE;
import static org.elasticsearch.script.mustache.CustomMustacheFactory.JSON_MEDIA_TYPE;
import static org.elasticsearch.script.mustache.CustomMustacheFactory.PLAIN_TEXT_MEDIA_TYPE;
import static org.elasticsearch.script.mustache.CustomMustacheFactory.X_WWW_FORM_URLENCODED_MEDIA_TYPE;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;

Expand All @@ -40,33 +40,33 @@ public void testCreateEncoder() {
{
final IllegalArgumentException e =
expectThrows(IllegalArgumentException.class, () -> CustomMustacheFactory.createEncoder("non-existent"));
assertThat(e.getMessage(), equalTo("No encoder found for MIME type [non-existent]"));
assertThat(e.getMessage(), equalTo("No encoder found for media type [non-existent]"));
}

{
final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> CustomMustacheFactory.createEncoder(""));
assertThat(e.getMessage(), equalTo("No encoder found for MIME type []"));
assertThat(e.getMessage(), equalTo("No encoder found for media type []"));
}

{
final IllegalArgumentException e =
expectThrows(IllegalArgumentException.class, () -> CustomMustacheFactory.createEncoder("test"));
assertThat(e.getMessage(), equalTo("No encoder found for MIME type [test]"));
assertThat(e.getMessage(), equalTo("No encoder found for media type [test]"));
}

assertThat(CustomMustacheFactory.createEncoder(CustomMustacheFactory.JSON_MIME_TYPE_WITH_CHARSET),
assertThat(CustomMustacheFactory.createEncoder(CustomMustacheFactory.JSON_MEDIA_TYPE_WITH_CHARSET),
instanceOf(CustomMustacheFactory.JsonEscapeEncoder.class));
assertThat(CustomMustacheFactory.createEncoder(CustomMustacheFactory.JSON_MIME_TYPE),
assertThat(CustomMustacheFactory.createEncoder(CustomMustacheFactory.JSON_MEDIA_TYPE),
instanceOf(CustomMustacheFactory.JsonEscapeEncoder.class));
assertThat(CustomMustacheFactory.createEncoder(CustomMustacheFactory.PLAIN_TEXT_MIME_TYPE),
assertThat(CustomMustacheFactory.createEncoder(CustomMustacheFactory.PLAIN_TEXT_MEDIA_TYPE),
instanceOf(CustomMustacheFactory.DefaultEncoder.class));
assertThat(CustomMustacheFactory.createEncoder(CustomMustacheFactory.X_WWW_FORM_URLENCODED_MIME_TYPE),
assertThat(CustomMustacheFactory.createEncoder(CustomMustacheFactory.X_WWW_FORM_URLENCODED_MEDIA_TYPE),
instanceOf(CustomMustacheFactory.UrlEncoder.class));
}

public void testJsonEscapeEncoder() {
final ScriptEngine engine = new MustacheScriptEngine();
final Map<String, String> params = randomBoolean() ? singletonMap(Script.CONTENT_TYPE_OPTION, JSON_MIME_TYPE) : emptyMap();
final Map<String, String> params = randomBoolean() ? singletonMap(Script.CONTENT_TYPE_OPTION, JSON_MEDIA_TYPE) : emptyMap();

TemplateScript.Factory compiled = engine.compile(null, "{\"field\": \"{{value}}\"}", TemplateScript.CONTEXT, params);

Expand All @@ -76,7 +76,7 @@ public void testJsonEscapeEncoder() {

public void testDefaultEncoder() {
final ScriptEngine engine = new MustacheScriptEngine();
final Map<String, String> params = singletonMap(Script.CONTENT_TYPE_OPTION, PLAIN_TEXT_MIME_TYPE);
final Map<String, String> params = singletonMap(Script.CONTENT_TYPE_OPTION, PLAIN_TEXT_MEDIA_TYPE);

TemplateScript.Factory compiled = engine.compile(null, "{\"field\": \"{{value}}\"}", TemplateScript.CONTEXT, params);

Expand All @@ -86,7 +86,7 @@ public void testDefaultEncoder() {

public void testUrlEncoder() {
final ScriptEngine engine = new MustacheScriptEngine();
final Map<String, String> params = singletonMap(Script.CONTENT_TYPE_OPTION, X_WWW_FORM_URLENCODED_MIME_TYPE);
final Map<String, String> params = singletonMap(Script.CONTENT_TYPE_OPTION, X_WWW_FORM_URLENCODED_MEDIA_TYPE);

TemplateScript.Factory compiled = engine.compile(null, "{\"field\": \"{{value}}\"}", TemplateScript.CONTEXT, params);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
---
"Verify that we can still find things with the template":
- skip:
version: "all"
reason: "Awaits fix: https://github.com/elastic/elasticsearch/issues/66986"

- do:
search_template:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
---
"Verify that we can still find things with the template":
- skip:
version: "all"
reason: "Awaits fix: https://github.com/elastic/elasticsearch/issues/66986"

- do:
search_template:
Expand Down