SOLR-867 move getParsedResponse utility

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@718395 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Ryan McKinley 2008-11-17 21:44:01 +00:00
parent a788efe789
commit 4c52f8212c
2 changed files with 43 additions and 32 deletions

View File

@ -17,28 +17,20 @@
package org.apache.solr.client.solrj.embedded;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import org.apache.solr.client.solrj.ResponseParser;
import org.apache.solr.client.solrj.SolrRequest;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.BinaryResponseParser;
import org.apache.solr.client.solrj.impl.XMLResponseParser;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.core.CoreContainer;
import org.apache.solr.core.SolrCore;
import org.apache.solr.core.CoreDescriptor;
import org.apache.solr.core.SolrCore;
import org.apache.solr.request.BinaryResponseWriter;
import org.apache.solr.request.QueryResponseWriter;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.request.SolrQueryResponse;
import org.apache.solr.request.SolrRequestHandler;
@ -166,25 +158,15 @@ public class EmbeddedSolrServer extends SolrServer
}
/**
* TODO -- in the future, this could perhaps transform the NamedList without serializing it
* then parsing it from the serialized form.
*
* @param req
* @param rsp
* @return a response object equivalent to what you get from the XML/JSON/javabin parser. Documents
* become SolrDocuments, DocList becomes SolrDocumentList etc.
*
* @deprecated use {@link BinaryResponseWriter#getParsedResponse(SolrQueryRequest, SolrQueryResponse)}
*/
public NamedList<Object> getParsedResponse( SolrQueryRequest req, SolrQueryResponse rsp )
{
try {
BinaryResponseWriter writer = new BinaryResponseWriter();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
writer.write( bos, req, rsp );
BinaryResponseParser parser = new BinaryResponseParser();
return parser.processResponse( new ByteArrayInputStream( bos.toByteArray() ), "UTF-8" );
}
catch( Exception ex ) {
throw new RuntimeException( ex );
}
return BinaryResponseWriter.getParsedResponse(req, rsp);
}
}

View File

@ -16,29 +16,30 @@
*/
package org.apache.solr.request;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Fieldable;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.NamedListCodec;
import org.apache.solr.schema.IndexSchema;
import org.apache.solr.schema.SchemaField;
import org.apache.solr.schema.TextField;
import org.apache.solr.schema.FieldType;
import org.apache.solr.schema.IndexSchema;
import org.apache.solr.search.DocIterator;
import org.apache.solr.search.DocList;
import org.apache.solr.search.SolrIndexSearcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
import java.util.*;
public class BinaryResponseWriter implements BinaryQueryResponseWriter {
private static final Logger LOG = LoggerFactory.getLogger(BinaryResponseWriter.class);
@ -162,4 +163,32 @@ public class BinaryResponseWriter implements BinaryQueryResponseWriter {
}
/**
* TODO -- there may be a way to do this without marshal at all...
*
* @param req
* @param rsp
* @return a response object equivalent to what you get from the XML/JSON/javabin parser. Documents
* become SolrDocuments, DocList becomes SolrDocumentList etc.
*
* @since solr 1.4
*/
@SuppressWarnings("unchecked")
public static NamedList<Object> getParsedResponse( SolrQueryRequest req, SolrQueryResponse rsp )
{
try {
Resolver resolver = new Resolver(req, rsp.getReturnFields());
ByteArrayOutputStream out = new ByteArrayOutputStream();
NamedListCodec codec = new NamedListCodec(resolver);
codec.marshal(rsp.getValues(), out);
InputStream in = new ByteArrayInputStream( out.toByteArray() );
return codec.unmarshal( in );
}
catch( Exception ex ) {
throw new RuntimeException( ex );
}
}
}