use of com.zimbra.cs.ldap.ZMutableEntry in project zm-mailbox by Zimbra.
the class LdapProvisioning method createZimlet.
@Override
public Zimlet createZimlet(String name, Map<String, Object> zimletAttrs) throws ServiceException {
name = name.toLowerCase().trim();
CallbackContext callbackContext = new CallbackContext(CallbackContext.Op.CREATE);
AttributeManager.getInstance().preModify(zimletAttrs, null, callbackContext, true);
ZLdapContext zlc = null;
try {
zlc = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.CREATE_ZIMLET);
String hasKeyword = LdapConstants.LDAP_FALSE;
if (zimletAttrs.containsKey(A_zimbraZimletKeyword)) {
hasKeyword = ProvisioningConstants.TRUE;
}
ZMutableEntry entry = LdapClient.createMutableEntry();
entry.mapToAttrs(zimletAttrs);
entry.setAttr(A_objectClass, "zimbraZimletEntry");
entry.setAttr(A_zimbraZimletEnabled, ProvisioningConstants.FALSE);
entry.setAttr(A_zimbraZimletIndexingEnabled, hasKeyword);
entry.setAttr(A_zimbraCreateTimestamp, LdapDateUtil.toGeneralizedTime(new Date()));
String dn = mDIT.zimletNameToDN(name);
entry.setDN(dn);
zlc.createEntry(entry);
Zimlet zimlet = lookupZimlet(name, zlc);
AttributeManager.getInstance().postModify(zimletAttrs, zimlet, callbackContext);
return zimlet;
} catch (LdapEntryAlreadyExistException nabe) {
throw AccountServiceException.ZIMLET_EXISTS(name);
} catch (LdapException e) {
throw e;
} catch (AccountServiceException e) {
throw e;
} catch (ServiceException e) {
throw ServiceException.FAILURE("unable to create zimlet: " + name, e);
} finally {
LdapClient.closeContext(zlc);
}
}
use of com.zimbra.cs.ldap.ZMutableEntry 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);
}
}
use of com.zimbra.cs.ldap.ZMutableEntry in project zm-mailbox by Zimbra.
the class TestLdapZMutableEntry method hasAttribute.
@Test
public void hasAttribute() throws Exception {
String KEY = "key";
String VALUE1 = "value1";
ZMutableEntry entry = LdapClient.createMutableEntry();
entry.setAttr(KEY, VALUE1);
assertTrue(entry.hasAttribute(KEY));
assertFalse(entry.hasAttribute(KEY + "-NOT"));
}
use of com.zimbra.cs.ldap.ZMutableEntry 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.ZMutableEntry 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);
}
}
Aggregations