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(String attrID, Object val);

Source Link

Document

Adds a new attribute to the attribute set.

Usage

From source file:RdnConstructors.java

public static void main(String args[]) throws Exception {
    /**/*from w  w  w  . j  ava2s  . c  o m*/
     * There are four ways of constructing an Rdn
     */
    Rdn rdn1 = new Rdn("ou= Juicy\\, Fruit");
    System.out.println("rdn1:" + rdn1.toString());

    Rdn rdn2 = new Rdn(rdn1);
    System.out.println("rdn2:" + rdn2.toString());

    Attributes attrs = new BasicAttributes();
    attrs.put("ou", "Juicy, Fruit");
    attrs.put("cn", "Mango");
    Rdn rdn3 = new Rdn(attrs);
    System.out.println("rdn3:" + rdn3.toString());

    Rdn rdn4 = new Rdn("ou", "Juicy, Fruit");
    System.out.println("rdn4:" + rdn4.toString());
}

From source file:RdnGetters.java

public static void main(String args[]) throws Exception {
    Attributes attrs = new BasicAttributes();
    attrs.put("o", "Yellow");
    attrs.put("cn", "Mango");

    byte[] mangoJuice = new byte[6];
    for (int i = 0; i < mangoJuice.length; i++) {
        mangoJuice[i] = (byte) i;
    }//w w w  . j a  va2 s . c  om
    attrs.put("ou", mangoJuice);
    Rdn rdn = new Rdn(attrs);

    System.out.println();
    System.out.println("size:" + rdn.size());
    System.out.println("getType(): " + rdn.getType());
    System.out.println("getValue(): " + rdn.getValue());

    // test toAttributes
    System.out.println();
    System.out.println("toAttributes(): " + rdn.toAttributes());
}

From source file:org.swordess.ldap.util.AttrUtils.java

/**
 * Construct attributes whose ids and values are specified in ordering.
 * <p/>// ww w.j  a v  a2  s .co m
 * NOTE: The length of <tt>ids</tt> and <tt>values</tt> must be equal, otherwise a RuntimeException will be thrown.
 * 
 * @param ids
 *            ids in ordering
 * @param values
 *            values in ordering
 * @return
 */
public static Attributes create(String[] ids, Object[] values) {
    if (ArrayUtils.isEmpty(ids) || ArrayUtils.isEmpty(values) || ids.length != values.length) {
        throw new RuntimeException("length of ids and values are not match");
    }

    Attributes attrs = new BasicAttributes();
    for (int i = 0; i < ids.length; i++) {
        attrs.put(ids[i], values[i]);
    }
    return attrs;
}

From source file:org.tolven.gatekeeper.CertificateHelper.java

public static X500Principal getX500Principal(String email, String commonName, String organizationUnitName,
        String organizationName, String stateOrProvince) {
    if (null == email || null == commonName || null == organizationUnitName || null == organizationName
            || null == stateOrProvince) {
        throw new RuntimeException(
                "Certificate requires EmailAddress, Common Name, organizationUnitName, organizationName, stateOrProvince");
    }//from   ww w  . jav  a  2s .  co  m
    Attributes attributes = new BasicAttributes();
    attributes.put(X509Name.EmailAddress.toString(), email);
    attributes.put(X509Name.CN.toString(), commonName);
    attributes.put(X509Name.OU.toString(), organizationUnitName);
    attributes.put(X509Name.O.toString(), organizationName);
    attributes.put(X509Name.ST.toString(), stateOrProvince);
    Rdn rdn;
    try {
        rdn = new Rdn(attributes);
    } catch (InvalidNameException ex) {
        throw new RuntimeException("Failed to obtain a Relative Distinguised Name", ex);
    }
    return new X500Principal(rdn.toString());
}

From source file:org.projectforge.business.ldap.LdapUtils.java

public static Attribute putAttribute(final Attributes attributes, final String attrId, final String attrValue) {
    final Attribute attr = attributes.get(attrId);
    if (attrValue == null) {
        return attr;
    }/*from   w  ww .j av a 2  s .c  o  m*/
    if (attr == null) {
        return attributes.put(attrId, attrValue);
    }
    attr.add(attrValue);
    return attr;
}

From source file:org.apache.directory.studio.ldapbrowser.core.jobs.ImportLdifRunnable.java

/**
 * Imports the LDIF record./*from   www  .  jav  a 2 s  .  c o  m*/
 * 
 * @param browserConnection the browser connection
 * @param record the LDIF record
 * @param updateIfEntryExists the update if entry exists flag
 * @param monitor the progress monitor
 * 
 * @throws NamingException the naming exception
 * @throws LdapInvalidDnException
 */
static void importLdifRecord(IBrowserConnection browserConnection, LdifRecord record,
        boolean updateIfEntryExists, StudioProgressMonitor monitor)
        throws NamingException, LdapInvalidDnException {
    if (!record.isValid()) {
        throw new NamingException(
                BrowserCoreMessages.bind(BrowserCoreMessages.model__invalid_record, record.getInvalidString()));
    }

    String dn = record.getDnLine().getValueAsString();

    if (record instanceof LdifContentRecord || record instanceof LdifChangeAddRecord) {
        LdifAttrValLine[] attrVals;
        IEntry dummyEntry;
        if (record instanceof LdifContentRecord) {
            LdifContentRecord attrValRecord = (LdifContentRecord) record;
            attrVals = attrValRecord.getAttrVals();
            try {
                dummyEntry = ModelConverter.ldifContentRecordToEntry(attrValRecord, browserConnection);
            } catch (LdapInvalidDnException e) {
                monitor.reportError(e);
                return;
            }
        } else {
            LdifChangeAddRecord changeAddRecord = (LdifChangeAddRecord) record;
            attrVals = changeAddRecord.getAttrVals();
            try {
                dummyEntry = ModelConverter.ldifChangeAddRecordToEntry(changeAddRecord, browserConnection);
            } catch (LdapInvalidDnException e) {
                monitor.reportError(e);
                return;
            }
        }

        Attributes jndiAttributes = new BasicAttributes();
        for (LdifAttrValLine attrVal : attrVals) {
            String attributeName = attrVal.getUnfoldedAttributeDescription();
            Object realValue = attrVal.getValueAsObject();

            if (jndiAttributes.get(attributeName) != null) {
                jndiAttributes.get(attributeName).add(realValue);
            } else {
                jndiAttributes.put(attributeName, realValue);
            }
        }

        browserConnection.getConnection().getConnectionWrapper().createEntry(dn, jndiAttributes,
                getControls(record), monitor, null);

        if (monitor.errorsReported() && updateIfEntryExists
                && monitor.getException() instanceof NameAlreadyBoundException) {
            // creation failed with Error 68, now try to update the existing entry
            monitor.reset();

            ModificationItem[] mis = ModelConverter.entryToReplaceModificationItems(dummyEntry);
            browserConnection.getConnection().getConnectionWrapper().modifyEntry(dn, mis, getControls(record),
                    monitor, null);
        }
    } else if (record instanceof LdifChangeDeleteRecord) {
        LdifChangeDeleteRecord changeDeleteRecord = (LdifChangeDeleteRecord) record;
        browserConnection.getConnection().getConnectionWrapper().deleteEntry(dn,
                getControls(changeDeleteRecord), monitor, null);
    } else if (record instanceof LdifChangeModifyRecord) {
        LdifChangeModifyRecord modifyRecord = (LdifChangeModifyRecord) record;
        LdifModSpec[] modSpecs = modifyRecord.getModSpecs();
        ModificationItem[] mis = new ModificationItem[modSpecs.length];
        for (int ii = 0; ii < modSpecs.length; ii++) {
            LdifModSpecTypeLine modSpecType = modSpecs[ii].getModSpecType();
            LdifAttrValLine[] attrVals = modSpecs[ii].getAttrVals();

            Attribute attribute = new BasicAttribute(modSpecType.getUnfoldedAttributeDescription());
            for (int x = 0; x < attrVals.length; x++) {
                attribute.add(attrVals[x].getValueAsObject());
            }

            if (modSpecType.isAdd()) {
                mis[ii] = new ModificationItem(DirContext.ADD_ATTRIBUTE, attribute);
            } else if (modSpecType.isDelete()) {
                mis[ii] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, attribute);
            } else if (modSpecType.isReplace()) {
                mis[ii] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, attribute);
            }
        }

        browserConnection.getConnection().getConnectionWrapper().modifyEntry(dn, mis, getControls(modifyRecord),
                monitor, null);
    } else if (record instanceof LdifChangeModDnRecord) {
        LdifChangeModDnRecord modDnRecord = (LdifChangeModDnRecord) record;
        if (modDnRecord.getNewrdnLine() != null && modDnRecord.getDeloldrdnLine() != null) {
            String newRdn = modDnRecord.getNewrdnLine().getValueAsString();
            boolean deleteOldRdn = modDnRecord.getDeloldrdnLine().isDeleteOldRdn();

            Dn newDn;
            if (modDnRecord.getNewsuperiorLine() != null) {
                newDn = new Dn(newRdn, modDnRecord.getNewsuperiorLine().getValueAsString());
            } else {
                Dn dnObject = new Dn(dn);
                Dn parent = dnObject.getParent();
                newDn = new Dn(newRdn, parent.getName());
            }

            browserConnection.getConnection().getConnectionWrapper().renameEntry(dn, newDn.toString(),
                    deleteOldRdn, getControls(modDnRecord), monitor, null);
        }
    }
}

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);/*from   w ww.j a va  2s  .  c  om*/
    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:org.nuxeo.ecm.directory.ldap.MockLdapServer.java

public void createManagerUser() {
    Attributes user = new BasicAttributes("cn", "manager", true);
    user.put("userPassword", "secret");

    Attribute objectClass = new BasicAttribute("objectClass");
    user.put(objectClass);/*from   w  ww.ja  v  a2s.c o m*/
    objectClass.add("top");
    objectClass.add("person");
    objectClass.add("organizationalPerson");
    objectClass.add("inetOrgPerson");
    user.put("sn", "Manager");
    user.put("cn", "manager");

    try {
        serverContext.createSubcontext("cn=manager", user);
    } catch (NameAlreadyBoundException ignore) {
        log.warn("Manager user already exists.");
    } catch (NamingException ne) {
        log.error("Failed to create manager user", ne);
    }
}

From source file:org.exoplatform.services.organization.DummyLDAPServiceImpl.java

private void addNewSchema() throws NamingException {
    DirContext ctx = getLdapContext();
    try {/*from   w  w  w.  j  a v a2  s . co m*/
        Attributes atAttrs = new BasicAttributes(true);
        atAttrs.put("attributeTypes",
                "( 1.2.840.113556.1.4.8 NAME 'userAccountControl' DESC 'Flags that control the behavior of the user account' EQUALITY integerMatch SYNTAX '1.3.6.1.4.1.1466.115.121.1.27' SINGLE-VALUE )");
        ctx.modifyAttributes("cn=schema", DirContext.ADD_ATTRIBUTE, atAttrs);
        Attributes ocAttrs = new BasicAttributes(true);
        ocAttrs.put("objectClasses",
                "( 1.2.840.113556.1.5.9 NAME 'user' SUP inetOrgPerson STRUCTURAL MAY (userAccountControl) )");
        ctx.modifyAttributes("cn=schema", DirContext.ADD_ATTRIBUTE, ocAttrs);
    } finally {
        ctx.close();
    }
}

From source file:org.apache.archiva.redback.common.ldap.user.LdapUserMapper.java

public Attributes getCreationAttributes(User user, boolean encodePasswordIfChanged) throws MappingException {
    Attributes userAttrs = new BasicAttributes();

    boolean passwordSet = false;

    if (!passwordSet && (user.getEncodedPassword() != null)) {
        userAttrs.put(getPasswordAttribute(), user.getEncodedPassword());
    }// www  .j  a  va  2  s . c  o  m

    if (!StringUtils.isEmpty(user.getFullName())) {
        userAttrs.put(getUserFullNameAttribute(), user.getFullName());
    }

    if (!StringUtils.isEmpty(user.getEmail())) {
        userAttrs.put(getEmailAddressAttribute(), user.getEmail());
    }

    return userAttrs;
}