Add Velocity engine and list tool objects, and default template based on path if not explicitly specified

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@747761 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Erik Hatcher 2009-02-25 12:10:03 +00:00
parent aea2aff871
commit 336a226ce7
1 changed files with 16 additions and 8 deletions

View File

@ -24,12 +24,7 @@ import org.apache.solr.client.solrj.response.SolrResponseBase;
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer; import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
import org.apache.velocity.Template; import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext; import org.apache.velocity.VelocityContext;
import org.apache.velocity.tools.generic.ComparisonDateTool; import org.apache.velocity.tools.generic.*;
import org.apache.velocity.tools.generic.DateTool;
import org.apache.velocity.tools.generic.EscapeTool;
import org.apache.velocity.tools.generic.MathTool;
import org.apache.velocity.tools.generic.NumberTool;
import org.apache.velocity.tools.generic.SortTool;
import org.apache.velocity.app.VelocityEngine; import org.apache.velocity.app.VelocityEngine;
import java.io.File; import java.io.File;
@ -76,9 +71,11 @@ public class VelocityResponseWriter implements QueryResponseWriter {
context.put("esc", new EscapeTool()); context.put("esc", new EscapeTool());
context.put("sort", new SortTool()); context.put("sort", new SortTool());
context.put("number", new NumberTool()); context.put("number", new NumberTool());
context.put("list", new ListTool());
context.put("date", new ComparisonDateTool()); context.put("date", new ComparisonDateTool());
context.put("math", new MathTool()); context.put("math", new MathTool());
context.put("engine", engine); // for $engine.resourceExists(...)
String layout_template = request.getParams().get("v.layout"); String layout_template = request.getParams().get("v.layout");
String json_wrapper = request.getParams().get("v.json"); String json_wrapper = request.getParams().get("v.json");
boolean wrap_response = (layout_template != null) || (json_wrapper !=null); boolean wrap_response = (layout_template != null) || (json_wrapper !=null);
@ -128,8 +125,19 @@ public class VelocityResponseWriter implements QueryResponseWriter {
private Template getTemplate(VelocityEngine engine, SolrQueryRequest request) throws IOException { private Template getTemplate(VelocityEngine engine, SolrQueryRequest request) throws IOException {
Template template; Template template;
String template_name = request.getParams().get("v.template");
String qt = request.getParams().get("qt");
String path = (String) request.getContext().get("path");
if (template_name == null && path != null) {
template_name = path;
} // TODO: path is never null, so qt won't get picked up maybe special case for '/select' to use qt, otherwise use path?
if (template_name == null && qt != null) {
template_name = qt;
}
if (template_name == null) template_name = "index";
try { try {
template = engine.getTemplate(request.getParams().get("v.template", "browse") + ".vm"); template = engine.getTemplate(template_name + ".vm");
} catch (Exception e) { } catch (Exception e) {
throw new IOException(e.getMessage()); throw new IOException(e.getMessage());
} }