mirror of https://github.com/apache/lucene.git
SOLR-8128: Set v.locale specified locale for all LocaleConfig extending VelocityResponseWriter tools
git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1708406 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
14854b4f08
commit
33850419bd
|
@ -263,6 +263,9 @@ Bug Fixes
|
||||||
|
|
||||||
* SOLR-8130: Solr's hdfs safe mode detection does not catch all cases of being in safe mode.
|
* SOLR-8130: Solr's hdfs safe mode detection does not catch all cases of being in safe mode.
|
||||||
(Mark Miller, Mike Drob)
|
(Mark Miller, Mike Drob)
|
||||||
|
|
||||||
|
* SOLR-8128: Set v.locale specified locale for all LocaleConfig extending VelocityResponseWriter tools.
|
||||||
|
(Erik Hatcher)
|
||||||
|
|
||||||
Optimizations
|
Optimizations
|
||||||
----------------------
|
----------------------
|
||||||
|
|
|
@ -50,6 +50,7 @@ import org.apache.velocity.tools.generic.ComparisonDateTool;
|
||||||
import org.apache.velocity.tools.generic.DisplayTool;
|
import org.apache.velocity.tools.generic.DisplayTool;
|
||||||
import org.apache.velocity.tools.generic.EscapeTool;
|
import org.apache.velocity.tools.generic.EscapeTool;
|
||||||
import org.apache.velocity.tools.generic.ListTool;
|
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.MathTool;
|
||||||
import org.apache.velocity.tools.generic.NumberTool;
|
import org.apache.velocity.tools.generic.NumberTool;
|
||||||
import org.apache.velocity.tools.generic.ResourceTool;
|
import org.apache.velocity.tools.generic.ResourceTool;
|
||||||
|
@ -194,17 +195,33 @@ public class VelocityResponseWriter implements QueryResponseWriter, SolrCoreAwar
|
||||||
VelocityContext context = new VelocityContext();
|
VelocityContext context = new VelocityContext();
|
||||||
|
|
||||||
// Register useful Velocity "tools"
|
// 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("log", log); // TODO: add test; TODO: should this be overridable with a custom "log" named tool?
|
||||||
context.put("esc", new EscapeTool());
|
context.put("esc", new EscapeTool());
|
||||||
context.put("date", new ComparisonDateTool());
|
context.put("date", new ComparisonDateTool());
|
||||||
context.put("list", new ListTool());
|
context.put("list", new ListTool());
|
||||||
context.put("math", new MathTool());
|
|
||||||
context.put("number", new NumberTool());
|
|
||||||
context.put("sort", new SortTool());
|
context.put("sort", new SortTool());
|
||||||
context.put("display", new DisplayTool());
|
|
||||||
context.put("resource", new SolrVelocityResourceTool(
|
MathTool mathTool = new MathTool();
|
||||||
request.getCore().getSolrConfig().getResourceLoader().getClassLoader(),
|
mathTool.configure(toolConfig);
|
||||||
request.getParams().get(LOCALE)));
|
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:
|
// 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>
|
<str name="mytool">com.example.solr.velocity.MyTool</str>
|
||||||
</lst>
|
</lst>
|
||||||
</queryResponseWriter>
|
</queryResponseWriter>
|
||||||
|
|
||||||
|
|
||||||
*/
|
*/
|
||||||
// Custom tools can override any of the built-in tools provided above, by registering one with the same name
|
// Custom tools can override any of the built-in tools provided above, by registering one with the same name
|
||||||
for(String name : customTools.keySet()) {
|
for(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
|
// custom tools _cannot_ override context objects added below, like $request and $response
|
||||||
|
@ -359,10 +378,8 @@ public class VelocityResponseWriter implements QueryResponseWriter, SolrCoreAwar
|
||||||
|
|
||||||
private ClassLoader solrClassLoader;
|
private ClassLoader solrClassLoader;
|
||||||
|
|
||||||
public SolrVelocityResourceTool(ClassLoader cl, String localeString) {
|
public SolrVelocityResourceTool(ClassLoader cl) {
|
||||||
this.solrClassLoader = cl;
|
this.solrClassLoader = cl;
|
||||||
Locale l = toLocale(localeString);
|
|
||||||
this.setLocale(l == null ? Locale.ROOT : l);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -18,8 +18,9 @@ package org.apache.solr.velocity;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import org.apache.solr.core.SolrCore;
|
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;
|
private final SolrCore core;
|
||||||
|
|
||||||
public MockTool(SolrCore core) {
|
public MockTool(SolrCore core) {
|
||||||
|
|
|
@ -121,7 +121,9 @@ public class VelocityResponseWriterTest extends SolrTestCaseJ4 {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCustomTools() throws Exception {
|
public 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("", h.query(req("q","*:*", "wt","velocity",VelocityResponseWriter.TEMPLATE,"custom_tool")));
|
||||||
|
|
||||||
assertEquals("** LATERALUS **", h.query(req("q","*:*", "wt","velocityWithCustomTools",VelocityResponseWriter.TEMPLATE,"t",
|
assertEquals("** LATERALUS **", h.query(req("q","*:*", "wt","velocityWithCustomTools",VelocityResponseWriter.TEMPLATE,"t",
|
||||||
SolrParamResourceLoader.TEMPLATE_PARAM_PREFIX+"t", "$mytool.star(\"LATERALUS\")")));
|
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 locale
|
// Test that $resource.get(key,baseName,locale) works with specified locale
|
||||||
assertEquals("Colour", h.query(req("q","*:*", "wt","velocity",VelocityResponseWriter.TEMPLATE,"resource_get")));
|
assertEquals("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
|
@Test
|
||||||
|
|
Loading…
Reference in New Issue