From 9ad83bada4e4a7f441babdc3c56085fdc7cc5055 Mon Sep 17 00:00:00 2001 From: Mark Herwege Date: Sun, 12 Jan 2025 22:18:10 +0100 Subject: [PATCH] Remove calls to deprecated URL constructor (#4551) Signed-off-by: Mark Herwege --- .../community/CommunityKarafAddonHandler.java | 13 +++++++------ .../CommunityBlockLibaryAddonHandler.java | 12 +++++++++--- .../community/CommunityBundleAddonHandler.java | 14 ++++++++------ .../CommunityMarketplaceAddonService.java | 6 +++--- .../CommunityRuleTemplateAddonHandler.java | 9 ++++++++- .../CommunityTransformationAddonHandler.java | 9 ++++++++- .../CommunityUIWidgetAddonHandler.java | 12 +++++++++--- .../internal/json/JsonAddonService.java | 18 +++++++++++++++--- .../internal/OAuthClientServiceImpl.java | 6 ++++-- .../commands/AutomationCommandImport.java | 8 +++++--- .../commands/AutomationCommandRemove.java | 8 +++++--- .../addon/upnp/tests/UpnpAddonFinderTests.java | 12 ++++-------- .../internal/EphemerisManagerImpl.java | 5 +++-- .../core/io/net/http/PEMTrustManager.java | 8 +++++++- .../storage/json/internal/JsonStorageTest.java | 6 ++++-- .../ui/internal/proxy/ProxyServletService.java | 11 ++++++++--- .../thing/binding/firmware/FirmwareTest.java | 7 ++++--- .../openhab/core/thing/firmware/Constants.java | 2 +- 18 files changed, 112 insertions(+), 54 deletions(-) diff --git a/bundles/org.openhab.core.addon.marketplace.karaf/src/main/java/org/openhab/core/addon/marketplace/karaf/internal/community/CommunityKarafAddonHandler.java b/bundles/org.openhab.core.addon.marketplace.karaf/src/main/java/org/openhab/core/addon/marketplace/karaf/internal/community/CommunityKarafAddonHandler.java index bc611964fa8..037e4021794 100644 --- a/bundles/org.openhab.core.addon.marketplace.karaf/src/main/java/org/openhab/core/addon/marketplace/karaf/internal/community/CommunityKarafAddonHandler.java +++ b/bundles/org.openhab.core.addon.marketplace.karaf/src/main/java/org/openhab/core/addon/marketplace/karaf/internal/community/CommunityKarafAddonHandler.java @@ -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 boolean isInstalled(String addonId) { @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 diff --git a/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/internal/community/CommunityBlockLibaryAddonHandler.java b/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/internal/community/CommunityBlockLibaryAddonHandler.java index 1c9beb9e456..700ccc480bd 100644 --- a/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/internal/community/CommunityBlockLibaryAddonHandler.java +++ b/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/internal/community/CommunityBlockLibaryAddonHandler.java @@ -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 void uninstall(Addon addon) throws MarketplaceHandlerException { } 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); } diff --git a/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/internal/community/CommunityBundleAddonHandler.java b/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/internal/community/CommunityBundleAddonHandler.java index 596c6f381f0..4db5e3c5ec6 100644 --- a/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/internal/community/CommunityBundleAddonHandler.java +++ b/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/internal/community/CommunityBundleAddonHandler.java @@ -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 boolean isInstalled(String id) { @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 diff --git a/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/internal/community/CommunityMarketplaceAddonService.java b/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/internal/community/CommunityMarketplaceAddonService.java index 294efeb14d3..6222584734f 100644 --- a/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/internal/community/CommunityMarketplaceAddonService.java +++ b/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/internal/community/CommunityMarketplaceAddonService.java @@ -163,7 +163,7 @@ protected List getRemoteAddons() { try { List 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 @@ protected List getRemoteAddons() { 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 @@ protected List getRemoteAddons() { // 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) { diff --git a/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/internal/community/CommunityRuleTemplateAddonHandler.java b/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/internal/community/CommunityRuleTemplateAddonHandler.java index 1103664cc7e..82e4e844f2b 100644 --- a/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/internal/community/CommunityRuleTemplateAddonHandler.java +++ b/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/internal/community/CommunityRuleTemplateAddonHandler.java @@ -17,6 +17,8 @@ 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 void uninstall(Addon addon) throws MarketplaceHandlerException { } 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); } diff --git a/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/internal/community/CommunityTransformationAddonHandler.java b/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/internal/community/CommunityTransformationAddonHandler.java index bba6401b662..ecf8988e5d6 100644 --- a/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/internal/community/CommunityTransformationAddonHandler.java +++ b/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/internal/community/CommunityTransformationAddonHandler.java @@ -17,6 +17,8 @@ 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 void uninstall(Addon addon) throws MarketplaceHandlerException { } 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); } diff --git a/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/internal/community/CommunityUIWidgetAddonHandler.java b/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/internal/community/CommunityUIWidgetAddonHandler.java index 4ae62430e55..fa1492c4621 100644 --- a/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/internal/community/CommunityUIWidgetAddonHandler.java +++ b/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/internal/community/CommunityUIWidgetAddonHandler.java @@ -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 void uninstall(Addon addon) throws MarketplaceHandlerException { } 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); } diff --git a/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/internal/json/JsonAddonService.java b/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/internal/json/JsonAddonService.java index 895de40057f..e91704df254 100644 --- a/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/internal/json/JsonAddonService.java +++ b/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/internal/json/JsonAddonService.java @@ -18,7 +18,9 @@ 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 JsonAddonService(@Reference EventPublisher eventPublisher, @Reference Sto public void modified(@Nullable Map 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 String getName() { protected List 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 @@ protected List getRemoteAddons() { } /** - * 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} diff --git a/bundles/org.openhab.core.auth.oauth2client/src/main/java/org/openhab/core/auth/oauth2client/internal/OAuthClientServiceImpl.java b/bundles/org.openhab.core.auth.oauth2client/src/main/java/org/openhab/core/auth/oauth2client/internal/OAuthClientServiceImpl.java index a7a9a8f78ee..7a817e02a60 100644 --- a/bundles/org.openhab.core.auth.oauth2client/src/main/java/org/openhab/core/auth/oauth2client/internal/OAuthClientServiceImpl.java +++ b/bundles/org.openhab.core.auth.oauth2client/src/main/java/org/openhab/core/auth/oauth2client/internal/OAuthClientServiceImpl.java @@ -16,6 +16,8 @@ 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 String getAuthorizationUrl(@Nullable String redirectURI, @Nullable String 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 String extractAuthCodeFromAuthResponse(String redirectURLwithParams) thro 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); } } diff --git a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/commands/AutomationCommandImport.java b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/commands/AutomationCommandImport.java index 047cd2aa95d..27e05f1476e 100644 --- a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/commands/AutomationCommandImport.java +++ b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/commands/AutomationCommandImport.java @@ -14,6 +14,8 @@ 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 String execute() { */ 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) { } } } diff --git a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/commands/AutomationCommandRemove.java b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/commands/AutomationCommandRemove.java index 921bf756757..619f89e3ad3 100644 --- a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/commands/AutomationCommandRemove.java +++ b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/commands/AutomationCommandRemove.java @@ -14,6 +14,8 @@ 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 String execute() { */ 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) { } } } diff --git a/bundles/org.openhab.core.config.discovery.addon.upnp/src/test/java/org/openhab/core/config/discovery/addon/upnp/tests/UpnpAddonFinderTests.java b/bundles/org.openhab.core.config.discovery.addon.upnp/src/test/java/org/openhab/core/config/discovery/addon/upnp/tests/UpnpAddonFinderTests.java index 0030fece49a..68c5f4ab28d 100644 --- a/bundles/org.openhab.core.config.discovery.addon.upnp/src/test/java/org/openhab/core/config/discovery/addon/upnp/tests/UpnpAddonFinderTests.java +++ b/bundles/org.openhab.core.config.discovery.addon.upnp/src/test/java/org/openhab/core/config/discovery/addon/upnp/tests/UpnpAddonFinderTests.java @@ -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 @@ private void setupMockUpnpService() { 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"); } diff --git a/bundles/org.openhab.core.ephemeris/src/main/java/org/openhab/core/ephemeris/internal/EphemerisManagerImpl.java b/bundles/org.openhab.core.ephemeris/src/main/java/org/openhab/core/ephemeris/internal/EphemerisManagerImpl.java index 9ac9ea86784..394932c7099 100644 --- a/bundles/org.openhab.core.ephemeris/src/main/java/org/openhab/core/ephemeris/internal/EphemerisManagerImpl.java +++ b/bundles/org.openhab.core.ephemeris/src/main/java/org/openhab/core/ephemeris/internal/EphemerisManagerImpl.java @@ -17,6 +17,7 @@ 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 @@ protected void modified(Map config) { 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 { diff --git a/bundles/org.openhab.core.io.net/src/main/java/org/openhab/core/io/net/http/PEMTrustManager.java b/bundles/org.openhab.core.io.net/src/main/java/org/openhab/core/io/net/http/PEMTrustManager.java index 9194988ffc1..2821e9fc27c 100644 --- a/bundles/org.openhab.core.io.net/src/main/java/org/openhab/core/io/net/http/PEMTrustManager.java +++ b/bundles/org.openhab.core.io.net/src/main/java/org/openhab/core/io/net/http/PEMTrustManager.java @@ -19,6 +19,8 @@ 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 static PEMTrustManager getInstanceFromFile(String path) throws FileNotFou * @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()); + } } /** diff --git a/bundles/org.openhab.core.storage.json/src/test/java/org/openhab/core/storage/json/internal/JsonStorageTest.java b/bundles/org.openhab.core.storage.json/src/test/java/org/openhab/core/storage/json/internal/JsonStorageTest.java index a6d914a3db9..a4c163af9c7 100644 --- a/bundles/org.openhab.core.storage.json/src/test/java/org/openhab/core/storage/json/internal/JsonStorageTest.java +++ b/bundles/org.openhab.core.storage.json/src/test/java/org/openhab/core/storage/json/internal/JsonStorageTest.java @@ -18,6 +18,8 @@ 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 DummyObject() { 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); } } diff --git a/bundles/org.openhab.core.ui/src/main/java/org/openhab/core/ui/internal/proxy/ProxyServletService.java b/bundles/org.openhab.core.ui/src/main/java/org/openhab/core/ui/internal/proxy/ProxyServletService.java index 3d713f83199..3ccfc339f66 100644 --- a/bundles/org.openhab.core.ui/src/main/java/org/openhab/core/ui/internal/proxy/ProxyServletService.java +++ b/bundles/org.openhab.core.ui/src/main/java/org/openhab/core/ui/internal/proxy/ProxyServletService.java @@ -17,7 +17,6 @@ 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 @@ URI uriFromRequest(HttpServletRequest request) { } 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; } /** diff --git a/itests/org.openhab.core.thing.tests/src/main/java/org/openhab/core/thing/binding/firmware/FirmwareTest.java b/itests/org.openhab.core.thing.tests/src/main/java/org/openhab/core/thing/binding/firmware/FirmwareTest.java index baa32899747..48c85ef0063 100644 --- a/itests/org.openhab.core.thing.tests/src/main/java/org/openhab/core/thing/binding/firmware/FirmwareTest.java +++ b/itests/org.openhab.core.thing.tests/src/main/java/org/openhab/core/thing/binding/firmware/FirmwareTest.java @@ -20,6 +20,7 @@ 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 void testFirmwareBuilder() throws MalformedURLException { 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 void assertDuplicateFirmwaresWithEquals() throws IOException { 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 properties = Map.of("prop1", "val1", "prop2", "val2"); @@ -330,7 +331,7 @@ public void duplicateFirmwaresHashWithoutRestrictions() throws IOException { 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 properties = Map.of("prop1", "val1", "prop2", "val2"); diff --git a/itests/org.openhab.core.thing.tests/src/main/java/org/openhab/core/thing/firmware/Constants.java b/itests/org.openhab.core.thing.tests/src/main/java/org/openhab/core/thing/firmware/Constants.java index b19313ed95e..6c87394ae64 100644 --- a/itests/org.openhab.core.thing.tests/src/main/java/org/openhab/core/thing/firmware/Constants.java +++ b/itests/org.openhab.core.thing.tests/src/main/java/org/openhab/core/thing/firmware/Constants.java @@ -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); }