package org.argeo.osgi.useradmin;
-import java.util.StringJoiner;
-
import javax.naming.InvalidNameException;
import javax.naming.ldap.LdapName;
import javax.naming.ldap.Rdn;
/** Utilities to simplify using {@link LdapName}. */
-public class LdapNameUtils {
-
- public static String toRevertPath(String dn, String prefix) {
- if (!dn.endsWith(prefix))
- throw new IllegalArgumentException("Prefix " + prefix + " not consistent with " + dn);
- String relativeName = dn.substring(0, dn.length() - prefix.length() - 1);
- LdapName name = toLdapName(relativeName);
- StringJoiner path = new StringJoiner("/");
- for (int i = 0; i < name.size(); i++) {
- path.add(name.get(i));
+class LdapNameUtils {
+
+ static LdapName relativeName(LdapName prefix, LdapName dn) {
+ try {
+ if (!dn.startsWith(prefix))
+ throw new IllegalArgumentException("Prefix " + prefix + " not consistent with " + dn);
+ LdapName res = (LdapName) dn.clone();
+ for (int i = 0; i < prefix.size(); i++) {
+ res.remove(0);
+ }
+ return res;
+ } catch (InvalidNameException e) {
+ throw new IllegalStateException("Cannot find realtive name", e);
}
- return path.toString();
}
- public static LdapName getParent(LdapName dn) {
+ static LdapName getParent(LdapName dn) {
try {
LdapName parent = (LdapName) dn.clone();
parent.remove(parent.size() - 1);
}
}
- public static LdapName toLdapName(String distinguishedName) {
+ static LdapName toLdapName(String distinguishedName) {
try {
return new LdapName(distinguishedName);
} catch (InvalidNameException e) {
}
}
- public static Rdn getLastRdn(LdapName dn) {
+ static Rdn getLastRdn(LdapName dn) {
return dn.getRdn(dn.size() - 1);
}
- public static String getLastRdnAsString(LdapName dn) {
+ static String getLastRdnAsString(LdapName dn) {
return getLastRdn(dn).toString();
}
- public static String getLastRdnValue(LdapName dn) {
+ static String getLastRdnValue(LdapName dn) {
return getLastRdn(dn).getValue().toString();
}