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:
Erik Hatcher 2015-10-13 14:32:59 +00:00
parent 14854b4f08
commit 33850419bd
4 changed files with 46 additions and 13 deletions

View File

@ -264,6 +264,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
----------------------

View File

@ -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, SolrCoreAwar
VelocityContext 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 name
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
@ -359,10 +378,8 @@ public class VelocityResponseWriter implements QueryResponseWriter, SolrCoreAwar
private 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);
}
@Override

View File

@ -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) {

View File

@ -121,7 +121,9 @@ public class VelocityResponseWriterTest extends SolrTestCaseJ4 {
@Test
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("** 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 locale
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