使用 UnboundID 的 @LDAPGetter 和 @LDAPSetter

发布于 2024-10-12 02:04:12 字数 683 浏览 2 评论 0原文

有没有人有使用这两个注释的基本示例 UnboundID 的 SDK< /a> 将对象保留在 LDAP 目录中?我似乎找不到有关 @LDAPSetter 方法的参数类型或从 @LDAPGetter 方法返回的值的任何信息。

换句话说,如何填写

/**
* Called when creating a Java object from an LDAP entry.
*/
@LDAPSetter(attribute="roleOccupants")
void initMembers(<?> occupants) {
    throw new UnsupportedOperationException();
}

/**
* Called when turning a Java object into an LDAP entry.
*/
@LDAPGetter(attribute="roleOccupants")
<?> storeMembers() {
    throw new UnsupportedOperationException();
}

Does anyone have a basic example of using these two annotations from UnboundID's SDK to persist objects in an LDAP directory? I can't seem to find any info about the type of the argument to the @LDAPSetter method or the value returned from the @LDAPGetter method.

To put it another way, how do I fill in the <?>s:

/**
* Called when creating a Java object from an LDAP entry.
*/
@LDAPSetter(attribute="roleOccupants")
void initMembers(<?> occupants) {
    throw new UnsupportedOperationException();
}

/**
* Called when turning a Java object into an LDAP entry.
*/
@LDAPGetter(attribute="roleOccupants")
<?> storeMembers() {
    throw new UnsupportedOperationException();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅忆流年 2024-10-19 02:04:12

它实际上取决于roleOccupants 属性的值。 @LDAPGetter@LDAPSetter 注释的可选字段之一是 encoderClass,它允许您指定一个对象编码器,该编码器将用于在 LDAP 属性值和 Java 数据类型之间进行转换。如果您没有指定自定义编码器(大多数时候您不需要),那么它将使用 DefaultObjectEncoder 类来执行工作,并且它支持以下数据类型:

  • boolean
  • byte[]
  • char[]
  • double
  • float
  • int
  • long
  • Short
  • java.lang.Boolean
  • java.lang.Double
  • java.lang.Float
  • java.lang.Integer
  • java.lang.Long
  • java.lang.Short
  • java.lang.String
  • java.lang .StringBuffer
  • java.lang.StringBuilder
  • java.math.BigDecimal
  • java.math.BigInteger
  • java.net.URI
  • java.net.URL
  • java.util.Date
  • java.util.UUID
  • java.util.concurrent.atomic.AtomicInteger
  • java.util.并发.atomic.AtomicLong
  • com.unboundid.ldap.sdk.DN
  • com.unboundid.ldap.sdk.Filter
  • com.unboundid.ldap.sdk.LDAPURL
  • com.unboundid.ldap.sdk.RDN

它还支持数组、列表和集合上述任何类型以及任何类型的枚举。请参阅 com.unboundid.ldap.sdk.persist.DefaultObjectEncoder 类,以获取支持的数据类型和 u 的任何约束的更完整描述。

因此,实际上,您选择的数据类型取决于以上哪种类型最适合存储在相应 LDAP 属性中的信息类型。在许多情况下,字符串可能是最合适的,但在其他情况下,您可能宁愿将其视为数字、DN、时间戳或类似的东西。如果没有任何默认类型是可接受的,那么您可以创建自己的自定义 ObjectEncoder 实例来执行您想要的任何转换。

It really depends on the value of the roleOccupants attribute. One of the optional fields of the @LDAPGetter and @LDAPSetter annotations is encoderClass, which allows you to specify an object encoder that will be used to convert between LDAP attribute values and Java data types. If you don't specify a custom encoder (and most of the time you shouldn't need to), then it will use the DefaultObjectEncoder class to perform the work, and it supports the following data types:

  • boolean
  • byte[]
  • char[]
  • double
  • float
  • int
  • long
  • short
  • java.lang.Boolean
  • java.lang.Double
  • java.lang.Float
  • java.lang.Integer
  • java.lang.Long
  • java.lang.Short
  • java.lang.String
  • java.lang.StringBuffer
  • java.lang.StringBuilder
  • java.math.BigDecimal
  • java.math.BigInteger
  • java.net.URI
  • java.net.URL
  • java.util.Date
  • java.util.UUID
  • java.util.concurrent.atomic.AtomicInteger
  • java.util.concurrent.atomic.AtomicLong
  • com.unboundid.ldap.sdk.DN
  • com.unboundid.ldap.sdk.Filter
  • com.unboundid.ldap.sdk.LDAPURL
  • com.unboundid.ldap.sdk.RDN

It also supports arrays, lists, and sets of any of the above types, and any kind of enumeration. See the class-level documentation for the com.unboundid.ldap.sdk.persist.DefaultObjectEncoder class for a more complete description of the supported data types and any constraints with u.

So really, the data type that you choose depends on which of the above is most appropriate for the kind of information that is stored in the corresponding LDAP attribute. In many cases, String may be the most appropriate, but in others you may rather have it treated as a number or a DN or a timestamp or something like that. If none of the default types is acceptable, then you can create your own custom ObjectEncoder instance to perform whatever translation you want.

·深蓝 2024-10-19 02:04:12

这是一个供其他想知道的人使用的示例。当您读取 organizationalRole 时,它会自动从 ldap 加载用户数据。

UserRole 类:

@LDAPObject(structuralClass="organizationalRole", defaultParentDN="dc=Roles,dc=example,dc=com")
public class UserRole extends LDAPModel implements models.deadbolt.Role {

    @LDAPField(attribute="cn",inRDN=true,filterUsage=FilterUsage.ALWAYS_ALLOWED)
    public String name;

    private List<User> members;

    public List<User> getMembers() {
        return members;
    }

    /**
    * Called when creating a Java object from an LDAP entry.
    */
    @LDAPSetter(attribute="roleOccupant")
    void initMembers(String[] occupants) throws LDAPException {
        members = new ArrayList<User>();

        if (occupants == null || occupants.length == 0)
            return;

        for (String dn : occupants)
            members.add(User.findByDN(dn));

    }

    /**
    * Called when turning a Java object into an LDAP entry.
    */
    @LDAPGetter(attribute="roleOccupant")
    String[] storeMembers() {
        if (members == null)
            return null;

        String[] member_strings = new String[members.size()];
        for (int i = 0; i < members.size(); i++)
            member_strings[i] = members.get(i).getDN();

        return member_strings;
    }

}

User 类:

@LDAPObject(structuralClass="inetOrgPerson",
    auxiliaryClass={"mozillaOrgPerson","posixAccount","sambaSamAccount","shadowAccount"},
    defaultParentDN="dc=People,dc=example,dc=com")
public class User extends LDAPModel implements RoleHolder {

    ...

    @LDAPField(attribute="givenName",filterUsage=FilterUsage.ALWAYS_ALLOWED)
    public String fname;

    @LDAPField(attribute="sn",filterUsage=FilterUsage.ALWAYS_ALLOWED)
    public String lname;

    public static User findByDN(String dn) throws LDAPException {
        LDAPConnection conn = ...
        LDAPPersister<User> persister = LDAPPersister.getInstance(User.class);
        return persister.get(dn, conn);
    }

Here's an example of usage for anyone else that's wondering. It automatically loads user data from ldap when you read an organizationalRole.

UserRole class:

@LDAPObject(structuralClass="organizationalRole", defaultParentDN="dc=Roles,dc=example,dc=com")
public class UserRole extends LDAPModel implements models.deadbolt.Role {

    @LDAPField(attribute="cn",inRDN=true,filterUsage=FilterUsage.ALWAYS_ALLOWED)
    public String name;

    private List<User> members;

    public List<User> getMembers() {
        return members;
    }

    /**
    * Called when creating a Java object from an LDAP entry.
    */
    @LDAPSetter(attribute="roleOccupant")
    void initMembers(String[] occupants) throws LDAPException {
        members = new ArrayList<User>();

        if (occupants == null || occupants.length == 0)
            return;

        for (String dn : occupants)
            members.add(User.findByDN(dn));

    }

    /**
    * Called when turning a Java object into an LDAP entry.
    */
    @LDAPGetter(attribute="roleOccupant")
    String[] storeMembers() {
        if (members == null)
            return null;

        String[] member_strings = new String[members.size()];
        for (int i = 0; i < members.size(); i++)
            member_strings[i] = members.get(i).getDN();

        return member_strings;
    }

}

User class:

@LDAPObject(structuralClass="inetOrgPerson",
    auxiliaryClass={"mozillaOrgPerson","posixAccount","sambaSamAccount","shadowAccount"},
    defaultParentDN="dc=People,dc=example,dc=com")
public class User extends LDAPModel implements RoleHolder {

    ...

    @LDAPField(attribute="givenName",filterUsage=FilterUsage.ALWAYS_ALLOWED)
    public String fname;

    @LDAPField(attribute="sn",filterUsage=FilterUsage.ALWAYS_ALLOWED)
    public String lname;

    public static User findByDN(String dn) throws LDAPException {
        LDAPConnection conn = ...
        LDAPPersister<User> persister = LDAPPersister.getInstance(User.class);
        return persister.get(dn, conn);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文