Search in sources :

Example 26 with CommitFailedException

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

the class ExternalIdentityValidatorTest method testRepExternalIdMultiple.

@Test
public void testRepExternalIdMultiple() throws Exception {
    Root systemRoot = getSystemRoot();
    try {
        NodeUtil n = new NodeUtil(systemRoot.getTree(testUserPath));
        n.setStrings(ExternalIdentityConstants.REP_EXTERNAL_ID, "id", "id2");
        systemRoot.commit();
        fail("Creating rep:externalId as multiple STRING property must be detected.");
    } catch (CommitFailedException e) {
        // success
        assertEquals(75, e.getCode());
    } finally {
        systemRoot.refresh();
    }
}
Also used : Root(org.apache.jackrabbit.oak.api.Root) CommitFailedException(org.apache.jackrabbit.oak.api.CommitFailedException) NodeUtil(org.apache.jackrabbit.oak.util.NodeUtil) Test(org.junit.Test)

Example 27 with CommitFailedException

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

the class NamespaceEditor method propertyAdded.

@Override
public void propertyAdded(PropertyState after) throws CommitFailedException {
    String prefix = after.getName();
    // ignore jcr:primaryType
    if (JCR_PRIMARYTYPE.equals(prefix)) {
        return;
    }
    if (namespaces.hasProperty(prefix)) {
        throw new CommitFailedException(CommitFailedException.NAMESPACE, 1, "Namespace mapping already registered: " + prefix);
    } else if (isValidPrefix(prefix)) {
        if (after.isArray() || !STRING.equals(after.getType())) {
            throw new CommitFailedException(CommitFailedException.NAMESPACE, 2, "Invalid namespace mapping: " + prefix);
        } else if (prefix.toLowerCase(Locale.ENGLISH).startsWith("xml")) {
            throw new CommitFailedException(CommitFailedException.NAMESPACE, 3, "XML prefixes are reserved: " + prefix);
        } else if (containsValue(namespaces, after.getValue(STRING))) {
            throw modificationNotAllowed(prefix);
        }
    } else {
        throw new CommitFailedException(CommitFailedException.NAMESPACE, 4, "Not a valid namespace prefix: " + prefix);
    }
    modified = true;
}
Also used : CommitFailedException(org.apache.jackrabbit.oak.api.CommitFailedException)

Example 28 with CommitFailedException

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

the class UserPrincipalProvider method cacheGroups.

private void cacheGroups(@Nonnull Tree authorizableNode, @Nonnull Set<Group> groupPrincipals) {
    try {
        root.refresh();
        Tree cache = authorizableNode.getChild(CacheConstants.REP_CACHE);
        if (!cache.exists()) {
            if (groupPrincipals.size() <= MEMBERSHIP_THRESHOLD) {
                log.debug("Omit cache creation for user without group membership at " + authorizableNode.getPath());
                return;
            } else {
                log.debug("Create new group membership cache at " + authorizableNode.getPath());
                cache = TreeUtil.addChild(authorizableNode, CacheConstants.REP_CACHE, CacheConstants.NT_REP_CACHE);
            }
        }
        cache.setProperty(CacheConstants.REP_EXPIRATION, LongUtils.calculateExpirationTime(expiration));
        String value = (groupPrincipals.isEmpty()) ? "" : Joiner.on(",").join(Iterables.transform(groupPrincipals, new Function<Group, String>() {

            @Override
            public String apply(Group input) {
                return Text.escape(input.getName());
            }
        }));
        cache.setProperty(CacheConstants.REP_GROUP_PRINCIPAL_NAMES, value);
        root.commit(CacheValidatorProvider.asCommitAttributes());
        log.debug("Cached group membership at " + authorizableNode.getPath());
    } catch (AccessDeniedException e) {
        log.debug("Failed to cache group membership", e.getMessage());
    } catch (CommitFailedException e) {
        log.debug("Failed to cache group membership", e.getMessage(), e);
    } finally {
        root.refresh();
    }
}
Also used : Group(java.security.acl.Group) AccessDeniedException(javax.jcr.AccessDeniedException) Tree(org.apache.jackrabbit.oak.api.Tree) CommitFailedException(org.apache.jackrabbit.oak.api.CommitFailedException)

Example 29 with CommitFailedException

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

the class NodeStoreDiffTest method testDiff.

/**
     * This testcase demonstrates that diff logic in merge part traverses node path
     * which are not affected by the commit
     * @throws Exception
     */
@Test
public void testDiff() throws Exception {
    createNodes("/oak:index/prop-a", "/oak:index/prop-b", "/etc/workflow");
    //1. Make some other changes so as to bump root rev=3
    createNodes("/fake/a");
    createNodes("/fake/b");
    //2 - Start change
    NodeBuilder b2 = ns.getRoot().builder();
    createNodes(b2, "/etc/workflow/instance1");
    tds.reset();
    //3. Merge which does a rebase
    ns.merge(b2, new CommitHook() {

        @Nonnull
        public NodeState processCommit(NodeState before, NodeState after, CommitInfo info) throws CommitFailedException {
            NodeBuilder rb = after.builder();
            createNodes(rb, "/oak:index/prop-a/a1");
            //2.1 Commit some change under prop-
            //This cause diff in lastRev of base node state in ModifiedNodeState for
            //oak:index due to which when the base state is compared in ModifiedNodeState
            //then it fetches the new DocumentNodeState whose lastRev is greater than this.base.lastRev
            //but less then lastRev of the of readRevision. Where readRevision is the rev of root node when
            //rebase was performed
            // remember paths accessed so far
            List<String> paths = Lists.newArrayList(tds.paths);
            //This is not to be done in actual cases as CommitHooks are invoked in critical sections
            //and creating nodes from within CommitHooks would cause deadlock. This is done here to ensure
            //that changes are done when rebase has been performed and merge is about to happen
            createNodes("/oak:index/prop-b/b1");
            // reset accessed paths
            tds.reset();
            tds.paths.addAll(paths);
            return rb.getNodeState();
        }
    }, CommitInfo.EMPTY);
    //Assert that diff logic does not traverse to /oak:index/prop-b/b1 as
    //its not part of commit
    assertFalse(tds.paths.contains("/oak:index/prop-b/b1"));
}
Also used : NodeState(org.apache.jackrabbit.oak.spi.state.NodeState) Nonnull(javax.annotation.Nonnull) CommitHook(org.apache.jackrabbit.oak.spi.commit.CommitHook) List(java.util.List) CommitInfo(org.apache.jackrabbit.oak.spi.commit.CommitInfo) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) CommitFailedException(org.apache.jackrabbit.oak.api.CommitFailedException) Test(org.junit.Test)

Example 30 with CommitFailedException

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

the class AccessControlValidatorTest method testInvalidRestriction.

@Test
public void testInvalidRestriction() throws Exception {
    NodeUtil restriction = createAcl().getChild(aceName).getChild(REP_RESTRICTIONS);
    restriction.setString("invalid", "value");
    try {
        root.commit();
        fail("Creating an unsupported restriction should fail.");
    } catch (CommitFailedException e) {
        // success
        assertTrue(e.isAccessControlViolation());
        assertThat(e.getMessage(), containsString("/testRoot/rep:policy"));
    }
}
Also used : CommitFailedException(org.apache.jackrabbit.oak.api.CommitFailedException) NodeUtil(org.apache.jackrabbit.oak.util.NodeUtil) AbstractSecurityTest(org.apache.jackrabbit.oak.AbstractSecurityTest) Test(org.junit.Test)

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