Search in sources :

Example 51 with NodeTypeManager

use of javax.jcr.nodetype.NodeTypeManager in project jackrabbit by apache.

the class AbstractNode method getPrimaryNodeType.

/**
     * Returns the primary node type of this node.
     * <p>
     * The default implementation uses the value of the
     * <code>jcr:primaryType</code> property to look up the primary
     * node type from the {@link NodeTypeManager} of the current workspace.
     *
     * @return primary node type
     * @throws RepositoryException if an error occurs
     */
public NodeType getPrimaryNodeType() throws RepositoryException {
    NodeTypeManager manager = getSession().getWorkspace().getNodeTypeManager();
    Property property = getProperty(getName("jcr:primaryType"));
    return manager.getNodeType(property.getString());
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) Property(javax.jcr.Property)

Example 52 with NodeTypeManager

use of javax.jcr.nodetype.NodeTypeManager in project jackrabbit by apache.

the class AbstractNode method getMixinNodeTypes.

//----------------------------------------------------------------< Node >
/**
     * Returns the declared mixin node types of this node.
     * <p>
     * The default implementation uses the values of the
     * <code>jcr:mixinTypes</code> property to look up the mixin node types
     * from the {@link NodeTypeManager} of the current workspace.
     *
     * @return mixin node types
     * @throws RepositoryException if an error occurs
     */
public NodeType[] getMixinNodeTypes() throws RepositoryException {
    try {
        NodeTypeManager manager = getSession().getWorkspace().getNodeTypeManager();
        Property property = getProperty(getName("jcr:mixinTypes"));
        Value[] values = property.getValues();
        NodeType[] types = new NodeType[values.length];
        for (int i = 0; i < values.length; i++) {
            types[i] = manager.getNodeType(values[i].getString());
        }
        return types;
    } catch (PathNotFoundException e) {
        // jcr:mixinTypes does not exist, i.e. no mixin types on this node
        return new NodeType[0];
    }
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NodeType(javax.jcr.nodetype.NodeType) Value(javax.jcr.Value) PathNotFoundException(javax.jcr.PathNotFoundException) Property(javax.jcr.Property)

Example 53 with NodeTypeManager

use of javax.jcr.nodetype.NodeTypeManager in project jackrabbit by apache.

the class WorkspaceResourceImpl method setProperty.

/**
     * Allows to alter the registered namespaces ({@link ItemResourceConstants#JCR_NAMESPACES})
     * or register node types {@link ItemResourceConstants#JCR_NODETYPES_CND}
     * where the passed value is a cnd string containing the definition
     * and forwards any other property to the super class.
     * <p>
     * Note that again no property status is set. Any failure while setting
     * a property results in an exception (violating RFC 2518).
     *
     * @param property
     * @throws DavException
     * @see DavResource#setProperty(org.apache.jackrabbit.webdav.property.DavProperty)
     */
@Override
public void setProperty(DavProperty<?> property) throws DavException {
    if (ItemResourceConstants.JCR_NAMESPACES.equals(property.getName())) {
        NamespacesProperty nsp = new NamespacesProperty(property);
        try {
            Map<String, String> changes = new HashMap<String, String>(nsp.getNamespaces());
            NamespaceRegistry nsReg = getRepositorySession().getWorkspace().getNamespaceRegistry();
            for (String prefix : nsReg.getPrefixes()) {
                if (!changes.containsKey(prefix)) {
                    // prefix not present amongst the new values any more > unregister
                    nsReg.unregisterNamespace(prefix);
                } else if (changes.get(prefix).equals(nsReg.getURI(prefix))) {
                    // present with same uri-value >> no action required
                    changes.remove(prefix);
                }
            }
            // it has not been present before.
            for (String prefix : changes.keySet()) {
                String uri = changes.get(prefix);
                nsReg.registerNamespace(prefix, uri);
            }
        } catch (RepositoryException e) {
            throw new JcrDavException(e);
        }
    } else if (ItemResourceConstants.JCR_NODETYPES_CND.equals(property.getName())) {
        try {
            Object value = property.getValue();
            List<?> cmds;
            if (value instanceof List) {
                cmds = (List) value;
            } else if (value instanceof Element) {
                cmds = Collections.singletonList(value);
            } else {
                log.warn("Unexpected structure of dcr:nodetypes-cnd property.");
                throw new DavException(DavServletResponse.SC_INTERNAL_SERVER_ERROR);
            }
            String registerCnd = null;
            boolean allowUpdate = false;
            List<String> unregisterNames = new ArrayList<String>();
            for (Object listEntry : cmds) {
                if (listEntry instanceof Element) {
                    Element e = (Element) listEntry;
                    String localName = e.getLocalName();
                    if (ItemResourceConstants.XML_CND.equals(localName)) {
                        registerCnd = DomUtil.getText(e);
                    } else if (ItemResourceConstants.XML_ALLOWUPDATE.equals(localName)) {
                        String allow = DomUtil.getTextTrim(e);
                        allowUpdate = Boolean.parseBoolean(allow);
                    } else if (ItemResourceConstants.XML_NODETYPENAME.equals(localName)) {
                        unregisterNames.add(DomUtil.getTextTrim(e));
                    }
                }
            }
            // TODO: for simplicity it's currently either registration or unregistration as nt-modifications are immediately persisted.
            Session s = getRepositorySession();
            NodeTypeManager ntMgr = s.getWorkspace().getNodeTypeManager();
            if (registerCnd != null) {
                StringReader reader = new StringReader(registerCnd);
                DefinitionBuilderFactory<NodeTypeTemplate, NamespaceRegistry> factory = new TemplateBuilderFactory(ntMgr, s.getValueFactory(), s.getWorkspace().getNamespaceRegistry());
                CompactNodeTypeDefReader<NodeTypeTemplate, NamespaceRegistry> cndReader = new CompactNodeTypeDefReader<NodeTypeTemplate, NamespaceRegistry>(reader, "davex", factory);
                List<NodeTypeTemplate> ntts = cndReader.getNodeTypeDefinitions();
                ntMgr.registerNodeTypes(ntts.toArray(new NodeTypeTemplate[ntts.size()]), allowUpdate);
            } else if (!unregisterNames.isEmpty()) {
                ntMgr.unregisterNodeTypes(unregisterNames.toArray(new String[unregisterNames.size()]));
            }
        } catch (ParseException e) {
            throw new DavException(DavServletResponse.SC_BAD_REQUEST, e);
        } catch (RepositoryException e) {
            throw new JcrDavException(e);
        }
    } else {
        // only jcr:namespace or node types can be modified
        throw new DavException(DavServletResponse.SC_CONFLICT);
    }
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) HashMap(java.util.HashMap) DavException(org.apache.jackrabbit.webdav.DavException) Element(org.w3c.dom.Element) TemplateBuilderFactory(org.apache.jackrabbit.commons.cnd.TemplateBuilderFactory) RepositoryException(javax.jcr.RepositoryException) NamespaceRegistry(javax.jcr.NamespaceRegistry) NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) StringReader(java.io.StringReader) DefinitionBuilderFactory(org.apache.jackrabbit.commons.cnd.DefinitionBuilderFactory) List(java.util.List) ArrayList(java.util.ArrayList) ParseException(org.apache.jackrabbit.commons.cnd.ParseException) NamespacesProperty(org.apache.jackrabbit.webdav.jcr.property.NamespacesProperty) CompactNodeTypeDefReader(org.apache.jackrabbit.commons.cnd.CompactNodeTypeDefReader) Session(javax.jcr.Session)

Example 54 with NodeTypeManager

use of javax.jcr.nodetype.NodeTypeManager in project jackrabbit by apache.

the class NodeTypesReport method getNodeTypes.

/**
     * Parse the Xml element in the info object an return an interator over
     * the specified node types.
     *
     * @return
     * @throws RepositoryException
     * @throws DavException
     */
private static NodeTypeIterator getNodeTypes(Session session, ReportInfo info) throws RepositoryException, DavException {
    NodeTypeManager ntMgr = session.getWorkspace().getNodeTypeManager();
    // check the simple types first...
    if (info.containsContentElement(XML_REPORT_ALLNODETYPES, ItemResourceConstants.NAMESPACE)) {
        return ntMgr.getAllNodeTypes();
    } else if (info.containsContentElement(XML_REPORT_MIXINNODETYPES, ItemResourceConstants.NAMESPACE)) {
        return ntMgr.getMixinNodeTypes();
    } else if (info.containsContentElement(XML_REPORT_PRIMARYNODETYPES, ItemResourceConstants.NAMESPACE)) {
        return ntMgr.getPrimaryNodeTypes();
    } else {
        // None of the simple types. test if a report for individual
        // nodetype was request. If not, the request body is not valid.
        List<Element> elemList = info.getContentElements(XML_NODETYPE, ItemResourceConstants.NAMESPACE);
        if (elemList.isEmpty()) {
            // throw exception if the request body does not contain a single nodetype element
            throw new DavException(DavServletResponse.SC_BAD_REQUEST, "NodeTypes report: request body has invalid format.");
        }
        // todo: find better solution...
        List<NodeType> ntList = new ArrayList<NodeType>();
        for (Element el : elemList) {
            String nodetypeName = DomUtil.getChildTextTrim(el, XML_NODETYPENAME, ItemResourceConstants.NAMESPACE);
            if (nodetypeName != null) {
                ntList.add(ntMgr.getNodeType(nodetypeName));
            }
        }
        return new NodeTypeIteratorAdapter(ntList);
    }
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) DavException(org.apache.jackrabbit.webdav.DavException) JcrDavException(org.apache.jackrabbit.webdav.jcr.JcrDavException) Element(org.w3c.dom.Element) NodeType(javax.jcr.nodetype.NodeType) ArrayList(java.util.ArrayList) NodeTypeIteratorAdapter(org.apache.jackrabbit.commons.iterator.NodeTypeIteratorAdapter)

Example 55 with NodeTypeManager

use of javax.jcr.nodetype.NodeTypeManager in project jackrabbit by apache.

the class NodeMixinUtil method getNonExistingMixinName.

/**
     * @return a string that is not the name of a mixin type
     */
public static String getNonExistingMixinName(Session session) throws RepositoryException {
    NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
    NodeTypeIterator mixins = manager.getMixinNodeTypes();
    StringBuffer s = new StringBuffer("X");
    while (mixins.hasNext()) {
        s.append(mixins.nextNodeType().getName());
    }
    return s.toString().replaceAll(":", "");
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator)

Aggregations

NodeTypeManager (javax.jcr.nodetype.NodeTypeManager)93 NodeType (javax.jcr.nodetype.NodeType)41 Node (javax.jcr.Node)32 Session (javax.jcr.Session)30 NodeTypeTemplate (javax.jcr.nodetype.NodeTypeTemplate)29 NodeTypeIterator (javax.jcr.nodetype.NodeTypeIterator)27 Test (org.junit.Test)24 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)14 PropertyDefinition (javax.jcr.nodetype.PropertyDefinition)14 RepositoryException (javax.jcr.RepositoryException)13 ByteArrayInputStream (java.io.ByteArrayInputStream)12 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)11 ArrayList (java.util.ArrayList)8 Workspace (javax.jcr.Workspace)8 NoSuchNodeTypeException (javax.jcr.nodetype.NoSuchNodeTypeException)8 NodeDefinition (javax.jcr.nodetype.NodeDefinition)8 PropertyDefinitionTemplate (javax.jcr.nodetype.PropertyDefinitionTemplate)8 NodeDefinitionTemplate (javax.jcr.nodetype.NodeDefinitionTemplate)7 AbstractRepositoryTest (org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)7 InputStream (java.io.InputStream)6