* @throws IOException
* @throws GeneralException
*/
public static Map<String, Object> getDataResourceStream(GenericValue dataResource, String https, String webSiteId, Locale locale, String contextRoot, boolean cache) throws IOException, GeneralException {
if (dataResource == null) {
throw new GeneralException("Cannot stream null data resource!");
}
String dataResourceTypeId = dataResource.getString("dataResourceTypeId");
String dataResourceId = dataResource.getString("dataResourceId");
Delegator delegator = dataResource.getDelegator();
// first text based data
if (dataResourceTypeId.endsWith("_TEXT") || "LINK".equals(dataResourceTypeId)) {
String text = "";
if ("SHORT_TEXT".equals(dataResourceTypeId) || "LINK".equals(dataResourceTypeId)) {
text = dataResource.getString("objectInfo");
} else if ("ELECTRONIC_TEXT".equals(dataResourceTypeId)) {
GenericValue electronicText = delegator.findOne("ElectronicText", UtilMisc.toMap("dataResourceId", dataResourceId), cache);
if (electronicText != null) {
text = electronicText.getString("textData");
}
} else {
throw new GeneralException("Unsupported TEXT type; cannot stream");
}
byte[] bytes = text.getBytes();
return UtilMisc.toMap("stream", new ByteArrayInputStream(bytes), "length", Integer.valueOf(bytes.length));
// object (binary) data
} else if (dataResourceTypeId.endsWith("_OBJECT")) {
byte[] bytes = new byte[0];
GenericValue valObj;
if ("IMAGE_OBJECT".equals(dataResourceTypeId)) {
valObj = delegator.findOne("ImageDataResource", UtilMisc.toMap("dataResourceId", dataResourceId), cache);
if (valObj != null) {
bytes = valObj.getBytes("imageData");
}
} else if ("VIDEO_OBJECT".equals(dataResourceTypeId)) {
valObj = delegator.findOne("VideoDataResource", UtilMisc.toMap("dataResourceId", dataResourceId), cache);
if (valObj != null) {
bytes = valObj.getBytes("videoData");
}
} else if ("AUDIO_OBJECT".equals(dataResourceTypeId)) {
valObj = delegator.findOne("AudioDataResource", UtilMisc.toMap("dataResourceId", dataResourceId), cache);
if (valObj != null) {
bytes = valObj.getBytes("audioData");
}
} else if ("OTHER_OBJECT".equals(dataResourceTypeId)) {
valObj = delegator.findOne("OtherDataResource", UtilMisc.toMap("dataResourceId", dataResourceId), cache);
if (valObj != null) {
bytes = valObj.getBytes("dataResourceContent");
}
} else {
throw new GeneralException("Unsupported OBJECT type [" + dataResourceTypeId + "]; cannot stream");
}
return UtilMisc.toMap("stream", new ByteArrayInputStream(bytes), "length", Long.valueOf(bytes.length));
// file data
} else if (dataResourceTypeId.endsWith("_FILE") || dataResourceTypeId.endsWith("_FILE_BIN")) {
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()));
} else {
throw new GeneralException("No objectInfo found for FILE type [" + dataResourceTypeId + "]; cannot stream");
}
// URL resource data
} else if ("URL_RESOURCE".equals(dataResourceTypeId)) {
String objectInfo = dataResource.getString("objectInfo");
if (UtilValidate.isNotEmpty(objectInfo)) {
URL url = new URL(objectInfo);
if (url.getHost() == null) { // is relative
String newUrl = DataResourceWorker.buildRequestPrefix(delegator, locale, webSiteId, https);
if (!newUrl.endsWith("/")) {
newUrl = newUrl + "/";
}
newUrl = newUrl + url.toString();
url = new URL(newUrl);
}
URLConnection con = url.openConnection();
return UtilMisc.toMap("stream", con.getInputStream(), "length", Long.valueOf(con.getContentLength()));
} else {
throw new GeneralException("No objectInfo found for URL_RESOURCE type; cannot stream");
}
}
// unsupported type
throw new GeneralException("The dataResourceTypeId [" + dataResourceTypeId + "] is not supported in getDataResourceStream");
}