You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by eh...@apache.org on 2015/09/02 02:12:56 UTC
svn commit: r1700691 - in /lucene/dev/trunk/solr: ./
contrib/velocity/src/java/org/apache/solr/response/
contrib/velocity/src/test-files/velocity/solr/collection1/conf/
contrib/velocity/src/test-files/velocity/solr/collection1/conf/velocity/
contrib/ve...
Author: ehatcher
Date: Wed Sep 2 00:12:56 2015
New Revision: 1700691
URL: http://svn.apache.org/r1700691
Log:
SOLR-7915: Provide pluggable context tool support for VelocityResponseWriter
Added:
lucene/dev/trunk/solr/contrib/velocity/src/test-files/velocity/solr/collection1/conf/velocity/custom_tool.vm (with props)
lucene/dev/trunk/solr/contrib/velocity/src/test/org/apache/solr/velocity/MockTool.java (with props)
Modified:
lucene/dev/trunk/solr/CHANGES.txt
lucene/dev/trunk/solr/contrib/velocity/src/java/org/apache/solr/response/VelocityResponseWriter.java
lucene/dev/trunk/solr/contrib/velocity/src/test-files/velocity/solr/collection1/conf/solrconfig.xml
lucene/dev/trunk/solr/contrib/velocity/src/test/org/apache/solr/velocity/VelocityResponseWriterTest.java
Modified: lucene/dev/trunk/solr/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/CHANGES.txt?rev=1700691&r1=1700690&r2=1700691&view=diff
==============================================================================
--- lucene/dev/trunk/solr/CHANGES.txt (original)
+++ lucene/dev/trunk/solr/CHANGES.txt Wed Sep 2 00:12:56 2015
@@ -144,6 +144,8 @@ New Features
* SOLR-4316: Add a collections dropdown to angular admin UI (Upayavira, Shalin Shekhar Mangar)
+* SOLR-7915: Provide pluggable context tool support for VelocityResponseWriter (Erik Hatcher)
+
Bug Fixes
----------------------
Modified: lucene/dev/trunk/solr/contrib/velocity/src/java/org/apache/solr/response/VelocityResponseWriter.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/contrib/velocity/src/java/org/apache/solr/response/VelocityResponseWriter.java?rev=1700691&r1=1700690&r2=1700691&view=diff
==============================================================================
--- lucene/dev/trunk/solr/contrib/velocity/src/java/org/apache/solr/response/VelocityResponseWriter.java (original)
+++ lucene/dev/trunk/solr/contrib/velocity/src/java/org/apache/solr/response/VelocityResponseWriter.java Wed Sep 2 00:12:56 2015
@@ -25,7 +25,9 @@ import java.io.StringWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.Locale;
+import java.util.Map;
import java.util.Properties;
import java.util.ResourceBundle;
@@ -45,7 +47,6 @@ import org.apache.velocity.runtime.Runti
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
import org.apache.velocity.tools.ConversionUtils;
import org.apache.velocity.tools.generic.ComparisonDateTool;
-import org.apache.velocity.tools.generic.ContextTool;
import org.apache.velocity.tools.generic.DisplayTool;
import org.apache.velocity.tools.generic.EscapeTool;
import org.apache.velocity.tools.generic.ListTool;
@@ -84,6 +85,7 @@ public class VelocityResponseWriter impl
private static final Logger log = LoggerFactory.getLogger(VelocityResponseWriter.class);
private static final SolrVelocityLogger velocityLogger = new SolrVelocityLogger(log);
private Properties velocityInitProps = new Properties();
+ private Map<String,String> customTools = new HashMap<String,String>();
@Override
public void init(NamedList args) {
@@ -112,6 +114,14 @@ public class VelocityResponseWriter impl
solrResourceLoaderEnabled = (null == srle ? true : srle);
initPropertiesFileName = (String) args.get(PROPERTIES_FILE);
+
+ NamedList tools = (NamedList)args.get("tools");
+ if (tools != null) {
+ for(Object t : tools) {
+ Map.Entry tool = (Map.Entry)t;
+ customTools.put(tool.getKey().toString(), tool.getValue().toString());
+ }
+ }
}
@Override
@@ -183,10 +193,8 @@ public class VelocityResponseWriter impl
private VelocityContext createContext(SolrQueryRequest request, SolrQueryResponse response) {
VelocityContext context = new VelocityContext();
- context.put("request", request);
-
// Register useful Velocity "tools"
- context.put("log", log); // TODO: add test
+ 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());
@@ -198,6 +206,25 @@ public class VelocityResponseWriter impl
request.getCore().getSolrConfig().getResourceLoader().getClassLoader(),
request.getParams().get(LOCALE)));
+/*
+ // Custom tools, specified in config as:
+ <queryResponseWriter name="velocityWithCustomTools" class="solr.VelocityResponseWriter">
+ <lst name="tools">
+ <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()));
+ }
+
+ // custom tools _cannot_ override context objects added below, like $request and $response
+ // TODO: at least log a warning when one of the *fixed* tools classes in name with a custom one, currently silently ignored
+
+
// Turn the SolrQueryResponse into a SolrResponse.
// QueryResponse has lots of conveniences suitable for a view
// Problem is, which SolrResponse class to use?
@@ -221,6 +248,8 @@ public class VelocityResponseWriter impl
rsp = new SolrResponseBase();
rsp.setResponse(parsedResponse);
}
+
+ context.put("request", request);
context.put("response", rsp);
return context;
Modified: lucene/dev/trunk/solr/contrib/velocity/src/test-files/velocity/solr/collection1/conf/solrconfig.xml
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/contrib/velocity/src/test-files/velocity/solr/collection1/conf/solrconfig.xml?rev=1700691&r1=1700690&r2=1700691&view=diff
==============================================================================
--- lucene/dev/trunk/solr/contrib/velocity/src/test-files/velocity/solr/collection1/conf/solrconfig.xml (original)
+++ lucene/dev/trunk/solr/contrib/velocity/src/test-files/velocity/solr/collection1/conf/solrconfig.xml Wed Sep 2 00:12:56 2015
@@ -35,4 +35,20 @@
<queryResponseWriter name="velocityWithInitProps" class="solr.VelocityResponseWriter">
<str name="init.properties.file">velocity-init.properties</str>
</queryResponseWriter>
+
+ <queryResponseWriter name="velocityWithCustomTools" class="solr.VelocityResponseWriter">
+ <!-- Enable params resource loader to make tests leaner, no external template needed -->
+ <bool name="params.resource.loader.enabled">true</bool>
+
+ <lst name="tools">
+ <!-- how someone would typically add a custom tool, with a custom, non-clashing name -->
+ <str name="mytool">org.apache.solr.velocity.MockTool</str>
+
+ <!-- override the $log context object -->
+ <str name="log">org.apache.solr.velocity.MockTool</str>
+
+ <!-- Try to override response, but ignored -->
+ <str name="response">org.apache.solr.velocity.MockTool</str>
+ </lst>
+ </queryResponseWriter>
</config>
Added: lucene/dev/trunk/solr/contrib/velocity/src/test-files/velocity/solr/collection1/conf/velocity/custom_tool.vm
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/contrib/velocity/src/test-files/velocity/solr/collection1/conf/velocity/custom_tool.vm?rev=1700691&view=auto
==============================================================================
--- lucene/dev/trunk/solr/contrib/velocity/src/test-files/velocity/solr/collection1/conf/velocity/custom_tool.vm (added)
+++ lucene/dev/trunk/solr/contrib/velocity/src/test-files/velocity/solr/collection1/conf/velocity/custom_tool.vm Wed Sep 2 00:12:56 2015
@@ -0,0 +1 @@
+$!mytool.star("foo")
\ No newline at end of file
Added: lucene/dev/trunk/solr/contrib/velocity/src/test/org/apache/solr/velocity/MockTool.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/contrib/velocity/src/test/org/apache/solr/velocity/MockTool.java?rev=1700691&view=auto
==============================================================================
--- lucene/dev/trunk/solr/contrib/velocity/src/test/org/apache/solr/velocity/MockTool.java (added)
+++ lucene/dev/trunk/solr/contrib/velocity/src/test/org/apache/solr/velocity/MockTool.java Wed Sep 2 00:12:56 2015
@@ -0,0 +1,34 @@
+package org.apache.solr.velocity;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import org.apache.solr.core.SolrCore;
+
+public class MockTool {
+ private final SolrCore core;
+
+ public MockTool(SolrCore core) {
+ this.core = core;
+ }
+
+ public String star(String str) {
+ return "** " + str + " **";
+ }
+
+ public SolrCore getCore() { return core; }
+}
Modified: lucene/dev/trunk/solr/contrib/velocity/src/test/org/apache/solr/velocity/VelocityResponseWriterTest.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/contrib/velocity/src/test/org/apache/solr/velocity/VelocityResponseWriterTest.java?rev=1700691&r1=1700690&r2=1700691&view=diff
==============================================================================
--- lucene/dev/trunk/solr/contrib/velocity/src/test/org/apache/solr/velocity/VelocityResponseWriterTest.java (original)
+++ lucene/dev/trunk/solr/contrib/velocity/src/test/org/apache/solr/velocity/VelocityResponseWriterTest.java Wed Sep 2 00:12:56 2015
@@ -120,6 +120,29 @@ public class VelocityResponseWriterTest
}
@Test
+ public void testCustomTools() throws Exception {
+ 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\")")));
+
+ // Does $log get overridden?
+ assertEquals("** log overridden **", h.query(req("q","*:*", "wt","velocityWithCustomTools",VelocityResponseWriter.TEMPLATE,"t",
+ SolrParamResourceLoader.TEMPLATE_PARAM_PREFIX+"t", "$log.star(\"log overridden\")")));
+
+ // Does $response get overridden? actual blank response because of the bang on $! reference that silences bogus $-references
+ assertEquals("", h.query(req("q","*:*", "wt","velocityWithCustomTools",VelocityResponseWriter.TEMPLATE,"t",
+ SolrParamResourceLoader.TEMPLATE_PARAM_PREFIX+"t", "$!response.star(\"response overridden??\")")));
+
+ // Custom tools can also have a SolrCore-arg constructor because they are instantiated with SolrCore.createInstance
+ // TODO: do we really need to support this? no great loss, as a custom tool could take a SolrCore object as a parameter to
+ // TODO: any method, so one could do $mytool.my_method($request.core)
+ // I'm currently inclined to make this feature undocumented/unsupported, as we may want to instantiate classes
+ // in a different manner that only supports no-arg constructors, commented (passing) test case out
+// assertEquals("collection1", h.query(req("q","*:*", "wt","velocityWithCustomTools",VelocityResponseWriter.TEMPLATE,"t",
+// SolrParamResourceLoader.TEMPLATE_PARAM_PREFIX+"t", "$mytool.core.name")));
+ }
+
+ @Test
public void testLocaleFeature() throws Exception {
assertEquals("Color", h.query(req("q", "*:*", "wt", "velocity", VelocityResponseWriter.TEMPLATE, "locale",
VelocityResponseWriter.LOCALE,"en_US")));