Example usage for javax.naming.directory Attributes put

List of usage examples for javax.naming.directory Attributes put

Introduction

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

Prototype

Attribute put(Attribute attr);

Source Link

Document

Adds a new attribute to the attribute set.

Usage

From source file:org.wso2.carbon.connector.ldap.AddEntry.java

@Override
public void connect(MessageContext messageContext) throws ConnectException {
    String objectClass = (String) getParameter(messageContext, LDAPConstants.OBJECT_CLASS);
    String attributesString = (String) getParameter(messageContext, LDAPConstants.ATTRIBUTES);
    String dn = (String) getParameter(messageContext, LDAPConstants.DN);

    OMFactory factory = OMAbstractFactory.getOMFactory();
    OMNamespace ns = factory.createOMNamespace(LDAPConstants.CONNECTOR_NAMESPACE, LDAPConstants.NAMESPACE);
    OMElement result = factory.createOMElement(LDAPConstants.RESULT, ns);
    OMElement message = factory.createOMElement(LDAPConstants.MESSAGE, ns);

    try {//from www  .  ja  v a  2s . com
        DirContext context = LDAPUtils.getDirectoryContext(messageContext);

        String classes[] = objectClass.split(",");
        Attributes entry = new BasicAttributes();
        Attribute obClassAttr = new BasicAttribute(LDAPConstants.OBJECT_CLASS);
        for (int i = 0; i < classes.length; i++) {
            obClassAttr.add(classes[i]);
        }
        entry.put(obClassAttr);
        if (StringUtils.isNotEmpty(attributesString)) {
            JSONObject object = new JSONObject(attributesString);
            Iterator keys = object.keys();
            while (keys.hasNext()) {
                String key = (String) keys.next();
                String val = object.getString(key);
                Attribute newAttr = new BasicAttribute(key);
                newAttr.add(val);
                entry.put(newAttr);
            }
        }
        try {
            context.createSubcontext(dn, entry);
            message.setText(LDAPConstants.SUCCESS);
            result.addChild(message);
            LDAPUtils.preparePayload(messageContext, result);
        } catch (NamingException e) {
            log.error("Failed to create ldap entry with dn = " + dn, e);
            LDAPUtils.handleErrorResponse(messageContext, LDAPConstants.ErrorConstants.ADD_ENTRY_ERROR, e);
            throw new SynapseException(e);
        }
    } catch (NamingException e) {
        LDAPUtils.handleErrorResponse(messageContext, LDAPConstants.ErrorConstants.INVALID_LDAP_CREDENTIALS, e);
        throw new SynapseException(e);
    } catch (JSONException e) {
        handleException("Error while passing the JSON object", e, messageContext);
    }
}

From source file:org.gbif.portal.registration.LDAPUtils.java

/**
 * Update the password for the supplied user.
 * @param username/*from   w ww .  ja  v a 2s.c  o m*/
 * @param newPassword
 * @throws NamingException
 */
public void updatePassword(String username, String newPassword) throws NamingException {
    DirContext ctx = getUserContext();
    Attributes attributes = new BasicAttributes();
    attributes.put(new BasicAttribute("userPassword", newPassword));
    ctx.modifyAttributes("uid=" + username, DirContext.REPLACE_ATTRIBUTE, attributes);
}

From source file:CreateCorbaSchema.java

/**
 * Inserts object class definitions from RFC 2714 into the schema.
 *
 * This method maps the LDAP schema definitions in RFC 2714 onto the
 * proprietary attributes required by the Active Directory schema.
 *
 * The resulting object class definitions differ from those of RFC 2714
 * in the following ways:/*from www.  j a  v a2  s.c  o m*/
 *
 *     - Abstract and auxiliary classes are now defined as structural.
 *     - The corbaObject class now inherits from corbaContainer.
 *     - The corbaObjectReference class now inherits from corbaObject.
 *
 * The effect of these differences is that CORBA object references
 * cannot be mixed-in with other directory entries, they may only be
 * stored as stand-alone entries.
 *
 * The reason for these differences is due to the way auxiliary classes
 * are supported in Active Directory. Only the names of structural
 * classes (not auxiliary) may appear in the object class attribute of
 * an entry. Therefore, the abstract and auxiliary classes in the CORBA
 * schema definition is re-defined as structural.
 */
protected void insertADObjectClasses(DirContext rootCtx, DirContext schemaCtx) throws NamingException {

    System.out.println("  [inserting new object class definitions ...]");

    String dn = schemaCtx.getNameInNamespace();
    String attrID;

    attrID = new String("corbaContainer");
    Attributes attrs1 = new BasicAttributes();

    attrs1.put(new BasicAttribute("cn", attrID));
    attrs1.put(new BasicAttribute("objectClass", "classSchema"));
    attrs1.put(new BasicAttribute("defaultHidingValue", "FALSE"));
    attrs1.put(new BasicAttribute("governsID", "1.3.6.1.4.1.42.2.27.4.2.10"));
    attrs1.put(new BasicAttribute("lDAPDisplayName", attrID));
    attrs1.put(new BasicAttribute("mustContain", "cn"));
    attrs1.put(new BasicAttribute("objectClassCategory", "1"));
    attrs1.put(new BasicAttribute("systemOnly", "FALSE"));
    attrs1.put(new BasicAttribute("subclassOf", "top"));
    attrs1.put(new BasicAttribute("possSuperiors", "top")); //any superior
    attrs1.put(new BasicAttribute("description", "Container for a CORBA object"));

    schemaCtx.createSubcontext("cn=" + attrID, attrs1);
    System.out.println("    [" + attrID + "]");

    flushADSchemaMods(rootCtx); // corbaObject relys on corbaContainer

    attrID = new String("corbaObject");
    Attributes attrs2 = new BasicAttributes();

    attrs2.put(new BasicAttribute("cn", attrID));
    attrs2.put(new BasicAttribute("objectClass", "classSchema"));
    attrs2.put(new BasicAttribute("defaultHidingValue", "FALSE"));
    attrs2.put(new BasicAttribute("governsID", "1.3.6.1.4.1.42.2.27.4.2.9"));
    attrs2.put(new BasicAttribute("lDAPDisplayName", attrID));

    Attribute coMay = new BasicAttribute("mayContain");
    coMay.add("corbaRepositoryId");
    coMay.add("description");
    attrs2.put(coMay);

    attrs2.put(new BasicAttribute("objectClassCategory", "1"));
    attrs2.put(new BasicAttribute("systemOnly", "FALSE"));
    attrs2.put(new BasicAttribute("subclassOf", "corbaContainer"));
    attrs2.put(new BasicAttribute("description", "CORBA object representation"));

    schemaCtx.createSubcontext("cn=" + attrID, attrs2);
    System.out.println("    [" + attrID + "]");

    flushADSchemaMods(rootCtx); // corbaObjectReference relys on corbaObject

    attrID = new String("corbaObjectReference");
    Attributes attrs3 = new BasicAttributes();

    attrs3.put(new BasicAttribute("cn", attrID));
    attrs3.put(new BasicAttribute("objectClass", "classSchema"));
    attrs3.put(new BasicAttribute("defaultHidingValue", "FALSE"));
    attrs3.put(new BasicAttribute("governsID", "1.3.6.1.4.1.42.2.27.4.2.11"));
    attrs3.put(new BasicAttribute("lDAPDisplayName", attrID));
    attrs3.put(new BasicAttribute("mustContain", "corbaIor"));
    attrs3.put(new BasicAttribute("objectClassCategory", "1"));
    attrs3.put(new BasicAttribute("systemOnly", "FALSE"));
    attrs3.put(new BasicAttribute("subclassOf", "corbaObject"));
    attrs3.put(new BasicAttribute("description", "CORBA interoperable object reference"));

    schemaCtx.createSubcontext("cn=" + attrID, attrs3);
    System.out.println("    [" + attrID + "]");

    flushADSchemaMods(rootCtx); // finally
}

From source file:org.gbif.portal.registration.LDAPUtils.java

/**
 * Update the details of the supplied user in LDAP.
 * @param userLogin//  w  w w .j a  va2 s  .  co  m
 * @return
 * @throws NamingException
 */
public boolean updateUser(UserLogin userLogin) throws NamingException {
    DirContext ctx = getUserContext();
    Attributes attributes = new BasicAttributes();
    attributes.put(new BasicAttribute("sn", userLogin.getSurname()));
    attributes.put(new BasicAttribute("givenName", userLogin.getFirstName()));
    attributes.put(new BasicAttribute("cn", userLogin.getFirstName() + " " + userLogin.getSurname()));
    attributes.put(new BasicAttribute("mail", userLogin.getEmail()));
    if (userLogin.getTelephone() != null) {
        attributes.put(new BasicAttribute("telephoneNumber", userLogin.getTelephone()));
    }
    attributes.put(new BasicAttribute("userPassword", userLogin.getPassword()));
    ctx.modifyAttributes("uid=" + userLogin.getUsername(), DirContext.REPLACE_ATTRIBUTE, attributes);
    return true;
}

From source file:org.apache.archiva.redback.rest.services.LdapGroupMappingServiceTest.java

private void createGroup(DirContext context, String groupName, String dn) throws Exception {

    Attributes attributes = new BasicAttributes(true);
    BasicAttribute objectClass = new BasicAttribute("objectClass");
    objectClass.add("top");
    objectClass.add("groupOfUniqueNames");
    attributes.put(objectClass);
    attributes.put("cn", groupName);
    BasicAttribute basicAttribute = new BasicAttribute("uniquemember");

    basicAttribute.add("uid=admin," + suffix);

    attributes.put(basicAttribute);//from   w  w  w . ja va  2 s.  c  o m
    context.createSubcontext(dn, attributes);
}

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

@Before
public void setUp() throws Exception {
    super.setUp();

    usersPerGroup = new HashMap<String, List<String>>(3);

    usersPerGroup.put("internal-repo-manager", Arrays.asList("admin", "user.9"));
    usersPerGroup.put("internal-repo-observer", Arrays.asList("admin", "user.7", "user.8"));
    usersPerGroup.put("archiva-admin", Arrays.asList("admin", "user.7"));

    users = new ArrayList<String>(4);
    users.add("admin");
    users.add("user.7");
    users.add("user.8");
    users.add("user.9");

    passwordEncoder = new SHA1PasswordEncoder();

    groupSuffix = apacheDs.addSimplePartition("test", new String[] { "archiva", "apache", "org" }).getSuffix();

    log.info("groupSuffix: {}", groupSuffix);

    suffix = "ou=People,dc=archiva,dc=apache,dc=org";

    log.info("DN Suffix: {}", suffix);

    apacheDs.startServer();/*from ww w.  j  ava2  s. c o  m*/

    BasicAttribute objectClass = new BasicAttribute("objectClass");
    objectClass.add("top");
    objectClass.add("organizationalUnit");

    Attributes attributes = new BasicAttributes(true);
    attributes.put(objectClass);
    attributes.put("organizationalUnitName", "foo");

    apacheDs.getAdminContext().createSubcontext(suffix, attributes);

    makeUsers();

    createGroups();

}

From source file:org.gbif.portal.registration.LDAPUtils.java

/**
 * Creates a user. String array contains:
 * 1) first name//from ww w .  j  ava 2 s  .  c  om
 * 2) surname
 * 3) email
 * 4) username
 * 5) password
 * 
 * @param userDetails
 * @return
 * @throws NamingException
 */
public boolean createNewUser(UserLogin userLogin) throws NamingException {
    DirContext ctx = getUserContext();
    Attributes attributes = new BasicAttributes();
    attributes.put(new BasicAttribute("sn", userLogin.getSurname()));
    attributes.put(new BasicAttribute("givenName", userLogin.getFirstName()));
    attributes.put(new BasicAttribute("cn", userLogin.getFirstName() + " " + userLogin.getSurname()));
    attributes.put(new BasicAttribute("mail", userLogin.getEmail()));
    if (userLogin.getTelephone() != null) {
        attributes.put(new BasicAttribute("telephoneNumber", userLogin.getTelephone()));
    }
    attributes.put(new BasicAttribute("userPassword", userLogin.getPassword()));
    attributes.put(new BasicAttribute("objectClass", "top"));
    attributes.put(new BasicAttribute("objectClass", "person"));
    attributes.put(new BasicAttribute("objectClass", "organizationalPerson"));
    attributes.put(new BasicAttribute("objectClass", "inetorgperson"));
    String contextName = "uid=" + userLogin.getUsername();
    String fullContextName = contextName + "," + ctx.getNameInNamespace();

    //add the user to ldap
    ctx.createSubcontext(contextName, attributes);

    //need to add user to group
    for (int i = 0; i < userGroups.length; i++) {
        DirContext groupContext = getGroupContext();
        Attributes groupAttributes = groupContext.getAttributes(userGroups[i]);
        groupAttributes.get("uniqueMember").add(fullContextName);
        groupContext.modifyAttributes(userGroups[i], DirContext.REPLACE_ATTRIBUTE, groupAttributes);
    }
    return true;
}

From source file:com.swdouglass.joid.store.DirectoryStoreImpl.java

public DirectoryStoreImpl() {
    try {//w  w  w . j a v a 2  s  .  c  om
        // Set up environment for creating the initial context
        initialCtx = DirectoryUtil.getInitialDirContext();

        this.setOuOpenID(OU.concat(DirectoryUtil.getProperty(OU_OPENID_PROP, OU_OPENID_PROP_DEFAULT)));
        this.setOuAssociation(
                OU.concat(DirectoryUtil.getProperty(OU_ASSOCIATION_PROP, OU_ASSOCIATION_PROP_DEFAULT)));
        this.setOuNonce(OU.concat(DirectoryUtil.getProperty(OU_NONCE_PROP, OU_NONCE_PROP_DEFAULT)));

        //try to create parent contexes
        Attributes attrs = new BasicAttributes(true); // case-ignore
        Attribute objclass = new BasicAttribute("objectclass");
        objclass.add("top");
        objclass.add("organizationalUnit");
        attrs.put(objclass);
        DirContext baseCtx = initialCtx.createSubcontext(ouOpenID, attrs);
        baseCtx.createSubcontext(ouAssociation, attrs);
        baseCtx.createSubcontext(ouNonce, attrs);

    } catch (NameAlreadyBoundException ex) {
        //ignore
    } catch (Exception ex) {
        log.warn(ex);
        ex.printStackTrace();
    }
}

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

public void createOu(String name) {
    Attributes ou = new BasicAttributes("ou", name);
    Attribute objectClass = new BasicAttribute("objectClass");
    objectClass.add("top");
    objectClass.add("organizationalUnit");
    ou.put(objectClass);

    try {//from ww  w  .j a  va 2 s  .  co  m
        serverContext.createSubcontext("ou=" + name, ou);
    } catch (NameAlreadyBoundException ignore) {
        log.warn("ou " + name + " already exists.");
    } catch (NamingException ne) {
        log.error("Failed to create ou: ", ne);
    }
}

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