X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.util%2Fsrc%2Forg%2Fargeo%2Futil%2Fdirectory%2Fldap%2FLdapEntry.java;h=4657c8798e41fdf40fe9b37110b593e041a7a14a;hb=3c1cdc594d954520b14646102b366290bdad58c7;hp=3fa23e5f1bd32a5851892bd7e0b29df1f52d0fd3;hpb=dc27b57704278684e72efcaf72b01c5b91df39f8;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.util/src/org/argeo/util/directory/ldap/LdapEntry.java b/org.argeo.util/src/org/argeo/util/directory/ldap/LdapEntry.java index 3fa23e5f1..4657c8798 100644 --- a/org.argeo.util/src/org/argeo/util/directory/ldap/LdapEntry.java +++ b/org.argeo.util/src/org/argeo/util/directory/ldap/LdapEntry.java @@ -1,10 +1,20 @@ package org.argeo.util.directory.ldap; +import java.util.Arrays; +import java.util.Collection; +import java.util.Dictionary; import java.util.List; +import java.util.Locale; +import java.util.Set; +import java.util.StringJoiner; +import java.util.TreeSet; import javax.naming.directory.Attributes; import javax.naming.ldap.LdapName; +import org.argeo.util.naming.LdapAttrs; + +/** An LDAP entry. */ public interface LdapEntry { LdapName getDn(); @@ -12,5 +22,44 @@ public interface LdapEntry { void publishAttributes(Attributes modifiedAttributes); - public List getReferences(String attributeId); + List getReferences(String attributeId); + + Dictionary getProperties(); + + boolean hasCredential(String key, Object value); + + /* + * UTILITIES + */ + /** + * Convert a collection of object classes to the format expected by an LDAP + * backend. + */ + public static void addObjectClasses(Dictionary properties, Collection objectClasses) { + String value = properties.get(LdapAttrs.objectClasses.name()).toString(); + Set currentObjectClasses = new TreeSet<>(Arrays.asList(value.toString().split("\n"))); + currentObjectClasses.addAll(objectClasses); + StringJoiner values = new StringJoiner("\n"); + currentObjectClasses.forEach((s) -> values.add(s)); + properties.put(LdapAttrs.objectClasses.name(), values.toString()); + } + + public static Object getLocalized(Dictionary properties, String key, Locale locale) { + if (locale == null) + return null; + Object value = null; + value = properties.get(key + ";lang-" + locale.getLanguage() + "-" + locale.getCountry()); + if (value == null) + value = properties.get(key + ";lang-" + locale.getLanguage()); + return value; + } + + public static String toLocalizedKey(String key, Locale locale) { + String country = locale.getCountry(); + if ("".equals(country)) { + return key + ";lang-" + locale.getLanguage(); + } else { + return key + ";lang-" + locale.getLanguage() + "-" + locale.getCountry(); + } + } }