Search in sources :

Example 16 with JcrCallback

use of org.springframework.extensions.jcr.JcrCallback in project pentaho-platform by pentaho.

the class JcrRepositoryFileDao method undeleteFile.

/**
 * {@inheritDoc}
 */
@Override
public void undeleteFile(final Serializable fileId, final String versionMessage) {
    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 {
            PentahoJcrConstants pentahoJcrConstants = new PentahoJcrConstants(session);
            String absOrigParentFolderPath = deleteHelper.getOriginalParentFolderPath(session, pentahoJcrConstants, fileId);
            Serializable origParentFolderId = null;
            RepositoryFile file = getFileById(fileId);
            RepositoryFileAcl acl = aclDao.getAcl(fileId);
            if (!accessVoterManager.hasAccess(file, RepositoryFilePermission.WRITE, acl, PentahoSessionHolder.getSession())) {
                return null;
            }
            // original parent folder path may no longer exist!
            if (session.itemExists(JcrStringHelper.pathEncode(absOrigParentFolderPath))) {
                origParentFolderId = ((Node) session.getItem(JcrStringHelper.pathEncode(absOrigParentFolderPath))).getIdentifier();
            } else {
                // go through each of the segments of the original parent folder path, creating as necessary
                String[] segments = pathConversionHelper.absToRel(absOrigParentFolderPath).split(RepositoryFile.SEPARATOR);
                RepositoryFile lastParentFolder = internalGetFile(session, ServerRepositoryPaths.getTenantRootFolderPath(), false, null);
                for (String segment : segments) {
                    if (StringUtils.hasLength(segment)) {
                        RepositoryFile tmp = internalGetFile(session, pathConversionHelper.relToAbs((lastParentFolder.getPath().equals(RepositoryFile.SEPARATOR) ? "" : lastParentFolder.getPath()) + RepositoryFile.SEPARATOR + segment), false, // $NON-NLS-1$
                        null);
                        if (tmp == null) {
                            lastParentFolder = internalCreateFolder(session, lastParentFolder.getId(), new RepositoryFile.Builder(segment).folder(true).build(), defaultAclHandler.createDefaultAcl(lastParentFolder), null);
                        } else {
                            lastParentFolder = tmp;
                        }
                    }
                }
                origParentFolderId = lastParentFolder.getId();
            }
            JcrRepositoryFileUtils.checkoutNearestVersionableFileIfNecessary(session, pentahoJcrConstants, origParentFolderId);
            deleteHelper.undeleteFile(session, pentahoJcrConstants, fileId);
            session.save();
            JcrRepositoryFileUtils.checkinNearestVersionableFileIfNecessary(session, pentahoJcrConstants, origParentFolderId, versionMessage);
            return null;
        }
    });
}
Also used : 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) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) RepositoryFileAcl(org.pentaho.platform.api.repository2.unified.RepositoryFileAcl) Session(javax.jcr.Session)

Example 17 with JcrCallback

use of org.springframework.extensions.jcr.JcrCallback in project pentaho-platform by pentaho.

the class JcrRepositoryFileDao method deleteFile.

/**
 * {@inheritDoc}
 */
@Override
public void deleteFile(final Serializable fileId, final String versionMessage) {
    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 {
            RepositoryFile fileToBeDeleted = getFileById(fileId);
            // Get repository file info and acl info of parent
            if (fileToBeDeleted != null) {
                RepositoryFileAcl toBeDeletedFileAcl = aclDao.getAcl(fileToBeDeleted.getId());
                // Invoke accessVoterManager to see if we have access to perform this operation
                if (!accessVoterManager.hasAccess(fileToBeDeleted, RepositoryFilePermission.DELETE, toBeDeletedFileAcl, PentahoSessionHolder.getSession())) {
                    return null;
                }
            }
            List<RepositoryFilePermission> perms = new ArrayList<RepositoryFilePermission>();
            perms.add(RepositoryFilePermission.DELETE);
            if (!aclDao.hasAccess(fileToBeDeleted.getPath(), EnumSet.copyOf(perms))) {
                throw new AccessDeniedException(Messages.getInstance().getString("JcrRepositoryFileDao.ERROR_0006_ACCESS_DENIED_DELETE", fileId));
            }
            PentahoJcrConstants pentahoJcrConstants = new PentahoJcrConstants(session);
            Serializable parentFolderId = JcrRepositoryFileUtils.getParentId(session, fileId);
            JcrRepositoryFileUtils.checkoutNearestVersionableFileIfNecessary(session, pentahoJcrConstants, parentFolderId);
            deleteHelper.deleteFile(session, pentahoJcrConstants, fileId);
            session.save();
            JcrRepositoryFileUtils.checkinNearestVersionableFileIfNecessary(session, pentahoJcrConstants, parentFolderId, versionMessage);
            return null;
        }
    });
}
Also used : AccessDeniedException(javax.jcr.AccessDeniedException) Serializable(java.io.Serializable) UnifiedRepositoryException(org.pentaho.platform.api.repository2.unified.UnifiedRepositoryException) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) JcrCallback(org.springframework.extensions.jcr.JcrCallback) RepositoryFilePermission(org.pentaho.platform.api.repository2.unified.RepositoryFilePermission) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) ArrayList(java.util.ArrayList) List(java.util.List) RepositoryFileAcl(org.pentaho.platform.api.repository2.unified.RepositoryFileAcl) Session(javax.jcr.Session)

Example 18 with JcrCallback

use of org.springframework.extensions.jcr.JcrCallback in project pentaho-platform by pentaho.

the class DumpToFilePentahoSystemListener method startup.

// ~ Instance fields
// =================================================================================================
// ~ Constructors
// ====================================================================================================
// ~ Methods
// =========================================================================================================
@Override
public boolean startup(IPentahoSession pentahoSession) {
    Mode tmpMode = null;
    if (!StringUtils.hasText(fileName)) {
        fileName = System.getProperty(PROP_DUMP_TO_FILE);
        tmpMode = Mode.CUSTOM;
        if (fileName == null) {
            fileName = System.getProperty(PROP_DUMP_TO_FILE_SYSTEM_VIEW);
            tmpMode = Mode.SYS;
        }
        if (fileName == null) {
            fileName = System.getProperty(PROP_DUMP_TO_FILE_DOCUMENT_VIEW);
            tmpMode = Mode.DOC;
        }
    } else {
        tmpMode = Mode.CUSTOM;
    }
    final Mode mode = tmpMode;
    if (fileName != null) {
        // $NON-NLS-1$
        final JcrTemplate jcrTemplate = PentahoSystem.get(JcrTemplate.class, "jcrTemplate", pentahoSession);
        TransactionTemplate txnTemplate = // $NON-NLS-1$
        PentahoSystem.get(TransactionTemplate.class, "jcrTransactionTemplate", pentahoSession);
        // $NON-NLS-1$
        String repositoryAdminUsername = PentahoSystem.get(String.class, "repositoryAdminUsername", pentahoSession);
        // $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));
    }
    return true;
}
Also used : JcrTemplate(org.springframework.extensions.jcr.JcrTemplate) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) ZipEntry(java.util.zip.ZipEntry) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) 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 19 with JcrCallback

use of org.springframework.extensions.jcr.JcrCallback in project pentaho-platform by pentaho.

the class RepositoryTenantManager method createTenantFolder.

private RepositoryFile createTenantFolder(final ITenant parentTenant, final String tenantName, final String tenantCreatorId) {
    return (RepositoryFile) jcrTemplate.execute(new JcrCallback() {

        @Override
        public Object doInJcr(final Session session) throws RepositoryException {
            Tenant tenant = null;
            RepositoryFile parentFolder = null;
            if (parentTenant == null) {
                tenant = new Tenant("/" + tenantName, true);
            } else {
                tenant = new Tenant(parentTenant.getRootFolderAbsolutePath() + "/" + tenantName, true);
                String folderPath = parentTenant.getRootFolderAbsolutePath();
                parentFolder = repositoryFileDao.getFileByAbsolutePath(folderPath);
            }
            RepositoryFileAcl acl = new RepositoryFileAcl.Builder(tenantCreatorId).entriesInheriting(false).build();
            RepositoryFile systemTenantFolder = repositoryFileDao.createFolder(parentFolder != null ? parentFolder.getId() : null, new RepositoryFile.Builder(tenant.getName()).folder(true).build(), acl, "");
            repositoryFileDao.getFileByAbsolutePath(tenant.getId());
            Map<String, Serializable> fileMeta = repositoryFileDao.getFileMetadata(systemTenantFolder.getId());
            fileMeta.put(ITenantManager.TENANT_ROOT, true);
            fileMeta.put(ITenantManager.TENANT_ENABLED, true);
            JcrRepositoryFileUtils.setFileMetadata(session, systemTenantFolder.getId(), fileMeta);
            createRuntimeRolesFolderNode(session, new PentahoJcrConstants(session), tenant);
            return systemTenantFolder;
        }
    });
}
Also used : Serializable(java.io.Serializable) Tenant(org.pentaho.platform.core.mt.Tenant) ITenant(org.pentaho.platform.api.mt.ITenant) PentahoJcrConstants(org.pentaho.platform.repository2.unified.jcr.PentahoJcrConstants) Builder(org.pentaho.platform.api.repository2.unified.RepositoryFileAcl.Builder) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) JcrCallback(org.springframework.extensions.jcr.JcrCallback) RepositoryFileAcl(org.pentaho.platform.api.repository2.unified.RepositoryFileAcl) Session(javax.jcr.Session) StandaloneSession(org.pentaho.platform.engine.core.system.StandaloneSession) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession)

Example 20 with JcrCallback

use of org.springframework.extensions.jcr.JcrCallback in project pentaho-platform by pentaho.

the class AbstractBackingRepositoryLifecycleManager method addMetadataToRepository.

public void addMetadataToRepository(final String metadataProperty) {
    txnTemplate.execute(new TransactionCallbackWithoutResult() {

        public void doInTransactionWithoutResult(final TransactionStatus status) {
            adminJcrTemplate.execute(new JcrCallback() {

                @Override
                public Object doInJcr(Session session) throws IOException, RepositoryException {
                    new PentahoJcrConstants(session);
                    String absPath = ServerRepositoryPaths.getPentahoRootFolderPath();
                    RepositoryFile rootFolder = JcrRepositoryFileUtils.getFileByAbsolutePath(session, absPath, pathConversionHelper, null, false, null);
                    if (rootFolder != null) {
                        Map<String, Serializable> metadataMap = JcrRepositoryFileUtils.getFileMetadata(session, rootFolder.getId());
                        if (metadataMap == null) {
                            metadataMap = new HashMap<String, Serializable>();
                        }
                        metadataMap.put(metadataProperty, Boolean.TRUE);
                        JcrRepositoryFileUtils.setFileMetadata(session, rootFolder.getId(), metadataMap);
                    } else {
                        throw new IllegalStateException("Repository has not been initialized properly");
                    }
                    session.save();
                    return null;
                }
            });
        }
    });
}
Also used : Serializable(java.io.Serializable) PentahoJcrConstants(org.pentaho.platform.repository2.unified.jcr.PentahoJcrConstants) TransactionStatus(org.springframework.transaction.TransactionStatus) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) JcrCallback(org.springframework.extensions.jcr.JcrCallback) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) 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