Search in sources :

Example 6 with NamespaceRegistry

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

the class RepositoryTest method sessionRemappedNamespace.

@Test
public void sessionRemappedNamespace() throws RepositoryException {
    NamespaceRegistry nsReg = getAdminSession().getWorkspace().getNamespaceRegistry();
    nsReg.registerNamespace("foo", "file:///foo");
    getAdminSession().getRootNode().addNode("foo:test");
    getAdminSession().save();
    Session s = createAdminSession();
    s.setNamespacePrefix("bar", "file:///foo");
    assertTrue(s.getRootNode().hasNode("bar:test"));
    Node n = s.getRootNode().getNode("bar:test");
    assertEquals("bar:test", n.getName());
    s.logout();
    getAdminSession().getRootNode().getNode("foo:test").remove();
    getAdminSession().save();
    nsReg.unregisterNamespace("foo");
}
Also used : NamespaceRegistry(javax.jcr.NamespaceRegistry) JackrabbitNode(org.apache.jackrabbit.api.JackrabbitNode) Node(javax.jcr.Node) Session(javax.jcr.Session) Test(org.junit.Test)

Example 7 with NamespaceRegistry

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

the class RepositoryUpgradeTest method createSourceContent.

@Override
@SuppressWarnings("unchecked")
protected void createSourceContent(Session session) throws Exception {
    JackrabbitWorkspace workspace = (JackrabbitWorkspace) session.getWorkspace();
    NamespaceRegistry registry = workspace.getNamespaceRegistry();
    registry.registerNamespace("test", "http://www.example.org/");
    NodeTypeManager nodeTypeManager = workspace.getNodeTypeManager();
    NodeTypeTemplate template = nodeTypeManager.createNodeTypeTemplate();
    template.setName("test:unstructured");
    template.setDeclaredSuperTypeNames(new String[] { "nt:unstructured" });
    PropertyDefinitionTemplate pDef1 = nodeTypeManager.createPropertyDefinitionTemplate();
    pDef1.setName("defaultString");
    pDef1.setRequiredType(PropertyType.STRING);
    Value stringValue = session.getValueFactory().createValue("stringValue");
    pDef1.setDefaultValues(new Value[] { stringValue });
    template.getPropertyDefinitionTemplates().add(pDef1);
    PropertyDefinitionTemplate pDef2 = nodeTypeManager.createPropertyDefinitionTemplate();
    pDef2.setName("defaultPath");
    pDef2.setRequiredType(PropertyType.PATH);
    Value pathValue = session.getValueFactory().createValue("/jcr:path/nt:value", PropertyType.PATH);
    pDef2.setDefaultValues(new Value[] { pathValue });
    template.getPropertyDefinitionTemplates().add(pDef2);
    nodeTypeManager.registerNodeType(template, false);
    template = nodeTypeManager.createNodeTypeTemplate();
    template.setName("test:referenceable");
    template.setDeclaredSuperTypeNames(new String[] { "nt:unstructured", "mix:referenceable" });
    nodeTypeManager.registerNodeType(template, false);
    Node root = session.getRootNode();
    Node referenceable = root.addNode("referenceable", "test:unstructured");
    referenceable.addMixin(NodeType.MIX_REFERENCEABLE);
    Node versionable = root.addNode("versionable", NT_UNSTRUCTURED);
    versionable.addMixin(MIX_VERSIONABLE);
    Node child = versionable.addNode("child", "test:referenceable");
    child.addNode("child2", NT_UNSTRUCTURED);
    session.save();
    Node sns = root.addNode("sns");
    sns.addNode("sibling");
    sns.addNode("sibling");
    sns.addNode("sibling");
    session.getWorkspace().getVersionManager().checkin("/versionable");
    Node properties = root.addNode("properties", "test:unstructured");
    properties.setProperty("boolean", true);
    Binary binary = session.getValueFactory().createBinary(new ByteArrayInputStream(BINARY));
    try {
        properties.setProperty("binary", binary);
    } finally {
        binary.dispose();
    }
    properties.setProperty("date", DATE);
    properties.setProperty("decimal", new BigDecimal(123));
    properties.setProperty("double", Math.PI);
    properties.setProperty("long", 9876543210L);
    properties.setProperty("reference", referenceable);
    properties.setProperty("weak_reference", session.getValueFactory().createValue(referenceable, true));
    properties.setProperty("mv_reference", new Value[] { session.getValueFactory().createValue(versionable, false) });
    properties.setProperty("mv_weak_reference", new Value[] { session.getValueFactory().createValue(versionable, true) });
    properties.setProperty("string", "test");
    properties.setProperty("multiple", "a,b,c".split(","));
    session.save();
    binary = properties.getProperty("binary").getBinary();
    try {
        InputStream stream = binary.getStream();
        try {
            for (byte aBINARY : BINARY) {
                assertEquals(aBINARY, (byte) stream.read());
            }
            assertEquals(-1, stream.read());
        } finally {
            stream.close();
        }
    } finally {
        binary.dispose();
    }
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NamespaceRegistry(javax.jcr.NamespaceRegistry) NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) PropertyDefinitionTemplate(javax.jcr.nodetype.PropertyDefinitionTemplate) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Node(javax.jcr.Node) Value(javax.jcr.Value) JackrabbitWorkspace(org.apache.jackrabbit.api.JackrabbitWorkspace) Binary(javax.jcr.Binary) BigDecimal(java.math.BigDecimal)

Example 8 with NamespaceRegistry

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

the class RepeatedRepositoryUpgradeTest method registerCustomPrivileges.

private void registerCustomPrivileges(Session session) throws RepositoryException {
    final JackrabbitWorkspace workspace = (JackrabbitWorkspace) session.getWorkspace();
    final NamespaceRegistry registry = workspace.getNamespaceRegistry();
    registry.registerNamespace("test", "http://www.example.org/");
    final PrivilegeManager privilegeManager = workspace.getPrivilegeManager();
    privilegeManager.registerPrivilege("test:privilege", false, null);
    privilegeManager.registerPrivilege("test:aggregate", false, new String[] { "jcr:read", "test:privilege" });
}
Also used : NamespaceRegistry(javax.jcr.NamespaceRegistry) PrivilegeManager(org.apache.jackrabbit.api.security.authorization.PrivilegeManager) JackrabbitWorkspace(org.apache.jackrabbit.api.JackrabbitWorkspace)

Example 9 with NamespaceRegistry

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

the class CndImporter method registerNodeTypes.

/**
     * Registers nodetypes in <code>cnd</code> format.
     * @param cnd  a reader to the cnd. The reader is closed on return.
     * @param systemId  a informative id of the given cnd input.
     * @param nodeTypeManager  the {@link NodeTypeManager} used for creating and registering the
     *     {@link NodeTypeTemplate}s, {@link NodeDefinitionTemplate}s and {@link PropertyDefinitionTemplate}s
     *     defined in the cnd.
     * @param namespaceRegistry  the {@link NamespaceRegistry} used for registering namespaces defined in
     *     the cnd.
     * @param valueFactory  the {@link ValueFactory} used to create
     *     {@link PropertyDefinitionTemplate#setDefaultValues(javax.jcr.Value[]) default value(s)}.
     * @param reregisterExisting  <code>true</code> if existing node types should be re-registered
     *     with those present in the cnd. <code>false</code> otherwise.
     * @return  the registered node types
     *
     * @throws ParseException  if the cnd cannot be parsed
     * @throws InvalidNodeTypeDefinitionException  if a <code>NodeTypeDefinition</code> is invalid.
     * @throws NodeTypeExistsException  if <code>reregisterExisting</code> is <code>false</code> and a
     *     <code>NodeTypeDefinition</code> specifies a node type name that is already registered.
     * @throws UnsupportedRepositoryOperationException  if the <code>NodeTypeManager</code> does not
     *     support node type registration.
     * @throws IOException  if closing the cnd reader fails
     * @throws RepositoryException  if another error occurs.
     */
public static NodeType[] registerNodeTypes(Reader cnd, String systemId, NodeTypeManager nodeTypeManager, NamespaceRegistry namespaceRegistry, ValueFactory valueFactory, boolean reregisterExisting) throws ParseException, InvalidNodeTypeDefinitionException, NodeTypeExistsException, UnsupportedRepositoryOperationException, RepositoryException, IOException {
    try {
        DefinitionBuilderFactory<NodeTypeTemplate, NamespaceRegistry> factory = new TemplateBuilderFactory(nodeTypeManager, valueFactory, namespaceRegistry);
        CompactNodeTypeDefReader<NodeTypeTemplate, NamespaceRegistry> cndReader = new CompactNodeTypeDefReader<NodeTypeTemplate, NamespaceRegistry>(cnd, systemId, factory);
        Map<String, NodeTypeTemplate> templates = new HashMap<String, NodeTypeTemplate>();
        for (NodeTypeTemplate template : cndReader.getNodeTypeDefinitions()) {
            templates.put(template.getName(), template);
        }
        List<NodeTypeTemplate> toRegister = new ArrayList<NodeTypeTemplate>(templates.size());
        for (NodeTypeTemplate ntt : templates.values()) {
            if (reregisterExisting || !nodeTypeManager.hasNodeType(ntt.getName())) {
                ensureNtBase(ntt, templates, nodeTypeManager);
                toRegister.add(ntt);
            }
        }
        NodeTypeIterator registered = nodeTypeManager.registerNodeTypes(toRegister.toArray(new NodeTypeTemplate[toRegister.size()]), true);
        return toArray(registered);
    } finally {
        cnd.close();
    }
}
Also used : NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) NamespaceRegistry(javax.jcr.NamespaceRegistry) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator)

Example 10 with NamespaceRegistry

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

the class RepositoryCopierTest method createSourceRepository.

private void createSourceRepository() throws Exception {
    RepositoryImpl repository = RepositoryImpl.create(RepositoryConfig.install(SOURCE));
    try {
        Session session = repository.login(CREDENTIALS);
        try {
            NamespaceRegistry registry = session.getWorkspace().getNamespaceRegistry();
            registry.registerNamespace("test", "http://www.example.org/");
            NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
            NodeTypeTemplate template = manager.createNodeTypeTemplate();
            template.setName("test:unstructured");
            template.setDeclaredSuperTypeNames(new String[] { "nt:unstructured" });
            manager.registerNodeType(template, false);
            Node root = session.getRootNode();
            Node referenceable = root.addNode("referenceable", "test:unstructured");
            referenceable.addMixin(NodeType.MIX_REFERENCEABLE);
            session.save();
            identifier = referenceable.getIdentifier();
            Node properties = root.addNode("properties", "test:unstructured");
            properties.setProperty("boolean", true);
            Binary binary = session.getValueFactory().createBinary(new ByteArrayInputStream(BINARY));
            try {
                properties.setProperty("binary", binary);
            } finally {
                binary.dispose();
            }
            properties.setProperty("date", DATE);
            properties.setProperty("decimal", new BigDecimal(123));
            properties.setProperty("double", Math.PI);
            properties.setProperty("long", 9876543210L);
            properties.setProperty("reference", referenceable);
            properties.setProperty("string", "test");
            properties.setProperty("multiple", "a,b,c".split(","));
            session.save();
            binary = properties.getProperty("binary").getBinary();
            try {
                InputStream stream = binary.getStream();
                try {
                    for (int i = 0; i < BINARY.length; i++) {
                        assertEquals(BINARY[i], (byte) stream.read());
                    }
                    assertEquals(-1, stream.read());
                } finally {
                    stream.close();
                }
            } finally {
                binary.dispose();
            }
        } finally {
            session.logout();
        }
    } finally {
        repository.shutdown();
    }
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NamespaceRegistry(javax.jcr.NamespaceRegistry) NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Node(javax.jcr.Node) Binary(javax.jcr.Binary) BigDecimal(java.math.BigDecimal) Session(javax.jcr.Session)

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