lucene-solr-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ehatc...@apache.org
Subject svn commit: r718553 - in /lucene/solr/trunk/contrib/velocity/src/main: java/org/apache/solr/request/VelocityResponseWriter.java solr/conf/solrconfig.xml
Date Tue, 18 Nov 2008 11:00:32 GMT
Author: ehatcher
Date: Tue Nov 18 03:00:31 2008
New Revision: 718553

URL: http://svn.apache.org/viewvc?rev=718553&view=rev
Log:
Partial acceptance of SolrJS related patch - prefix all parameters with v., added in JSON
wrapping, but did not include the rawResponse bit yet as it isn't needed by any current example
and is planned to be reworked to use SolrJs API more natively

Modified:
    lucene/solr/trunk/contrib/velocity/src/main/java/org/apache/solr/request/VelocityResponseWriter.java
    lucene/solr/trunk/contrib/velocity/src/main/solr/conf/solrconfig.xml

Modified: lucene/solr/trunk/contrib/velocity/src/main/java/org/apache/solr/request/VelocityResponseWriter.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/contrib/velocity/src/main/java/org/apache/solr/request/VelocityResponseWriter.java?rev=718553&r1=718552&r2=718553&view=diff
==============================================================================
--- lucene/solr/trunk/contrib/velocity/src/main/java/org/apache/solr/request/VelocityResponseWriter.java
(original)
+++ lucene/solr/trunk/contrib/velocity/src/main/java/org/apache/solr/request/VelocityResponseWriter.java
Tue Nov 18 03:00:31 2008
@@ -25,6 +25,7 @@
 
 import java.io.File;
 import java.io.IOException;
+import java.io.StringWriter;
 import java.io.Writer;
 
 public class VelocityResponseWriter implements QueryResponseWriter {
@@ -36,11 +37,22 @@
 
     VelocityContext context = new VelocityContext();
 
-    context.put("request", request);
-    context.put("response", response);
+    // TODO: Make this use the SolrJ API, rather than "embedded" Solr API
+    context.put("request", request);    // TODO: inject a SolrRequest instead of a SolrQueryRequest
+    context.put("response", response);  // TODO: inject a SolrResponse instead of a SolrQueryResponse
     context.put("page",new PageTool(request,response));
     context.put("esc", new EscapeTool());
-    template.merge(context, writer);
+   
+    // create output, optionally wrap it into a json object
+    if (request.getParams().getBool("v.json", false)) {
+      StringWriter stringWriter = new StringWriter();
+      template.merge(context, stringWriter);
+      writer.write(request.getParams().get("v.json") + "(");
+      writer.write(getJSONWrap(stringWriter.toString()));
+      writer.write(')');
+    } else {
+      template.merge(context, writer);
+    }
   }
 
   private VelocityEngine getEngine(SolrQueryRequest request) {
@@ -62,7 +74,7 @@
   private Template getTemplate(VelocityEngine engine, SolrQueryRequest request) throws IOException
{
     Template template;
     try {
-      template = engine.getTemplate(request.getParams().get("template", "browse") + ".vm");
+      template = engine.getTemplate(request.getParams().get("v.template", "browse") + ".vm");
     } catch (Exception e) {
       throw new IOException(e.getMessage());
     }
@@ -71,7 +83,17 @@
   }
 
   public String getContentType(SolrQueryRequest request, SolrQueryResponse response) {
-    return request.getParams().get("contentType","text/html");
+    return request.getParams().get("v.contentType","text/html");
+  }
+  
+  private String getJSONWrap(String xmlResult) {
+    // escape the double quotes and backslashes
+    String replace1 = xmlResult.replaceAll("\\\\", "\\\\\\\\");
+    replace1 = replace1.replaceAll("\\n", "\\\\n");
+    replace1 = replace1.replaceAll("\\r", "\\\\r");
+    String replaced = replace1.replaceAll("\"", "\\\\\"");
+    // wrap it in a JSON object
+    return "{\"result\":\"" + replaced + "\"}";
   }
 
   public void init(NamedList args) {

Modified: lucene/solr/trunk/contrib/velocity/src/main/solr/conf/solrconfig.xml
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/contrib/velocity/src/main/solr/conf/solrconfig.xml?rev=718553&r1=718552&r2=718553&view=diff
==============================================================================
--- lucene/solr/trunk/contrib/velocity/src/main/solr/conf/solrconfig.xml (original)
+++ lucene/solr/trunk/contrib/velocity/src/main/solr/conf/solrconfig.xml Tue Nov 18 03:00:31
2008
@@ -400,7 +400,7 @@
 
   <requestHandler name="/itas" class="solr.SearchHandler">
      <lst name="defaults">
-       <str name="template">browse</str>
+       <str name="v.template">browse</str>
        <str name="title">Solritas</str>
 
        <str name="wt">velocity</str>



Mime
View raw message