try {
if (UtilValidate.isEmpty(dataResourceTypeId) || dataResourceTypeId.startsWith("LOCAL_FILE")) {
fileName = prefix + sep + objectInfo;
file = new File(fileName);
if (file == null) {
throw new GenericServiceException("File: " + fileName + " is null.");
}
if (!file.isAbsolute()) {
throw new GenericServiceException("File: " + fileName + " is not absolute.");
}
} else if (dataResourceTypeId.startsWith("OFBIZ_FILE")) {
prefix = System.getProperty("ofbiz.home");
if (objectInfo.indexOf("/") != 0 && prefix.lastIndexOf("/") != (prefix.length() - 1)) {
sep = "/";
}
file = new File(prefix + sep + objectInfo);
} else if (dataResourceTypeId.startsWith("CONTEXT_FILE")) {
prefix = (String) context.get("rootDir");
if (objectInfo.indexOf("/") != 0 && prefix.lastIndexOf("/") != (prefix.length() - 1)) {
sep = "/";
}
file = new File(prefix + sep + objectInfo);
}
if (file == null) {
throw new IOException("File: " + file + " is null");
}
// write the data to the file
if (UtilValidate.isNotEmpty(textData)) {
try {
FileWriter out = new FileWriter(file);
out.write(textData);
out.close();
} catch (IOException e) {
Debug.logWarning(e, module);
return ServiceUtil.returnError("Unable to write character data to: " + file.getAbsolutePath());
}
} else if (binData != null) {
try {
RandomAccessFile out = new RandomAccessFile(file, "rw");
out.write(binData.array());
out.close();
} catch (FileNotFoundException e) {
Debug.logError(e, module);
return ServiceUtil.returnError("Unable to open file for writing: " + file.getAbsolutePath());
} catch (IOException e) {
Debug.logError(e, module);
return ServiceUtil.returnError("Unable to write binary data to: " + file.getAbsolutePath());
}
} else {
return ServiceUtil.returnError("No file content passed for: " + file.getAbsolutePath());
}
} catch (IOException e) {
Debug.logWarning(e, module);
throw new GenericServiceException(e.getMessage());
}
return result;
}