Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SOLR-17321: Remove Deprecated URL API in Preparation for Java 21 #2501

Merged
merged 12 commits into from
Jun 21, 2024
3 changes: 3 additions & 0 deletions gradle/validation/forbidden-apis/defaults.all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,6 @@ java.util.logging.**

@defaultMessage Use List.sort(Comparator) instead of Collections.sort(List, Comparator) please.
java.util.Collections#sort(java.util.List, java.util.Comparator)

@defaultMessage Use URI.toURL() to construct an instance of URL.
java.net.URL#<init>(**)
2 changes: 2 additions & 0 deletions solr/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ Improvements
* SOLR-17300: Http2SolrClient.Builder.withHttpClient now copies HttpListenerFactory (e.g. for auth, metrics, traces, etc.)
(Sanjay Dutt, David Smiley)

* SOLR-17321: Remove Deprecated URL and replace it with URI in Preparation for Java 21 (Sanjay Dutt, David Smiley)
iamsanjay marked this conversation as resolved.
Show resolved Hide resolved

Optimizations
---------------------
* SOLR-17257: Both Minimize Cores and the Affinity replica placement strategies would over-gather
Expand Down
43 changes: 22 additions & 21 deletions solr/core/src/java/org/apache/solr/cli/PostTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,10 @@ public void runImpl(CommandLine cli) throws Exception {
solrUpdateUrl = null;
if (cli.hasOption("url")) {
String url = cli.getOptionValue("url");
solrUpdateUrl = new URL(url);
solrUpdateUrl = URI.create(url).toURL();
} else if (cli.hasOption("c")) {
String url = SolrCLI.getDefaultSolrUrl() + "/solr/" + cli.getOptionValue("c") + "/update";
solrUpdateUrl = new URL(url);
solrUpdateUrl = URI.create(url).toURL();
} else {
throw new IllegalArgumentException(
"Must specify either -url or -c parameter to post documents.");
Expand Down Expand Up @@ -624,13 +624,14 @@ protected int webCrawl(int level, OutputStream out) {
if (result.httpStatus == 200) {
url = (result.redirectUrl != null) ? result.redirectUrl : url;
URL postUrl =
new URL(
appendParam(
solrUpdateUrl.toString(),
"literal.id="
+ URLEncoder.encode(url.toString(), UTF_8)
+ "&literal.url="
+ URLEncoder.encode(url.toString(), UTF_8)));
new URI(
appendParam(
solrUpdateUrl.toString(),
"literal.id="
+ URLEncoder.encode(url.toString(), UTF_8)
+ "&literal.url="
+ URLEncoder.encode(url.toString(), UTF_8)))
.toURL();
ByteBuffer content = result.content;
boolean success =
postData(
Expand Down Expand Up @@ -797,7 +798,7 @@ public void postFile(File file, OutputStream output, String type) throws Malform
if (type.equals("application/json") && !PostTool.FORMAT_SOLR.equals(format)) {
suffix = "/json/docs";
String urlStr = appendUrlPath(solrUpdateUrl, suffix).toString();
url = new URL(urlStr);
url = URI.create(urlStr).toURL();
} else if (type.equals("application/xml")
|| type.equals("text/csv")
|| type.equals("application/json")) {
Expand All @@ -815,7 +816,7 @@ public void postFile(File file, OutputStream output, String type) throws Malform
urlStr =
appendParam(urlStr, "literal.id=" + URLEncoder.encode(file.getAbsolutePath(), UTF_8));
}
url = new URL(urlStr);
url = URI.create(urlStr).toURL();
}
} else {
if (type == null) {
Expand Down Expand Up @@ -861,13 +862,14 @@ public void postFile(File file, OutputStream output, String type) throws Malform
* @return the final URL version
*/
protected static URL appendUrlPath(URL url, String append) throws MalformedURLException {
return new URL(
url.getProtocol()
+ "://"
+ url.getAuthority()
+ url.getPath()
+ append
+ (url.getQuery() != null ? "?" + url.getQuery() : ""));
return URI.create(
url.getProtocol()
+ "://"
+ url.getAuthority()
+ url.getPath()
+ append
+ (url.getQuery() != null ? "?" + url.getQuery() : ""))
.toURL();
}

/**
Expand Down Expand Up @@ -1200,7 +1202,7 @@ public boolean isDisallowedByRobots(URL url) {
disallows = new ArrayList<>();
URL urlRobot;
try {
urlRobot = new URL(strRobot);
urlRobot = URI.create(strRobot).toURL();
disallows = parseRobotsTxt(urlRobot.openStream());
} catch (MalformedURLException e) {
return true; // We cannot trust this robots URL, should not happen
Expand Down Expand Up @@ -1258,8 +1260,7 @@ protected Set<URI> getLinksFromWebPage(URL url, InputStream is, String type, URL

try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
URL extractUrl = new URL(appendParam(postUrl.toString(), "extractOnly=true"));
extractUrl = new URL(appendParam(extractUrl.toString(), "wt=xml"));
URL extractUrl = URI.create(appendParam(postUrl.toString(), "extractOnly=true")).toURL();
boolean success = postData(is, null, os, type, extractUrl);
if (success) {
Document d = makeDom(os.toByteArray());
Expand Down
3 changes: 2 additions & 1 deletion solr/core/src/java/org/apache/solr/cli/RunExampleTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.InputStream;
import java.io.PrintStream;
import java.net.Socket;
import java.net.URI;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
Expand Down Expand Up @@ -890,7 +891,7 @@ protected Map<String, Object> getNodeStatus(String solrUrl, String credentials,
StatusTool statusTool = new StatusTool();
if (verbose) echo("\nChecking status of Solr at " + solrUrl + " ...");

URL solrURL = new URL(solrUrl);
URL solrURL = new URI(solrUrl).toURL();
Map<String, Object> nodeStatus =
statusTool.waitToSeeSolrUp(solrUrl, credentials, maxWaitSecs, TimeUnit.SECONDS);
nodeStatus.put("baseUrl", solrUrl);
Expand Down
3 changes: 2 additions & 1 deletion solr/core/src/java/org/apache/solr/cli/SolrCLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.net.SocketException;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -359,7 +360,7 @@ private static Set<String> findClasses(String path, String packageName) throws E
Set<String> classes = new TreeSet<>();
if (path.startsWith("file:") && path.contains("!")) {
String[] split = path.split("!");
URL jar = new URL(split[0]);
URL jar = new URI(split[0]).toURL();
try (ZipInputStream zip = new ZipInputStream(jar.openStream())) {
ZipEntry entry;
while ((entry = zip.getNextEntry()) != null) {
Expand Down
4 changes: 2 additions & 2 deletions solr/core/src/java/org/apache/solr/cli/StatusTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package org.apache.solr.cli;

import java.io.PrintStream;
import java.net.URL;
import java.net.URI;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -94,7 +94,7 @@ public void runImpl(CommandLine cli) throws Exception {
int maxWaitSecs = Integer.parseInt(cli.getOptionValue("maxWaitSecs", "0"));
String solrUrl = SolrCLI.normalizeSolrUrl(cli);
if (maxWaitSecs > 0) {
int solrPort = (new URL(solrUrl)).getPort();
int solrPort = (new URI(solrUrl).toURL()).getPort();
echo("Waiting up to " + maxWaitSecs + " seconds to see Solr running on port " + solrPort);
try {
waitToSeeSolrUp(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.net.URI;
import java.net.URL;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -133,7 +134,7 @@ public static Pair<Future<NamedList<Object>>, SolrClient> callRemoteNode(
String nodeName, String endpoint, SolrParams params, ZkController zkController)
throws IOException, SolrServerException {
log.debug("Proxying {} request to node {}", endpoint, nodeName);
URL baseUrl = new URL(zkController.zkStateReader.getBaseUrlForNodeName(nodeName));
URL baseUrl = URI.create(zkController.zkStateReader.getBaseUrlForNodeName(nodeName)).toURL();
HttpSolrClient solr = new HttpSolrClient.Builder(baseUrl.toString()).build();
SolrRequest<?> proxyReq = new GenericSolrRequest(SolrRequest.METHOD.GET, endpoint, params);
HttpSolrClient.HttpUriRequestResponse proxyResp = solr.httpUriRequest(proxyReq);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.net.URI;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -86,9 +87,9 @@ public Path download(String artifactName) throws SolrException, IOException {
Path tmpDirectory = Files.createTempDirectory("solr-packages");
tmpDirectory.toFile().deleteOnExit();
URL url =
new URL(
new URL(repositoryURL.endsWith("/") ? repositoryURL : repositoryURL + "/"),
artifactName);
URI.create(repositoryURL.endsWith("/") ? repositoryURL : repositoryURL + "/")
.resolve(artifactName)
.toURL();
String fileName = FilenameUtils.getName(url.getPath());
Path destination = tmpDirectory.resolve(fileName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.lang.invoke.MethodHandles;
import java.net.URL;
import java.net.URI;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
Expand Down Expand Up @@ -133,7 +133,7 @@ public void addRepository(String repoName, String uri) throws Exception {
true);
}

try (InputStream is = new URL(uri + "/publickey.der").openStream()) {
try (InputStream is = new URI(uri + "/publickey.der").toURL().openStream()) {
addKey(is.readAllBytes(), repoName + ".der");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.invoke.MethodHandles;
import java.net.URL;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -151,7 +151,7 @@ public boolean reload() throws SolrException {
try {
log.debug("Reloading exchange rates from {}", ratesFileLocation);
try {
ratesJsonStream = (new URL(ratesFileLocation)).openStream();
ratesJsonStream = (new URI(ratesFileLocation).toURL()).openStream();
} catch (Exception e) {
ratesJsonStream = resourceLoader.openResource(ratesFileLocation);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.common.annotations.VisibleForTesting;
import java.lang.invoke.MethodHandles;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.Collections;
import java.util.HashSet;
Expand Down Expand Up @@ -198,9 +199,9 @@ private static String parseHostPort(String url) throws MalformedURLException {
if (!protocolMatcher.group(1).startsWith("http")) {
url = "http" + protocolMatcher.group(2);
}
u = new URL(url);
u = URI.create(url).toURL();
} else {
u = new URL("http://" + url);
u = URI.create("http://" + url).toURL();
}
if (u.getHost() == null || u.getPort() < 0) {
throw new MalformedURLException("Invalid host or port in '" + url + "'");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package org.apache.solr.security;

import java.io.IOException;
import java.net.URL;
import java.net.URI;
import java.security.spec.InvalidKeySpecException;
import org.apache.solr.common.util.StrUtils;
import org.apache.solr.core.CloudConfig;
Expand Down Expand Up @@ -55,7 +55,8 @@ private static CryptoKeys.RSAKeyPair createKeyPair(CloudConfig config) {
}

try {
return new CryptoKeys.RSAKeyPair(new URL(privateKey), new URL(publicKey));
return new CryptoKeys.RSAKeyPair(
URI.create(privateKey).toURL(), URI.create(publicKey).toURL());
} catch (IOException | InvalidKeySpecException e) {
throw new RuntimeException("Bad PublicKeyHandler configuration.", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import java.io.InputStream;
import java.io.PushbackInputStream;
import java.lang.invoke.MethodHandles;
import java.net.URL;
import java.net.URI;
import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
Expand Down Expand Up @@ -212,7 +212,7 @@ private SolrQueryRequest buildRequestFrom(
throw new SolrException(ErrorCode.BAD_REQUEST, "Remote Streaming is disabled.");
}
for (final String url : strs) {
ContentStreamBase stream = new ContentStreamBase.URLStream(new URL(url));
ContentStreamBase stream = new ContentStreamBase.URLStream(URI.create(url).toURL());
if (contentType != null) {
stream.setContentType(contentType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public URL getCanonicalUrl(URL url) throws MalformedURLException {
// NOTE: Do we want to make sure this URL is normalized? (Christian thinks we should)
String urlString = url.toString();
String lps = landingPageSuffix(url);
return new URL(urlString.replaceFirst("/" + lps + "$", "/"));
return URI.create(urlString.replaceFirst("/" + lps + "$", "/")).toURL();
}

/**
Expand Down
35 changes: 22 additions & 13 deletions solr/core/src/test/org/apache/solr/cli/PostToolTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,19 +171,24 @@ public void testComputeFullUrl() throws IOException {

assertEquals(
"http://[ff01::114]/index.html",
webPostTool.computeFullUrl(new URL("http://[ff01::114]/"), "/index.html"));
webPostTool.computeFullUrl(URI.create("http://[ff01::114]/").toURL(), "/index.html"));
assertEquals(
"http://[ff01::114]/index.html",
webPostTool.computeFullUrl(new URL("http://[ff01::114]/foo/bar/"), "/index.html"));
webPostTool.computeFullUrl(
URI.create("http://[ff01::114]/foo/bar/").toURL(), "/index.html"));
assertEquals(
"http://[ff01::114]/fil.html",
webPostTool.computeFullUrl(new URL("http://[ff01::114]/foo.htm?baz#hello"), "fil.html"));
webPostTool.computeFullUrl(
URI.create("http://[ff01::114]/foo.htm?baz#hello").toURL(), "fil.html"));
// TODO: How to know what is the base if URL path ends with "foo"??
// assertEquals("http://[ff01::114]/fil.html", t_web.computeFullUrl(new
// URL("http://[ff01::114]/foo?baz#hello"), "fil.html"));
assertNull(webPostTool.computeFullUrl(new URL("http://[ff01::114]/"), "fil.jpg"));
assertNull(webPostTool.computeFullUrl(new URL("http://[ff01::114]/"), "mailto:[email protected]"));
assertNull(webPostTool.computeFullUrl(new URL("http://[ff01::114]/"), "ftp://server/file"));
assertNull(webPostTool.computeFullUrl(URI.create("http://[ff01::114]/").toURL(), "fil.jpg"));
assertNull(
webPostTool.computeFullUrl(
URI.create("http://[ff01::114]/").toURL(), "mailto:[email protected]"));
assertNull(
webPostTool.computeFullUrl(URI.create("http://[ff01::114]/").toURL(), "ftp://server/file"));
}

@Test
Expand Down Expand Up @@ -212,8 +217,8 @@ public void testAppendParam() {
@Test
public void testAppendUrlPath() throws MalformedURLException {
assertEquals(
new URL("http://[ff01::114]/a?foo=bar"),
PostTool.appendUrlPath(new URL("http://[ff01::114]?foo=bar"), "/a"));
URI.create("http://[ff01::114]/a?foo=bar").toURL(),
PostTool.appendUrlPath(URI.create("http://[ff01::114]?foo=bar").toURL(), "/a"));
}

@Test
Expand All @@ -231,7 +236,7 @@ public void testDoFilesMode() throws MalformedURLException {
PostTool postTool = new PostTool();
postTool.recursive = 0;
postTool.dryRun = true;
postTool.solrUpdateUrl = new URL("http://localhost:8983/solr/fake/update");
postTool.solrUpdateUrl = URI.create("http://localhost:8983/solr/fake/update").toURL();
File dir = getFile("exampledocs");
int num = postTool.postFiles(new String[] {dir.toString()}, 0, null, null);
assertEquals(2, num);
Expand All @@ -253,7 +258,7 @@ public void testRecursionAppliesToFilesMode() throws MalformedURLException {
PostTool postTool = new PostTool();
postTool.recursive = 1; // This is the default
postTool.dryRun = true;
postTool.solrUpdateUrl = new URL("http://localhost:8983/solr/fake/update");
postTool.solrUpdateUrl = URI.create("http://localhost:8983/solr/fake/update").toURL();
File dir = getFile("exampledocs");
int num = postTool.postFiles(new String[] {dir.toString()}, 0, null, null);
assertEquals(2, num);
Expand All @@ -264,7 +269,8 @@ public void testDoWebMode() throws IOException, URISyntaxException {
PostTool postTool = new PostTool();
postTool.pageFetcher = new MockPageFetcher();
postTool.dryRun = true;
postTool.solrUpdateUrl = new URL("http://user:password@localhost:5150/solr/fake/update");
postTool.solrUpdateUrl =
URI.create("http://user:password@localhost:5150/solr/fake/update").toURL();

// Uses mock pageFetcher
postTool.delay = 0;
Expand All @@ -289,8 +295,11 @@ public void testRobotsExclusion() throws IOException, URISyntaxException {
postTool.pageFetcher = new MockPageFetcher();
postTool.dryRun = true;

assertFalse(postTool.pageFetcher.isDisallowedByRobots(new URL("http://[ff01::114]/")));
assertTrue(postTool.pageFetcher.isDisallowedByRobots(new URL("http://[ff01::114]/disallowed")));
assertFalse(
postTool.pageFetcher.isDisallowedByRobots(URI.create("http://[ff01::114]/").toURL()));
assertTrue(
postTool.pageFetcher.isDisallowedByRobots(
URI.create("http://[ff01::114]/disallowed").toURL()));
assertEquals(
"There should be two entries parsed from robots.txt",
2,
Expand Down
Loading
Loading