SOLR-13727: Bug fix for V2Request handling in HttpSolrClient

Using regex to validate baseUrl and replace path for V2Requests

 Changed to using Java.net.URL for validation + path replacement
This commit is contained in:
Megan Carey 2019-08-29 17:32:13 -07:00 committed by yonik
parent 5204d0f963
commit 8c796b5f46
2 changed files with 19 additions and 2 deletions

View File

@ -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";
}

View File

@ -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";
}