From 0ee5acc49400f5083c516c93b1137a01eaad8140 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Tue, 15 Oct 2024 15:45:47 +0200 Subject: [PATCH] Remove usage of deprecated URL constructor Although those URL constructors are only deprecated with Java 20, this already removes their usage. --- .../main/java/org/jivesoftware/smack/Smack.java | 3 ++- .../jivesoftware/smack/SmackConfiguration.java | 3 ++- .../smack/altconnections/HttpLookupMethod.java | 2 +- .../smack/provider/AbstractProvider.java | 17 ++++++++++++++++- .../httpfileupload/provider/SlotProvider.java | 12 +++++++----- .../org/jivesoftware/smackx/muc/RoomInfo.java | 9 +++++---- 6 files changed, 33 insertions(+), 13 deletions(-) diff --git a/smack-core/src/main/java/org/jivesoftware/smack/Smack.java b/smack-core/src/main/java/org/jivesoftware/smack/Smack.java index 6996e1ac9..0b7f173bb 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/Smack.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/Smack.java @@ -18,6 +18,7 @@ package org.jivesoftware.smack; import java.io.InputStream; import java.net.MalformedURLException; +import java.net.URI; import java.net.URL; import java.util.logging.Logger; @@ -35,7 +36,7 @@ public class Smack { static { try { - BUG_REPORT_URL = new URL("https://discourse.igniterealtime.org/c/smack/smack-support/9"); + BUG_REPORT_URL = URI.create("https://discourse.igniterealtime.org/c/smack/smack-support/9").toURL(); } catch (MalformedURLException e) { throw new ExceptionInInitializerError(e); } diff --git a/smack-core/src/main/java/org/jivesoftware/smack/SmackConfiguration.java b/smack-core/src/main/java/org/jivesoftware/smack/SmackConfiguration.java index ef1101cff..10d70228e 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/SmackConfiguration.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/SmackConfiguration.java @@ -18,6 +18,7 @@ package org.jivesoftware.smack; import java.net.MalformedURLException; +import java.net.URI; import java.net.URL; import java.util.ArrayList; import java.util.Collection; @@ -60,7 +61,7 @@ public final class SmackConfiguration { static { try { - SMACK_URL = new URL(SMACK_URL_STRING); + SMACK_URL = URI.create(SMACK_URL_STRING).toURL(); } catch (MalformedURLException e) { throw new IllegalStateException(e); } diff --git a/smack-core/src/main/java/org/jivesoftware/smack/altconnections/HttpLookupMethod.java b/smack-core/src/main/java/org/jivesoftware/smack/altconnections/HttpLookupMethod.java index e30530be7..f836c5cec 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/altconnections/HttpLookupMethod.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/altconnections/HttpLookupMethod.java @@ -117,7 +117,7 @@ public final class HttpLookupMethod { */ public static InputStream getXrdStream(DomainBareJid xmppServiceAddress) throws IOException { final String metadataUrl = "https://" + xmppServiceAddress + "/.well-known/host-meta"; - final URL putUrl = new URL(metadataUrl); + final URL putUrl = URI.create(metadataUrl).toURL(); final URLConnection urlConnection = putUrl.openConnection(); return urlConnection.getInputStream(); } diff --git a/smack-core/src/main/java/org/jivesoftware/smack/provider/AbstractProvider.java b/smack-core/src/main/java/org/jivesoftware/smack/provider/AbstractProvider.java index 2831ed864..665ccdb39 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/provider/AbstractProvider.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/provider/AbstractProvider.java @@ -1,6 +1,6 @@ /** * - * Copyright 2019-2021 Florian Schmaus + * Copyright 2019-2024 Florian Schmaus * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,10 +20,15 @@ import java.io.IOException; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.lang.reflect.TypeVariable; +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; import java.text.ParseException; import org.jivesoftware.smack.packet.Element; import org.jivesoftware.smack.parsing.SmackParsingException; +import org.jivesoftware.smack.parsing.SmackParsingException.SmackUriSyntaxParsingException; import org.jivesoftware.smack.xml.XmlPullParserException; public class AbstractProvider { @@ -106,4 +111,14 @@ public class AbstractProvider { return e; } + + public static URL toUrl(String string) throws SmackUriSyntaxParsingException, MalformedURLException { + URI uri; + try { + uri = new URI(string); + } catch (URISyntaxException e) { + throw new SmackUriSyntaxParsingException(e); + } + return uri.toURL(); + } } diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/httpfileupload/provider/SlotProvider.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/httpfileupload/provider/SlotProvider.java index f856e216a..f67786108 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/httpfileupload/provider/SlotProvider.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/httpfileupload/provider/SlotProvider.java @@ -1,6 +1,6 @@ /** * - * Copyright © 2017 Grigory Fedorov, 2017-2019 Florian Schmaus + * Copyright © 2017 Grigory Fedorov, 2017-2024 Florian Schmaus * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,12 +17,14 @@ package org.jivesoftware.smackx.httpfileupload.provider; import java.io.IOException; +import java.net.URI; import java.net.URL; import java.util.HashMap; import java.util.Map; import org.jivesoftware.smack.packet.IqData; import org.jivesoftware.smack.packet.XmlEnvironment; +import org.jivesoftware.smack.parsing.SmackParsingException.SmackUriSyntaxParsingException; import org.jivesoftware.smack.provider.IqProvider; import org.jivesoftware.smack.util.ParserUtils; import org.jivesoftware.smack.xml.XmlPullParser; @@ -42,7 +44,7 @@ import org.jivesoftware.smackx.httpfileupload.element.Slot_V0_2; public class SlotProvider extends IqProvider { @Override - public Slot parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException { + public Slot parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackUriSyntaxParsingException { final String namespace = parser.getNamespace(); final UploadService.Version version = HttpFileUploadManager.namespaceToVersion(namespace); @@ -63,7 +65,7 @@ public class SlotProvider extends IqProvider { switch (version) { case v0_2: String putUrlString = parser.nextText(); - putUrl = new URL(putUrlString); + putUrl = toUrl(putUrlString); break; case v0_3: putElementV04Content = parsePutElement_V0_4(parser); @@ -85,7 +87,7 @@ public class SlotProvider extends IqProvider { default: throw new AssertionError(); } - getUrl = new URL(getUrlString); + getUrl = toUrl(getUrlString); break; } break; @@ -114,7 +116,7 @@ public class SlotProvider extends IqProvider { final int initialDepth = parser.getDepth(); String putUrlString = parser.getAttributeValue(null, "url"); - URL putUrl = new URL(putUrlString); + URL putUrl = URI.create(putUrlString).toURL(); Map headers = null; outerloop: while (true) { diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/RoomInfo.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/RoomInfo.java index b43e33ae9..e1b11103f 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/RoomInfo.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/RoomInfo.java @@ -18,10 +18,11 @@ package org.jivesoftware.smackx.muc; import java.net.MalformedURLException; +import java.net.URI; +import java.net.URISyntaxException; import java.net.URL; import java.util.Collections; import java.util.List; -import java.util.logging.Level; import java.util.logging.Logger; import org.jivesoftware.smackx.disco.packet.DiscoverInfo; @@ -215,9 +216,9 @@ public class RoomInfo { if (urlField != null && !urlField.getValues().isEmpty()) { String urlString = urlField.getFirstValue(); try { - logs = new URL(urlString); - } catch (MalformedURLException e) { - LOGGER.log(Level.SEVERE, "Could not parse URL", e); + logs = new URI(urlString).toURL(); + } catch (MalformedURLException | URISyntaxException e) { + throw new IllegalArgumentException("Could not parse '" + urlString + "' to URL", e); } }