This is an automated email from the ASF dual-hosted git repository. jleroux pushed a commit to branch trunk in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git
The following commit(s) were added to refs/heads/trunk by this push: new fb6796f6cd Improved: URL constructor is deprecated in Java 20 (OFBIZ-12881) fb6796f6cd is described below commit fb6796f6cd74c95f682ab0788ae05412e1a26bf7 Author: Jacques Le Roux <jacques.le.r...@les7arts.com> AuthorDate: Mon Jan 29 11:14:04 2024 +0100 Improved: URL constructor is deprecated in Java 20 (OFBIZ-12881) Explanation: https://inside.java/2023/02/15/quality-heads-up/ I have kept the existing log information in UrlLoader, NetConverters, UtilURL and EmailServices classes --- .../accounting/thirdparty/eway/GatewayConnector.java | 3 ++- .../accounting/thirdparty/paypal/PayPalEvents.java | 3 ++- .../ofbiz/content/data/DataResourceWorker.java | 7 ++++--- .../java/org/apache/ofbiz/base/config/UrlLoader.java | 11 +++++++---- .../apache/ofbiz/base/conversion/NetConverters.java | 8 ++++++-- .../base/location/StandardUrlLocationResolver.java | 4 +++- .../java/org/apache/ofbiz/base/util/HttpClient.java | 2 +- .../java/org/apache/ofbiz/base/util/UtilURL.java | 20 +++++++++++++------- .../org/apache/ofbiz/base/conversion/MiscTests.java | 3 ++- .../org/apache/ofbiz/common/email/EmailServices.java | 9 ++++++--- .../org/apache/ofbiz/service/ModelServiceTest.groovy | 5 +++-- .../ofbiz/webtools/datafile/ViewDataFile.groovy | 4 ++-- .../webtools/artifactinfo/ArtifactInfoFactory.java | 7 ++++--- 13 files changed, 55 insertions(+), 31 deletions(-) diff --git a/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/eway/GatewayConnector.java b/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/eway/GatewayConnector.java index 8694a81f20..38dd02d266 100644 --- a/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/eway/GatewayConnector.java +++ b/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/eway/GatewayConnector.java @@ -26,6 +26,7 @@ import java.net.HttpURLConnection; import java.net.URL; import org.apache.ofbiz.base.util.Debug; +import org.apache.ofbiz.base.util.UtilURL; /** * Handles connections to the eWay servers. @@ -83,7 +84,7 @@ public class GatewayConnector { HttpURLConnection connection = null; try { // connect to the gateway - URL u = new URL(serverurl); + URL u = UtilURL.fromUrlString(serverurl); connection = (HttpURLConnection) (u.openConnection()); connection.setDoOutput(true); connection.setDoInput(true); diff --git a/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/paypal/PayPalEvents.java b/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/paypal/PayPalEvents.java index fd02b243cf..b49c82e29d 100644 --- a/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/paypal/PayPalEvents.java +++ b/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/paypal/PayPalEvents.java @@ -43,6 +43,7 @@ import org.apache.ofbiz.base.util.UtilFormatOut; import org.apache.ofbiz.base.util.UtilHttp; import org.apache.ofbiz.base.util.UtilMisc; import org.apache.ofbiz.base.util.UtilProperties; +import org.apache.ofbiz.base.util.UtilURL; import org.apache.ofbiz.base.util.UtilValidate; import org.apache.ofbiz.entity.Delegator; import org.apache.ofbiz.entity.GenericEntityException; @@ -240,7 +241,7 @@ public class PayPalEvents { // send off the confirm request String confirmResp = null; String str = UtilHttp.urlEncodeArgs(parametersMap); - URL u = new URL(redirectUrl); + URL u = UtilURL.fromUrlString(redirectUrl); URLConnection uc = u.openConnection(); uc.setDoOutput(true); uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); diff --git a/applications/content/src/main/java/org/apache/ofbiz/content/data/DataResourceWorker.java b/applications/content/src/main/java/org/apache/ofbiz/content/data/DataResourceWorker.java index 18bd81734b..6e03054547 100644 --- a/applications/content/src/main/java/org/apache/ofbiz/content/data/DataResourceWorker.java +++ b/applications/content/src/main/java/org/apache/ofbiz/content/data/DataResourceWorker.java @@ -66,6 +66,7 @@ import org.apache.ofbiz.base.util.UtilHttp; import org.apache.ofbiz.base.util.UtilIO; import org.apache.ofbiz.base.util.UtilMisc; import org.apache.ofbiz.base.util.UtilProperties; +import org.apache.ofbiz.base.util.UtilURL; import org.apache.ofbiz.base.util.UtilValidate; import org.apache.ofbiz.base.util.UtilXml; import org.apache.ofbiz.base.util.collections.MapStack; @@ -945,7 +946,7 @@ public class DataResourceWorker implements org.apache.ofbiz.widget.content.DataR sep = "/"; } String fixedUrlStr = prefix + sep + url.toString(); - URL fixedUrl = new URL(fixedUrlStr); + URL fixedUrl = UtilURL.fromUrlString(fixedUrlStr); text = (String) fixedUrl.getContent(); } out.append(text); @@ -1169,14 +1170,14 @@ public class DataResourceWorker implements org.apache.ofbiz.widget.content.DataR } else if ("URL_RESOURCE".equals(dataResourceTypeId)) { String objectInfo = dataResource.getString("objectInfo"); if (UtilValidate.isNotEmpty(objectInfo)) { - URL url = new URL(objectInfo); + URL url = UtilURL.fromUrlString(objectInfo); if (url.getHost() == null) { // is relative String newUrl = DataResourceWorker.buildRequestPrefix(delegator, locale, webSiteId, https); if (!newUrl.endsWith("/")) { newUrl = newUrl + "/"; } newUrl = newUrl + url.toString(); - url = new URL(newUrl); + url = UtilURL.fromUrlString(newUrl); } URLConnection con = url.openConnection(); diff --git a/framework/base/src/main/java/org/apache/ofbiz/base/config/UrlLoader.java b/framework/base/src/main/java/org/apache/ofbiz/base/config/UrlLoader.java index c0ca78c219..116c01d665 100644 --- a/framework/base/src/main/java/org/apache/ofbiz/base/config/UrlLoader.java +++ b/framework/base/src/main/java/org/apache/ofbiz/base/config/UrlLoader.java @@ -20,6 +20,9 @@ package org.apache.ofbiz.base.config; import java.io.InputStream; import java.io.Serializable; +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URISyntaxException; import java.net.URL; /** @@ -32,12 +35,12 @@ public class UrlLoader extends ResourceLoader implements Serializable { @Override public URL getURL(String location) throws GenericConfigException { String fullLocation = fullLocation(location); - URL url = null; - + URI uri; try { - url = new URL(fullLocation); - } catch (java.net.MalformedURLException e) { + uri = new URI(fullLocation); + url = uri.toURL(); + } catch (IllegalArgumentException | URISyntaxException | MalformedURLException e) { throw new GenericConfigException("Error with malformed URL while trying to load URL resource at location [" + fullLocation + "]", e); } return url; diff --git a/framework/base/src/main/java/org/apache/ofbiz/base/conversion/NetConverters.java b/framework/base/src/main/java/org/apache/ofbiz/base/conversion/NetConverters.java index 5b6fb7c778..aa81a58600 100644 --- a/framework/base/src/main/java/org/apache/ofbiz/base/conversion/NetConverters.java +++ b/framework/base/src/main/java/org/apache/ofbiz/base/conversion/NetConverters.java @@ -92,11 +92,15 @@ public class NetConverters implements ConverterLoader { @Override public URL convert(String obj) throws ConversionException { + URL url = null; + URI uri; try { - return new URL(obj); - } catch (MalformedURLException e) { + uri = new URI(obj); + url = uri.toURL(); + } catch (IllegalArgumentException | URISyntaxException | MalformedURLException e) { throw (ConversionException) new ConversionException(e.getMessage()).initCause(e); } + return url; } } diff --git a/framework/base/src/main/java/org/apache/ofbiz/base/location/StandardUrlLocationResolver.java b/framework/base/src/main/java/org/apache/ofbiz/base/location/StandardUrlLocationResolver.java index 07257bd55a..87d5e046db 100644 --- a/framework/base/src/main/java/org/apache/ofbiz/base/location/StandardUrlLocationResolver.java +++ b/framework/base/src/main/java/org/apache/ofbiz/base/location/StandardUrlLocationResolver.java @@ -21,6 +21,8 @@ package org.apache.ofbiz.base.location; import java.net.MalformedURLException; import java.net.URL; +import org.apache.ofbiz.base.util.UtilURL; + /** * A special location resolver that uses Strings like URLs, but with more options * @@ -29,6 +31,6 @@ import java.net.URL; public class StandardUrlLocationResolver implements LocationResolver { @Override public URL resolveLocation(String location) throws MalformedURLException { - return new URL(location); + return UtilURL.fromUrlString(location); } } diff --git a/framework/base/src/main/java/org/apache/ofbiz/base/util/HttpClient.java b/framework/base/src/main/java/org/apache/ofbiz/base/util/HttpClient.java index 7682a3f595..de3654b8ec 100644 --- a/framework/base/src/main/java/org/apache/ofbiz/base/util/HttpClient.java +++ b/framework/base/src/main/java/org/apache/ofbiz/base/util/HttpClient.java @@ -466,7 +466,7 @@ public class HttpClient { // Create the URL and open the connection. try { - requestUrl = new URL(url); + requestUrl = UtilURL.fromUrlString(url); if (overrideTrust) { con = URLConnector.openUntrustedConnection(requestUrl, timeout, clientCertAlias, hostVerification); } else { diff --git a/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilURL.java b/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilURL.java index e1655f83b8..ab88e48ba0 100644 --- a/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilURL.java +++ b/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilURL.java @@ -20,6 +20,8 @@ package org.apache.ofbiz.base.util; import java.io.File; import java.net.MalformedURLException; +import java.net.URI; +import java.net.URISyntaxException; import java.net.URL; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -79,11 +81,13 @@ public final class UtilURL { */ public static URL fromResource(String resourceName, ClassLoader loader) { URL url = URL_MAP.get(resourceName); + URI uri; if (url != null) { try { - return new URL(url.toString()); - } catch (MalformedURLException e) { - Debug.logWarning(e, "Exception thrown while copying URL: ", MODULE); + uri = new URI(url.toString()); + url = uri.toURL(); + } catch (IllegalArgumentException | URISyntaxException | MalformedURLException e) { + Debug.logWarning(e, "Exception thrown while copying URL", MODULE); } } if (loader == null) { @@ -132,7 +136,7 @@ public final class UtilURL { if (file.exists()) { url = file.toURI().toURL(); } - } catch (java.net.MalformedURLException e) { + } catch (MalformedURLException e) { Debug.logError(e, "unable to retrieve URL for file: " + filename, MODULE); } return url; @@ -140,10 +144,12 @@ public final class UtilURL { public static URL fromUrlString(String urlString) { URL url = null; + URI uri; try { - url = new URL(urlString); - } catch (MalformedURLException e) { - // We purposely don't want to do anything here + uri = new URI(urlString); + url = uri.toURL(); + } catch (IllegalArgumentException | URISyntaxException | MalformedURLException e) { + // We purposely don't want to do anything here. } return url; } diff --git a/framework/base/src/test/java/org/apache/ofbiz/base/conversion/MiscTests.java b/framework/base/src/test/java/org/apache/ofbiz/base/conversion/MiscTests.java index 3f5ba5e2c6..e5bbff3cf0 100644 --- a/framework/base/src/test/java/org/apache/ofbiz/base/conversion/MiscTests.java +++ b/framework/base/src/test/java/org/apache/ofbiz/base/conversion/MiscTests.java @@ -31,6 +31,7 @@ import java.util.Map; import org.apache.ofbiz.base.util.UtilGenerics; import org.apache.ofbiz.base.util.UtilMisc; +import org.apache.ofbiz.base.util.UtilURL; import org.junit.Test; public class MiscTests { @@ -67,7 +68,7 @@ public class MiscTests { public void testPassthru() throws Exception { String string = "ofbiz"; BigDecimal bigDecimal = new BigDecimal("1.234"); - URL url = new URL("http://ofbiz.apache.org"); + URL url = UtilURL.fromUrlString("http://ofbiz.apache.org"); List<String> baseList = UtilMisc.toList("a", "1", "b", "2", "c", "3"); List<String> arrayList = new ArrayList<>(); arrayList.addAll(baseList); diff --git a/framework/common/src/main/java/org/apache/ofbiz/common/email/EmailServices.java b/framework/common/src/main/java/org/apache/ofbiz/common/email/EmailServices.java index 4356517f03..6bbc5ae842 100644 --- a/framework/common/src/main/java/org/apache/ofbiz/common/email/EmailServices.java +++ b/framework/common/src/main/java/org/apache/ofbiz/common/email/EmailServices.java @@ -27,6 +27,8 @@ import java.io.StringReader; import java.io.StringWriter; import java.io.Writer; import java.net.MalformedURLException; +import java.net.URI; +import java.net.URISyntaxException; import java.net.URL; import java.util.Date; import java.util.LinkedHashMap; @@ -401,10 +403,11 @@ public class EmailServices { LocalDispatcher dispatcher = ctx.getDispatcher(); URL url = null; - + URI uri; try { - url = new URL(bodyUrl); - } catch (MalformedURLException e) { + uri = new URI(bodyUrl); + url = uri.toURL(); + } catch (IllegalArgumentException | URISyntaxException | MalformedURLException e) { Debug.logWarning(e, MODULE); return ServiceUtil.returnError(UtilProperties.getMessage(RESOURCE, "CommonEmailSendMalformedUrl", UtilMisc.toMap("bodyUrl", bodyUrl, "errorString", e.toString()), locale)); diff --git a/framework/service/src/test/groovy/org/apache/ofbiz/service/ModelServiceTest.groovy b/framework/service/src/test/groovy/org/apache/ofbiz/service/ModelServiceTest.groovy index 2dead6b19f..3955214c39 100644 --- a/framework/service/src/test/groovy/org/apache/ofbiz/service/ModelServiceTest.groovy +++ b/framework/service/src/test/groovy/org/apache/ofbiz/service/ModelServiceTest.groovy @@ -22,6 +22,7 @@ import static org.mockito.ArgumentMatchers.any import static org.mockito.Mockito.eq import org.apache.ofbiz.base.util.UtilProperties +import org.apache.ofbiz.base.util.UtilURL; import org.apache.ofbiz.base.util.UtilXml import org.apache.ofbiz.base.util.cache.UtilCache import org.junit.Assert @@ -321,7 +322,7 @@ class ModelServiceTest { @Test void callValidateServiceWitImplementParameter() { - ModelServiceReader reader = new ModelServiceReader(true, new URL('http://ofbiz.apache.org'), null, null) + ModelServiceReader reader = new ModelServiceReader(true, UtilURL.fromUrlString('http://ofbiz.apache.org'), null, null) String serviceXml1 = ''' <service name="toImplement" engine="java" location="org.apache.ofbiz.common.CommonServices" invoke="ping"> @@ -402,7 +403,7 @@ class ModelServiceTest { private static ModelService createModelService(String serviceXml) { Element serviceElement = UtilXml.readXmlDocument(serviceXml, false).getDocumentElement() - return new ModelServiceReader(true, new URL('http://ofbiz.apache.org'), null, null) + return new ModelServiceReader(true, UtilURL.fromUrlString('http://ofbiz.apache.org'), null, null) .createModelService(serviceElement, 'TEST') } diff --git a/framework/webtools/src/main/groovy/org/apache/ofbiz/webtools/datafile/ViewDataFile.groovy b/framework/webtools/src/main/groovy/org/apache/ofbiz/webtools/datafile/ViewDataFile.groovy index da2a6bc64f..ed9b28709e 100644 --- a/framework/webtools/src/main/groovy/org/apache/ofbiz/webtools/datafile/ViewDataFile.groovy +++ b/framework/webtools/src/main/groovy/org/apache/ofbiz/webtools/datafile/ViewDataFile.groovy @@ -39,14 +39,14 @@ dataFileIsUrl = null != request.getParameter('DATAFILE_IS_URL') definitionIsUrl = null != request.getParameter('DEFINITION_IS_URL') try { - dataFileUrl = dataFileIsUrl ? new URL(dataFileLoc) : UtilURL.fromFilename(dataFileLoc) + dataFileUrl = dataFileIsUrl ? UtilURL.fromUrlString(dataFileLoc) : UtilURL.fromFilename(dataFileLoc) } catch (java.net.MalformedURLException e) { messages.add(e.getMessage()) } try { - definitionUrl = definitionIsUrl ? new URL(definitionLoc) : UtilURL.fromFilename(definitionLoc) + definitionUrl = definitionIsUrl ? UtilURL.fromUrlString(definitionLoc) : UtilURL.fromFilename(definitionLoc) } catch (java.net.MalformedURLException e) { messages.add(e.getMessage()) diff --git a/framework/webtools/src/main/java/org/apache/ofbiz/webtools/artifactinfo/ArtifactInfoFactory.java b/framework/webtools/src/main/java/org/apache/ofbiz/webtools/artifactinfo/ArtifactInfoFactory.java index 94de3f6334..02d661fe41 100644 --- a/framework/webtools/src/main/java/org/apache/ofbiz/webtools/artifactinfo/ArtifactInfoFactory.java +++ b/framework/webtools/src/main/java/org/apache/ofbiz/webtools/artifactinfo/ArtifactInfoFactory.java @@ -39,6 +39,7 @@ import org.apache.ofbiz.base.concurrent.ExecutionPool; import org.apache.ofbiz.base.util.Debug; import org.apache.ofbiz.base.util.FileUtil; import org.apache.ofbiz.base.util.GeneralException; +import org.apache.ofbiz.base.util.UtilURL; import org.apache.ofbiz.base.util.UtilValidate; import org.apache.ofbiz.base.util.cache.UtilCache; import org.apache.ofbiz.entity.GenericEntityException; @@ -602,11 +603,11 @@ public class ArtifactInfoFactory { } else if ("screen".equals(type)) { return this.getScreenWidgetArtifactInfo(artifactName, artifactLocation); } else if ("request".equals(type)) { - return this.getControllerRequestArtifactInfo(new URL(artifactLocation), artifactName); + return this.getControllerRequestArtifactInfo(UtilURL.fromUrlString(artifactLocation), artifactName); } else if ("view".equals(type)) { - return this.getControllerViewArtifactInfo(new URL(artifactLocation), artifactName); + return this.getControllerViewArtifactInfo(UtilURL.fromUrlString(artifactLocation), artifactName); } - } catch (GeneralException | MalformedURLException e) { + } catch (GeneralException e) { Debug.logError(e, "Error getting artifact info: " + e.toString(), MODULE); } return null;