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/01/11 00:42:03 UTC

svn commit: r1650832 - in /lucene/dev/branches/branch_5x: ./ solr/ solr/contrib/ solr/contrib/velocity/src/java/org/apache/solr/response/VelocityResponseWriter.java solr/contrib/velocity/src/test/org/apache/solr/velocity/VelocityResponseWriterTest.java

Author: ehatcher
Date: Sat Jan 10 23:42:02 2015
New Revision: 1650832

URL: http://svn.apache.org/r1650832
Log:
SOLR-1723: polished the content-type capability (merged from trunk r1650831)

Modified:
    lucene/dev/branches/branch_5x/   (props changed)
    lucene/dev/branches/branch_5x/solr/   (props changed)
    lucene/dev/branches/branch_5x/solr/contrib/   (props changed)
    lucene/dev/branches/branch_5x/solr/contrib/velocity/src/java/org/apache/solr/response/VelocityResponseWriter.java
    lucene/dev/branches/branch_5x/solr/contrib/velocity/src/test/org/apache/solr/velocity/VelocityResponseWriterTest.java

Modified: lucene/dev/branches/branch_5x/solr/contrib/velocity/src/java/org/apache/solr/response/VelocityResponseWriter.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/solr/contrib/velocity/src/java/org/apache/solr/response/VelocityResponseWriter.java?rev=1650832&r1=1650831&r2=1650832&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/solr/contrib/velocity/src/java/org/apache/solr/response/VelocityResponseWriter.java (original)
+++ lucene/dev/branches/branch_5x/solr/contrib/velocity/src/java/org/apache/solr/response/VelocityResponseWriter.java Sat Jan 10 23:42:02 2015
@@ -73,6 +73,7 @@ public class VelocityResponseWriter impl
 
   public static final String TEMPLATE_EXTENSION = ".vm";
   public static final String DEFAULT_CONTENT_TYPE = "text/html;charset=UTF-8";
+  public static final String JSON_CONTENT_TYPE = "application/json;charset=UTF-8";
 
   private File fileResourceLoaderBaseDir;
   private boolean paramsResourceLoaderEnabled;
@@ -127,7 +128,10 @@ public class VelocityResponseWriter impl
 
   @Override
   public String getContentType(SolrQueryRequest request, SolrQueryResponse response) {
-    return request.getParams().get(CONTENT_TYPE, DEFAULT_CONTENT_TYPE);
+    String contentType = request.getParams().get(CONTENT_TYPE);
+
+    // Use the v.contentType specified, or either of the default content types depending on the presence of v.json
+    return (contentType != null) ? contentType : ((request.getParams().get(JSON) == null) ? DEFAULT_CONTENT_TYPE : JSON_CONTENT_TYPE);
   }
 
   @Override

Modified: lucene/dev/branches/branch_5x/solr/contrib/velocity/src/test/org/apache/solr/velocity/VelocityResponseWriterTest.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/solr/contrib/velocity/src/test/org/apache/solr/velocity/VelocityResponseWriterTest.java?rev=1650832&r1=1650831&r2=1650832&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/solr/contrib/velocity/src/test/org/apache/solr/velocity/VelocityResponseWriterTest.java (original)
+++ lucene/dev/branches/branch_5x/solr/contrib/velocity/src/test/org/apache/solr/velocity/VelocityResponseWriterTest.java Sat Jan 10 23:42:02 2015
@@ -60,7 +60,7 @@ public class VelocityResponseWriterTest
 
   @Test
   public void testParamResourceLoaderDisabled() throws Exception {
-    org.apache.solr.response.VelocityResponseWriter vrw = new VelocityResponseWriter();
+    VelocityResponseWriter vrw = new VelocityResponseWriter();
     // by default param resource loader is disabled, no need to set it here
     SolrQueryRequest req = req(VelocityResponseWriter.TEMPLATE,"custom",
         SolrParamResourceLoader.TEMPLATE_PARAM_PREFIX+"custom","$response.response.response_data");
@@ -76,7 +76,7 @@ public class VelocityResponseWriterTest
 
   @Test
   public void testFileResourceLoader() throws Exception {
-    org.apache.solr.response.VelocityResponseWriter vrw = new VelocityResponseWriter();
+    VelocityResponseWriter vrw = new VelocityResponseWriter();
     NamedList<String> nl = new NamedList<String>();
     nl.add("template.base.dir", getFile("velocity").getAbsolutePath());
     vrw.init(nl);
@@ -147,4 +147,30 @@ public class VelocityResponseWriterTest
         VelocityResponseWriter.LAYOUT,"layout")));
   }
 
+  @Test
+  public void testContentType() throws Exception {
+    VelocityResponseWriter vrw = new VelocityResponseWriter();
+    NamedList<String> nl = new NamedList<String>();
+    vrw.init(nl);
+    SolrQueryResponse rsp = new SolrQueryResponse();
+
+    // with v.json=wrf, content type should default to application/json
+    assertEquals("application/json;charset=UTF-8",
+        vrw.getContentType(req(VelocityResponseWriter.TEMPLATE, "numFound",
+            VelocityResponseWriter.JSON, "wrf"), rsp));
+
+    // with no v.json specified, the default text/html should be returned
+    assertEquals("text/html;charset=UTF-8",
+        vrw.getContentType(req(VelocityResponseWriter.TEMPLATE, "numFound"), rsp));
+
+    // if v.contentType is specified, that should be used, even if v.json is specified
+    assertEquals("text/plain",
+        vrw.getContentType(req(VelocityResponseWriter.TEMPLATE, "numFound",
+            VelocityResponseWriter.CONTENT_TYPE,"text/plain"), rsp));
+    assertEquals("text/plain",
+        vrw.getContentType(req(VelocityResponseWriter.TEMPLATE, "numFound",
+            VelocityResponseWriter.JSON,"wrf",
+            VelocityResponseWriter.CONTENT_TYPE,"text/plain"), rsp));
+  }
+
 }