use of org.springframework.extensions.jcr.JcrCallback in project pentaho-platform by pentaho.
the class JcrRepositoryDumpToFile method execute.
public void execute() {
if (filename != null) {
// $NON-NLS-1$
final String ZIP_EXTENSION = ".zip";
// let the user know this is a zip
if (!filename.endsWith(ZIP_EXTENSION)) {
filename = filename + ZIP_EXTENSION;
}
// $NON-NLS-1$
logger.debug(String.format("dumping repository to file \"%s\"", filename));
ZipOutputStream tmpOut = null;
try {
tmpOut = new ZipOutputStream(new BufferedOutputStream(FileUtils.openOutputStream(new File(filename))));
} catch (IOException e) {
IOUtils.closeQuietly(tmpOut);
throw new RuntimeException(e);
}
final ZipOutputStream out = tmpOut;
// stash existing session
IPentahoSession origPentahoSession = PentahoSessionHolder.getSession();
// run as repo super user
PentahoSessionHolder.setSession(createRepositoryAdminPentahoSession(repositoryAdminUsername));
try {
txnTemplate.execute(new TransactionCallbackWithoutResult() {
public void doInTransactionWithoutResult(final TransactionStatus status) {
jcrTemplate.execute(new JcrCallback() {
public Object doInJcr(final Session session) throws RepositoryException, IOException {
switch(mode) {
case SYS:
{
final boolean SKIP_BINARY = false;
final boolean NO_RECURSE = false;
// $NON-NLS-1$
out.putNextEntry(new ZipEntry("repository.xml"));
// $NON-NLS-1$
session.exportSystemView("/", out, SKIP_BINARY, NO_RECURSE);
return null;
}
case DOC:
{
final boolean SKIP_BINARY = false;
final boolean NO_RECURSE = false;
// $NON-NLS-1$
out.putNextEntry(new ZipEntry("repository.xml"));
// $NON-NLS-1$
session.exportDocumentView("/", out, SKIP_BINARY, NO_RECURSE);
return null;
}
default:
{
// $NON-NLS-1$
out.putNextEntry(new ZipEntry("repository.txt"));
session.getRootNode().accept(new DumpToFileTraversingItemVisitor(out));
return null;
}
}
}
});
}
});
} finally {
// restore original session
PentahoSessionHolder.setSession(origPentahoSession);
IOUtils.closeQuietly(out);
}
// $NON-NLS-1$
logger.debug(String.format("dumped repository to file \"%s\"", filename));
}
}
use of org.springframework.extensions.jcr.JcrCallback in project pentaho-platform by pentaho.
the class JcrRepositoryFileAclDao method createAcl.
public RepositoryFileAcl createAcl(final Serializable fileId, final RepositoryFileAcl acl) {
if (isKioskEnabled()) {
// $NON-NLS-1$
throw new RuntimeException(Messages.getInstance().getString("JcrRepositoryFileDao.ERROR_0006_ACCESS_DENIED"));
}
return (RepositoryFileAcl) jcrTemplate.execute(new JcrCallback() {
public Object doInJcr(final Session session) throws RepositoryException, IOException {
PentahoJcrConstants pentahoJcrConstants = new PentahoJcrConstants(session);
Node node = session.getNodeByIdentifier(fileId.toString());
String absPath = node.getPath();
AccessControlManager acMgr = session.getAccessControlManager();
AccessControlList acList = getAccessControlList(acMgr, absPath);
acMgr.setPolicy(absPath, acList);
return internalUpdateAcl(session, pentahoJcrConstants, fileId, acl);
}
});
}
use of org.springframework.extensions.jcr.JcrCallback in project pentaho-platform by pentaho.
the class JcrRepositoryFileAclDao method updateAcl.
public RepositoryFileAcl updateAcl(final RepositoryFileAcl acl) {
return (RepositoryFileAcl) jcrTemplate.execute(new JcrCallback() {
public Object doInJcr(final Session session) throws RepositoryException, IOException {
PentahoJcrConstants pentahoJcrConstants = new PentahoJcrConstants(session);
JcrRepositoryFileUtils.checkoutNearestVersionableFileIfNecessary(session, pentahoJcrConstants, acl.getId());
RepositoryFileAcl updatedAcl = internalUpdateAcl(session, pentahoJcrConstants, acl.getId(), acl);
JcrRepositoryFileUtils.checkinNearestVersionableFileIfNecessary(session, pentahoJcrConstants, acl.getId(), null, null, true);
return updatedAcl;
}
});
}
use of org.springframework.extensions.jcr.JcrCallback in project pentaho-platform by pentaho.
the class JcrRepositoryFileAclDao method getParentAcl.
protected RepositoryFileAcl getParentAcl(final Serializable id) {
return (RepositoryFileAcl) jcrTemplate.execute(new JcrCallback() {
public Object doInJcr(final Session session) throws RepositoryException, IOException {
PentahoJcrConstants pentahoJcrConstants = new PentahoJcrConstants(session);
Node node = session.getNodeByIdentifier(id.toString());
if (!node.getParent().isSame(session.getRootNode())) {
return toAcl(session, pentahoJcrConstants, node.getParent().getIdentifier());
} else {
return null;
}
}
});
}
use of org.springframework.extensions.jcr.JcrCallback in project pentaho-platform by pentaho.
the class JcrRepositoryFileDao method internalCopyOrMove.
private void internalCopyOrMove(final Serializable fileId, final String destRelPath, final String versionMessage, final boolean copy) {
if (isKioskEnabled()) {
// $NON-NLS-1$
throw new RuntimeException(Messages.getInstance().getString("JcrRepositoryFileDao.ERROR_0006_ACCESS_DENIED"));
}
Assert.notNull(fileId);
jcrTemplate.execute(new JcrCallback() {
@Override
public Object doInJcr(final Session session) throws RepositoryException, IOException {
// if we're moving the file,
// check that user has permissions to remove the file from it's current location
RepositoryFile file = getFileById(fileId);
if (!copy) {
RepositoryFileAcl acl = aclDao.getAcl(fileId);
if (!accessVoterManager.hasAccess(file, RepositoryFilePermission.WRITE, acl, PentahoSessionHolder.getSession())) {
throw new AccessDeniedException(Messages.getInstance().getString("JcrRepositoryFileDao.ERROR_0006_ACCESS_DENIED_DELETE", fileId));
}
}
// check that user has permissions to write to the destination folder
RepositoryFile destFolder = getFile(destRelPath);
if (destFolder != null) {
RepositoryFileAcl destFolderAcl = aclDao.getAcl(destFolder.getId());
if (!accessVoterManager.hasAccess(destFolder, RepositoryFilePermission.WRITE, destFolderAcl, PentahoSessionHolder.getSession())) {
throw new AccessDeniedException(Messages.getInstance().getString("JcrRepositoryFileDao.ERROR_0006_ACCESS_DENIED_CREATE", destFolder.getId()));
}
}
PentahoJcrConstants pentahoJcrConstants = new PentahoJcrConstants(session);
String destAbsPath = pathConversionHelper.relToAbs(destRelPath);
String cleanDestAbsPath = destAbsPath;
if (cleanDestAbsPath.endsWith(RepositoryFile.SEPARATOR)) {
cleanDestAbsPath.substring(0, cleanDestAbsPath.length() - 1);
}
Node srcFileNode = session.getNodeByIdentifier(fileId.toString());
Serializable srcParentFolderId = JcrRepositoryFileUtils.getParentId(session, fileId);
boolean appendFileName = false;
boolean destExists = true;
Node destFileNode = null;
Node destParentFolderNode = null;
try {
destFileNode = (Node) session.getItem(JcrStringHelper.pathEncode(cleanDestAbsPath));
} catch (PathNotFoundException e) {
destExists = false;
}
if (destExists) {
// make sure it's a file or folder
Assert.isTrue(JcrRepositoryFileUtils.isSupportedNodeType(pentahoJcrConstants, destFileNode));
// existing item; make sure src is not a folder if dest is a file
Assert.isTrue(!(JcrRepositoryFileUtils.isPentahoFolder(pentahoJcrConstants, srcFileNode) && JcrRepositoryFileUtils.isPentahoFile(pentahoJcrConstants, destFileNode)), Messages.getInstance().getString(// $NON-NLS-1$
"JcrRepositoryFileDao.ERROR_0002_CANNOT_OVERWRITE_FILE_WITH_FOLDER"));
if (JcrRepositoryFileUtils.isPentahoFolder(pentahoJcrConstants, destFileNode)) {
// existing item; caller is not renaming file, only moving it
appendFileName = true;
destParentFolderNode = destFileNode;
} else {
// get parent of existing dest item
int lastSlashIndex = cleanDestAbsPath.lastIndexOf(RepositoryFile.SEPARATOR);
Assert.isTrue(lastSlashIndex > 1, Messages.getInstance().getString(// $NON-NLS-1$
"JcrRepositoryFileDao.ERROR_0003_ILLEGAL_DEST_PATH"));
String absPathToDestParentFolder = cleanDestAbsPath.substring(0, lastSlashIndex);
destParentFolderNode = (Node) session.getItem(JcrStringHelper.pathEncode(absPathToDestParentFolder));
}
} else {
// destination doesn't exist; go up one level to a folder that does exist
int lastSlashIndex = cleanDestAbsPath.lastIndexOf(RepositoryFile.SEPARATOR);
Assert.isTrue(lastSlashIndex > 1, Messages.getInstance().getString(// $NON-NLS-1$
"JcrRepositoryFileDao.ERROR_0003_ILLEGAL_DEST_PATH"));
String absPathToDestParentFolder = cleanDestAbsPath.substring(0, lastSlashIndex);
// JcrRepositoryFileUtils.checkName( cleanDestAbsPath.substring( lastSlashIndex + 1 ) );
try {
destParentFolderNode = (Node) session.getItem(JcrStringHelper.pathEncode(absPathToDestParentFolder));
} catch (PathNotFoundException e1) {
Assert.isTrue(false, Messages.getInstance().getString(// $NON-NLS-1$
"JcrRepositoryFileDao.ERROR_0004_PARENT_MUST_EXIST"));
}
Assert.isTrue(JcrRepositoryFileUtils.isPentahoFolder(pentahoJcrConstants, destParentFolderNode), Messages.getInstance().getString(// $NON-NLS-1$
"JcrRepositoryFileDao.ERROR_0005_PARENT_MUST_BE_FOLDER"));
}
if (!copy) {
JcrRepositoryFileUtils.checkoutNearestVersionableFileIfNecessary(session, pentahoJcrConstants, srcParentFolderId);
}
JcrRepositoryFileUtils.checkoutNearestVersionableNodeIfNecessary(session, pentahoJcrConstants, destParentFolderNode);
String finalEncodedSrcAbsPath = srcFileNode.getPath();
String finalEncodedDestAbsPath = null;
if (appendFileName) {
final String fileName = srcFileNode.getName();
if (JcrStringHelper.isEncoded(fileName)) {
finalEncodedDestAbsPath = JcrStringHelper.pathEncode(cleanDestAbsPath) + RepositoryFile.SEPARATOR + fileName;
} else {
finalEncodedDestAbsPath = JcrStringHelper.pathEncode(cleanDestAbsPath + RepositoryFile.SEPARATOR + fileName);
}
} else {
finalEncodedDestAbsPath = JcrStringHelper.pathEncode(cleanDestAbsPath);
}
try {
if (copy) {
session.getWorkspace().copy(finalEncodedSrcAbsPath, finalEncodedDestAbsPath);
} else {
session.getWorkspace().move(finalEncodedSrcAbsPath, finalEncodedDestAbsPath);
}
} catch (ItemExistsException iae) {
throw new UnifiedRepositoryException((file.isFolder() ? "Folder " : "File ") + "with path [" + cleanDestAbsPath + "] already exists in the repository");
}
JcrRepositoryFileUtils.checkinNearestVersionableNodeIfNecessary(session, pentahoJcrConstants, destParentFolderNode, versionMessage);
// if it's a move within the same folder, then the next checkin is unnecessary
if (!copy && !destParentFolderNode.getIdentifier().equals(srcParentFolderId.toString())) {
JcrRepositoryFileUtils.checkinNearestVersionableFileIfNecessary(session, pentahoJcrConstants, srcParentFolderId, versionMessage);
}
session.save();
return null;
}
});
}
Aggregations