Search in sources :

Example 11 with NamespaceRegistry

use of javax.jcr.NamespaceRegistry in project jackrabbit-oak by apache.

the class RepositoryTest method registryRemappedNamespace.

@Test
public void registryRemappedNamespace() throws RepositoryException {
    NamespaceRegistry nsReg = getAdminSession().getWorkspace().getNamespaceRegistry();
    nsReg.registerNamespace("foo", "file:///foo");
    getAdminSession().getRootNode().addNode("foo:test");
    getAdminSession().save();
    try {
        nsReg.registerNamespace("bar", "file:///foo");
        fail("Remapping namespace through NamespaceRegistry must not be allowed");
    } catch (NamespaceException e) {
    // expected
    } finally {
        getAdminSession().getRootNode().getNode("foo:test").remove();
        getAdminSession().save();
        nsReg.unregisterNamespace("foo");
    }
}
Also used : NamespaceRegistry(javax.jcr.NamespaceRegistry) NamespaceException(javax.jcr.NamespaceException) Test(org.junit.Test)

Example 12 with NamespaceRegistry

use of javax.jcr.NamespaceRegistry in project sling by apache.

the class RegisterNodetypesTest method NSregistered.

@Test
public void NSregistered() throws Exception {
    final NamespaceRegistry ns = U.getAdminSession().getWorkspace().getNamespaceRegistry();
    assertEquals(NS_URI, ns.getURI(NS_PREFIX));
}
Also used : NamespaceRegistry(javax.jcr.NamespaceRegistry) Test(org.junit.Test)

Example 13 with NamespaceRegistry

use of javax.jcr.NamespaceRegistry in project sling by apache.

the class NodeTypeDefinitionScanner method registerNodeTypes.

/**
     * Registers node types found in classpath in JCR repository.
     * @param session Session
     * @param nodeTypeResources List of classpath resource URLs pointing to node type definitions
     */
private void registerNodeTypes(Session session, List<String> nodeTypeResources) throws RepositoryException {
    ClassLoader classLoader = getClass().getClassLoader();
    Workspace workspace = session.getWorkspace();
    NodeTypeManager nodeTypeManager = workspace.getNodeTypeManager();
    NamespaceRegistry namespaceRegistry = workspace.getNamespaceRegistry();
    ValueFactory valueFactory = session.getValueFactory();
    // try registering node types multiple times because the exact order is not known
    int iteration = 0;
    List<String> remainingNodeTypeResources = new ArrayList<String>(nodeTypeResources);
    while (!remainingNodeTypeResources.isEmpty()) {
        registerNodeTypesAndRemoveSucceeds(remainingNodeTypeResources, classLoader, nodeTypeManager, namespaceRegistry, valueFactory, false);
        iteration++;
        if (iteration >= MAX_ITERATIONS) {
            break;
        }
    }
    if (!remainingNodeTypeResources.isEmpty()) {
        registerNodeTypesAndRemoveSucceeds(remainingNodeTypeResources, classLoader, nodeTypeManager, namespaceRegistry, valueFactory, true);
    }
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NamespaceRegistry(javax.jcr.NamespaceRegistry) ArrayList(java.util.ArrayList) ValueFactory(javax.jcr.ValueFactory) Workspace(javax.jcr.Workspace)

Example 14 with NamespaceRegistry

use of javax.jcr.NamespaceRegistry 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 15 with NamespaceRegistry

use of javax.jcr.NamespaceRegistry in project jackrabbit by apache.

the class WorkspaceResourceImpl method initProperties.

@Override
protected void initProperties() {
    super.initProperties();
    try {
        // init workspace specific properties
        NamespaceRegistry nsReg = getRepositorySession().getWorkspace().getNamespaceRegistry();
        DavProperty<?> namespacesProp = new NamespacesProperty(nsReg);
        properties.add(namespacesProp);
    } catch (RepositoryException e) {
        log.error("Failed to access NamespaceRegistry: " + e.getMessage());
    }
}
Also used : NamespaceRegistry(javax.jcr.NamespaceRegistry) RepositoryException(javax.jcr.RepositoryException) NamespacesProperty(org.apache.jackrabbit.webdav.jcr.property.NamespacesProperty)

Aggregations

NamespaceRegistry (javax.jcr.NamespaceRegistry)34 Session (javax.jcr.Session)14 NamespaceException (javax.jcr.NamespaceException)6 Node (javax.jcr.Node)6 RepositoryException (javax.jcr.RepositoryException)6 NodeTypeManager (javax.jcr.nodetype.NodeTypeManager)6 NodeTypeTemplate (javax.jcr.nodetype.NodeTypeTemplate)6 Test (org.junit.Test)6 InputStream (java.io.InputStream)4 JackrabbitWorkspace (org.apache.jackrabbit.api.JackrabbitWorkspace)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 BigDecimal (java.math.BigDecimal)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Binary (javax.jcr.Binary)3 Workspace (javax.jcr.Workspace)3 Value (javax.jcr.Value)2 ValueFactory (javax.jcr.ValueFactory)2 PropertyDefinitionTemplate (javax.jcr.nodetype.PropertyDefinitionTemplate)2 JackrabbitSession (org.apache.jackrabbit.api.JackrabbitSession)2