Search in sources :

Example 21 with NamespaceRegistry

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

the class RepositorySidegradeTest method createSourceContent.

@SuppressWarnings("unchecked")
protected NodeStore createSourceContent() throws Exception {
    NodeStore source = SegmentNodeStoreBuilders.builder(new MemoryStore()).build();
    Repository repository = new Jcr(new Oak(source)).createRepository();
    Session session = repository.login(CREDENTIALS);
    try {
        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();
        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();
        }
        return source;
    } finally {
        session.logout();
    }
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Node(javax.jcr.Node) JackrabbitWorkspace(org.apache.jackrabbit.api.JackrabbitWorkspace) BigDecimal(java.math.BigDecimal) MemoryStore(org.apache.jackrabbit.oak.segment.memory.MemoryStore) Repository(javax.jcr.Repository) NodeStore(org.apache.jackrabbit.oak.spi.state.NodeStore) NamespaceRegistry(javax.jcr.NamespaceRegistry) NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) PropertyDefinitionTemplate(javax.jcr.nodetype.PropertyDefinitionTemplate) ByteArrayInputStream(java.io.ByteArrayInputStream) Jcr(org.apache.jackrabbit.oak.jcr.Jcr) Oak(org.apache.jackrabbit.oak.Oak) Value(javax.jcr.Value) Binary(javax.jcr.Binary) Session(javax.jcr.Session) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession)

Example 22 with NamespaceRegistry

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

the class L5_SpecialPermissionsTest method testRepositoryLevelPrivileges.

public void testRepositoryLevelPrivileges() throws RepositoryException {
    // EXERCISE : setup the permissions such that test-session can register a
    // - new namespace|node type|privilege (note: workspace management not yet supported)
    // EXERCISE: refactory the test to verify that granting these privs at 'childPath' doesn;t have the desired effect.
    JackrabbitSession s = (JackrabbitSession) createTestSession();
    NamespaceRegistry nsRegistry = s.getWorkspace().getNamespaceRegistry();
    nsRegistry.registerNamespace("jr", "http://jackrabbit.apache.org");
}
Also used : NamespaceRegistry(javax.jcr.NamespaceRegistry) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession)

Example 23 with NamespaceRegistry

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

the class ReadWriteNamespaceRegistryTest method testMappings.

@Test
public void testMappings() throws Exception {
    final ContentSession session = createContentSession();
    final Root root = session.getLatestRoot();
    NamespaceRegistry r = new ReadWriteNamespaceRegistry(root) {

        @Override
        protected Root getWriteRoot() {
            return session.getLatestRoot();
        }

        @Override
        protected void refresh() {
            root.refresh();
        }
    };
    assertEquals("", r.getURI(""));
    assertEquals("http://www.jcp.org/jcr/1.0", r.getURI("jcr"));
    assertEquals("http://www.jcp.org/jcr/nt/1.0", r.getURI("nt"));
    assertEquals("http://www.jcp.org/jcr/mix/1.0", r.getURI("mix"));
    assertEquals("http://www.w3.org/XML/1998/namespace", r.getURI("xml"));
    assertEquals("", r.getPrefix(""));
    assertEquals("jcr", r.getPrefix("http://www.jcp.org/jcr/1.0"));
    assertEquals("nt", r.getPrefix("http://www.jcp.org/jcr/nt/1.0"));
    assertEquals("mix", r.getPrefix("http://www.jcp.org/jcr/mix/1.0"));
    assertEquals("xml", r.getPrefix("http://www.w3.org/XML/1998/namespace"));
    r.registerNamespace("p", "n");
    assertEquals(r.getURI("p"), "n");
    assertEquals(r.getPrefix("n"), "p");
    r.registerNamespace("p2", "n2");
    assertEquals(r.getURI("p2"), "n2");
    assertEquals(r.getPrefix("n2"), "p2");
}
Also used : Root(org.apache.jackrabbit.oak.api.Root) NamespaceRegistry(javax.jcr.NamespaceRegistry) ContentSession(org.apache.jackrabbit.oak.api.ContentSession) Test(org.junit.Test) OakBaseTest(org.apache.jackrabbit.oak.OakBaseTest)

Example 24 with NamespaceRegistry

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

the class RepositoryTest method testNamespaceRegistry.

@Test
public void testNamespaceRegistry() throws RepositoryException {
    NamespaceRegistry nsReg = getAdminSession().getWorkspace().getNamespaceRegistry();
    assertFalse(asList(nsReg.getPrefixes()).contains("foo"));
    assertFalse(asList(nsReg.getURIs()).contains("file:///foo"));
    nsReg.registerNamespace("foo", "file:///foo");
    assertTrue(asList(nsReg.getPrefixes()).contains("foo"));
    assertTrue(asList(nsReg.getURIs()).contains("file:///foo"));
    nsReg.unregisterNamespace("foo");
    assertFalse(asList(nsReg.getPrefixes()).contains("foo"));
    assertFalse(asList(nsReg.getURIs()).contains("file:///foo"));
}
Also used : NamespaceRegistry(javax.jcr.NamespaceRegistry) Test(org.junit.Test)

Example 25 with NamespaceRegistry

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

the class NamespaceRegistryTest method testUnregisteredNamespaceVisibility.

/**
     * Test if unregistering a namespace is propagated to all other sessions.
     *
     * @throws RepositoryException
     */
public void testUnregisteredNamespaceVisibility() throws RepositoryException, NotExecutableException {
    String prefix = getUnusedPrefix();
    String uri = getUnusedURI();
    Session otherSession = getHelper().getReadOnlySession();
    try {
        NamespaceRegistry other = otherSession.getWorkspace().getNamespaceRegistry();
        nsRegistry.registerNamespace(prefix, uri);
        try {
            nsRegistry.unregisterNamespace(prefix);
        } catch (NamespaceException e) {
            throw new NotExecutableException("Repository does not support unregistration of namespaces.");
        }
        String otherUri = other.getURI(prefix);
        String otherPrefix = other.getPrefix(uri);
        assertTrue("Namespace registered must be immediately visible to any other session.", uri.equals(otherUri) && prefix.equals(otherPrefix));
    } finally {
        otherSession.logout();
    }
}
Also used : NamespaceRegistry(javax.jcr.NamespaceRegistry) NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) NamespaceException(javax.jcr.NamespaceException) 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