Apache Solr open-source search software
SOLR-8128: Set v.locale specified locale for all LocaleConfig extending VelocityResponseWriter tools
Erik Hatcher33850419bddb
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex c556c6386d5..c3431639f00 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -263,6 +263,9 @@ Bug Fixes* SOLR-8130: Solr's hdfs safe mode detection does not catch all cases of being in safe mode.(Mark Miller, Mike Drob)++* SOLR-8128: Set v.locale specified locale for all LocaleConfig extending VelocityResponseWriter tools.+ (Erik Hatcher)Optimizations----------------------diff --git a/solr/contrib/velocity/src/java/org/apache/solr/response/VelocityResponseWriter.java b/solr/contrib/velocity/src/java/org/apache/solr/response/VelocityResponseWriter.javaindex a812e131443..0995f5c1d5d 100644--- a/solr/contrib/velocity/src/java/org/apache/solr/response/VelocityResponseWriter.java+++ b/solr/contrib/velocity/src/java/org/apache/solr/response/VelocityResponseWriter.java@@ -50,6 +50,7 @@ import org.apache.velocity.tools.generic.ComparisonDateTool;import org.apache.velocity.tools.generic.DisplayTool;import org.apache.velocity.tools.generic.EscapeTool;import org.apache.velocity.tools.generic.ListTool;+import org.apache.velocity.tools.generic.LocaleConfig;import org.apache.velocity.tools.generic.MathTool;import org.apache.velocity.tools.generic.NumberTool;import org.apache.velocity.tools.generic.ResourceTool;@@ -194,17 +195,33 @@ public class VelocityResponseWriter implements QueryResponseWriter, SolrCoreAwarVelocityContext context = new VelocityContext();// Register useful Velocity "tools"+ String locale = request.getParams().get(LOCALE);+ Map toolConfig = new HashMap();+ toolConfig.put("locale", locale);++context.put("log", log); // TODO: add test; TODO: should this be overridable with a custom "log" named tool?context.put("esc", new EscapeTool());context.put("date", new ComparisonDateTool());context.put("list", new ListTool());- context.put("math", new MathTool());- context.put("number", new NumberTool());context.put("sort", new SortTool());- context.put("display", new DisplayTool());- context.put("resource", new SolrVelocityResourceTool(- request.getCore().getSolrConfig().getResourceLoader().getClassLoader(),- request.getParams().get(LOCALE)));++ MathTool mathTool = new MathTool();+ mathTool.configure(toolConfig);+ context.put("math", mathTool);++ NumberTool numberTool = new NumberTool();+ numberTool.configure(toolConfig);+ context.put("number", numberTool);+++ DisplayTool displayTool = new DisplayTool();+ displayTool.configure(toolConfig);+ context.put("display", displayTool);++ ResourceTool resourceTool = new SolrVelocityResourceTool(request.getCore().getSolrConfig().getResourceLoader().getClassLoader());+ resourceTool.configure(toolConfig);+ context.put("resource", resourceTool);/*// Custom tools, specified in config as:@@ -213,12 +230,14 @@ public class VelocityResponseWriter implements QueryResponseWriter, SolrCoreAwar<str name="mytool">com.example.solr.velocity.MyTool</str></lst></queryResponseWriter>--*/// Custom tools can override any of the built-in tools provided above, by registering one with the same namefor(String name : customTools.keySet()) {- context.put(name, SolrCore.createInstance(customTools.get(name), Object.class, "VrW custom tool", request.getCore(), request.getCore().getResourceLoader()));+ Object customTool = SolrCore.createInstance(customTools.get(name), Object.class, "VrW custom tool: " + name, request.getCore(), request.getCore().getResourceLoader());+ if (customTool instanceof LocaleConfig) {+ ((LocaleConfig)customTool).configure(toolConfig);+ }+ context.put(name, customTool);}// custom tools _cannot_ override context objects added below, like $request and $response@@ -359,10 +378,8 @@ public class VelocityResponseWriter implements QueryResponseWriter, SolrCoreAwarprivate ClassLoader solrClassLoader;- public SolrVelocityResourceTool(ClassLoader cl, String localeString) {+ public SolrVelocityResourceTool(ClassLoader cl) {this.solrClassLoader = cl;- Locale l = toLocale(localeString);- this.setLocale(l == null ? Locale.ROOT : l);}@Overridediff --git a/solr/contrib/velocity/src/test/org/apache/solr/velocity/MockTool.java b/solr/contrib/velocity/src/test/org/apache/solr/velocity/MockTool.javaindex ebe475205d6..94d587938cd 100644--- a/solr/contrib/velocity/src/test/org/apache/solr/velocity/MockTool.java+++ b/solr/contrib/velocity/src/test/org/apache/solr/velocity/MockTool.java@@ -18,8 +18,9 @@ package org.apache.solr.velocity;*/import org.apache.solr.core.SolrCore;+import org.apache.velocity.tools.generic.LocaleConfig;-public class MockTool {+public class MockTool extends LocaleConfig {private final SolrCore core;public MockTool(SolrCore core) {diff --git a/solr/contrib/velocity/src/test/org/apache/solr/velocity/VelocityResponseWriterTest.java b/solr/contrib/velocity/src/test/org/apache/solr/velocity/VelocityResponseWriterTest.javaindex eefe61a630d..881fb998192 100644--- a/solr/contrib/velocity/src/test/org/apache/solr/velocity/VelocityResponseWriterTest.java+++ b/solr/contrib/velocity/src/test/org/apache/solr/velocity/VelocityResponseWriterTest.java@@ -121,7 +121,9 @@ public class VelocityResponseWriterTest extends SolrTestCaseJ4 {@Testpublic void testCustomTools() throws Exception {+ // custom_tool.vm responds with $!mytool.star("foo"), but $mytool is not defined (only in velocityWithCustomTools)assertEquals("", h.query(req("q","*:*", "wt","velocity",VelocityResponseWriter.TEMPLATE,"custom_tool")));+assertEquals("** LATERALUS **", h.query(req("q","*:*", "wt","velocityWithCustomTools",VelocityResponseWriter.TEMPLATE,"t",SolrParamResourceLoader.TEMPLATE_PARAM_PREFIX+"t", "$mytool.star(\"LATERALUS\")")));@@ -151,6 +153,16 @@ public class VelocityResponseWriterTest extends SolrTestCaseJ4 {// Test that $resource.get(key,baseName,locale) works with specified localeassertEquals("Colour", h.query(req("q","*:*", "wt","velocity",VelocityResponseWriter.TEMPLATE,"resource_get")));++ // Test that $number tool uses the specified locale+ assertEquals("2,112", h.query(req("q","*:*", "wt","velocityWithCustomTools",VelocityResponseWriter.TEMPLATE,"t",+ SolrParamResourceLoader.TEMPLATE_PARAM_PREFIX+"t","$number.format(2112)", VelocityResponseWriter.LOCALE, "en_US")));+ assertEquals("2.112", h.query(req("q","*:*", "wt","velocityWithCustomTools",VelocityResponseWriter.TEMPLATE,"t",+ SolrParamResourceLoader.TEMPLATE_PARAM_PREFIX+"t","$number.format(2112)", VelocityResponseWriter.LOCALE, "de_DE")));++ // Test that custom tool extending LocaleConfig gets the right locale+ assertEquals("de_DE", h.query(req("q","*:*", "wt","velocityWithCustomTools",VelocityResponseWriter.TEMPLATE,"t",+ SolrParamResourceLoader.TEMPLATE_PARAM_PREFIX+"t","$mytool.locale", VelocityResponseWriter.LOCALE, "de_DE")));}@Test