Search in sources :

Example 6 with JcrCallback

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));
    }
}
Also used : IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) ZipEntry(java.util.zip.ZipEntry) TransactionStatus(org.springframework.transaction.TransactionStatus) IOException(java.io.IOException) JcrCallback(org.springframework.extensions.jcr.JcrCallback) ZipOutputStream(java.util.zip.ZipOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) File(java.io.File) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Session(javax.jcr.Session) StandaloneSession(org.pentaho.platform.engine.core.system.StandaloneSession) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession)

Example 7 with JcrCallback

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);
        }
    });
}
Also used : AccessControlManager(javax.jcr.security.AccessControlManager) AccessControlList(javax.jcr.security.AccessControlList) Node(javax.jcr.Node) JcrCallback(org.springframework.extensions.jcr.JcrCallback) RepositoryFileAcl(org.pentaho.platform.api.repository2.unified.RepositoryFileAcl) Session(javax.jcr.Session)

Example 8 with JcrCallback

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;
        }
    });
}
Also used : JcrCallback(org.springframework.extensions.jcr.JcrCallback) RepositoryFileAcl(org.pentaho.platform.api.repository2.unified.RepositoryFileAcl) Session(javax.jcr.Session)

Example 9 with JcrCallback

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;
            }
        }
    });
}
Also used : Node(javax.jcr.Node) JcrCallback(org.springframework.extensions.jcr.JcrCallback) RepositoryFileAcl(org.pentaho.platform.api.repository2.unified.RepositoryFileAcl) Session(javax.jcr.Session)

Example 10 with JcrCallback

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;
        }
    });
}
Also used : AccessDeniedException(javax.jcr.AccessDeniedException) Serializable(java.io.Serializable) DataNode(org.pentaho.platform.api.repository2.unified.data.node.DataNode) Node(javax.jcr.Node) UnifiedRepositoryException(org.pentaho.platform.api.repository2.unified.UnifiedRepositoryException) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) JcrCallback(org.springframework.extensions.jcr.JcrCallback) ItemExistsException(javax.jcr.ItemExistsException) UnifiedRepositoryException(org.pentaho.platform.api.repository2.unified.UnifiedRepositoryException) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) PathNotFoundException(javax.jcr.PathNotFoundException) RepositoryFileAcl(org.pentaho.platform.api.repository2.unified.RepositoryFileAcl) Session(javax.jcr.Session)

Aggregations

JcrCallback (org.springframework.extensions.jcr.JcrCallback)38 Session (javax.jcr.Session)37 Node (javax.jcr.Node)18 IOException (java.io.IOException)14 RepositoryException (javax.jcr.RepositoryException)14 RepositoryFileAcl (org.pentaho.platform.api.repository2.unified.RepositoryFileAcl)12 RepositoryFile (org.pentaho.platform.api.repository2.unified.RepositoryFile)11 UnifiedRepositoryException (org.pentaho.platform.api.repository2.unified.UnifiedRepositoryException)11 DataNode (org.pentaho.platform.api.repository2.unified.data.node.DataNode)8 Item (javax.jcr.Item)7 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)6 Serializable (java.io.Serializable)5 StandaloneSession (org.pentaho.platform.engine.core.system.StandaloneSession)5 PentahoJcrConstants (org.pentaho.platform.repository2.unified.jcr.PentahoJcrConstants)4 TransactionStatus (org.springframework.transaction.TransactionStatus)4 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 AccessDeniedException (javax.jcr.AccessDeniedException)3 PathNotFoundException (javax.jcr.PathNotFoundException)3