Example usage for javax.naming.ldap Rdn escapeValue

List of usage examples for javax.naming.ldap Rdn escapeValue

Introduction

In this page you can find the example usage for javax.naming.ldap Rdn escapeValue.

Prototype

public static String escapeValue(Object val) 

Source Link

Document

Given the value of an attribute, returns a string escaped according to the rules specified in <a href="http://www.ietf.org/rfc/rfc2253.txt">RFC 2253</a>.

Usage

From source file:UnescapingValues.java

public static void main(String args[]) {
    // DN with ',' (comma)
    String unformatted = "Juicy, Fruit";
    String formatted = Rdn.escapeValue(unformatted);
    System.out.println("Formatted:" + formatted);
    Object original = Rdn.unescapeValue(formatted);
    System.out.println("Original:" + original);

    // DN with a '+' (plus)
    unformatted = "true+false";
    formatted = Rdn.escapeValue(unformatted);
    System.out.println("Formatted:" + formatted);
    original = Rdn.unescapeValue(formatted);
    System.out.println("Original:" + original);

    // DN with a binary value as one one of its attribute value
    byte[] bytes = new byte[] { 1, 2, 3, 4 };
    formatted = Rdn.escapeValue(bytes);
    System.out.println("Formatted:" + formatted);
    original = Rdn.unescapeValue(formatted);
    System.out.println("Original:" + original);
}

From source file:EscapingDNs.java

public static void main(String args[]) {
    try {/*from  w ww . j av  a 2  s  . com*/
        // DN with ',' (comma) in its attribute value
        String unformatted = "Juicy, Fruit";
        String formatted = Rdn.escapeValue(unformatted);
        LdapName dn = new LdapName("cn=" + formatted);
        System.out.println("dn:" + dn);

        // DN with a '+' (plus) in its attribute value
        unformatted = "true+false";
        formatted = Rdn.escapeValue(unformatted);
        dn = new LdapName("cn=" + formatted);
        System.out.println("dn:" + dn);

        // DN with a binary value as its attribute value
        byte[] bytes = new byte[] { 1, 2, 3, 4 };
        formatted = Rdn.escapeValue(bytes);
        System.out.println("Orig val: " + bytes + " Escaped val: " + formatted);
    } catch (InvalidNameException e) {
        e.printStackTrace();
    }
}

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

public List<String> getGroups(String username, DirContext context) throws MappingException {

    List<String> userGroups = new ArrayList<String>();

    NamingEnumeration<SearchResult> namingEnumeration = null;
    try {//from  w w  w. j  av  a 2 s.  c  o m

        SearchControls searchControls = new SearchControls();

        searchControls.setDerefLinkFlag(true);
        searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
        String groupEntry = null;
        try {
            //try to look the user up
            User user = userManager.findUser(username);
            if (user instanceof LdapUser) {
                LdapUser ldapUser = LdapUser.class.cast(user);
                Attribute dnAttribute = ldapUser.getOriginalAttributes().get(getLdapDnAttribute());
                if (dnAttribute != null) {
                    groupEntry = String.class.cast(dnAttribute.get());
                }

            }
        } catch (UserNotFoundException e) {
            log.warn("Failed to look up user {}. Computing distinguished name manually", username, e);
        } catch (UserManagerException e) {
            log.warn("Failed to look up user {}. Computing distinguished name manually", username, e);
        }
        if (groupEntry == null) {
            //failed to look up the user's groupEntry directly
            StringBuilder builder = new StringBuilder();
            String posixGroup = "posixGroup";
            if (posixGroup.equals(getLdapGroupClass())) {
                builder.append(username);
            } else {
                builder.append(this.userIdAttribute).append("=").append(username).append(",")
                        .append(getBaseDn());
            }
            groupEntry = builder.toString();
        }

        String filter = new StringBuilder().append("(&").append("(objectClass=" + getLdapGroupClass() + ")")
                .append("(").append(getLdapGroupMember()).append("=").append(Rdn.escapeValue(groupEntry))
                .append(")").append(")").toString();

        log.debug("filter: {}", filter);

        namingEnumeration = context.search(getGroupsDn(), filter, searchControls);

        while (namingEnumeration.hasMore()) {
            SearchResult searchResult = namingEnumeration.next();

            List<String> allMembers = new ArrayList<String>();

            Attribute uniqueMemberAttr = searchResult.getAttributes().get(getLdapGroupMember());

            if (uniqueMemberAttr != null) {
                NamingEnumeration<String> allMembersEnum = (NamingEnumeration<String>) uniqueMemberAttr
                        .getAll();
                while (allMembersEnum.hasMore()) {

                    String userName = allMembersEnum.next();
                    //the original dn
                    allMembers.add(userName);
                    // uid=blabla we only want bla bla
                    userName = StringUtils.substringAfter(userName, "=");
                    userName = StringUtils.substringBefore(userName, ",");
                    allMembers.add(userName);
                }
                close(allMembersEnum);
            }

            if (allMembers.contains(username)) {
                String groupName = searchResult.getName();
                // cn=blabla we only want bla bla
                groupName = StringUtils.substringAfter(groupName, "=");
                userGroups.add(groupName);

            } else if (allMembers.contains(groupEntry)) {
                String groupName = searchResult.getName();
                // cn=blabla we only want bla bla
                groupName = StringUtils.substringAfter(groupName, "=");
                userGroups.add(groupName);
            }

        }

        return userGroups;
    } catch (LdapException e) {
        throw new MappingException(e.getMessage(), e);
    } catch (NamingException e) {
        throw new MappingException(e.getMessage(), e);
    } finally {
        close(namingEnumeration);
    }
}

From source file:org.dcm4che3.conf.dicom.ldap.LdapConfigUtils.java

public static String dnOf(String parentDN, String attrID, String attrValue) {
    return attrID + '=' + Rdn.escapeValue(attrValue) + ',' + parentDN;
}

From source file:org.glite.slcs.util.Utils.java

/**
 * RFC2253 compliant escaping (with a <code>\</code>) of the special characters <code>,</code>, <code>=</code>, <code>+</code>, <code><</code>,
 * <code>></code>, <code>#</code> and <code>;</code> if present in the string.
 * <br>/*from  www . j av a 2  s  . com*/
 * See RFC2253
 * 
 * @param s The string to be escaped.
 * @return The escaped string.
 * @see javax.naming.ldap.Rdn#escapeValue(Object)
 */
public static String escapeAttributeValue(String s) {
    String escaped = Rdn.escapeValue(s);
    return escaped;
}