Remove calls to deprecated URL constructor (#4551)

Signed-off-by: Mark Herwege <mark.herwege@telenet.be>
pull/4558/head
Mark Herwege 2025-01-12 22:18:10 +01:00 committed by GitHub
parent 229d87263f
commit 9ad83bada4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
18 changed files with 112 additions and 54 deletions

View File

@ -12,13 +12,13 @@
*/
package org.openhab.core.addon.marketplace.karaf.internal.community;
import static org.openhab.core.addon.marketplace.MarketplaceConstants.KAR_CONTENT_TYPE;
import static org.openhab.core.addon.marketplace.MarketplaceConstants.KAR_DOWNLOAD_URL_PROPERTY;
import static org.openhab.core.addon.marketplace.MarketplaceConstants.*;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
@ -108,13 +108,14 @@ public class CommunityKarafAddonHandler implements MarketplaceAddonHandler {
@Override
public void install(Addon addon) throws MarketplaceHandlerException {
URL sourceUrl;
try {
URL sourceUrl = new URL((String) addon.getProperties().get(KAR_DOWNLOAD_URL_PROPERTY));
addKarToCache(addon.getUid(), sourceUrl);
installFromCache(addon.getUid());
} catch (MalformedURLException e) {
sourceUrl = (new URI((String) addon.getProperties().get(KAR_DOWNLOAD_URL_PROPERTY))).toURL();
} catch (IllegalArgumentException | MalformedURLException | URISyntaxException e) {
throw new MarketplaceHandlerException("Malformed source URL: " + e.getMessage(), e);
}
addKarToCache(addon.getUid(), sourceUrl);
installFromCache(addon.getUid());
}
@Override

View File

@ -12,12 +12,13 @@
*/
package org.openhab.core.addon.marketplace.internal.community;
import static org.openhab.core.addon.marketplace.MarketplaceConstants.BLOCKLIBRARIES_CONTENT_TYPE;
import static org.openhab.core.addon.marketplace.MarketplaceConstants.YAML_DOWNLOAD_URL_PROPERTY;
import static org.openhab.core.addon.marketplace.MarketplaceConstants.*;
import static org.openhab.core.addon.marketplace.internal.community.CommunityMarketplaceAddonService.YAML_CONTENT_PROPERTY;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
@ -102,7 +103,12 @@ public class CommunityBlockLibaryAddonHandler implements MarketplaceAddonHandler
}
private String getWidgetFromURL(String urlString) throws IOException {
URL u = new URL(urlString);
URL u;
try {
u = (new URI(urlString)).toURL();
} catch (IllegalArgumentException | URISyntaxException e) {
throw new IOException(e);
}
try (InputStream in = u.openStream()) {
return new String(in.readAllBytes(), StandardCharsets.UTF_8);
}

View File

@ -12,10 +12,11 @@
*/
package org.openhab.core.addon.marketplace.internal.community;
import static org.openhab.core.addon.marketplace.MarketplaceConstants.JAR_CONTENT_TYPE;
import static org.openhab.core.addon.marketplace.MarketplaceConstants.JAR_DOWNLOAD_URL_PROPERTY;
import static org.openhab.core.addon.marketplace.MarketplaceConstants.*;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.List;
import java.util.concurrent.ScheduledExecutorService;
@ -73,13 +74,14 @@ public class CommunityBundleAddonHandler extends MarketplaceBundleInstaller impl
@Override
public void install(Addon addon) throws MarketplaceHandlerException {
URL sourceUrl;
try {
URL sourceUrl = new URL((String) addon.getProperties().get(JAR_DOWNLOAD_URL_PROPERTY));
addBundleToCache(addon.getUid(), sourceUrl);
installFromCache(bundleContext, addon.getUid());
} catch (MalformedURLException e) {
sourceUrl = (new URI((String) addon.getProperties().get(JAR_DOWNLOAD_URL_PROPERTY))).toURL();
} catch (IllegalArgumentException | MalformedURLException | URISyntaxException e) {
throw new MarketplaceHandlerException("Malformed source URL: " + e.getMessage(), e);
}
addBundleToCache(addon.getUid(), sourceUrl);
installFromCache(bundleContext, addon.getUid());
}
@Override

View File

@ -163,7 +163,7 @@ public class CommunityMarketplaceAddonService extends AbstractRemoteAddonService
try {
List<DiscourseCategoryResponseDTO> pages = new ArrayList<>();
URL url = new URL(COMMUNITY_MARKETPLACE_URL);
URL url = URI.create(COMMUNITY_MARKETPLACE_URL).toURL();
int pageNb = 1;
while (url != null) {
URLConnection connection = url.openConnection();
@ -180,7 +180,7 @@ public class CommunityMarketplaceAddonService extends AbstractRemoteAddonService
if (parsed.topicList.moreTopicsUrl != null) {
// Discourse URL for next page is wrong
url = new URL(COMMUNITY_MARKETPLACE_URL + "?page=" + pageNb++);
url = URI.create(COMMUNITY_MARKETPLACE_URL + "?page=" + pageNb++).toURL();
} else {
url = null;
}
@ -215,7 +215,7 @@ public class CommunityMarketplaceAddonService extends AbstractRemoteAddonService
// retrieve from remote
try {
URL url = new URL(String.format("%s%s", COMMUNITY_TOPIC_URL, uid.replace(ADDON_ID_PREFIX, "")));
URL url = URI.create(COMMUNITY_TOPIC_URL + uid.replace(ADDON_ID_PREFIX, "")).toURL();
URLConnection connection = url.openConnection();
connection.addRequestProperty("Accept", "application/json");
if (this.apiKey != null) {

View File

@ -17,6 +17,8 @@ import static org.openhab.core.addon.marketplace.internal.community.CommunityMar
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
@ -99,7 +101,12 @@ public class CommunityRuleTemplateAddonHandler implements MarketplaceAddonHandle
}
private String getTemplateFromURL(String urlString) throws IOException {
URL u = new URL(urlString);
URL u;
try {
u = (new URI(urlString)).toURL();
} catch (IllegalArgumentException | URISyntaxException e) {
throw new IOException(e);
}
try (InputStream in = u.openStream()) {
return new String(in.readAllBytes(), StandardCharsets.UTF_8);
}

View File

@ -17,6 +17,8 @@ import static org.openhab.core.addon.marketplace.internal.community.CommunityMar
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
@ -131,7 +133,12 @@ public class CommunityTransformationAddonHandler implements MarketplaceAddonHand
}
private String downloadTransformation(String urlString) throws IOException {
URL u = new URL(urlString);
URL u;
try {
u = (new URI(urlString)).toURL();
} catch (IllegalArgumentException | URISyntaxException e) {
throw new IOException(e);
}
try (InputStream in = u.openStream()) {
return new String(in.readAllBytes(), StandardCharsets.UTF_8);
}

View File

@ -12,12 +12,13 @@
*/
package org.openhab.core.addon.marketplace.internal.community;
import static org.openhab.core.addon.marketplace.MarketplaceConstants.UIWIDGETS_CONTENT_TYPE;
import static org.openhab.core.addon.marketplace.MarketplaceConstants.YAML_DOWNLOAD_URL_PROPERTY;
import static org.openhab.core.addon.marketplace.MarketplaceConstants.*;
import static org.openhab.core.addon.marketplace.internal.community.CommunityMarketplaceAddonService.YAML_CONTENT_PROPERTY;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
@ -106,7 +107,12 @@ public class CommunityUIWidgetAddonHandler implements MarketplaceAddonHandler {
}
private String getWidgetFromURL(String urlString) throws IOException {
URL u = new URL(urlString);
URL u;
try {
u = (new URI(urlString)).toURL();
} catch (IllegalArgumentException | URISyntaxException e) {
throw new IOException(e);
}
try (InputStream in = u.openStream()) {
return new String(in.readAllBytes(), StandardCharsets.UTF_8);
}

View File

@ -18,7 +18,9 @@ import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.reflect.Type;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Arrays;
@ -91,13 +93,23 @@ public class JsonAddonService extends AbstractRemoteAddonService {
public void modified(@Nullable Map<String, Object> config) {
if (config != null) {
String urls = ConfigParser.valueAsOrElse(config.get(CONFIG_URLS), String.class, "");
addonServiceUrls = Arrays.asList(urls.split("\\|"));
addonServiceUrls = Arrays.asList(urls.split("\\|")).stream().filter(this::isValidUrl).toList();
showUnstable = ConfigParser.valueAsOrElse(config.get(CONFIG_SHOW_UNSTABLE), Boolean.class, false);
cachedRemoteAddons.invalidateValue();
refreshSource();
}
}
private boolean isValidUrl(String urlString) {
try {
(new URI(urlString)).toURL();
} catch (IllegalArgumentException | URISyntaxException | MalformedURLException e) {
logger.warn("JSON Addon Service invalid URL: {}", urlString);
return false;
}
return true;
}
@Override
@Reference(cardinality = ReferenceCardinality.AT_LEAST_ONE, policy = ReferencePolicy.DYNAMIC)
protected void addAddonHandler(MarketplaceAddonHandler handler) {
@ -124,7 +136,7 @@ public class JsonAddonService extends AbstractRemoteAddonService {
protected List<Addon> getRemoteAddons() {
return addonServiceUrls.stream().map(urlString -> {
try {
URL url = new URL(urlString);
URL url = URI.create(urlString).toURL();
URLConnection connection = url.openConnection();
connection.addRequestProperty("Accept", "application/json");
try (Reader reader = new InputStreamReader(connection.getInputStream())) {
@ -139,7 +151,7 @@ public class JsonAddonService extends AbstractRemoteAddonService {
}
/**
* Check if the addon UID is present and the entry is eitehr stable or unstable add-ons are requested
* Check if the addon UID is present and the entry is either stable or unstable add-ons are requested
*
* @param addonEntry the add-on entry to check
* @return {@code true} if the add-on entry should be processed, {@code false otherwise}

View File

@ -16,6 +16,8 @@ import static org.openhab.core.auth.oauth2client.internal.Keyword.*;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.security.GeneralSecurityException;
import java.time.Instant;
@ -168,7 +170,7 @@ public class OAuthClientServiceImpl implements OAuthClientService {
public String extractAuthCodeFromAuthResponse(String redirectURLwithParams) throws OAuthException {
// parse the redirectURL
try {
URL redirectURLObject = new URL(redirectURLwithParams);
URL redirectURLObject = (new URI(redirectURLwithParams)).toURL();
UrlEncoded urlEncoded = new UrlEncoded(redirectURLObject.getQuery());
String stateFromRedirectURL = urlEncoded.getValue(STATE, 0); // may contain multiple...
@ -186,7 +188,7 @@ public class OAuthClientServiceImpl implements OAuthClientService {
throw new OAuthException(String.format("state from redirectURL is incorrect. Expected: %s Found: %s",
persistedParams.state, stateFromRedirectURL));
}
} catch (MalformedURLException e) {
} catch (IllegalArgumentException | MalformedURLException | URISyntaxException e) {
throw new OAuthException("Redirect URL is malformed", e);
}
}

View File

@ -14,6 +14,8 @@ package org.openhab.core.automation.internal.commands;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import org.eclipse.jdt.annotation.NonNullByDefault;
@ -101,13 +103,13 @@ public class AutomationCommandImport extends AutomationCommand {
*/
private @Nullable URL initURL(String parameterValue) {
try {
return new URL(parameterValue);
} catch (MalformedURLException mue) {
return (new URI(parameterValue)).toURL();
} catch (MalformedURLException | URISyntaxException mue) {
File f = new File(parameterValue);
if (f.isFile()) {
try {
return f.toURI().toURL();
} catch (MalformedURLException e) {
} catch (IllegalArgumentException | MalformedURLException e) {
}
}
}

View File

@ -14,6 +14,8 @@ package org.openhab.core.automation.internal.commands;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import org.eclipse.jdt.annotation.NonNullByDefault;
@ -96,13 +98,13 @@ public class AutomationCommandRemove extends AutomationCommand {
*/
private @Nullable URL initURL(String parameterValue) {
try {
return new URL(parameterValue);
} catch (MalformedURLException mue) {
return (new URI(parameterValue)).toURL();
} catch (MalformedURLException | URISyntaxException mue) {
File f = new File(parameterValue);
if (f.isFile()) {
try {
return f.toURI().toURL();
} catch (MalformedURLException e) {
} catch (IllegalArgumentException | MalformedURLException e) {
}
}
}

View File

@ -12,16 +12,12 @@
*/
package org.openhab.core.config.discovery.addon.upnp.tests;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.ArrayList;
@ -85,7 +81,7 @@ public class UpnpAddonFinderTests {
upnpService = mock(UpnpService.class, Mockito.RETURNS_DEEP_STUBS);
URL url = null;
try {
url = new URL("http://www.openhab.org/");
url = URI.create("http://www.openhab.org/").toURL();
} catch (MalformedURLException e) {
fail("MalformedURLException");
}

View File

@ -17,6 +17,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
@ -224,8 +225,8 @@ public class EphemerisManagerImpl implements EphemerisManager, ConfigOptionProvi
private URL getUrl(String filename) throws FileNotFoundException {
if (Files.exists(Paths.get(filename))) {
try {
return new URL("file:" + filename);
} catch (MalformedURLException e) {
return (new URI("file:" + filename)).toURL();
} catch (IllegalArgumentException | MalformedURLException | URISyntaxException e) {
throw new FileNotFoundException(e.getMessage());
}
} else {

View File

@ -19,6 +19,8 @@ import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
@ -113,7 +115,11 @@ public final class PEMTrustManager extends X509ExtendedTrustManager {
* @throws CertificateInstantiationException
*/
public static PEMTrustManager getInstanceFromServer(String url) throws MalformedURLException, CertificateException {
return getInstanceFromServer(new URL(url));
try {
return getInstanceFromServer((new URI(url)).toURL());
} catch (IllegalArgumentException | URISyntaxException e) {
throw new MalformedURLException(e.getMessage());
}
}
/**

View File

@ -18,6 +18,8 @@ import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.time.Instant;
@ -306,8 +308,8 @@ public class JsonStorageTest extends JavaTest {
private static URL newURL(String url) {
try {
return new URL(url);
} catch (MalformedURLException e) {
return (new URI(url)).toURL();
} catch (MalformedURLException | URISyntaxException e) {
throw new IllegalArgumentException(e);
}
}

View File

@ -17,7 +17,6 @@ import java.io.Serial;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Base64;
import java.util.Hashtable;
import java.util.List;
@ -295,9 +294,15 @@ public class ProxyServletService extends HttpServlet {
}
private URI createURIFromString(String url) throws MalformedURLException, URISyntaxException {
// URI in this context should be valid URL. Therefore before creating URI, create URL,
URI uri = new URI(url);
// URI in this context should be valid URL. Therefore before returning URI, create URL,
// which validates the string.
return new URL(url).toURI();
try {
uri.toURL();
} catch (IllegalArgumentException e) {
throw new MalformedURLException(e.getMessage());
}
return uri;
}
/**

View File

@ -20,6 +20,7 @@ import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.Map;
@ -91,7 +92,7 @@ public class FirmwareTest extends JavaOSGiTest {
String description = "description";
String model = "model";
boolean modelRestricted = true;
URL onlineChangelog = new URL("http://online.changelog");
URL onlineChangelog = URI.create("http://online.changelog").toURL();
String prerequisiteVersion = "0.0.9";
String md5hash = "123abc";
String vendor = "vendor";
@ -304,7 +305,7 @@ public class FirmwareTest extends JavaOSGiTest {
String description = "description";
String model = "model";
boolean modelRestricted = true;
URL onlineChangelog = new URL("https://secure.com/changelog");
URL onlineChangelog = URI.create("https://secure.com/changelog").toURL();
String prerequisiteVersion = "0.1";
String vendor = "vendor";
Map<String, String> properties = Map.of("prop1", "val1", "prop2", "val2");
@ -330,7 +331,7 @@ public class FirmwareTest extends JavaOSGiTest {
String description = "description";
String model = "model";
boolean modelRestricted = true;
URL onlineChangelog = new URL("https://secure.com/changelog");
URL onlineChangelog = URI.create("https://secure.com/changelog").toURL();
String prerequisiteVersion = "0.1";
String vendor = "vendor";
Map<String, String> properties = Map.of("prop1", "val1", "prop2", "val2");

View File

@ -133,7 +133,7 @@ public class Constants {
private static URL newURL(String url) {
try {
return new URL(url);
return URI.create(url).toURL();
} catch (MalformedURLException e) {
throw new UncheckedIOException(e);
}