You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@solr.apache.org by ja...@apache.org on 2022/03/08 16:11:02 UTC

[solr] branch branch_9_0 updated: SOLR-16088 De-couple Http2SolrClient from org.apache.http (#731)

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

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


The following commit(s) were added to refs/heads/branch_9_0 by this push:
     new 7809a0e  SOLR-16088 De-couple Http2SolrClient from org.apache.http (#731)
7809a0e is described below

commit 7809a0eac19c113ce667de670697c51d67cf14f8
Author: Jan Høydahl <ja...@users.noreply.github.com>
AuthorDate: Tue Mar 8 14:18:34 2022 +0100

    SOLR-16088 De-couple Http2SolrClient from org.apache.http (#731)
    
    Co-authored-by: Kevin Risden <ri...@users.noreply.github.com>
    (cherry picked from commit bcfa45d84ab0258ddded57f4b1889adae57ce3b2)
---
 solr/CHANGES.txt                                   |  2 ++
 .../solr/client/solrj/impl/Http2SolrClient.java    | 30 ++++++++++------------
 .../apache/solr/common/EmptyEntityResolver.java    |  5 ++--
 .../apache/solr/common/util/ContentStreamBase.java | 23 +++++++++--------
 4 files changed, 29 insertions(+), 31 deletions(-)

diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index b69c2c6..3e0f422 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -250,6 +250,8 @@ when told to. The admin UI now tells it to. (Nazerke Seidan, David Smiley)
 
 * SOLR-15982: Add end time value to backup response, standardize backup response key names and date formats (Artem Abeleshev, Christine Poerschke, Houston Putman)
 
+* SOLR-16088: De-couple Http2SolrClient and ContentStreamBase from org.apache.http (janhoy)
+
 Build
 ---------------------
 * LUCENE-9077 LUCENE-9433: Support Gradle build, remove Ant support from trunk (Dawid Weiss, Erick Erickson, Uwe Schindler et.al.)
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 afeacde..a12d03c 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
@@ -47,8 +47,6 @@ import java.util.concurrent.Phaser;
 import java.util.concurrent.Semaphore;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
-import org.apache.http.HttpStatus;
-import org.apache.http.entity.ContentType;
 import org.apache.solr.client.solrj.ResponseParser;
 import org.apache.solr.client.solrj.SolrClient;
 import org.apache.solr.client.solrj.SolrRequest;
@@ -90,6 +88,8 @@ import org.eclipse.jetty.http.HttpField;
 import org.eclipse.jetty.http.HttpFields;
 import org.eclipse.jetty.http.HttpHeader;
 import org.eclipse.jetty.http.HttpMethod;
+import org.eclipse.jetty.http.HttpStatus;
+import org.eclipse.jetty.http.MimeTypes;
 import org.eclipse.jetty.http2.client.HTTP2Client;
 import org.eclipse.jetty.http2.client.http.HttpClientTransportOverHTTP2;
 import org.eclipse.jetty.util.BlockingArrayQueue;
@@ -475,22 +475,17 @@ public class Http2SolrClient extends SolrClient {
   private NamedList<Object> processErrorsAndResponse(
       SolrRequest<?> solrRequest, ResponseParser parser, Response response, InputStream is)
       throws SolrServerException {
-    ContentType contentType = getContentType(response);
+    String contentType = response.getHeaders().get(HttpHeader.CONTENT_TYPE);
     String mimeType = null;
     String encoding = null;
     if (contentType != null) {
-      mimeType = contentType.getMimeType();
-      encoding = contentType.getCharset() != null ? contentType.getCharset().name() : null;
+      mimeType = MimeTypes.getContentTypeWithoutCharset(contentType);
+      encoding = MimeTypes.getCharsetFromContentType(contentType);
     }
     return processErrorsAndResponse(
         response, parser, is, mimeType, encoding, isV2ApiRequest(solrRequest));
   }
 
-  private ContentType getContentType(Response response) {
-    String contentType = response.getHeaders().get(HttpHeader.CONTENT_TYPE);
-    return StringUtils.isEmpty(contentType) ? null : ContentType.parse(contentType);
-  }
-
   private void setBasicAuthHeader(SolrRequest<?> solrRequest, Request req) {
     if (solrRequest.getBasicAuthUser() != null && solrRequest.getBasicAuthPassword() != null) {
       String encoded =
@@ -722,12 +717,12 @@ public class Http2SolrClient extends SolrClient {
       int httpStatus = response.getStatus();
 
       switch (httpStatus) {
-        case HttpStatus.SC_OK:
-        case HttpStatus.SC_BAD_REQUEST:
-        case HttpStatus.SC_CONFLICT: // 409
+        case HttpStatus.OK_200:
+        case HttpStatus.BAD_REQUEST_400:
+        case HttpStatus.CONFLICT_409:
           break;
-        case HttpStatus.SC_MOVED_PERMANENTLY:
-        case HttpStatus.SC_MOVED_TEMPORARILY:
+        case HttpStatus.MOVED_PERMANENTLY_301:
+        case HttpStatus.MOVED_TEMPORARILY_302:
           if (!httpClient.isFollowRedirects()) {
             throw new SolrServerException(
                 "Server at " + getBaseURL() + " sent back a redirect (" + httpStatus + ").");
@@ -755,7 +750,7 @@ public class Http2SolrClient extends SolrClient {
       String procCt = processor.getContentType();
       if (procCt != null) {
         String procMimeType =
-            ContentType.parse(procCt).getMimeType().trim().toLowerCase(Locale.ROOT);
+            MimeTypes.getContentTypeWithoutCharset(procCt).trim().toLowerCase(Locale.ROOT);
         if (!procMimeType.equals(mimeType)) {
           // unexpected mime type
           String prefix = "Expected mime type " + procMimeType + " but got " + mimeType + ". ";
@@ -788,7 +783,7 @@ public class Http2SolrClient extends SolrClient {
               .endsWith("ExceptionWithErrObject"))) {
         throw RemoteExecutionException.create(serverBaseUrl, rsp);
       }
-      if (httpStatus != HttpStatus.SC_OK && !isV2Api) {
+      if (httpStatus != HttpStatus.OK_200 && !isV2Api) {
         NamedList<String> metadata = null;
         String reason = null;
         try {
@@ -814,6 +809,7 @@ public class Http2SolrClient extends SolrClient {
             }
           }
         } catch (Exception ex) {
+          /* Ignored */
         }
         if (reason == null) {
           StringBuilder msg = new StringBuilder();
diff --git a/solr/solrj/src/java/org/apache/solr/common/EmptyEntityResolver.java b/solr/solrj/src/java/org/apache/solr/common/EmptyEntityResolver.java
index 97a1bc3..a1e3b69 100644
--- a/solr/solrj/src/java/org/apache/solr/common/EmptyEntityResolver.java
+++ b/solr/solrj/src/java/org/apache/solr/common/EmptyEntityResolver.java
@@ -21,7 +21,6 @@ import javax.xml.XMLConstants;
 import javax.xml.parsers.SAXParserFactory;
 import javax.xml.stream.XMLInputFactory;
 import javax.xml.stream.XMLResolver;
-import org.apache.http.impl.io.EmptyInputStream;
 import org.xml.sax.EntityResolver;
 import org.xml.sax.InputSource;
 
@@ -41,7 +40,7 @@ public final class EmptyEntityResolver {
       new EntityResolver() {
         @Override
         public InputSource resolveEntity(String publicId, String systemId) {
-          return new InputSource(EmptyInputStream.INSTANCE);
+          return new InputSource(InputStream.nullInputStream());
         }
       };
 
@@ -50,7 +49,7 @@ public final class EmptyEntityResolver {
         @Override
         public InputStream resolveEntity(
             String publicId, String systemId, String baseURI, String namespace) {
-          return EmptyInputStream.INSTANCE;
+          return InputStream.nullInputStream();
         }
       };
 
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 0c5e123..48999ef 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
@@ -34,7 +34,6 @@ import java.util.List;
 import java.util.Locale;
 import java.util.function.Predicate;
 import java.util.zip.GZIPInputStream;
-import org.apache.http.entity.ContentType;
 import org.apache.solr.client.solrj.SolrRequest;
 import org.apache.solr.client.solrj.request.RequestWriter;
 
@@ -47,11 +46,13 @@ public abstract class ContentStreamBase implements ContentStream {
 
   public static final String DEFAULT_CHARSET = StandardCharsets.UTF_8.name();
   private static final String TEXT_CSV = "text/csv";
+  public static final String TEXT_XML = "text/xml";
+  public static final String APPLICATION_OCTET_STREAM = "application/octet-stream";
+  public static final String APPLICATION_GZIP = "application/gzip";
+  public static final String APPLICATION_XML = "application/xml";
+  public static final String APPLICATION_JSON = "application/json";
   private static final List<String> UNHELPFUL_TYPES =
-      Arrays.asList(
-          ContentType.APPLICATION_OCTET_STREAM.getMimeType(),
-          "application/gzip",
-          "content/unknown");
+      Arrays.asList(APPLICATION_OCTET_STREAM, APPLICATION_GZIP, "content/unknown");
   private static final List<String> XML_SUF = Arrays.asList(".xml", ".xml.gz", ".xml.gzip");
   private static final List<String> JSON_SUF = Arrays.asList(".json", ".json.gz", ".json.gzip");
   private static final List<String> CSV_SUF = Arrays.asList(".csv", ".csv.gz", ".csv.gzip");
@@ -80,9 +81,9 @@ public abstract class ContentStreamBase implements ContentStream {
       Predicate<String> endsWith = suffix -> name.toLowerCase(Locale.ROOT).endsWith(suffix);
 
       if (XML_SUF.stream().anyMatch(endsWith)) {
-        type = ContentType.APPLICATION_XML.getMimeType();
+        type = APPLICATION_XML;
       } else if (JSON_SUF.stream().anyMatch(endsWith)) {
-        type = ContentType.APPLICATION_JSON.getMimeType();
+        type = APPLICATION_JSON;
       } else if (CSV_SUF.stream().anyMatch(endsWith)) {
         type = TEXT_CSV;
       } else {
@@ -102,9 +103,9 @@ public abstract class ContentStreamBase implements ContentStream {
         data = stream.read();
       }
       if ((char) data == '<') {
-        type = ContentType.APPLICATION_XML.getMimeType();
+        type = APPLICATION_XML;
       } else if ((char) data == '{') {
-        type = ContentType.APPLICATION_JSON.getMimeType();
+        type = APPLICATION_JSON;
       }
     } catch (Exception ex) {
       // This code just eats, the exception and leaves
@@ -230,9 +231,9 @@ public abstract class ContentStreamBase implements ContentStream {
                     || str.charAt(i + 1) == '*')) // single line or multi-line comment
             || (ch == '{' || ch == '[') // start of JSON object
         ) {
-          detectedContentType = "application/json";
+          detectedContentType = APPLICATION_JSON;
         } else if (ch == '<') {
-          detectedContentType = "text/xml";
+          detectedContentType = TEXT_XML;
         }
         break;
       }