Search in sources :

Example 1 with DefinitionBuilderFactory

use of org.apache.jackrabbit.commons.cnd.DefinitionBuilderFactory 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)

Aggregations

StringReader (java.io.StringReader)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 NamespaceRegistry (javax.jcr.NamespaceRegistry)1 RepositoryException (javax.jcr.RepositoryException)1 Session (javax.jcr.Session)1 NodeTypeManager (javax.jcr.nodetype.NodeTypeManager)1 NodeTypeTemplate (javax.jcr.nodetype.NodeTypeTemplate)1 CompactNodeTypeDefReader (org.apache.jackrabbit.commons.cnd.CompactNodeTypeDefReader)1 DefinitionBuilderFactory (org.apache.jackrabbit.commons.cnd.DefinitionBuilderFactory)1 ParseException (org.apache.jackrabbit.commons.cnd.ParseException)1 TemplateBuilderFactory (org.apache.jackrabbit.commons.cnd.TemplateBuilderFactory)1 DavException (org.apache.jackrabbit.webdav.DavException)1 NamespacesProperty (org.apache.jackrabbit.webdav.jcr.property.NamespacesProperty)1 Element (org.w3c.dom.Element)1