From 47487967fceb6f6e5d5b16f0f736a4600d28c36f Mon Sep 17 00:00:00 2001 From: Shalin Shekhar Mangar Date: Wed, 25 Feb 2009 14:13:00 +0000 Subject: [PATCH] SOLR-973 -- CommonsHttpSolrServer writes the xml directly to the server git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@747790 13f79535-47bb-0310-9956-ffa450edef68 --- CHANGES.txt | 3 + .../solrj/impl/BinaryRequestWriter.java | 79 +++++++++------- .../solrj/impl/CommonsHttpSolrServer.java | 35 +++++-- .../client/solrj/request/RequestWriter.java | 92 ++++++++++++++++++- 4 files changed, 166 insertions(+), 43 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 5d42f09fecf..a0e9d46d578 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -193,6 +193,9 @@ Optimizations 7. SOLR-921: SolrResourceLoader must cache short class name vs fully qualified classname (Noble Paul, hossman via shalin) + 8. SOLR-973: CommonsHttpSolrServer writes the xml directly to the server. + (Noble Paul via shalin) + Bug Fixes ---------------------- 1. SOLR-774: Fixed logging level display (Sean Timm via Otis Gospodnetic) diff --git a/src/solrj/org/apache/solr/client/solrj/impl/BinaryRequestWriter.java b/src/solrj/org/apache/solr/client/solrj/impl/BinaryRequestWriter.java index 6efaa5e0d73..2ab2c97d77f 100644 --- a/src/solrj/org/apache/solr/client/solrj/impl/BinaryRequestWriter.java +++ b/src/solrj/org/apache/solr/client/solrj/impl/BinaryRequestWriter.java @@ -43,39 +43,9 @@ public class BinaryRequestWriter extends RequestWriter { isNull(updateRequest.getDeleteById()) && isNull(updateRequest.getDeleteQuery())) { return null; - } - - final BAOS baos = new BAOS(); - new JavaBinUpdateRequestCodec().marshal(updateRequest, baos); - List l = new ArrayList(1); - l.add(new ContentStream() { - public String getName() { - return null; - } - - public String getSourceInfo() { - return "javabin"; - } - - public String getContentType() { - return "application/octet-stream"; - } - - public Long getSize() // size if we know it, otherwise null - { - return new Long(baos.size()); - } - - public InputStream getStream() throws IOException { - return new ByteArrayInputStream(baos.getbuf(), 0, baos.size()); - } - - public Reader getReader() throws IOException { - throw new RuntimeException("No reader available . this is a binarystream"); - } - }); - + List l = new ArrayList(); + l.add(new LazyContentStream(updateRequest)); return l; } else { return super.getContentStreams(req); @@ -83,11 +53,50 @@ public class BinaryRequestWriter extends RequestWriter { } - private boolean isNull(List l) { - return l == null || l.isEmpty(); + + public String getUpdateContentType() { + return "application/octet-stream"; } - /* + public ContentStream getContentStream(final UpdateRequest request) throws IOException { + final BAOS baos = new BAOS(); + new JavaBinUpdateRequestCodec().marshal(request, baos); + return new ContentStream() { + public String getName() { + return null; + } + + public String getSourceInfo() { + return "javabin"; + } + + public String getContentType() { + return "application/octet-stream"; + } + + public Long getSize() // size if we know it, otherwise null + { + return new Long(baos.size()); + } + + public InputStream getStream() throws IOException { + return new ByteArrayInputStream(baos.getbuf(), 0, baos.size()); + } + + public Reader getReader() throws IOException { + throw new RuntimeException("No reader available . this is a binarystream"); + } + }; + } + + + public void write(SolrRequest request, OutputStream os) throws IOException { + if (request instanceof UpdateRequest) { + UpdateRequest updateRequest = (UpdateRequest) request; + new JavaBinUpdateRequestCodec().marshal(updateRequest, os); + } + + }/* * A hack to get access to the protected internal buffer and avoid an additional copy */ class BAOS extends ByteArrayOutputStream { diff --git a/src/solrj/org/apache/solr/client/solrj/impl/CommonsHttpSolrServer.java b/src/solrj/org/apache/solr/client/solrj/impl/CommonsHttpSolrServer.java index 6a9a985c015..3654c063002 100644 --- a/src/solrj/org/apache/solr/client/solrj/impl/CommonsHttpSolrServer.java +++ b/src/solrj/org/apache/solr/client/solrj/impl/CommonsHttpSolrServer.java @@ -41,6 +41,7 @@ import org.apache.commons.httpclient.NoHttpResponseException; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.InputStreamRequestEntity; import org.apache.commons.httpclient.methods.PostMethod; +import org.apache.commons.httpclient.methods.RequestEntity; import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; import org.apache.commons.httpclient.methods.multipart.Part; import org.apache.commons.httpclient.methods.multipart.PartBase; @@ -350,17 +351,39 @@ public class CommonsHttpSolrServer extends SolrServer } // It is has one stream, it is the post body, put the params in the URL else { - String pstr = ClientUtils.toQueryString( params, false ); - PostMethod post = new PostMethod( url+pstr ); + String pstr = ClientUtils.toQueryString(params, false); + PostMethod post = new PostMethod(url + pstr); // Single stream as body // Using a loop just to get the first one - for( ContentStream content : streams ) { - post.setRequestEntity( - new InputStreamRequestEntity( content.getStream(), content.getContentType()) - ); + final ContentStream[] contentStream = new ContentStream[1]; + for (ContentStream content : streams) { + contentStream[0] = content; break; } + if (contentStream[0] instanceof RequestWriter.LazyContentStream) { + post.setRequestEntity(new RequestEntity() { + public long getContentLength() { + return -1; + } + + public String getContentType() { + return contentStream[0].getContentType(); + } + + public boolean isRepeatable() { + return false; + } + + public void writeRequest(OutputStream outputStream) throws IOException { + ((RequestWriter.LazyContentStream) contentStream[0]).writeTo(outputStream); + } + } + ); + + } else { + post.setRequestEntity(new InputStreamRequestEntity(contentStream[0].getStream(), contentStream[0].getContentType())); + } method = post; } } diff --git a/src/solrj/org/apache/solr/client/solrj/request/RequestWriter.java b/src/solrj/org/apache/solr/client/solrj/request/RequestWriter.java index 8eb3f0d849e..9e6574b79a5 100644 --- a/src/solrj/org/apache/solr/client/solrj/request/RequestWriter.java +++ b/src/solrj/org/apache/solr/client/solrj/request/RequestWriter.java @@ -18,26 +18,114 @@ package org.apache.solr.client.solrj.request; import org.apache.solr.client.solrj.SolrRequest; +import org.apache.solr.client.solrj.util.ClientUtils; import org.apache.solr.common.util.ContentStream; +import org.apache.solr.common.util.ContentStreamBase; -import java.io.IOException; +import java.io.*; +import java.util.ArrayList; import java.util.Collection; +import java.util.List; /** * A RequestWriter is used to write requests to Solr. *

* A subclass can override the methods in this class to supply a custom format in which a request can be sent. - * @since solr 1.4 + * * @version $Id$ + * @since solr 1.4 */ public class RequestWriter { public Collection getContentStreams(SolrRequest req) throws IOException { + if (req instanceof UpdateRequest) { + UpdateRequest updateRequest = (UpdateRequest) req; + if (isEmpty(updateRequest)) return null; + List l = new ArrayList(); + l.add(new LazyContentStream(updateRequest)); + return l; + } return req.getContentStreams(); } + private boolean isEmpty(UpdateRequest updateRequest) { + return isNull(updateRequest.getDocuments()) && + isNull(updateRequest.getDeleteById()) && + isNull(updateRequest.getDeleteQuery()); + } + public String getPath(SolrRequest req) { return req.getPath(); } + public ContentStream getContentStream(UpdateRequest req) throws IOException { + return new ContentStreamBase.StringStream(req.getXML()); + } + + public void write(SolrRequest request, OutputStream os) throws IOException { + if (request instanceof UpdateRequest) { + UpdateRequest updateRequest = (UpdateRequest) request; + OutputStreamWriter writer = new OutputStreamWriter(os); + updateRequest.writeXML(writer); + writer.flush(); + } + } + + public String getUpdateContentType() { + return ClientUtils.TEXT_XML; + + } + + public class LazyContentStream implements ContentStream { + ContentStream contentStream = null; + UpdateRequest req = null; + + public LazyContentStream(UpdateRequest req) { + this.req = req; + } + + private ContentStream getDelegate() { + if (contentStream == null) { + try { + contentStream = getContentStream(req); + } catch (IOException e) { + throw new RuntimeException("Unable to write xml into a stream", e); + } + } + return contentStream; + } + + public String getName() { + return getDelegate().getName(); + } + + public String getSourceInfo() { + return getDelegate().getSourceInfo(); + } + + public String getContentType() { + return getUpdateContentType(); + } + + public Long getSize() { + return getDelegate().getSize(); + } + + public InputStream getStream() throws IOException { + return getDelegate().getStream(); + } + + public Reader getReader() throws IOException { + return getDelegate().getReader(); + } + + public void writeTo(OutputStream os) throws IOException { + write(req, os); + + } + } + + protected boolean isNull(List l) { + return l == null || l.isEmpty(); + } }