Search in sources :

Example 26 with IPentahoSession

use of org.pentaho.platform.api.engine.IPentahoSession in project pentaho-platform by pentaho.

the class UserRoleDaoResourceTest method testRemoveRolesFromUser.

@Test
public void testRemoveRolesFromUser() {
    String user = "testUser1";
    String roles = "testRole1";
    userRoleResource = spy(userRoleResource);
    IPentahoSession session = mock(IPentahoSession.class);
    doReturn(user).when(session).getName();
    doReturn(session).when(userRoleResource).getSession();
    doNothing().when(userRoleResource).updateRolesForCurrentSession();
    Response response = userRoleResource.removeRolesFromUser(user, roles);
    assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 27 with IPentahoSession

use of org.pentaho.platform.api.engine.IPentahoSession 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 28 with IPentahoSession

use of org.pentaho.platform.api.engine.IPentahoSession in project pentaho-platform by pentaho.

the class DefaultDeleteHelper method getOrCreateTrashInternalFolderNode.

/**
 * Creates and/or returns an internal folder called {@code .trash} located just below the user's home folder.
 */
private Node getOrCreateTrashInternalFolderNode(final Session session, final PentahoJcrConstants pentahoJcrConstants) throws RepositoryException {
    IPentahoSession pentahoSession = PentahoSessionHolder.getSession();
    String tenantId = (String) pentahoSession.getAttribute(IPentahoSession.TENANT_ID_KEY);
    Tenant tenant = new Tenant(tenantId, true);
    String userName = pentahoSession.getName();
    Node userHomeFolderNode = (Node) session.getItem(ServerRepositoryPaths.getUserHomeFolderPath(tenant, JcrStringHelper.fileNameEncode(userName)));
    if (userHomeFolderNode.hasNode(FOLDER_NAME_TRASH)) {
        return userHomeFolderNode.getNode(FOLDER_NAME_TRASH);
    } else {
        return userHomeFolderNode.addNode(FOLDER_NAME_TRASH, pentahoJcrConstants.getPHO_NT_INTERNALFOLDER());
    }
}
Also used : Tenant(org.pentaho.platform.core.mt.Tenant) ITenant(org.pentaho.platform.api.mt.ITenant) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) Node(javax.jcr.Node)

Example 29 with IPentahoSession

use of org.pentaho.platform.api.engine.IPentahoSession in project pentaho-platform by pentaho.

the class DefaultDeleteHelper method permanentlyDeleteFile.

/**
 * {@inheritDoc}
 */
public void permanentlyDeleteFile(final Session session, final PentahoJcrConstants pentahoJcrConstants, final Serializable fileId) throws RepositoryException {
    Assert.notNull(fileId);
    Node fileNode = session.getNodeByIdentifier(fileId.toString());
    // guard against using a file retrieved from a more lenient session inside a more strict session
    Assert.notNull(fileNode);
    // see if anything is referencing this node; if yes, then we cannot delete it as a
    // ReferentialIntegrityException
    // will result
    Set<RepositoryFile> referrers = new HashSet<RepositoryFile>();
    PropertyIterator refIter = fileNode.getReferences();
    if (refIter.hasNext()) {
        while (refIter.hasNext()) {
            // for each referrer property, march up the tree until we find the file node to which the property belongs
            RepositoryFile referrer = getReferrerFile(session, pentahoJcrConstants, refIter.nextProperty());
            if (referrer != null) {
                referrers.add(referrer);
            }
        }
        if (!referrers.isEmpty()) {
            RepositoryFile referee = JcrRepositoryFileUtils.nodeToFile(session, pentahoJcrConstants, pathConversionHelper, lockHelper, fileNode);
            throw new RepositoryFileDaoReferentialIntegrityException(referee, referrers);
        }
    }
    // it first
    if (fileNode.isLocked()) {
        Lock lock = session.getWorkspace().getLockManager().getLock(fileNode.getPath());
        // don't need lock token anymore
        lockHelper.removeLockToken(session, pentahoJcrConstants, lock);
    }
    // if this file was non-permanently deleted, delete its containing folder too
    IPentahoSession pentahoSession = PentahoSessionHolder.getSession();
    String tenantId = (String) pentahoSession.getAttribute(IPentahoSession.TENANT_ID_KEY);
    String trashFolder = ServerRepositoryPaths.getUserHomeFolderPath(new Tenant(tenantId, true), PentahoSessionHolder.getSession().getName()) + RepositoryFile.SEPARATOR + FOLDER_NAME_TRASH;
    Node parent = fileNode.getParent();
    purgeHistory(fileNode, session, pentahoJcrConstants);
    if (fileNode.getPath().startsWith(trashFolder)) {
        // Remove the file and then the wrapper foler
        fileNode.remove();
        parent.remove();
    } else {
        fileNode.remove();
    }
}
Also used : Tenant(org.pentaho.platform.core.mt.Tenant) ITenant(org.pentaho.platform.api.mt.ITenant) RepositoryFileDaoReferentialIntegrityException(org.pentaho.platform.repository2.unified.exception.RepositoryFileDaoReferentialIntegrityException) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) Node(javax.jcr.Node) PropertyIterator(javax.jcr.PropertyIterator) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) HashSet(java.util.HashSet) Lock(javax.jcr.lock.Lock)

Example 30 with IPentahoSession

use of org.pentaho.platform.api.engine.IPentahoSession 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)

Aggregations

IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)231 StandaloneSession (org.pentaho.platform.engine.core.system.StandaloneSession)76 Test (org.junit.Test)70 Matchers.anyString (org.mockito.Matchers.anyString)40 ArrayList (java.util.ArrayList)32 ITenant (org.pentaho.platform.api.mt.ITenant)22 IOException (java.io.IOException)20 StandaloneObjectFactory (org.pentaho.platform.engine.core.system.objfac.StandaloneObjectFactory)18 File (java.io.File)17 RepositoryFile (org.pentaho.platform.api.repository2.unified.RepositoryFile)16 Before (org.junit.Before)14 OutputStream (java.io.OutputStream)13 HashMap (java.util.HashMap)13 InputStream (java.io.InputStream)12 IRuntimeContext (org.pentaho.platform.api.engine.IRuntimeContext)12 ModelInfo (org.pentaho.platform.dataaccess.datasource.wizard.models.ModelInfo)12 Domain (org.pentaho.metadata.model.Domain)11 ObjectFactoryException (org.pentaho.platform.api.engine.ObjectFactoryException)11 List (java.util.List)10 IPentahoResultSet (org.pentaho.commons.connection.IPentahoResultSet)10