【问题标题】:Parsing LDAP attributes from String in Java从 Java 中的 String 解析 LDAP 属性
【发布时间】:2013-11-06 19:24:21
【问题描述】:

有没有办法从String 解析属性?例如,如果我有以下内容:

CN=Doe, John: Markets (LDN),OU=Users,DC=FOOCORP,DC=COM

并且想将其放入AttributesAttribute-s 的集合中,是否有一个可以使用的实用程序类来完成所有正确的转义,或者我应该只是敲击我自己的一些实现?

我有以下代码:

    String cnBase = "CN=Doe\\, John: Markets (LDN),OU=Users,DC=FOOCORP,DC=COM";

    StringTokenizer st = new StringTokenizer(cnBase, "=");

    Attributes attributes = new BasicAttributes();

    String attributeId = null;
    String attributeValue = null;
    String previousToken = null;

    while (st.hasMoreTokens())
    {
        String token = st.nextToken();

        if (previousToken == null && attributeId == null)
        {
            // Get the attribute's id
            attributeId = token;
            continue;
        }

        if (attributeId != null)
        {
            if (token.contains(","))
            {
                attributeValue = token.substring(0, token.lastIndexOf(","));
            }
            else
            {
                attributeValue = token;
            }
        }

        if (attributeId != null && attributeValue != null)
        {
            // Add a new Attribute to the attributes object
            Attribute attribute = new BasicAttribute(attributeId, attributeValue);
            attributes.put(attribute);

            System.out.println(attribute.toString());

            attributeId = token.substring(token.lastIndexOf(",") + 1, token.length());
            attributeValue = null;
        }

        previousToken = token;
    }

我认为可以用更聪明的方式重写。

【问题讨论】:

  • 这是一个 DN,而不是一个属性。
  • 好的,标准 JDK API 中是否有一个类来保存它以及可以从 String 解析它的相应类?

标签: java ldap jndi


【解决方案1】:

JNDI 有一个名为LdapName(错误命名)的类,它代表一个专有名称。它基于过时的 RFC,但可能令人满意。

另见

【讨论】:

  • 链接失效了。 @Terry Gardner 你还知道一个好的来源作为答案吗?
猜你喜欢
  • 2023-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-21
  • 2012-06-29
  • 1970-01-01
相关资源
最近更新 更多