Search in sources :

Example 6 with CreateException

use of org.apache.directory.fortress.core.CreateException in project directory-fortress-core by apache.

the class OrganizationalUnitDAO method create.

/**
 * @param oe
 * @throws org.apache.directory.fortress.core.CreateException
 */
void create(OrganizationalUnit oe) throws CreateException {
    LdapConnection ld = null;
    String nodeDn = SchemaConstants.OU_AT + "=" + oe.getName() + ",";
    if (StringUtils.isNotEmpty(oe.getParent())) {
        nodeDn += SchemaConstants.OU_AT + "=" + oe.getParent() + ",";
    }
    nodeDn += getRootDn(oe.getContextId());
    try {
        LOG.info("create container dn [{}]", nodeDn);
        Entry myEntry = new DefaultEntry(nodeDn, SchemaConstants.OBJECT_CLASS, SchemaConstants.ORGANIZATIONAL_UNIT_OC, SchemaConstants.OU_AT, oe.getName(), SchemaConstants.DESCRIPTION_AT, oe.getDescription());
        ld = getAdminConnection();
        add(ld, myEntry);
    } catch (LdapException e) {
        String error = "create container node dn [" + nodeDn + "] caught LdapException=" + e;
        throw new CreateException(GlobalErrIds.CNTR_CREATE_FAILED, error, e);
    } finally {
        closeAdminConnection(ld);
    }
}
Also used : DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) Entry(org.apache.directory.api.ldap.model.entry.Entry) DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) CreateException(org.apache.directory.fortress.core.CreateException) LdapConnection(org.apache.directory.ldap.client.api.LdapConnection)

Example 7 with CreateException

use of org.apache.directory.fortress.core.CreateException in project directory-fortress-core by apache.

the class SuffixDAO method create.

/**
 * @param se
 * @throws org.apache.directory.fortress.core.CreateException
 */
void create(Suffix se) throws CreateException {
    LdapConnection ld = null;
    String nodeDn = getDn(se);
    try {
        LOG.info("create suffix dn [{}]", nodeDn);
        Entry myEntry = new DefaultEntry(nodeDn);
        myEntry.add(SchemaConstants.OBJECT_CLASS_AT, SUFFIX_OBJ_CLASS);
        myEntry.add(SchemaConstants.DC_AT, se.getName());
        myEntry.add(SchemaConstants.O_AT, se.getDescription());
        ld = getAdminConnection();
        add(ld, myEntry);
    } catch (LdapException e) {
        String error = "create container node dn [" + nodeDn + "] caught LDAPException=" + e.getMessage();
        throw new CreateException(GlobalErrIds.SUFX_CREATE_FAILED, error, e);
    } finally {
        closeAdminConnection(ld);
    }
}
Also used : DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) Entry(org.apache.directory.api.ldap.model.entry.Entry) DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) CreateException(org.apache.directory.fortress.core.CreateException) LdapConnection(org.apache.directory.ldap.client.api.LdapConnection)

Example 8 with CreateException

use of org.apache.directory.fortress.core.CreateException in project directory-fortress-core by apache.

the class UserDAO method create.

/**
 * Add new user entity to LDAP
 *
 * @param entity
 * @return
 * @throws CreateException
 */
User create(User entity) throws CreateException {
    LdapConnection ld = null;
    try {
        entity.setInternalId();
        String dn = getDn(entity.getUserId(), entity.getContextId());
        Entry myEntry = new DefaultEntry(dn);
        myEntry.add(SchemaConstants.OBJECT_CLASS_AT, getUserObjectClass());
        // myEntry.add( SchemaConstants.OBJECT_CLASS_AT, USER_OBJ_CLASS );
        myEntry.add(GlobalIds.FT_IID, entity.getInternalId());
        myEntry.add(SchemaConstants.UID_AT, entity.getUserId());
        // CN is required on inetOrgPerson object class, if caller did not set, use the userId:
        if (StringUtils.isEmpty(entity.getCn())) {
            entity.setCn(entity.getUserId());
        }
        myEntry.add(SchemaConstants.CN_AT, entity.getCn());
        // SN is required on inetOrgPerson object class, if caller did not set, use the userId:
        if (StringUtils.isEmpty(entity.getSn())) {
            entity.setSn(entity.getUserId());
        }
        myEntry.add(SchemaConstants.SN_AT, entity.getSn());
        if (StringUtils.isNotEmpty(entity.getPassword())) {
            myEntry.add(SchemaConstants.USER_PASSWORD_AT, entity.getPassword());
        } else if (!Config.getInstance().getBoolean(GlobalIds.USER_CREATION_PASSWORD_FIELD, false)) {
            myEntry.add(SchemaConstants.USER_PASSWORD_AT, new String());
        }
        myEntry.add(SchemaConstants.DISPLAY_NAME_AT, entity.getCn());
        if (StringUtils.isNotEmpty(entity.getTitle())) {
            myEntry.add(SchemaConstants.TITLE_AT, entity.getTitle());
        }
        if (StringUtils.isNotEmpty(entity.getEmployeeType())) {
            myEntry.add(EMPLOYEE_TYPE, entity.getEmployeeType());
        }
        // These are multi-valued attributes, use the util function to load.
        // These items are optional.  The utility function will return quietly if item list is empty:
        loadAttrs(entity.getPhones(), myEntry, SchemaConstants.TELEPHONE_NUMBER_AT);
        loadAttrs(entity.getMobiles(), myEntry, MOBILE);
        loadAttrs(entity.getEmails(), myEntry, SchemaConstants.MAIL_AT);
        // The following attributes are optional:
        if (entity.isSystem() != null) {
            myEntry.add(SYSTEM_USER, entity.isSystem().toString().toUpperCase());
        }
        // If password policy is set and either openldap or apacheds in use:
        if ((Config.getInstance().isOpenldap() || Config.getInstance().isApacheds()) && StringUtils.isNotEmpty(entity.getPwPolicy())) {
            myEntry.add(OPENLDAP_POLICY_SUBENTRY, PolicyDAO.getPolicyDn(entity));
        }
        if (StringUtils.isNotEmpty(entity.getOu())) {
            myEntry.add(SchemaConstants.OU_AT, entity.getOu());
        }
        if (StringUtils.isNotEmpty(entity.getDescription())) {
            myEntry.add(SchemaConstants.DESCRIPTION_AT, entity.getDescription());
        }
        // props are optional as well:
        // Add "initial" property here.
        entity.addProperty("initAttrArrays", "");
        loadProperties(entity.getProperties(), myEntry, GlobalIds.PROPS);
        // map the userid to the name field in constraint:
        entity.setName(entity.getUserId());
        myEntry.add(GlobalIds.CONSTRAINT, ConstraintUtil.setConstraint(entity));
        loadAddress(entity.getAddress(), myEntry);
        if (ArrayUtils.isNotEmpty(entity.getJpegPhoto())) {
            myEntry.add(JPEGPHOTO, entity.getJpegPhoto());
        }
        ld = getAdminConnection();
        add(ld, myEntry, entity);
        entity.setDn(dn);
    } catch (LdapEntryAlreadyExistsException e) {
        String error = "create userId [" + entity.getUserId() + "] failed, already exists in directory";
        throw new CreateException(GlobalErrIds.USER_ADD_FAILED_ALREADY_EXISTS, error, e);
    } catch (LdapException e) {
        String error = "create userId [" + entity.getUserId() + "] caught LDAPException=" + e.getMessage();
        throw new CreateException(GlobalErrIds.USER_ADD_FAILED, error, e);
    } finally {
        closeAdminConnection(ld);
    }
    return entity;
}
Also used : LdapEntryAlreadyExistsException(org.apache.directory.api.ldap.model.exception.LdapEntryAlreadyExistsException) DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) Entry(org.apache.directory.api.ldap.model.entry.Entry) DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) CreateException(org.apache.directory.fortress.core.CreateException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) LdapConnection(org.apache.directory.ldap.client.api.LdapConnection)

Example 9 with CreateException

use of org.apache.directory.fortress.core.CreateException in project directory-fortress-core by apache.

the class ExampleDAO method create.

/**
 * @param entity
 * @return
 * @throws org.apache.directory.fortress.core.CreateException
 */
public Example create(Example entity) throws CreateException {
    LdapConnection ld = null;
    String dn = SchemaConstants.CN_AT + "=" + entity.getName() + "," + Config.getInstance().getProperty(EIds.EXAMPLE_ROOT);
    if (LOG.isDebugEnabled()) {
        LOG.debug("create dn [" + dn + "]");
    }
    try {
        /*
        public class Example
                implements Constraint, java.io.Serializable
        {
            private String id;          // this maps to oamId
            private String name;          // this is oamRoleName
            private String description; // this is description
            private String dn;          // this attribute is automatically saved to each ldap record.
            private String beginTime;     // this attribute is oamBeginTime
            private String endTime;        // this attribute is oamEndTime
            private String beginDate;    // this attribute is oamBeginDate
            private String endDate;        // this attribute is oamEndDate
            private String beginLockDate;// this attribute is oamBeginLockDate
            private String endLockDate;    // this attribute is oamEndLockDate
            private String dayMask;        // this attribute is oamDayMask
            private int timeout;        // this attribute is oamTimeOut
            */
        ld = getAdminConnection();
        Entry entry = new DefaultEntry(dn);
        entry.add(createAttributes(SchemaConstants.OBJECT_CLASS_AT, EIds.EXAMPLE_OBJ_CLASS));
        entity.setId();
        entry.add(GlobalIds.FT_IID, entity.getId());
        entry.add(EIds.EXAMPLE_NM, entity.getName());
        if (entity.getDescription() != null && entity.getDescription().length() > 0)
            entry.add(SchemaConstants.DESCRIPTION_AT, entity.getDescription());
        // organizational name requires CN attribute:
        entry.add(SchemaConstants.CN_AT, entity.getName());
        // AttrHelper.loadTemporalAttrs(entity, attrs);
        entity.setName("EXAMPLE");
        entry.add(GlobalIds.CONSTRAINT, ConstraintUtil.setConstraint(entity));
        add(ld, entry);
    } catch (LdapException e) {
        String error = "create [" + entity.getName() + "] caught LDAPException=" + e;
        LOG.error(error);
        throw new CreateException(EErrIds.EXAMPLE_ADD_FAILED, error);
    } finally {
        closeAdminConnection(ld);
    }
    return entity;
}
Also used : DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) Entry(org.apache.directory.api.ldap.model.entry.Entry) DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) CreateException(org.apache.directory.fortress.core.CreateException) LdapConnection(org.apache.directory.ldap.client.api.LdapConnection)

Example 10 with CreateException

use of org.apache.directory.fortress.core.CreateException in project directory-fortress-core by apache.

the class SdDAO method create.

/**
 * @param entity
 * @return
 * @throws org.apache.directory.fortress.core.CreateException
 */
SDSet create(SDSet entity) throws CreateException {
    LdapConnection ld = null;
    String dn = getDn(entity.getName(), entity.getContextId());
    String[] objectClass = SSD_OBJ_CLASS;
    if (entity.getType() == SDSet.SDType.DYNAMIC) {
        objectClass = DSD_OBJ_CLASS;
    }
    try {
        Entry entry = new DefaultEntry(dn);
        entry.add(createAttributes(SchemaConstants.OBJECT_CLASS_AT, objectClass));
        entity.setId();
        entry.add(GlobalIds.FT_IID, entity.getId());
        entry.add(SD_SET_NM, entity.getName());
        // description field is optional on this object class:
        if (StringUtils.isNotEmpty(entity.getDescription())) {
            entry.add(SchemaConstants.DESCRIPTION_AT, entity.getDescription());
        }
        // CN attribute is required for this object class:
        entry.add(SchemaConstants.CN_AT, entity.getName());
        loadAttrs(entity.getMembers(), entry, ROLES);
        entry.add(SD_SET_CARDINALITY, "" + entity.getCardinality());
        ld = getAdminConnection();
        add(ld, entry, entity);
    } catch (LdapException e) {
        String error = "create SD set name [" + entity.getName() + "] type [" + entity.getType() + "] caught LdapException=" + e.getMessage();
        int errCode;
        if (entity.getType() == SDSet.SDType.DYNAMIC) {
            errCode = GlobalErrIds.DSD_ADD_FAILED;
        } else {
            errCode = GlobalErrIds.SSD_ADD_FAILED;
        }
        throw new CreateException(errCode, error, e);
    } finally {
        closeAdminConnection(ld);
    }
    return entity;
}
Also used : DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) Entry(org.apache.directory.api.ldap.model.entry.Entry) DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) CreateException(org.apache.directory.fortress.core.CreateException) LdapConnection(org.apache.directory.ldap.client.api.LdapConnection)

Aggregations

LdapException (org.apache.directory.api.ldap.model.exception.LdapException)16 CreateException (org.apache.directory.fortress.core.CreateException)16 LdapConnection (org.apache.directory.ldap.client.api.LdapConnection)16 DefaultEntry (org.apache.directory.api.ldap.model.entry.DefaultEntry)15 Entry (org.apache.directory.api.ldap.model.entry.Entry)15 LdapEntryAlreadyExistsException (org.apache.directory.api.ldap.model.exception.LdapEntryAlreadyExistsException)2 ArrayList (java.util.ArrayList)1 DefaultModification (org.apache.directory.api.ldap.model.entry.DefaultModification)1 Modification (org.apache.directory.api.ldap.model.entry.Modification)1 Dn (org.apache.directory.api.ldap.model.name.Dn)1 PermissionAttribute (org.apache.directory.fortress.core.model.PermissionAttribute)1