You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by no...@apache.org on 2017/11/01 08:55:45 UTC

lucene-solr:branch_7x: SOLR-11380: XML request writer also stream request

Repository: lucene-solr
Updated Branches:
  refs/heads/branch_7x 588967597 -> 9192e7e68


SOLR-11380: XML request writer also stream request


Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/9192e7e6
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/9192e7e6
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/9192e7e6

Branch: refs/heads/branch_7x
Commit: 9192e7e6887f08beea3f61e2c574c1e2232b0c38
Parents: 5889675
Author: Noble Paul <no...@apache.org>
Authored: Wed Nov 1 19:25:35 2017 +1030
Committer: Noble Paul <no...@apache.org>
Committed: Wed Nov 1 19:25:35 2017 +1030

----------------------------------------------------------------------
 .../SignatureUpdateProcessorFactoryTest.java    |  7 +-
 .../client/solrj/impl/BinaryRequestWriter.java  |  2 +-
 .../solr/client/solrj/impl/HttpSolrClient.java  | 37 +++-----
 .../client/solrj/request/RequestWriter.java     | 92 +++++---------------
 .../solr/common/util/ContentStreamBase.java     | 10 +++
 5 files changed, 45 insertions(+), 103 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/9192e7e6/solr/core/src/test/org/apache/solr/update/processor/SignatureUpdateProcessorFactoryTest.java
----------------------------------------------------------------------
diff --git a/solr/core/src/test/org/apache/solr/update/processor/SignatureUpdateProcessorFactoryTest.java b/solr/core/src/test/org/apache/solr/update/processor/SignatureUpdateProcessorFactoryTest.java
index 012b8ce..8f9d2b7 100644
--- a/solr/core/src/test/org/apache/solr/update/processor/SignatureUpdateProcessorFactoryTest.java
+++ b/solr/core/src/test/org/apache/solr/update/processor/SignatureUpdateProcessorFactoryTest.java
@@ -17,6 +17,7 @@
 package org.apache.solr.update.processor;
 
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -27,7 +28,7 @@ import org.apache.solr.client.solrj.request.UpdateRequest;
 import org.apache.solr.common.SolrInputDocument;
 import org.apache.solr.common.params.MultiMapSolrParams;
 import org.apache.solr.common.params.UpdateParams;
-import org.apache.solr.common.util.ContentStream;
+import org.apache.solr.common.util.ContentStreamBase;
 import org.apache.solr.common.util.NamedList;
 import org.apache.solr.core.SolrCore;
 import org.apache.solr.handler.UpdateRequestHandler;
@@ -323,11 +324,9 @@ public class SignatureUpdateProcessorFactoryTest extends SolrTestCaseJ4 {
     }
         
 
-    ArrayList<ContentStream> streams = new ArrayList<>(2);
-    streams.add(new BinaryRequestWriter().getContentStream(ureq));
     LocalSolrQueryRequest req = new LocalSolrQueryRequest(h.getCore(), mmparams);
     try {
-      req.setContentStreams(streams);
+      req.setContentStreams(Collections.singletonList(ContentStreamBase.create(new BinaryRequestWriter(), ureq)));
       UpdateRequestHandler h = new UpdateRequestHandler();
       h.init(new NamedList());
       h.handleRequestBody(req, new SolrQueryResponse());

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/9192e7e6/solr/solrj/src/java/org/apache/solr/client/solrj/impl/BinaryRequestWriter.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/BinaryRequestWriter.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/BinaryRequestWriter.java
index ee961ef..4687fe3 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/BinaryRequestWriter.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/BinaryRequestWriter.java
@@ -53,7 +53,7 @@ public class BinaryRequestWriter extends RequestWriter {
         }
       };
     } else {
-      return super.getContentWriter(req);
+      return null;
     }
   }
 

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/9192e7e6/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrClient.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrClient.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrClient.java
index c292534..2b60e33 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrClient.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrClient.java
@@ -443,34 +443,19 @@ public class HttpSolrClient extends SolrClient {
       contentStream[0] = content;
       break;
     }
-    if (contentStream[0] instanceof RequestWriter.LazyContentStream) {
-      Long size = contentStream[0].getSize();
-      postOrPut.setEntity(new InputStreamEntity(contentStream[0].getStream(), size == null ? -1 : size) {
-        @Override
-        public Header getContentType() {
-          return new BasicHeader("Content-Type", contentStream[0].getContentType());
-        }
-
-        @Override
-        public boolean isRepeatable() {
-          return false;
-        }
+    Long size = contentStream[0].getSize();
+    postOrPut.setEntity(new InputStreamEntity(contentStream[0].getStream(), size == null ? -1 : size) {
+      @Override
+      public Header getContentType() {
+        return new BasicHeader("Content-Type", contentStream[0].getContentType());
+      }
 
-      });
-    } else {
-      Long size = contentStream[0].getSize();
-      postOrPut.setEntity(new InputStreamEntity(contentStream[0].getStream(), size == null ? -1 : size) {
-        @Override
-        public Header getContentType() {
-          return new BasicHeader("Content-Type", contentStream[0].getContentType());
-        }
+      @Override
+      public boolean isRepeatable() {
+        return false;
+      }
+    });
 
-        @Override
-        public boolean isRepeatable() {
-          return false;
-        }
-      });
-    }
   }
 
   private HttpEntityEnclosingRequestBase fillContentStream(SolrRequest request, Collection<ContentStream> streams, ModifiableSolrParams wparams, boolean isMultipart, LinkedList<NameValuePair> postOrPutParams, String fullQueryUrl) throws IOException {

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/9192e7e6/solr/solrj/src/java/org/apache/solr/client/solrj/request/RequestWriter.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/request/RequestWriter.java b/solr/solrj/src/java/org/apache/solr/client/solrj/request/RequestWriter.java
index d83ca91..ef7f14d 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/request/RequestWriter.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/request/RequestWriter.java
@@ -18,13 +18,9 @@ package org.apache.solr.client.solrj.request;
 
 import java.io.BufferedWriter;
 import java.io.IOException;
-import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
-import java.io.Reader;
-import java.nio.charset.Charset;
 import java.nio.charset.StandardCharsets;
-import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
 import java.util.Map;
@@ -32,7 +28,6 @@ import java.util.Map;
 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;
 
 /**
  * A RequestWriter is used to write requests to Solr.
@@ -43,7 +38,6 @@ import org.apache.solr.common.util.ContentStreamBase;
  * @since solr 1.4
  */
 public class RequestWriter {
-  public static final Charset UTF_8 = StandardCharsets.UTF_8;
 
 
   public interface ContentWriter {
@@ -55,20 +49,33 @@ public class RequestWriter {
 
   /**
    * Use this to do a push writing instead of pull. If this method returns null
-   * {@link org.apache.solr.client.solrj.request.RequestWriter#getContentStream(UpdateRequest)} is
+   * {@link org.apache.solr.client.solrj.request.RequestWriter#getContentStreams(SolrRequest)} is
    * invoked to do a pull write.
    */
   public ContentWriter getContentWriter(SolrRequest req) {
+    if (req instanceof UpdateRequest) {
+      UpdateRequest updateRequest = (UpdateRequest) req;
+      if (isEmpty(updateRequest)) return null;
+      return new ContentWriter() {
+        @Override
+        public void write(OutputStream os) throws IOException {
+          OutputStreamWriter writer = new OutputStreamWriter(os, StandardCharsets.UTF_8);
+          updateRequest.writeXML(writer);
+          writer.flush();
+        }
+
+        @Override
+        public String getContentType() {
+          return ClientUtils.TEXT_XML;
+        }
+      };
+    }
     return null;
   }
 
   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<>();
-      l.add(new LazyContentStream(updateRequest));
-      return l;
+      return null;
     }
     return req.getContentStreams();
   }
@@ -84,14 +91,10 @@ public class RequestWriter {
     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;
-      BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, UTF_8));
+      BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, StandardCharsets.UTF_8));
       updateRequest.writeXML(writer);
       writer.flush();
     }
@@ -102,61 +105,6 @@ public class RequestWriter {
 
   }
 
-  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;
-    }
-
-    @Override
-    public String getName() {
-      return getDelegate().getName();
-    }
-
-    @Override
-    public String getSourceInfo() {
-      return getDelegate().getSourceInfo();
-    }
-
-    @Override
-    public String getContentType() {
-      return getUpdateContentType();
-    }
-
-    @Override
-    public Long getSize() {
-      return getDelegate().getSize();
-    }
-
-    @Override
-    public InputStream getStream() throws IOException {
-      return getDelegate().getStream();
-    }
-
-    @Override
-    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();
   }

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/9192e7e6/solr/solrj/src/java/org/apache/solr/common/util/ContentStreamBase.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/common/util/ContentStreamBase.java b/solr/solrj/src/java/org/apache/solr/common/util/ContentStreamBase.java
index f9db69c..33860ff 100644
--- a/solr/solrj/src/java/org/apache/solr/common/util/ContentStreamBase.java
+++ b/solr/solrj/src/java/org/apache/solr/common/util/ContentStreamBase.java
@@ -17,6 +17,7 @@
 package org.apache.solr.common.util;
 
 import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
@@ -30,6 +31,9 @@ import java.net.URLConnection;
 import java.nio.charset.StandardCharsets;
 import java.util.Locale;
 
+import org.apache.solr.client.solrj.SolrRequest;
+import org.apache.solr.client.solrj.request.RequestWriter;
+
 /**
  * Three concrete implementations for ContentStream - one for File/URL/String
  * 
@@ -251,6 +255,12 @@ public abstract class ContentStreamBase implements ContentStream
   public void setSourceInfo(String sourceInfo) {
     this.sourceInfo = sourceInfo;
   }
+  public static ContentStream create(RequestWriter requestWriter, SolrRequest req) throws IOException {
+    ByteArrayOutputStream baos = new ByteArrayOutputStream();
+    RequestWriter.ContentWriter contentWriter = requestWriter.getContentWriter(req);
+    contentWriter.write(baos);
+    return new ByteArrayStream(baos.toByteArray(), null,contentWriter.getContentType() );
+  }
   
   /**
    * Construct a <code>ContentStream</code> from a <code>File</code>