Search in sources :

Example 26 with InternalValue

use of org.apache.jackrabbit.core.value.InternalValue in project jackrabbit by apache.

the class EventState method nodeReordered.

/**
     * Creates a new {@link javax.jcr.observation.Event} of type
     * <code>NODE_MOVED</code>. The parent node associated with this event type
     * is the parent node of the destination of the reorder!
     *
     * @param parentId      the id of the parent node associated with this
     *                      <code>EventState</code>.
     * @param parentPath    the path of the parent node associated with
     *                      this <code>EventState</code>.
     * @param childId       the id of the child node associated with this
     *                      event.
     * @param destChildPath the name element of the node before it was reordered.
     * @param srcChildPath  the name element of the reordered node before the
     *                      reorder operation.
     * @param beforeChildPath the name element of the node before which the
     *                      reordered node is placed. (may be <code>null</code>
     *                      if reordered to the end.
     * @param nodeType      the node type of the parent node.
     * @param mixins        mixins assigned to the parent node.
     * @param session       the session that removed the node.
     * @param external      flag indicating whether this is an external event
     * @return an <code>EventState</code> instance.
     */
public static EventState nodeReordered(NodeId parentId, Path parentPath, NodeId childId, Path destChildPath, Path srcChildPath, Path beforeChildPath, Name nodeType, Set<Name> mixins, Session session, boolean external) {
    EventState es = nodeMoved(parentId, parentPath, childId, destChildPath, nodeType, mixins, session, external);
    Map<String, InternalValue> info = new HashMap<String, InternalValue>();
    info.put(SRC_CHILD_REL_PATH, createValue(srcChildPath));
    InternalValue value = null;
    if (beforeChildPath != null) {
        value = createValue(beforeChildPath);
    }
    info.put(DEST_CHILD_REL_PATH, value);
    es.setInfo(info);
    return es;
}
Also used : HashMap(java.util.HashMap) InternalValue(org.apache.jackrabbit.core.value.InternalValue)

Example 27 with InternalValue

use of org.apache.jackrabbit.core.value.InternalValue in project jackrabbit by apache.

the class VirtualNodeTypeStateProvider method createPropertyDefState.

/**
     * creates a node state for the given property def
     *
     * @param parent
     * @param propDef
     * @return
     * @throws RepositoryException
     */
private VirtualNodeState createPropertyDefState(VirtualNodeState parent, QPropertyDefinition propDef, QNodeTypeDefinition ntDef, int n) throws RepositoryException {
    NodeId id = calculateStableId(ntDef.getName().toString() + "/" + NameConstants.JCR_PROPERTYDEFINITION.toString() + "/" + n);
    VirtualNodeState pState = createNodeState(parent, NameConstants.JCR_PROPERTYDEFINITION, id, NameConstants.NT_PROPERTYDEFINITION);
    // add properties
    if (!propDef.definesResidual()) {
        pState.setPropertyValue(NameConstants.JCR_NAME, InternalValue.create(propDef.getName()));
    }
    pState.setPropertyValue(NameConstants.JCR_AUTOCREATED, InternalValue.create(propDef.isAutoCreated()));
    pState.setPropertyValue(NameConstants.JCR_MANDATORY, InternalValue.create(propDef.isMandatory()));
    pState.setPropertyValue(NameConstants.JCR_ONPARENTVERSION, InternalValue.create(OnParentVersionAction.nameFromValue(propDef.getOnParentVersion())));
    pState.setPropertyValue(NameConstants.JCR_PROTECTED, InternalValue.create(propDef.isProtected()));
    pState.setPropertyValue(NameConstants.JCR_MULTIPLE, InternalValue.create(propDef.isMultiple()));
    pState.setPropertyValue(NameConstants.JCR_REQUIREDTYPE, InternalValue.create(PropertyType.nameFromValue(propDef.getRequiredType()).toUpperCase()));
    InternalValue[] defVals = InternalValue.create(propDef.getDefaultValues());
    // retrieve the property type from the first default value present with
    // the property definition. in case no default values are defined,
    // fallback to PropertyType.STRING in order to avoid creating a property
    // with type UNDEFINED which is illegal.
    int defValsType = PropertyType.STRING;
    if (defVals != null && defVals.length > 0) {
        defValsType = defVals[0].getType();
    }
    if (defVals != null) {
        pState.setPropertyValues(NameConstants.JCR_DEFAULTVALUES, defValsType, defVals);
    }
    QValueConstraint[] vc = propDef.getValueConstraints();
    InternalValue[] vals = new InternalValue[vc.length];
    for (int i = 0; i < vc.length; i++) {
        vals[i] = InternalValue.create(vc[i].getString());
    }
    pState.setPropertyValues(NameConstants.JCR_VALUECONSTRAINTS, PropertyType.STRING, vals);
    return pState;
}
Also used : VirtualNodeState(org.apache.jackrabbit.core.virtual.VirtualNodeState) NodeId(org.apache.jackrabbit.core.id.NodeId) InternalValue(org.apache.jackrabbit.core.value.InternalValue) QValueConstraint(org.apache.jackrabbit.spi.QValueConstraint) QValueConstraint(org.apache.jackrabbit.spi.QValueConstraint)

Example 28 with InternalValue

use of org.apache.jackrabbit.core.value.InternalValue in project jackrabbit by apache.

the class LazyTextExtractorFieldTest method testEmptyParser.

/**
     * @see <a
     *      href="https://issues.apache.org/jira/browse/JCR-3296">JCR-3296</a>
     *      Indexing ignored file types creates some garbage
     */
public void testEmptyParser() throws Exception {
    InternalValue val = InternalValue.create(new RandomInputStream(1, 1024));
    Metadata metadata = new Metadata();
    metadata.set(Metadata.CONTENT_TYPE, "application/java-archive");
    metadata.set(Metadata.CONTENT_ENCODING, "UTF-8");
    Parser p = getSearchIndex().getParser();
    ParsingTask task = new ParsingTask(p, val, metadata, Integer.MAX_VALUE) {

        public void setExtractedText(String value) {
            assertEquals("", value);
        }
    };
    task.run();
}
Also used : ParsingTask(org.apache.jackrabbit.core.query.lucene.LazyTextExtractorField.ParsingTask) Metadata(org.apache.tika.metadata.Metadata) InternalValue(org.apache.jackrabbit.core.value.InternalValue) RandomInputStream(org.apache.jackrabbit.core.data.RandomInputStream) Parser(org.apache.tika.parser.Parser)

Example 29 with InternalValue

use of org.apache.jackrabbit.core.value.InternalValue in project jackrabbit by apache.

the class BatchedItemOperations method copy.

/**
     * Copies the tree at <code>srcPath</code> retrieved using the specified
     * <code>srcStateMgr</code> to the new location at <code>destPath</code>.
     * Returns the id of the node at its new position.
     * <p>
     * <b>Precondition:</b> the state manager needs to be in edit mode.
     *
     * @param srcPath
     * @param srcStateMgr
     * @param srcHierMgr
     * @param srcAccessMgr
     * @param destPath
     * @param flag         one of
     *                     <ul>
     *                     <li><code>COPY</code></li>
     *                     <li><code>CLONE</code></li>
     *                     <li><code>CLONE_REMOVE_EXISTING</code></li>
     *                     </ul>
     * @return the id of the node at its new position
     * @throws ConstraintViolationException
     * @throws AccessDeniedException
     * @throws VersionException
     * @throws PathNotFoundException
     * @throws ItemExistsException
     * @throws LockException
     * @throws RepositoryException
     * @throws IllegalStateException if the state manager is not in edit mode.
     */
public NodeId copy(Path srcPath, ItemStateManager srcStateMgr, HierarchyManager srcHierMgr, AccessManager srcAccessMgr, Path destPath, int flag) throws ConstraintViolationException, AccessDeniedException, VersionException, PathNotFoundException, ItemExistsException, LockException, RepositoryException, IllegalStateException {
    // check precondition
    checkInEditMode();
    // 1. check paths & retrieve state
    NodeState srcState = getNodeState(srcStateMgr, srcHierMgr, srcPath);
    Path destParentPath = destPath.getAncestor(1);
    NodeState destParentState = getNodeState(destParentPath);
    int ind = destPath.getIndex();
    if (ind > 0) {
        // subscript in name element
        String msg = "invalid copy destination path: " + safeGetJCRPath(destPath) + " (subscript in name element is not allowed)";
        log.debug(msg);
        throw new RepositoryException(msg);
    }
    // 2. check access rights, lock status, node type constraints, etc.
    // JCR-2269: store target node state in changelog early as a
    // precautionary measure in order to isolate it from concurrent
    // underlying changes while checking preconditions
    stateMgr.store(destParentState);
    checkAddNode(destParentState, destPath.getName(), srcState.getNodeTypeName(), CHECK_ACCESS | CHECK_LOCK | CHECK_CHECKED_OUT | CHECK_CONSTRAINTS | CHECK_HOLD | CHECK_RETENTION);
    // check read access right on source node using source access manager
    try {
        if (!srcAccessMgr.isGranted(srcPath, Permission.READ)) {
            throw new PathNotFoundException(safeGetJCRPath(srcPath));
        }
    } catch (ItemNotFoundException infe) {
        String msg = "internal error: failed to check access rights for " + safeGetJCRPath(srcPath);
        log.debug(msg);
        throw new RepositoryException(msg, infe);
    }
    // 3. do copy operation (modify and store affected states)
    ReferenceChangeTracker refTracker = new ReferenceChangeTracker();
    // create deep copy of source node state
    NodeState newState = copyNodeState(srcState, srcPath, srcStateMgr, srcAccessMgr, destParentState.getNodeId(), flag, refTracker);
    // add to new parent
    destParentState.addChildNodeEntry(destPath.getName(), newState.getNodeId());
    // adjust references that refer to uuid's which have been mapped to
    // newly generated uuid's on copy/clone
    Iterator<Object> iter = refTracker.getProcessedReferences();
    while (iter.hasNext()) {
        PropertyState prop = (PropertyState) iter.next();
        // being paranoid...
        if (prop.getType() != PropertyType.REFERENCE && prop.getType() != PropertyType.WEAKREFERENCE) {
            continue;
        }
        boolean modified = false;
        InternalValue[] values = prop.getValues();
        InternalValue[] newVals = new InternalValue[values.length];
        for (int i = 0; i < values.length; i++) {
            NodeId adjusted = refTracker.getMappedId(values[i].getNodeId());
            if (adjusted != null) {
                boolean weak = prop.getType() == PropertyType.WEAKREFERENCE;
                newVals[i] = InternalValue.create(adjusted, weak);
                modified = true;
            } else {
                // reference doesn't need adjusting, just copy old value
                newVals[i] = values[i];
            }
        }
        if (modified) {
            prop.setValues(newVals);
            stateMgr.store(prop);
        }
    }
    refTracker.clear();
    // store states
    stateMgr.store(newState);
    stateMgr.store(destParentState);
    return newState.getNodeId();
}
Also used : Path(org.apache.jackrabbit.spi.Path) NodeState(org.apache.jackrabbit.core.state.NodeState) RepositoryException(javax.jcr.RepositoryException) InternalValue(org.apache.jackrabbit.core.value.InternalValue) PropertyState(org.apache.jackrabbit.core.state.PropertyState) ReferenceChangeTracker(org.apache.jackrabbit.core.util.ReferenceChangeTracker) NodeId(org.apache.jackrabbit.core.id.NodeId) PathNotFoundException(javax.jcr.PathNotFoundException) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 30 with InternalValue

use of org.apache.jackrabbit.core.value.InternalValue in project jackrabbit by apache.

the class BatchedItemOperations method copyNodeState.

/**
     * Recursively copies the specified node state including its properties and
     * child nodes.
     *
     * @param srcState
     * @param srcPath
     * @param srcStateMgr
     * @param srcAccessMgr
     * @param destParentId
     * @param flag           one of
     *                       <ul>
     *                       <li><code>COPY</code></li>
     *                       <li><code>CLONE</code></li>
     *                       <li><code>CLONE_REMOVE_EXISTING</code></li>
     *                       </ul>
     * @param refTracker     tracks uuid mappings and processed reference properties
     * @return a deep copy of the given node state and its children
     * @throws RepositoryException if an error occurs
     */
private NodeState copyNodeState(NodeState srcState, Path srcPath, ItemStateManager srcStateMgr, AccessManager srcAccessMgr, NodeId destParentId, int flag, ReferenceChangeTracker refTracker) throws RepositoryException {
    NodeState newState;
    try {
        NodeId id = null;
        EffectiveNodeType ent = getEffectiveNodeType(srcState);
        boolean referenceable = ent.includesNodeType(NameConstants.MIX_REFERENCEABLE);
        boolean versionable = ent.includesNodeType(NameConstants.MIX_SIMPLE_VERSIONABLE);
        boolean fullVersionable = ent.includesNodeType(NameConstants.MIX_VERSIONABLE);
        boolean shareable = ent.includesNodeType(NameConstants.MIX_SHAREABLE);
        switch(flag) {
            case COPY:
                /* if this node is shareable and another node in the same shared set
                     * has been already been copied and given a new uuid, use this one
                     * (see section 14.5 of the specification)
                     */
                if (shareable && refTracker.getMappedId(srcState.getNodeId()) != null) {
                    NodeId newId = refTracker.getMappedId(srcState.getNodeId());
                    NodeState sharedState = (NodeState) stateMgr.getItemState(newId);
                    sharedState.addShare(destParentId);
                    return sharedState;
                }
                break;
            case CLONE:
                if (!referenceable) {
                    // non-referenceable node: always create new node id
                    break;
                }
                // use same uuid as source node
                id = srcState.getNodeId();
                if (stateMgr.hasItemState(id)) {
                    if (shareable) {
                        NodeState sharedState = (NodeState) stateMgr.getItemState(id);
                        sharedState.addShare(destParentId);
                        return sharedState;
                    }
                    // node with this uuid already exists
                    throw new ItemExistsException(safeGetJCRPath(id));
                }
                break;
            case CLONE_REMOVE_EXISTING:
                if (!referenceable) {
                    // non-referenceable node: always create new node id
                    break;
                }
                // use same uuid as source node
                id = srcState.getNodeId();
                if (stateMgr.hasItemState(id)) {
                    NodeState existingState = (NodeState) stateMgr.getItemState(id);
                    // or an ancestor thereof
                    if (id.equals(destParentId) || hierMgr.isAncestor(id, destParentId)) {
                        String msg = "cannot remove node " + safeGetJCRPath(srcPath) + " because it is an ancestor of the destination";
                        log.debug(msg);
                        throw new RepositoryException(msg);
                    }
                    // check if existing can be removed
                    // (access rights, locking & versioning status,
                    // node type constraints and retention/hold)
                    checkRemoveNode(existingState, CHECK_ACCESS | CHECK_LOCK | CHECK_CHECKED_OUT | CHECK_CONSTRAINTS | CHECK_HOLD | CHECK_RETENTION);
                    // do remove existing
                    removeNodeState(existingState);
                }
                break;
            default:
                throw new IllegalArgumentException("unknown flag for copying node state: " + flag);
        }
        newState = stateMgr.createNew(id, srcState.getNodeTypeName(), destParentId);
        id = newState.getNodeId();
        if (flag == COPY && referenceable) {
            // remember uuid mapping
            refTracker.mappedId(srcState.getNodeId(), id);
        }
        // copy node state
        newState.setMixinTypeNames(srcState.getMixinTypeNames());
        if (shareable) {
            // initialize shared set
            newState.addShare(destParentId);
        }
        // copy child nodes
        for (ChildNodeEntry entry : srcState.getChildNodeEntries()) {
            Path srcChildPath = PathFactoryImpl.getInstance().create(srcPath, entry.getName(), true);
            if (!srcAccessMgr.isGranted(srcChildPath, Permission.READ)) {
                continue;
            }
            NodeId nodeId = entry.getId();
            NodeState srcChildState = (NodeState) srcStateMgr.getItemState(nodeId);
            /**
                 * If child is shareble and its UUID has already been remapped,
                 * then simply add a reference to the state with that remapped
                 * UUID instead of copying the whole subtree.
                 */
            if (srcChildState.isShareable()) {
                NodeId mappedId = refTracker.getMappedId(srcChildState.getNodeId());
                if (mappedId != null) {
                    if (stateMgr.hasItemState(mappedId)) {
                        NodeState destState = (NodeState) stateMgr.getItemState(mappedId);
                        if (!destState.isShareable()) {
                            String msg = "Remapped child (" + safeGetJCRPath(srcPath) + ") is not shareable.";
                            throw new ItemStateException(msg);
                        }
                        if (!destState.addShare(id)) {
                            String msg = "Unable to add share to node: " + id;
                            throw new ItemStateException(msg);
                        }
                        stateMgr.store(destState);
                        newState.addChildNodeEntry(entry.getName(), mappedId);
                        continue;
                    }
                }
            }
            // recursive copying of child node
            NodeState newChildState = copyNodeState(srcChildState, srcChildPath, srcStateMgr, srcAccessMgr, id, flag, refTracker);
            // store new child node
            stateMgr.store(newChildState);
            // add new child node entry to new node
            newState.addChildNodeEntry(entry.getName(), newChildState.getNodeId());
        }
        // init version history if needed
        VersionHistoryInfo history = null;
        if (versionable && flag == COPY) {
            NodeId copiedFrom = null;
            if (fullVersionable) {
                // base version of copied versionable node is reference value of
                // the histories jcr:copiedFrom property
                PropertyId propId = new PropertyId(srcState.getNodeId(), NameConstants.JCR_BASEVERSION);
                PropertyState prop = (PropertyState) srcStateMgr.getItemState(propId);
                copiedFrom = prop.getValues()[0].getNodeId();
            }
            InternalVersionManager manager = session.getInternalVersionManager();
            history = manager.getVersionHistory(session, newState, copiedFrom);
        }
        // copy properties
        for (Name propName : srcState.getPropertyNames()) {
            Path propPath = PathFactoryImpl.getInstance().create(srcPath, propName, true);
            PropertyId propId = new PropertyId(srcState.getNodeId(), propName);
            if (!srcAccessMgr.canRead(propPath, propId)) {
                continue;
            }
            PropertyState srcChildState = (PropertyState) srcStateMgr.getItemState(propId);
            /**
                 * special handling required for properties with special semantics
                 * (e.g. those defined by mix:referenceable, mix:versionable,
                 * mix:lockable, et.al.)
                 *
                 * todo FIXME delegate to 'node type instance handler'
                 */
            QPropertyDefinition def = ent.getApplicablePropertyDef(srcChildState.getName(), srcChildState.getType(), srcChildState.isMultiValued());
            if (NameConstants.MIX_LOCKABLE.equals(def.getDeclaringNodeType())) {
                // skip properties defined by mix:lockable
                continue;
            }
            PropertyState newChildState = copyPropertyState(srcChildState, id, propName, def);
            if (history != null) {
                if (fullVersionable) {
                    if (propName.equals(NameConstants.JCR_VERSIONHISTORY)) {
                        // jcr:versionHistory
                        InternalValue value = InternalValue.create(history.getVersionHistoryId());
                        newChildState.setValues(new InternalValue[] { value });
                    } else if (propName.equals(NameConstants.JCR_BASEVERSION) || propName.equals(NameConstants.JCR_PREDECESSORS)) {
                        // jcr:baseVersion or jcr:predecessors
                        InternalValue value = InternalValue.create(history.getRootVersionId());
                        newChildState.setValues(new InternalValue[] { value });
                    } else if (propName.equals(NameConstants.JCR_ISCHECKEDOUT)) {
                        // jcr:isCheckedOut
                        newChildState.setValues(new InternalValue[] { InternalValue.create(true) });
                    }
                } else {
                    // version history when we see the jcr:isCheckedOut
                    if (propName.equals(NameConstants.JCR_ISCHECKEDOUT)) {
                        // jcr:isCheckedOut
                        newChildState.setValues(new InternalValue[] { InternalValue.create(true) });
                    }
                }
            }
            if (newChildState.getType() == PropertyType.REFERENCE || newChildState.getType() == PropertyType.WEAKREFERENCE) {
                refTracker.processedReference(newChildState);
            }
            // store new property
            stateMgr.store(newChildState);
            // add new property entry to new node
            newState.addPropertyName(propName);
        }
        return newState;
    } catch (ItemStateException ise) {
        String msg = "internal error: failed to copy state of " + srcState.getNodeId();
        log.debug(msg);
        throw new RepositoryException(msg, ise);
    }
}
Also used : Path(org.apache.jackrabbit.spi.Path) VersionHistoryInfo(org.apache.jackrabbit.core.version.VersionHistoryInfo) NodeState(org.apache.jackrabbit.core.state.NodeState) ChildNodeEntry(org.apache.jackrabbit.core.state.ChildNodeEntry) RepositoryException(javax.jcr.RepositoryException) InternalValue(org.apache.jackrabbit.core.value.InternalValue) NoSuchItemStateException(org.apache.jackrabbit.core.state.NoSuchItemStateException) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException) PropertyId(org.apache.jackrabbit.core.id.PropertyId) PropertyState(org.apache.jackrabbit.core.state.PropertyState) Name(org.apache.jackrabbit.spi.Name) EffectiveNodeType(org.apache.jackrabbit.core.nodetype.EffectiveNodeType) ItemExistsException(javax.jcr.ItemExistsException) QPropertyDefinition(org.apache.jackrabbit.spi.QPropertyDefinition) NodeId(org.apache.jackrabbit.core.id.NodeId) InternalVersionManager(org.apache.jackrabbit.core.version.InternalVersionManager)

Aggregations

InternalValue (org.apache.jackrabbit.core.value.InternalValue)62 Name (org.apache.jackrabbit.spi.Name)21 NodeId (org.apache.jackrabbit.core.id.NodeId)20 PropertyState (org.apache.jackrabbit.core.state.PropertyState)17 RepositoryException (javax.jcr.RepositoryException)16 ItemStateException (org.apache.jackrabbit.core.state.ItemStateException)13 PropertyId (org.apache.jackrabbit.core.id.PropertyId)12 NoSuchItemStateException (org.apache.jackrabbit.core.state.NoSuchItemStateException)11 QPropertyDefinition (org.apache.jackrabbit.spi.QPropertyDefinition)11 NodeState (org.apache.jackrabbit.core.state.NodeState)10 Value (javax.jcr.Value)9 IOException (java.io.IOException)7 InputStream (java.io.InputStream)6 HashMap (java.util.HashMap)6 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)6 FileSystemResource (org.apache.jackrabbit.core.fs.FileSystemResource)6 ArrayList (java.util.ArrayList)5 PropertyDefinition (javax.jcr.nodetype.PropertyDefinition)4 EffectiveNodeType (org.apache.jackrabbit.core.nodetype.EffectiveNodeType)4 Path (org.apache.jackrabbit.spi.Path)4