1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-12-22 10:59:01 +00:00

java-20 deprecates new URL(), use new URI()

cite from the ticket towards openjdk:
The URL class does not itself encode or decode any URL components
according to the escaping mechanism defined in RFC2396. It is the
responsibility of the caller to encode any fields, ...

In Java SE 1.4 a new class, java.net.URI, has been added to mitigate
some of the shortcoming of java.net.URL. Conversion methods to create
an URL from an URI were also added.

references:
* https://inside.java/2023/02/15/quality-heads-up/
* https://bugs.openjdk.org/browse/JDK-8294241
This commit is contained in:
soloturn 2023-06-14 06:32:30 +02:00
parent 32c856503f
commit 75443a5d28
3 changed files with 35 additions and 28 deletions

View File

@ -44,6 +44,8 @@ import java.io.OutputStream;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.Proxy; import java.net.Proxy;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.nio.charset.Charset; import java.nio.charset.Charset;
@ -141,8 +143,8 @@ public class SURL {
if (url.startsWith("http://") || url.startsWith("https://")) if (url.startsWith("http://") || url.startsWith("https://"))
try { try {
return create(new URL(url)); return create(new URI(url).toURL());
} catch (MalformedURLException e) { } catch (MalformedURLException | URISyntaxException e) {
Logme.error(e); Logme.error(e);
} }
return null; return null;
@ -156,8 +158,9 @@ public class SURL {
* @param url * @param url
* @return the secure URL * @return the secure URL
* @throws MalformedURLException if <code>url</code> is null * @throws MalformedURLException if <code>url</code> is null
* @throws URISyntaxException
*/ */
public static SURL create(URL url) throws MalformedURLException { public static SURL create(URL url) throws MalformedURLException, URISyntaxException {
if (url == null) if (url == null)
throw new MalformedURLException("URL cannot be null"); throw new MalformedURLException("URL cannot be null");
@ -268,8 +271,9 @@ public class SURL {
* @param url plain URL * @param url plain URL
* @return SURL without any user credential information. * @return SURL without any user credential information.
* @throws MalformedURLException * @throws MalformedURLException
* @throws URISyntaxException
*/ */
static SURL createWithoutUser(URL url) throws MalformedURLException { static SURL createWithoutUser(URL url) throws MalformedURLException, URISyntaxException {
return new SURL(removeUserInfo(url), WITHOUT_AUTHENTICATION); return new SURL(removeUserInfo(url), WITHOUT_AUTHENTICATION);
} }
@ -481,14 +485,14 @@ public class SURL {
return http; return http;
} }
public byte[] call() throws IOException { public byte[] call() throws IOException, URISyntaxException {
HttpURLConnection http = openConnection(url); HttpURLConnection http = openConnection(url);
final int responseCode = http.getResponseCode(); final int responseCode = http.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_MOVED_TEMP if (responseCode == HttpURLConnection.HTTP_MOVED_TEMP
|| responseCode == HttpURLConnection.HTTP_MOVED_PERM) { || responseCode == HttpURLConnection.HTTP_MOVED_PERM) {
final String newUrl = http.getHeaderField("Location"); final String newUrl = http.getHeaderField("Location");
http = openConnection(new URL(newUrl)); http = openConnection(new URI(newUrl).toURL());
} }
return retrieveResponseAsBytes(http); return retrieveResponseAsBytes(http);
@ -679,9 +683,10 @@ public class SURL {
* @param url URL with UserInfo part * @param url URL with UserInfo part
* @return url without UserInfo part * @return url without UserInfo part
* @throws MalformedURLException * @throws MalformedURLException
* @throws URISyntaxException
*/ */
private static URL removeUserInfo(URL url) throws MalformedURLException { private static URL removeUserInfo(URL url) throws MalformedURLException, URISyntaxException {
return new URL(removeUserInfoFromUrlPath(url.toExternalForm())); return new URI(removeUserInfoFromUrlPath(url.toExternalForm())).toURL();
} }
/** /**

View File

@ -10,7 +10,7 @@ import java.io.InputStream;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
import java.net.URL; import java.net.URI;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import javax.imageio.stream.MemoryCacheImageInputStream; import javax.imageio.stream.MemoryCacheImageInputStream;
@ -310,7 +310,7 @@ public class PicoWebServerTest {
} }
private static HttpURLConnection urlConnection(String path) throws Exception { private static HttpURLConnection urlConnection(String path) throws Exception {
final HttpURLConnection conn = (HttpURLConnection) new URL("http://localhost:" + port + path).openConnection(); final HttpURLConnection conn = (HttpURLConnection) new URI("http://localhost:" + port + path).toURL().openConnection();
conn.setInstanceFollowRedirects(false); conn.setInstanceFollowRedirects(false);
return conn; return conn;
} }

View File

@ -4,7 +4,8 @@ import static org.assertj.core.api.Assertions.assertThat;
import java.io.File; import java.io.File;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
@ -66,6 +67,7 @@ class SURLTest {
* Checks a SURL after removing the UserInfo part. * Checks a SURL after removing the UserInfo part.
* *
* @throws MalformedURLException this should not be happened * @throws MalformedURLException this should not be happened
* @throws URISyntaxException should not happen
*/ */
@ParameterizedTest @ParameterizedTest
@ValueSource(strings = { @ValueSource(strings = {
@ -75,8 +77,8 @@ class SURLTest {
"https://localhost:8080/api", "https://localhost:8080/api",
"https://alice@localhost:8080/api", "https://alice@localhost:8080/api",
"https://alice_secret@localhost:8080/api"}) "https://alice_secret@localhost:8080/api"})
void removeUserInfo(String url) throws MalformedURLException { void removeUserInfo(String url) throws MalformedURLException, URISyntaxException {
SURL surl = SURL.createWithoutUser(new URL(url)); SURL surl = SURL.createWithoutUser(new URI(url).toURL());
assertThat(surl).isNotNull(); assertThat(surl).isNotNull();
assertThat(surl.isAuthorizationConfigured()).isFalse(); assertThat(surl.isAuthorizationConfigured()).isFalse();