try {
if (arguments[0] instanceof String) {
fileName = FilenameUtils.normalizeNoEndSeparator((String) arguments[0]);
if (fileName == null) {
throw new CarbonException(
"FileHostObject : Illegal file path, Cannot navigate away from resources directory");
}
} else {
throw new CarbonException(
"Invalid parameter. Expecting destination file location as a string.");
}
BufferedReader source = new BufferedReader(new FileReader(fileHostObject.file));
//Creating the new file
Object object = cx.getThreadLocal(MashupConstants.AXIS2_SERVICE);
AxisService axisService;
if (object instanceof AxisService) {
axisService = (AxisService) object;
} else {
throw new CarbonException("Error obtaining the AxisService.");
}
Object resourceFileObject = axisService
.getParameterValue(MashupConstants.RESOURCES_FOLDER);
File resourceFolder;
if (resourceFileObject != null && resourceFileObject instanceof File) {
resourceFolder = (File) resourceFileObject;
} else {
throw new CarbonException("Resources folder not found.");
}
File newFile = new File(resourceFolder, fileName);
if (newFile.isDirectory()) {
throw new CarbonException(
"Given path is not a File. This object does not support directories.");
}
//Creating the file and directories
if (!newFile.exists()) {
File parentFile = newFile.getParentFile();
if (parentFile != null && !parentFile.exists() && !parentFile.mkdirs()) {
throw new CarbonException("Unable to create directory " + parentFile.getName());
}
}
BufferedWriter destination = new BufferedWriter(new FileWriter(newFile));
String str;
// Copying contents to new location
while ((str = source.readLine()) != null) {
destination.write(str);
destination.newLine();
}
source.close();
destination.flush();
destination.close();
//Delete the source file
if(!fileHostObject.file.delete()) {
throw new CarbonException("Unable to delete file " + fileHostObject.file.getName());
}
//Point to the new file
fileHostObject.file = newFile;
result = true;
} catch (IOException e) {
throw new CarbonException(e);
}
return result;
}