Skip to content

Apache Lucene open-source search software

SOLR-973 -- CommonsHttpSolrServer writes the xml directly to the server

Shalin Shekhar Mangar47487967fceb
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,51 +43,60 @@ public class BinaryRequestWriter extends RequestWriter {
isNull(updateRequest.getDeleteById()) &&
isNull(updateRequest.getDeleteQuery())) {
return null;
-
}
+ List<ContentStream> l = new ArrayList<ContentStream>();
+ l.add(new LazyContentStream(updateRequest));
+ return l;
+ } else {
+ return super.getContentStreams(req);
+ }
- final BAOS baos = new BAOS();
- new JavaBinUpdateRequestCodec().marshal(updateRequest, baos);
- List<ContentStream> l = new ArrayList<ContentStream>(1);
- l.add(new ContentStream() {
- public String getName() {
- return null;
- }
+ }
- public String getSourceInfo() {
- return "javabin";
- }
- public String getContentType() {
- return "application/octet-stream";
- }
+ public String getUpdateContentType() {
+ return "application/octet-stream";
+ }
- public Long getSize() // size if we know it, otherwise null
- {
- return new Long(baos.size());
- }
+ 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 InputStream getStream() throws IOException {
- return new ByteArrayInputStream(baos.getbuf(), 0, baos.size());
- }
+ public String getSourceInfo() {
+ return "javabin";
+ }
- public Reader getReader() throws IOException {
- throw new RuntimeException("No reader available . this is a binarystream");
- }
- });
+ public String getContentType() {
+ return "application/octet-stream";
+ }
- return l;
- } else {
- return super.getContentStreams(req);
- }
+ 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());
+ }
- private boolean isNull(List l) {
- return l == null || l.isEmpty();
+ 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.
* <p/>
* 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<ContentStream> getContentStreams(SolrRequest req) throws IOException {
+ if (req instanceof UpdateRequest) {
+ UpdateRequest updateRequest = (UpdateRequest) req;
+ if (isEmpty(updateRequest)) return null;
+ List<ContentStream> l = new ArrayList<ContentStream>();
+ 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();
+ }
}