Search in sources :

Example 81 with RepositoryException

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

the class VersionManagerImpl method mergeOrUpdate.

/**
     * Combines merge and update method
     * @param state the state to merge or update
     * @param srcWorkspaceName source workspace name
     * @param failedIds list that will contain the failed ids.
     *        if <code>null</code> and update will be performed.
     * @param bestEffort best effort flag
     * @param isShallow is shallow flag
     * @throws RepositoryException if an error occurs
     */
private void mergeOrUpdate(NodeStateEx state, String srcWorkspaceName, List<ItemId> failedIds, boolean bestEffort, boolean isShallow) throws RepositoryException {
    // if same workspace, ignore
    if (!srcWorkspaceName.equals(session.getWorkspace().getName())) {
        // check authorization for specified workspace
        if (!session.getAccessManager().canAccess(srcWorkspaceName)) {
            String msg = "not authorized to access " + srcWorkspaceName;
            log.error(msg);
            throw new AccessDeniedException(msg);
        }
        // get root node of src workspace
        SessionImpl srcSession = null;
        try {
            // create session on other workspace for current subject
            // (may throw NoSuchWorkspaceException and AccessDeniedException)
            srcSession = ((RepositoryImpl) session.getRepository()).createSession(session.getSubject(), srcWorkspaceName);
            WorkspaceImpl srcWsp = (WorkspaceImpl) srcSession.getWorkspace();
            NodeId rootNodeId = ((NodeImpl) srcSession.getRootNode()).getNodeId();
            NodeStateEx srcRoot = new NodeStateEx(srcWsp.getItemStateManager(), ntReg, rootNodeId);
            merge(state, srcRoot, failedIds, bestEffort, isShallow);
        } catch (ItemStateException e) {
            throw new RepositoryException(e);
        } finally {
            if (srcSession != null) {
                // we don't need the other session anymore, logout
                srcSession.logout();
            }
        }
    }
}
Also used : AccessDeniedException(javax.jcr.AccessDeniedException) NodeId(org.apache.jackrabbit.core.id.NodeId) RepositoryException(javax.jcr.RepositoryException) NodeStateEx(org.apache.jackrabbit.core.version.NodeStateEx) InvalidItemStateException(javax.jcr.InvalidItemStateException) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException)

Example 82 with RepositoryException

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

the class DescendantSelfAxisQuery method execute.

//------------------------< JackrabbitQuery >-------------------------------
/**
     * {@inheritDoc}
     */
public QueryHits execute(final JackrabbitIndexSearcher searcher, final SessionImpl session, final Sort sort) throws IOException {
    if (sort.getSort().length == 0 && subQueryMatchesAll()) {
        // maps path String to ScoreNode
        Map<String, ScoreNode> startingPoints = new TreeMap<String, ScoreNode>();
        QueryHits result = searcher.evaluate(getContextQuery());
        try {
            // intermediate ChildNodesQueryHits are required.
            for (int i = 2; i <= getMinLevels(); i++) {
                result = new ChildNodesQueryHits(result, session);
            }
            ScoreNode sn;
            while ((sn = result.nextScoreNode()) != null) {
                NodeId id = sn.getNodeId();
                try {
                    Node node = session.getNodeById(id);
                    startingPoints.put(node.getPath(), sn);
                } catch (ItemNotFoundException e) {
                    // JCR-3001 access denied to score node, will just skip it
                    log.warn("Access denied to node id {}.", id);
                } catch (RepositoryException e) {
                    throw Util.createIOException(e);
                }
            }
        } finally {
            result.close();
        }
        // prune overlapping starting points
        String previousPath = null;
        for (Iterator<String> it = startingPoints.keySet().iterator(); it.hasNext(); ) {
            String path = it.next();
            // current path is obsolete
            if (previousPath != null && path.startsWith(previousPath)) {
                it.remove();
            } else {
                previousPath = path;
            }
        }
        final Iterator<ScoreNode> scoreNodes = startingPoints.values().iterator();
        return new AbstractQueryHits() {

            private NodeTraversingQueryHits currentTraversal;

            {
                fetchNextTraversal();
            }

            public void close() throws IOException {
                if (currentTraversal != null) {
                    currentTraversal.close();
                }
            }

            public ScoreNode nextScoreNode() throws IOException {
                while (currentTraversal != null) {
                    ScoreNode sn = currentTraversal.nextScoreNode();
                    if (sn != null) {
                        return sn;
                    } else {
                        fetchNextTraversal();
                    }
                }
                // if we get here there are no more score nodes
                return null;
            }

            private void fetchNextTraversal() throws IOException {
                if (currentTraversal != null) {
                    currentTraversal.close();
                }
                currentTraversal = null;
                // iterate until we find a good one
                while (scoreNodes.hasNext()) {
                    ScoreNode sn = scoreNodes.next();
                    NodeId id = sn.getNodeId();
                    try {
                        Node node = session.getNodeById(id);
                        currentTraversal = new NodeTraversingQueryHits(node, getMinLevels() == 0);
                        break;
                    } catch (ItemNotFoundException e) {
                        // JCR-3001 node access denied, will just skip it
                        log.warn("Access denied to node id {}.", id);
                    } catch (RepositoryException e) {
                        throw Util.createIOException(e);
                    }
                }
            }
        };
    } else {
        return null;
    }
}
Also used : Node(javax.jcr.Node) RepositoryException(javax.jcr.RepositoryException) TreeMap(java.util.TreeMap) NodeId(org.apache.jackrabbit.core.id.NodeId) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 83 with RepositoryException

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

the class ChildNodesQueryHits method fetchNextChildHits.

/**
     * Fetches the next {@link #childHits}
     *
     * @throws IOException if an error occurs while reading from the index.
     */
private void fetchNextChildHits() throws IOException {
    if (childHits != null) {
        childHits.close();
    }
    ScoreNode nextParent = parents.nextScoreNode();
    if (nextParent != null) {
        try {
            Node parent = session.getNodeById(nextParent.getNodeId());
            childHits = new NodeTraversingQueryHits(parent, false, 1);
        } catch (ItemNotFoundException e) {
        // access denied to node, will just skip it
        } catch (RepositoryException e) {
            throw Util.createIOException(e);
        }
    } else {
        childHits = null;
    }
}
Also used : Node(javax.jcr.Node) RepositoryException(javax.jcr.RepositoryException) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 84 with RepositoryException

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

the class TargetImportHandler method startDocument.

/**
     * Initializes the underlying {@link Importer} instance. This method
     * is called by the XML parser when the XML document starts.
     *
     * @throws SAXException if the importer can not be initialized
     * @see DefaultHandler#startDocument()
     */
@Override
public void startDocument() throws SAXException {
    try {
        importer.start();
        nsContext = null;
    } catch (RepositoryException re) {
        throw new SAXException(re);
    }
}
Also used : RepositoryException(javax.jcr.RepositoryException) SAXException(org.xml.sax.SAXException)

Example 85 with RepositoryException

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

the class WorkspaceImporter method resolveUUIDConflict.

/**
     * @param parent parent node state
     * @param conflicting conflicting node state
     * @param nodeInfo the node info
     * @return the resolved node state
     * @throws RepositoryException if an error occurs
     */
protected NodeState resolveUUIDConflict(NodeState parent, NodeState conflicting, NodeInfo nodeInfo) throws RepositoryException {
    NodeState node;
    if (uuidBehavior == ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW) {
        // create new with new uuid:
        // check if new node can be added (check access rights &
        // node type constraints only, assume locking & versioning status
        // and retention/hold has already been checked on ancestor)
        itemOps.checkAddNode(parent, nodeInfo.getName(), nodeInfo.getNodeTypeName(), BatchedItemOperations.CHECK_ACCESS | BatchedItemOperations.CHECK_CONSTRAINTS);
        node = itemOps.createNodeState(parent, nodeInfo.getName(), nodeInfo.getNodeTypeName(), nodeInfo.getMixinNames(), null);
        // remember uuid mapping
        EffectiveNodeType ent = itemOps.getEffectiveNodeType(node);
        if (ent.includesNodeType(NameConstants.MIX_REFERENCEABLE)) {
            refTracker.mappedId(nodeInfo.getId(), node.getNodeId());
        }
    } else if (uuidBehavior == ImportUUIDBehavior.IMPORT_UUID_COLLISION_THROW) {
        // new node and share with existing
        if (conflicting.isShareable()) {
            itemOps.clone(conflicting, parent, nodeInfo.getName());
            return null;
        }
        String msg = "a node with uuid " + nodeInfo.getId() + " already exists!";
        log.debug(msg);
        throw new ItemExistsException(msg);
    } else if (uuidBehavior == ImportUUIDBehavior.IMPORT_UUID_COLLISION_REMOVE_EXISTING) {
        // make sure conflicting node is not importTarget or an ancestor thereof
        Path p0 = hierMgr.getPath(importTarget.getNodeId());
        Path p1 = hierMgr.getPath(conflicting.getNodeId());
        try {
            if (p1.equals(p0) || p1.isAncestorOf(p0)) {
                String msg = "cannot remove ancestor node";
                log.debug(msg);
                throw new ConstraintViolationException(msg);
            }
        } catch (MalformedPathException mpe) {
            // should never get here...
            String msg = "internal error: failed to determine degree of relationship";
            log.error(msg, mpe);
            throw new RepositoryException(msg, mpe);
        }
        // remove conflicting:
        // check if conflicting can be removed
        // (access rights, node type constraints, locking & versioning status)
        itemOps.checkRemoveNode(conflicting, BatchedItemOperations.CHECK_ACCESS | BatchedItemOperations.CHECK_LOCK | BatchedItemOperations.CHECK_CHECKED_OUT | BatchedItemOperations.CHECK_CONSTRAINTS | BatchedItemOperations.CHECK_HOLD | BatchedItemOperations.CHECK_RETENTION);
        // do remove conflicting (recursive)
        itemOps.removeNodeState(conflicting);
        // create new with given uuid:
        // check if new node can be added (check access rights &
        // node type constraints only, assume locking & versioning status
        // and retention/hold has already been checked on ancestor)
        itemOps.checkAddNode(parent, nodeInfo.getName(), nodeInfo.getNodeTypeName(), BatchedItemOperations.CHECK_ACCESS | BatchedItemOperations.CHECK_CONSTRAINTS);
        // do create new node
        node = itemOps.createNodeState(parent, nodeInfo.getName(), nodeInfo.getNodeTypeName(), nodeInfo.getMixinNames(), nodeInfo.getId());
    } else if (uuidBehavior == ImportUUIDBehavior.IMPORT_UUID_COLLISION_REPLACE_EXISTING) {
        NodeId parentId = conflicting.getParentId();
        if (parentId == null) {
            String msg = "root node cannot be replaced";
            log.debug(msg);
            throw new RepositoryException(msg);
        }
        // 'replace' current parent with parent of conflicting
        try {
            parent = itemOps.getNodeState(parentId);
        } catch (ItemNotFoundException infe) {
            // should never get here...
            String msg = "internal error: failed to retrieve parent state";
            log.error(msg, infe);
            throw new RepositoryException(msg, infe);
        }
        // remove conflicting:
        // check if conflicting can be removed
        // (access rights, node type constraints, locking & versioning status)
        itemOps.checkRemoveNode(conflicting, BatchedItemOperations.CHECK_ACCESS | BatchedItemOperations.CHECK_LOCK | BatchedItemOperations.CHECK_CHECKED_OUT | BatchedItemOperations.CHECK_CONSTRAINTS | BatchedItemOperations.CHECK_HOLD | BatchedItemOperations.CHECK_RETENTION);
        // 'replace' is actually a 'remove existing/add new' operation;
        // this unfortunately changes the order of the parent's
        // child node entries (JCR-1055);
        // => backup list of child node entries beforehand in order
        // to restore it afterwards
        ChildNodeEntry cneConflicting = parent.getChildNodeEntry(nodeInfo.getId());
        List<ChildNodeEntry> cneList = new ArrayList<ChildNodeEntry>(parent.getChildNodeEntries());
        // do remove conflicting (recursive)
        itemOps.removeNodeState(conflicting);
        // create new with given uuid at same location as conflicting:
        // check if new node can be added at other location
        // (access rights, node type constraints, locking & versioning
        // status and retention/hold)
        itemOps.checkAddNode(parent, nodeInfo.getName(), nodeInfo.getNodeTypeName(), BatchedItemOperations.CHECK_ACCESS | BatchedItemOperations.CHECK_LOCK | BatchedItemOperations.CHECK_CHECKED_OUT | BatchedItemOperations.CHECK_CONSTRAINTS | BatchedItemOperations.CHECK_HOLD | BatchedItemOperations.CHECK_RETENTION);
        // do create new node
        node = itemOps.createNodeState(parent, nodeInfo.getName(), nodeInfo.getNodeTypeName(), nodeInfo.getMixinNames(), nodeInfo.getId());
        // restore list of child node entries (JCR-1055)
        if (cneConflicting.getName().equals(nodeInfo.getName())) {
            // restore original child node list
            parent.setChildNodeEntries(cneList);
        } else {
            // replace child node entry with different name
            // but preserving original position
            parent.removeAllChildNodeEntries();
            for (ChildNodeEntry cne : cneList) {
                if (cne.getId().equals(nodeInfo.getId())) {
                    // replace entry with different name
                    parent.addChildNodeEntry(nodeInfo.getName(), nodeInfo.getId());
                } else {
                    parent.addChildNodeEntry(cne.getName(), cne.getId());
                }
            }
        }
    } else {
        String msg = "unknown uuidBehavior: " + uuidBehavior;
        log.debug(msg);
        throw new RepositoryException(msg);
    }
    return node;
}
Also used : Path(org.apache.jackrabbit.spi.Path) NodeState(org.apache.jackrabbit.core.state.NodeState) ChildNodeEntry(org.apache.jackrabbit.core.state.ChildNodeEntry) MalformedPathException(org.apache.jackrabbit.spi.commons.conversion.MalformedPathException) RepositoryException(javax.jcr.RepositoryException) EffectiveNodeType(org.apache.jackrabbit.core.nodetype.EffectiveNodeType) ItemExistsException(javax.jcr.ItemExistsException) NodeId(org.apache.jackrabbit.core.id.NodeId) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) ArrayList(java.util.ArrayList) List(java.util.List) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Aggregations

RepositoryException (javax.jcr.RepositoryException)1236 Node (javax.jcr.Node)289 Session (javax.jcr.Session)182 IOException (java.io.IOException)156 ArrayList (java.util.ArrayList)106 Name (org.apache.jackrabbit.spi.Name)94 DavException (org.apache.jackrabbit.webdav.DavException)90 Test (org.junit.Test)87 Value (javax.jcr.Value)80 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)76 ItemStateException (org.apache.jackrabbit.core.state.ItemStateException)72 Path (org.apache.jackrabbit.spi.Path)67 ItemNotFoundException (javax.jcr.ItemNotFoundException)65 PathNotFoundException (javax.jcr.PathNotFoundException)65 NodeId (org.apache.jackrabbit.core.id.NodeId)64 Property (javax.jcr.Property)61 HashMap (java.util.HashMap)53 Authorizable (org.apache.jackrabbit.api.security.user.Authorizable)53 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)52 InvalidItemStateException (javax.jcr.InvalidItemStateException)50