move VwR classes to response package

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@924412 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Erik Hatcher 2010-03-17 18:34:50 +00:00
parent a45422f66a
commit a5dc94a6c0
4 changed files with 40 additions and 39 deletions

View File

@ -15,8 +15,9 @@
* limitations under the License. * limitations under the License.
*/ */
package org.apache.solr.request; package org.apache.solr.response;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.response.SolrQueryResponse; import org.apache.solr.response.SolrQueryResponse;
import org.apache.solr.search.DocSlice; import org.apache.solr.search.DocSlice;
import org.apache.solr.common.SolrDocumentList; import org.apache.solr.common.SolrDocumentList;

View File

@ -15,8 +15,9 @@
* limitations under the License. * limitations under the License.
*/ */
package org.apache.solr.request; package org.apache.solr.response;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.velocity.runtime.resource.loader.ResourceLoader; import org.apache.velocity.runtime.resource.loader.ResourceLoader;
import org.apache.velocity.runtime.resource.Resource; import org.apache.velocity.runtime.resource.Resource;
import org.apache.velocity.exception.ResourceNotFoundException; import org.apache.velocity.exception.ResourceNotFoundException;

View File

@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.apache.solr.request; package org.apache.solr.response;
import org.apache.velocity.runtime.resource.loader.ResourceLoader; import org.apache.velocity.runtime.resource.loader.ResourceLoader;
import org.apache.velocity.runtime.resource.Resource; import org.apache.velocity.runtime.resource.Resource;

View File

@ -15,37 +15,32 @@
* limitations under the License. * limitations under the License.
*/ */
package org.apache.solr.request; package org.apache.solr.response;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.client.solrj.SolrResponse; import org.apache.solr.client.solrj.SolrResponse;
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse; import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.SolrResponseBase; import org.apache.solr.client.solrj.response.SolrResponseBase;
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer; import org.apache.solr.common.util.NamedList;
import org.apache.solr.response.QueryResponseWriter; import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.response.SolrQueryResponse;
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.*;
import org.apache.velocity.app.VelocityEngine; import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.tools.generic.*;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.File; import java.io.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Properties; import java.util.Properties;
public class VelocityResponseWriter implements QueryResponseWriter { public class VelocityResponseWriter implements QueryResponseWriter {
// TODO: maybe pass this Logger to the template for logging from there?
private static final Logger log = LoggerFactory.getLogger(VelocityResponseWriter.class); private static final Logger log = LoggerFactory.getLogger(VelocityResponseWriter.class);
public void write(Writer writer, SolrQueryRequest request, SolrQueryResponse response) throws IOException { public void write(Writer writer, SolrQueryRequest request, SolrQueryResponse response) throws IOException {
VelocityEngine engine = getEngine(request); // TODO: have HTTP headers available for configuring engine VelocityEngine engine = getEngine(request); // TODO: have HTTP headers available for configuring engine
// TODO: Add layout capability, render to string buffer, then render layout
Template template = getTemplate(engine, request); Template template = getTemplate(engine, request);
VelocityContext context = new VelocityContext(); VelocityContext context = new VelocityContext();
@ -67,10 +62,11 @@ public class VelocityResponseWriter implements QueryResponseWriter {
rsp.setResponse(parsedResponse); rsp.setResponse(parsedResponse);
// page only injected if QueryResponse works // page only injected if QueryResponse works
context.put("page",new PageTool(request,response)); // page tool only makes sense for a SearchHandler request... *sigh* context.put("page", new PageTool(request, response)); // page tool only makes sense for a SearchHandler request... *sigh*
} catch (ClassCastException e) { } catch (ClassCastException e) {
// known edge case where QueryResponse's extraction assumes "response" is a SolrDocumentList // known edge case where QueryResponse's extraction assumes "response" is a SolrDocumentList
// (AnalysisRequestHandler emits a "response") // (AnalysisRequestHandler emits a "response")
e.printStackTrace();
rsp = new SolrResponseBase(); rsp = new SolrResponseBase();
rsp.setResponse(parsedResponse); rsp.setResponse(parsedResponse);
} }
@ -78,16 +74,17 @@ public class VelocityResponseWriter implements QueryResponseWriter {
// Velocity context tools - TODO: make these pluggable // Velocity context tools - TODO: make these pluggable
context.put("esc", new EscapeTool()); context.put("esc", new EscapeTool());
context.put("sort", new SortTool());
context.put("number", new NumberTool());
context.put("list", new ListTool());
context.put("date", new ComparisonDateTool()); context.put("date", new ComparisonDateTool());
context.put("list", new ListTool());
context.put("math", new MathTool()); context.put("math", new MathTool());
context.put("number", new NumberTool());
context.put("sort", new SortTool());
context.put("engine", engine); // for $engine.resourceExists(...) 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);
// create output, optionally wrap it into a json object // create output, optionally wrap it into a json object
if (wrap_response) { if (wrap_response) {
@ -126,28 +123,30 @@ public class VelocityResponseWriter implements QueryResponseWriter {
engine.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, baseDir.getAbsolutePath()); engine.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, baseDir.getAbsolutePath());
engine.setProperty("params.resource.loader.instance", new SolrParamResourceLoader(request)); engine.setProperty("params.resource.loader.instance", new SolrParamResourceLoader(request));
SolrVelocityResourceLoader resourceLoader = SolrVelocityResourceLoader resourceLoader =
new SolrVelocityResourceLoader(request.getCore().getSolrConfig().getResourceLoader()); new SolrVelocityResourceLoader(request.getCore().getSolrConfig().getResourceLoader());
engine.setProperty("solr.resource.loader.instance", resourceLoader); engine.setProperty("solr.resource.loader.instance", resourceLoader);
// TODO: Externalize Velocity properties
engine.setProperty(VelocityEngine.RESOURCE_LOADER, "params,file,solr"); engine.setProperty(VelocityEngine.RESOURCE_LOADER, "params,file,solr");
String propFile = request.getParams().get("v.properties"); String propFile = request.getParams().get("v.properties");
try{ try {
if( propFile == null ) if (propFile == null)
engine.init(); engine.init();
else{ else {
InputStream is = null; InputStream is = null;
try{ try {
is = resourceLoader.getResourceStream( propFile ); is = resourceLoader.getResourceStream(propFile);
Properties props = new Properties(); Properties props = new Properties();
props.load( is ); props.load(is);
engine.init( props ); engine.init(props);
} }
finally{ finally {
if( is != null ) is.close(); if (is != null) is.close();
} }
} }
} }
catch( Exception e ){ catch (Exception e) {
throw new RuntimeException( e ); throw new RuntimeException(e);
} }
return engine; return engine;
@ -176,7 +175,7 @@ public class VelocityResponseWriter implements QueryResponseWriter {
} }
public String getContentType(SolrQueryRequest request, SolrQueryResponse response) { public String getContentType(SolrQueryRequest request, SolrQueryResponse response) {
return request.getParams().get("v.contentType","text/html"); return request.getParams().get("v.contentType", "text/html");
} }
private String getJSONWrap(String xmlResult) { // TODO: maybe noggit or Solr's JSON utilities can make this cleaner? private String getJSONWrap(String xmlResult) { // TODO: maybe noggit or Solr's JSON utilities can make this cleaner?