| | |
| | | |
| | | package org.forgerock.opendj.rest2ldap; |
| | | |
| | | import static org.forgerock.opendj.rest2ldap.Utils.getAttributeName; |
| | | import static org.forgerock.opendj.rest2ldap.Utils.attributeToJson; |
| | | import static org.forgerock.opendj.rest2ldap.Utils.toLowerCase; |
| | | |
| | | import java.util.LinkedHashMap; |
| | | import java.util.Collections; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Set; |
| | |
| | | /** |
| | | * |
| | | */ |
| | | public final class SimpleAttributeMapper implements AttributeMapper { |
| | | public class SimpleAttributeMapper implements AttributeMapper { |
| | | |
| | | // All user attributes by default. |
| | | private final Map<String, String> includedAttributes = new LinkedHashMap<String, String>(); |
| | | private final Map<String, String> excludedAttributes = new LinkedHashMap<String, String>(); |
| | | private final String ldapAttributeName; |
| | | private final String jsonAttributeName; |
| | | private final String normalizedJsonAttributeName; |
| | | |
| | | public SimpleAttributeMapper() { |
| | | // No implementation required. |
| | | /** |
| | | * Creates a new simple attribute mapper which maps a single LDAP attribute |
| | | * to an entry. |
| | | * |
| | | * @param attributeName |
| | | * The name of the simple JSON and LDAP attribute. |
| | | */ |
| | | public SimpleAttributeMapper(String attributeName) { |
| | | this(attributeName, attributeName); |
| | | } |
| | | |
| | | public SimpleAttributeMapper includeAttribute(String... attributes) { |
| | | for (String attribute : attributes) { |
| | | includedAttributes.put(toLowerCase(attribute), attribute); |
| | | } |
| | | return this; |
| | | /** |
| | | * Creates a new simple attribute mapper which maps a single LDAP attribute |
| | | * to an entry. |
| | | * |
| | | * @param jsonAttributeName |
| | | * The name of the simple JSON attribute. |
| | | * @param ldapAttributeName |
| | | * The name of the LDAP attribute. |
| | | */ |
| | | public SimpleAttributeMapper(String jsonAttributeName, String ldapAttributeName) { |
| | | this.jsonAttributeName = jsonAttributeName; |
| | | this.normalizedJsonAttributeName = toLowerCase(jsonAttributeName); |
| | | this.ldapAttributeName = ldapAttributeName; |
| | | } |
| | | |
| | | public SimpleAttributeMapper excludeAttribute(String... attributes) { |
| | | for (String attribute : attributes) { |
| | | excludedAttributes.put(toLowerCase(attribute), attribute); |
| | | } |
| | | return this; |
| | | } |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | public void getLDAPAttributes(Set<String> ldapAttributes) { |
| | | if (!includedAttributes.isEmpty()) { |
| | | ldapAttributes.addAll(includedAttributes.values()); |
| | | } else { |
| | | // All user attributes. |
| | | ldapAttributes.add("*"); |
| | | } |
| | | ldapAttributes.add(ldapAttributeName); |
| | | } |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | public void getLDAPAttributes(Set<String> ldapAttributes, JsonPointer resourceAttribute) { |
| | | String name = resourceAttribute.leaf(); |
| | | if (name != null) { |
| | | if (isIncludedAttribute(name)) { |
| | | ldapAttributes.add(name); |
| | | } else { |
| | | // FIXME: log something or return a ResourceException? |
| | | } |
| | | if (toLowerCase(resourceAttribute.leaf()).equals(normalizedJsonAttributeName)) { |
| | | ldapAttributes.add(ldapAttributeName); |
| | | } |
| | | } |
| | | |
| | | public void toJson(Context c, Entry e, AttributeMapperCompletionHandler<Map<String, Object>> h) { |
| | | Map<String, Object> result = new LinkedHashMap<String, Object>(e.getAttributeCount()); |
| | | for (Attribute a : e.getAllAttributes()) { |
| | | String name = getAttributeName(a); |
| | | if (isIncludedAttribute(name)) { |
| | | result.put(name, Utils.attributeToJson(a)); |
| | | } |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | public void toJson(Context c, Entry e, |
| | | final AttributeMapperCompletionHandler<Map<String, Object>> h) { |
| | | Attribute a = e.getAttribute(ldapAttributeName); |
| | | if (a != null) { |
| | | Map<String, Object> result = |
| | | Collections.singletonMap(jsonAttributeName, attributeToJson(a)); |
| | | h.onSuccess(result); |
| | | } |
| | | h.onSuccess(result); |
| | | } |
| | | |
| | | private boolean isIncludedAttribute(String name) { |
| | | String lowerName = toLowerCase(name); |
| | | |
| | | // Ignore the requested attribute if it has been excluded. |
| | | if (excludedAttributes.containsKey(lowerName)) { |
| | | return false; |
| | | } |
| | | |
| | | // Include all attributes by default. |
| | | if (includedAttributes.isEmpty() || includedAttributes.containsKey(lowerName)) { |
| | | return true; |
| | | } |
| | | |
| | | return false; |
| | | } |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | public void toLDAP(Context c, JsonValue v, AttributeMapperCompletionHandler<List<Attribute>> h) { |
| | | // TODO: |
| | | // TODO Auto-generated method stub |
| | | |
| | | } |
| | | |
| | | } |