You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@solr.apache.org by tf...@apache.org on 2023/03/17 02:49:29 UTC
[solr] branch branch_9x updated: SOLR-16694: Shortcut Http2SolrClient mimeType check for default response parse (#1447)
This is an automated email from the ASF dual-hosted git repository.
tflobbe pushed a commit to branch branch_9x
in repository https://gitbox.apache.org/repos/asf/solr.git
The following commit(s) were added to refs/heads/branch_9x by this push:
new 321dfc6e263 SOLR-16694: Shortcut Http2SolrClient mimeType check for default response parse (#1447)
321dfc6e263 is described below
commit 321dfc6e2636a6f92ccbf5162aaa2e23948dcb94
Author: Tomas Eduardo Fernandez Lobbe <tf...@apache.org>
AuthorDate: Thu Mar 16 17:39:08 2023 -0700
SOLR-16694: Shortcut Http2SolrClient mimeType check for default response parse (#1447)
---
solr/CHANGES.txt | 5 ++
.../client/solrj/impl/CloudHttp2SolrClient.java | 2 +-
.../solr/client/solrj/impl/Http2SolrClient.java | 85 +++++++++++++++-------
3 files changed, 65 insertions(+), 27 deletions(-)
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index a1cdea3b7cb..af56692b123 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -16,6 +16,11 @@ Improvements
Bug 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.java
index 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.java
index 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 {
@Deprecated
public 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) {