diff --git a/CHANGES.txt b/CHANGES.txt index d0c2ef24e5c..d0a71f4ef8e 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -12,8 +12,9 @@ New Features 6. The default search field may now be overridden by requests to the standard request handler using the df query parameter. (Erik Hatcher) 7. Added DisMaxRequestHandler and SolrPluginUtils. (Chris Hostetter) - - + 8. Support for customizing the QueryResponseWriter per request + (Mike Baranczak / SOLR-16 / hossman) + Changes in runtime behavior 1. classes reorganized into different packages, package names changed to Apache 2. force read of document stored fields in QuerySenderListener diff --git a/example/solr/conf/solrconfig.xml b/example/solr/conf/solrconfig.xml index 3744943bb2a..9558660757d 100755 --- a/example/solr/conf/solrconfig.xml +++ b/example/solr/conf/solrconfig.xml @@ -218,6 +218,17 @@ + + + solr diff --git a/src/java/org/apache/solr/core/SolrCore.java b/src/java/org/apache/solr/core/SolrCore.java index 9f5b923f10b..f1d76405e24 100644 --- a/src/java/org/apache/solr/core/SolrCore.java +++ b/src/java/org/apache/solr/core/SolrCore.java @@ -16,13 +16,17 @@ package org.apache.solr.core; +import java.util.Map; +import java.util.TreeMap; import org.apache.lucene.index.IndexReader; import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; +import org.apache.solr.request.QueryResponseWriter; import org.apache.solr.request.SolrRequestHandler; import org.apache.solr.request.SolrQueryRequest; import org.apache.solr.request.SolrQueryResponse; +import org.apache.solr.request.XMLResponseWriter; import org.apache.solr.schema.IndexSchema; import org.apache.solr.search.SolrIndexSearcher; import org.apache.solr.update.*; @@ -30,6 +34,7 @@ import org.apache.solr.util.DOMUtil; import org.apache.solr.util.RefCounted; import org.apache.solr.util.StrUtils; import org.apache.solr.util.XML; +import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xmlpull.v1.XmlPullParser; @@ -52,6 +57,7 @@ import java.util.logging.Logger; /** * @author yonik + * @author Mike Baranczak * @version $Id$ */ @@ -196,6 +202,8 @@ public final class SolrCore { parseListeners(); initIndex(); + + initWriters(); try { // Open the searcher *before* the handler so we don't end up opening @@ -937,6 +945,61 @@ public final class SolrCore { log.severe("Error writing to putput stream: "+ee); } } + + + + private QueryResponseWriter defaultResponseWriter; + private Map responseWriters + = new TreeMap(); + + /** Configure the query response writers. There will always be a default writer; additional + * writers may also be configured. */ + private void initWriters() { + String xpath = "queryResponseWriter"; + NodeList nodes = (NodeList) SolrConfig.config.evaluate(xpath, XPathConstants.NODESET); + int length = nodes.getLength(); + for (int i=0; i Mike Baranczak + */ +public class OutputWriterTest extends AbstractSolrTestCase { + + /** The XML string that's output for testing purposes. */ + public static final String USELESS_OUTPUT = "useless output"; + + + public String getSchemaFile() { return "solr/crazy-path-to-schema.xml"; } + + public String getSolrConfigFile() { return "solr/crazy-path-to-config.xml"; } + + + public void testOriginalSolrWriter() { + lrf.args.put("wt", "standard"); + assertQ(req("foo"), "//response/responseHeader/status"); + + lrf.args.remove("wt"); + assertQ(req("foo"), "//response/responseHeader/status"); + } + + public void testUselessWriter() throws Exception { + lrf.args.put("wt", "useless"); + String out = h.query(req("foo")); + assertEquals(USELESS_OUTPUT, out); + } + + + //////////////////////////////////////////////////////////////////////////// + /** An output writer that doesn't do anything useful. */ + + public static class UselessOutputWriter implements QueryResponseWriter { + + public UselessOutputWriter() {} + + public void write(Writer writer, SolrQueryRequest request, SolrQueryResponse response) + throws IOException { + + writer.write(USELESS_OUTPUT); + } + + } + +} diff --git a/src/test/test-files/solr/crazy-path-to-config.xml b/src/test/test-files/solr/crazy-path-to-config.xml index 7c2eb04575d..4b3d0362ba0 100644 --- a/src/test/test-files/solr/crazy-path-to-config.xml +++ b/src/test/test-files/solr/crazy-path-to-config.xml @@ -40,8 +40,13 @@ + + + + + solr diff --git a/src/webapp/src/org/apache/solr/servlet/SolrServlet.java b/src/webapp/src/org/apache/solr/servlet/SolrServlet.java index 08eb9c2f2d4..e25f0e89ce6 100644 --- a/src/webapp/src/org/apache/solr/servlet/SolrServlet.java +++ b/src/webapp/src/org/apache/solr/servlet/SolrServlet.java @@ -20,7 +20,7 @@ import org.apache.solr.core.Config; import org.apache.solr.core.SolrCore; import org.apache.solr.core.SolrException; import org.apache.solr.request.SolrQueryResponse; -import org.apache.solr.request.XMLResponseWriter; +import org.apache.solr.request.QueryResponseWriter; import javax.naming.Context; import javax.naming.InitialContext; @@ -36,17 +36,17 @@ import java.util.logging.Logger; /** * @author yonik + * @author Mike Baranczak */ public class SolrServlet extends HttpServlet { + final Logger log = Logger.getLogger(SolrServlet.class.getName()); SolrCore core; private static String CONTENT_TYPE="text/xml;charset=UTF-8"; - - XMLResponseWriter xmlResponseWriter; - - public void init() throws ServletException - { + + + public void init() throws ServletException { log.info("SolrServlet.init()"); try { Context c = new InitialContext(); @@ -71,7 +71,7 @@ public class SolrServlet extends HttpServlet { log.info("user.dir=" + System.getProperty("user.dir")); core = SolrCore.getSolrCore(); - xmlResponseWriter=new XMLResponseWriter(); + log.info("SolrServlet.init() done"); } @@ -94,14 +94,15 @@ public class SolrServlet extends HttpServlet { core.execute(solrReq, solrRsp); if (solrRsp.getException() == null) { response.setContentType(CONTENT_TYPE); - PrintWriter writer = response.getWriter(); - // if (solrReq.getStrParam("version","2").charAt(0) == '1') - xmlResponseWriter.write(writer, solrReq, solrRsp); + PrintWriter out = response.getWriter(); + + QueryResponseWriter responseWriter = core.getQueryResponseWriter(solrReq); + responseWriter.write(out, solrReq, solrRsp); } else { Exception e = solrRsp.getException(); int rc=500; if (e instanceof SolrException) { - rc=((SolrException)e).code(); + rc=((SolrException)e).code(); } sendErr(rc, SolrException.toStr(e), request, response); } @@ -115,7 +116,6 @@ public class SolrServlet extends HttpServlet { // This releases the IndexReader associated with the request solrReq.close(); } - } final void sendErr(int rc, String msg, HttpServletRequest request, HttpServletResponse response) { @@ -142,4 +142,4 @@ public class SolrServlet extends HttpServlet { return request.getParameter(param)!=null ? true : false; } -} \ No newline at end of file +}