X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=org.argeo.security.core%2Fsrc%2Forg%2Fargeo%2Fosgi%2Fuseradmin%2FLdifParser.java;h=da793adcebec4b05ff159c1b21d47ed4131ae64a;hb=0b8aa4c76cb7a1d19abf93a4c1ae0c973abdab5b;hp=b59cc36e1bd1ee0714bb92d0ef0f387b124d8329;hpb=d49ef557ceaa438399743027dcfd2c1b2e257126;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.security.core/src/org/argeo/osgi/useradmin/LdifParser.java b/org.argeo.security.core/src/org/argeo/osgi/useradmin/LdifParser.java index b59cc36e1..da793adce 100644 --- a/org.argeo.security.core/src/org/argeo/osgi/useradmin/LdifParser.java +++ b/org.argeo.security.core/src/org/argeo/osgi/useradmin/LdifParser.java @@ -1,5 +1,7 @@ package org.argeo.osgi.useradmin; +import static org.argeo.osgi.useradmin.LdifName.dn; + import java.io.IOException; import java.io.InputStream; import java.util.List; @@ -24,6 +26,32 @@ import org.apache.commons.logging.LogFactory; class LdifParser { private final static Log log = LogFactory.getLog(LdifParser.class); + protected Attributes addAttributes(SortedMap res, + int lineNumber, LdapName currentDn, Attributes currentAttributes) { + try { + Rdn nameRdn = currentDn.getRdn(currentDn.size() - 1); + Attribute nameAttr = currentAttributes.get(nameRdn.getType()); + if (nameAttr == null) + currentAttributes.put(nameRdn.getType(), nameRdn.getValue()); + else if (!nameAttr.get().equals(nameRdn.getValue())) + throw new UserDirectoryException("Attribute " + + nameAttr.getID() + "=" + nameAttr.get() + + " not consistent with DN " + currentDn + + " (shortly before line " + lineNumber + + " in LDIF file)"); + Attributes previous = res.put(currentDn, currentAttributes); + if (log.isTraceEnabled()) + log.trace("Added " + currentDn); + return previous; + } catch (NamingException e) { + throw new UserDirectoryException("Cannot add " + currentDn, e); + } + } + + static void checkDnConsistency() { + + } + SortedMap read(InputStream in) throws IOException { SortedMap res = new TreeMap(); try { @@ -70,37 +98,13 @@ class LdifParser { .decodeBase64(cleanValueStr) : cleanValueStr; // manage DN attributes - if (attributeId.equals("dn") || isLastLine) { + if (attributeId.equals(dn.name()) || isLastLine) { if (currentDn != null) { // // ADD // - Rdn nameRdn = currentDn - .getRdn(currentDn.size() - 1); - Attribute nameAttr = currentAttributes.get(nameRdn - .getType()); - if (nameAttr == null) - currentAttributes.put(nameRdn.getType(), - nameRdn.getValue()); - else - try { - if (!nameAttr.get().equals( - nameRdn.getValue())) - throw new UserDirectoryException( - "Attribute " - + nameAttr.getID() - + "=" - + nameAttr.get() - + " not consistent with DN " - + currentDn); - } catch (NamingException e) { - throw new UserDirectoryException( - "Cannot get attribute value", e); - } - Attributes previous = res.put(currentDn, - currentAttributes); - if (log.isTraceEnabled()) - log.trace("Added " + currentDn); + Attributes previous = addAttributes(res, + lineNumber, currentDn, currentAttributes); if (previous != null) { log.warn("There was already an entry with DN " + currentDn @@ -108,11 +112,11 @@ class LdifParser { } } - if (attributeId.equals("dn")) + if (attributeId.equals(dn.name())) try { currentDn = new LdapName( attributeValue.toString()); - currentAttributes = new BasicAttributes(); + currentAttributes = new BasicAttributes(true); } catch (InvalidNameException e) { log.error(attributeValue + " not a valid DN, skipping the entry.");