From 4c52f8212c296d4dc426d3ff3d36e8393491c6f3 Mon Sep 17 00:00:00 2001 From: Ryan McKinley Date: Mon, 17 Nov 2008 21:44:01 +0000 Subject: [PATCH] SOLR-867 move getParsedResponse utility git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@718395 13f79535-47bb-0310-9956-ffa450edef68 --- .../solrj/embedded/EmbeddedSolrServer.java | 26 ++-------- .../solr/request/BinaryResponseWriter.java | 49 +++++++++++++++---- 2 files changed, 43 insertions(+), 32 deletions(-) diff --git a/client/java/solrj/src/org/apache/solr/client/solrj/embedded/EmbeddedSolrServer.java b/client/java/solrj/src/org/apache/solr/client/solrj/embedded/EmbeddedSolrServer.java index 4465b346e71..e822eeeaff2 100644 --- a/client/java/solrj/src/org/apache/solr/client/solrj/embedded/EmbeddedSolrServer.java +++ b/client/java/solrj/src/org/apache/solr/client/solrj/embedded/EmbeddedSolrServer.java @@ -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 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); } } diff --git a/src/java/org/apache/solr/request/BinaryResponseWriter.java b/src/java/org/apache/solr/request/BinaryResponseWriter.java index 089138796ba..2a3f46ed026 100755 --- a/src/java/org/apache/solr/request/BinaryResponseWriter.java +++ b/src/java/org/apache/solr/request/BinaryResponseWriter.java @@ -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 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 ); + } + } }