Search in sources :

Example 1 with GenericLdapConfig

use of com.zimbra.cs.ldap.LdapServerConfig.GenericLdapConfig in project zm-mailbox by Zimbra.

the class LdapExample method deleteEntry.

public void deleteEntry() throws Exception {
    String dn = "uid=user1,ou=people,dc=test,dc=com";
    GenericLdapConfig ldapConfig = getLdapConfig();
    ZLdapContext zlc = null;
    try {
        zlc = LdapClient.getContext(ldapConfig, LdapUsage.DELETE);
        zlc.deleteEntry(dn);
    } finally {
        // Note: this is important!!
        LdapClient.closeContext(zlc);
    }
}
Also used : GenericLdapConfig(com.zimbra.cs.ldap.LdapServerConfig.GenericLdapConfig) ZLdapContext(com.zimbra.cs.ldap.ZLdapContext)

Example 2 with GenericLdapConfig

use of com.zimbra.cs.ldap.LdapServerConfig.GenericLdapConfig in project zm-mailbox by Zimbra.

the class LdapExample method search.

public void search() throws Exception {
    String base = "cn=servers,cn=zimbra";
    String filter = "(objectClass=zimbraServer)";
    String[] returnAttrs = new String[] { "objectClass", "cn" };
    ZLdapFilter zFilter = ZLdapFilterFactory.getInstance().fromFilterString(FilterId.ZMCONFIGD, filter);
    ZSearchControls searchControls = ZSearchControls.createSearchControls(ZSearchScope.SEARCH_SCOPE_SUBTREE, ZSearchControls.SIZE_UNLIMITED, returnAttrs);
    GenericLdapConfig ldapConfig = getLdapConfig();
    ZLdapContext zlc = null;
    try {
        zlc = LdapClient.getContext(ldapConfig, LdapUsage.SEARCH);
        ZSearchResultEnumeration ne = zlc.searchDir(base, zFilter, searchControls);
        while (ne.hasMore()) {
            ZSearchResultEntry entry = ne.next();
            String dn = entry.getDN();
            ZAttributes attrs = entry.getAttributes();
            String cn = attrs.getAttrString("cn");
            String[] objectClasses = attrs.getMultiAttrString("objectClass");
            System.out.println("dn = " + dn);
            System.out.println("cn = " + cn);
            for (String objectClass : objectClasses) {
                System.out.println("objetClass = " + objectClass);
            }
        }
        ne.close();
    } catch (LdapSizeLimitExceededException e) {
        e.printStackTrace();
        throw e;
    } finally {
        // Note: this is important!!
        LdapClient.closeContext(zlc);
    }
}
Also used : ZLdapFilter(com.zimbra.cs.ldap.ZLdapFilter) ZSearchControls(com.zimbra.cs.ldap.ZSearchControls) GenericLdapConfig(com.zimbra.cs.ldap.LdapServerConfig.GenericLdapConfig) ZLdapContext(com.zimbra.cs.ldap.ZLdapContext) ZSearchResultEnumeration(com.zimbra.cs.ldap.ZSearchResultEnumeration) ZAttributes(com.zimbra.cs.ldap.ZAttributes) LdapSizeLimitExceededException(com.zimbra.cs.ldap.LdapException.LdapSizeLimitExceededException) ZSearchResultEntry(com.zimbra.cs.ldap.ZSearchResultEntry)

Example 3 with GenericLdapConfig

use of com.zimbra.cs.ldap.LdapServerConfig.GenericLdapConfig in project zm-mailbox by Zimbra.

the class LdapExample method createEntry.

public void createEntry() throws Exception {
    // dn of entry to create
    String dn = "uid=user1,ou=people,dc=test,dc=com";
    GenericLdapConfig ldapConfig = getLdapConfig();
    ZLdapContext zlc = null;
    try {
        zlc = LdapClient.getContext(ldapConfig, LdapUsage.ADD);
        ZMutableEntry entry = LdapClient.createMutableEntry();
        entry.setDN(dn);
        entry.addAttr("objectClass", "inetOrgPerson");
        entry.addAttr("objectClass", "zimbraAccount");
        entry.addAttr("objectClass", "amavisAccount");
        entry.setAttr("uid", "user1");
        entry.setAttr("cn", "user1");
        entry.setAttr("sn", "lastname");
        entry.setAttr("zimbraAccountStatus", "active");
        entry.setAttr("zimbraId", "ba6198a3-bb49-4425-94b0-d4e9354e87b5");
        entry.addAttr("mail", "user1@trest.com");
        entry.addAttr("mail", "user-one@test.com");
        zlc.createEntry(entry);
    } finally {
        // Note: this is important!!
        LdapClient.closeContext(zlc);
    }
}
Also used : GenericLdapConfig(com.zimbra.cs.ldap.LdapServerConfig.GenericLdapConfig) ZMutableEntry(com.zimbra.cs.ldap.ZMutableEntry) ZLdapContext(com.zimbra.cs.ldap.ZLdapContext)

Example 4 with GenericLdapConfig

use of com.zimbra.cs.ldap.LdapServerConfig.GenericLdapConfig in project zm-mailbox by Zimbra.

the class LdapExample method getAttributes.

public void getAttributes() throws Exception {
    GenericLdapConfig ldapConfig = getLdapConfig();
    ZLdapContext zlc = null;
    try {
        zlc = LdapClient.getContext(ldapConfig, LdapUsage.SEARCH);
        /*
             * get attributes zimbraId, cn and description on DN "cn=default,cn=cos,cn=zimbra"
             */
        ZAttributes attrs = zlc.getAttributes("cn=default,cn=cos,cn=zimbra", new String[] { "zimbraId", "cn", "description" });
        String zimbraId = attrs.getAttrString("zimbraId");
        /*
             * get all attributes on DN "cn=default,cn=cos,cn=zimbra"
             */
        ZAttributes allAttrs = zlc.getAttributes("cn=default,cn=cos,cn=zimbra", null);
    } finally {
        // Note: this is important!!
        LdapClient.closeContext(zlc);
    }
}
Also used : GenericLdapConfig(com.zimbra.cs.ldap.LdapServerConfig.GenericLdapConfig) ZLdapContext(com.zimbra.cs.ldap.ZLdapContext) ZAttributes(com.zimbra.cs.ldap.ZAttributes)

Example 5 with GenericLdapConfig

use of com.zimbra.cs.ldap.LdapServerConfig.GenericLdapConfig in project zm-mailbox by Zimbra.

the class LdapExample method modifyEntry.

public void modifyEntry() throws Exception {
    String dn = "cn=config,cn=zimbra";
    GenericLdapConfig ldapConfig = getLdapConfig();
    ZLdapContext zlc = null;
    try {
        zlc = LdapClient.getContext(ldapConfig, LdapUsage.MOD);
        ZMutableEntry entry = LdapClient.createMutableEntry();
        entry.setAttr("description", "so this gets modified");
        zlc.replaceAttributes(dn, entry.getAttributes());
    } finally {
        // Note: this is important!!
        LdapClient.closeContext(zlc);
    }
}
Also used : GenericLdapConfig(com.zimbra.cs.ldap.LdapServerConfig.GenericLdapConfig) ZMutableEntry(com.zimbra.cs.ldap.ZMutableEntry) ZLdapContext(com.zimbra.cs.ldap.ZLdapContext)

Aggregations

GenericLdapConfig (com.zimbra.cs.ldap.LdapServerConfig.GenericLdapConfig)5 ZLdapContext (com.zimbra.cs.ldap.ZLdapContext)5 ZAttributes (com.zimbra.cs.ldap.ZAttributes)2 ZMutableEntry (com.zimbra.cs.ldap.ZMutableEntry)2 LdapSizeLimitExceededException (com.zimbra.cs.ldap.LdapException.LdapSizeLimitExceededException)1 ZLdapFilter (com.zimbra.cs.ldap.ZLdapFilter)1 ZSearchControls (com.zimbra.cs.ldap.ZSearchControls)1 ZSearchResultEntry (com.zimbra.cs.ldap.ZSearchResultEntry)1 ZSearchResultEnumeration (com.zimbra.cs.ldap.ZSearchResultEnumeration)1