Skip to content

Commit

Permalink
Improved: URL constructor is deprecated in Java 20 (OFBIZ-12881)
Browse files Browse the repository at this point in the history
Explanation: https://inside.java/2023/02/15/quality-heads-up/

I have kept the existing log information in UrlLoader, NetConverters, UtilURL
and EmailServices classes
  • Loading branch information
JacquesLeRoux committed Jan 29, 2024
1 parent a07c44f commit fb6796f
Show file tree
Hide file tree
Showing 13 changed files with 55 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.net.URL;

import org.apache.ofbiz.base.util.Debug;
import org.apache.ofbiz.base.util.UtilURL;

/**
* Handles connections to the eWay servers.
Expand Down Expand Up @@ -83,7 +84,7 @@ public GatewayResponse sendRequest(GatewayRequest request) throws Exception {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
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;
Expand Down Expand Up @@ -240,7 +241,7 @@ public static String payPalIPN(HttpServletRequest request, HttpServletResponse r
// 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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
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;
Expand Down Expand Up @@ -945,7 +946,7 @@ public static void writeDataResourceText(GenericValue dataResource, String mimeT
sep = "/";
}
String fixedUrlStr = prefix + sep + url.toString();
URL fixedUrl = new URL(fixedUrlStr);
URL fixedUrl = UtilURL.fromUrlString(fixedUrlStr);
text = (String) fixedUrl.getContent();
}
out.append(text);
Expand Down Expand Up @@ -1169,14 +1170,14 @@ public static Map<String, Object> getDataResourceStream(GenericValue dataResourc
} 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@

import java.io.InputStream;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

/**
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,15 @@ public StringToURL() {

@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;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
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
*
Expand All @@ -29,6 +31,6 @@
public class StandardUrlLocationResolver implements LocationResolver {
@Override
public URL resolveLocation(String location) throws MalformedURLException {
return new URL(location);
return UtilURL.fromUrlString(location);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ private InputStream sendHttpRequestStream(String method, boolean overrideTrust)

// 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

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;
Expand Down Expand Up @@ -79,11 +81,13 @@ public static <C> URL fromResource(Class<C> contextClass, String resourceName) {
*/
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) {
Expand Down Expand Up @@ -132,18 +136,20 @@ public static URL fromFilename(String filename) {
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;
}

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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

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 {
Expand Down Expand Up @@ -67,7 +68,7 @@ public static <S> void assertPassThru(Object wanted, Class<S> sourceClass, Class
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
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;
Expand Down Expand Up @@ -401,10 +403,11 @@ public static Map<String, Object> sendMailFromUrl(DispatchContext ctx, Map<Strin
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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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">
Expand Down Expand Up @@ -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')
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
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;
Expand Down Expand Up @@ -602,11 +603,11 @@ public ArtifactInfoBase getArtifactInfoByNameAndType(String artifactName, String
} 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;
Expand Down

0 comments on commit fb6796f

Please sign in to comment.