Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove calls to deprecated URL constructor #4551

Merged
merged 4 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ protected List<Addon> getRemoteAddons() {
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();
Expand All @@ -180,7 +180,7 @@ protected List<Addon> 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;
}
Expand Down Expand Up @@ -215,7 +215,7 @@ protected List<Addon> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -91,13 +93,23 @@ public JsonAddonService(@Reference EventPublisher eventPublisher, @Reference Sto
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) {
Expand All @@ -124,7 +136,7 @@ public String getName() {
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())) {
Expand All @@ -139,7 +151,7 @@ protected List<Addon> 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}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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...
Expand All @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -224,8 +225,8 @@ protected void modified(Map<String, Object> 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 {
Expand Down
Loading