Calling an Object parameter ArrayList and converting it to a String in CSV format

I got some code for our WSO2 Custom Claims that is all working right. Though I would like to add or alter a part of it. When I make a call using the identifiers.getRolesCSV() it will print out like a String, but if you try to use it as a variable it sees it as an ArrayList still. Or another way to say it is, right now the roles being passed from WSO2 is an array that looks like this below.

"roles": [
"Admin",
" Software Engineer",
" Current User",
],

I would like to change it to be just be a comma separated string like this.

"roles": "Admin, Software Engineer, Current User"

The second one is what is being returned in the ID token.

Here is some code below.

Code

    private Map<String, String> addClaims(String subscriberId, Map<String, String> map) {

    if (subscriberId == null) {
        return map;
    }

    Entity identifiers = null;

    identifiers = EntityHelper.getEntityFromUserName(subscriberId);
    if (identifiers == null) {
        return map;
    }

    map.put("roles", identifiers.getRolesCSV());

    return map;
}

public class Entity {
List roles = new ArrayList();


public Entity() {
}

public String getRolesCSV() {

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < roles.size(); i++) {
        sb.append(roles.get(i));

        if (i != roles.size() - 1)
        {
            sb.append(", ");
        }

    }

    return sb.toString();     
}

public class EntityHelper {

private final LdapHelp ldapHelper = new LdapHelp();

public EntityHelper() {

}

public Entity getEntityFromUserName(String netid) {

    Attributes attributes = null;
    Entity domainEntity = new Entity();

    try {
        attributes = ldapHelper.getAttributesForUsername(netid, Domain.*local*);

        if (attributes != null) {

            List roles = getAttributeList(attributes.get(LdapHelp.MEMBER_OF_ATTRIBUTE), "CN");
            domainEntity.setRoles(roles);

        } else {
            if (log.isDebugEnabled()) {
                log.debug("no attributes returned");
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return domainEntity;
}

Answer

So what it looks like is the code is working right. When are code builds the token it prints it out right. Its when wso2 builds the claims it prints it out the other way. Point is it works right just looked like it worked wrong.

Leave a Reply

Your email address will not be published. Required fields are marked *