Search in sources :

Example 66 with QValue

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

the class RepositoryServiceImpl method getRepositoryDescriptors.

@Override
public Map<String, QValue[]> getRepositoryDescriptors() throws RepositoryException {
    if (descriptors.isEmpty()) {
        ReportInfo info = new ReportInfo(JcrRemotingConstants.REPORT_REPOSITORY_DESCRIPTORS, ItemResourceConstants.NAMESPACE);
        HttpReport request = null;
        try {
            request = new HttpReport(uriResolver.getRepositoryUri(), info);
            HttpResponse response = executeRequest(null, request);
            int sc = response.getStatusLine().getStatusCode();
            if (sc == HttpStatus.SC_UNAUTHORIZED || sc == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED) {
                // JCR-3076: Mandatory authentication prevents us from
                // accessing the descriptors on the server, so instead
                // of failing with an exception we simply return an empty
                // set of descriptors
                log.warn("Authentication required to access repository descriptors");
                return descriptors;
            }
            request.checkSuccess(response);
            Document doc = request.getResponseBodyAsDocument(response.getEntity());
            if (doc != null) {
                Element rootElement = doc.getDocumentElement();
                ElementIterator nsElems = DomUtil.getChildren(rootElement, JcrRemotingConstants.XML_DESCRIPTOR, ItemResourceConstants.NAMESPACE);
                while (nsElems.hasNext()) {
                    Element elem = nsElems.nextElement();
                    String key = DomUtil.getChildText(elem, JcrRemotingConstants.XML_DESCRIPTORKEY, ItemResourceConstants.NAMESPACE);
                    ElementIterator it = DomUtil.getChildren(elem, JcrRemotingConstants.XML_DESCRIPTORVALUE, ItemResourceConstants.NAMESPACE);
                    List<QValue> vs = new ArrayList<QValue>();
                    while (it.hasNext()) {
                        Element dv = it.nextElement();
                        String descriptor = DomUtil.getText(dv);
                        if (key != null && descriptor != null) {
                            String typeStr = (DomUtil.getAttribute(dv, JcrRemotingConstants.ATTR_VALUE_TYPE, null));
                            int type = (typeStr == null) ? PropertyType.STRING : PropertyType.valueFromName(typeStr);
                            vs.add(getQValueFactory().create(descriptor, type));
                        } else {
                            log.error("Invalid descriptor key / value pair: " + key + " -> " + descriptor);
                        }
                    }
                    descriptors.put(key, vs.toArray(new QValue[vs.size()]));
                }
            }
        } catch (IOException e) {
            throw new RepositoryException(e);
        } catch (DavException e) {
            throw ExceptionConverter.generate(e);
        } finally {
            if (request != null) {
                request.releaseConnection();
            }
        }
    }
    return descriptors;
}
Also used : HttpReport(org.apache.jackrabbit.webdav.client.methods.HttpReport) ElementIterator(org.apache.jackrabbit.webdav.xml.ElementIterator) QValue(org.apache.jackrabbit.spi.QValue) DavException(org.apache.jackrabbit.webdav.DavException) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) ReportInfo(org.apache.jackrabbit.webdav.version.report.ReportInfo) HttpResponse(org.apache.http.HttpResponse) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) Document(org.w3c.dom.Document)

Example 67 with QValue

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

the class RepositoryServiceImpl method buildPropertyInfos.

private List<PropertyInfo> buildPropertyInfos(NodeInfo nInfo) throws RepositoryException {
    List<PropertyInfo> l = new ArrayList<PropertyInfo>(3);
    NodeId nid = nInfo.getId();
    Path nPath = nInfo.getPath();
    if (nid.getPath() == null) {
        PropertyId id = getIdFactory().createPropertyId(nid, NameConstants.JCR_UUID);
        QValue[] vs = new QValue[] { getQValueFactory().create(nid.getUniqueID(), PropertyType.STRING) };
        Path p = getPathFactory().create(nPath, NameConstants.JCR_UUID, true);
        PropertyInfo pi = new PropertyInfoImpl(id, p, PropertyType.STRING, false, vs);
        l.add(pi);
    }
    Name pName = NameConstants.JCR_PRIMARYTYPE;
    QValue[] vs = new QValue[] { getQValueFactory().create(nInfo.getNodetype()) };
    PropertyInfo pi = new PropertyInfoImpl(getIdFactory().createPropertyId(nid, pName), getPathFactory().create(nPath, pName, true), PropertyType.NAME, false, vs);
    l.add(pi);
    Name[] mixins = nInfo.getMixins();
    if (mixins.length > 0) {
        pName = NameConstants.JCR_MIXINTYPES;
        vs = new QValue[mixins.length];
        for (int i = 0; i < mixins.length; i++) {
            vs[i] = getQValueFactory().create(mixins[i]);
        }
        pi = new PropertyInfoImpl(getIdFactory().createPropertyId(nid, pName), getPathFactory().create(nPath, pName, true), PropertyType.NAME, true, vs);
        l.add(pi);
    }
    return l;
}
Also used : Path(org.apache.jackrabbit.spi.Path) QValue(org.apache.jackrabbit.spi.QValue) ArrayList(java.util.ArrayList) NodeId(org.apache.jackrabbit.spi.NodeId) PropertyInfo(org.apache.jackrabbit.spi.PropertyInfo) PropertyId(org.apache.jackrabbit.spi.PropertyId) DavPropertyName(org.apache.jackrabbit.webdav.property.DavPropertyName) Name(org.apache.jackrabbit.spi.Name)

Example 68 with QValue

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

the class ItemInfoJsonHandler method value.

/**
     * there is currently one special string-value hint:
     *
     *   :<nameOfProperty> : "PropertyTypeName"
     *
     * @param value The value.
     * @throws IOException
     */
public void value(String value) throws IOException {
    if (expectingHintValue) {
        // :<nameOfProperty> : "PropertyTypeName"
        propertyType = PropertyType.valueFromName(value);
        return;
    }
    try {
        QValue v;
        switch(propertyType) {
            case PropertyType.UNDEFINED:
                if (!NameConstants.JCR_UUID.equals(name)) {
                    value = StringCache.cache(value);
                }
                v = vFactory.create(value, PropertyType.STRING);
                break;
            case PropertyType.NAME:
                v = vFactory.create(resolver.getQName(value));
                break;
            case PropertyType.PATH:
                v = vFactory.create(resolver.getQPath(value));
                break;
            default:
                v = vFactory.create(value, propertyType);
        }
        value(v);
    } catch (RepositoryException e) {
        throw new IOException(e.getMessage());
    }
}
Also used : QValue(org.apache.jackrabbit.spi.QValue) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException)

Example 69 with QValue

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

the class DefinitionValidator method validateNodeTypeDef.

/**
     *
     * @param ntDef
     * @param validatedDefs Map of nodetype names and nodetype definitions
     * that are known to be valid or are already registered. This map is used to
     * validated dependencies and check for circular inheritance
     * @return
     * @throws InvalidNodeTypeDefinitionException
     * @throws RepositoryException
     */
public EffectiveNodeType validateNodeTypeDef(QNodeTypeDefinition ntDef, Map<Name, QNodeTypeDefinition> validatedDefs) throws InvalidNodeTypeDefinitionException, RepositoryException {
    /**
         * the effective (i.e. merged and resolved) node type resulting from
         * the specified node type definition;
         * the effective node type will finally be created after the definition
         * has been verified and checked for conflicts etc.; in some cases it
         * will be created already at an earlier stage during the validation
         * of child node definitions
         */
    EffectiveNodeType ent = null;
    Name name = ntDef.getName();
    if (name == null) {
        String msg = "no name specified";
        log.debug(msg);
        throw new InvalidNodeTypeDefinitionException(msg);
    }
    checkNamespace(name);
    // validate supertypes
    Name[] supertypes = ntDef.getSupertypes();
    if (supertypes.length > 0) {
        for (int i = 0; i < supertypes.length; i++) {
            checkNamespace(supertypes[i]);
            /**
                 * simple check for infinite recursion
                 * (won't trap recursion on a deeper inheritance level)
                 */
            if (name.equals(supertypes[i])) {
                String msg = "[" + name + "] invalid supertype: " + supertypes[i] + " (infinite recursion))";
                log.debug(msg);
                throw new InvalidNodeTypeDefinitionException(msg);
            }
            /* compare to given nt-name set and not to registered nodetypes */
            if (!validatedDefs.containsKey(supertypes[i])) {
                String msg = "[" + name + "] invalid supertype: " + supertypes[i];
                log.debug(msg);
                throw new InvalidNodeTypeDefinitionException(msg);
            }
        }
        /**
             * check for circularity in inheritance chain
             * ('a' extends 'b' extends 'a')
             */
        Stack<Name> inheritanceChain = new Stack<Name>();
        inheritanceChain.push(name);
        checkForCircularInheritance(supertypes, inheritanceChain, validatedDefs);
    }
    /**
         * build effective (i.e. merged and resolved) node type from supertypes
         * and check for conflicts
         */
    if (supertypes.length > 0) {
        try {
            EffectiveNodeType est = entProvider.getEffectiveNodeType(supertypes, validatedDefs);
            // make sure that all primary types except nt:base extend from nt:base
            if (!ntDef.isMixin() && !NameConstants.NT_BASE.equals(ntDef.getName()) && !est.includesNodeType(NameConstants.NT_BASE)) {
                String msg = "[" + name + "] all primary node types except" + " nt:base itself must be (directly or indirectly) derived from nt:base";
                log.debug(msg);
                throw new InvalidNodeTypeDefinitionException(msg);
            }
        } catch (ConstraintViolationException e) {
            String msg = "[" + name + "] failed to validate supertypes";
            log.debug(msg);
            throw new InvalidNodeTypeDefinitionException(msg, e);
        } catch (NoSuchNodeTypeException e) {
            String msg = "[" + name + "] failed to validate supertypes";
            log.debug(msg);
            throw new InvalidNodeTypeDefinitionException(msg, e);
        }
    } else {
        // no supertypes specified: has to be either a mixin type or nt:base
        if (!ntDef.isMixin() && !NameConstants.NT_BASE.equals(ntDef.getName())) {
            String msg = "[" + name + "] all primary node types except nt:base itself must be (directly or indirectly) derived from nt:base";
            log.debug(msg);
            throw new InvalidNodeTypeDefinitionException(msg);
        }
    }
    checkNamespace(ntDef.getPrimaryItemName());
    // validate property definitions
    QPropertyDefinition[] pda = ntDef.getPropertyDefs();
    for (int i = 0; i < pda.length; i++) {
        QPropertyDefinition pd = pda[i];
        /**
             * sanity check:
             * make sure declaring node type matches name of node type definition
             */
        if (!name.equals(pd.getDeclaringNodeType())) {
            String msg = "[" + name + "#" + pd.getName() + "] invalid declaring node type specified";
            log.debug(msg);
            throw new InvalidNodeTypeDefinitionException(msg);
        }
        checkNamespace(pd.getName());
        // check that auto-created properties specify a name
        if (pd.definesResidual() && pd.isAutoCreated()) {
            String msg = "[" + name + "#" + pd.getName() + "] auto-created properties must specify a name";
            log.debug(msg);
            throw new InvalidNodeTypeDefinitionException(msg);
        }
        // check that auto-created properties specify a type
        if (pd.getRequiredType() == PropertyType.UNDEFINED && pd.isAutoCreated()) {
            String msg = "[" + name + "#" + pd.getName() + "] auto-created properties must specify a type";
            log.debug(msg);
            throw new InvalidNodeTypeDefinitionException(msg);
        }
        /* check default values:
             * make sure type of value is consistent with required property type
             * Note: default internal values are built from the required type,
             * thus check for match with pd.getRequiredType is redundant.
             */
        QValue[] defVals = pd.getDefaultValues();
        /* check that default values satisfy value constraints.
             * Note however, that no check is performed if autocreated property-
             * definitions define a default value. JSR170 does not require this.
             */
        ValueConstraint.checkValueConstraints(pd, defVals);
        /* ReferenceConstraint:
             * the specified node type must be registered, with one notable
             * exception: the node type just being registered
             */
        QValueConstraint[] constraints = pd.getValueConstraints();
        if (constraints != null && constraints.length > 0) {
            if (pd.getRequiredType() == PropertyType.REFERENCE) {
                for (QValueConstraint constraint : constraints) {
                    // TODO improve. don't rely on a specific factory impl
                    Name ntName = NameFactoryImpl.getInstance().create(constraint.getString());
                    /* compare to given ntd map and not registered nts only */
                    if (!name.equals(ntName) && !validatedDefs.containsKey(ntName)) {
                        String msg = "[" + name + "#" + pd.getName() + "] invalid REFERENCE value constraint '" + ntName + "' (unknown node type)";
                        log.debug(msg);
                        throw new InvalidNodeTypeDefinitionException(msg);
                    }
                }
            }
        }
    }
    // validate child-node definitions
    QNodeDefinition[] cnda = ntDef.getChildNodeDefs();
    for (int i = 0; i < cnda.length; i++) {
        QNodeDefinition cnd = cnda[i];
        /* make sure declaring node type matches name of node type definition */
        if (!name.equals(cnd.getDeclaringNodeType())) {
            String msg = "[" + name + "#" + cnd.getName() + "] invalid declaring node type specified";
            log.debug(msg);
            throw new InvalidNodeTypeDefinitionException(msg);
        }
        checkNamespace(cnd.getName());
        // check that auto-created child-nodes specify a name
        if (cnd.definesResidual() && cnd.isAutoCreated()) {
            String msg = "[" + name + "#" + cnd.getName() + "] auto-created child-nodes must specify a name";
            log.debug(msg);
            throw new InvalidNodeTypeDefinitionException(msg);
        }
        // check that auto-created child-nodes specify a default primary type
        if (cnd.getDefaultPrimaryType() == null && cnd.isAutoCreated()) {
            String msg = "[" + name + "#" + cnd.getName() + "] auto-created child-nodes must specify a default primary type";
            log.debug(msg);
            throw new InvalidNodeTypeDefinitionException(msg);
        }
        // check default primary type
        Name dpt = cnd.getDefaultPrimaryType();
        checkNamespace(dpt);
        boolean referenceToSelf = false;
        EffectiveNodeType defaultENT = null;
        if (dpt != null) {
            // check if this node type specifies itself as default primary type
            if (name.equals(dpt)) {
                referenceToSelf = true;
            }
            /**
                 * the default primary type must be registered, with one notable
                 * exception: the node type just being registered
                 */
            if (!name.equals(dpt) && !validatedDefs.containsKey(dpt)) {
                String msg = "[" + name + "#" + cnd.getName() + "] invalid default primary type '" + dpt + "'";
                log.debug(msg);
                throw new InvalidNodeTypeDefinitionException(msg);
            }
            /**
                 * build effective (i.e. merged and resolved) node type from
                 * default primary type and check for conflicts
                 */
            try {
                if (!referenceToSelf) {
                    defaultENT = entProvider.getEffectiveNodeType(new Name[] { dpt }, validatedDefs);
                } else {
                    /**
                         * the default primary type is identical with the node
                         * type just being registered; we have to instantiate it
                         * 'manually'
                         */
                    ent = entProvider.getEffectiveNodeType(ntDef, validatedDefs);
                    defaultENT = ent;
                }
                if (cnd.isAutoCreated()) {
                    /**
                         * check for circularity through default primary types
                         * of auto-created child nodes (node type 'a' defines
                         * auto-created child node with default primary type 'a')
                         */
                    Stack<Name> definingNTs = new Stack<Name>();
                    definingNTs.push(name);
                    checkForCircularNodeAutoCreation(defaultENT, definingNTs, validatedDefs);
                }
            } catch (ConstraintViolationException e) {
                String msg = "[" + name + "#" + cnd.getName() + "] failed to validate default primary type";
                log.debug(msg);
                throw new InvalidNodeTypeDefinitionException(msg, e);
            } catch (NoSuchNodeTypeException e) {
                String msg = "[" + name + "#" + cnd.getName() + "] failed to validate default primary type";
                log.debug(msg);
                throw new InvalidNodeTypeDefinitionException(msg, e);
            }
        }
        // check required primary types
        Name[] reqTypes = cnd.getRequiredPrimaryTypes();
        if (reqTypes != null && reqTypes.length > 0) {
            for (int n = 0; n < reqTypes.length; n++) {
                Name rpt = reqTypes[n];
                checkNamespace(rpt);
                referenceToSelf = false;
                /**
                     * check if this node type specifies itself as required
                     * primary type
                     */
                if (name.equals(rpt)) {
                    referenceToSelf = true;
                }
                /**
                     * the required primary type must be registered, with one
                     * notable exception: the node type just being registered
                     */
                if (!name.equals(rpt) && !validatedDefs.containsKey(rpt)) {
                    String msg = "[" + name + "#" + cnd.getName() + "] invalid required primary type: " + rpt;
                    log.debug(msg);
                    throw new InvalidNodeTypeDefinitionException(msg);
                }
                /**
                     * check if default primary type satisfies the required
                     * primary type constraint
                     */
                if (defaultENT != null && !defaultENT.includesNodeType(rpt)) {
                    String msg = "[" + name + "#" + cnd.getName() + "] default primary type does not satisfy required primary type constraint " + rpt;
                    log.debug(msg);
                    throw new InvalidNodeTypeDefinitionException(msg);
                }
                /**
                     * build effective (i.e. merged and resolved) node type from
                     * required primary type constraint and check for conflicts
                     */
                try {
                    if (!referenceToSelf) {
                        entProvider.getEffectiveNodeType(new Name[] { rpt }, validatedDefs);
                    } else {
                        /**
                             * the required primary type is identical with the
                             * node type just being registered; we have to
                             * instantiate it 'manually'
                             */
                        if (ent == null) {
                            ent = entProvider.getEffectiveNodeType(ntDef, validatedDefs);
                        }
                    }
                } catch (ConstraintViolationException e) {
                    String msg = "[" + name + "#" + cnd.getName() + "] failed to validate required primary type constraint";
                    log.debug(msg);
                    throw new InvalidNodeTypeDefinitionException(msg, e);
                } catch (NoSuchNodeTypeException e) {
                    String msg = "[" + name + "#" + cnd.getName() + "] failed to validate required primary type constraint";
                    log.debug(msg);
                    throw new InvalidNodeTypeDefinitionException(msg, e);
                }
            }
        }
    }
    /**
         * now build effective (i.e. merged and resolved) node type from
         * this node type definition; this will potentially detect more
         * conflicts or problems
         */
    if (ent == null) {
        try {
            ent = entProvider.getEffectiveNodeType(ntDef, validatedDefs);
        } catch (ConstraintViolationException e) {
            String msg = "[" + name + "] failed to resolve node type definition";
            log.debug(msg);
            throw new InvalidNodeTypeDefinitionException(msg, e);
        } catch (NoSuchNodeTypeException e) {
            String msg = "[" + name + "] failed to resolve node type definition";
            log.debug(msg);
            throw new InvalidNodeTypeDefinitionException(msg, e);
        }
    }
    return ent;
}
Also used : InvalidNodeTypeDefinitionException(javax.jcr.nodetype.InvalidNodeTypeDefinitionException) QValue(org.apache.jackrabbit.spi.QValue) QValueConstraint(org.apache.jackrabbit.spi.QValueConstraint) QNodeDefinition(org.apache.jackrabbit.spi.QNodeDefinition) ValueConstraint(org.apache.jackrabbit.spi.commons.nodetype.constraint.ValueConstraint) QValueConstraint(org.apache.jackrabbit.spi.QValueConstraint) Name(org.apache.jackrabbit.spi.Name) Stack(java.util.Stack) NoSuchNodeTypeException(javax.jcr.nodetype.NoSuchNodeTypeException) QPropertyDefinition(org.apache.jackrabbit.spi.QPropertyDefinition) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException)

Example 70 with QValue

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

the class NodeTypeImpl method canSetProperty.

/**
     * @see javax.jcr.nodetype.NodeType#canSetProperty(String, Value)
     */
public boolean canSetProperty(String propertyName, Value value) {
    if (value == null) {
        // setting a property to null is equivalent of removing it
        return canRemoveItem(propertyName);
    }
    try {
        Name name = resolver().getQName(propertyName);
        QPropertyDefinition def;
        try {
            // try to get definition that matches the given value type
            def = getApplicablePropDef(name, value.getType(), false);
        } catch (ConstraintViolationException cve) {
            // fallback: ignore type
            def = getApplicablePropDef(name, PropertyType.UNDEFINED, false);
        }
        if (def.isProtected()) {
            return false;
        }
        if (def.isMultiple()) {
            return false;
        }
        Value v;
        if (def.getRequiredType() != PropertyType.UNDEFINED && def.getRequiredType() != value.getType()) {
            // type conversion required
            v = ValueHelper.convert(value, def.getRequiredType(), mgrProvider.getJcrValueFactory());
        } else {
            // no type conversion required
            v = value;
        }
        // create QValue from Value
        QValue qValue = ValueFormat.getQValue(v, resolver(), mgrProvider.getQValueFactory());
        checkSetPropertyValueConstraints(def, new QValue[] { qValue });
        return true;
    } catch (NameException re) {
    // fall through
    } catch (RepositoryException e) {
    // fall through
    }
    return false;
}
Also used : QValue(org.apache.jackrabbit.spi.QValue) NameException(org.apache.jackrabbit.spi.commons.conversion.NameException) QPropertyDefinition(org.apache.jackrabbit.spi.QPropertyDefinition) QValue(org.apache.jackrabbit.spi.QValue) Value(javax.jcr.Value) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) RepositoryException(javax.jcr.RepositoryException) Name(org.apache.jackrabbit.spi.Name)

Aggregations

QValue (org.apache.jackrabbit.spi.QValue)125 Name (org.apache.jackrabbit.spi.Name)40 RepositoryException (javax.jcr.RepositoryException)23 Value (javax.jcr.Value)21 NodeId (org.apache.jackrabbit.spi.NodeId)21 Batch (org.apache.jackrabbit.spi.Batch)19 PropertyInfo (org.apache.jackrabbit.spi.PropertyInfo)18 QPropertyDefinition (org.apache.jackrabbit.spi.QPropertyDefinition)12 IOException (java.io.IOException)9 ArrayList (java.util.ArrayList)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 ValueFormatException (javax.jcr.ValueFormatException)8 Path (org.apache.jackrabbit.spi.Path)8 QValueConstraint (org.apache.jackrabbit.spi.QValueConstraint)8 HashMap (java.util.HashMap)7 ValueConstraint (org.apache.jackrabbit.spi.commons.nodetype.constraint.ValueConstraint)7 InputStream (java.io.InputStream)5 Node (javax.jcr.Node)5 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)5 PropertyId (org.apache.jackrabbit.spi.PropertyId)5