Search in sources :

Example 6 with Path

use of org.apache.jackrabbit.spi.Path in project jackrabbit by apache.

the class ChangeLogRecord method readEventRecord.

/**
     * Read an event record.
     *
     * @throws JournalException if an error occurs
     */
private void readEventRecord() throws JournalException {
    int type = record.readByte();
    NodeId parentId = record.readNodeId();
    Path parentPath = record.readPath();
    NodeId childId = record.readNodeId();
    Path childRelPath = record.readPathElement();
    Name ntName = record.readQName();
    Set<Name> mixins = new HashSet<Name>();
    int mixinCount = record.readInt();
    for (int i = 0; i < mixinCount; i++) {
        mixins.add(record.readQName());
    }
    String userId = record.readString();
    Map<String, InternalValue> info = null;
    if (type == Event.NODE_MOVED) {
        info = new HashMap<String, InternalValue>();
        // read info map
        int infoSize = record.readInt();
        for (int i = 0; i < infoSize; i++) {
            String key = record.readString();
            int propType = record.readInt();
            InternalValue value;
            if (propType == PropertyType.UNDEFINED) {
                // indicates null value
                value = null;
            } else {
                value = InternalValue.valueOf(record.readString(), propType);
            }
            info.put(key, value);
        }
    }
    EventState es = createEventState(type, parentId, parentPath, childId, childRelPath, ntName, mixins, userId);
    if (info != null) {
        es.setInfo(info);
    }
    events.add(es);
}
Also used : Path(org.apache.jackrabbit.spi.Path) EventState(org.apache.jackrabbit.core.observation.EventState) NodeId(org.apache.jackrabbit.core.id.NodeId) InternalValue(org.apache.jackrabbit.core.value.InternalValue) Name(org.apache.jackrabbit.spi.Name) HashSet(java.util.HashSet)

Example 7 with Path

use of org.apache.jackrabbit.spi.Path 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)

Example 8 with Path

use of org.apache.jackrabbit.spi.Path in project jackrabbit by apache.

the class AccessManagerTest method testIsGrantedReadOnlySession.

public void testIsGrantedReadOnlySession() throws NotExecutableException, RepositoryException {
    Session s = getHelper().getReadOnlySession();
    try {
        AccessManager acMgr = getAccessManager(s);
        Path p = PathFactoryImpl.getInstance().getRootPath();
        // existing node-path
        assertTrue(acMgr.isGranted(p, Permission.READ));
        // not existing property:
        assertTrue(acMgr.isGranted(p, NameConstants.JCR_CREATED, Permission.READ));
        // existing node-path
        assertFalse(acMgr.isGranted(p, Permission.ALL));
        // not existing property:
        assertFalse(acMgr.isGranted(p, NameConstants.JCR_CREATED, Permission.ALL));
    } finally {
        s.logout();
    }
}
Also used : Path(org.apache.jackrabbit.spi.Path) Session(javax.jcr.Session)

Example 9 with Path

use of org.apache.jackrabbit.spi.Path in project jackrabbit by apache.

the class NodeEntryImpl method getDeepPropertyEntry.

/**
     * @see NodeEntry#getDeepPropertyEntry(Path)
     */
public PropertyEntry getDeepPropertyEntry(Path path) throws PathNotFoundException, RepositoryException {
    NodeEntryImpl entry = this;
    Path.Element[] elems = path.getElements();
    int i = 0;
    for (; i < elems.length - 1; i++) {
        Path.Element elem = elems[i];
        if (elems[i].denotesRoot()) {
            if (entry.getParent() != null) {
                throw new RepositoryException("NodeEntry out of 'hierarchy' " + path.toString());
            }
            continue;
        }
        int index = elem.getNormalizedIndex();
        Name name = elem.getName();
        // first try to resolve to known node or property entry
        NodeEntry cne = entry.getNodeEntry(name, index, false);
        if (cne != null) {
            entry = (NodeEntryImpl) cne;
        } else {
            //    on the persistent layer.
            if (entry.childNodeEntries.isComplete()) {
                throw new PathNotFoundException(factory.saveGetJCRPath(path));
            }
            // -> check for moved child entry in node-attic
            // -> check if child points to a removed/moved sns
            List<NodeEntry> siblings = entry.childNodeEntries.get(name);
            if (entry.containsAtticChild(siblings, name, index)) {
                throw new PathNotFoundException(factory.saveGetJCRPath(path));
            }
            // break out of the loop and start deep loading the property
            break;
        }
    }
    int st = entry.getStatus();
    PropertyEntry pe;
    if (i == elems.length - 1 && Status.INVALIDATED != st && Status._UNDEFINED_ != st) {
        // all node entries present in the hierarchy and the direct ancestor
        // has already been resolved and isn't invalidated -> no need to
        // retrieve property entry from SPI
        pe = entry.properties.get(path.getName());
    } else {
        /*
            * Unknown parent entry (not-existing or not yet loaded) or a parent
            * entry that has been invalidated:
            * Skip all intermediate entries and directly try to load the
            * PropertyState (including building the intermediate entries. If that
            * fails ItemNotFoundException is thrown.
            */
        PathBuilder pb = new PathBuilder(getPathFactory());
        for (int j = i; j < elems.length; j++) {
            pb.addLast(elems[j]);
        }
        Path remainingPath = pb.getPath();
        IdFactory idFactory = getIdFactory();
        NodeId parentId = entry.getWorkspaceId();
        if (remainingPath.getLength() != 1) {
            parentId = idFactory.createNodeId(parentId, remainingPath.getAncestor(1));
        }
        PropertyId propId = idFactory.createPropertyId(parentId, remainingPath.getName());
        pe = entry.loadPropertyEntry(propId);
    }
    if (pe == null) {
        throw new PathNotFoundException(factory.saveGetJCRPath(path));
    }
    return pe;
}
Also used : Path(org.apache.jackrabbit.spi.Path) RepositoryException(javax.jcr.RepositoryException) Name(org.apache.jackrabbit.spi.Name) PropertyId(org.apache.jackrabbit.spi.PropertyId) PathBuilder(org.apache.jackrabbit.spi.commons.name.PathBuilder) IdFactory(org.apache.jackrabbit.spi.IdFactory) NodeId(org.apache.jackrabbit.spi.NodeId) PathNotFoundException(javax.jcr.PathNotFoundException)

Example 10 with Path

use of org.apache.jackrabbit.spi.Path in project jackrabbit by apache.

the class HierarchyEventListener method pushEvents.

/**
     * Retrieve the workspace state(s) affected by the given event and refresh
     * them accordingly.
     *
     * @param events the events to process.
     */
private void pushEvents(Collection<Event> events) {
    if (events.isEmpty()) {
        log.debug("Empty event bundle");
        return;
    }
    // TODO: handle new 283 event types and clean add/remove that is also present as move-event.
    // collect set of removed node ids
    Set<ItemId> removedEvents = new HashSet<ItemId>();
    // separately collect the add events
    Set<Event> addEvents = new HashSet<Event>();
    for (Iterator<Event> it = events.iterator(); it.hasNext(); ) {
        Event event = it.next();
        int type = event.getType();
        if (type == Event.NODE_REMOVED) {
            // remember removed nodes separately for proper handling later on.
            removedEvents.add(event.getItemId());
        } else if (type == Event.NODE_ADDED || type == Event.PROPERTY_ADDED) {
            addEvents.add(event);
            it.remove();
        }
    }
    /* Process ADD-events.
           In case of persisting transients modifications, the event-set may
           still contain events that are not covered by the changeLog such as
           new version-history or other autocreated properties and nodes.

           Add events need to be processed hierarchically, since its not possible
           to add a new child reference to a state that is not yet present in
           the state manager.
           The 'progress' flag is used to make sure, that during each loop at
           least one event has been processed and removed from the iterator.
           If this is not the case, there are not parent states present in the
           state manager that need to be updated and the remaining events may
           be ignored.
         */
    boolean progress = true;
    while (!addEvents.isEmpty() && progress) {
        progress = false;
        for (Iterator<Event> it = addEvents.iterator(); it.hasNext(); ) {
            Event ev = it.next();
            NodeId parentId = ev.getParentId();
            HierarchyEntry parent = null;
            if (parentId != null) {
                parent = hierarchyMgr.lookup(parentId);
                if (parent == null && ev.getPath() != null && parentId.getUniqueID() != null) {
                    // the parent by path.
                    try {
                        Path parentPath = ev.getPath().getAncestor(1);
                        parent = hierarchyMgr.lookup(parentPath);
                    } catch (RepositoryException e) {
                        // should not occur
                        log.debug(e.getMessage());
                    }
                }
            }
            if (parent != null && parent.denotesNode()) {
                ((NodeEntry) parent).refresh(ev);
                it.remove();
                progress = true;
            }
        }
    }
    /* process all other events (removal, property changed) */
    for (Event event : events) {
        int type = event.getType();
        NodeId parentId = event.getParentId();
        NodeEntry parent = (parentId != null) ? (NodeEntry) hierarchyMgr.lookup(parentId) : null;
        switch(type) {
            case Event.NODE_REMOVED:
            case Event.PROPERTY_REMOVED:
                //   only remove the parent an skip this event.
                if (parent != null && !removedEvents.contains(parentId)) {
                    parent.refresh(event);
                }
                break;
            case Event.PROPERTY_CHANGED:
                // not exist either -> no need to inform propEntry
                if (parent != null) {
                    parent.refresh(event);
                }
                break;
            case Event.NODE_MOVED:
                // TODO: implementation missing
                throw new UnsupportedOperationException("Implementation missing");
            //break;
            case Event.PERSIST:
                // TODO: implementation missing
                throw new UnsupportedOperationException("Implementation missing");
            //break;
            default:
                // should never occur
                throw new IllegalArgumentException("Invalid event type: " + event.getType());
        }
    }
}
Also used : Path(org.apache.jackrabbit.spi.Path) RepositoryException(javax.jcr.RepositoryException) ItemId(org.apache.jackrabbit.spi.ItemId) NodeId(org.apache.jackrabbit.spi.NodeId) Event(org.apache.jackrabbit.spi.Event) HashSet(java.util.HashSet)

Aggregations

Path (org.apache.jackrabbit.spi.Path)222 RepositoryException (javax.jcr.RepositoryException)72 Name (org.apache.jackrabbit.spi.Name)37 ItemNotFoundException (javax.jcr.ItemNotFoundException)25 NodeState (org.apache.jackrabbit.core.state.NodeState)24 PathNotFoundException (javax.jcr.PathNotFoundException)22 NodeId (org.apache.jackrabbit.core.id.NodeId)22 NameException (org.apache.jackrabbit.spi.commons.conversion.NameException)20 NamespaceException (javax.jcr.NamespaceException)16 ArrayList (java.util.ArrayList)13 AccessDeniedException (javax.jcr.AccessDeniedException)13 Node (javax.jcr.Node)12 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)12 ChildNodeEntry (org.apache.jackrabbit.core.state.ChildNodeEntry)11 ItemStateException (org.apache.jackrabbit.core.state.ItemStateException)11 NodeId (org.apache.jackrabbit.spi.NodeId)11 QValue (org.apache.jackrabbit.spi.QValue)10 MalformedPathException (org.apache.jackrabbit.spi.commons.conversion.MalformedPathException)10 IOException (java.io.IOException)9 ItemExistsException (javax.jcr.ItemExistsException)8