SOLR-3648: Fix Velocity template loading in SolrCloud mode

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1366775 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Erik Hatcher 2012-07-28 22:20:38 +00:00
parent 0ebfb6a2f7
commit eef6a6bec2
4 changed files with 32 additions and 9 deletions

View File

@ -153,6 +153,10 @@ Bug Fixes
when 'gap' is zero -- or effectively zero due to floating point arithmetic
underflow. (hossman)
* SOLR-3648: Fixed VelocityResponseWriter template loading in SolrCloud mode.
For the example configuration, this means /browse now works with SolrCloud.
(janhoy, ehatcher)
Other Changes
----------------------

View File

@ -25,7 +25,9 @@ import org.apache.solr.core.SolrResourceLoader;
import java.io.IOException;
import java.io.InputStream;
// TODO: the name of this class seems ridiculous
/**
* Velocity resource loader wrapper around Solr resource loader
*/
public class SolrVelocityResourceLoader extends ResourceLoader {
private SolrResourceLoader loader;
@ -41,7 +43,7 @@ public class SolrVelocityResourceLoader extends ResourceLoader {
@Override
public InputStream getResourceStream(String template_name) throws ResourceNotFoundException {
try {
return loader.openResource(template_name);
return loader.openResource("velocity/" + template_name);
} catch (IOException ioe) {
throw new ResourceNotFoundException(ioe);
}

View File

@ -20,6 +20,7 @@ package org.apache.solr.response;
import org.apache.solr.client.solrj.SolrResponse;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.SolrResponseBase;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.velocity.Template;
@ -113,19 +114,32 @@ public class VelocityResponseWriter implements QueryResponseWriter {
private VelocityEngine getEngine(SolrQueryRequest request) {
VelocityEngine engine = new VelocityEngine();
String template_root = request.getParams().get("v.base_dir");
File baseDir = new File(request.getCore().getResourceLoader().getConfigDir(), "velocity");
if (template_root != null) {
baseDir = new File(template_root);
}
engine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, baseDir.getAbsolutePath());
engine.setProperty("params.resource.loader.instance", new SolrParamResourceLoader(request));
SolrVelocityResourceLoader resourceLoader =
new SolrVelocityResourceLoader(request.getCore().getSolrConfig().getResourceLoader());
engine.setProperty("solr.resource.loader.instance", resourceLoader);
File fileResourceLoaderBaseDir = null;
try {
String template_root = request.getParams().get("v.base_dir");
fileResourceLoaderBaseDir = new File(request.getCore().getResourceLoader().getConfigDir(), "velocity");
if (template_root != null) {
fileResourceLoaderBaseDir = new File(template_root);
}
} catch (SolrException e) {
// no worries... probably in ZooKeeper mode and getConfigDir() isn't available, so we'll just ignore omit
// the file system resource loader
}
if (fileResourceLoaderBaseDir != null) {
engine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, fileResourceLoaderBaseDir.getAbsolutePath());
engine.setProperty(RuntimeConstants.RESOURCE_LOADER, "params,file,solr");
} else {
engine.setProperty(RuntimeConstants.RESOURCE_LOADER, "params,solr");
}
// TODO: Externalize Velocity properties
engine.setProperty(RuntimeConstants.RESOURCE_LOADER, "params,file,solr");
String propFile = request.getParams().get("v.properties");
try {
if (propFile == null)

View File

@ -144,6 +144,9 @@ public class ShowFileRequestHandler extends RequestHandlerBase
if (fname.indexOf("..") >= 0) {
throw new SolrException(ErrorCode.FORBIDDEN, "Invalid path: " + fname);
}
if (fname.startsWith("/")) { // Only files relative to conf are valid
fname = fname.substring(1);
}
adminFile = confPath + "/" + fname;
}