Skip to content

Commit 02730de

Browse files
authored
[JAVA-37265] Update URL API (#17029)
1 parent b26054e commit 02730de

File tree

17 files changed

+56
-35
lines changed

17 files changed

+56
-35
lines changed

core-java-modules/core-java-security-4/src/main/java/com/baeldung/enablessldebug/SSLDebugLogger.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.BufferedReader;
44
import java.io.InputStreamReader;
5+
import java.net.URI;
56
import java.net.URL;
67

78
import javax.net.ssl.HttpsURLConnection;
@@ -18,7 +19,7 @@ public static void enableSSLDebugUsingSystemProperties() {
1819

1920
public static void makeHttpsRequest() throws Exception {
2021
String url = "https://github.com/eugenp/tutorials";
21-
URL httpsUrl = new URL(url);
22+
URL httpsUrl = new URI(url).toURL();
2223
HttpsURLConnection connection = (HttpsURLConnection) httpsUrl.openConnection();
2324

2425
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {

core-java-modules/core-java-streams-simple/src/main/java/com/baeldung/streams/filter/Customer.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.io.IOException;
44
import java.net.HttpURLConnection;
5+
import java.net.URI;
6+
import java.net.URISyntaxException;
57
import java.net.URL;
68

79
import javax.net.ssl.HttpsURLConnection;
@@ -37,18 +39,18 @@ public boolean hasOverHundredPoints() {
3739
return this.points > 100;
3840
}
3941

40-
public boolean hasValidProfilePhoto() throws IOException {
41-
URL url = new URL(this.profilePhotoUrl);
42+
public boolean hasValidProfilePhoto() throws IOException, URISyntaxException {
43+
URL url = new URI(this.profilePhotoUrl).toURL();
4244
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
4345
return connection.getResponseCode() == HttpURLConnection.HTTP_OK;
4446
}
4547

4648
public boolean hasValidProfilePhotoWithoutCheckedException() {
4749
try {
48-
URL url = new URL(this.profilePhotoUrl);
50+
URL url = new URI(this.profilePhotoUrl).toURL();
4951
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
5052
return connection.getResponseCode() == HttpURLConnection.HTTP_OK;
51-
} catch (IOException e) {
53+
} catch (Exception e) {
5254
throw new RuntimeException(e);
5355
}
5456
}

core-java-modules/core-java-streams-simple/src/test/java/com/baeldung/streams/filter/StreamFilterUnitTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public void givenListOfCustomers_whenFilterWithTryCatch_thenGetTwo() {
129129
.filter(c -> {
130130
try {
131131
return c.hasValidProfilePhoto();
132-
} catch (IOException e) {
132+
} catch (Exception e) {
133133
//handle exception
134134
}
135135
return false;
@@ -150,7 +150,7 @@ public void givenListOfCustomers_whenFilterWithTryCatchAndRuntime_thenThrowExcep
150150
.filter(c -> {
151151
try {
152152
return c.hasValidProfilePhoto();
153-
} catch (IOException e) {
153+
} catch (Exception e) {
154154
throw new RuntimeException(e);
155155
}
156156
})

core-java-modules/core-java-streams/src/main/java/com/baeldung/stream/filter/Customer.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import javax.net.ssl.HttpsURLConnection;
44
import java.io.IOException;
55
import java.net.HttpURLConnection;
6+
import java.net.URI;
7+
import java.net.URISyntaxException;
68
import java.net.URL;
79

810
public class Customer {
@@ -36,18 +38,18 @@ public boolean hasOverHundredPoints() {
3638
return this.points > 100;
3739
}
3840

39-
public boolean hasValidProfilePhoto() throws IOException {
40-
URL url = new URL(this.profilePhotoUrl);
41+
public boolean hasValidProfilePhoto() throws Exception {
42+
URL url = new URI(this.profilePhotoUrl).toURL();
4143
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
4244
return connection.getResponseCode() == HttpURLConnection.HTTP_OK;
4345
}
4446

4547
public boolean hasValidProfilePhotoWithoutCheckedException() {
4648
try {
47-
URL url = new URL(this.profilePhotoUrl);
49+
URL url = new URI(this.profilePhotoUrl).toURL();
4850
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
4951
return connection.getResponseCode() == HttpURLConnection.HTTP_OK;
50-
} catch (IOException e) {
52+
} catch (Exception e) {
5153
throw new RuntimeException(e);
5254
}
5355
}

jackson-simple/src/test/java/com/baeldung/jackson/objectmapper/JavaReadWriteJsonExampleUnitTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import static org.junit.Assert.assertThat;
88

99
import java.io.File;
10+
import java.net.URI;
1011
import java.net.URL;
1112
import java.util.List;
1213
import java.util.Map;
@@ -103,7 +104,7 @@ public void wheReadFromFile_thanCorrect() throws Exception {
103104

104105
@Test
105106
public void wheReadFromUrl_thanCorrect() throws Exception {
106-
URL resource = new URL("file:src/test/resources/json_car.json");
107+
URL resource = new URI("file:src/test/resources/json_car.json").toURL();
107108

108109
ObjectMapper objectMapper = new ObjectMapper();
109110
Car fromFile = objectMapper.readValue(resource, Car.class);

libraries-apache-commons-2/src/test/java/com/baeldung/commons/ftp/JdkFtpClientIntegrationTest.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.io.File;
66
import java.io.IOException;
77
import java.io.InputStream;
8+
import java.net.URI;
89
import java.net.URL;
910
import java.net.URLConnection;
1011
import java.nio.file.Files;
@@ -44,10 +45,10 @@ public void teardown() throws IOException {
4445
}
4546

4647
@Test
47-
public void givenRemoteFile_whenDownloading_thenItIsOnTheLocalFilesystem() throws IOException {
48+
public void givenRemoteFile_whenDownloading_thenItIsOnTheLocalFilesystem() throws Exception {
4849
String ftpUrl = String.format("ftp://user:password@localhost:%d/foobar.txt", fakeFtpServer.getServerControlPort());
4950

50-
URLConnection urlConnection = new URL(ftpUrl).openConnection();
51+
URLConnection urlConnection = new URI(ftpUrl).toURL().openConnection();
5152
InputStream inputStream = urlConnection.getInputStream();
5253
Files.copy(inputStream, new File("downloaded_buz.txt").toPath());
5354
inputStream.close();

libraries-http-2/src/test/java/com/baeldung/mock/url/UrlFetcherJMockitUnitTest.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import static org.junit.jupiter.api.Assertions.assertTrue;
55

66
import java.net.HttpURLConnection;
7+
import java.net.URI;
78
import java.net.URL;
89

910
import org.junit.jupiter.api.Test;
@@ -24,7 +25,7 @@ void givenMockedUrl_whenRequestSent_thenIsUrlAvailableTrue(@Mocked URL anyURL, @
2425
result = HttpURLConnection.HTTP_OK;
2526
}};
2627

27-
UrlFetcher fetcher = new UrlFetcher(new URL("https://www.baeldung.com/"));
28+
UrlFetcher fetcher = new UrlFetcher(new URI("https://www.baeldung.com/").toURL());
2829
assertTrue(fetcher.isUrlAvailable(), "Url should be available: ");
2930
}
3031

@@ -35,7 +36,7 @@ void givenMockedUrl_whenRequestSent_thenIsUrlAvailableFalse(@Mocked URL anyURL,
3536
result = HttpURLConnection.HTTP_INTERNAL_ERROR;
3637
}};
3738

38-
UrlFetcher fetcher = new UrlFetcher(new URL("https://www.baeldung.com/"));
39+
UrlFetcher fetcher = new UrlFetcher(new URI("https://www.baeldung.com/").toURL());
3940
assertFalse(fetcher.isUrlAvailable(), "Url should NOT be available: ");
4041
}
4142

libraries-http-2/src/test/java/com/baeldung/mock/url/UrlFetcherUnitTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import static org.junit.jupiter.api.Assertions.assertTrue;
55

66
import java.net.HttpURLConnection;
7+
import java.net.URI;
78
import java.net.URL;
89
import java.net.URI;
910

@@ -32,7 +33,7 @@ void givenMockedUrl_whenRequestSent_thenIsUrlAvailableTrue() throws Exception {
3233
@Test
3334
void givenMockedUrl_whenRequestSent_thenIsUrlAvailableFalse() throws Exception {
3435
mockHttpURLConnection.setResponseCode(HttpURLConnection.HTTP_FORBIDDEN);
35-
URL url = new URL("https://www.baeldung.com/");
36+
URL url = new URI("https://www.baeldung.com/").toURL();
3637

3738
UrlFetcher fetcher = new UrlFetcher(url);
3839
assertFalse(fetcher.isUrlAvailable(), "Url should NOT be available: ");

maven-modules/maven-integration-test/src/test/java/com/baeldung/maven/it/RestIT.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.io.IOException;
66
import java.io.InputStream;
7+
import java.net.URI;
78
import java.net.URL;
89
import java.net.URLConnection;
910
import java.util.Scanner;
@@ -12,9 +13,9 @@
1213

1314
public class RestIT {
1415
@Test
15-
public void whenSendingGet_thenMessageIsReturned() throws IOException {
16+
public void whenSendingGet_thenMessageIsReturned() throws Exception {
1617
String url = "http://localhost:8999";
17-
URLConnection connection = new URL(url).openConnection();
18+
URLConnection connection = new URI(url).toURL().openConnection();
1819
try (InputStream response = connection.getInputStream();
1920
Scanner scanner = new Scanner(response)) {
2021
String responseBody = scanner.nextLine();

maven-modules/maven-integration-test/src/test/java/com/baeldung/maven/it/RestIntegrationTest.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.io.IOException;
66
import java.io.InputStream;
7+
import java.net.URI;
78
import java.net.URL;
89
import java.net.URLConnection;
910
import java.util.Scanner;
@@ -12,9 +13,9 @@
1213

1314
public class RestIntegrationTest {
1415
@Test
15-
public void whenSendingGet_thenMessageIsReturned() throws IOException {
16+
public void whenSendingGet_thenMessageIsReturned() throws Exception {
1617
String url = "http://localhost:8999";
17-
URLConnection connection = new URL(url).openConnection();
18+
URLConnection connection = new URI(url).toURL().openConnection();
1819
try (InputStream response = connection.getInputStream();
1920
Scanner scanner = new Scanner(response)) {
2021
String responseBody = scanner.nextLine();

maven-modules/maven-integration-test/src/test/java/com/baeldung/maven/it/RestJUnitTest.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import java.io.IOException;
77
import java.io.InputStream;
8+
import java.net.URI;
89
import java.net.URL;
910
import java.net.URLConnection;
1011
import java.util.Scanner;
@@ -14,9 +15,9 @@
1415
@Category(Integration.class)
1516
public class RestJUnitTest {
1617
@Test
17-
public void whenSendingGet_thenMessageIsReturned() throws IOException {
18+
public void whenSendingGet_thenMessageIsReturned() throws Exception {
1819
String url = "http://localhost:8999";
19-
URLConnection connection = new URL(url).openConnection();
20+
URLConnection connection = new URI(url).toURL().openConnection();
2021
try (InputStream response = connection.getInputStream();
2122
Scanner scanner = new Scanner(response)) {
2223
String responseBody = scanner.nextLine();

saas-modules/sentry-servlet/src/test/java/com/baeldung/sentry/servlet/FaultyServletLiveTest.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static org.assertj.core.api.Assertions.assertThat;
44

55
import java.net.HttpURLConnection;
6+
import java.net.URI;
67
import java.net.URL;
78

89
import org.junit.jupiter.api.Test;
@@ -14,7 +15,7 @@ class FaultyServletLiveTest {
1415
void testGivenFaultyRequestWithNoQueryString_thenSuccess() throws Exception {
1516

1617
//int port = getServerPort();
17-
URL u = new URL("http://localhost:8080/sentry-servlet/fault");
18+
URL u = new URI("http://localhost:8080/sentry-servlet/fault").toURL();
1819
HttpURLConnection conn = (HttpURLConnection)u.openConnection();
1920
int rc = conn.getResponseCode();
2021
assertThat(rc)
@@ -25,7 +26,7 @@ void testGivenFaultyRequestWithNoQueryString_thenSuccess() throws Exception {
2526
void testGivenFaultyRequestWithFaultString_thenFail() throws Exception {
2627

2728
//int port = getServerPort();
28-
URL u = new URL("http://localhost:8080/sentry-servlet/fault?fault=true");
29+
URL u = new URI("http://localhost:8080/sentry-servlet/fault?fault=true").toURL();
2930
HttpURLConnection conn = (HttpURLConnection)u.openConnection();
3031
int rc = conn.getResponseCode();
3132
assertThat(rc)
@@ -36,7 +37,7 @@ void testGivenFaultyRequestWithFaultString_thenFail() throws Exception {
3637
void testGivenFaultyRequestWithExceptionString_thenFail() throws Exception {
3738

3839
//int port = getServerPort();
39-
URL u = new URL("http://localhost:8080/sentry-servlet/fault?exception=true");
40+
URL u = new URI("http://localhost:8080/sentry-servlet/fault?exception=true").toURL();
4041
HttpURLConnection conn = (HttpURLConnection)u.openConnection();
4142
int rc = conn.getResponseCode();
4243
assertThat(rc)

spring-boot-modules/spring-boot-security/src/test/java/com/baeldung/springbootsecurity/autoconfig/config/BasicConfigurationIntegrationTest.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import java.io.IOException;
99
import java.net.MalformedURLException;
10+
import java.net.URI;
1011
import java.net.URL;
1112

1213
import org.junit.jupiter.api.BeforeEach;
@@ -29,9 +30,9 @@ class BasicConfigurationIntegrationTest {
2930
int port;
3031

3132
@BeforeEach
32-
void setUp() throws MalformedURLException {
33+
void setUp() throws Exception {
3334
restTemplate = new TestRestTemplate("user", "password");
34-
base = new URL("http://localhost:" + port);
35+
base = new URI("http://localhost:" + port).toURL();
3536
}
3637

3738
@Test

spring-security-modules/spring-security-legacy-oidc/src/main/java/com/baeldung/openid/oidc/OpenIdConnectFilter.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.baeldung.openid.oidc;
22

33
import java.io.IOException;
4+
import java.net.URI;
45
import java.net.URL;
56
import java.security.interfaces.RSAPublicKey;
67
import java.util.Date;
@@ -82,7 +83,7 @@ public void verifyClaims(Map claims) {
8283

8384

8485
private RsaVerifier verifier(String kid) throws Exception {
85-
JwkProvider provider = new UrlJwkProvider(new URL(jwkUrl));
86+
JwkProvider provider = new UrlJwkProvider(new URI(jwkUrl).toURL());
8687
Jwk jwk = provider.get(kid);
8788
return new RsaVerifier((RSAPublicKey) jwk.getPublicKey());
8889
}

spring-security-modules/spring-security-web-angular/spring-security-web-angular-server/src/test/java/com/baeldung/springbootsecurityrest/BasicAuthConfigurationIntegrationTest.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
import java.io.IOException;
88
import java.net.MalformedURLException;
9+
import java.net.URI;
10+
import java.net.URISyntaxException;
911
import java.net.URL;
1012

1113
import org.junit.Before;
@@ -31,9 +33,9 @@ public class BasicAuthConfigurationIntegrationTest {
3133
@LocalServerPort int port;
3234

3335
@Before
34-
public void setUp() throws MalformedURLException {
36+
public void setUp() throws MalformedURLException, URISyntaxException {
3537
restTemplate = new TestRestTemplate("user", "password");
36-
base = new URL("http://localhost:" + port);
38+
base = new URI("http://localhost:" + port).toURL();
3739
}
3840

3941
@Test

spring-shell/src/main/java/com/baeldung/shell/simple/SimpleURLConverter.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.baeldung.shell.simple;
22

33
import java.net.MalformedURLException;
4+
import java.net.URI;
5+
import java.net.URISyntaxException;
46
import java.net.URL;
57
import java.util.List;
68
import org.springframework.shell.core.Completion;
@@ -14,8 +16,8 @@ public class SimpleURLConverter implements Converter<URL> {
1416
@Override
1517
public URL convertFromText(String value, Class<?> requiredType, String optionContext) {
1618
try {
17-
return new URL(value);
18-
} catch (MalformedURLException ex) {
19+
return new URI(value).toURL();
20+
} catch (URISyntaxException | MalformedURLException ex) {
1921
// Ignore
2022
}
2123
return null;

testing-modules/selenium-2/src/main/java/com/baeldung/selenium/visualregression/DriverManager.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import org.openqa.selenium.remote.RemoteWebDriver;
77

88
import java.net.MalformedURLException;
9+
import java.net.URI;
10+
import java.net.URISyntaxException;
911
import java.net.URL;
1012
import java.time.Duration;
1113
import java.util.HashMap;
@@ -28,8 +30,8 @@ public void startChromeInCloud() {
2830
browserOptions.setCapability("LT:Options", ltOptions);
2931

3032
try {
31-
this.driver = new RemoteWebDriver(new URL(format("https://{0}:{1}{2}", ltUsername, ltAccessKey, gridUrl)), browserOptions);
32-
} catch (MalformedURLException e) {
33+
this.driver = new RemoteWebDriver(new URI(format("https://{0}:{1}{2}", ltUsername, ltAccessKey, gridUrl)).toURL(), browserOptions);
34+
} catch (MalformedURLException | URISyntaxException e) {
3335
throw new Error("Error in setting RemoteDriver's URL!");
3436
}
3537
this.driver.manage()

0 commit comments

Comments
 (0)