Skip to content

Commit cfcb489

Browse files
author
cube
committed
Use non-deprecated URI constructor for URLs
1 parent ee7f1e0 commit cfcb489

8 files changed

Lines changed: 55 additions & 40 deletions

File tree

src/main/java/com/chaudhuri/cataloggenerator/Generator.java

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import java.io.OutputStream;
1717
import java.net.ConnectException;
1818
import java.net.MalformedURLException;
19+
import java.net.URI;
20+
import java.net.URISyntaxException;
1921
import java.net.URL;
2022
import java.nio.file.Files;
2123
import java.nio.file.LinkOption;
@@ -226,7 +228,7 @@ public void init() {
226228
* @param urlString the url to the OXZ
227229
* @return the parsed manifest
228230
*/
229-
ExpansionManifest getManifestFromUrl(String urlString) {
231+
ExpansionManifest getManifestFromUrl(String urlString) throws MalformedURLException, URISyntaxException {
230232
log.debug("getManifestFromUrl({})", urlString);
231233
if (cache == null) {
232234
throw new IllegalStateException("cache must not be null.");
@@ -315,7 +317,7 @@ ExpansionManifest getManifestFromOXZ(ZipInputStream zin, String urlString) {
315317
return expansion.getManifest();
316318
}
317319

318-
private boolean isOrdered(List<String> urls) throws MalformedURLException {
320+
private boolean isOrdered(List<String> urls) throws MalformedURLException, URISyntaxException {
319321
boolean result = true;
320322

321323
URL lastUrl = null;
@@ -328,7 +330,7 @@ private boolean isOrdered(List<String> urls) throws MalformedURLException {
328330
continue;
329331
}
330332

331-
URL thisUrl = new URL(url);
333+
URL thisUrl = new URI(url).toURL();
332334
if (lastUrl == null) {
333335
lastUrl = thisUrl;
334336
lastString = thisUrl.getHost() + thisUrl.getPort() + thisUrl.getPath() + thisUrl.getFile() + thisUrl.getProtocol();
@@ -355,7 +357,7 @@ private boolean isOrdered(List<String> urls) throws MalformedURLException {
355357
return result;
356358
}
357359

358-
private boolean checkAllHaveLastModified(List<String> urls) throws IOException {
360+
private boolean checkAllHaveLastModified(List<String> urls) throws IOException, URISyntaxException {
359361
boolean result = true;
360362

361363
int lineCount = 0;
@@ -413,17 +415,20 @@ List<ExpansionManifest> parseUrls(List<String> urls) throws Exception {
413415
.filter(url -> !url.startsWith("#"))
414416
.filter(url -> !url.isBlank())
415417
.map(url -> {
416-
ExpansionManifest em = getManifestFromUrl(url);
417-
if (em == null) {
418-
log.error("Could not parse expansion at {}", url);
419-
} else if (em.getIdentifier() == null || em.getIdentifier().isBlank()) {
420-
log.error("Invalid identifier in expansion {}", url);
421-
} else {
422-
log.debug("Parsed {}, found {}", url, em.getIdentifier());
418+
try {
419+
ExpansionManifest em = getManifestFromUrl(url);
420+
if (em == null) {
421+
log.error("Could not parse expansion at {}", url);
422+
} else if (em.getIdentifier() == null || em.getIdentifier().isBlank()) {
423+
log.error("Invalid identifier in expansion {}", url);
424+
} else {
425+
log.debug("Parsed {}, found {}", url, em.getIdentifier());
426+
}
427+
return em;
428+
} catch (Exception e) {
429+
log.error("Could not read url {}", url, e);
430+
return null;
423431
}
424-
425-
426-
return em;
427432
})
428433
.filter(m -> m != null)
429434
.collect(Collectors.toList());

src/main/java/com/chaudhuri/ooliteaddonscanner2/AddonsUtil.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.io.IOException;
2727
import java.io.InputStream;
2828
import java.net.ConnectException;
29+
import java.net.URISyntaxException;
2930
import java.nio.channels.Channels;
3031
import java.nio.channels.ReadableByteChannel;
3132
import java.nio.charset.Charset;
@@ -299,7 +300,7 @@ public static void readEquipment(String url, InputStream in, Registry registry,
299300
*
300301
* @param registry
301302
*/
302-
public static void readOolite(ExpansionCache cache, Registry registry) throws IOException, SAXException, ParserConfigurationException, RegistryException, TransformerException {
303+
public static void readOolite(ExpansionCache cache, Registry registry) throws IOException, SAXException, ParserConfigurationException, RegistryException, TransformerException, URISyntaxException {
303304
log.debug("readOolite({})", registry);
304305
if (cache == null) {
305306
throw new IllegalArgumentException(EXCEPTION_CACHE_MUST_NOT_BE_NULL);

src/main/java/com/chaudhuri/ooliteaddonscanner2/ExpansionCache.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import java.io.OutputStream;
1414
import java.net.HttpURLConnection;
1515
import java.net.MalformedURLException;
16+
import java.net.URI;
17+
import java.net.URISyntaxException;
1618
import java.net.URL;
1719
import java.net.URLConnection;
1820
import java.nio.file.DirectoryNotEmptyException;
@@ -183,13 +185,13 @@ private void cleanCache(File dir) throws IOException {
183185
* @return the manifest found, as it is returned by Genson
184186
* @throws IOException something went wrong
185187
*/
186-
public Map<String, Object> getOoliteManifest(String tag) throws IOException {
188+
public Map<String, Object> getOoliteManifest(String tag) throws IOException, URISyntaxException {
187189
log.debug("getOoliteManifest({})", tag);
188190
String repository = "OoliteProject/oolite";
189191
String urlStr = baseUrl + "/" + repository + "/releases/" + tag;
190192

191193
log.debug("Reading {}", urlStr);
192-
URL url = new URL(urlStr);
194+
URL url = new URI(urlStr).toURL();
193195
try ( InputStream in = url.openStream()) {
194196
return (Map<String, Object>) new Genson().deserialize(url.openStream(), Object.class);
195197
}
@@ -227,8 +229,8 @@ public String getOoliteDownloadUrl(Map<String, Object> manifest) {
227229
* @return the file
228230
* @throws MalformedURLException something went wrong
229231
*/
230-
public File getCachedFile(String url) throws MalformedURLException {
231-
URL u = new URL(url);
232+
public File getCachedFile(String url) throws MalformedURLException, URISyntaxException {
233+
URL u = new URI(url).toURL();
232234
return new File(cacheDIR, u.getHost() + File.separator + u.getFile());
233235
}
234236

@@ -238,7 +240,7 @@ public File getCachedFile(String url) throws MalformedURLException {
238240
* @param urls the urls to download
239241
* @throws IOException something went wrong
240242
*/
241-
public void update(List<String> urls) throws IOException {
243+
public void update(List<String> urls) throws IOException, MalformedURLException, URISyntaxException {
242244
int count = 0;
243245
for (String u: urls) {
244246
update(u);
@@ -305,7 +307,7 @@ private void doDownloadHttp(URL u, File local) throws IOException {
305307

306308
while (status != HttpURLConnection.HTTP_OK) {
307309
String newUrl = conn.getHeaderField("Location");
308-
conn = (HttpURLConnection)new URL(newUrl).openConnection();
310+
conn = (HttpURLConnection)new URI(newUrl).toURL().openConnection();
309311
conn.setReadTimeout(5000);
310312
status = conn.getResponseCode();
311313
log.info("HTTP status for {}: {}", newUrl, status);
@@ -348,7 +350,7 @@ private void doDownloadHttp(URL u, File local) throws IOException {
348350
* @return the last modified date
349351
* @throws IOException something went wrong
350352
*/
351-
private Date doCheckLastModified(URL u, int followRedirectsCount) throws IOException {
353+
private Date doCheckLastModified(URL u, int followRedirectsCount) throws IOException, URISyntaxException {
352354
URLConnection urlconnection = u.openConnection();
353355

354356
if (urlconnection instanceof HttpURLConnection) {
@@ -365,7 +367,7 @@ private Date doCheckLastModified(URL u, int followRedirectsCount) throws IOExcep
365367
log.debug("Redirect {} with {}", u, headers);
366368
if (followRedirectsCount == 0)
367369
throw new IllegalStateException("Received redirect but cannot follow");
368-
return doCheckLastModified(new URL(headers.get("Location").get(0)), followRedirectsCount - 1);
370+
return doCheckLastModified(new URI(headers.get("Location").get(0)).toURL(), followRedirectsCount - 1);
369371
} else {
370372
throw new IOException("HEAD " + u + " resulted in "+con.getResponseCode() + " " + con.getResponseMessage());
371373
}
@@ -383,13 +385,13 @@ private Date doCheckLastModified(URL u, int followRedirectsCount) throws IOExcep
383385
* @throws MalformedURLException
384386
* @throws IOException
385387
*/
386-
public void update(String url) throws IOException {
388+
public void update(String url) throws IOException, MalformedURLException, URISyntaxException {
387389
log.debug("update({})", url);
388390
if (url == null) {
389391
throw new IllegalArgumentException("url must not be null");
390392
}
391393

392-
URL u = new URL(url);
394+
URL u = new URI(url).toURL();
393395
File localFile = getCachedFile(url);
394396

395397
Duration age = Duration.between(Instant.ofEpochMilli(localFile.lastModified()), Instant.now());
@@ -417,7 +419,7 @@ public void update(String url) throws IOException {
417419
* @return the inputstream (to the cached file on disk)
418420
* @throws IOException something went wrong
419421
*/
420-
public InputStream getPluginInputStream(String url) throws IOException {
422+
public InputStream getPluginInputStream(String url) throws IOException, MalformedURLException, URISyntaxException {
421423
log.debug("getPluginInputStream({})", url);
422424
if (url == null) {
423425
throw new IllegalArgumentException("url must not be null");
@@ -437,14 +439,14 @@ public InputStream getPluginInputStream(String url) throws IOException {
437439
* @return the last-modified data, or null if not present
438440
* @throws IOException something went wrong
439441
*/
440-
public static Instant getLastModified(String url) throws IOException {
442+
public static Instant getLastModified(String url) throws IOException, URISyntaxException {
441443
log.debug("getLastModified({})", url);
442444
if (url == null) {
443445
throw new IllegalArgumentException("url must not be null");
444446
}
445447

446448
// run a custom HTTP request - see https://www.baeldung.com/java-http-request
447-
URL u = new URL(url);
449+
URL u = new URI(url).toURL();
448450
URLConnection uc = u.openConnection();
449451
if (uc instanceof HttpURLConnection) {
450452
HttpURLConnection con = (HttpURLConnection)uc;
@@ -492,7 +494,7 @@ public static Instant getLastModified(String url) throws IOException {
492494
* @param url
493495
* @throws MalformedURLException
494496
*/
495-
public void invalidate(String url) throws IOException {
497+
public void invalidate(String url) throws IOException, MalformedURLException, URISyntaxException {
496498
log.debug("invalidate({})", url);
497499
File cached = getCachedFile(url);
498500
Files.delete(cached.toPath());

src/main/java/com/chaudhuri/ooliteaddonscanner2/Main.java

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

55
import com.chaudhuri.ooliteaddonscanner2.model.CustomSearch;
66
import java.io.File;
7+
import java.net.URI;
78
import java.net.URL;
89
import java.util.Arrays;
910
import org.apache.commons.cli.CommandLine;
@@ -51,7 +52,7 @@ public static void main(String[] args) throws Exception {
5152
}
5253
if (commandline.hasOption("url")) {
5354
String urlStr = commandline.getOptionValue("url");
54-
scanner.setCatalogUrl(new URL(urlStr));
55+
scanner.setCatalogUrl(new URI(urlStr).toURL());
5556
}
5657
if (commandline.hasOption("out")) {
5758
String outputDirStr = commandline.getOptionValue("out");

src/main/java/com/chaudhuri/ooliteaddonscanner2/Scanner.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import java.io.InputStreamReader;
1818
import java.io.OutputStream;
1919
import java.net.MalformedURLException;
20+
import java.net.URI;
21+
import java.net.URISyntaxException;
2022
import java.net.URL;
2123
import java.time.Duration;
2224
import java.time.Instant;
@@ -65,8 +67,8 @@ public class Scanner implements Runnable {
6567
*
6668
* @throws MalformedURLException something went wrong
6769
*/
68-
public Scanner() throws MalformedURLException {
69-
this.catalogUrl = new URL("http://addons.oolite.space/api/1.0/overview");
70+
public Scanner() throws MalformedURLException, URISyntaxException {
71+
this.catalogUrl = new URI("http://addons.oolite.space/api/1.0/overview").toURL();
7072
}
7173

7274
/**

src/main/java/com/chaudhuri/ooliteaddonscanner2/Wiki.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.chaudhuri.ooliteaddonscanner2.model.Wikiworthy;
77
import java.io.IOException;
88
import java.io.InputStreamReader;
9+
import java.net.URI;
910
import java.net.URL;
1011
import java.nio.charset.StandardCharsets;
1112
import java.util.Map;
@@ -166,7 +167,7 @@ public static String wikiPageFor(String name) {
166167
urlStr = getPageUrl(name);
167168

168169
try {
169-
URL url = new URL(urlStr);
170+
URL url = new URI(urlStr).toURL();
170171
try (InputStreamReader rin = new InputStreamReader(url.openStream())) {
171172
StringBuilder sb = new StringBuilder();
172173
char[] buffer = new char[1024];

src/test/java/com/chaudhuri/cataloggenerator/GeneratorTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import java.io.File;
88
import java.io.FileInputStream;
99
import java.io.FileNotFoundException;
10+
import java.net.MalformedURLException;
11+
import java.net.URISyntaxException;
1012
import java.nio.file.FileSystems;
1113
import java.nio.file.Files;
1214
import java.nio.file.LinkOption;
@@ -205,7 +207,7 @@ public void testCall4() throws Exception {
205207
}
206208

207209
@Test
208-
public void testGetManifestFromUrl() {
210+
public void testGetManifestFromUrl() throws MalformedURLException, URISyntaxException {
209211
log.info("testGetManifestFromUrl");
210212

211213
Generator instance = new Generator();
@@ -220,7 +222,7 @@ public void testGetManifestFromUrl() {
220222
}
221223

222224
@Test
223-
public void testGetManifestFromUrl2() {
225+
public void testGetManifestFromUrl2() throws MalformedURLException, URISyntaxException {
224226
log.info("testGetManifestFromUrl2");
225227

226228
Generator instance = new Generator();

src/test/java/com/chaudhuri/ooliteaddonscanner2/ExpansionCacheTest.java

Lines changed: 6 additions & 5 deletions
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.net.MalformedURLException;
8+
import java.net.URISyntaxException;
89
import java.nio.file.Files;
910
import java.time.Duration;
1011
import java.time.Instant;
@@ -289,7 +290,7 @@ public void testCleanCache() throws IOException {
289290
}
290291

291292
@Test
292-
public void testGetLastModified() throws IOException {
293+
public void testGetLastModified() throws IOException, URISyntaxException {
293294
log.info("testGetLastModified");
294295

295296
File testCache = File.createTempFile("testCache", ".dir", tempCacheDir);
@@ -308,7 +309,7 @@ public void testGetLastModified() throws IOException {
308309
}
309310

310311
@Test
311-
public void testGetLastModified2() throws IOException {
312+
public void testGetLastModified2() throws IOException, URISyntaxException {
312313
log.info("testGetLastModified2");
313314

314315
File testCache = File.createTempFile("testCache", ".dir", tempCacheDir);
@@ -320,14 +321,14 @@ public void testGetLastModified2() throws IOException {
320321
try {
321322
cache.getLastModified("hallo");
322323
fail("expected exception");
323-
} catch (MalformedURLException e) {
324-
assertEquals("no protocol: hallo", e.getMessage());
324+
} catch (IllegalArgumentException e) {
325+
assertEquals("URI is not absolute", e.getMessage());
325326
log.debug("caught expected exception");
326327
}
327328
}
328329

329330
@Test
330-
public void testGetLastModified3() throws IOException {
331+
public void testGetLastModified3() throws IOException, URISyntaxException {
331332
log.info("testGetLastModified3");
332333

333334
File testCache = File.createTempFile("testCache", ".dir", tempCacheDir);

0 commit comments

Comments
 (0)