You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@solr.apache.org by ep...@apache.org on 2023/07/31 19:14:38 UTC

[solr] branch branch_9x updated (a6e6730fba0 -> 7293ad6d0c1)

This is an automated email from the ASF dual-hosted git repository.

epugh pushed a change to branch branch_9x
in repository https://gitbox.apache.org/repos/asf/solr.git


    from a6e6730fba0 complete backport of SOLR-16883
     new 02390b7e755 SOLR-16803: Remove dependencies on SimplePostTool from Solr Core classes.. (#1805)
     new 7293ad6d0c1 Finish backporting SOLR-16803.

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 solr/CHANGES.txt                                   |  2 +
 .../java/org/apache/solr/cli/RunExampleTool.java   |  1 +
 .../java/org/apache/solr/cli/SimplePostTool.java   | 35 +---------------
 .../client/solrj/embedded/EmbeddedSolrServer.java  |  4 +-
 .../java/org/apache/solr/core/BlobRepository.java  |  3 +-
 .../java/org/apache/solr/handler/BlobHandler.java  |  4 +-
 .../solr/handler/admin/ZookeeperInfoHandler.java   |  4 +-
 .../org/apache/solr/cloud/ZkNodePropsTest.java     |  3 +-
 .../apache/solr/core/TestSolrConfigHandler.java    |  6 +--
 .../org/apache/solr/handler/TestBlobHandler.java   |  4 +-
 .../src/test/org/apache/solr/pkg/TestPackages.java |  6 +--
 .../client/solrj/impl/BinaryRequestWriter.java     | 10 -----
 .../solr/client/solrj/impl/Http2SolrClient.java    |  2 +-
 .../solrj/request/MultiContentWriterRequest.java   |  4 +-
 .../java/org/apache/solr/common/util/Utils.java    | 46 ++++++++++++++++++++--
 .../solr/common/util/TestFastJavabinDecoder.java   |  9 ++---
 16 files changed, 69 insertions(+), 74 deletions(-)


[solr] 01/02: SOLR-16803: Remove dependencies on SimplePostTool from Solr Core classes.. (#1805)

Posted by ep...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

epugh pushed a commit to branch branch_9x
in repository https://gitbox.apache.org/repos/asf/solr.git

commit 02390b7e7555eb5458590bb433f06d75ea394ff7
Author: Eric Pugh <ep...@opensourceconnections.com>
AuthorDate: Mon Jul 31 14:31:02 2023 -0400

    SOLR-16803: Remove dependencies on SimplePostTool from Solr Core classes.. (#1805)
    
    Classes through solr/core and solr/solrj depended on methods defined in the SimplePostTool, a auxillary class that isn't really "part" of the core Solr offering.  Relocate those methods to the common Utils.java class.
---
 solr/CHANGES.txt                                   |  2 +
 .../java/org/apache/solr/cli/RunExampleTool.java   | 23 +++++++----
 .../java/org/apache/solr/cli/SimplePostTool.java   | 35 +---------------
 .../client/solrj/embedded/EmbeddedSolrServer.java  |  4 +-
 .../java/org/apache/solr/core/BlobRepository.java  |  3 +-
 .../java/org/apache/solr/handler/BlobHandler.java  |  4 +-
 .../solr/handler/admin/ZookeeperInfoHandler.java   |  4 +-
 .../org/apache/solr/cloud/ZkNodePropsTest.java     |  3 +-
 .../apache/solr/core/TestSolrConfigHandler.java    |  6 +--
 .../org/apache/solr/handler/TestBlobHandler.java   |  4 +-
 .../src/test/org/apache/solr/pkg/TestPackages.java |  6 +--
 .../client/solrj/impl/BinaryRequestWriter.java     | 10 -----
 .../solr/client/solrj/impl/Http2SolrClient.java    |  2 +-
 .../solrj/request/MultiContentWriterRequest.java   |  4 +-
 .../java/org/apache/solr/common/util/Utils.java    | 46 ++++++++++++++++++++--
 .../solr/common/util/TestFastJavabinDecoder.java   |  9 ++---
 16 files changed, 83 insertions(+), 82 deletions(-)

diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index c7889a598cb..6da8c73c90a 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -47,6 +47,8 @@ Other Changes
 
 * PR#1629: Fix typos in org.apache.solr.core package (Andrey Bozhko, Marcus Eagan)
 
+* SOLR-16803: Remove dependencies on methods defined in the SimplePostTool from Solr core and solrj modules.
+
 ==================  9.3.0 ==================
 
 Upgrade Notes
diff --git a/solr/core/src/java/org/apache/solr/cli/RunExampleTool.java b/solr/core/src/java/org/apache/solr/cli/RunExampleTool.java
index 0d643de529f..44b851a4599 100644
--- a/solr/core/src/java/org/apache/solr/cli/RunExampleTool.java
+++ b/solr/core/src/java/org/apache/solr/cli/RunExampleTool.java
@@ -390,14 +390,21 @@ public class RunExampleTool extends ToolBase {
         File filmsJsonFile = new File(exampleDir, "films/films.json");
         String updateUrl = String.format(Locale.ROOT, "%s/%s/update/json", solrUrl, collectionName);
         echo("Indexing films example docs from " + filmsJsonFile.getAbsolutePath());
-        String currentPropVal = System.getProperty("url");
-        System.setProperty("url", updateUrl);
-        SimplePostTool.main(new String[] {filmsJsonFile.getAbsolutePath()});
-        if (currentPropVal != null) {
-          System.setProperty("url", currentPropVal); // reset
-        } else {
-          System.clearProperty("url");
-        }
+        String[] args =
+            new String[] {
+              "post",
+              "-commit",
+              "-url",
+              updateUrl,
+              "-type",
+              "application/json",
+              exampleDir.toString()
+            };
+        PostTool postTool = new PostTool();
+        CommandLine postToolCli =
+            SolrCLI.parseCmdLine(postTool.getName(), args, postTool.getOptions());
+        postTool.runTool(postToolCli);
+
       } catch (Exception ex) {
         throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, ex);
       }
diff --git a/solr/core/src/java/org/apache/solr/cli/SimplePostTool.java b/solr/core/src/java/org/apache/solr/cli/SimplePostTool.java
index 8a364259a71..4ccbc871f33 100644
--- a/solr/core/src/java/org/apache/solr/cli/SimplePostTool.java
+++ b/solr/core/src/java/org/apache/solr/cli/SimplePostTool.java
@@ -36,7 +36,6 @@ import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.URL;
 import java.net.URLEncoder;
-import java.nio.BufferOverflowException;
 import java.nio.ByteBuffer;
 import java.nio.charset.Charset;
 import java.nio.charset.StandardCharsets;
@@ -66,6 +65,7 @@ import javax.xml.xpath.XPathConstants;
 import javax.xml.xpath.XPathExpression;
 import javax.xml.xpath.XPathExpressionException;
 import javax.xml.xpath.XPathFactory;
+import org.apache.solr.common.util.Utils;
 import org.apache.solr.util.RTimer;
 import org.w3c.dom.Document;
 import org.w3c.dom.Node;
@@ -731,37 +731,6 @@ public class SimplePostTool {
     return numPages;
   }
 
-  public static class BAOS extends ByteArrayOutputStream {
-    public ByteBuffer getByteBuffer() {
-      return ByteBuffer.wrap(super.buf, 0, super.count);
-    }
-  }
-
-  public static ByteBuffer inputStreamToByteArray(InputStream is) throws IOException {
-    return inputStreamToByteArray(is, Integer.MAX_VALUE);
-  }
-
-  /**
-   * Reads an input stream into a byte array
-   *
-   * @param is the input stream
-   * @return the byte array
-   * @throws IOException If there is a low-level I/O error.
-   */
-  public static ByteBuffer inputStreamToByteArray(InputStream is, long maxSize) throws IOException {
-    try (BAOS bos = new BAOS()) {
-      long sz = 0;
-      int next = is.read();
-      while (next > -1) {
-        if (++sz > maxSize) throw new BufferOverflowException();
-        bos.write(next);
-        next = is.read();
-      }
-      bos.flush();
-      return bos.getByteBuffer();
-    }
-  }
-
   /**
    * Computes the full URL based on a base url and a possibly relative link found in the href param
    * of an HTML anchor.
@@ -1264,7 +1233,7 @@ public class SimplePostTool {
             }
 
             // Read into memory, so that we later can pull links from the page without re-fetching
-            res.content = inputStreamToByteArray(is);
+            res.content = Utils.toByteArray(is);
             is.close();
           } else {
             warn("Skipping URL with unsupported type " + type);
diff --git a/solr/core/src/java/org/apache/solr/client/solrj/embedded/EmbeddedSolrServer.java b/solr/core/src/java/org/apache/solr/client/solrj/embedded/EmbeddedSolrServer.java
index 5a55aaa1fd3..eaef35304be 100644
--- a/solr/core/src/java/org/apache/solr/client/solrj/embedded/EmbeddedSolrServer.java
+++ b/solr/core/src/java/org/apache/solr/client/solrj/embedded/EmbeddedSolrServer.java
@@ -35,7 +35,6 @@ import org.apache.solr.client.solrj.SolrRequest;
 import org.apache.solr.client.solrj.SolrServerException;
 import org.apache.solr.client.solrj.StreamingResponseCallback;
 import org.apache.solr.client.solrj.impl.BinaryRequestWriter;
-import org.apache.solr.client.solrj.impl.BinaryRequestWriter.BAOS;
 import org.apache.solr.client.solrj.request.ContentStreamUpdateRequest;
 import org.apache.solr.client.solrj.request.RequestWriter;
 import org.apache.solr.common.SolrDocument;
@@ -48,6 +47,7 @@ import org.apache.solr.common.util.ContentStream;
 import org.apache.solr.common.util.ContentStreamBase;
 import org.apache.solr.common.util.JavaBinCodec;
 import org.apache.solr.common.util.NamedList;
+import org.apache.solr.common.util.Utils;
 import org.apache.solr.core.CoreContainer;
 import org.apache.solr.core.NodeConfig;
 import org.apache.solr.core.SolrCore;
@@ -294,7 +294,7 @@ public class EmbeddedSolrServer extends SolrClient {
     final RequestWriter.ContentWriter contentWriter = request.getContentWriter(null);
 
     String cType;
-    final BAOS baos = new BAOS();
+    final Utils.BAOS baos = new Utils.BAOS();
     if (contentWriter != null) {
       contentWriter.write(baos);
       cType = contentWriter.getContentType();
diff --git a/solr/core/src/java/org/apache/solr/core/BlobRepository.java b/solr/core/src/java/org/apache/solr/core/BlobRepository.java
index 044b715a891..b2960a67151 100644
--- a/solr/core/src/java/org/apache/solr/core/BlobRepository.java
+++ b/solr/core/src/java/org/apache/solr/core/BlobRepository.java
@@ -40,7 +40,6 @@ import org.apache.http.HttpEntity;
 import org.apache.http.HttpResponse;
 import org.apache.http.client.HttpClient;
 import org.apache.http.client.methods.HttpGet;
-import org.apache.solr.cli.SimplePostTool;
 import org.apache.solr.common.SolrException;
 import org.apache.solr.common.cloud.ClusterState;
 import org.apache.solr.common.cloud.DocCollection;
@@ -232,7 +231,7 @@ public class BlobRepository {
       }
 
       try (InputStream is = entity.getContent()) {
-        b = SimplePostTool.inputStreamToByteArray(is, MAX_JAR_SIZE);
+        b = Utils.toByteArray(is, MAX_JAR_SIZE);
       }
     } catch (Exception e) {
       if (e instanceof SolrException) {
diff --git a/solr/core/src/java/org/apache/solr/handler/BlobHandler.java b/solr/core/src/java/org/apache/solr/handler/BlobHandler.java
index 727eb1252df..3512b0154ed 100644
--- a/solr/core/src/java/org/apache/solr/handler/BlobHandler.java
+++ b/solr/core/src/java/org/apache/solr/handler/BlobHandler.java
@@ -44,7 +44,6 @@ import org.apache.lucene.search.TopDocs;
 import org.apache.lucene.search.TopFieldDocs;
 import org.apache.solr.api.AnnotatedApi;
 import org.apache.solr.api.Api;
-import org.apache.solr.cli.SimplePostTool;
 import org.apache.solr.common.SolrException;
 import org.apache.solr.common.SolrInputDocument;
 import org.apache.solr.common.params.CommonParams;
@@ -53,6 +52,7 @@ import org.apache.solr.common.params.SolrParams;
 import org.apache.solr.common.util.ContentStream;
 import org.apache.solr.common.util.NamedList;
 import org.apache.solr.common.util.StrUtils;
+import org.apache.solr.common.util.Utils;
 import org.apache.solr.core.PluginInfo;
 import org.apache.solr.core.SolrCore;
 import org.apache.solr.handler.admin.api.GetBlobInfoAPI;
@@ -112,7 +112,7 @@ public class BlobHandler extends RequestHandlerBase
       for (ContentStream stream : req.getContentStreams()) {
         ByteBuffer payload;
         try (InputStream is = stream.getStream()) {
-          payload = SimplePostTool.inputStreamToByteArray(is, maxSize);
+          payload = Utils.toByteArray(is, maxSize);
         }
         MessageDigest m = MessageDigest.getInstance("MD5");
         m.update(payload.array(), payload.arrayOffset() + payload.position(), payload.limit());
diff --git a/solr/core/src/java/org/apache/solr/handler/admin/ZookeeperInfoHandler.java b/solr/core/src/java/org/apache/solr/handler/admin/ZookeeperInfoHandler.java
index 161676fd696..b5a8234a321 100644
--- a/solr/core/src/java/org/apache/solr/handler/admin/ZookeeperInfoHandler.java
+++ b/solr/core/src/java/org/apache/solr/handler/admin/ZookeeperInfoHandler.java
@@ -41,7 +41,6 @@ import java.util.TreeMap;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 import org.apache.lucene.util.BytesRef;
-import org.apache.solr.cli.SimplePostTool.BAOS;
 import org.apache.solr.cloud.ZkController;
 import org.apache.solr.common.SolrException;
 import org.apache.solr.common.SolrException.ErrorCode;
@@ -56,6 +55,7 @@ import org.apache.solr.common.cloud.ZkStateReader;
 import org.apache.solr.common.params.MapSolrParams;
 import org.apache.solr.common.params.SolrParams;
 import org.apache.solr.common.util.ContentStream;
+import org.apache.solr.common.util.Utils;
 import org.apache.solr.core.CoreContainer;
 import org.apache.solr.handler.RequestHandlerBase;
 import org.apache.solr.request.SolrQueryRequest;
@@ -439,7 +439,7 @@ public final class ZookeeperInfoHandler extends RequestHandlerBase {
 
     String keeperAddr; // the address we're connected to
 
-    final BAOS baos = new BAOS();
+    final Utils.BAOS baos = new Utils.BAOS();
     final Writer out = new OutputStreamWriter(baos, StandardCharsets.UTF_8);
     SolrZkClient zkClient;
 
diff --git a/solr/core/src/test/org/apache/solr/cloud/ZkNodePropsTest.java b/solr/core/src/test/org/apache/solr/cloud/ZkNodePropsTest.java
index 376dc97a502..8de6eb3252a 100644
--- a/solr/core/src/test/org/apache/solr/cloud/ZkNodePropsTest.java
+++ b/solr/core/src/test/org/apache/solr/cloud/ZkNodePropsTest.java
@@ -20,7 +20,6 @@ import java.io.IOException;
 import java.util.HashMap;
 import java.util.Map;
 import org.apache.solr.SolrTestCaseJ4;
-import org.apache.solr.cli.SimplePostTool;
 import org.apache.solr.common.cloud.ZkNodeProps;
 import org.apache.solr.common.util.JavaBinCodec;
 import org.apache.solr.common.util.Utils;
@@ -43,7 +42,7 @@ public class ZkNodePropsTest extends SolrTestCaseJ4 {
     ZkNodeProps props2 = ZkNodeProps.load(bytes);
 
     props.forEach((s, o) -> assertEquals(o, props2.get(s)));
-    SimplePostTool.BAOS baos = new SimplePostTool.BAOS();
+    Utils.BAOS baos = new Utils.BAOS();
     try (JavaBinCodec jbc = new JavaBinCodec()) {
       jbc.marshal(zkProps.getProperties(), baos);
     }
diff --git a/solr/core/src/test/org/apache/solr/core/TestSolrConfigHandler.java b/solr/core/src/test/org/apache/solr/core/TestSolrConfigHandler.java
index 14c9b2ffc9b..44ad982c714 100644
--- a/solr/core/src/test/org/apache/solr/core/TestSolrConfigHandler.java
+++ b/solr/core/src/test/org/apache/solr/core/TestSolrConfigHandler.java
@@ -37,7 +37,6 @@ import java.util.zip.ZipEntry;
 import java.util.zip.ZipOutputStream;
 import org.apache.commons.io.FileUtils;
 import org.apache.solr.SolrTestCaseJ4;
-import org.apache.solr.cli.SimplePostTool;
 import org.apache.solr.client.solrj.impl.CloudSolrClient;
 import org.apache.solr.common.LinkedHashMapWriter;
 import org.apache.solr.common.MapWriter;
@@ -99,14 +98,13 @@ public class TestSolrConfigHandler extends RestTestBase {
   }
 
   public static ByteBuffer generateZip(Class<?>... classes) throws IOException {
-    SimplePostTool.BAOS bos = new SimplePostTool.BAOS();
+    Utils.BAOS bos = new Utils.BAOS();
     try (ZipOutputStream zipOut = new ZipOutputStream(bos)) {
       zipOut.setLevel(ZipOutputStream.DEFLATED);
       for (Class<?> c : classes) {
         String path = c.getName().replace('.', '/').concat(".class");
         ZipEntry entry = new ZipEntry(path);
-        ByteBuffer b =
-            SimplePostTool.inputStreamToByteArray(c.getClassLoader().getResourceAsStream(path));
+        ByteBuffer b = Utils.toByteArray(c.getClassLoader().getResourceAsStream(path));
         zipOut.putNextEntry(entry);
         zipOut.write(b.array(), b.arrayOffset(), b.limit());
         zipOut.closeEntry();
diff --git a/solr/core/src/test/org/apache/solr/handler/TestBlobHandler.java b/solr/core/src/test/org/apache/solr/handler/TestBlobHandler.java
index 9916919d4e3..0126fbc2cfb 100644
--- a/solr/core/src/test/org/apache/solr/handler/TestBlobHandler.java
+++ b/solr/core/src/test/org/apache/solr/handler/TestBlobHandler.java
@@ -32,7 +32,6 @@ import org.apache.http.client.methods.HttpGet;
 import org.apache.http.client.methods.HttpPost;
 import org.apache.http.entity.ByteArrayEntity;
 import org.apache.http.util.EntityUtils;
-import org.apache.solr.cli.SimplePostTool;
 import org.apache.solr.client.solrj.SolrClient;
 import org.apache.solr.client.solrj.SolrServerException;
 import org.apache.solr.client.solrj.impl.CloudLegacySolrClient;
@@ -44,6 +43,7 @@ import org.apache.solr.common.MapWriter;
 import org.apache.solr.common.cloud.DocCollection;
 import org.apache.solr.common.cloud.Replica;
 import org.apache.solr.common.util.StrUtils;
+import org.apache.solr.common.util.Utils;
 import org.apache.solr.util.RTimer;
 import org.junit.Test;
 import org.noggit.JSONParser;
@@ -168,7 +168,7 @@ public class TestBlobHandler extends AbstractFullDistribZkTestBase {
 
     HttpGet httpGet = new HttpGet(url);
     HttpResponse entity = httpClient.execute(httpGet);
-    ByteBuffer b = SimplePostTool.inputStreamToByteArray(entity.getEntity().getContent());
+    ByteBuffer b = Utils.toByteArray(entity.getEntity().getContent());
     try {
       assertEquals(b.limit(), bytarr.length);
       for (int i = 0; i < bytarr.length; i++) {
diff --git a/solr/core/src/test/org/apache/solr/pkg/TestPackages.java b/solr/core/src/test/org/apache/solr/pkg/TestPackages.java
index 086cdf5a1d3..3e8fea806e5 100644
--- a/solr/core/src/test/org/apache/solr/pkg/TestPackages.java
+++ b/solr/core/src/test/org/apache/solr/pkg/TestPackages.java
@@ -41,7 +41,6 @@ import org.apache.lucene.analysis.core.WhitespaceTokenizerFactory;
 import org.apache.lucene.analysis.pattern.PatternReplaceCharFilterFactory;
 import org.apache.lucene.util.ResourceLoader;
 import org.apache.lucene.util.ResourceLoaderAware;
-import org.apache.solr.cli.SimplePostTool;
 import org.apache.solr.client.solrj.SolrClient;
 import org.apache.solr.client.solrj.SolrQuery;
 import org.apache.solr.client.solrj.SolrRequest;
@@ -903,14 +902,13 @@ public class TestPackages extends SolrCloudTestCase {
   }
 
   public static ByteBuffer generateZip(Class<?>... classes) throws IOException {
-    SimplePostTool.BAOS bos = new SimplePostTool.BAOS();
+    Utils.BAOS bos = new Utils.BAOS();
     try (ZipOutputStream zipOut = new ZipOutputStream(bos)) {
       zipOut.setLevel(ZipOutputStream.DEFLATED);
       for (Class<?> c : classes) {
         String path = c.getName().replace('.', '/').concat(".class");
         ZipEntry entry = new ZipEntry(path);
-        ByteBuffer b =
-            SimplePostTool.inputStreamToByteArray(c.getClassLoader().getResourceAsStream(path));
+        ByteBuffer b = Utils.toByteArray(c.getClassLoader().getResourceAsStream(path));
         zipOut.putNextEntry(entry);
         zipOut.write(b.array(), b.arrayOffset(), b.limit());
         zipOut.closeEntry();
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 b489f88f2b5..0dcdba37599 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
@@ -18,7 +18,6 @@ package org.apache.solr.client.solrj.impl;
 
 import static org.apache.solr.common.params.CommonParams.JAVABIN_MIME;
 
-import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.util.Collection;
@@ -80,13 +79,4 @@ public class BinaryRequestWriter extends RequestWriter {
       new JavaBinUpdateRequestCodec().marshal(updateRequest, os);
     }
   }
-
-  /*
-   * A hack to get access to the protected internal buffer and avoid an additional copy
-   */
-  public static class BAOS extends ByteArrayOutputStream {
-    public byte[] getbuf() {
-      return super.buf;
-    }
-  }
 }
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/Http2SolrClient.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/Http2SolrClient.java
index 61b5b7e6798..38cc819bea7 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/Http2SolrClient.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/Http2SolrClient.java
@@ -692,7 +692,7 @@ public class Http2SolrClient extends SolrClient {
 
       if (contentWriter != null) {
         Request req = httpClient.newRequest(url + wparams.toQueryString()).method(method);
-        BinaryRequestWriter.BAOS baos = new BinaryRequestWriter.BAOS();
+        Utils.BAOS baos = new Utils.BAOS();
         contentWriter.write(baos);
 
         // SOLR-16265: TODO reduce memory usage
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/request/MultiContentWriterRequest.java b/solr/solrj/src/java/org/apache/solr/client/solrj/request/MultiContentWriterRequest.java
index 4eda65a1486..2baa4415df3 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/request/MultiContentWriterRequest.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/request/MultiContentWriterRequest.java
@@ -28,12 +28,12 @@ import java.io.OutputStream;
 import java.io.Reader;
 import java.nio.ByteBuffer;
 import java.util.Iterator;
-import org.apache.solr.client.solrj.impl.BinaryRequestWriter;
 import org.apache.solr.common.IteratorWriter;
 import org.apache.solr.common.params.ModifiableSolrParams;
 import org.apache.solr.common.util.JavaBinCodec;
 import org.apache.solr.common.util.NamedList;
 import org.apache.solr.common.util.Pair;
+import org.apache.solr.common.util.Utils;
 
 public class MultiContentWriterRequest extends AbstractUpdateRequest {
 
@@ -121,7 +121,7 @@ public class MultiContentWriterRequest extends AbstractUpdateRequest {
   }
 
   public static ByteBuffer readByteBuffer(InputStream is) throws IOException {
-    BinaryRequestWriter.BAOS baos = new BinaryRequestWriter.BAOS();
+    Utils.BAOS baos = new Utils.BAOS();
     is.transferTo(baos);
     return ByteBuffer.wrap(baos.getbuf(), 0, baos.size());
   }
diff --git a/solr/solrj/src/java/org/apache/solr/common/util/Utils.java b/solr/solrj/src/java/org/apache/solr/common/util/Utils.java
index 1158eaae608..a796872a825 100644
--- a/solr/solrj/src/java/org/apache/solr/common/util/Utils.java
+++ b/solr/solrj/src/java/org/apache/solr/common/util/Utils.java
@@ -21,6 +21,7 @@ import static java.util.Collections.singletonList;
 import static java.util.concurrent.TimeUnit.NANOSECONDS;
 
 import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
@@ -70,7 +71,6 @@ import org.apache.http.client.HttpClient;
 import org.apache.http.client.methods.HttpGet;
 import org.apache.http.client.methods.HttpRequestBase;
 import org.apache.http.util.EntityUtils;
-import org.apache.solr.client.solrj.impl.BinaryRequestWriter;
 import org.apache.solr.common.IteratorWriter;
 import org.apache.solr.common.LinkedHashMapWriter;
 import org.apache.solr.common.MapWriter;
@@ -174,7 +174,7 @@ public class Utils {
 
   public static InputStream toJavabin(Object o) throws IOException {
     try (final JavaBinCodec jbc = new JavaBinCodec()) {
-      BinaryRequestWriter.BAOS baos = new BinaryRequestWriter.BAOS();
+      BAOS baos = new BAOS();
       jbc.marshal(o, baos);
       return new ByteArrayInputStream(baos.getbuf(), 0, baos.size());
     }
@@ -743,7 +743,7 @@ public class Utils {
 
   public static InputStreamConsumer<ByteBuffer> newBytesConsumer(int maxSize) {
     return is -> {
-      try (BinaryRequestWriter.BAOS bos = new BinaryRequestWriter.BAOS()) {
+      try (BAOS bos = new BAOS()) {
         long sz = 0;
         int next = is.read();
         while (next > -1) {
@@ -1011,4 +1011,44 @@ public class Utils {
   interface FieldWriter {
     void write(MapWriter.EntryWriter ew, Object inst) throws Throwable;
   }
+
+  public static class BAOS extends ByteArrayOutputStream {
+    public ByteBuffer getByteBuffer() {
+      return ByteBuffer.wrap(super.buf, 0, super.count);
+    }
+
+    /*
+     * A hack to get access to the protected internal buffer and avoid an additional copy
+     */
+    public byte[] getbuf() {
+      return super.buf;
+    }
+  }
+
+  public static ByteBuffer toByteArray(InputStream is) throws IOException {
+    return toByteArray(is, Integer.MAX_VALUE);
+  }
+
+  /**
+   * Reads an input stream into a byte array
+   *
+   * @param is the input stream
+   * @return the byte array
+   * @throws IOException If there is a low-level I/O error.
+   */
+  public static ByteBuffer toByteArray(InputStream is, long maxSize) throws IOException {
+    try (BAOS bos = new BAOS()) {
+      long sz = 0;
+      int next = is.read();
+      while (next > -1) {
+        if (++sz > maxSize) {
+          throw new BufferOverflowException();
+        }
+        bos.write(next);
+        next = is.read();
+      }
+      bos.flush();
+      return bos.getByteBuffer();
+    }
+  }
 }
diff --git a/solr/solrj/src/test/org/apache/solr/common/util/TestFastJavabinDecoder.java b/solr/solrj/src/test/org/apache/solr/common/util/TestFastJavabinDecoder.java
index 8b569bb4f3b..55ed6128cf4 100644
--- a/solr/solrj/src/test/org/apache/solr/common/util/TestFastJavabinDecoder.java
+++ b/solr/solrj/src/test/org/apache/solr/common/util/TestFastJavabinDecoder.java
@@ -29,7 +29,6 @@ import java.util.Map;
 import java.util.Objects;
 import org.apache.solr.SolrTestCaseJ4;
 import org.apache.solr.client.solrj.FastStreamingDocsCallback;
-import org.apache.solr.client.solrj.impl.BinaryRequestWriter;
 import org.apache.solr.client.solrj.impl.StreamingBinaryResponseParser;
 import org.apache.solr.common.SolrDocument;
 import org.apache.solr.common.SolrDocumentList;
@@ -38,7 +37,7 @@ import org.apache.solr.common.util.FastJavaBinDecoder.Tag;
 public class TestFastJavabinDecoder extends SolrTestCaseJ4 {
 
   public void testTagRead() throws Exception {
-    BinaryRequestWriter.BAOS baos = new BinaryRequestWriter.BAOS();
+    Utils.BAOS baos = new Utils.BAOS();
     FastOutputStream faos = FastOutputStream.wrap(baos);
 
     try (JavaBinCodec codec = new JavaBinCodec(faos, null)) {
@@ -75,7 +74,7 @@ public class TestFastJavabinDecoder extends SolrTestCaseJ4 {
 
     @SuppressWarnings({"rawtypes"})
     Map m = (Map) Utils.fromJSONString(sampleObj);
-    BinaryRequestWriter.BAOS baos = new BinaryRequestWriter.BAOS();
+    Utils.BAOS baos = new Utils.BAOS();
     try (JavaBinCodec jbc = new JavaBinCodec()) {
       jbc.marshal(m, baos);
     }
@@ -131,7 +130,7 @@ public class TestFastJavabinDecoder extends SolrTestCaseJ4 {
   }
 
   public void testFastJavabinStreamingDecoder() throws IOException {
-    BinaryRequestWriter.BAOS baos = new BinaryRequestWriter.BAOS();
+    Utils.BAOS baos = new Utils.BAOS();
     try (InputStream is = getClass().getResourceAsStream("/solrj/javabin_sample.bin")) {
       is.transferTo(baos);
     }
@@ -216,7 +215,7 @@ public class TestFastJavabinDecoder extends SolrTestCaseJ4 {
     SimpleOrderedMap<SolrDocumentList> orderedMap = new SimpleOrderedMap<>();
     orderedMap.add("response", sdocs);
 
-    BinaryRequestWriter.BAOS baos = new BinaryRequestWriter.BAOS();
+    Utils.BAOS baos = new Utils.BAOS();
     try (JavaBinCodec jbc = new JavaBinCodec()) {
       jbc.marshal(orderedMap, baos);
     }


[solr] 02/02: Finish backporting SOLR-16803.

Posted by ep...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

epugh pushed a commit to branch branch_9x
in repository https://gitbox.apache.org/repos/asf/solr.git

commit 7293ad6d0c1518fbe2ab25d3b159d93940c5867e
Author: Eric Pugh <ep...@opensourceconnections.com>
AuthorDate: Mon Jul 31 15:14:25 2023 -0400

    Finish backporting SOLR-16803.
---
 .../java/org/apache/solr/cli/RunExampleTool.java   | 22 ++++++++--------------
 1 file changed, 8 insertions(+), 14 deletions(-)

diff --git a/solr/core/src/java/org/apache/solr/cli/RunExampleTool.java b/solr/core/src/java/org/apache/solr/cli/RunExampleTool.java
index 44b851a4599..a3615ef92c4 100644
--- a/solr/core/src/java/org/apache/solr/cli/RunExampleTool.java
+++ b/solr/core/src/java/org/apache/solr/cli/RunExampleTool.java
@@ -390,20 +390,14 @@ public class RunExampleTool extends ToolBase {
         File filmsJsonFile = new File(exampleDir, "films/films.json");
         String updateUrl = String.format(Locale.ROOT, "%s/%s/update/json", solrUrl, collectionName);
         echo("Indexing films example docs from " + filmsJsonFile.getAbsolutePath());
-        String[] args =
-            new String[] {
-              "post",
-              "-commit",
-              "-url",
-              updateUrl,
-              "-type",
-              "application/json",
-              exampleDir.toString()
-            };
-        PostTool postTool = new PostTool();
-        CommandLine postToolCli =
-            SolrCLI.parseCmdLine(postTool.getName(), args, postTool.getOptions());
-        postTool.runTool(postToolCli);
+        String currentPropVal = System.getProperty("url");
+        System.setProperty("url", updateUrl);
+        SimplePostTool.main(new String[] {filmsJsonFile.getAbsolutePath()});
+        if (currentPropVal != null) {
+          System.setProperty("url", currentPropVal); // reset
+        } else {
+          System.clearProperty("url");
+        }
 
       } catch (Exception ex) {
         throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, ex);