SOLR-184: add echoHandler=true to responseHeader, support echoParams=all (contributed by Ryan McKinley)

Additionally I added some unit tests to prove the new features added.



git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@526634 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Erik Hatcher 2007-04-09 02:29:24 +00:00
parent 6e21109499
commit 65e0b152db
6 changed files with 78 additions and 21 deletions

View File

@ -379,7 +379,12 @@
-->
</requestHandler>
<requestHandler name="/debug/dump" class="solr.DumpRequestHandler" />
<requestHandler name="/debug/dump" class="solr.DumpRequestHandler" >
<lst name="defaults">
<str name="echoParams">explicit</str> <!-- for all params (including the default etc) 'all' -->
<str name="echoHandler">true</str>
</lst>
</requestHandler>
<!-- NOTE, /update is mapped to a servlet, we can have the filter handle requests off that! -->
<requestHandler name="/update/commit" class="solr.CommitRequestHandler" />

View File

@ -39,10 +39,12 @@ import org.apache.solr.request.JSONResponseWriter;
import org.apache.solr.request.PythonResponseWriter;
import org.apache.solr.request.QueryResponseWriter;
import org.apache.solr.request.RubyResponseWriter;
import org.apache.solr.request.SolrParams;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.request.SolrQueryResponse;
import org.apache.solr.request.SolrRequestHandler;
import org.apache.solr.request.XMLResponseWriter;
import org.apache.solr.request.SolrParams.EchoParamStyle;
import org.apache.solr.schema.IndexSchema;
import org.apache.solr.search.SolrIndexSearcher;
import org.apache.solr.update.DirectUpdateHandler;
@ -649,10 +651,10 @@ public final class SolrCore {
public void execute(SolrRequestHandler handler, SolrQueryRequest req, SolrQueryResponse rsp) {
// setup response header and handle request
final NamedList responseHeader = new SimpleOrderedMap();
final NamedList<Object> responseHeader = new SimpleOrderedMap<Object>();
rsp.add("responseHeader", responseHeader);
handler.handleRequest(req,rsp);
setResponseHeaderValues(responseHeader,req,rsp);
setResponseHeaderValues(handler,responseHeader,req,rsp);
log.info(req.getContext().get("path") + " "
+ req.getParamString()+ " 0 "+
@ -669,26 +671,36 @@ public final class SolrCore {
execute(handler, req, rsp);
}
protected void setResponseHeaderValues(NamedList responseHeader,SolrQueryRequest req, SolrQueryResponse rsp) {
protected void setResponseHeaderValues(SolrRequestHandler handler, NamedList<Object> responseHeader,SolrQueryRequest req, SolrQueryResponse rsp) {
// TODO should check that responseHeader has not been replaced by handler
final int qtime=(int)(rsp.getEndTime() - req.getStartTime());
responseHeader.add("status",rsp.getException()==null ? 0 : 500);
responseHeader.add("QTime",qtime);
SolrParams params = req.getParams();
if( params.getBool(SolrParams.HEADER_ECHO_HANDLER, false) ) {
responseHeader.add("handler", handler.getName() );
}
// Values for echoParams... false/true/all or false/explicit/all ???
final String EP_PARAM = "echoParams";
final String EXPLICIT = "explicit";
final String epValue = req.getParams().get(EP_PARAM);
if (EXPLICIT.equals(epValue)) {
String ep = params.get( SolrParams.HEADER_ECHO_PARAMS, null );
if( ep != null ) {
EchoParamStyle echoParams = EchoParamStyle.get( ep );
if( echoParams == null ) {
throw new SolrException(400,"Invalid value '" + ep + "' for " + SolrParams.HEADER_ECHO_PARAMS
+ " parameter, use '" + EchoParamStyle.EXPLICIT + "' or '" + EchoParamStyle.ALL + "'" );
}
if( echoParams == EchoParamStyle.EXPLICIT ) {
responseHeader.add("params", req.getOriginalParams().toNamedList());
} else if(epValue!=null) {
throw new SolrException(400,"Invalid value '" + epValue + "' for " + EP_PARAM + " parameter, use '" + EXPLICIT + "'");
}
else if( echoParams == EchoParamStyle.ALL ) {
responseHeader.add("params", req.getParams().toNamedList());
}
}
}
final public static void log(Throwable e) {
SolrException.logOnce(log,null,e);
}
@ -761,11 +773,3 @@ public final class SolrCore {
}
}

View File

@ -131,6 +131,35 @@ public abstract class SolrParams {
*/
public static final String STREAM_CONTENTTYPE = "stream.contentType";
/** 'true' if the header should include the handler name */
public static final String HEADER_ECHO_HANDLER = "echoHandler";
/** include the parameters in the header **/
public static final String HEADER_ECHO_PARAMS = "echoParams";
/** valid values for: <code>echoParams</code> */
public enum EchoParamStyle {
EXPLICIT,
ALL,
NONE;
public static EchoParamStyle get( String v ) {
if( v != null ) {
v = v.toUpperCase();
if( v.equals( "EXPLICIT" ) ) {
return EXPLICIT;
}
if( v.equals( "ALL") ) {
return ALL;
}
if( v.equals( "NONE") ) { // the same as nothing...
return NONE;
}
}
return null;
}
};
/** returns the String value of a param, or null if not set */
public abstract String get(String param);
@ -333,3 +362,5 @@ public abstract class SolrParams {
}

View File

@ -424,7 +424,7 @@ public class TestHarness {
}
public LocalSolrQueryRequest makeRequest(String ... q) {
if (q.length==1) {
return new LocalSolrQueryRequest(TestHarness.this.getCore(),
return new LocalSolrQueryRequest(TestHarness.this.getCore(),
q[0], qtype, start, limit, args);
}

View File

@ -51,4 +51,17 @@ public class EchoParamsTest extends AbstractSolrTestCase {
assertQ(req("foo"),HEADER_XPATH + "/lst[@name='params']/str[@name='wt'][.='xml']");
}
public void testAllEchoParams() {
lrf = h.getRequestFactory
("crazy_custom_qt", 0, 20,
"version","2.2",
"wt","xml",
"echoParams", "all",
"echoHandler","true"
);
assertQ(req("foo"),HEADER_XPATH + "/lst[@name='params']/str[@name='fl'][.='implicit']");
assertQ(req("foo"),HEADER_XPATH + "/str[@name='handler'][.='org.apache.solr.request.StandardRequestHandler']");
}
}

View File

@ -57,7 +57,11 @@
</query>
<requestHandler name="standard" class="solr.StandardRequestHandler" />
<requestHandler name="crazy_custom_qt" class="solr.StandardRequestHandler" />
<requestHandler name="crazy_custom_qt" class="solr.StandardRequestHandler">
<lst name="defaults">
<str name="fl">implicit</str>
</lst>
</requestHandler>
<queryResponseWriter name="standard" class="org.apache.solr.request.XMLResponseWriter"/>
<queryResponseWriter name="useless" class="org.apache.solr.OutputWriterTest$UselessOutputWriter"/>