Example usage for javax.naming.directory BasicAttributes BasicAttributes

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

Introduction

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

Prototype

public BasicAttributes() 

Source Link

Document

Constructs a new instance of Attributes.

Usage

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;
    }/*from w w w  .j  a  va2  s .c o m*/
    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:RdnConstructors.java

public static void main(String args[]) throws Exception {
    /**/* w ww .  j  a  v  a 2  s. 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:org.wso2.carbon.connector.ldap.UpdateEntry.java

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

    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 {// w  w w.ja va2  s  .c om
        DirContext context = LDAPUtils.getDirectoryContext(messageContext);

        Attributes entry = new BasicAttributes();
        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 {
            if (mode.equals(LDAPConstants.REPLACE)) {
                context.modifyAttributes(dn, DirContext.REPLACE_ATTRIBUTE, entry);
            } else if (mode.equals(LDAPConstants.ADD)) {
                context.modifyAttributes(dn, DirContext.ADD_ATTRIBUTE, entry);
            } else if (mode.equals(LDAPConstants.REMOVE)) {
                context.modifyAttributes(dn, DirContext.REMOVE_ATTRIBUTE, entry);
            }
            message.setText(LDAPConstants.SUCCESS);
            result.addChild(message);
            LDAPUtils.preparePayload(messageContext, result);
        } catch (NamingException e) { // LDAP Errors are catched
            LDAPUtils.handleErrorResponse(messageContext, LDAPConstants.ErrorConstants.UPDATE_ENTRY_ERROR, e);
            throw new SynapseException(e);
        }
    } catch (NamingException e) { //Authentication failures are catched
        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.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   w ww  .  j  a 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.springframework.ldap.support.LdapUtilsTest.java

@Test
public void testCollectAttributeValues() {
    String expectedAttributeName = "someAttribute";
    BasicAttribute expectedAttribute = new BasicAttribute(expectedAttributeName);
    expectedAttribute.add("value1");
    expectedAttribute.add("value2");

    BasicAttributes attributes = new BasicAttributes();
    attributes.put(expectedAttribute);/*from   w ww  . j a  va 2 s.c  o m*/

    LinkedList list = new LinkedList();
    LdapUtils.collectAttributeValues(attributes, expectedAttributeName, list);

    assertThat(list).hasSize(2);
    assertThat(list.get(0)).isEqualTo("value1");
    assertThat(list.get(1)).isEqualTo("value2");
}

From source file:se.vgregion.service.innovationsslussen.ldap.LdapServiceTest.java

@Test
public void newAttributesMapper() throws NamingException {
    AttributesMapper mapper = service.newAttributesMapper(Individual.class);
    Assert.assertNotNull(mapper);/* w  w  w.j  av  a 2 s  .c  o m*/
    BasicAttributes attributes = new BasicAttributes();
    attributes.put("middleName", "middleNameValue");
    attributes.put("nameNotInBean", "nameNotInBean");
    Individual result = (Individual) mapper.mapFromAttributes(attributes);
    Assert.assertEquals("middleNameValue", result.getMiddleName());
}

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

/**
 * @param user//from ww w .j  a v  a2  s .  com
 * @return
 */
public static Attributes toAttributes(LdapUser user) {
    Attributes attributes = new BasicAttributes();

    if (user.getCommonName() != null)
        attributes.put(new BasicAttribute(RdnType.CN.toString(), user.getCommonName()));

    if (user.getGivenName() != null)
        attributes.put(new BasicAttribute(RdnType.GIVEN_NAME.toString(), user.getGivenName()));

    if (user.getSurname() != null)
        attributes.put(new BasicAttribute(RdnType.SN.toString(), user.getSurname()));

    if (user.getUserId() != null)
        attributes.put(new BasicAttribute(RdnType.UID.toString(), user.getUserId()));

    if (user.getEmail() != null)
        attributes.put(new BasicAttribute(RdnType.MAIL.toString(), user.getEmail()));

    if (user.getPassword() != null)
        attributes.put(new BasicAttribute(RdnType.PASSWORD.toString(), user.getPassword()));

    return attributes;
}

From source file:org.springframework.ldap.support.LdapUtilsTest.java

@Test
public void testCollectAttributeValuesThrowsExceptionWhenAttributeNotPresent() {
    String expectedAttributeName = "someAttribute";
    BasicAttributes attributes = new BasicAttributes();

    LinkedList list = new LinkedList();
    try {/*w w  w.java  2s.c  om*/
        LdapUtils.collectAttributeValues(attributes, expectedAttributeName, list);
        fail("NoSuchAttributeException expected");
    } catch (NoSuchAttributeException expected) {
        assertThat(true).isTrue();
    }
}

From source file:com.surevine.ldap2alfresco.ProfileUpdater.java

/**
 * Update the profile fields into LDAP for a single user.
 * @param ldap To connect to LDAP//from  ww w  . j a va 2 s . com
 * @param username User to update
 * @param fields Contains entries for every field listed in properties file
 * @throws LdapException On any LDAP errors
 */
public void updateSingleUserToLdap(final LdapConnector ldap, final String username, final JSONObject fields)
        throws LdapException {

    Attributes attributes = new BasicAttributes();

    // encode each profile field in turn
    Iterator<ProfileFieldConverter> fieldIter = profileFields.iterator();

    while (fieldIter.hasNext()) {
        ProfileFieldConverter converter = fieldIter.next();
        converter.toAttributes(attributes, fields);
    }

    ldap.updateUser(username, attributes);
}

From source file:egovframework.com.ext.ldapumt.service.impl.OrgManageLdapDAO.java

/**
 * ? ldap? ./*  w  ww  .ja  va2  s .  c o  m*/
 * @param vo  vo
 * @param attr
 */
protected void insertOrgManage(LdapObject vo, BasicAttribute attr) {
    final Attributes attrs = new BasicAttributes();
    attrs.put(attr);

    introspect(vo, new Executable() {
        @Override
        public void execute(String key, Object value) {
            attrs.put(key, value);
        }
    });

    ldapTemplate.bind(vo.getDn(), null, attrs);
}