From 823cc0bf996d492b9c3ded3c8fc4200527cddea6 Mon Sep 17 00:00:00 2001 From: Jacques Le Roux Date: Fri, 12 Jan 2018 21:59:22 +0000 Subject: [PATCH] Fixed: streaming large content cause out of memory exception. (OFBIZ-10133) When ofbiz streams a large content (eg. video file), get out of memory exception. To reproduce the issue see the Jira jleroux: I just removed final for bufferSize, I see no needs for that local variable Thanks: Wai for the patch Michael for the discussion git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1821036 13f79535-47bb-0310-9956-ffa450edef68 --- applications/content/config/content.properties | 5 ++++- .../java/org/apache/ofbiz/content/data/DataEvents.java | 2 +- .../org/apache/ofbiz/content/data/DataResourceWorker.java | 5 +++-- .../src/main/java/org/apache/ofbiz/base/util/UtilHttp.java | 7 +++---- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/applications/content/config/content.properties b/applications/content/config/content.properties index 0bda1143fe..963627d7bb 100644 --- a/applications/content/config/content.properties +++ b/applications/content/config/content.properties @@ -21,6 +21,7 @@ img.request=/content/control/img/ img.request.param.name=imgId stream.permission.service=genericContentPermission +stream.buffersize=8192 baseUrl=https://localhost:8443 disable.ftl.template.cache=true @@ -37,4 +38,6 @@ content.upload.always.local.file=true content.output.path=runtime/output # default mime type used in *ContentWrapper classes -defaultMimeType=text/html; charset=utf-8 \ No newline at end of file +defaultMimeType=text/html; charset=utf-8 + + diff --git a/applications/content/src/main/java/org/apache/ofbiz/content/data/DataEvents.java b/applications/content/src/main/java/org/apache/ofbiz/content/data/DataEvents.java index fe3f1aeca7..68ac6ceca3 100644 --- a/applications/content/src/main/java/org/apache/ofbiz/content/data/DataEvents.java +++ b/applications/content/src/main/java/org/apache/ofbiz/content/data/DataEvents.java @@ -172,7 +172,7 @@ public class DataEvents { String mimeType = DataResourceWorker.getMimeType(dataResource); // hack for IE and mime types - if (userAgent.indexOf("MSIE") > -1) { + if (UtilValidate.isNotEmpty(userAgent) && userAgent.indexOf("MSIE") > -1) { Debug.logInfo("Found MSIE changing mime type from - " + mimeType, module); mimeType = "application/octet-stream"; } 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 dcf5de5e6d..c9c063cca6 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 @@ -30,6 +30,8 @@ import java.io.Writer; import java.net.URL; import java.net.URLConnection; import java.nio.ByteBuffer; +import java.nio.file.Files; +import java.nio.file.StandardOpenOption; import java.sql.Timestamp; import java.util.Comparator; import java.util.HashMap; @@ -49,7 +51,6 @@ import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; -import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.ofbiz.base.location.FlexibleLocation; import org.apache.ofbiz.base.util.Debug; @@ -1104,7 +1105,7 @@ public class DataResourceWorker implements org.apache.ofbiz.widget.content.Data String objectInfo = dataResource.getString("objectInfo"); if (UtilValidate.isNotEmpty(objectInfo)) { File file = DataResourceWorker.getContentFile(dataResourceTypeId, objectInfo, contextRoot); - return UtilMisc.toMap("stream", new ByteArrayInputStream(FileUtils.readFileToByteArray(file)), "length", Long.valueOf(file.length())); + return UtilMisc.toMap("stream", Files.newInputStream(file.toPath(), StandardOpenOption.READ), "length", Long.valueOf(file.length())); } throw new GeneralException("No objectInfo found for FILE type [" + dataResourceTypeId + "]; cannot stream"); diff --git a/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilHttp.java b/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilHttp.java index 28b80c59e5..be37c14818 100644 --- a/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilHttp.java +++ b/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilHttp.java @@ -56,6 +56,7 @@ import org.apache.http.conn.ssl.TrustSelfSignedStrategy; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.ssl.SSLContexts; +import org.apache.ofbiz.entity.util.EntityUtilProperties; import org.apache.ofbiz.widget.renderer.VisualTheme; import org.apache.oro.text.regex.MalformedPatternException; import org.apache.oro.text.regex.Pattern; @@ -1086,8 +1087,6 @@ public final class UtilHttp { * @throws IOException */ public static void streamContent(OutputStream out, InputStream in, int length) throws IOException { - int bufferSize = 512; // same as the default buffer size; change as needed - // make sure we have something to write to if (out == null) { throw new IOException("Attempt to write to null output stream"); @@ -1104,8 +1103,8 @@ public final class UtilHttp { } // initialize the buffered streams - - byte[] buffer = new byte[length]; + int bufferSize = EntityUtilProperties.getPropertyAsInteger("content", "stream.buffersize", 8192); + byte[] buffer = new byte[bufferSize]; int read = 0; try ( BufferedOutputStream bos = new BufferedOutputStream(out, bufferSize);