Skip to content

Commit 79e467f

Browse files
committed
Replace deprecated URL constructor with helper method (via URI constructor).
Providing Java 21 forward compatibility (#765; JDK 20: "The `java.net.URL` constructors are deprecated. Developers are encouraged to use `java.net.URI` to parse or construct a URL."). -------- * JDK 21: - "warning: [deprecation] URL(String) in URL has been deprecated" - "warning: [deprecation] URL(URL,String) in URL has been deprecated"
1 parent 356b0b8 commit 79e467f

10 files changed

Lines changed: 67 additions & 15 deletions

File tree

metafacture-commons/src/main/java/org/metafacture/commons/ResourceUtil.java

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import java.io.InputStreamReader;
2626
import java.io.Reader;
2727
import java.net.MalformedURLException;
28+
import java.net.URI;
29+
import java.net.URISyntaxException;
2830
import java.net.URL;
2931
import java.nio.CharBuffer;
3032
import java.nio.charset.Charset;
@@ -70,7 +72,7 @@ public static InputStream getStream(final String name)
7072
.getResourceAsStream(name);
7173
if (stream == null) {
7274
try {
73-
stream = new URL(name).openStream();
75+
stream = toURL(name).openStream();
7476
}
7577
catch (final IOException e) {
7678
throwFileNotFoundException(name, e);
@@ -177,7 +179,7 @@ public static URL getUrl(final String name) throws MalformedURLException {
177179

178180
final URL resourceUrl =
179181
Thread.currentThread().getContextClassLoader().getResource(name);
180-
return resourceUrl != null ? resourceUrl : new URL(name);
182+
return resourceUrl != null ? resourceUrl : toURL(name);
181183
}
182184

183185
/**
@@ -191,6 +193,52 @@ public static URL getUrl(final File file) throws MalformedURLException {
191193
return file.toURI().toURL();
192194
}
193195

196+
/**
197+
* Creates a URL by parsing the given spec within a specified context.
198+
*
199+
* @param context the context in which to parse the specification
200+
* @param spec the String to parse as a URL
201+
*
202+
* @return the resolved URL
203+
*
204+
* @throws MalformedURLException if the spec is invalid
205+
*/
206+
public static URL toURL(final URL context, final String spec) throws MalformedURLException {
207+
try {
208+
return context.toURI().resolve(toURI(spec)).toURL();
209+
}
210+
catch (final URISyntaxException e) {
211+
throw new MalformedURLException(e.toString());
212+
}
213+
}
214+
215+
/**
216+
* Creates a URL object from the String representation.
217+
*
218+
* @param spec the String to parse as a URL
219+
*
220+
* @return the URL
221+
*
222+
* @throws MalformedURLException if the spec is invalid
223+
*/
224+
public static URL toURL(final String spec) throws MalformedURLException {
225+
try {
226+
return toURI(spec).toURL();
227+
}
228+
catch (final IllegalArgumentException e) {
229+
throw new MalformedURLException(e.toString());
230+
}
231+
}
232+
233+
private static URI toURI(final String spec) throws MalformedURLException {
234+
try {
235+
return new URI(spec);
236+
}
237+
catch (final URISyntaxException e) {
238+
throw new MalformedURLException(e.toString());
239+
}
240+
}
241+
194242
/**
195243
* Creates Properties based upon a location. First attempts to open a file. On
196244
* fail attempts to open the resource with name. On fail attempts to open name

metafacture-io/src/main/java/org/metafacture/io/HttpOpener.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.metafacture.io;
1818

19+
import org.metafacture.commons.ResourceUtil;
1920
import org.metafacture.framework.FluxCommand;
2021
import org.metafacture.framework.MetafactureException;
2122
import org.metafacture.framework.ObjectReceiver;
@@ -301,7 +302,7 @@ public boolean open(final String input, final Consumer<Reader> consumer) {
301302
final String requestBody = getInput(input,
302303
body == null && method.getRequestHasBody() ? INPUT_DESIGNATOR : body);
303304

304-
final URL urlToOpen = new URL(requestUrl);
305+
final URL urlToOpen = ResourceUtil.toURL(requestUrl);
305306
final HttpURLConnection connection = requestBody != null ?
306307
doOutput(urlToOpen, requestBody) : doRedirects(urlToOpen);
307308

@@ -368,7 +369,7 @@ private HttpURLConnection doRedirects(final URL startingUrl) throws IOException
368369
case HttpURLConnection.HTTP_MOVED_PERM:
369370
case HttpURLConnection.HTTP_MOVED_TEMP:
370371
final String location = URLDecoder.decode(connection.getHeaderField("Location"), "UTF-8");
371-
urlToFollow = new URL(urlToFollow, location); // Deal with relative URLs
372+
urlToFollow = ResourceUtil.toURL(urlToFollow, location); // Deal with relative URLs
372373
break;
373374
default:
374375
return connection;

metafacture-json/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ description = 'Modules for processing JSON data in Metafacture'
1919

2020
dependencies {
2121
api project(':metafacture-framework')
22+
implementation project(':metafacture-commons')
2223
implementation "com.fasterxml.jackson.core:jackson-core:${versions.jackson}"
2324
implementation "com.fasterxml.jackson.core:jackson-databind:${versions.jackson}"
2425
implementation "com.jayway.jsonpath:json-path:${versions.jsonpath}"

metafacture-json/src/main/java/org/metafacture/json/JsonValidator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.metafacture.json;
1818

19+
import org.metafacture.commons.ResourceUtil;
1920
import org.metafacture.framework.FluxCommand;
2021
import org.metafacture.framework.MetafactureException;
2122
import org.metafacture.framework.MetafactureLogger;
@@ -125,7 +126,7 @@ private void initSchema(final String schemaUrl) {
125126
}
126127
SchemaLoaderBuilder schemaLoader = SchemaLoader.builder();
127128
try {
128-
final URL url = new URL(schemaUrl);
129+
final URL url = ResourceUtil.toURL(schemaUrl);
129130
schemaLoader = schemaLoader.schemaJson(jsonFrom(url.openStream()))
130131
.resolutionScope(baseFor(url.toString()));
131132
}

metafacture-json/src/test/java/org/metafacture/json/JsonValidatorTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.metafacture.json;
1818

19+
import org.metafacture.commons.ResourceUtil;
1920
import org.metafacture.framework.MetafactureException;
2021
import org.metafacture.framework.ObjectReceiver;
2122

@@ -113,7 +114,7 @@ private String readToString(final URL url) throws IOException {
113114

114115
@Test
115116
public void callWireMockSchema() throws MalformedURLException, IOException {
116-
final String schemaContent = readToString(new URL(wireMockRule.baseUrl() + MAIN_SCHEMA));
117+
final String schemaContent = readToString(ResourceUtil.toURL(wireMockRule.baseUrl() + MAIN_SCHEMA));
117118
MatcherAssert.assertThat(schemaContent, CoreMatchers.both(CoreMatchers.containsString("$schema")).and(CoreMatchers.containsString("$ref")));
118119
}
119120

metafacture-triples/src/main/java/org/metafacture/triples/TripleObjectRetriever.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public void process(final Triple triple) {
100100

101101
private String retrieveObjectValue(final String urlString) {
102102
try {
103-
final URL url = new URL(urlString);
103+
final URL url = ResourceUtil.toURL(urlString);
104104
final URLConnection connection = url.openConnection();
105105
connection.connect();
106106
final String encodingName = connection.getContentEncoding();

metafix/src/main/java/org/metafacture/metafix/Metafix.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
package org.metafacture.metafix;
2020

21+
import org.metafacture.commons.ResourceUtil;
2122
import org.metafacture.framework.FluxCommand;
2223
import org.metafacture.framework.MetafactureException;
2324
import org.metafacture.framework.MetafactureLogger;
@@ -39,7 +40,6 @@
3940
import java.io.StringReader;
4041
import java.io.UncheckedIOException;
4142
import java.net.MalformedURLException;
42-
import java.net.URL;
4343
import java.nio.file.Path;
4444
import java.nio.file.Paths;
4545
import java.util.ArrayList;
@@ -290,7 +290,7 @@ private String resolvePathInternal(final String path) {
290290

291291
private boolean isValidUrl(final String url) {
292292
try {
293-
new URL(url);
293+
ResourceUtil.toURL(url);
294294
return true;
295295
}
296296
catch (final MalformedURLException e) {

metafix/src/main/java/org/metafacture/metafix/maps/RdfMap.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.metafacture.metafix.maps;
1818

19+
import org.metafacture.commons.ResourceUtil;
1920
import org.metafacture.metafix.FixExecutionException;
2021
import org.metafacture.metamorph.api.Maps;
2122
import org.metafacture.metamorph.api.helpers.AbstractReadOnlyMap;
@@ -34,7 +35,6 @@
3435
import java.io.Closeable;
3536
import java.io.IOException;
3637
import java.net.HttpURLConnection;
37-
import java.net.URL;
3838
import java.net.URLConnection;
3939
import java.util.ArrayList;
4040
import java.util.Collections;
@@ -365,7 +365,7 @@ private String read(final String url) throws IOException {
365365
URLConnection conn;
366366

367367
while (true) {
368-
final URLConnection conn2 = new URL(connectionURL).openConnection();
368+
final URLConnection conn2 = ResourceUtil.toURL(connectionURL).openConnection();
369369
if (!(conn2 instanceof HttpURLConnection)) {
370370
conn = conn2;
371371
break;

metamorph/src/main/java/org/metafacture/metamorph/maps/FileMap.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.metafacture.metamorph.maps;
1818

19+
import org.metafacture.commons.ResourceUtil;
1920
import org.metafacture.io.FileOpener;
2021
import org.metafacture.metamorph.api.MorphExecutionException;
2122
import org.metafacture.metamorph.api.helpers.AbstractReadOnlyMap;
@@ -229,7 +230,7 @@ private Optional<InputStream> openAsResource(final String file) {
229230
private Optional<InputStream> openAsUrl(final String file) {
230231
final URL url;
231232
try {
232-
url = new URL(file);
233+
url = ResourceUtil.toURL(file);
233234
}
234235
catch (final MalformedURLException e) {
235236
return Optional.empty();

metamorph/src/main/java/org/metafacture/metamorph/maps/RestMap.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@
1616

1717
package org.metafacture.metamorph.maps;
1818

19+
import org.metafacture.commons.ResourceUtil;
1920
import org.metafacture.metamorph.api.helpers.AbstractReadOnlyMap;
2021

2122
import java.io.BufferedReader;
2223
import java.io.IOException;
2324
import java.io.InputStream;
2425
import java.io.InputStreamReader;
25-
import java.net.URI;
2626
import java.net.URISyntaxException;
27-
import java.net.URL;
2827
import java.nio.charset.Charset;
2928
import java.util.regex.Matcher;
3029
import java.util.regex.Pattern;
@@ -73,7 +72,7 @@ public String get(final Object key) {
7372
}
7473

7574
private String readFromUrl(final String targetUrl) throws IOException, URISyntaxException {
76-
final InputStream inputStream = new URL(new URI(targetUrl.replace(" ", "%20")).toASCIIString()).openConnection()
75+
final InputStream inputStream = ResourceUtil.toURL(targetUrl.replace(" ", "%20")).openConnection()
7776
.getInputStream();
7877
try {
7978
final BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, Charset.forName(charsetName)));

0 commit comments

Comments
 (0)