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);
}
}
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();
}
}
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);
}
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));
}
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);
}
Aggregations