Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

SOLR-13727: Bug fix for V2Request handling in HttpSolrClient

Megan Carey8c796b5f463c
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 8d38cd5cea1..113ccc94f13 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
@@ -23,6 +23,8 @@ import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.lang.invoke.MethodHandles;
import java.net.ConnectException;
+import java.net.MalformedURLException;
+import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collection;
@@ -477,6 +479,12 @@ public class Http2SolrClient extends SolrClient {
}
}
}
+
+ private String changeV2RequestEndpoint(String basePath) throws MalformedURLException {
+ URL oldURL = new URL(basePath);
+ String newPath = oldURL.getPath().replaceFirst("/solr", "/api");
+ return new URL(oldURL.getProtocol(), oldURL.getHost(), oldURL.getPort(), newPath).toString();
+ }
private Request createRequest(SolrRequest solrRequest, String collection) throws IOException, SolrServerException {
if (solrRequest.getBasePath() == null && serverBaseUrl == null)
@@ -514,7 +522,7 @@ public class Http2SolrClient extends SolrClient {
if (solrRequest instanceof V2Request) {
if (System.getProperty("solr.v2RealPath") == null) {
- basePath = serverBaseUrl.replace("/solr", "/api");
+ basePath = changeV2RequestEndpoint(basePath);
} else {
basePath = serverBaseUrl + "/____v2";
}
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 7498075bd84..8f1af8c7831 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
@@ -22,7 +22,9 @@ import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.lang.invoke.MethodHandles;
import java.net.ConnectException;
+import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
+import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.security.Principal;
import java.util.Arrays;
@@ -184,6 +186,7 @@ public class HttpSolrClient extends BaseHttpSolrClient {
if (baseUrl.endsWith("/")) {
baseUrl = baseUrl.substring(0, baseUrl.length() - 1);
}
+
if (baseUrl.indexOf('?') >= 0) {
throw new RuntimeException(
"Invalid base url for solrj. The base URL must not contain parameters: "
@@ -330,6 +333,12 @@ public class HttpSolrClient extends BaseHttpSolrClient {
}
return queryModParams;
}
+
+ static String changeV2RequestEndpoint(String basePath) throws MalformedURLException {
+ URL oldURL = new URL(basePath);
+ String newPath = oldURL.getPath().replaceFirst("/solr", "/api");
+ return new URL(oldURL.getProtocol(), oldURL.getHost(), oldURL.getPort(), newPath).toString();
+ }
protected HttpRequestBase createMethod(SolrRequest request, String collection) throws IOException, SolrServerException {
if (request instanceof V2RequestSupport) {
@@ -365,7 +374,7 @@ public class HttpSolrClient extends BaseHttpSolrClient {
if (request instanceof V2Request) {
if (System.getProperty("solr.v2RealPath") == null || ((V2Request) request).isForceV2()) {
- basePath = baseUrl.replace("/solr", "/api");
+ basePath = changeV2RequestEndpoint(basePath);
} else {
basePath = baseUrl + "/____v2";
}