Author: pgil
Date: Thu Apr 14 21:12:51 2016 New Revision: 1739198 URL: http://svn.apache.org/viewvc?rev=1739198&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/branches/release13.07/applications/content/src/org/ofbiz/content/data/DataEvents.java ofbiz/branches/release13.07/applications/content/src/org/ofbiz/content/data/DataResourceWorker.java ofbiz/branches/release13.07/applications/order/src/org/ofbiz/order/order/OrderEvents.java Modified: ofbiz/branches/release13.07/applications/content/src/org/ofbiz/content/data/DataEvents.java URL: http://svn.apache.org/viewvc/ofbiz/branches/release13.07/applications/content/src/org/ofbiz/content/data/DataEvents.java?rev=1739198&r1=1739197&r2=1739198&view=diff ============================================================================== --- ofbiz/branches/release13.07/applications/content/src/org/ofbiz/content/data/DataEvents.java (original) +++ ofbiz/branches/release13.07/applications/content/src/org/ofbiz/content/data/DataEvents.java Thu Apr 14 21:12:51 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; @@ -30,6 +31,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; @@ -42,7 +44,6 @@ import org.ofbiz.entity.GenericValue; import org.ofbiz.service.GenericServiceException; import org.ofbiz.service.LocalDispatcher; import org.ofbiz.service.ServiceUtil; -import org.ofbiz.webapp.website.WebSiteWorker; /** * DataEvents Class @@ -280,7 +281,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/branches/release13.07/applications/content/src/org/ofbiz/content/data/DataResourceWorker.java URL: http://svn.apache.org/viewvc/ofbiz/branches/release13.07/applications/content/src/org/ofbiz/content/data/DataResourceWorker.java?rev=1739198&r1=1739197&r2=1739198&view=diff ============================================================================== --- ofbiz/branches/release13.07/applications/content/src/org/ofbiz/content/data/DataResourceWorker.java (original) +++ ofbiz/branches/release13.07/applications/content/src/org/ofbiz/content/data/DataResourceWorker.java Thu Apr 14 21:12:51 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; @@ -50,6 +49,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; @@ -1055,7 +1056,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"); } @@ -1085,69 +1086,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 = delegator.findOne("DataResource", UtilMisc.toMap("dataResourceId", dataResourceId), true); - 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 = delegator.findOne("ElectronicText", UtilMisc.toMap("dataResourceId", dataResourceId), true); - 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 = delegator.findOne("DataResource", UtilMisc.toMap("dataResourceId", dataResourceId), false); + 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/branches/release13.07/applications/order/src/org/ofbiz/order/order/OrderEvents.java URL: http://svn.apache.org/viewvc/ofbiz/branches/release13.07/applications/order/src/org/ofbiz/order/order/OrderEvents.java?rev=1739198&r1=1739197&r2=1739198&view=diff ============================================================================== --- ofbiz/branches/release13.07/applications/order/src/org/ofbiz/order/order/OrderEvents.java (original) +++ ofbiz/branches/release13.07/applications/order/src/org/ofbiz/order/order/OrderEvents.java Thu Apr 14 21:12:51 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.List; @@ -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; @@ -37,11 +39,9 @@ import org.ofbiz.content.data.DataResour import org.ofbiz.entity.Delegator; import org.ofbiz.entity.GenericEntityException; import org.ofbiz.entity.GenericValue; - import org.ofbiz.service.GenericServiceException; import org.ofbiz.service.LocalDispatcher; import org.ofbiz.service.ServiceUtil; -import org.ofbiz.webapp.website.WebSiteWorker; import javolution.util.FastMap; @@ -77,7 +77,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 = delegator.findOne("DataResource", UtilMisc.toMap("dataResourceId", dataResourceId), true); + 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 |