Search in sources :

Example 11 with ZAttributes

use of com.zimbra.cs.ldap.ZAttributes 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 12 with ZAttributes

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

the class TestLdapConnectivity method testConnectivity.

/*
     * Important: Each invocation of testConnectivity must be run in its own VM
     */
public void testConnectivity(ConnectionConfig connConfig) throws Exception {
    connConfig.setLocalConfig();
    try {
        LdapClient.initialize();
        int expectedPort;
        if (connConfig == ConnectionConfig.LDAPI) {
            // LdapServerPool.DUMMY_LDAPI_PORT
            expectedPort = 1;
        } else if (connConfig == ConnectionConfig.LDAP || connConfig == ConnectionConfig.STARTTLS_T_UNTRUSTED_T_MISMATCHED || connConfig == ConnectionConfig.STARTTLS_T_UNTRUSTED_F_MISMATCHED || connConfig == ConnectionConfig.STARTTLS_F_UNTRUSTED_T_MISMATCHED || connConfig == ConnectionConfig.STARTTLS_F_UNTRUSTED_F_MISMATCHED) {
            expectedPort = 389;
        } else {
            expectedPort = 636;
        }
        UBIDLdapContext zlc1 = getContext();
        assertEquals(expectedPort, zlc1.getNative().getConnectedPort());
        ZAttributes attrs = zlc1.getAttributes("cn=zimbra", null);
        assertEquals("Zimbra Systems Application Data", attrs.getAttrString("description"));
        UBIDLdapContext zlc2 = getContext();
        assertEquals(expectedPort, zlc2.getNative().getConnectedPort());
        closeContext(zlc1);
        closeContext(zlc2);
    } finally {
        // so next test can re-initialized UBIDLdapContext and run
        LdapClient.shutdown();
    }
}
Also used : ZAttributes(com.zimbra.cs.ldap.ZAttributes) UBIDLdapContext(com.zimbra.cs.ldap.unboundid.UBIDLdapContext)

Example 13 with ZAttributes

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

the class TestLdapHelper method getAttributesEntryNotFound.

@Test
public void getAttributesEntryNotFound() throws Exception {
    String dn = prov.getDIT().configDN() + "-not";
    boolean caughtException = false;
    try {
        ZAttributes attrs = ldapHelper.getAttributes(LdapUsage.UNITTEST, dn);
    } catch (LdapEntryNotFoundException e) {
        caughtException = true;
    }
    assertTrue(caughtException);
}
Also used : LdapEntryNotFoundException(com.zimbra.cs.ldap.LdapException.LdapEntryNotFoundException) ZAttributes(com.zimbra.cs.ldap.ZAttributes)

Example 14 with ZAttributes

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

the class TestLdapZMutableEntry method mapToAttrsSimple.

@Test
public void mapToAttrsSimple() throws Exception {
    String KEY_SINGLE = "key";
    String KEY_MULTI = "key-multi";
    String VALUE1 = "value1";
    String VALUE2 = "value2";
    Map<String, Object> attrMap = new HashMap<String, Object>();
    // single value
    attrMap.put(KEY_SINGLE, VALUE1);
    // multi value
    attrMap.put(KEY_MULTI, new String[] { VALUE1, VALUE2 });
    ZMutableEntry entry = LdapClient.createMutableEntry();
    entry.mapToAttrs(attrMap);
    // single value
    String valueGot = entry.getAttrString(KEY_SINGLE);
    assertEquals(VALUE1, valueGot);
    //  multi value
    ZAttributes zattrs = entry.getAttributes();
    Map<String, Object> attrs = zattrs.getAttrs();
    Object multiValueGot = attrs.get(KEY_MULTI);
    assertTrue(multiValueGot instanceof String[]);
    List<String> valuesGot = Arrays.asList((String[]) multiValueGot);
    assertTrue(valuesGot.contains(VALUE1));
    assertTrue(valuesGot.contains(VALUE2));
}
Also used : ZMutableEntry(com.zimbra.cs.ldap.ZMutableEntry) HashMap(java.util.HashMap) ZAttributes(com.zimbra.cs.ldap.ZAttributes)

Example 15 with ZAttributes

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

the class AutoProvisionManual method createAccount.

private Account createAccount() throws ServiceException {
    String acctZimbraName;
    ExternalEntry externalEntry;
    if (by == AutoProvPrincipalBy.dn) {
        ZAttributes externalAttrs = getExternalAttrsByDn(principal);
        externalEntry = new ExternalEntry(principal, externalAttrs);
        acctZimbraName = mapName(externalAttrs, null);
    } else if (by == AutoProvPrincipalBy.name) {
        externalEntry = getExternalAttrsByName(principal);
        acctZimbraName = mapName(externalEntry.getAttrs(), principal);
    } else {
        throw ServiceException.FAILURE("unknown AutoProvPrincipalBy", null);
    }
    ZimbraLog.autoprov.info("auto creating account in MANUAL mode: " + acctZimbraName);
    return createAccount(acctZimbraName, externalEntry, password, AutoProvMode.MANUAL);
}
Also used : ZAttributes(com.zimbra.cs.ldap.ZAttributes)

Aggregations

ZAttributes (com.zimbra.cs.ldap.ZAttributes)30 ServiceException (com.zimbra.common.service.ServiceException)18 AccountServiceException (com.zimbra.cs.account.AccountServiceException)16 AuthFailedServiceException (com.zimbra.cs.account.AccountServiceException.AuthFailedServiceException)15 LdapEntryNotFoundException (com.zimbra.cs.ldap.LdapException.LdapEntryNotFoundException)8 Account (com.zimbra.cs.account.Account)6 ZLdapContext (com.zimbra.cs.ldap.ZLdapContext)6 Domain (com.zimbra.cs.account.Domain)4 GuestAccount (com.zimbra.cs.account.GuestAccount)4 LdapAccount (com.zimbra.cs.account.ldap.entry.LdapAccount)4 HashMap (java.util.HashMap)4 LdapDomain (com.zimbra.cs.account.ldap.entry.LdapDomain)3 LdapDynamicGroup (com.zimbra.cs.account.ldap.entry.LdapDynamicGroup)3 ZMutableEntry (com.zimbra.cs.ldap.ZMutableEntry)3 Alias (com.zimbra.cs.account.Alias)2 AlwaysOnCluster (com.zimbra.cs.account.AlwaysOnCluster)2 DynamicGroup (com.zimbra.cs.account.DynamicGroup)2 Group (com.zimbra.cs.account.Group)2 NamedEntry (com.zimbra.cs.account.NamedEntry)2 Server (com.zimbra.cs.account.Server)2