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:Main.java

public static void main(String[] argv) throws Exception {
    Attributes attrs = new BasicAttributes(true);
    Attribute objclass = new BasicAttribute("objectclass");
    objclass.add("top");
    objclass.add("extensible");
    attrs.put(objclass);/*  w  ww.ja  v  a  2  s .c om*/

    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "yourURL");

    DirContext ctx = new InitialDirContext(env);
    Context entry = ctx.createSubcontext("cn=Sample", attrs);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Attributes attrs = new BasicAttributes(true);
    Attribute objclass = new BasicAttribute("objectclass");
    objclass.add("top");
    objclass.add("extensible");
    attrs.put(objclass);/*ww  w  .j  av a 2 s .com*/

    Object obj = "yourObject";
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "yourURL");

    DirContext ctx = new InitialDirContext(env);

    ctx.bind("cn=Sample", obj, attrs);
}

From source file:AttributeExample.java

public static void main(String[] argc) throws Exception {
    Hashtable env = new Hashtable(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");

    env.put(Context.PROVIDER_URL, "ldap://MyHost/o=JNDIExample");
    DirContext dctx = new InitialDirContext(env);
    Attributes attrs = new BasicAttributes(true);
    attrs.put(new BasicAttribute("email"));
    attrs.put(new BasicAttribute("website", "www.pri.com"));

    NamingEnumeration result = dctx.search("ou=People", attrs);

    while (result.hasMore()) {
        SearchResult sr = (SearchResult) result.next();
        System.out.println("Result = " + sr.getName());
        Attributes srchAttrs = sr.getAttributes();

        NamingEnumeration attributes = srchAttrs.getAll();

        while (attributes.hasMore()) {
            Attribute attr = (Attribute) attributes.next();
            System.out.println("Attribute: " + attr.getID());
            NamingEnumeration values = attr.getAll();
            while (values.hasMore()) {
                Object value = values.next();
                System.out.println("Value = " + value);
            }/*from  w ww.  j  av  a  2s.  co m*/
        }
    }
}

From source file:Create.java

public static void main(String[] args) {

    // Set up the environment for creating the initial context
    Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");

    try {// w  ww  . j a  va 2s.c o  m
        // Create the initial context
        DirContext ctx = new InitialDirContext(env);

        // Create attributes to be associated with the new context
        Attributes attrs = new BasicAttributes(true); // case-ignore
        Attribute objclass = new BasicAttribute("objectclass");
        objclass.add("top");
        objclass.add("organizationalUnit");
        attrs.put(objclass);

        // Create the context
        Context result = ctx.createSubcontext("ou=NewOu", attrs);

        // Check that it was created by listing its parent
        NamingEnumeration list = ctx.list("");

        // Go through each item in list
        while (list.hasMore()) {
            NameClassPair nc = (NameClassPair) list.next();
            System.out.println(nc);
        }

        // Close the contexts when we're done
        result.close();
        ctx.close();
    } catch (NamingException e) {
        System.out.println("Create failed: " + e);
    }
}

From source file:nl.nn.adapterframework.ldap.BasicAttributeFactory.java

public Object createObject(Attributes arg0) throws Exception {
    return new BasicAttribute(arg0.getValue(0));
}

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

public static <T> Attribute create(String id, Collection<?> values, Evaluator<T> evaluator) {
    if (StringUtils.isEmpty(id) || CollectionUtils.isEmpty(values)) {
        return null;
    }/*www .ja  v a  2s .  co  m*/

    boolean hasOneNotNullAtLeast = false;
    Attribute attr = new BasicAttribute(id);

    if (null == evaluator) {
        for (Object value : values) {
            if (null != value) {
                hasOneNotNullAtLeast = true;
                attr.add(value);
            }
        }

    } else {
        for (Object value : values) {
            if (null == value) {
                continue;
            }
            T evaled = evaluator.eval(value);
            if (null != evaled) {
                hasOneNotNullAtLeast = true;
                attr.add(evaled);
            }
        }
    }

    return hasOneNotNullAtLeast ? attr : null;
}

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.  ja va2s . 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: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 {//  ww w . j a  v a2s  .  c  o m
        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.swordess.ldap.util.ModUtils.java

public static ModificationItem remove(String id) {
    return new ModificationItem(DirContext.REMOVE_ATTRIBUTE, new BasicAttribute(id));
}

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

public static <T> Attribute create(String id, Object[] values, Evaluator<T> evaluator) {
    if (StringUtils.isEmpty(id) || ArrayUtils.isEmpty(values)) {
        return null;
    }/*from   w w  w. j  a va  2  s .com*/

    boolean hasOneNotNullAtLeast = false;
    Attribute attr = new BasicAttribute(id);

    if (null == evaluator) {
        for (Object value : values) {
            if (null != value) {
                hasOneNotNullAtLeast = true;
                attr.add(value);
            }
        }

    } else {
        for (Object value : values) {
            if (null == value) {
                continue;
            }
            T evaled = evaluator.eval(value);
            if (null != evaled) {
                hasOneNotNullAtLeast = true;
                attr.add(evaled);
            }
        }
    }

    return hasOneNotNullAtLeast ? attr : null;
}