操纵相对专有名称(RDN)

原文: https://docs.oracle.com/javase/tutorial/jndi/newstuff/rdn.html

javax.naming.ldap.Rdn表示 RFC 2253 中指定的相对可分辨名称(RDN)。 RDN 代表 DN 的一个组成部分,如 Manipulating LdapName 课程中所述。 RDN 由类型和值对组成。 RDN 的示例是:

  • OU =太阳
  • OU =销售额+ CN = J.Smith 。 上面的例子显示了多值 RDN 的表示。

Rdn 类提供访问 RDN 的名称/值对的方法,获取其字符串表示,检索 属性 查看,比较和确定 RDN 的相等性以及要转义和忽略 RDN 值部分的方法。

Rdn类是不可变的。

构建一个 Rdn

如果 Rdn 是单个名称/值配对 RDN,则可以使用指定的名称和值对构造 Rdn。对于多值 RDN,您应该创建一个由所有名称/值对组成的属性集,并使用以Attributes作为参数的构造器。您还可以从 RFC 2253 中指定的字符串表示创建 Rdn。最后,您可以使用其复制构造器克隆 Rdn。这是 example ,它使用不同类型的构造器创建 RDN。

  1. Rdn rdn1 = new Rdn("ou= Juicy\\, Fruit");
  2. System.out.println("rdn1:" + rdn1.toString());
  3. Rdn rdn2 = new Rdn(rdn1);
  4. System.out.println("rdn2:" + rdn2.toString());
  5. Attributes attrs = new BasicAttributes();
  6. attrs.put("ou", "Juicy, Fruit");
  7. attrs.put("cn", "Mango");
  8. Rdn rdn3 = new Rdn(attrs);
  9. System.out.println("rdn3:" + rdn3.toString());
  10. Rdn rdn4 = new Rdn("ou", "Juicy, Fruit");
  11. System.out.println("rdn4:" + rdn4.toString());

访问 RDN 的类型/值对

可以使用以下方法获得和 RDN 的类型/值:

getType() getValue() toAttributes()

对于由单一类型/值对组成的 RDN, getType() 方法返回类型和 getValue() method 返回 RDN 的值。方法 toAttributes() 返回类型/值对的属性视图。下面的 example 打印 RDN 的类型/值对。

  1. Attributes attrs = new BasicAttributes();
  2. attrs.put("o", "Yellow");
  3. attrs.put("cn", "Mango");
  4. // create a binary value for the RDN
  5. byte[] mangoJuice = new byte[6];
  6. for (int i = 0; i < mangoJuice.length; i++) {
  7. mangoJuice[i] = (byte) i;
  8. }
  9. attrs.put("ou", mangoJuice);
  10. Rdn rdn = new Rdn(attrs);
  11. System.out.println();
  12. System.out.println("size:" + rdn.size());
  13. System.out.println("getType(): " + rdn.getType());
  14. System.out.println("getValue(): " + rdn.getValue());
  15. // test toAttributes
  16. System.out.println();
  17. System.out.println("toAttributes(): " + rdn.toAttributes());

获取字符串表示

为了获得根据 RFC 2253 中指定的语法格式化的 RDN 的字符串表示,您可以使用:

toString()

当您使用带有String参数的 Rdn 构造器时,您提供 RDN 的字符串表示形式,并返回一个 Rdn 实例。要执行相反的操作,即获取 Rdn 实例的字符串表示形式,请使用 toString()。 toString()的结果可以反馈给 Rdn 构造器,以生成一个等于原始 Rdn 实例的 Rdn 实例。

这是 example

  1. Rdn rdn = new Rdn("cn=Juicy\\,Fruit");
  2. String str = rdn.toString();
  3. System.out.println(str);
  4. Rdn rdn2 = new Rdn(str);
  5. System.out.println(rdn.equals(rdn2)); // true

比较 RDN

以下方法可以比较 RDN:

等于(对象 Rdn) compareTo(对象 Rdn)

您可以使用 compareTo()对 Rdn 实例列表进行排序。 equals()允许您确定两个 Rdns 在语法上是否相等。如果两个 Rdns 都具有相同(大小写完全匹配)的类型/值对,则它们是相等的。多值 RDN 中的组件顺序并不重要。

这是 example

  1. Rdn one = new Rdn("ou=Sales+cn=Bob");
  2. Rdn two = new Rdn("cn=Bob+ou=Sales");
  3. Rdn three = new Rdn("ou=Sales+cn=Bob+c=US");
  4. Rdn four = new Rdn("cn=lowercase");
  5. Rdn five = new Rdn("cn=LowerCASE");
  6. System.out.println(one.equals(two)); // true
  7. System.out.println(two.equals(three)); // false
  8. System.out.println(one.equals(three)); // false
  9. System.out.println(four.equals(five)); // true

特殊字符的逃避和失败

Rdn 类的最佳用途之一是当必须处理由特殊字符组成的 DN 时。它会自动处理转义和转义特殊字符。根据 RFC 2253 ,具有’\’(反斜杠),’,’(逗号),+(加号)等字符具有特定的语义。您可以在 RFC2253 中找到所有特殊字符的列表。当这些字符在 DN 中用作文字时,必须使用’\’(黑色)转义它们。

例如,考虑 RDN:cn=Juicy, Fruit Juicy 和 Fruit 之间出现的字符(逗号)是一个特殊字符,需要通过’\’(blackslash)进行转义。生成的语法格式化的 RDN 如下所示:cn=Juicy\, Fruit但是,根据 Java 语言字符串语法,’\’(反斜杠)字符本身是一个特殊字符,它需要再次使用’\’(反斜杠)进行转义。 Java 语言字符串格式和 RFC 2253 都使用’\’(反斜杠)来转义特殊字符。因此,Java 格式的 RDN 字符串如下所示:cn=Juicy\\, Fruit请注意,上述格式规则仅适用于 Rdn 的值组件。 Rdn类提供两种静态方法来处理 RDN 值的自动转义和转义:

escapeValue() unescapeValue()

下面的 example 显示了如何获取 DN 的字符串表示,而不必处理处理 RFC 2253 中定义的特殊字符的语法。

  1. // DN with ',' (comma)
  2. String unformatted = "Juicy, Fruit";
  3. String formatted = Rdn.escapeValue(unformatted);
  4. LdapName dn = new LdapName("cn=" + formatted);
  5. System.out.println("dn:" + dn);
  6. unformatted = "true+false";
  7. formatted = Rdn.escapeValue(unformatted);
  8. dn = new LdapName("cn=" + formatted);
  9. System.out.println("dn:" + dn);
  10. // DN with a binary value as one one of its attribute value
  11. byte[] bytes = new byte[] {1, 2, 3, 4};
  12. formatted = Rdn.escapeValue(bytes);
  13. System.out.println("Orig val: " + bytes + "Escaped val: " + formatted);

类似地,使用静态unescapeValue()方法可以从格式化的值中获取原始字符串。 Here 是用于检索原始值的示例。

  1. // DN with ',' (comma)
  2. String unformatted = "Juicy, Fruit";
  3. String formatted = Rdn.escapeValue(unformatted);
  4. System.out.println("Formatted:" + formatted);
  5. Object original = Rdn.unescapeValue(formatted);
  6. System.out.println("Original:" + original);
  7. // DN with a '+' (plus)
  8. unformatted = "true+false";
  9. formatted = Rdn.escapeValue(unformatted);
  10. System.out.println("Formatted:" + formatted);
  11. original = Rdn.unescapeValue(formatted);
  12. System.out.println("Original:" + original);
  13. // DN with a binary value as one one of its attribute value
  14. byte[] bytes = new byte[] {1, 2, 3, 4};
  15. formatted = Rdn.escapeValue(bytes);
  16. System.out.println("Formatted:" + formatted);
  17. original = Rdn.unescapeValue(formatted);
  18. System.out.println("Original:" + original);