This is an automated email from the ASF dual-hosted git repository.

mridulpathak pushed a commit to branch release24.09
in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git


The following commit(s) were added to refs/heads/release24.09 by this push:
     new b1319105d9 Fixed: URL.getFile() used as a filesystem path breaks OFBiz 
when the install directory contains a space (OFBIZ-13687)
b1319105d9 is described below

commit b1319105d980fcd7dbe6dfe0c9808c1513a41a75
Author: Mridul Pathak <[email protected]>
AuthorDate: Thu Oct 15 19:19:19 2026 +0530

    Fixed: URL.getFile() used as a filesystem path breaks OFBiz when the 
install directory contains a space (OFBIZ-13687)
    
    URL.getFile() and URL.getPath() keep percent-encoded characters such as 
%20, so using their result as a file name points to a path that does not exist 
when the OFBiz home directory contains a space. Added UtilURL.toFile(URL), 
which converts through URL.toURI() to decode the path, and used it at the 
affected call sites:
    
    - ApacheFopWorker: fop.xconf and the font base directory were not found, 
leaving the FopFactory null and making every PDF render fail
    - ScriptUtil.executeScript: read the script through URL.openStream() 
instead of a FileInputStream on the encoded path
    - MiniLangUtil.writeMiniLangDocument, CommonEvents.openSourceFile, 
ScaleImage and ImageManagementServices: resolve the file through UtilURL.toFile
    - UtilURL.getOfbizHomeRelativeLocation: compare the decoded URI path with 
ofbiz.home
---
 .../org/apache/ofbiz/product/image/ScaleImage.java |  5 +++--
 .../imagemanagement/ImageManagementServices.java   |  3 ++-
 .../org/apache/ofbiz/base/util/ScriptUtil.java     |  3 +--
 .../java/org/apache/ofbiz/base/util/UtilURL.java   | 23 +++++++++++++++++++++-
 .../java/org/apache/ofbiz/common/CommonEvents.java |  3 ++-
 .../org/apache/ofbiz/minilang/MiniLangUtil.java    |  3 ++-
 .../apache/ofbiz/webapp/view/ApacheFopWorker.java  |  6 +++---
 7 files changed, 35 insertions(+), 11 deletions(-)

diff --git 
a/applications/product/src/main/java/org/apache/ofbiz/product/image/ScaleImage.java
 
b/applications/product/src/main/java/org/apache/ofbiz/product/image/ScaleImage.java
index b350bd1f9d..6604b334b2 100644
--- 
a/applications/product/src/main/java/org/apache/ofbiz/product/image/ScaleImage.java
+++ 
b/applications/product/src/main/java/org/apache/ofbiz/product/image/ScaleImage.java
@@ -34,6 +34,7 @@ import org.apache.ofbiz.base.util.Debug;
 import org.apache.ofbiz.base.util.UtilGenerics;
 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.string.FlexibleStringExpander;
 import org.apache.ofbiz.common.image.ImageTransform;
@@ -94,7 +95,7 @@ public class ScaleImage {
 
         /* ImageProperties.xml */
         String fileName = "component://product/config/ImageProperties.xml";
-        String imgPropertyFullPath = 
FlexibleLocation.resolveLocation(fileName).getFile();
+        String imgPropertyFullPath = 
UtilURL.toFile(FlexibleLocation.resolveLocation(fileName)).getPath();
         resultXMLMap.putAll(ImageTransform.getXMLValue(imgPropertyFullPath, 
locale));
         if (resultXMLMap.containsKey("responseMessage") && 
"success".equals(resultXMLMap.get("responseMessage"))) {
             imgPropertyMap.putAll(UtilGenerics.<Map<String, Map<String, 
String>>>cast(resultXMLMap.get("xml")));
@@ -288,7 +289,7 @@ public class ScaleImage {
 
         /* ImageProperties.xml */
         String fileName = "component://product/config/ImageProperties.xml";
-        String imgPropertyFullPath = 
FlexibleLocation.resolveLocation(fileName).getFile();
+        String imgPropertyFullPath = 
UtilURL.toFile(FlexibleLocation.resolveLocation(fileName)).getPath();
         resultXMLMap.putAll(ImageTransform.getXMLValue(imgPropertyFullPath, 
locale));
         if (resultXMLMap.containsKey("responseMessage") && 
"success".equals(resultXMLMap.get("responseMessage"))) {
             imgPropertyMap.putAll(UtilGenerics.<Map<String, Map<String, 
String>>>cast(resultXMLMap.get("xml")));
diff --git 
a/applications/product/src/main/java/org/apache/ofbiz/product/imagemanagement/ImageManagementServices.java
 
b/applications/product/src/main/java/org/apache/ofbiz/product/imagemanagement/ImageManagementServices.java
index 16111550b5..d5b72cac23 100644
--- 
a/applications/product/src/main/java/org/apache/ofbiz/product/imagemanagement/ImageManagementServices.java
+++ 
b/applications/product/src/main/java/org/apache/ofbiz/product/imagemanagement/ImageManagementServices.java
@@ -45,6 +45,7 @@ import org.apache.ofbiz.base.util.UtilDateTime;
 import org.apache.ofbiz.base.util.UtilGenerics;
 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.string.FlexibleStringExpander;
 import org.apache.ofbiz.common.image.ImageTransform;
@@ -414,7 +415,7 @@ public class ImageManagementServices {
 
         /* ImageProperties.xml */
         String fileName = "component://product/config/ImageProperties.xml";
-        String imgPropertyFullPath = 
FlexibleLocation.resolveLocation(fileName).getFile();
+        String imgPropertyFullPath = 
UtilURL.toFile(FlexibleLocation.resolveLocation(fileName)).getPath();
         resultXMLMap.putAll(ImageTransform.getXMLValue(imgPropertyFullPath, 
locale));
         if (resultXMLMap.containsKey("responseMessage") && 
"success".equals(resultXMLMap.get("responseMessage"))) {
             imgPropertyMap.putAll(UtilGenerics.<Map<String, Map<String, 
String>>>cast(resultXMLMap.get("xml")));
diff --git 
a/framework/base/src/main/java/org/apache/ofbiz/base/util/ScriptUtil.java 
b/framework/base/src/main/java/org/apache/ofbiz/base/util/ScriptUtil.java
index d83bf98619..bb773377a1 100644
--- a/framework/base/src/main/java/org/apache/ofbiz/base/util/ScriptUtil.java
+++ b/framework/base/src/main/java/org/apache/ofbiz/base/util/ScriptUtil.java
@@ -19,7 +19,6 @@
 package org.apache.ofbiz.base.util;
 
 import java.io.BufferedReader;
-import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.net.URL;
@@ -369,7 +368,7 @@ public final class ScriptUtil {
             throw new ScriptException("Script not found at location [" + 
filePath + "]");
         }
         try (
-                InputStreamReader reader = new InputStreamReader(new 
FileInputStream(scriptUrl.getFile()), StandardCharsets.UTF_8);) {
+                InputStreamReader reader = new 
InputStreamReader(scriptUrl.openStream(), StandardCharsets.UTF_8);) {
             Object result = engine.eval(reader);
             if (UtilValidate.isNotEmpty(functionName)) {
                 try {
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 0d19001314..b8e9187d75 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
@@ -147,6 +147,22 @@ public final class UtilURL {
         return url;
     }
 
+    /**
+     * Converts a <code>file:</code> URL to a <code>File</code>. Unlike using 
{@link URL#getFile()} or
+     * {@link URL#getPath()} as a file name, this decodes percent-encoded 
characters such as <code>%20</code>,
+     * so it works when the path contains spaces.
+     * @param fileUrl the <code>file:</code> URL to convert
+     * @return the corresponding <code>File</code>
+     * @throws IllegalArgumentException if the URL is not a valid 
<code>file:</code> URL
+     */
+    public static File toFile(URL fileUrl) {
+        try {
+            return new File(fileUrl.toURI());
+        } catch (URISyntaxException e) {
+            throw new IllegalArgumentException("Invalid file URL: " + fileUrl, 
e);
+        }
+    }
+
     public static URL fromUrlString(String urlString) {
         URL url = null;
         URI uri;
@@ -337,7 +353,12 @@ public final class UtilURL {
 
     public static String getOfbizHomeRelativeLocation(URL fileUrl) {
         String ofbizHome = System.getProperty("ofbiz.home");
-        String path = fileUrl.getPath();
+        String path;
+        try {
+            path = fileUrl.toURI().getPath();
+        } catch (URISyntaxException e) {
+            path = fileUrl.getPath();
+        }
         if (path.startsWith(ofbizHome)) {
             // note: the +1 is to remove the leading slash
             path = path.substring(ofbizHome.length() + 1);
diff --git 
a/framework/common/src/main/java/org/apache/ofbiz/common/CommonEvents.java 
b/framework/common/src/main/java/org/apache/ofbiz/common/CommonEvents.java
index c5e38dabf7..1b440f6e66 100644
--- a/framework/common/src/main/java/org/apache/ofbiz/common/CommonEvents.java
+++ b/framework/common/src/main/java/org/apache/ofbiz/common/CommonEvents.java
@@ -51,6 +51,7 @@ import org.apache.ofbiz.base.util.StringUtil;
 import org.apache.ofbiz.base.util.UtilGenerics;
 import org.apache.ofbiz.base.util.UtilHttp;
 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.string.FlexibleStringExpander;
 import org.apache.ofbiz.entity.Delegator;
@@ -414,7 +415,7 @@ public class CommonEvents {
                     } else {
                         sourceFileUrl = 
componentLocationResolver.resolveLocation(sourceLocation);
                     }
-                    String platformSpecificPath = sourceFileUrl.getFile();
+                    String platformSpecificPath = 
UtilURL.toFile(sourceFileUrl).getPath();
                     // ensure file separator in location is correct
                     if (!platformSpecificPath.contains(File.separator) && 
"\\".equals(File.separator)) {
                         platformSpecificPath = 
platformSpecificPath.replace("/", "\\");
diff --git 
a/framework/minilang/src/main/java/org/apache/ofbiz/minilang/MiniLangUtil.java 
b/framework/minilang/src/main/java/org/apache/ofbiz/minilang/MiniLangUtil.java
index 70e695e4a7..53230f6d09 100644
--- 
a/framework/minilang/src/main/java/org/apache/ofbiz/minilang/MiniLangUtil.java
+++ 
b/framework/minilang/src/main/java/org/apache/ofbiz/minilang/MiniLangUtil.java
@@ -41,6 +41,7 @@ import org.apache.ofbiz.base.location.FlexibleLocation;
 import org.apache.ofbiz.base.util.Debug;
 import org.apache.ofbiz.base.util.ScriptUtil;
 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.collections.FlexibleMapAccessor;
 import org.apache.ofbiz.base.util.string.FlexibleStringExpander;
@@ -297,7 +298,7 @@ public final class MiniLangUtil {
             Debug.logWarning(e, "Error reading minilang/config/MiniLang.xslt: 
", MODULE);
             return;
         }
-        try (FileOutputStream fos = new FileOutputStream(xmlURL.getFile())) {
+        try (FileOutputStream fos = new 
FileOutputStream(UtilURL.toFile(xmlURL))) {
             UtilXml.transformDomDocument(transformer, document, fos);
             Debug.logInfo("Saved Mini-language file " + xmlURL, MODULE);
         } catch (Exception e) {
diff --git 
a/framework/webapp/src/main/java/org/apache/ofbiz/webapp/view/ApacheFopWorker.java
 
b/framework/webapp/src/main/java/org/apache/ofbiz/webapp/view/ApacheFopWorker.java
index a1ad59e0ab..bcbbbfe864 100644
--- 
a/framework/webapp/src/main/java/org/apache/ofbiz/webapp/view/ApacheFopWorker.java
+++ 
b/framework/webapp/src/main/java/org/apache/ofbiz/webapp/view/ApacheFopWorker.java
@@ -44,8 +44,8 @@ import org.apache.fop.apps.MimeConstants;
 import org.apache.fop.apps.io.ResourceResolverFactory;
 import org.apache.ofbiz.base.location.FlexibleLocation;
 import org.apache.ofbiz.base.util.Debug;
-import org.apache.ofbiz.base.util.FileUtil;
 import org.apache.ofbiz.base.util.UtilProperties;
+import org.apache.ofbiz.base.util.UtilURL;
 import org.apache.ofbiz.base.util.UtilValidate;
 
 /**
@@ -104,14 +104,14 @@ public final class ApacheFopWorker {
 
                 try {
                     URL configFilePath = 
FlexibleLocation.resolveLocation(FOP_PATH + "/fop.xconf");
-                    File userConfigFile = 
FileUtil.getFile(configFilePath.getFile());
+                    File userConfigFile = UtilURL.toFile(configFilePath);
                     if (userConfigFile.exists()) {
                         fopFactory = FopFactory.newInstance(userConfigFile);
                     } else {
                         Debug.logWarning("FOP configuration file not found: " 
+ userConfigFile, MODULE);
                     }
                     URL fontBaseFileUrl = 
FlexibleLocation.resolveLocation(FOP_FONT_BASE_PROPERTY);
-                    File fontBaseFile = 
FileUtil.getFile(fontBaseFileUrl.getFile());
+                    File fontBaseFile = UtilURL.toFile(fontBaseFileUrl);
 
                     if (fontBaseFile.isDirectory()) {
                         
fopFactory.getFontManager().setResourceResolver(ResourceResolverFactory

Reply via email to