Author: pgil
Date: Thu Apr 14 21:12:29 2016 New Revision: 1739194 URL: http://svn.apache.org/viewvc?rev=1739194&view=rev Log: OFBIZ-6944 : streamDataResource do not manage OTHER_OBJECT dataResourceTypeId Fix an old TODO replacing streamDataResource with getDataResourceStream DataResourceWorker method, this fixing OTHER_OBJECT stream dataResource management. Thanks Jacques for the feedback Modified: ofbiz/trunk/applications/content/src/org/ofbiz/content/data/DataEvents.java ofbiz/trunk/applications/content/src/org/ofbiz/content/data/DataResourceWorker.java ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderEvents.java Modified: ofbiz/trunk/applications/content/src/org/ofbiz/content/data/DataEvents.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/content/src/org/ofbiz/content/data/DataEvents.java?rev=1739194&r1=1739193&r2=1739194&view=diff ============================================================================== --- ofbiz/trunk/applications/content/src/org/ofbiz/content/data/DataEvents.java (original) +++ ofbiz/trunk/applications/content/src/org/ofbiz/content/data/DataEvents.java Thu Apr 14 21:12:29 2016 @@ -18,6 +18,7 @@ *******************************************************************************/ package org.ofbiz.content.data; +import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -29,6 +30,7 @@ import javax.servlet.http.HttpServletReq import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; +import org.apache.commons.io.IOUtils; import org.ofbiz.base.util.Debug; import org.ofbiz.base.util.GeneralException; import org.ofbiz.base.util.UtilHttp; @@ -43,7 +45,6 @@ import org.ofbiz.entity.util.EntityUtilP import org.ofbiz.service.GenericServiceException; import org.ofbiz.service.LocalDispatcher; import org.ofbiz.service.ServiceUtil; -import org.ofbiz.webapp.website.WebSiteWorker; /** * DataEvents Class @@ -283,7 +284,8 @@ public class DataEvents { response.setContentType(mimeType); } OutputStream os = response.getOutputStream(); - DataResourceWorker.streamDataResource(os, delegator, dataResourceId, "", WebSiteWorker.getWebSiteId(request), UtilHttp.getLocale(request), application.getRealPath("/")); + Map<String, Object> resourceData = DataResourceWorker.getDataResourceStream(dataResource, "", application.getInitParameter("webSiteId"), UtilHttp.getLocale(request), application.getRealPath("/"), false); + os.write(IOUtils.toByteArray((ByteArrayInputStream)resourceData.get("stream"))); os.flush(); } catch (GenericEntityException e) { String errMsg = "Error downloading digital product content: " + e.toString(); Modified: ofbiz/trunk/applications/content/src/org/ofbiz/content/data/DataResourceWorker.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/content/src/org/ofbiz/content/data/DataResourceWorker.java?rev=1739194&r1=1739193&r2=1739194&view=diff ============================================================================== --- ofbiz/trunk/applications/content/src/org/ofbiz/content/data/DataResourceWorker.java (original) +++ ofbiz/trunk/applications/content/src/org/ofbiz/content/data/DataResourceWorker.java Thu Apr 14 21:12:29 2016 @@ -26,7 +26,6 @@ import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; import java.io.StringWriter; import java.io.Writer; import java.net.URL; @@ -49,6 +48,8 @@ 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.tika.Tika; import org.ofbiz.base.location.FlexibleLocation; import org.ofbiz.base.util.Debug; @@ -1039,7 +1040,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 FileInputStream(file), "length", Long.valueOf(file.length())); + return UtilMisc.toMap("stream", new ByteArrayInputStream(FileUtils.readFileToByteArray(file)), "length", Long.valueOf(file.length())); } else { throw new GeneralException("No objectInfo found for FILE type [" + dataResourceTypeId + "]; cannot stream"); } @@ -1069,69 +1070,11 @@ public class DataResourceWorker impleme throw new GeneralException("The dataResourceTypeId [" + dataResourceTypeId + "] is not supported in getDataResourceStream"); } - // TODO: remove this method in favor of getDataResourceStream - public static void streamDataResource(OutputStream os, Delegator delegator, String dataResourceId, String https, String webSiteId, Locale locale, String rootDir) throws IOException, GeneralException { - try { - GenericValue dataResource = EntityQuery.use(delegator).from("DataResource").where("dataResourceId", dataResourceId).cache().queryOne(); - if (dataResource == null) { - throw new GeneralException("Error in streamDataResource: DataResource with ID [" + dataResourceId + "] was not found."); - } - String dataResourceTypeId = dataResource.getString("dataResourceTypeId"); - if (UtilValidate.isEmpty(dataResourceTypeId)) { - dataResourceTypeId = "SHORT_TEXT"; - } - String mimeTypeId = dataResource.getString("mimeTypeId"); - if (UtilValidate.isEmpty(mimeTypeId)) { - mimeTypeId = "text/html"; - } - - if (dataResourceTypeId.equals("SHORT_TEXT")) { - String text = dataResource.getString("objectInfo"); - os.write(text.getBytes()); - } else if (dataResourceTypeId.equals("ELECTRONIC_TEXT")) { - GenericValue electronicText = EntityQuery.use(delegator).from("ElectronicText").where("dataResourceId", dataResourceId).cache().queryOne(); - if (electronicText != null) { - String text = electronicText.getString("textData"); - if (text != null) os.write(text.getBytes()); - } - } else if (dataResourceTypeId.equals("IMAGE_OBJECT")) { - byte[] imageBytes = acquireImage(delegator, dataResource); - if (imageBytes != null) os.write(imageBytes); - } else if (dataResourceTypeId.equals("LINK")) { - String text = dataResource.getString("objectInfo"); - os.write(text.getBytes()); - } else if (dataResourceTypeId.equals("URL_RESOURCE")) { - URL url = new URL(dataResource.getString("objectInfo")); - if (url.getHost() == null) { // is relative - String prefix = buildRequestPrefix(delegator, locale, webSiteId, https); - String sep = ""; - //String s = ""; - if (url.toString().indexOf("/") != 0 && prefix.lastIndexOf("/") != (prefix.length() - 1)) { - sep = "/"; - } - String s2 = prefix + sep + url.toString(); - url = new URL(s2); - } - InputStream in = url.openStream(); - UtilIO.copy(in, true, os, false); - } else if (dataResourceTypeId.indexOf("_FILE") >= 0) { - String objectInfo = dataResource.getString("objectInfo"); - File inputFile = getContentFile(dataResourceTypeId, objectInfo, rootDir); - //long fileSize = inputFile.length(); - FileInputStream fis = new FileInputStream(inputFile); - UtilIO.copy(fis, true, os, false); - } else { - throw new GeneralException("The dataResourceTypeId [" + dataResourceTypeId + "] is not supported in streamDataResource"); - } - } catch (GenericEntityException e) { - throw new GeneralException("Error in streamDataResource", e); - } - } - public static ByteBuffer getContentAsByteBuffer(Delegator delegator, String dataResourceId, String https, String webSiteId, Locale locale, String rootDir) throws IOException, GeneralException { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - streamDataResource(baos, delegator, dataResourceId, https, webSiteId, locale, rootDir); - ByteBuffer byteBuffer = ByteBuffer.wrap(baos.toByteArray()); + GenericValue dataResource = EntityQuery.use(delegator).from("DataResource").where("dataResourceId", dataResourceId).queryOne(); + Map<String, Object> resourceData = DataResourceWorker.getDataResourceStream(dataResource, https, webSiteId, locale, rootDir, false); + ByteArrayInputStream stream = (ByteArrayInputStream) resourceData.get("stream"); + ByteBuffer byteBuffer = ByteBuffer.wrap(IOUtils.toByteArray(stream)); return byteBuffer; } Modified: ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderEvents.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderEvents.java?rev=1739194&r1=1739193&r2=1739194&view=diff ============================================================================== --- ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderEvents.java (original) +++ ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderEvents.java Thu Apr 14 21:12:29 2016 @@ -18,6 +18,7 @@ *******************************************************************************/ package org.ofbiz.order.order; +import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.OutputStream; import java.util.HashMap; @@ -30,9 +31,11 @@ import javax.servlet.http.HttpServletReq import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; +import org.apache.commons.io.IOUtils; import org.ofbiz.base.util.Debug; import org.ofbiz.base.util.GeneralException; import org.ofbiz.base.util.UtilHttp; +import org.ofbiz.base.util.UtilMisc; import org.ofbiz.content.data.DataResourceWorker; import org.ofbiz.entity.Delegator; import org.ofbiz.entity.GenericEntityException; @@ -41,7 +44,6 @@ import org.ofbiz.entity.util.EntityQuery import org.ofbiz.service.GenericServiceException; import org.ofbiz.service.LocalDispatcher; import org.ofbiz.service.ServiceUtil; -import org.ofbiz.webapp.website.WebSiteWorker; /** * Order Events @@ -78,7 +80,9 @@ public class OrderEvents { response.setContentType(orderRoleAndProductContentInfo.getString("mimeTypeId")); } OutputStream os = response.getOutputStream(); - DataResourceWorker.streamDataResource(os, delegator, dataResourceId, "", WebSiteWorker.getWebSiteId(request), UtilHttp.getLocale(request), application.getRealPath("/")); + GenericValue dataResource = EntityQuery.use(delegator).from("DataResource").where("dataResourceId", dataResourceId).cache().queryOne(); + Map<String, Object> resourceData = DataResourceWorker.getDataResourceStream(dataResource, "", application.getInitParameter("webSiteId"), UtilHttp.getLocale(request), application.getRealPath("/"), false); + os.write(IOUtils.toByteArray((ByteArrayInputStream) resourceData.get("stream"))); os.flush(); } catch (GenericEntityException e) { String errMsg = "Error downloading digital product content: " + e.toString(); |
Free forum by Nabble | Edit this page |