Search in sources :

Example 1 with MalformedPathException

use of org.apache.jackrabbit.spi.commons.conversion.MalformedPathException in project jackrabbit by apache.

the class EventImpl method getQPath.

// -----------------------------------------------------------< EventImpl >
/**
 * Returns the <code>Path</code> of this event.
 *
 * @return path or <code>null</code> when no path is associated with the event
 * @throws RepositoryException if the path can't be constructed
 */
public Path getQPath() throws RepositoryException {
    try {
        Path parent = eventState.getParentPath();
        Path child = eventState.getChildRelPath();
        if (parent == null || child == null) {
            // an event without associated path information
            return null;
        } else {
            int index = child.getIndex();
            if (index > 0) {
                return PathFactoryImpl.getInstance().create(parent, child.getName(), index, false);
            } else {
                return PathFactoryImpl.getInstance().create(parent, child.getName(), false);
            }
        }
    } catch (MalformedPathException e) {
        String msg = "internal error: malformed path for event";
        log.debug(msg);
        throw new RepositoryException(msg, e);
    }
}
Also used : Path(org.apache.jackrabbit.spi.Path) MalformedPathException(org.apache.jackrabbit.spi.commons.conversion.MalformedPathException) RepositoryException(javax.jcr.RepositoryException)

Example 2 with MalformedPathException

use of org.apache.jackrabbit.spi.commons.conversion.MalformedPathException in project jackrabbit by apache.

the class InternalValue method create.

/**
 * Create a new internal value from the given JCR value.
 * If the data store is enabled, large binary values are stored in the data store.
 *
 * @param value the JCR value
 * @param resolver
 * @param store the data store
 * @return the created internal value
 * @throws RepositoryException
 * @throws ValueFormatException
 */
public static InternalValue create(Value value, NamePathResolver resolver, DataStore store) throws ValueFormatException, RepositoryException {
    switch(value.getType()) {
        case PropertyType.BINARY:
            BLOBFileValue blob = null;
            if (value instanceof BinaryValueImpl) {
                BinaryValueImpl bin = (BinaryValueImpl) value;
                DataIdentifier identifier = bin.getDataIdentifier();
                if (identifier != null) {
                    if (bin.usesDataStore(store)) {
                        // access the record to ensure it is not garbage collected
                        store.getRecord(identifier);
                        blob = BLOBInDataStore.getInstance(store, identifier);
                    } else {
                        if (store.getRecordIfStored(identifier) != null) {
                            // it exists - so we don't need to stream it again
                            // but we need to create a new object because the original
                            // one might be in a different data store (repository)
                            blob = BLOBInDataStore.getInstance(store, identifier);
                        }
                    }
                }
            }
            if (blob == null) {
                Binary b = value.getBinary();
                boolean dispose = false;
                try {
                    if (b instanceof BLOBFileValue) {
                        // use as is
                        blob = (BLOBFileValue) b;
                    } else {
                        // create a copy from the stream
                        dispose = true;
                        blob = getBLOBFileValue(store, b.getStream(), true);
                    }
                } finally {
                    if (dispose) {
                        b.dispose();
                    }
                }
            }
            return new InternalValue(blob);
        case PropertyType.BOOLEAN:
            return create(value.getBoolean());
        case PropertyType.DATE:
            return create(value.getDate());
        case PropertyType.DOUBLE:
            return create(value.getDouble());
        case PropertyType.DECIMAL:
            return create(value.getDecimal());
        case PropertyType.LONG:
            return create(value.getLong());
        case PropertyType.REFERENCE:
            return create(new NodeId(value.getString()));
        case PropertyType.WEAKREFERENCE:
            return create(new NodeId(value.getString()), true);
        case PropertyType.URI:
            try {
                return create(new URI(value.getString()));
            } catch (URISyntaxException e) {
                throw new ValueFormatException(e.getMessage());
            }
        case PropertyType.NAME:
            try {
                if (value instanceof QValueValue) {
                    QValue qv = ((QValueValue) value).getQValue();
                    if (qv instanceof InternalValue) {
                        return (InternalValue) qv;
                    } else {
                        return create(qv.getName());
                    }
                } else {
                    return create(resolver.getQName(value.getString()));
                }
            } catch (NameException e) {
                throw new ValueFormatException(e.getMessage());
            }
        case PropertyType.PATH:
            try {
                if (value instanceof QValueValue) {
                    QValue qv = ((QValueValue) value).getQValue();
                    if (qv instanceof InternalValue) {
                        return (InternalValue) qv;
                    } else {
                        return create(qv.getPath());
                    }
                } else {
                    return create(resolver.getQPath(value.getString(), false));
                }
            } catch (MalformedPathException mpe) {
                throw new ValueFormatException(mpe.getMessage());
            }
        case PropertyType.STRING:
            return create(value.getString());
        default:
            throw new IllegalArgumentException("illegal value");
    }
}
Also used : QValueValue(org.apache.jackrabbit.spi.commons.value.QValueValue) AbstractQValue(org.apache.jackrabbit.spi.commons.value.AbstractQValue) QValue(org.apache.jackrabbit.spi.QValue) DataIdentifier(org.apache.jackrabbit.core.data.DataIdentifier) MalformedPathException(org.apache.jackrabbit.spi.commons.conversion.MalformedPathException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) NameException(org.apache.jackrabbit.spi.commons.conversion.NameException) NodeId(org.apache.jackrabbit.core.id.NodeId) ValueFormatException(javax.jcr.ValueFormatException) Binary(javax.jcr.Binary)

Example 3 with MalformedPathException

use of org.apache.jackrabbit.spi.commons.conversion.MalformedPathException in project jackrabbit by apache.

the class BatchedItemOperations method move.

/**
 * Moves the tree at <code>srcPath</code> to the new location at
 * <code>destPath</code>. Returns the id of the moved node.
 * <p>
 * <b>Precondition:</b> the state manager needs to be in edit mode.
 *
 * @param srcPath
 * @param destPath
 * @return the id of the moved node
 * @throws ConstraintViolationException
 * @throws VersionException
 * @throws AccessDeniedException
 * @throws PathNotFoundException
 * @throws ItemExistsException
 * @throws LockException
 * @throws RepositoryException
 * @throws IllegalStateException        if the state manager is not in edit mode
 */
public NodeId move(Path srcPath, Path destPath) throws ConstraintViolationException, VersionException, AccessDeniedException, PathNotFoundException, ItemExistsException, LockException, RepositoryException, IllegalStateException {
    // check precondition
    if (!stateMgr.inEditMode()) {
        throw new IllegalStateException("cannot move path " + safeGetJCRPath(srcPath) + " because manager is not in edit mode");
    }
    try {
        if (srcPath.isAncestorOf(destPath)) {
            String msg = safeGetJCRPath(destPath) + ": invalid destination path" + " (cannot be descendant of source path)";
            log.debug(msg);
            throw new RepositoryException(msg);
        }
    } catch (MalformedPathException mpe) {
        String msg = "invalid path for move: " + safeGetJCRPath(destPath);
        log.debug(msg);
        throw new RepositoryException(msg, mpe);
    }
    Path srcParentPath = srcPath.getAncestor(1);
    NodeState target = getNodeState(srcPath);
    NodeState srcParent = getNodeState(srcParentPath);
    Path destParentPath = destPath.getAncestor(1);
    NodeState destParent = getNodeState(destParentPath);
    int ind = destPath.getIndex();
    if (ind > 0) {
        // subscript in name element
        String msg = safeGetJCRPath(destPath) + ": invalid destination path" + " (subscript in name element is not allowed)";
        log.debug(msg);
        throw new RepositoryException(msg);
    }
    HierarchyManagerImpl hierMgr = (HierarchyManagerImpl) this.hierMgr;
    if (hierMgr.isShareAncestor(target.getNodeId(), destParent.getNodeId())) {
        String msg = safeGetJCRPath(destPath) + ": invalid destination path" + " (share cycle detected)";
        log.debug(msg);
        throw new RepositoryException(msg);
    }
    // 2. check if target state can be removed from old/added to new parent
    checkRemoveNode(target, srcParent.getNodeId(), CHECK_ACCESS | CHECK_LOCK | CHECK_CHECKED_OUT | CHECK_CONSTRAINTS | CHECK_HOLD | CHECK_RETENTION);
    checkAddNode(destParent, destPath.getName(), target.getNodeTypeName(), CHECK_ACCESS | CHECK_LOCK | CHECK_CHECKED_OUT | CHECK_CONSTRAINTS | CHECK_HOLD | CHECK_RETENTION);
    // 3. do move operation (modify and store affected states)
    boolean renameOnly = srcParent.getNodeId().equals(destParent.getNodeId());
    int srcNameIndex = srcPath.getIndex();
    if (srcNameIndex == 0) {
        srcNameIndex = 1;
    }
    stateMgr.store(target);
    if (renameOnly) {
        stateMgr.store(srcParent);
        // change child node entry
        destParent.renameChildNodeEntry(srcPath.getName(), srcNameIndex, destPath.getName());
    } else {
        // check shareable case
        if (target.isShareable()) {
            String msg = "Moving a shareable node (" + safeGetJCRPath(srcPath) + ") is not supported.";
            log.debug(msg);
            throw new UnsupportedRepositoryOperationException(msg);
        }
        stateMgr.store(srcParent);
        stateMgr.store(destParent);
        // 1. remove child node entry from old parent
        if (srcParent.removeChildNodeEntry(target.getNodeId())) {
            // 2. re-parent target node
            target.setParentId(destParent.getNodeId());
            // 3. add child node entry to new parent
            destParent.addChildNodeEntry(destPath.getName(), target.getNodeId());
        }
    }
    return target.getNodeId();
}
Also used : Path(org.apache.jackrabbit.spi.Path) UnsupportedRepositoryOperationException(javax.jcr.UnsupportedRepositoryOperationException) NodeState(org.apache.jackrabbit.core.state.NodeState) MalformedPathException(org.apache.jackrabbit.spi.commons.conversion.MalformedPathException) RepositoryException(javax.jcr.RepositoryException)

Example 4 with MalformedPathException

use of org.apache.jackrabbit.spi.commons.conversion.MalformedPathException 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 5 with MalformedPathException

use of org.apache.jackrabbit.spi.commons.conversion.MalformedPathException in project jackrabbit by apache.

the class TextsearchQueryNode method addPathElement.

/**
 * Adds a path element to the existing relative path. To add a path element
 * which matches all node names use {@link RelationQueryNode#STAR_NAME_TEST}.
 *
 * @param element the path element to append.
 */
public void addPathElement(Path.Element element) {
    PathBuilder builder = new PathBuilder();
    if (relPath != null) {
        builder.addAll(relPath.getElements());
    }
    builder.addLast(element);
    try {
        relPath = builder.getPath();
    } catch (MalformedPathException e) {
    // path is always valid
    }
}
Also used : PathBuilder(org.apache.jackrabbit.spi.commons.name.PathBuilder) MalformedPathException(org.apache.jackrabbit.spi.commons.conversion.MalformedPathException)

Aggregations

MalformedPathException (org.apache.jackrabbit.spi.commons.conversion.MalformedPathException)16 Path (org.apache.jackrabbit.spi.Path)10 RepositoryException (javax.jcr.RepositoryException)8 PathBuilder (org.apache.jackrabbit.spi.commons.name.PathBuilder)5 ItemNotFoundException (javax.jcr.ItemNotFoundException)3 NodeId (org.apache.jackrabbit.core.id.NodeId)3 NodeState (org.apache.jackrabbit.core.state.NodeState)3 ItemExistsException (javax.jcr.ItemExistsException)2 PathNotFoundException (javax.jcr.PathNotFoundException)2 ValueFormatException (javax.jcr.ValueFormatException)2 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)2 ChildNodeEntry (org.apache.jackrabbit.core.state.ChildNodeEntry)2 ItemStateException (org.apache.jackrabbit.core.state.ItemStateException)2 Name (org.apache.jackrabbit.spi.Name)2 QValue (org.apache.jackrabbit.spi.QValue)2 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1