Search in sources :

Example 51 with SessionImpl

use of org.apache.jackrabbit.core.SessionImpl in project jackrabbit by apache.

the class GCEventListenerTest method doTestEventListener.

private void doTestEventListener(boolean allowPmScan) throws Exception {
    Node root = testRootNode;
    Session session = root.getSession();
    if (root.hasNode(TEST_NODE_NAME)) {
        root.getNode(TEST_NODE_NAME).remove();
        session.save();
    }
    Node test = root.addNode(TEST_NODE_NAME);
    Random random = new Random();
    byte[] data = new byte[10000];
    for (int i = 0; i < 10; i++) {
        Node n = test.addNode("x" + i);
        random.nextBytes(data);
        ValueFactory vf = session.getValueFactory();
        n.setProperty("data", vf.createBinary(new ByteArrayInputStream(data)));
        session.save();
        if (i % 2 == 0) {
            n.remove();
            session.save();
        }
    }
    session.save();
    SessionImpl si = (SessionImpl) session;
    DataStoreGarbageCollector gc = si.createDataStoreGarbageCollector();
    DataStore ds = ((GarbageCollector) gc).getDataStore();
    if (ds != null) {
        ds.clearInUse();
        boolean pmScan = gc.isPersistenceManagerScan();
        gc.setPersistenceManagerScan(allowPmScan);
        gotNullNode = false;
        gotNode = false;
        gc.setMarkEventListener(this);
        gc.mark();
        if (pmScan && allowPmScan) {
            assertTrue("PM scan without null Node", gotNullNode);
            assertFalse("PM scan, but got a real node", gotNode);
        } else {
            assertFalse("Not a PM scan - but got a null Node", gotNullNode);
            assertTrue("Not a PM scan - without a real node", gotNode);
        }
        int deleted = gc.sweep();
        LOG.debug("Deleted " + deleted);
        assertTrue("Should delete at least one item", deleted >= 0);
        gc.close();
    }
}
Also used : Random(java.util.Random) ByteArrayInputStream(java.io.ByteArrayInputStream) Node(javax.jcr.Node) ValueFactory(javax.jcr.ValueFactory) SessionImpl(org.apache.jackrabbit.core.SessionImpl) Session(javax.jcr.Session) DataStoreGarbageCollector(org.apache.jackrabbit.api.management.DataStoreGarbageCollector) DataStoreGarbageCollector(org.apache.jackrabbit.api.management.DataStoreGarbageCollector) GarbageCollector(org.apache.jackrabbit.core.gc.GarbageCollector)

Example 52 with SessionImpl

use of org.apache.jackrabbit.core.SessionImpl in project jackrabbit by apache.

the class AdministratorTest method testAdminNodeCollidingWithRandomNode.

/**
     * Test for collisions that would prevent from recreate the admin user.
     * - a colliding node somewhere else with the same jcr:uuid.
     *
     * Test if creation of the administrator user forces the removal of some
     * other node in the repository that by change happens to have the same
     * jcr:uuid and thus inhibits the creation of the admininstrator user.
     */
public void testAdminNodeCollidingWithRandomNode() throws RepositoryException, NotExecutableException {
    Authorizable admin = userMgr.getAuthorizable(adminId);
    if (admin == null || !(admin instanceof AuthorizableImpl)) {
        throw new NotExecutableException();
    }
    // access the node corresponding to the admin user and remove it
    NodeImpl adminNode = ((AuthorizableImpl) admin).getNode();
    NodeId nid = adminNode.getNodeId();
    Session s = adminNode.getSession();
    adminNode.remove();
    // use session obtained from the node as usermgr may point to a dedicated
    // system workspace different from the superusers workspace.
    s.save();
    Session s2 = null;
    String collidingPath = null;
    try {
        // create a colliding node outside of the user tree
        NameResolver nr = (SessionImpl) s;
        // NOTE: testRootNode will not be present if users are stored in a distinct wsp.
        //       therefore use root node as start...
        NodeImpl tr = (NodeImpl) s.getRootNode();
        Node n = tr.addNode(nr.getQName("tmpNode"), nr.getQName(testNodeType), nid);
        collidingPath = n.getPath();
        s.save();
        // force recreation of admin user.
        s2 = getHelper().getSuperuserSession();
        admin = userMgr.getAuthorizable(adminId);
        assertNotNull(admin);
        // the colliding node must have been removed.
        assertFalse(s2.nodeExists(collidingPath));
    } finally {
        if (s2 != null) {
            s2.logout();
        }
        if (collidingPath != null && s.nodeExists(collidingPath)) {
            s.getNode(collidingPath).remove();
            s.save();
        }
    }
}
Also used : NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) NodeImpl(org.apache.jackrabbit.core.NodeImpl) Node(javax.jcr.Node) NodeId(org.apache.jackrabbit.core.id.NodeId) Authorizable(org.apache.jackrabbit.api.security.user.Authorizable) SessionImpl(org.apache.jackrabbit.core.SessionImpl) NameResolver(org.apache.jackrabbit.spi.commons.conversion.NameResolver) Session(javax.jcr.Session)

Example 53 with SessionImpl

use of org.apache.jackrabbit.core.SessionImpl in project jackrabbit by apache.

the class NodeResolverTest method testFindNodes.

public void testFindNodes() throws NotExecutableException, RepositoryException {
    Value[] vs = new Value[] { superuser.getValueFactory().createValue("blub"), superuser.getValueFactory().createValue("blib") };
    UserImpl currentUser = getCurrentUser();
    currentUser.setProperty(propertyName1, vs);
    int expResultSize = 1;
    Iterator<Group> it = currentUser.memberOf();
    while (it.hasNext()) {
        GroupImpl gr = (GroupImpl) it.next();
        gr.setProperty(propertyName1, vs);
        expResultSize++;
    }
    save();
    Name propName = ((SessionImpl) superuser).getQName(propertyName1);
    try {
        NodeResolver nr = createNodeResolver(currentUser.getNode().getSession());
        NodeIterator result = nr.findNodes(propName, "blub", UserConstants.NT_REP_USER, false);
        assertTrue("expected result", result.hasNext());
        assertEquals(currentUser.getNode().getPath(), result.nextNode().getPath());
        assertFalse("expected no more results", result.hasNext());
        result = nr.findNodes(propName, "blub", UserConstants.NT_REP_AUTHORIZABLE, false);
        assertEquals(expResultSize, getSize(result));
    } finally {
        currentUser.removeProperty(propertyName1);
        it = currentUser.memberOf();
        while (it.hasNext()) {
            GroupImpl gr = (GroupImpl) it.next();
            gr.removeProperty(propertyName1);
        }
        save();
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) Group(org.apache.jackrabbit.api.security.user.Group) Value(javax.jcr.Value) SessionImpl(org.apache.jackrabbit.core.SessionImpl) Name(org.apache.jackrabbit.spi.Name)

Example 54 with SessionImpl

use of org.apache.jackrabbit.core.SessionImpl in project jackrabbit by apache.

the class NodeResolverTest method testFindNodesByRelPathProperties.

/**
     * 
     * @throws NotExecutableException
     * @throws RepositoryException
     */
public void testFindNodesByRelPathProperties() throws NotExecutableException, RepositoryException {
    Value[] vs = new Value[] { superuser.getValueFactory().createValue("blub"), superuser.getValueFactory().createValue("blib") };
    String relPath = "relPath/" + propertyName1;
    String relPath2 = "another/" + propertyName1;
    String relPath3 = "relPath/relPath/" + propertyName1;
    UserImpl currentUser = getCurrentUser();
    currentUser.setProperty(relPath, vs);
    currentUser.setProperty(relPath2, vs);
    currentUser.setProperty(relPath3, vs);
    save();
    Path relQPath = ((SessionImpl) superuser).getQPath(relPath);
    Path relQName = ((SessionImpl) superuser).getQPath(propertyName1);
    try {
        NodeResolver nr = createNodeResolver(currentUser.getNode().getSession());
        // 1) findNodes(QPath.....
        // relPath : "prop1" -> should find the currentuserNode
        NodeIterator result = nr.findNodes(relQName, "blub", UserManager.SEARCH_TYPE_USER, false, Long.MAX_VALUE);
        assertTrue("expected result", result.hasNext());
        Node n = result.nextNode();
        assertEquals(currentUser.getNode().getPath(), n.getPath());
        assertFalse("expected no more results", result.hasNext());
        // relPath : "relPath/prop1" -> should find the currentuserNode
        result = nr.findNodes(relQPath, "blub", UserManager.SEARCH_TYPE_USER, false, Long.MAX_VALUE);
        assertTrue("expected result", result.hasNext());
        assertEquals(currentUser.getNode().getPath(), result.nextNode().getPath());
        assertFalse("expected no more results", result.hasNext());
        // 2) findNodes(Name.......)
        // search by Name -> should not find deep property
        Name propName = ((SessionImpl) superuser).getQName(propertyName1);
        result = nr.findNodes(propName, "blub", UserConstants.NT_REP_USER, false);
        assertFalse("should not find result", result.hasNext());
    } finally {
        currentUser.removeProperty(relPath);
        currentUser.removeProperty(relPath2);
        currentUser.removeProperty(relPath3);
        save();
    }
}
Also used : Path(org.apache.jackrabbit.spi.Path) NodeIterator(javax.jcr.NodeIterator) Node(javax.jcr.Node) Value(javax.jcr.Value) SessionImpl(org.apache.jackrabbit.core.SessionImpl) Name(org.apache.jackrabbit.spi.Name)

Example 55 with SessionImpl

use of org.apache.jackrabbit.core.SessionImpl in project jackrabbit by apache.

the class IndexNodeResolverTest method testFindNodesExactWithApostrophe.

public void testFindNodesExactWithApostrophe() throws NotExecutableException, RepositoryException {
    UserImpl currentUser = getCurrentUser();
    Value vs = superuser.getValueFactory().createValue("value ' with apostrophe");
    currentUser.setProperty(propertyName1, vs);
    save();
    Name propName = ((SessionImpl) superuser).getQName(propertyName1);
    try {
        NodeResolver nr = createNodeResolver(currentUser.getNode().getSession());
        NodeIterator result = nr.findNodes(propName, "value ' with apostrophe", UserConstants.NT_REP_USER, true);
        assertTrue("expected result", result.hasNext());
        assertEquals(currentUser.getNode().getPath(), result.nextNode().getPath());
        assertFalse("expected no more results", result.hasNext());
    } finally {
        currentUser.removeProperty(propertyName1);
        save();
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) Value(javax.jcr.Value) SessionImpl(org.apache.jackrabbit.core.SessionImpl) Name(org.apache.jackrabbit.spi.Name)

Aggregations

SessionImpl (org.apache.jackrabbit.core.SessionImpl)66 RepositoryException (javax.jcr.RepositoryException)17 Node (javax.jcr.Node)12 Value (javax.jcr.Value)12 Name (org.apache.jackrabbit.spi.Name)12 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)11 Session (javax.jcr.Session)10 NodeImpl (org.apache.jackrabbit.core.NodeImpl)9 NodeId (org.apache.jackrabbit.core.id.NodeId)9 Principal (java.security.Principal)8 NodeIterator (javax.jcr.NodeIterator)7 Privilege (javax.jcr.security.Privilege)7 DataStoreGarbageCollector (org.apache.jackrabbit.api.management.DataStoreGarbageCollector)7 UserManager (org.apache.jackrabbit.api.security.user.UserManager)6 Path (org.apache.jackrabbit.spi.Path)6 JackrabbitAccessControlList (org.apache.jackrabbit.api.security.JackrabbitAccessControlList)5 PathMap (org.apache.jackrabbit.spi.commons.name.PathMap)5 ArrayList (java.util.ArrayList)4 Authorizable (org.apache.jackrabbit.api.security.user.Authorizable)4 GarbageCollector (org.apache.jackrabbit.core.gc.GarbageCollector)4