Search in sources :

Example 1 with PrivilegeDefinitionWriter

use of org.apache.jackrabbit.spi.commons.privilege.PrivilegeDefinitionWriter in project jackrabbit by apache.

the class AbstractRecord method writePrivilegeDef.

/**
     * {@inheritDoc}
     */
public void writePrivilegeDef(PrivilegeDefinition privilegeDefinition) throws JournalException {
    try {
        Map<String, String> nsMapping = new HashMap<String, String>();
        String uri = privilegeDefinition.getName().getNamespaceURI();
        nsMapping.put(nsResolver.getPrefix(uri), uri);
        for (Name n : privilegeDefinition.getDeclaredAggregateNames()) {
            nsMapping.put(nsResolver.getPrefix(n.getNamespaceURI()), n.getNamespaceURI());
        }
        StringWriter sw = new StringWriter();
        PrivilegeDefinitionWriter writer = new PrivilegeDefinitionWriter("text/xml");
        writer.writeDefinitions(sw, new PrivilegeDefinition[] { privilegeDefinition }, nsMapping);
        sw.close();
        writeString(sw.toString());
    } catch (IOException e) {
        String msg = "I/O error while writing privilege definition.";
        throw new JournalException(msg, e);
    } catch (NamespaceException e) {
        String msg = "NamespaceException error while writing privilege definition.";
        throw new JournalException(msg, e);
    }
}
Also used : StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) NamespaceException(javax.jcr.NamespaceException) PrivilegeDefinitionWriter(org.apache.jackrabbit.spi.commons.privilege.PrivilegeDefinitionWriter) IOException(java.io.IOException) Name(org.apache.jackrabbit.spi.Name)

Example 2 with PrivilegeDefinitionWriter

use of org.apache.jackrabbit.spi.commons.privilege.PrivilegeDefinitionWriter in project jackrabbit by apache.

the class CustomPrivilegeTest method testCustomEquivalentDefinitions.

public void testCustomEquivalentDefinitions() throws RepositoryException, FileSystemException, IOException {
    // setup the custom privilege file with cyclic references
    FileSystem fs = ((RepositoryImpl) superuser.getRepository()).getConfig().getFileSystem();
    FileSystemResource resource = new FileSystemResource(fs, "/privileges/custom_privileges.xml");
    if (!resource.exists()) {
        resource.makeParentDirs();
    }
    NameFactory nf = NameFactoryImpl.getInstance();
    Name test = nf.create(Name.NS_DEFAULT_URI, "test");
    Name test2 = nf.create(Name.NS_DEFAULT_URI, "test2");
    Name test3 = nf.create(Name.NS_DEFAULT_URI, "test3");
    Name test4 = nf.create(Name.NS_DEFAULT_URI, "test4");
    Name test5 = nf.create(Name.NS_DEFAULT_URI, "test5");
    Name test6 = nf.create(Name.NS_DEFAULT_URI, "test6");
    OutputStream out = resource.getOutputStream();
    try {
        List<PrivilegeDefinition> defs = new ArrayList<PrivilegeDefinition>();
        defs.add(new PrivilegeDefinitionImpl(test, false, createNameSet(test2, test3)));
        defs.add(new PrivilegeDefinitionImpl(test2, true, Collections.singleton(test4)));
        defs.add(new PrivilegeDefinitionImpl(test3, true, Collections.singleton(test5)));
        defs.add(new PrivilegeDefinitionImpl(test4, true, Collections.<Name>emptySet()));
        defs.add(new PrivilegeDefinitionImpl(test5, true, Collections.<Name>emptySet()));
        // the equivalent definition to 'test'
        defs.add(new PrivilegeDefinitionImpl(test6, false, createNameSet(test2, test5)));
        PrivilegeDefinitionWriter pdw = new PrivilegeDefinitionWriter("text/xml");
        pdw.writeDefinitions(out, defs.toArray(new PrivilegeDefinition[defs.size()]), Collections.<String, String>emptyMap());
        new PrivilegeRegistry(superuser.getWorkspace().getNamespaceRegistry(), fs);
        fail("Equivalent definitions must be detected upon registry startup.");
    } catch (RepositoryException e) {
    // success
    } finally {
        out.close();
        fs.deleteFolder("/privileges");
    }
}
Also used : PrivilegeDefinitionImpl(org.apache.jackrabbit.spi.commons.privilege.PrivilegeDefinitionImpl) FileSystem(org.apache.jackrabbit.core.fs.FileSystem) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) PrivilegeDefinition(org.apache.jackrabbit.spi.PrivilegeDefinition) RepositoryException(javax.jcr.RepositoryException) FileSystemResource(org.apache.jackrabbit.core.fs.FileSystemResource) PrivilegeDefinitionWriter(org.apache.jackrabbit.spi.commons.privilege.PrivilegeDefinitionWriter) NameFactory(org.apache.jackrabbit.spi.NameFactory) Name(org.apache.jackrabbit.spi.Name)

Example 3 with PrivilegeDefinitionWriter

use of org.apache.jackrabbit.spi.commons.privilege.PrivilegeDefinitionWriter in project jackrabbit by apache.

the class CustomPrivilegeTest method testCustomDefinitionsWithCyclicReferences.

public void testCustomDefinitionsWithCyclicReferences() throws RepositoryException, FileSystemException, IOException {
    // setup the custom privilege file with cyclic references
    FileSystem fs = ((RepositoryImpl) superuser.getRepository()).getConfig().getFileSystem();
    FileSystemResource resource = new FileSystemResource(fs, "/privileges/custom_privileges.xml");
    if (!resource.exists()) {
        resource.makeParentDirs();
    }
    NameFactory nf = NameFactoryImpl.getInstance();
    Name test = nf.create(Name.NS_DEFAULT_URI, "test");
    Name test2 = nf.create(Name.NS_DEFAULT_URI, "test2");
    Name test3 = nf.create(Name.NS_DEFAULT_URI, "test3");
    Name test4 = nf.create(Name.NS_DEFAULT_URI, "test4");
    Name test5 = nf.create(Name.NS_DEFAULT_URI, "test5");
    OutputStream out = resource.getOutputStream();
    try {
        List<PrivilegeDefinition> defs = new ArrayList<PrivilegeDefinition>();
        defs.add(new PrivilegeDefinitionImpl(test, false, Collections.singleton(test2)));
        defs.add(new PrivilegeDefinitionImpl(test4, true, Collections.singleton(test5)));
        defs.add(new PrivilegeDefinitionImpl(test5, false, Collections.singleton(test3)));
        defs.add(new PrivilegeDefinitionImpl(test3, false, Collections.singleton(test)));
        defs.add(new PrivilegeDefinitionImpl(test2, false, Collections.singleton(test4)));
        PrivilegeDefinitionWriter pdw = new PrivilegeDefinitionWriter("text/xml");
        pdw.writeDefinitions(out, defs.toArray(new PrivilegeDefinition[defs.size()]), Collections.<String, String>emptyMap());
        new PrivilegeRegistry(superuser.getWorkspace().getNamespaceRegistry(), fs);
        fail("Cyclic definitions must be detected upon registry startup.");
    } catch (RepositoryException e) {
    // success
    } finally {
        out.close();
        fs.deleteFolder("/privileges");
    }
}
Also used : PrivilegeDefinitionImpl(org.apache.jackrabbit.spi.commons.privilege.PrivilegeDefinitionImpl) FileSystem(org.apache.jackrabbit.core.fs.FileSystem) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) PrivilegeDefinition(org.apache.jackrabbit.spi.PrivilegeDefinition) RepositoryException(javax.jcr.RepositoryException) FileSystemResource(org.apache.jackrabbit.core.fs.FileSystemResource) PrivilegeDefinitionWriter(org.apache.jackrabbit.spi.commons.privilege.PrivilegeDefinitionWriter) NameFactory(org.apache.jackrabbit.spi.NameFactory) Name(org.apache.jackrabbit.spi.Name)

Aggregations

Name (org.apache.jackrabbit.spi.Name)3 PrivilegeDefinitionWriter (org.apache.jackrabbit.spi.commons.privilege.PrivilegeDefinitionWriter)3 OutputStream (java.io.OutputStream)2 ArrayList (java.util.ArrayList)2 RepositoryException (javax.jcr.RepositoryException)2 FileSystem (org.apache.jackrabbit.core.fs.FileSystem)2 FileSystemResource (org.apache.jackrabbit.core.fs.FileSystemResource)2 NameFactory (org.apache.jackrabbit.spi.NameFactory)2 PrivilegeDefinition (org.apache.jackrabbit.spi.PrivilegeDefinition)2 PrivilegeDefinitionImpl (org.apache.jackrabbit.spi.commons.privilege.PrivilegeDefinitionImpl)2 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 HashMap (java.util.HashMap)1 NamespaceException (javax.jcr.NamespaceException)1