You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by jl...@apache.org on 2018/01/12 21:59:22 UTC
svn commit: r1821036 - in /ofbiz/ofbiz-framework/trunk:
applications/content/config/
applications/content/src/main/java/org/apache/ofbiz/content/data/
framework/base/src/main/java/org/apache/ofbiz/base/util/
Author: jleroux
Date: Fri Jan 12 21:59:22 2018
New Revision: 1821036
URL: http://svn.apache.org/viewvc?rev=1821036&view=rev
Log:
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
Modified:
ofbiz/ofbiz-framework/trunk/applications/content/config/content.properties
ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/data/DataEvents.java
ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/data/DataResourceWorker.java
ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilHttp.java
Modified: ofbiz/ofbiz-framework/trunk/applications/content/config/content.properties
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/applications/content/config/content.properties?rev=1821036&r1=1821035&r2=1821036&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/applications/content/config/content.properties (original)
+++ ofbiz/ofbiz-framework/trunk/applications/content/config/content.properties Fri Jan 12 21:59:22 2018
@@ -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
+
+
Modified: ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/data/DataEvents.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/data/DataEvents.java?rev=1821036&r1=1821035&r2=1821036&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/data/DataEvents.java (original)
+++ ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/data/DataEvents.java Fri Jan 12 21:59:22 2018
@@ -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";
}
Modified: ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/data/DataResourceWorker.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/data/DataResourceWorker.java?rev=1821036&r1=1821035&r2=1821036&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/data/DataResourceWorker.java (original)
+++ ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/data/DataResourceWorker.java Fri Jan 12 21:59:22 2018
@@ -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.Fil
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 impleme
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");
Modified: ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilHttp.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilHttp.java?rev=1821036&r1=1821035&r2=1821036&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilHttp.java (original)
+++ ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilHttp.java Fri Jan 12 21:59:22 2018
@@ -56,6 +56,7 @@ import org.apache.http.conn.ssl.TrustSel
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);