Skip to content

Commit

Permalink
SOLR-17321: Remove Deprecated URL ctors in Preparation for Java 21 (a…
Browse files Browse the repository at this point in the history
…pache#2501)

* Switch to URI
* Added URL to the forbidden API

-------------

Co-authored-by:  Uwe Schindler <[email protected]>
Co-authored-by: David Smiley <[email protected]>
(cherry picked from commit 2a56bfc)
  • Loading branch information
iamsanjay committed Jun 21, 2024
1 parent 84b7406 commit 82f6b51
Show file tree
Hide file tree
Showing 36 changed files with 168 additions and 136 deletions.
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>(**)
4 changes: 4 additions & 0 deletions solr/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ Other Changes

* SOLR-16503: Use Jetty HTTP2 for SyncStrategy and PeerSyncWithLeader for "recovery" operations (Sanjay Dutt, David Smiley)

* SOLR-16796: Include cyclonedx SBOMs with maven artifacts (Arnout Engelen, Houston Putman, Kevin Risden)

* SOLR-17321: Remove Deprecated URL and replace it with URI in Preparation for Java 21 (Sanjay Dutt, David Smiley, Uwe Schindler)

================== 9.6.1 ==================
Bug Fixes
---------------------
Expand Down
60 changes: 27 additions & 33 deletions solr/core/src/java/org/apache/solr/cli/PostTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public class PostTool extends ToolBase {
int recursive = 0;
int delay = 0;
String fileTypes = PostTool.DEFAULT_FILE_TYPES;
URL solrUpdateUrl;
URI solrUpdateUrl;
String credentials;
OutputStream out = null;
String type;
Expand Down Expand Up @@ -254,10 +254,10 @@ public void runImpl(CommandLine cli) throws Exception {
solrUpdateUrl = null;
if (cli.hasOption("url")) {
String url = cli.getOptionValue("url");
solrUpdateUrl = new URL(url);
solrUpdateUrl = new URI(url);
} else if (cli.hasOption("c")) {
String url = SolrCLI.getDefaultSolrUrl() + "/solr/" + cli.getOptionValue("c") + "/update";
solrUpdateUrl = new URL(url);
solrUpdateUrl = new URI(url);
} else {
throw new IllegalArgumentException(
"Must specify either -url or -c parameter to post documents.");
Expand Down Expand Up @@ -385,7 +385,7 @@ private void doWebMode() {
numPagesPosted = postWebPages(args, 0, out);
info(numPagesPosted + " web pages indexed.");

} catch (MalformedURLException e) {
} catch (URISyntaxException e) {
warn("Wrong URL trying to append /extract to " + solrUpdateUrl);
}
}
Expand Down Expand Up @@ -511,7 +511,7 @@ int postFiles(File[] files, OutputStream out, String type) {
postFile(srcFile, out, type);
Thread.sleep(delay * 1000L);
filesPosted++;
} catch (InterruptedException | MalformedURLException e) {
} catch (InterruptedException | URISyntaxException e) {
throw new RuntimeException(e);
}
}
Expand Down Expand Up @@ -619,8 +619,8 @@ protected int webCrawl(int level, OutputStream out) {
PostTool.PageFetcherResult result = pageFetcher.readPageFromUrl(url);
if (result.httpStatus == 200) {
url = (result.redirectUrl != null) ? result.redirectUrl : url;
URL postUrl =
new URL(
URI postUri =
new URI(
appendParam(
solrUpdateUrl.toString(),
"literal.id="
Expand All @@ -634,7 +634,7 @@ protected int webCrawl(int level, OutputStream out) {
null,
out,
result.contentType,
postUrl);
postUri);
if (success) {
info("POSTed web resource " + url + " (depth: " + level + ")");
Thread.sleep(delay * 1000L);
Expand All @@ -647,7 +647,7 @@ protected int webCrawl(int level, OutputStream out) {
new ByteArrayInputStream(
content.array(), content.arrayOffset(), content.limit()),
result.contentType,
postUrl);
postUri);
subStack.addAll(children);
}
} else {
Expand Down Expand Up @@ -778,10 +778,10 @@ public static String appendParam(String url, String param) {
}

/** Opens the file and posts its contents to the solrUrl, writes to response to output. */
public void postFile(File file, OutputStream output, String type) throws MalformedURLException {
public void postFile(File file, OutputStream output, String type) throws URISyntaxException {
InputStream is = null;

URL url = solrUpdateUrl;
URI uri = solrUpdateUrl;
String suffix = "";
if (auto) {
if (type == null) {
Expand All @@ -793,7 +793,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);
uri = new URI(urlStr);
} else if (type.equals("application/xml")
|| type.equals("text/csv")
|| type.equals("application/json")) {
Expand All @@ -811,7 +811,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);
uri = new URI(urlStr);
}
} else {
if (type == null) {
Expand All @@ -834,7 +834,7 @@ public void postFile(File file, OutputStream output, String type) throws Malform
+ " to [base]"
+ suffix);
is = new FileInputStream(file);
postData(is, file.length(), output, type, url);
postData(is, file.length(), output, type, uri);
} catch (IOException e) {
warn("Can't open/read file: " + file);
} finally {
Expand All @@ -852,18 +852,13 @@ public void postFile(File file, OutputStream output, String type) throws Malform
/**
* Appends to the path of the URL
*
* @param url the URL
* @param uri the URI
* @param append the path to append
* @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() : ""));
protected static URI appendUrlPath(URI uri, String append) throws URISyntaxException {
var newPath = uri.getPath() + append;
return new URI(uri.getScheme(), uri.getAuthority(), newPath, uri.getQuery(), uri.getFragment());
}

/**
Expand All @@ -886,7 +881,7 @@ protected static String guessType(File file) {
* @return true if success
*/
public boolean postData(
InputStream data, Long length, OutputStream output, String type, URL url) {
InputStream data, Long length, OutputStream output, String type, URI uri) {
if (dryRun) {
return true;
}
Expand All @@ -898,7 +893,7 @@ public boolean postData(
HttpURLConnection urlConnection = null;
try {
try {
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection = (HttpURLConnection) (uri.toURL()).openConnection();
try {
urlConnection.setRequestMethod("POST");
} catch (ProtocolException e) {
Expand Down Expand Up @@ -1196,9 +1191,9 @@ public boolean isDisallowedByRobots(URL url) {
disallows = new ArrayList<>();
URL urlRobot;
try {
urlRobot = new URL(strRobot);
urlRobot = new URI(strRobot).toURL();
disallows = parseRobotsTxt(urlRobot.openStream());
} catch (MalformedURLException e) {
} catch (URISyntaxException | MalformedURLException e) {
return true; // We cannot trust this robots URL, should not happen
} catch (IOException e) {
// There is no robots.txt, will cache an empty disallow list
Expand Down Expand Up @@ -1246,17 +1241,16 @@ protected List<String> parseRobotsTxt(InputStream is) throws IOException {
* @param url the URL of the web page
* @param is the input stream of the page
* @param type the content-type
* @param postUrl the URL (typically /solr/extract) in order to pull out links
* @param postUri the URI (typically /solr/extract) in order to pull out links
* @return a set of URIs parsed from the page
*/
protected Set<URI> getLinksFromWebPage(URL url, InputStream is, String type, URL postUrl) {
protected Set<URI> getLinksFromWebPage(URL url, InputStream is, String type, URI postUri) {
Set<URI> linksFromPage = new HashSet<>();

try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
URL extractUrl = new URL(appendParam(postUrl.toString(), "extractOnly=true"));
extractUrl = new URL(appendParam(extractUrl.toString(), "wt=xml"));
boolean success = postData(is, null, os, type, extractUrl);
URI extractUri = new URI(appendParam(postUri.toString(), "extractOnly=true"));
boolean success = postData(is, null, os, type, extractUri);
if (success) {
Document d = makeDom(os.toByteArray());
String innerXml = getXP(d, "/response/str/text()[1]", false);
Expand All @@ -1275,7 +1269,7 @@ protected Set<URI> getLinksFromWebPage(URL url, InputStream is, String type, URL
}
}
}
} catch (MalformedURLException e) {
} catch (URISyntaxException e) {
warn("Malformed URL " + url);
} catch (IOException e) {
warn("IOException opening URL " + url + ": " + e.getMessage());
Expand Down
6 changes: 3 additions & 3 deletions solr/core/src/java/org/apache/solr/cli/RunExampleTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import java.io.InputStream;
import java.io.PrintStream;
import java.net.Socket;
import java.net.URL;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Arrays;
Expand Down Expand Up @@ -871,7 +871,7 @@ protected Map<String, Object> getNodeStatus(String solrUrl, int maxWaitSecs) thr
StatusTool statusTool = new StatusTool();
if (verbose) echo("\nChecking status of Solr at " + solrUrl + " ...");

URL solrURL = new URL(solrUrl);
URI solrURI = new URI(solrUrl);
Map<String, Object> nodeStatus =
statusTool.waitToSeeSolrUp(solrUrl, maxWaitSecs, TimeUnit.SECONDS);
nodeStatus.put("baseUrl", solrUrl);
Expand All @@ -881,7 +881,7 @@ protected Map<String, Object> getNodeStatus(String solrUrl, int maxWaitSecs) thr
if (verbose)
echo(
"\nSolr is running on "
+ solrURL.getPort()
+ solrURI.getPort()
+ " in "
+ mode
+ " mode with status:\n"
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 @@ -24,6 +24,7 @@
import java.io.File;
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 @@ -393,7 +394,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 @@ -77,7 +77,7 @@ public void runImpl(CommandLine cli) throws Exception {
int maxWaitSecs = Integer.parseInt(cli.getOptionValue("maxWaitSecs", "0"));
String solrUrl = cli.getOptionValue("solr", SolrCLI.getDefaultSolrUrl());
if (maxWaitSecs > 0) {
int solrPort = (new URL(solrUrl)).getPort();
int solrPort = new URI(solrUrl).getPort();
echo("Waiting up to " + maxWaitSecs + " seconds to see Solr running on port " + solrPort);
try {
waitToSeeSolrUp(solrUrl, maxWaitSecs, TimeUnit.SECONDS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.net.URL;
import java.net.URI;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -133,8 +133,8 @@ 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));
HttpSolrClient solr = new HttpSolrClient.Builder(baseUrl.toString()).build();
URI baseUri = URI.create(zkController.zkStateReader.getBaseUrlForNodeName(nodeName));
HttpSolrClient solr = new HttpSolrClient.Builder(baseUri.toString()).build();
SolrRequest<?> proxyReq = new GenericSolrRequest(SolrRequest.METHOD.GET, endpoint, params);
HttpSolrClient.HttpUriRequestResponse proxyResp = solr.httpUriRequest(proxyReq);
return new Pair<>(proxyResp.future, solr);
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,7 +20,7 @@
import com.google.common.annotations.VisibleForTesting;
import java.lang.invoke.MethodHandles;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URI;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -191,16 +191,16 @@ private static String parseHostPort(String url) throws MalformedURLException {
// Parse the host and port.
// It doesn't really matter which protocol we set here because we are not going to use it.
url = url.trim();
URL u;
URI u;
Matcher protocolMatcher = PROTOCOL_PATTERN.matcher(url);
if (protocolMatcher.matches()) {
// Replace any protocol unsupported by URL.
if (!protocolMatcher.group(1).startsWith("http")) {
url = "http" + protocolMatcher.group(2);
}
u = new URL(url);
u = URI.create(url);
} else {
u = new URL("http://" + url);
u = URI.create("http://" + url);
}
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
Loading

0 comments on commit 82f6b51

Please sign in to comment.