Java Code Examples for org.apache.directory.server.core.api.CoreSession#add()

The following examples show how to use org.apache.directory.server.core.api.CoreSession#add() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LdapService.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Import all of the entries from the provided LDIF stream.
 *
 * Note: The whole stream is read
 *
 * @param ldif - Stream containing the LDIF.
 * @return This Builder for subsequent changes.
 */
public Builder importLdif(final InputStream ldif) throws Exception {
    assertNotStarted();
    if (directoryService == null) {
        throw new IllegalStateException("The Directory service has not been created.");
    }
    CoreSession adminSession = directoryService.getAdminSession();
    SchemaManager schemaManager = directoryService.getSchemaManager();

    LdifReader ldifReader = new LdifReader(ldif);
    for (LdifEntry ldifEntry : ldifReader) {
        adminSession.add(new DefaultEntry(schemaManager, ldifEntry.getEntry()));
    }
    ldifReader.close();
    ldif.close();

    return this;
}
 
Example 2
Source File: LdapTestSuite.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static void processLdif(final SchemaManager schemaManager, final CoreSession adminSession, final String ldifName) throws LdapException, IOException {
    InputStream ldifInput = LdapTestSuite.class.getResourceAsStream(ldifName);
    LdifReader ldifReader = new LdifReader(ldifInput);
    for (LdifEntry ldifEntry : ldifReader) {
        adminSession.add(new DefaultEntry(schemaManager, ldifEntry.getEntry()));
    }
    ldifReader.close();
    ldifInput.close();
}
 
Example 3
Source File: EmbeddedLdapServer.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
protected void createRootEntry() throws LdapException {
    Entry entry = getDirectoryService().newEntry(getDirectoryService().getDnFactory().create(getBaseStructure()));
    entry.add("objectClass", "top", "domain", "extensibleObject");
    entry.add("dc", getBasePartitionName());
    CoreSession session = getDirectoryService().getAdminSession();
    try {
        session.add(entry);
    } finally {
        session.unbind();
    }
}
 
Example 4
Source File: KerberosKDCUtil.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
private static void processLdif(final SchemaManager schemaManager, final CoreSession adminSession, final String ldifName,
        final Map<String, String> mappings) throws Exception {
    InputStream resourceInput = KerberosKDCUtil.class.getResourceAsStream("/ldif/" + ldifName);
    ByteArrayOutputStream baos = new ByteArrayOutputStream(resourceInput.available());
    int current;
    while ((current = resourceInput.read()) != -1) {
        if (current == '$') {
            // Enter String replacement mode.
            int second = resourceInput.read();
            if (second == '{') {
                ByteArrayOutputStream substitute = new ByteArrayOutputStream();
                while ((current = resourceInput.read()) != -1 && current != '}') {
                    substitute.write(current);
                }
                if (current == -1) {
                    baos.write(current);
                    baos.write(second);
                    baos.write(substitute.toByteArray()); // Terminator never found.
                }
                String toReplace = new String(substitute.toByteArray(), StandardCharsets.UTF_8);
                if (mappings.containsKey(toReplace)) {
                    baos.write(mappings.get(toReplace).getBytes());
                } else {
                    throw new IllegalArgumentException(String.format("No mapping found for '%s'", toReplace));
                }
            } else {
                baos.write(current);
                baos.write(second);
            }
        } else {
            baos.write(current);
        }
    }

    ByteArrayInputStream ldifInput = new ByteArrayInputStream(baos.toByteArray());
    LdifReader ldifReader = new LdifReader(ldifInput);
    for (LdifEntry ldifEntry : ldifReader) {
        adminSession.add(new DefaultEntry(schemaManager, ldifEntry.getEntry()));
    }
    ldifReader.close();
    ldifInput.close();
}
 
Example 5
Source File: ApacheDirectoryServer.java    From light-oauth2 with Apache License 2.0 4 votes vote down vote up
private static void processLdif(final SchemaManager schemaManager, final CoreSession adminSession, final String ldifName,
                                final Map<String, String> mappings) throws Exception {
    InputStream resourceInput = KerberosKDCUtil.class.getResourceAsStream("/ldif/" + ldifName);
    ByteArrayOutputStream baos = new ByteArrayOutputStream(resourceInput.available());
    int current;
    while ((current = resourceInput.read()) != -1) {
        if (current == '$') {
            // Enter String replacement mode.
            int second = resourceInput.read();
            if (second == '{') {
                ByteArrayOutputStream substitute = new ByteArrayOutputStream();
                while ((current = resourceInput.read()) != -1 && current != '}') {
                    substitute.write(current);
                }
                if (current == -1) {
                    baos.write(current);
                    baos.write(second);
                    baos.write(substitute.toByteArray()); // Terminator never found.
                }
                String toReplace = new String(substitute.toByteArray(), UTF_8);
                if (mappings.containsKey(toReplace)) {
                    baos.write(mappings.get(toReplace).getBytes(UTF_8));
                } else {
                    throw new IllegalArgumentException(String.format("No mapping found for '%s'", toReplace));
                }
            } else {
                baos.write(current);
                baos.write(second);
            }
        } else {
            baos.write(current);
        }
    }

    ByteArrayInputStream ldifInput = new ByteArrayInputStream(baos.toByteArray());
    LdifReader ldifReader = new LdifReader(ldifInput);
    for (LdifEntry ldifEntry : ldifReader) {
        adminSession.add(new DefaultEntry(schemaManager, ldifEntry.getEntry()));
    }
    ldifReader.close();
    ldifInput.close();
}
 
Example 6
Source File: ApacheDirectoryServer.java    From light-oauth2 with Apache License 2.0 4 votes vote down vote up
private static void processLdif(final SchemaManager schemaManager, final CoreSession adminSession, final String ldifName,
                                final Map<String, String> mappings) throws Exception {
    InputStream resourceInput = KerberosKDCUtil.class.getResourceAsStream("/ldif/" + ldifName);
    ByteArrayOutputStream baos = new ByteArrayOutputStream(resourceInput.available());
    int current;
    while ((current = resourceInput.read()) != -1) {
        if (current == '$') {
            // Enter String replacement mode.
            int second = resourceInput.read();
            if (second == '{') {
                ByteArrayOutputStream substitute = new ByteArrayOutputStream();
                while ((current = resourceInput.read()) != -1 && current != '}') {
                    substitute.write(current);
                }
                if (current == -1) {
                    baos.write(current);
                    baos.write(second);
                    baos.write(substitute.toByteArray()); // Terminator never found.
                }
                String toReplace = new String(substitute.toByteArray(), StandardCharsets.UTF_8);
                if (mappings.containsKey(toReplace)) {
                    baos.write(mappings.get(toReplace).getBytes());
                } else {
                    throw new IllegalArgumentException(String.format("No mapping found for '%s'", toReplace));
                }
            } else {
                baos.write(current);
                baos.write(second);
            }
        } else {
            baos.write(current);
        }
    }

    ByteArrayInputStream ldifInput = new ByteArrayInputStream(baos.toByteArray());
    LdifReader ldifReader = new LdifReader(ldifInput);
    for (LdifEntry ldifEntry : ldifReader) {
        adminSession.add(new DefaultEntry(schemaManager, ldifEntry.getEntry()));
    }
    ldifReader.close();
    ldifInput.close();
}