Search in sources :

Example 26 with ItemExistsException

use of javax.jcr.ItemExistsException in project jackrabbit by apache.

the class MoveTest method testMovePropertyExists.

/**
     * Tries to move a node using <code>{@link javax.jcr.Session#move(String src, String dest)}
     * </code> to a location where a property already exists with same name.
     * <br/>
     * With JCR 1.0 this should throw an <code>{@link javax.jcr.ItemExistsException}</code>.
     * With JCR 2.0 the support for same-named property and node is optional and
     * the expected behaviour depends on the
     * {@link Repository#OPTION_NODE_AND_PROPERTY_WITH_SAME_NAME_SUPPORTED} descriptor.
     */
public void testMovePropertyExists() throws RepositoryException, NotExecutableException {
    // try to create a property with the name of the node to be moved
    // to the destination parent
    Property destProperty;
    try {
        destProperty = destParentNode.setProperty(nodeName2, "anyString");
    } catch (RepositoryException e) {
        throw new NotExecutableException("Cannot create property with name '" + nodeName2 + "' and value 'anyString' at move destination.");
    }
    // TODO: fix 2.0 behaviour according to the OPTION_NODE_AND_PROPERTY_WITH_SAME_NAME_SUPPORTED descriptor
    if ("1.0".equals(getHelper().getRepository().getDescriptor(Repository.SPEC_VERSION_DESC))) {
        try {
            // move the node
            doMove(moveNode.getPath(), destProperty.getPath());
            fail("Moving a node to a location where a property exists must throw ItemExistsException");
        } catch (ItemExistsException e) {
        // ok, works as expected
        }
    } else {
        // move the node: same name property and node must be supported
        // see Issue 725
        doMove(moveNode.getPath(), destProperty.getPath());
    }
}
Also used : NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) ItemExistsException(javax.jcr.ItemExistsException) RepositoryException(javax.jcr.RepositoryException) Property(javax.jcr.Property)

Example 27 with ItemExistsException

use of javax.jcr.ItemExistsException in project jackrabbit-oak by apache.

the class CompatibilityIssuesTest method noSNSSupport.

@Test
public void noSNSSupport() throws RepositoryException {
    Session session = getAdminSession();
    Node testNode = session.getRootNode().addNode("test", "nt:unstructured");
    session.save();
    testNode.addNode("foo");
    try {
        testNode.addNode("foo");
        // This would fail on JR2 since there SNSs are supported
        fail("Expected ItemExistsException");
    } catch (ItemExistsException e) {
    //ItemExistsException is expected to be thrown
    }
    session.save();
    try {
        testNode.addNode("foo");
        // This would fail on JR2 since there SNSs are supported
        fail("Expected ItemExistsException");
    } catch (ItemExistsException e) {
    //ItemExistsException is expected to be thrown
    }
}
Also used : ItemExistsException(javax.jcr.ItemExistsException) Node(javax.jcr.Node) Session(javax.jcr.Session) Test(org.junit.Test)

Example 28 with ItemExistsException

use of javax.jcr.ItemExistsException in project jackrabbit-oak by apache.

the class VersionManagerImpl method restore.

@Override
public void restore(final String absPath, final Version version, final boolean removeExisting) throws RepositoryException {
    final SessionDelegate sessionDelegate = sessionContext.getSessionDelegate();
    sessionDelegate.performVoid(new SessionOperation<Void>("restore", true) {

        @Override
        public void performVoid() throws RepositoryException {
            String oakPath = getOakPathOrThrowNotFound(absPath);
            NodeDelegate nodeDelegate = sessionDelegate.getNode(oakPath);
            if (nodeDelegate != null) {
                throw new VersionException("VersionManager.restore(String, Version, boolean)" + " not allowed on existing nodes; use" + " VersionManager.restore(Version, boolean) instead: " + absPath);
            }
            // check if parent exists
            NodeDelegate parent = ensureParentExists(sessionDelegate, absPath);
            // check for pending changes
            checkPendingChangesForRestore(sessionDelegate);
            // check lock status
            checkNotLocked(parent.getPath());
            // check for existing nodes
            List<NodeDelegate> existing = getExisting(version, Collections.<String>emptySet());
            boolean success = false;
            try {
                if (!existing.isEmpty()) {
                    if (removeExisting) {
                        removeExistingNodes(existing);
                    } else {
                        List<String> paths = new ArrayList<String>();
                        for (NodeDelegate nd : existing) {
                            paths.add(nd.getPath());
                        }
                        throw new ItemExistsException("Unable to restore with " + "removeExisting=false. Existing nodes in " + "workspace: " + paths);
                    }
                }
                // ready for restore
                VersionDelegate vd = versionManagerDelegate.getVersionByIdentifier(version.getIdentifier());
                versionManagerDelegate.restore(parent, PathUtils.getName(oakPath), vd);
                sessionDelegate.commit();
                success = true;
            } catch (CommitFailedException e) {
                throw e.asRepositoryException();
            } finally {
                if (!success) {
                    // refresh if one of the modifying operations fail
                    sessionDelegate.refresh(false);
                }
            }
        }
    });
}
Also used : VersionDelegate(org.apache.jackrabbit.oak.jcr.delegate.VersionDelegate) RepositoryException(javax.jcr.RepositoryException) NodeDelegate(org.apache.jackrabbit.oak.jcr.delegate.NodeDelegate) SessionDelegate(org.apache.jackrabbit.oak.jcr.delegate.SessionDelegate) ItemExistsException(javax.jcr.ItemExistsException) ArrayList(java.util.ArrayList) List(java.util.List) CommitFailedException(org.apache.jackrabbit.oak.api.CommitFailedException) VersionException(javax.jcr.version.VersionException)

Example 29 with ItemExistsException

use of javax.jcr.ItemExistsException in project jackrabbit-oak by apache.

the class VersionManagerImpl method restore.

@Override
public void restore(final Version[] versions, final boolean removeExisting) throws ItemExistsException, UnsupportedRepositoryOperationException, VersionException, LockException, InvalidItemStateException, RepositoryException {
    if (versions.length > 1) {
        throw new UnsupportedRepositoryOperationException("OAK-168: Restore of multiple versions not implemented.");
    }
    final Version version = versions[0];
    VersionHistory history = (VersionHistory) version.getParent();
    final String versionableId = history.getVersionableIdentifier();
    if (history.getRootVersion().isSame(version)) {
        throw new VersionException("Restore of root version not possible");
    }
    final SessionDelegate sessionDelegate = sessionContext.getSessionDelegate();
    sessionDelegate.performVoid(new SessionOperation<Void>("restore", true) {

        @Override
        public void performVoid() throws RepositoryException {
            // check for pending changes
            checkPendingChangesForRestore(sessionDelegate);
            NodeDelegate n = sessionDelegate.getNodeByIdentifier(versionableId);
            if (n == null) {
                throw new VersionException("Unable to restore version. " + "No versionable node with identifier: " + versionableId);
            }
            // check lock status
            checkNotLocked(n.getPath());
            // check for existing nodes
            List<NodeDelegate> existing = getExisting(version, Collections.singleton(n.getPath()));
            boolean success = false;
            try {
                if (!existing.isEmpty()) {
                    if (removeExisting) {
                        removeExistingNodes(existing);
                    } else {
                        List<String> paths = new ArrayList<String>();
                        for (NodeDelegate nd : existing) {
                            paths.add(nd.getPath());
                        }
                        throw new ItemExistsException("Unable to restore with " + "removeExisting=false. Existing nodes in " + "workspace: " + paths);
                    }
                }
                // ready for restore
                VersionDelegate vd = versionManagerDelegate.getVersionByIdentifier(version.getIdentifier());
                versionManagerDelegate.restore(n.getParent(), n.getName(), vd);
                sessionDelegate.commit();
                success = true;
            } catch (CommitFailedException e) {
                throw new RepositoryException(e);
            } finally {
                if (!success) {
                    // refresh if one of the modifying operations fail
                    sessionDelegate.refresh(false);
                }
            }
        }
    });
}
Also used : UnsupportedRepositoryOperationException(javax.jcr.UnsupportedRepositoryOperationException) VersionDelegate(org.apache.jackrabbit.oak.jcr.delegate.VersionDelegate) RepositoryException(javax.jcr.RepositoryException) VersionHistory(javax.jcr.version.VersionHistory) NodeDelegate(org.apache.jackrabbit.oak.jcr.delegate.NodeDelegate) SessionDelegate(org.apache.jackrabbit.oak.jcr.delegate.SessionDelegate) Version(javax.jcr.version.Version) ItemExistsException(javax.jcr.ItemExistsException) ArrayList(java.util.ArrayList) List(java.util.List) CommitFailedException(org.apache.jackrabbit.oak.api.CommitFailedException) VersionException(javax.jcr.version.VersionException)

Example 30 with ItemExistsException

use of javax.jcr.ItemExistsException in project jackrabbit by apache.

the class NodeImpl method clone.

/**
     * Create a child node that is a clone of a shareable node.
     *
     * @param src shareable source node
     * @param name name of new node
     * @return child node
     * @throws ItemExistsException if there already is a child node with the
     *             name given and the definition does not allow creating another one
     * @throws VersionException if this node is not checked out
     * @throws ConstraintViolationException if no definition is found in this
     *             node that would allow creating the child node
     * @throws LockException if this node is locked
     * @throws RepositoryException if some other error occurs
     */
public synchronized NodeImpl clone(NodeImpl src, Name name) throws ItemExistsException, VersionException, ConstraintViolationException, LockException, RepositoryException {
    Path nodePath;
    try {
        nodePath = PathFactoryImpl.getInstance().create(getPrimaryPath(), name, true);
    } catch (MalformedPathException e) {
        // should never happen
        String msg = "internal error: invalid path " + this;
        log.debug(msg);
        throw new RepositoryException(msg, e);
    }
    // (1) make sure that parent node is checked-out
    // (2) check lock status
    // (3) check protected flag of parent (i.e. this) node
    int options = ItemValidator.CHECK_LOCK | ItemValidator.CHECK_CHECKED_OUT | ItemValidator.CHECK_CONSTRAINTS;
    sessionContext.getItemValidator().checkModify(this, options, Permission.NONE);
    // (4) check for name collisions
    NodeDefinitionImpl def;
    try {
        def = getApplicableChildNodeDefinition(name, null);
    } catch (RepositoryException re) {
        String msg = "no definition found in parent node's node type for new node";
        log.debug(msg);
        throw new ConstraintViolationException(msg, re);
    }
    NodeState thisState = data.getNodeState();
    ChildNodeEntry cne = thisState.getChildNodeEntry(name, 1);
    if (cne != null) {
        // check same-name sibling setting of new node
        if (!def.allowsSameNameSiblings()) {
            throw new ItemExistsException(itemMgr.safeGetJCRPath(nodePath));
        }
        // check same-name sibling setting of existing node
        NodeId newId = cne.getId();
        if (!((NodeImpl) itemMgr.getItem(newId)).getDefinition().allowsSameNameSiblings()) {
            throw new ItemExistsException(itemMgr.safeGetJCRPath(nodePath));
        }
    }
    // (5) do clone operation
    NodeId parentId = getNodeId();
    src.addShareParent(parentId);
    // (6) modify the state of 'this', i.e. the parent node
    NodeId srcId = src.getNodeId();
    thisState = (NodeState) getOrCreateTransientItemState();
    // add new child node entry
    thisState.addChildNodeEntry(name, srcId);
    return itemMgr.getNode(srcId, parentId);
}
Also used : Path(org.apache.jackrabbit.spi.Path) NodeDefinitionImpl(org.apache.jackrabbit.spi.commons.nodetype.NodeDefinitionImpl) NodeState(org.apache.jackrabbit.core.state.NodeState) ItemExistsException(javax.jcr.ItemExistsException) ChildNodeEntry(org.apache.jackrabbit.core.state.ChildNodeEntry) MalformedPathException(org.apache.jackrabbit.spi.commons.conversion.MalformedPathException) NodeId(org.apache.jackrabbit.core.id.NodeId) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) RepositoryException(javax.jcr.RepositoryException)

Aggregations

ItemExistsException (javax.jcr.ItemExistsException)63 Node (javax.jcr.Node)25 RepositoryException (javax.jcr.RepositoryException)25 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)16 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)15 NodeDefinition (javax.jcr.nodetype.NodeDefinition)14 ItemNotFoundException (javax.jcr.ItemNotFoundException)13 Version (javax.jcr.version.Version)13 NodeId (org.apache.jackrabbit.core.id.NodeId)12 Name (org.apache.jackrabbit.spi.Name)12 NodeState (org.apache.jackrabbit.core.state.NodeState)10 ChildNodeEntry (org.apache.jackrabbit.core.state.ChildNodeEntry)9 Path (org.apache.jackrabbit.spi.Path)8 PathNotFoundException (javax.jcr.PathNotFoundException)6 EffectiveNodeType (org.apache.jackrabbit.core.nodetype.EffectiveNodeType)6 QPropertyDefinition (org.apache.jackrabbit.spi.QPropertyDefinition)6 NodeImpl (org.apache.jackrabbit.core.NodeImpl)5 QNodeDefinition (org.apache.jackrabbit.spi.QNodeDefinition)5 ArrayList (java.util.ArrayList)4 AccessDeniedException (javax.jcr.AccessDeniedException)4