Search in sources :

Example 16 with NameException

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

the class NodeImpl method rename.

//-------------------------------------------------------< JackrabbitNode >
/**
     * {@inheritDoc}
     */
public void rename(String newName) throws RepositoryException {
    // check if this is the root node
    if (getDepth() == 0) {
        throw new RepositoryException("Cannot rename the root node");
    }
    Name qName;
    try {
        qName = sessionContext.getQName(newName);
    } catch (NameException e) {
        throw new RepositoryException("invalid node name: " + newName, e);
    }
    NodeImpl parent = (NodeImpl) getParent();
    // check for name collisions
    NodeImpl existing = null;
    try {
        existing = parent.getNode(qName);
        // check same-name sibling setting of existing node
        if (!existing.getDefinition().allowsSameNameSiblings()) {
            throw new ItemExistsException("Same name siblings are not allowed: " + existing);
        }
    } catch (AccessDeniedException ade) {
        // FIXME by throwing ItemExistsException we're disclosing too much information
        throw new ItemExistsException();
    } catch (ItemNotFoundException infe) {
    // no name collision, fall through
    }
    // verify that parent node
    // - is checked-out
    // - is not protected neither by node type constraints nor by retention/hold
    int options = ItemValidator.CHECK_CHECKED_OUT | ItemValidator.CHECK_LOCK | ItemValidator.CHECK_CONSTRAINTS | ItemValidator.CHECK_HOLD | ItemValidator.CHECK_RETENTION;
    sessionContext.getItemValidator().checkRemove(parent, options, Permission.NONE);
    sessionContext.getItemValidator().checkModify(parent, options, Permission.NONE);
    // check constraints
    // get applicable definition of renamed target node
    NodeTypeImpl nt = (NodeTypeImpl) getPrimaryNodeType();
    org.apache.jackrabbit.spi.commons.nodetype.NodeDefinitionImpl newTargetDef;
    try {
        newTargetDef = parent.getApplicableChildNodeDefinition(qName, nt.getQName());
    } catch (RepositoryException re) {
        String msg = safeGetJCRPath() + ": no definition found in parent node's node type for renamed node";
        log.debug(msg);
        throw new ConstraintViolationException(msg, re);
    }
    // necessarily have identical definitions
    if (existing != null && !newTargetDef.allowsSameNameSiblings()) {
        throw new ItemExistsException("Same name siblings not allowed: " + existing);
    }
    // check permissions:
    // 1. on the parent node the session must have permission to manipulate the child-entries
    AccessManager acMgr = sessionContext.getAccessManager();
    if (!acMgr.isGranted(parent.getPrimaryPath(), qName, Permission.MODIFY_CHILD_NODE_COLLECTION)) {
        String msg = "Not allowed to rename node " + safeGetJCRPath() + " to " + newName;
        log.debug(msg);
        throw new AccessDeniedException(msg);
    }
    //    the primary node type on this node itself.
    if (!nt.getName().equals(newTargetDef.getName()) && !(acMgr.isGranted(getPrimaryPath(), Permission.NODE_TYPE_MNGMT))) {
        String msg = "Not allowed to rename node " + safeGetJCRPath() + " to " + newName;
        log.debug(msg);
        throw new AccessDeniedException(msg);
    }
    // change definition
    onRedefine(newTargetDef.unwrap());
    // delegate to parent
    parent.renameChildNode(getNodeId(), qName, true);
}
Also used : AccessManager(org.apache.jackrabbit.core.security.AccessManager) AccessDeniedException(javax.jcr.AccessDeniedException) NodeDefinitionImpl(org.apache.jackrabbit.spi.commons.nodetype.NodeDefinitionImpl) NodeTypeImpl(org.apache.jackrabbit.core.nodetype.NodeTypeImpl) RepositoryException(javax.jcr.RepositoryException) Name(org.apache.jackrabbit.spi.Name) NameException(org.apache.jackrabbit.spi.commons.conversion.NameException) ItemExistsException(javax.jcr.ItemExistsException) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 17 with NameException

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

the class NodeImpl method getReferences.

/**
     * {@inheritDoc}
     */
public PropertyIterator getReferences(String name) throws RepositoryException {
    // check state of this instance
    sanityCheck();
    try {
        if (stateMgr.hasNodeReferences(getNodeId())) {
            NodeReferences refs = stateMgr.getNodeReferences(getNodeId());
            // refs.getReferences() returns a list of PropertyId's
            List<PropertyId> idList = refs.getReferences();
            if (name != null) {
                Name qName;
                try {
                    qName = sessionContext.getQName(name);
                } catch (NameException e) {
                    throw new RepositoryException("invalid property name: " + name, e);
                }
                ArrayList<PropertyId> filteredList = new ArrayList<PropertyId>(idList.size());
                for (PropertyId propId : idList) {
                    if (propId.getName().equals(qName)) {
                        filteredList.add(propId);
                    }
                }
                idList = filteredList;
            }
            return new LazyItemIterator(sessionContext, idList);
        } else {
            // there are no references, return empty iterator
            return PropertyIteratorAdapter.EMPTY;
        }
    } catch (ItemStateException e) {
        String msg = "Unable to retrieve REFERENCE properties that refer to " + id;
        log.debug(msg);
        throw new RepositoryException(msg, e);
    }
}
Also used : NameException(org.apache.jackrabbit.spi.commons.conversion.NameException) ArrayList(java.util.ArrayList) NodeReferences(org.apache.jackrabbit.core.state.NodeReferences) RepositoryException(javax.jcr.RepositoryException) PropertyId(org.apache.jackrabbit.core.id.PropertyId) Name(org.apache.jackrabbit.spi.Name) InvalidItemStateException(javax.jcr.InvalidItemStateException) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException)

Example 18 with NameException

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

the class DocViewImportHandler method startElement.

//-------------------------------------------------------< ContentHandler >
/**
     * {@inheritDoc}
     * <p>
     * See also {@link org.apache.jackrabbit.commons.xml.DocumentViewExporter#exportProperty(String, String, int, javax.jcr.Value[])}
     * regarding special handling of multi-valued properties on export.
     */
@Override
public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
    // process buffered character data
    processCharacters();
    try {
        String dcdLocalName = ISO9075.decode(localName);
        Name nodeName = nameFactory.create(namespaceURI, dcdLocalName);
        // properties
        String uuid = null;
        Name nodeTypeName = null;
        Name[] mixinTypes = null;
        List<Importer.PropInfo> props = new ArrayList<Importer.PropInfo>(atts.getLength());
        for (int i = 0; i < atts.getLength(); i++) {
            if (atts.getURI(i).equals(Name.NS_XMLNS_URI)) {
                // see http://issues.apache.org/jira/browse/JCR-620#action_12448164
                continue;
            }
            dcdLocalName = ISO9075.decode(atts.getLocalName(i));
            Name propName = nameFactory.create(atts.getURI(i), dcdLocalName);
            // attribute value
            String attrValue = atts.getValue(i);
            if (propName.equals(NameConstants.JCR_PRIMARYTYPE)) {
                // jcr:primaryType
                if (attrValue.length() > 0) {
                    try {
                        nodeTypeName = resolver.getQName(attrValue);
                    } catch (NameException ne) {
                        throw new SAXException("illegal jcr:primaryType value: " + attrValue, ne);
                    }
                }
            } else if (propName.equals(NameConstants.JCR_MIXINTYPES)) {
                // jcr:mixinTypes
                mixinTypes = parseNames(attrValue);
            } else if (propName.equals(NameConstants.JCR_UUID)) {
                // jcr:uuid
                if (attrValue.length() > 0) {
                    uuid = attrValue;
                }
            } else {
                // always assume single-valued property for the time being
                // until a way of properly serializing/detecting multi-valued
                // properties on re-import is found (see JCR-325);
                // see also DocViewSAXEventGenerator#leavingProperties(Node, int)
                // TODO: proper multi-value serialization support
                Importer.TextValue[] propValues = new Importer.TextValue[1];
                propValues[0] = new StringValue(attrValue);
                props.add(new Importer.PropInfo(propName, PropertyType.UNDEFINED, propValues));
            }
        }
        Importer.NodeInfo node = new Importer.NodeInfo(nodeName, nodeTypeName, mixinTypes, uuid);
        // all information has been collected, now delegate to importer
        importer.startNode(node, props, resolver);
        // push current node data onto stack
        stack.push(node);
    } catch (RepositoryException re) {
        throw new SAXException(re);
    }
}
Also used : ArrayList(java.util.ArrayList) RepositoryException(javax.jcr.RepositoryException) Name(org.apache.jackrabbit.spi.Name) SAXException(org.xml.sax.SAXException) NameException(org.apache.jackrabbit.spi.commons.conversion.NameException)

Example 19 with NameException

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

the class LuceneQueryBuilder method getStringValues.

/**
     * Returns an array of String values to be used as a term to lookup the search index
     * for a String <code>literal</code> of a certain property name. This method
     * will lookup the <code>propertyName</code> in the node type registry
     * trying to find out the {@link javax.jcr.PropertyType}s.
     * If no property type is found looking up node type information, this
     * method will guess the property type.
     *
     * @param propertyName the name of the property in the relation.
     * @param literal      the String literal in the relation.
     * @return the String values to use as term for the query.
     */
private String[] getStringValues(Name propertyName, String literal) {
    PropertyTypeRegistry.TypeMapping[] types = propRegistry.getPropertyTypes(propertyName);
    List<String> values = new ArrayList<String>();
    for (PropertyTypeRegistry.TypeMapping type : types) {
        switch(type.type) {
            case PropertyType.NAME:
                // try to translate name
                try {
                    Name n = session.getQName(literal);
                    values.add(nsMappings.translateName(n));
                    log.debug("Coerced " + literal + " into NAME.");
                } catch (NameException e) {
                    log.debug("Unable to coerce '" + literal + "' into a NAME: " + e.toString());
                } catch (NamespaceException e) {
                    log.debug("Unable to coerce '" + literal + "' into a NAME: " + e.toString());
                }
                break;
            case PropertyType.PATH:
                // try to translate path
                try {
                    Path p = session.getQPath(literal);
                    values.add(resolver.getJCRPath(p));
                    log.debug("Coerced " + literal + " into PATH.");
                } catch (NameException e) {
                    log.debug("Unable to coerce '" + literal + "' into a PATH: " + e.toString());
                } catch (NamespaceException e) {
                    log.debug("Unable to coerce '" + literal + "' into a PATH: " + e.toString());
                }
                break;
            case PropertyType.DATE:
                // try to parse date
                Calendar c = ISO8601.parse(literal);
                if (c != null) {
                    values.add(DateField.timeToString(c.getTimeInMillis()));
                    log.debug("Coerced " + literal + " into DATE.");
                } else {
                    log.debug("Unable to coerce '" + literal + "' into a DATE.");
                }
                break;
            case PropertyType.DOUBLE:
                // try to parse double
                try {
                    double d = Double.parseDouble(literal);
                    values.add(DoubleField.doubleToString(d));
                    log.debug("Coerced " + literal + " into DOUBLE.");
                } catch (NumberFormatException e) {
                    log.debug("Unable to coerce '" + literal + "' into a DOUBLE: " + e.toString());
                }
                break;
            case PropertyType.LONG:
                // try to parse long
                try {
                    long l = Long.parseLong(literal);
                    values.add(LongField.longToString(l));
                    log.debug("Coerced " + literal + " into LONG.");
                } catch (NumberFormatException e) {
                    log.debug("Unable to coerce '" + literal + "' into a LONG: " + e.toString());
                }
                break;
            case PropertyType.DECIMAL:
                // try to parse decimal
                try {
                    BigDecimal d = new BigDecimal(literal);
                    values.add(DecimalField.decimalToString(d));
                    log.debug("Coerced " + literal + " into DECIMAL.");
                } catch (NumberFormatException e) {
                    log.debug("Unable to coerce '" + literal + "' into a DECIMAL: " + e.toString());
                }
                break;
            case PropertyType.URI:
            // fall through... TODO: correct?
            case PropertyType.STRING:
                values.add(literal);
                log.debug("Using literal " + literal + " as is.");
                break;
        }
    }
    if (values.size() == 0) {
        // use literal as is then try to guess other types
        values.add(literal);
        // try to guess property type
        if (literal.indexOf('/') > -1) {
            // might be a path
            try {
                values.add(resolver.getJCRPath(session.getQPath(literal)));
                log.debug("Coerced " + literal + " into PATH.");
            } catch (Exception e) {
            // not a path
            }
        }
        if (XMLChar.isValidName(literal)) {
            // might be a name
            try {
                Name n = session.getQName(literal);
                values.add(nsMappings.translateName(n));
                log.debug("Coerced " + literal + " into NAME.");
            } catch (Exception e) {
            // not a name
            }
        }
        if (literal.indexOf(':') > -1) {
            // is it a date?
            Calendar c = ISO8601.parse(literal);
            if (c != null) {
                values.add(DateField.timeToString(c.getTimeInMillis()));
                log.debug("Coerced " + literal + " into DATE.");
            }
        } else {
            // long or double are possible at this point
            try {
                values.add(LongField.longToString(Long.parseLong(literal)));
                log.debug("Coerced " + literal + " into LONG.");
            } catch (NumberFormatException e) {
                // try double
                try {
                    values.add(DoubleField.doubleToString(Double.parseDouble(literal)));
                    log.debug("Coerced " + literal + " into DOUBLE.");
                } catch (NumberFormatException e1) {
                // not a double
                }
            }
        }
    }
    // if still no values use literal as is
    if (values.size() == 0) {
        values.add(literal);
        log.debug("Using literal " + literal + " as is.");
    }
    return values.toArray(new String[values.size()]);
}
Also used : Path(org.apache.jackrabbit.spi.Path) PropertyTypeRegistry(org.apache.jackrabbit.core.query.PropertyTypeRegistry) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal) NameException(org.apache.jackrabbit.spi.commons.conversion.NameException) ParseException(org.apache.lucene.queryParser.ParseException) RepositoryException(javax.jcr.RepositoryException) NamespaceException(javax.jcr.NamespaceException) InvalidQueryException(javax.jcr.query.InvalidQueryException) Name(org.apache.jackrabbit.spi.Name) NameException(org.apache.jackrabbit.spi.commons.conversion.NameException) NamespaceException(javax.jcr.NamespaceException)

Example 20 with NameException

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

the class LuceneQueryBuilder method visit.

public Object visit(NodeTypeQueryNode node, Object data) {
    List<Term> terms = new ArrayList<Term>();
    try {
        String mixinTypesField = resolver.getJCRName(NameConstants.JCR_MIXINTYPES);
        String primaryTypeField = resolver.getJCRName(NameConstants.JCR_PRIMARYTYPE);
        NodeTypeManager ntMgr = session.getWorkspace().getNodeTypeManager();
        NodeType base = ntMgr.getNodeType(session.getJCRName(node.getValue()));
        if (base.isMixin()) {
            // search for nodes where jcr:mixinTypes is set to this mixin
            Term t = new Term(FieldNames.PROPERTIES, FieldNames.createNamedValue(mixinTypesField, resolver.getJCRName(node.getValue())));
            terms.add(t);
        } else {
            // search for nodes where jcr:primaryType is set to this type
            Term t = new Term(FieldNames.PROPERTIES, FieldNames.createNamedValue(primaryTypeField, resolver.getJCRName(node.getValue())));
            terms.add(t);
        }
        // now search for all node types that are derived from base
        NodeTypeIterator allTypes = ntMgr.getAllNodeTypes();
        while (allTypes.hasNext()) {
            NodeType nt = allTypes.nextNodeType();
            NodeType[] superTypes = nt.getSupertypes();
            if (Arrays.asList(superTypes).contains(base)) {
                Name n = session.getQName(nt.getName());
                String ntName = nsMappings.translateName(n);
                Term t;
                if (nt.isMixin()) {
                    // search on jcr:mixinTypes
                    t = new Term(FieldNames.PROPERTIES, FieldNames.createNamedValue(mixinTypesField, ntName));
                } else {
                    // search on jcr:primaryType
                    t = new Term(FieldNames.PROPERTIES, FieldNames.createNamedValue(primaryTypeField, ntName));
                }
                terms.add(t);
            }
        }
    } catch (NameException e) {
        exceptions.add(e);
    } catch (RepositoryException e) {
        exceptions.add(e);
    }
    if (terms.size() == 0) {
        // exception occured
        return new BooleanQuery();
    } else if (terms.size() == 1) {
        return new JackrabbitTermQuery(terms.get(0));
    } else {
        BooleanQuery b = new BooleanQuery();
        for (Term term : terms) {
            b.add(new JackrabbitTermQuery(term), Occur.SHOULD);
        }
        return b;
    }
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) ArrayList(java.util.ArrayList) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator) RepositoryException(javax.jcr.RepositoryException) Term(org.apache.lucene.index.Term) Name(org.apache.jackrabbit.spi.Name) NameException(org.apache.jackrabbit.spi.commons.conversion.NameException) NodeType(javax.jcr.nodetype.NodeType)

Aggregations

NameException (org.apache.jackrabbit.spi.commons.conversion.NameException)65 RepositoryException (javax.jcr.RepositoryException)44 Name (org.apache.jackrabbit.spi.Name)38 Path (org.apache.jackrabbit.spi.Path)20 NamespaceException (javax.jcr.NamespaceException)17 ArrayList (java.util.ArrayList)16 IllegalNameException (org.apache.jackrabbit.spi.commons.conversion.IllegalNameException)9 SAXException (org.xml.sax.SAXException)8 Node (javax.jcr.Node)6 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)6 InvalidQueryException (javax.jcr.query.InvalidQueryException)6 NodeId (org.apache.jackrabbit.core.id.NodeId)6 IOException (java.io.IOException)5 ItemNotFoundException (javax.jcr.ItemNotFoundException)5 PathNotFoundException (javax.jcr.PathNotFoundException)5 Value (javax.jcr.Value)5 LocationStepQueryNode (org.apache.jackrabbit.spi.commons.query.LocationStepQueryNode)5 AccessDeniedException (javax.jcr.AccessDeniedException)4 NodeType (javax.jcr.nodetype.NodeType)4 VersionException (javax.jcr.version.VersionException)4