Apache Solr open-source search software
SOLR-16694: Shortcut Http2SolrClient mimeType check for default response parse (#1447)
Tomas Eduardo Fernandez Lobbea50cb06b2f30
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 8acdfc9eac7..18563ed053e 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -54,6 +54,11 @@ ImprovementsBug Fixes---------------------+Optimizations+---------------------++* SOLR-16694: Shortcut Http2SolrClient mimeType check for default response parse (Tomás Fernández Löbbe, Alex Deparvu)+Other Changes---------------------diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CloudHttp2SolrClient.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CloudHttp2SolrClient.javaindex 14dfa8cf3a3..632f2ab808f 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CloudHttp2SolrClient.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CloudHttp2SolrClient.java@@ -70,7 +70,7 @@ public class CloudHttp2SolrClient extends CloudSolrClient {this.myClient.requestWriter = builder.requestWriter;}if (builder.responseParser != null) {- this.myClient.parser = builder.responseParser;+ this.myClient.setParser(builder.responseParser);}if (builder.stateProvider == null) {if (builder.zkHosts != null && builder.solrUrls != null) {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.javaindex 78e191e696d..7101c041949 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@@ -136,6 +136,8 @@ public class Http2SolrClient extends SolrClient {private long requestTimeoutMillis;protected ResponseParser parser = new BinaryResponseParser();+ private Set<String> defaultParserMimeTypes;+protected RequestWriter requestWriter = new BinaryRequestWriter();private List<HttpListenerFactory> listenerFactory = new ArrayList<>();private AsyncTracker asyncTracker = new AsyncTracker();@@ -188,6 +190,7 @@ public class Http2SolrClient extends SolrClient {if (builder.responseParser != null) {parser = builder.responseParser;}+ updateDefaultMimeTypeForParser();if (builder.requestTimeoutMillis == null) {requestTimeoutMillis = -1;} else {@@ -824,32 +827,7 @@ public class Http2SolrClient extends SolrClient {return rsp;}- final Collection<String> processorSupportedContentTypes = processor.getContentTypes();- if (processorSupportedContentTypes != null && !processorSupportedContentTypes.isEmpty()) {- final Collection<String> processorMimeTypes =- processorSupportedContentTypes.stream()- .map(ct -> ContentType.parse(ct).getMimeType().trim().toLowerCase(Locale.ROOT))- .collect(Collectors.toSet());- if (!processorMimeTypes.contains(mimeType)) {- // unexpected mime type- final String allSupportedTypes = String.join(", ", processorMimeTypes);- String prefix =- "Expected mime type in [" + allSupportedTypes + "] but got " + mimeType + ". ";- String exceptionEncoding = encoding != null ? encoding : FALLBACK_CHARSET.name();- try {- ByteArrayOutputStream body = new ByteArrayOutputStream();- is.transferTo(body);- throw new RemoteSolrException(- urlExceptionMessage, httpStatus, prefix + body.toString(exceptionEncoding), null);- } catch (IOException e) {- throw new RemoteSolrException(- urlExceptionMessage,- httpStatus,- "Could not parse response with encoding " + exceptionEncoding,- e);- }- }- }+ checkContentType(processor, is, mimeType, encoding, httpStatus, urlExceptionMessage);NamedList<Object> rsp;try {@@ -918,6 +896,53 @@ public class Http2SolrClient extends SolrClient {}}+ /**+ * Validates that the content type in the response can be processed by the Response Parser. Throws+ * a {@code RemoteSolrException} if not.+ */+ private void checkContentType(+ ResponseParser processor,+ InputStream is,+ String mimeType,+ String encoding,+ int httpStatus,+ String urlExceptionMessage) {+ if (mimeType == null+ || (processor == this.parser && defaultParserMimeTypes.contains(mimeType))) {+ // Shortcut the default scenario+ return;+ }+ final Collection<String> processorSupportedContentTypes = processor.getContentTypes();+ if (processorSupportedContentTypes != null && !processorSupportedContentTypes.isEmpty()) {+ boolean processorAcceptsMimeType =+ processorSupportedContentTypes.stream()+ .map(ct -> ContentType.parse(ct).getMimeType().trim())+ .anyMatch(mimeType::equalsIgnoreCase);+ if (!processorAcceptsMimeType) {+ // unexpected mime type+ final String allSupportedTypes =+ processorSupportedContentTypes.stream()+ .map(ct -> ContentType.parse(ct).getMimeType().trim().toLowerCase(Locale.ROOT))+ .collect(Collectors.joining(", "));+ String prefix =+ "Expected mime type in [" + allSupportedTypes + "] but got " + mimeType + ". ";+ String exceptionEncoding = encoding != null ? encoding : FALLBACK_CHARSET.name();+ try {+ ByteArrayOutputStream body = new ByteArrayOutputStream();+ is.transferTo(body);+ throw new RemoteSolrException(+ urlExceptionMessage, httpStatus, prefix + body.toString(exceptionEncoding), null);+ } catch (IOException e) {+ throw new RemoteSolrException(+ urlExceptionMessage,+ httpStatus,+ "Could not parse response with encoding " + exceptionEncoding,+ e);+ }+ }+ }+ }+/*** Choose the {@link RequestWriter} to use.*@@ -1249,6 +1274,14 @@ public class Http2SolrClient extends SolrClient {@Deprecatedpublic void setParser(ResponseParser parser) {this.parser = parser;+ updateDefaultMimeTypeForParser();+ }++ protected void updateDefaultMimeTypeForParser() {+ defaultParserMimeTypes =+ parser.getContentTypes().stream()+ .map(ct -> ContentType.parse(ct).getMimeType().trim().toLowerCase(Locale.ROOT))+ .collect(Collectors.toSet());}public static void setDefaultSSLConfig(SSLConfig sslConfig) {