Example usage for javax.naming.directory BasicAttribute BasicAttribute

List of usage examples for javax.naming.directory BasicAttribute BasicAttribute

Introduction

In this page you can find the example usage for javax.naming.directory BasicAttribute BasicAttribute.

Prototype

public BasicAttribute(String id) 

Source Link

Document

Constructs a new instance of an unordered attribute with no value.

Usage

From source file:nl.knaw.dans.common.ldap.repo.LdapMapper.java

/**
 * Marshal an object to attributes./* ww  w  .ja va  2 s.c o  m*/
 *
 * @param instance
 *        the object to be marshaled
 * @param forUpdate
 *        is this an add or update operation
 * @return the attributes derived from annotated fields and methods of the object
 * @throws MissingAttributeException
 *         if a required attribute is null or blank
 * @throws LdapMappingException
 *         wrapper for various exceptions
 */
public Attributes marshal(T instance, boolean forUpdate)
        throws MissingAttributeException, LdapMappingException {
    if (!clazz.equals(instance.getClass())) {
        throw new IllegalArgumentException(instance.getClass().getName() + " is not a " + clazz.getName());
    }
    Attributes attrs = new BasicAttributes();
    Attribute oc = new BasicAttribute("objectclass");
    for (String objectClass : getObjectClasses()) {
        oc.add(objectClass);
    }
    attrs.put(oc);

    loadAttributesFromFields(instance, attrs, forUpdate);
    loadAttributesFromMethods(instance, attrs, forUpdate);

    return attrs;
}

From source file:org.nuxeo.ecm.directory.ldap.MockLdapServer.java

public void createUser(String uid, String cn, String password) {
    Attributes user = new BasicAttributes("uid", uid);
    user.put("cn", cn);
    user.put("userPassword", password);

    Attribute objectClass = new BasicAttribute("objectClass");
    user.put(objectClass);/* w  ww  . j ava2  s  .c o m*/
    objectClass.add("top");
    objectClass.add("person");
    objectClass.add("organizationalPerson");
    objectClass.add("inetOrgPerson");
    user.put("sn", uid);

    try {
        serverContext.createSubcontext("uid=" + uid + ",ou=people", user);
    } catch (NameAlreadyBoundException ignore) {
        // System.out.println(" user " + uid + " already exists.");
    } catch (NamingException ne) {
        System.err.println("Failed to create user.");
        ne.printStackTrace();
    }
}

From source file:com.globalsight.everest.usermgr.UserLdapHelper.java

/**
 * Convert a User object to an Attributes object
 * /*from   www.  j a  va  2  s.c om*/
 * @param user
 *            The ldap user info.
 * @param needEncodePwd
 *            If user password need to be encoded, set this to true.
 * 
 * @return a Attributes
 */
static Attributes convertUserToLDAPEntry(User user, boolean needEncodePwd) {

    BasicAttributes attrSet = new BasicAttributes();
    Attribute objClass = new BasicAttribute(LDAP_ATTR_OBJECT_CLASS);
    objClass.add(LDAP_USER_OBJECT_CLASSES[0]);
    objClass.add(LDAP_USER_OBJECT_CLASSES[1]);
    objClass.add(LDAP_USER_OBJECT_CLASSES[2]);
    objClass.add(LDAP_USER_OBJECT_CLASSES[3]);
    objClass.add(LDAP_USER_OBJECT_CLASSES[4]);
    // add each LDAP attribute
    attrSet.put(objClass);
    if (isStringValid(user.getUserId())) {
        attrSet.put(generateLDAPAttribute(LDAP_ATTR_USERID, user.getUserId()));
    }
    if (isStringValid(user.getUserName())) {
        attrSet.put(generateLDAPAttribute(LDAP_ATTR_USER_NAME, user.getUserName()));
    }
    if (isStringValid(user.getTitle())) {
        attrSet.put(generateLDAPAttribute(LDAP_ATTR_TITLE, user.getTitle()));
    }
    if (isStringValid(user.getLastName())) {
        attrSet.put(generateLDAPAttribute(LDAP_ATTR_LAST_NAME, user.getLastName()));
    }
    if (isStringValid(user.getFirstName())) {
        attrSet.put(generateLDAPAttribute(LDAP_ATTR_FIRST_NAME, user.getFirstName()));
    }

    String status = getStateAsString(user.getState());
    attrSet.put(generateLDAPAttribute(LDAP_ATTR_STATUS, status));

    if (isStringValid(user.getPassword())) {
        if (needEncodePwd) {
            attrSet.put(generateLDAPAttribute(LDAP_ATTR_PASSWORD, encyptMD5Password(user.getPassword())));
        } else {
            attrSet.put(generateLDAPAttribute(LDAP_ATTR_PASSWORD, user.getPassword()));
        }
    }

    if (isStringValid(user.getEmail())) {
        attrSet.put(generateLDAPAttribute(LDAP_ATTR_EMAIL, user.getEmail()));
    }
    if (isStringValid(user.getCCEmail())) {
        attrSet.put(generateLDAPAttribute(LDAP_ATTR_CC_EMAIL, user.getCCEmail()));
    }
    if (isStringValid(user.getBCCEmail())) {
        attrSet.put(generateLDAPAttribute(LDAP_ATTR_BCC_EMAIL, user.getBCCEmail()));
    }
    if (isStringValid(user.getHomePhoneNumber())) {
        attrSet.put(generateLDAPAttribute(LDAP_ATTR_HOME_PHONE, user.getHomePhoneNumber()));
    }
    if (isStringValid(user.getOfficePhoneNumber())) {
        attrSet.put(generateLDAPAttribute(LDAP_ATTR_OFFICE_PHONE, user.getOfficePhoneNumber()));
    }
    if (isStringValid(user.getFaxPhoneNumber())) {
        attrSet.put(generateLDAPAttribute(LDAP_ATTR_FAX_NUMBER, user.getFaxPhoneNumber()));
    }
    if (isStringValid(user.getCellPhoneNumber())) {
        attrSet.put(generateLDAPAttribute(LDAP_ATTR_CELL_NUMBER, user.getCellPhoneNumber()));
    }
    if (isStringValid(user.getDefaultUILocale())) {
        attrSet.put(generateLDAPAttribute(LDAP_ATTR_DEFAULT_UI_LOCALE, user.getDefaultUILocale()));
    }
    if (isStringValid(user.getAddress()))
        attrSet.put(generateLDAPAttribute(LDAP_ATTR_ADDRESS, user.getAddress()));
    if (isStringValid(user.getCompanyName())) {
        attrSet.put(generateLDAPAttribute(LDAP_ATTR_COMPANY, user.getCompanyName()));
    }
    // vonverts 'boolean' to corresponding 'String' because openldap
    // 'Attribute' need string parameter.
    attrSet.put(generateLDAPAttribute(LDAP_ATTR_INALLPROJECTS, user.isInAllProjects()));
    // if anonymous then write it out - otherwise just leave NULL
    if (user.getType() == User.UserType.ANONYMOUS) {
        attrSet.put(generateLDAPAttribute(LDAP_ATTR_TYPE, LDAP_ANONYMOUS_USER_TYPE));
    }

    return attrSet;
}

From source file:LDAPTest.java

/**
     * Saves the changes that the user made.
     *///from  ww w.j  av  a2  s  .  c om
    public void saveEntry() {
        try {
            if (dataPanel == null)
                return;
            if (context == null)
                context = getContext();
            if (uidField.getText().equals(uid)) // update existing entry
            {
                String dn = "uid=" + uidField.getText() + ",ou=people,dc=mycompany,dc=com";
                Attributes editedAttrs = dataPanel.getEditedAttributes();
                NamingEnumeration<? extends Attribute> attrEnum = attrs.getAll();
                while (attrEnum.hasMore()) {
                    Attribute attr = attrEnum.next();
                    String id = attr.getID();
                    Attribute editedAttr = editedAttrs.get(id);
                    if (editedAttr != null && !attr.get().equals(editedAttr.get()))
                        context.modifyAttributes(dn, DirContext.REPLACE_ATTRIBUTE,
                                new BasicAttributes(id, editedAttr.get()));
                }
            } else
            // create new entry
            {
                String dn = "uid=" + uidField.getText() + ",ou=people,dc=mycompany,dc=com";
                attrs = dataPanel.getEditedAttributes();
                Attribute objclass = new BasicAttribute("objectClass");
                objclass.add("uidObject");
                objclass.add("person");
                attrs.put(objclass);
                attrs.put("uid", uidField.getText());
                context.createSubcontext(dn, attrs);
            }

            findEntry();
        } catch (NamingException e) {
            JOptionPane.showMessageDialog(LDAPFrame.this, e);
            e.printStackTrace();
        } catch (IOException e) {
            JOptionPane.showMessageDialog(LDAPFrame.this, e);
            e.printStackTrace();
        }
    }

From source file:org.apache.archiva.redback.authentication.ldap.LdapBindAuthenticatorTest.java

private void bindUserObject(DirContext context, String cn, String dn) throws Exception {
    Attributes attributes = new BasicAttributes(true);
    BasicAttribute objectClass = new BasicAttribute("objectClass");
    objectClass.add("top");
    objectClass.add("inetOrgPerson");
    objectClass.add("person");
    objectClass.add("organizationalperson");
    attributes.put(objectClass);//from   www .j  a va2 s .  co m
    attributes.put("cn", cn);
    attributes.put("sn", "foo");
    attributes.put("mail", "foo");
    attributes.put("userPassword", passwordEncoder.encodePassword("foo"));
    attributes.put("givenName", "foo");
    context.createSubcontext(dn, attributes);
}

From source file:org.mule.providers.ldap.util.DSManager.java

private void setUpPartition(MutableServerStartupConfiguration configuration) throws NamingException {
    // Add partition 'sevenSeas'
    MutablePartitionConfiguration pcfg = new MutablePartitionConfiguration();
    pcfg.setName("sevenSeas");
    pcfg.setSuffix("o=sevenseas");

    // Create some indices
    java.util.Set indexedAttrs = new HashSet();
    indexedAttrs.add("objectClass");
    indexedAttrs.add("o");
    pcfg.setIndexedAttributes(indexedAttrs);

    // Create a first entry associated to the partition
    Attributes attrs = new BasicAttributes(true);

    // First, the objectClass attribute
    Attribute attr = new BasicAttribute("objectClass");
    attr.add("top");
    attr.add("organization");
    attrs.put(attr);/*from ww w .j  a  v  a 2  s .c  o  m*/

    // The the 'Organization' attribute
    attr = new BasicAttribute("o");
    attr.add("sevenseas");
    attrs.put(attr);

    // Associate this entry to the partition
    pcfg.setContextEntry(attrs);

    // As we can create more than one partition, we must store
    // each created partition in a Set before initialization
    Set pcfgs = new HashSet();
    pcfgs.add(pcfg);

    configuration.setContextPartitionConfigurations(pcfgs);

    // Create a working directory
    // File workingDirectory = new File( "server-work" );
    // configuration.setWorkingDirectory( workingDirectory );

}

From source file:org.easy.ldap.NamingFactory.java

public static Attribute getRolesObjectClasses() {
    Attribute objClasses = new BasicAttribute(LdapClasseNames.OBJECT_CLASS.toString());
    objClasses.add(LdapClasseNames.TOP.toString());
    objClasses.add(LdapClasseNames.ORG_UNIT.toString());

    return objClasses;
}

From source file:org.apache.archiva.redback.common.ldap.role.TestLdapRoleMapper.java

private void createGroup(DirContext context, String groupName, String dn, List<String> users) throws Exception {

    Attributes attributes = new BasicAttributes(true);
    BasicAttribute objectClass = new BasicAttribute("objectClass");
    objectClass.add("top");
    objectClass.add("groupOfUniqueNames");
    attributes.put(objectClass);/*from ww w.  j  av a2  s. c  o m*/
    attributes.put("cn", groupName);
    BasicAttribute basicAttribute = new BasicAttribute("uniquemember");
    for (String user : users) {
        basicAttribute.add("uid=" + user + "," + suffix);// dc=archiva,dc=apache,dc=org" );
    }

    attributes.put(basicAttribute);
    context.createSubcontext(dn, attributes);
}

From source file:com.liferay.portal.security.ldap.BasePortalToLDAPConverter.java

public Attributes getLDAPUserAttributes(long ldapServerId, User user, Properties userMappings)
        throws SystemException {

    Attributes attributes = new BasicAttributes(true);

    Attribute objectClass = new BasicAttribute(_OBJECT_CLASS);

    String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId);

    String[] defaultObjectClasses = PrefsPropsUtil.getStringArray(user.getCompanyId(),
            PropsKeys.LDAP_USER_DEFAULT_OBJECT_CLASSES + postfix, StringPool.COMMA);

    for (int i = 0; i < defaultObjectClasses.length; i++) {
        objectClass.add(defaultObjectClasses[i]);
    }/*from ww w .java  2 s.  c o  m*/

    attributes.put(objectClass);

    addAttributeMapping(userMappings.getProperty(UserConverterKeys.SCREEN_NAME), user.getScreenName(),
            attributes);
    addAttributeMapping(userMappings.getProperty(UserConverterKeys.PASSWORD), user.getPasswordUnencrypted(),
            attributes);
    addAttributeMapping(userMappings.getProperty(UserConverterKeys.EMAIL_ADDRESS), user.getEmailAddress(),
            attributes);
    addAttributeMapping(userMappings.getProperty(UserConverterKeys.FULL_NAME), user.getFullName(), attributes);
    addAttributeMapping(userMappings.getProperty(UserConverterKeys.FIRST_NAME), user.getFirstName(),
            attributes);
    addAttributeMapping(userMappings.getProperty(UserConverterKeys.MIDDLE_NAME), user.getMiddleName(),
            attributes);
    addAttributeMapping(userMappings.getProperty(UserConverterKeys.LAST_NAME), user.getLastName(), attributes);
    addAttributeMapping(userMappings.getProperty(UserConverterKeys.JOB_TITLE), user.getJobTitle(), attributes);
    addAttributeMapping(userMappings.getProperty(UserConverterKeys.PORTRAIT), getUserPortrait(user),
            attributes);

    return attributes;
}

From source file:edu.kit.scc.ldap.LdapPosixUserDao.java

/**
 * Updates a POSIX user in the LDAP directory.
 * /*from  w ww. j av  a 2  s .  co  m*/
 * @param posixUser the {@link PosixUser} to update
 * @return the {@link PosixUser} updated
 */
public PosixUser updateUser(PosixUser posixUser) {
    BasicAttribute personBasicAttribute = new BasicAttribute("objectclass");
    personBasicAttribute.add("extensibleObject");
    personBasicAttribute.add("inetOrgPerson");
    personBasicAttribute.add("organizationalPerson");
    personBasicAttribute.add("person");
    personBasicAttribute.add("posixAccount");

    Attributes personAttributes = new BasicAttributes();
    personAttributes.put(personBasicAttribute);

    if (posixUser.getCommonName() != null) {
        personAttributes.put("cn", posixUser.getCommonName());
    }
    if (posixUser.getSurName() != null) {
        personAttributes.put("sn", posixUser.getSurName());
    }
    if (posixUser.getUid() != null) {
        personAttributes.put("uid", posixUser.getUid());
    }
    if (posixUser.getUidNumber() != null) {
        personAttributes.put("uidNumber", String.valueOf(posixUser.getUidNumber()));
    }
    if (posixUser.getGidNumber() != null) {
        personAttributes.put("gidNumber", String.valueOf(posixUser.getGidNumber()));
    }
    if (posixUser.getHomeDirectory() != null) {
        personAttributes.put("homeDirectory", posixUser.getHomeDirectory());
    }
    if (posixUser.getUniqueIdentifier() != null) {
        personAttributes.put("uniqueIdentifier", posixUser.getUniqueIdentifier());
    }
    if (posixUser.getDescription() != null) {
        personAttributes.put("description", posixUser.getDescription());
    }
    if (posixUser.getGecos() != null) {
        personAttributes.put("gecos", posixUser.getGecos());
    }
    if (posixUser.getLoginShell() != null) {
        personAttributes.put("loginShell", posixUser.getLoginShell());
    }
    if (posixUser.getUserPassword() != null) {
        personAttributes.put("userPassword", posixUser.getUserPassword());
    }
    if (posixUser.getGivenName() != null) {
        personAttributes.put("givenName", posixUser.getGivenName());
    }
    if (posixUser.getMail() != null) {
        personAttributes.put("mail", posixUser.getMail());
    }

    LdapName userDn = LdapUtils.emptyLdapName();
    try {
        userDn = new LdapName(userBase);
        userDn.add("uid=" + posixUser.getUid());
        log.debug("Update {}", userDn.toString());
        ldapTemplate.rebind(userDn, null, personAttributes);

        return posixUser;
    } catch (InvalidNameException ex) {
        log.error("ERROR {}", ex.toString());
        // ex.printStackTrace();
    }
    return null;
}