Search in sources :

Example 11 with ZMutableEntry

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);
    }
}
Also used : ZMutableEntry(com.zimbra.cs.ldap.ZMutableEntry) LdapEntryAlreadyExistException(com.zimbra.cs.ldap.LdapException.LdapEntryAlreadyExistException) AccountServiceException(com.zimbra.cs.account.AccountServiceException) Zimlet(com.zimbra.cs.account.Zimlet) LdapZimlet(com.zimbra.cs.account.ldap.entry.LdapZimlet) ZLdapContext(com.zimbra.cs.ldap.ZLdapContext) AccountServiceException(com.zimbra.cs.account.AccountServiceException) AuthFailedServiceException(com.zimbra.cs.account.AccountServiceException.AuthFailedServiceException) ServiceException(com.zimbra.common.service.ServiceException) CallbackContext(com.zimbra.cs.account.callback.CallbackContext) LdapException(com.zimbra.cs.ldap.LdapException) Date(java.util.Date)

Example 12 with ZMutableEntry

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);
    }
}
Also used : GenericLdapConfig(com.zimbra.cs.ldap.LdapServerConfig.GenericLdapConfig) ZMutableEntry(com.zimbra.cs.ldap.ZMutableEntry) ZLdapContext(com.zimbra.cs.ldap.ZLdapContext)

Example 13 with ZMutableEntry

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"));
}
Also used : ZMutableEntry(com.zimbra.cs.ldap.ZMutableEntry)

Example 14 with ZMutableEntry

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));
}
Also used : ZMutableEntry(com.zimbra.cs.ldap.ZMutableEntry) HashMap(java.util.HashMap) ZAttributes(com.zimbra.cs.ldap.ZAttributes)

Example 15 with ZMutableEntry

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);
    }
}
Also used : GenericLdapConfig(com.zimbra.cs.ldap.LdapServerConfig.GenericLdapConfig) ZMutableEntry(com.zimbra.cs.ldap.ZMutableEntry) ZLdapContext(com.zimbra.cs.ldap.ZLdapContext)

Aggregations

ZMutableEntry (com.zimbra.cs.ldap.ZMutableEntry)26 ZLdapContext (com.zimbra.cs.ldap.ZLdapContext)18 LdapEntryAlreadyExistException (com.zimbra.cs.ldap.LdapException.LdapEntryAlreadyExistException)15 AccountServiceException (com.zimbra.cs.account.AccountServiceException)14 CallbackContext (com.zimbra.cs.account.callback.CallbackContext)14 LdapException (com.zimbra.cs.ldap.LdapException)14 ServiceException (com.zimbra.common.service.ServiceException)13 AuthFailedServiceException (com.zimbra.cs.account.AccountServiceException.AuthFailedServiceException)13 Date (java.util.Date)13 Domain (com.zimbra.cs.account.Domain)6 LdapDomain (com.zimbra.cs.account.ldap.entry.LdapDomain)6 HashMap (java.util.HashMap)6 LdapEntry (com.zimbra.cs.account.ldap.entry.LdapEntry)4 ZAttributes (com.zimbra.cs.ldap.ZAttributes)3 Cos (com.zimbra.cs.account.Cos)2 LdapCos (com.zimbra.cs.account.ldap.entry.LdapCos)2 LdapDynamicGroup (com.zimbra.cs.account.ldap.entry.LdapDynamicGroup)2 GenericLdapConfig (com.zimbra.cs.ldap.LdapServerConfig.GenericLdapConfig)2 BinaryLdapData (com.zimbra.qa.unittest.prov.BinaryLdapData)2 HashSet (java.util.HashSet)2