SOLR-476: CommonsHttpSolrServer can set the ResponseParser on a per request basis.

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@632228 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Ryan McKinley 2008-02-29 05:34:51 +00:00
parent b4d3eeb0d1
commit dd622ea5f5
4 changed files with 107 additions and 40 deletions

View File

@ -40,6 +40,9 @@ Changes in runtime behavior
4. SOLR-436: To make future changes easier, SolrServer changed from an
interface to an abstract super class. (ryan)
5. SOLR-476: CommonsHttpSolrServer can set the ResponseParser on a per
request basis. (Grant Ingersoll, ryan)
Bug Fixes
Other Changes

View File

@ -33,4 +33,12 @@ public abstract class ResponseParser
public abstract NamedList<Object> processResponse(InputStream body, String encoding);
public abstract NamedList<Object> processResponse(Reader reader);
/**
* @return the version param passed to solr
*/
public String getVersion()
{
return "2.2";
}
}

View File

@ -39,6 +39,7 @@ public abstract class SolrRequest implements Serializable
private METHOD method = METHOD.GET;
private String path = null;
private ResponseParser responseParser;
//---------------------------------------------------------
//---------------------------------------------------------
@ -65,6 +66,23 @@ public abstract class SolrRequest implements Serializable
this.path = path;
}
/**
*
* @return The {@link org.apache.solr.client.solrj.ResponseParser}
*/
public ResponseParser getResponseParser() {
return responseParser;
}
/**
* Optionally specify how the Response should be parsed. Not all server implementations require a ResponseParser
* to be specified.
* @param responseParser The {@link org.apache.solr.client.solrj.ResponseParser}
*/
public void setResponseParser(ResponseParser responseParser) {
this.responseParser = responseParser;
}
public abstract SolrParams getParams();
public abstract Collection<ContentStream> getContentStreams() throws IOException;
public abstract SolrResponse process( SolrServer server ) throws SolrServerException, IOException;

View File

@ -62,7 +62,7 @@ public class CommonsHttpSolrServer extends SolrServer
*/
protected String _baseURL;
protected ModifiableSolrParams _invariantParams;
protected ResponseParser _processor;
protected ResponseParser _parser;
private final HttpClient _httpClient;
private boolean _followRedirects = false;
@ -85,7 +85,11 @@ public class CommonsHttpSolrServer extends SolrServer
* will use this SolrServer.
*/
public CommonsHttpSolrServer(String solrServerUrl, HttpClient httpClient) throws MalformedURLException {
this(new URL(solrServerUrl), httpClient);
this(new URL(solrServerUrl), httpClient, new XMLResponseParser());
}
public CommonsHttpSolrServer(String solrServerUrl, HttpClient httpClient, ResponseParser parser) throws MalformedURLException {
this(new URL(solrServerUrl), httpClient, parser);
}
/**
@ -96,11 +100,15 @@ public class CommonsHttpSolrServer extends SolrServer
*/
public CommonsHttpSolrServer(URL baseURL)
{
this(baseURL, null);
this(baseURL, null, new XMLResponseParser());
}
public CommonsHttpSolrServer(URL baseURL, HttpClient client){
this(baseURL, client, new XMLResponseParser());
}
private CommonsHttpSolrServer(URL baseURL, HttpClient client) {
public CommonsHttpSolrServer(URL baseURL, HttpClient client, ResponseParser parser) {
this._baseURL = baseURL.toExternalForm();
if( this._baseURL.endsWith( "/" ) ) {
this._baseURL = this._baseURL.substring( 0, this._baseURL.length()-1 );
@ -117,23 +125,35 @@ public class CommonsHttpSolrServer extends SolrServer
}
// by default use the XML one
_processor = new XMLResponseParser();
// TODO -- expose these so that people can add things like 'u' & 'p'
_invariantParams = new ModifiableSolrParams();
_invariantParams.set( CommonParams.WT, _processor.getWriterType() );
_invariantParams.set( CommonParams.VERSION, "2.2" );
_parser = parser;
}
//------------------------------------------------------------------------
//------------------------------------------------------------------------
/**
* Process the request. If {@link org.apache.solr.client.solrj.SolrRequest#getResponseParser()} is null, then use
* {@link #getParser()}
* @param request The {@link org.apache.solr.client.solrj.SolrRequest} to process
* @return The {@link org.apache.solr.common.util.NamedList} result
* @throws SolrServerException
* @throws IOException
*
* @see #request(org.apache.solr.client.solrj.SolrRequest, org.apache.solr.client.solrj.ResponseParser)
*/
@Override
public NamedList<Object> request( final SolrRequest request ) throws SolrServerException, IOException
{
// TODO -- need to set the WRITER TYPE!!!
// params.set( SolrParams.WT, wt );
ResponseParser responseParser = request.getResponseParser();
if (responseParser == null) {
responseParser = _parser;
}
return request(request, responseParser);
}
public NamedList<Object> request(final SolrRequest request, ResponseParser processor) throws SolrServerException, IOException{
HttpMethod method = null;
SolrParams params = request.getParams();
@ -143,9 +163,23 @@ public class CommonsHttpSolrServer extends SolrServer
path = "/select";
}
if( params == null ) {
params = new ModifiableSolrParams();
ResponseParser parser = request.getResponseParser();
if( parser == null ) {
parser = _parser;
}
// The parser 'wt=' and 'version=' params are used instead of the original params
ModifiableSolrParams wparams = new ModifiableSolrParams();
wparams = new ModifiableSolrParams();
wparams.set( CommonParams.WT, parser.getWriterType() );
wparams.set( CommonParams.VERSION, parser.getVersion() );
if( params == null ) {
params = wparams;
}
else {
params = new DefaultSolrParams( wparams, params );
}
if( _invariantParams != null ) {
params = new DefaultSolrParams( _invariantParams, params );
}
@ -314,7 +348,7 @@ public class CommonsHttpSolrServer extends SolrServer
}
}
}
return _processor.processResponse(respBody, charset);
return processor.processResponse(respBody, charset);
}
catch (HttpException e) {
throw new SolrServerException( e );
@ -347,12 +381,16 @@ public class CommonsHttpSolrServer extends SolrServer
this._baseURL = baseURL;
}
public ResponseParser getProcessor() {
return _processor;
public ResponseParser getParser() {
return _parser;
}
public void setProcessor(ResponseParser processor) {
_processor = processor;
/**
* Note: Setting this value is not thread-safe.
* @param processor The {@link org.apache.solr.client.solrj.ResponseParser}
*/
public void setParser(ResponseParser processor) {
_parser = processor;
}
public HttpClient getHttpClient() {