Search in sources :

Example 31 with CommitFailedException

use of org.apache.jackrabbit.oak.api.CommitFailedException in project jackrabbit-oak by apache.

the class PrivilegeValidatorTest method testBitsConflict.

@Test
public void testBitsConflict() {
    try {
        Tree privTree = createPrivilegeTree("test");
        bitsProvider.getBits(JCR_READ).writeTo(privTree);
        root.commit();
        fail("Conflicting privilege bits property must be detected.");
    } catch (CommitFailedException e) {
        // success
        assertTrue(e.isConstraintViolation());
        assertEquals(49, e.getCode());
    } finally {
        root.refresh();
    }
}
Also used : Tree(org.apache.jackrabbit.oak.api.Tree) CommitFailedException(org.apache.jackrabbit.oak.api.CommitFailedException) AbstractSecurityTest(org.apache.jackrabbit.oak.AbstractSecurityTest) Test(org.junit.Test)

Example 32 with CommitFailedException

use of org.apache.jackrabbit.oak.api.CommitFailedException in project jackrabbit-oak by apache.

the class NodeStoreTest method rebaseWithFailedMerge.

// OAK-1320
@Test
public void rebaseWithFailedMerge() throws CommitFailedException {
    NodeBuilder rootBuilder = store.getRoot().builder();
    rootBuilder.child("foo");
    // commit something in between to force rebase
    NodeBuilder b = store.getRoot().builder();
    b.child("bar");
    store.merge(b, EmptyHook.INSTANCE, CommitInfo.EMPTY);
    try {
        store.merge(rootBuilder, new CommitHook() {

            @Nonnull
            @Override
            public NodeState processCommit(NodeState before, NodeState after, CommitInfo info) throws CommitFailedException {
                throw new CommitFailedException("", 0, "commit rejected");
            }
        }, CommitInfo.EMPTY);
        fail("must throw CommitFailedException");
    } catch (CommitFailedException e) {
    // expected
    }
    // merge again
    NodeState root = store.merge(rootBuilder, EmptyHook.INSTANCE, CommitInfo.EMPTY);
    assertTrue(root.hasChildNode("bar"));
}
Also used : Nonnull(javax.annotation.Nonnull) CommitHook(org.apache.jackrabbit.oak.spi.commit.CommitHook) CommitInfo(org.apache.jackrabbit.oak.spi.commit.CommitInfo) CommitFailedException(org.apache.jackrabbit.oak.api.CommitFailedException) Test(org.junit.Test) OakBaseTest(org.apache.jackrabbit.oak.OakBaseTest)

Example 33 with CommitFailedException

use of org.apache.jackrabbit.oak.api.CommitFailedException in project jackrabbit-oak by apache.

the class NodeDelegate method unlock.

public void unlock() throws RepositoryException {
    String path = getPath();
    Root root = sessionDelegate.getContentSession().getLatestRoot();
    Tree tree = root.getTree(path);
    if (!tree.exists()) {
        throw new ItemNotFoundException("Node " + path + " does not exist");
    } else if (!isNodeType(tree, MIX_LOCKABLE, root)) {
        throw new LockException("Node " + path + " is not lockable");
    } else if (!tree.hasProperty(JCR_LOCKISDEEP)) {
        throw new LockException("Node " + path + " is not locked");
    }
    try {
        tree.removeProperty(JCR_LOCKISDEEP);
        tree.removeProperty(JCR_LOCKOWNER);
        sessionDelegate.commit(root);
    } catch (CommitFailedException e) {
        if (e.isAccessViolation()) {
            throw new AccessControlException("Access denied to unlock node " + path, e);
        } else {
            throw new RepositoryException("Unable to unlock node " + path, e);
        }
    }
}
Also used : Root(org.apache.jackrabbit.oak.api.Root) LockException(javax.jcr.lock.LockException) Tree(org.apache.jackrabbit.oak.api.Tree) AccessControlException(javax.jcr.security.AccessControlException) RepositoryException(javax.jcr.RepositoryException) CommitFailedException(org.apache.jackrabbit.oak.api.CommitFailedException) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 34 with CommitFailedException

use of org.apache.jackrabbit.oak.api.CommitFailedException in project jackrabbit-oak by apache.

the class NodeDelegate method lock.

public void lock(boolean isDeep) throws RepositoryException {
    String path = getPath();
    Root root = sessionDelegate.getContentSession().getLatestRoot();
    Tree tree = root.getTree(path);
    if (!tree.exists()) {
        throw new ItemNotFoundException("Node " + path + " does not exist");
    } else if (!isNodeType(tree, MIX_LOCKABLE, root)) {
        throw new LockException("Node " + path + " is not lockable");
    } else if (tree.hasProperty(JCR_LOCKISDEEP)) {
        throw new LockException("Node " + path + " is already locked");
    }
    // look for locked ancestor
    Tree inheritedLock = findLock(tree, true);
    if (inheritedLock != null) {
        throw new LockException("Node already indirectly locked by " + inheritedLock.getPath());
    }
    // scan for locked descendant
    if (isDeep) {
        Tree descendantLock = findDescendantLock(tree);
        if (descendantLock != null) {
            throw new LockException("Lock conflicts with lock hold by " + descendantLock.getPath());
        }
    }
    try {
        String owner = sessionDelegate.getAuthInfo().getUserID();
        if (owner == null) {
            owner = "";
        }
        tree.setProperty(JCR_LOCKISDEEP, isDeep);
        tree.setProperty(JCR_LOCKOWNER, owner);
        sessionDelegate.commit(root);
    } catch (CommitFailedException e) {
        if (e.isAccessViolation()) {
            throw new AccessControlException("Access denied to lock node " + path, e);
        } else {
            throw new RepositoryException("Unable to lock node " + path, e);
        }
    }
}
Also used : Root(org.apache.jackrabbit.oak.api.Root) LockException(javax.jcr.lock.LockException) Tree(org.apache.jackrabbit.oak.api.Tree) AccessControlException(javax.jcr.security.AccessControlException) RepositoryException(javax.jcr.RepositoryException) CommitFailedException(org.apache.jackrabbit.oak.api.CommitFailedException) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 35 with CommitFailedException

use of org.apache.jackrabbit.oak.api.CommitFailedException in project jackrabbit-oak by apache.

the class SlowObservationIT method initJcr.

@Override
protected Jcr initJcr(Jcr jcr) {
    CommitRateLimiter limiter = new CommitRateLimiter() {

        long lastLog;

        @Override
        public void setDelay(long delay) {
            long now = System.currentTimeMillis();
            if (now > lastLog + 1000) {
                log("Delay " + delay);
                lastLog = now;
            }
            super.setDelay(delay);
        }

        @Override
        protected void delay() throws CommitFailedException {
            if (!NO_DELAY_JUST_BLOCK) {
                // default behavior
                super.delay();
                return;
            }
            if (getBlockCommits() && isThreadBlocking()) {
                synchronized (this) {
                    try {
                        while (getBlockCommits()) {
                            wait(1000);
                        }
                    } catch (InterruptedException e) {
                        throw new CommitFailedException(CommitFailedException.OAK, 2, "Interrupted while waiting to commit", e);
                    }
                }
            }
        }
    };
    return super.initJcr(jcr).with(limiter);
}
Also used : CommitRateLimiter(org.apache.jackrabbit.oak.plugins.observation.CommitRateLimiter) CommitFailedException(org.apache.jackrabbit.oak.api.CommitFailedException)

Aggregations

CommitFailedException (org.apache.jackrabbit.oak.api.CommitFailedException)246 Test (org.junit.Test)166 Tree (org.apache.jackrabbit.oak.api.Tree)75 AbstractSecurityTest (org.apache.jackrabbit.oak.AbstractSecurityTest)66 NodeBuilder (org.apache.jackrabbit.oak.spi.state.NodeBuilder)60 NodeUtil (org.apache.jackrabbit.oak.util.NodeUtil)59 Root (org.apache.jackrabbit.oak.api.Root)48 NodeState (org.apache.jackrabbit.oak.spi.state.NodeState)42 RepositoryException (javax.jcr.RepositoryException)17 PropertyState (org.apache.jackrabbit.oak.api.PropertyState)13 EditorHook (org.apache.jackrabbit.oak.spi.commit.EditorHook)13 EmptyNodeState (org.apache.jackrabbit.oak.plugins.memory.EmptyNodeState)12 Nonnull (javax.annotation.Nonnull)10 Authorizable (org.apache.jackrabbit.api.security.user.Authorizable)10 MemoryDocumentStore (org.apache.jackrabbit.oak.plugins.document.memory.MemoryDocumentStore)10 TokenInfo (org.apache.jackrabbit.oak.spi.security.authentication.token.TokenInfo)10 CommitInfo (org.apache.jackrabbit.oak.spi.commit.CommitInfo)9 ArrayList (java.util.ArrayList)8 ContentSession (org.apache.jackrabbit.oak.api.ContentSession)8 UserManager (org.apache.jackrabbit.api.security.user.UserManager)7