SOLR-1723: polished the content-type capability

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1650831 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Erik Hatcher 2015-01-10 23:40:57 +00:00
parent 25e24d94d9
commit 4ed87ec42f
2 changed files with 33 additions and 3 deletions

View File

@ -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

View File

@ -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));
}
}