List<String> files = new ArrayList<String>();
List<String> urls = new LinkedList<String>();
// If target directory is defined or if it is an ajax request then save the file now
// otherwise we delay the save of the file to the node creation
if (isContributePost || isTargetDirectoryDefined || isAjaxRequest) {
JCRSessionWrapper session =
jcrSessionFactory.getCurrentUserSession(workspace, locale);
String target;
if (isTargetDirectoryDefined) {
target = (fileUpload.getParameterValues(TARGETDIRECTORY))[0];
} else if (isContributePost) {
String path = urlResolver.getPath();
path = (path.endsWith("*") ? StringUtils.substringBeforeLast(path, "/") : path);
JCRNodeWrapper sessionNode = session.getNode(path);
JCRSiteNode siteNode = sessionNode.getResolveSite();
if (siteNode != null) {
String s = sessionNode.getResolveSite().getPath() + "/files/contributed/";
String name = sessionNode.getPrimaryNodeTypeName().replaceAll(":", "_") + "_" + sessionNode.getName();
target = s + name;
try {
session.getNode(target);
} catch (RepositoryException e) {
JCRNodeWrapper node = session.getNode(s);
session.checkout(node);
node.addNode(name, "jnt:folder");
session.save();
}
} else {
target = sessionNode.getPath() + "/files";
if (!sessionNode.hasNode("files")) {
session.checkout(sessionNode);
sessionNode.addNode("files", "jnt:folder");
session.save();
}
}
} else {
String path = urlResolver.getPath();
target = (path.endsWith("*") ? StringUtils.substringBeforeLast(path, "/") : path);
}
final JCRNodeWrapper targetDirectory = session.getNode(target);
boolean isVersionActivated = fileUpload.getParameterNames().contains(VERSION) ?
(fileUpload.getParameterValues(VERSION))[0].equals("true") : false;
final Map<String, DiskFileItem> stringDiskFileItemMap = fileUpload.getFileItems();
for (Map.Entry<String, DiskFileItem> itemEntry : stringDiskFileItemMap.entrySet()) {
//if node exists, do a checkout before
String name = itemEntry.getValue().getName();
if (fileUpload.getParameterNames().contains(TARGETNAME)) {
name = (fileUpload.getParameterValues(TARGETNAME))[0];
}
JCRNodeWrapper fileNode = targetDirectory.hasNode(name) ?
targetDirectory.getNode(name) : null;
if (fileNode != null && isVersionActivated) {
session.checkout(fileNode);
}
// checkout parent directory
session.getWorkspace().getVersionManager().checkout(targetDirectory.getPath());
final JCRNodeWrapper wrapper = targetDirectory
.uploadFile(name,
itemEntry.getValue().getInputStream(),
itemEntry.getValue().getContentType());
uuids.add(wrapper.getIdentifier());
urls.add(wrapper.getAbsoluteUrl(req));
files.add(itemEntry.getValue().getName());
if (isVersionActivated) {
if (!wrapper.isVersioned()) {
wrapper.versionFile();
}
session.save();
wrapper.checkpoint();
}
}
fileUpload.markFilesAsConsumed();
session.save();
}
if (!isAjaxRequest && !isContributePost) {
parameters.putAll(fileUpload.getParameterMap());
if (isTargetDirectoryDefined) {