Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

SOLR-1723: polished the content-type capability

Erik Hatcher4ed87ec42f36
diff --git a/solr/contrib/velocity/src/java/org/apache/solr/response/VelocityResponseWriter.java b/solr/contrib/velocity/src/java/org/apache/solr/response/VelocityResponseWriter.java
index 9493ad34e20..857199cb89d 100644
--- a/solr/contrib/velocity/src/java/org/apache/solr/response/VelocityResponseWriter.java
+++ b/solr/contrib/velocity/src/java/org/apache/solr/response/VelocityResponseWriter.java
@@ -73,6 +73,7 @@ public class VelocityResponseWriter implements QueryResponseWriter, SolrCoreAwar
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 implements QueryResponseWriter, SolrCoreAwar
@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
diff --git a/solr/contrib/velocity/src/test/org/apache/solr/velocity/VelocityResponseWriterTest.java b/solr/contrib/velocity/src/test/org/apache/solr/velocity/VelocityResponseWriterTest.java
index c56c1600e7c..0d2c3d1143c 100644
--- a/solr/contrib/velocity/src/test/org/apache/solr/velocity/VelocityResponseWriterTest.java
+++ b/solr/contrib/velocity/src/test/org/apache/solr/velocity/VelocityResponseWriterTest.java
@@ -60,7 +60,7 @@ public class VelocityResponseWriterTest extends SolrTestCaseJ4 {
@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 extends SolrTestCaseJ4 {
@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 extends SolrTestCaseJ4 {
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));
+ }
+
}