Package org.ldaptive
Class LdapAttribute
- java.lang.Object
-
- org.ldaptive.LdapAttribute
-
- Direct Known Subclasses:
UnicodePwdAttribute
public class LdapAttribute extends Object
LDAP attribute defined as:Attribute ::= PartialAttribute(WITH COMPONENTS { ..., vals (SIZE(1..MAX))})
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
LdapAttribute.Builder
-
Field Summary
Fields Modifier and Type Field Description private String
attributeName
Attribute name.private Set<ByteBuffer>
attributeValues
Attribute values.private boolean
binary
Whether this attribute is binary and string representations should be base64 encoded.private static String[]
BINARY_ATTRIBUTES
List of custom binary attribute names.private static String[]
DEFAULT_BINARY_ATTRIBUTES
List of attribute names known to use binary syntax.private static int
HASH_CODE_SEED
hash code seed.
-
Constructor Summary
Constructors Constructor Description LdapAttribute()
Default constructor.LdapAttribute(String type)
Creates a new attribute.LdapAttribute(String type, byte[]... value)
Creates a new attribute.LdapAttribute(String type, String... value)
Creates a new attribute.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addBinaryValues(byte[]... value)
Adds the supplied byte array as a value for this attribute.void
addBinaryValues(Collection<byte[]> values)
Adds all the byte arrays in the supplied collection as values for this attribute.void
addBufferValues(ByteBuffer... values)
Adds all the buffers in the supplied collection as values for this attribute.void
addBufferValues(Collection<ByteBuffer> values)
Adds all the buffers in the supplied collection as values for this attribute.void
addStringValues(String... value)
Adds the supplied string as a value for this attribute.void
addStringValues(Collection<String> values)
Adds all the strings in the supplied collection as values for this attribute.<T> void
addValues(Function<T,byte[]> func, Collection<T> values)
Adds all the values in the supplied collection for this attribute by encoding them with the supplied function.<T> void
addValues(Function<T,byte[]> func, T... value)
Adds the supplied values for this attribute by encoding them with the supplied function.static LdapAttribute.Builder
builder()
Creates a builder for this class.void
clear()
Removes all the values in this ldap attribute.void
configureBinary(String... attrNames)
Checks whether attrNames matches the name of this attribute.boolean
equals(Object o)
byte[]
getBinaryValue()
Returns a single byte array value of this attribute.Collection<byte[]>
getBinaryValues()
Returns the values of this attribute as byte arrays.String
getName()
Returns the attribute description with options.String
getName(boolean withOptions)
Returns the attribute description with or without options.List<String>
getOptions()
Returns any options that may exist on the attribute description.String
getStringValue()
Returns a single string value of this attribute.Collection<String>
getStringValues()
Returns the values of this attribute as strings.<T> T
getValue(Function<byte[],T> func)
Returns a single decoded value of this attribute.<T> Collection<T>
getValues(Function<byte[],T> func)
Returns the values of this attribute decoded by the supplied function.int
hashCode()
boolean
hasValue(byte[] value)
Returns whether the supplied value exists in this attribute.boolean
hasValue(String value)
Returns whether the supplied value exists in this attribute.<T> boolean
hasValue(Function<T,byte[]> func, T value)
Returns whether the supplied value exists in this attribute.boolean
isBinary()
Returns whether this ldap attribute is binary.void
removeBinaryValues(byte[]... value)
Removes the supplied byte array as a value from this attribute.void
removeBinaryValues(Collection<byte[]> values)
Removes all the byte arrays in the supplied collection as values from this attribute.void
removeBufferValues(ByteBuffer... values)
Removes all the buffers in the supplied collection as values from this attribute.void
removeBufferValues(Collection<ByteBuffer> values)
Removes all the buffers in the supplied collection as values from this attribute.void
removeStringValues(String... value)
Removes the supplied string as a value from this attribute.void
removeStringValues(Collection<String> values)
Removes all the strings in the supplied collection as values from this attribute.void
setBinary(boolean b)
Sets whether this ldap attribute is binary.void
setName(String type)
Sets the name.int
size()
Returns the number of values in this ldap attribute.static LdapAttribute
sort(LdapAttribute la)
Returns a new attribute whose values are sorted.private byte[]
toByteArray(String value, boolean throwOnError)
Converts the supplied string value to a byte array respecting thebinary
flag.String
toString()
-
-
-
Field Detail
-
HASH_CODE_SEED
private static final int HASH_CODE_SEED
hash code seed.- See Also:
- Constant Field Values
-
DEFAULT_BINARY_ATTRIBUTES
private static final String[] DEFAULT_BINARY_ATTRIBUTES
List of attribute names known to use binary syntax.
-
BINARY_ATTRIBUTES
private static final String[] BINARY_ATTRIBUTES
List of custom binary attribute names.
-
attributeName
private String attributeName
Attribute name.
-
attributeValues
private Set<ByteBuffer> attributeValues
Attribute values.
-
binary
private boolean binary
Whether this attribute is binary and string representations should be base64 encoded.
-
-
Constructor Detail
-
LdapAttribute
public LdapAttribute()
Default constructor.
-
LdapAttribute
public LdapAttribute(String type)
Creates a new attribute.- Parameters:
type
- attribute description
-
LdapAttribute
public LdapAttribute(String type, byte[]... value)
Creates a new attribute.- Parameters:
type
- attribute descriptionvalue
- attribute values
-
-
Method Detail
-
setName
public void setName(String type)
Sets the name. This method has the side effect of setting this attribute as binary if the name has an option of 'binary' or the name matches one ofBINARY_ATTRIBUTES
.- Parameters:
type
- attribute name
-
isBinary
public boolean isBinary()
Returns whether this ldap attribute is binary.- Returns:
- whether this ldap attribute is binary
-
setBinary
public void setBinary(boolean b)
Sets whether this ldap attribute is binary.- Parameters:
b
- whether this ldap attribute is binary
-
configureBinary
public void configureBinary(String... attrNames)
Checks whether attrNames matches the name of this attribute. If a match is found this attribute is set as binary.- Parameters:
attrNames
- custom binary attribute names
-
getName
public String getName()
Returns the attribute description with options.- Returns:
- attribute description
-
getName
public String getName(boolean withOptions)
Returns the attribute description with or without options.- Parameters:
withOptions
- whether the attribute description should include options- Returns:
- attribute description
-
getOptions
public List<String> getOptions()
Returns any options that may exist on the attribute description.- Returns:
- attribute description options
-
getBinaryValue
public byte[] getBinaryValue()
Returns a single byte array value of this attribute.- Returns:
- single byte array attribute value or null if this attribute is empty
-
getBinaryValues
public Collection<byte[]> getBinaryValues()
Returns the values of this attribute as byte arrays. The return collection cannot be modified.- Returns:
- collection of string attribute values
-
getStringValue
public String getStringValue()
Returns a single string value of this attribute.- Returns:
- single string attribute value or null if this attribute is empty
-
getStringValues
public Collection<String> getStringValues()
Returns the values of this attribute as strings. Binary data is base64 encoded. The return collection cannot be modified.- Returns:
- collection of string attribute values
-
getValue
public <T> T getValue(Function<byte[],T> func)
Returns a single decoded value of this attribute.- Type Parameters:
T
- type of decoded attribute- Parameters:
func
- to decode attribute value with- Returns:
- single decoded attribute value or null if this attribute is empty
-
getValues
public <T> Collection<T> getValues(Function<byte[],T> func)
Returns the values of this attribute decoded by the supplied function.- Type Parameters:
T
- type of decoded attributes- Parameters:
func
- to decode attribute values with- Returns:
- collection of decoded attribute values, null values are discarded
-
addBinaryValues
public void addBinaryValues(byte[]... value)
Adds the supplied byte array as a value for this attribute.- Parameters:
value
- to add, null values are discarded
-
addBinaryValues
public void addBinaryValues(Collection<byte[]> values)
Adds all the byte arrays in the supplied collection as values for this attribute.- Parameters:
values
- to add, null values are discarded
-
addStringValues
public void addStringValues(String... value)
Adds the supplied string as a value for this attribute.- Parameters:
value
- to add, null values are discarded
-
addStringValues
public void addStringValues(Collection<String> values)
Adds all the strings in the supplied collection as values for this attribute.- Parameters:
values
- to add, null values are discarded
-
addBufferValues
public void addBufferValues(ByteBuffer... values)
Adds all the buffers in the supplied collection as values for this attribute.- Parameters:
values
- to add, null values are discarded
-
addBufferValues
public void addBufferValues(Collection<ByteBuffer> values)
Adds all the buffers in the supplied collection as values for this attribute.- Parameters:
values
- to add, null values are discarded
-
addValues
public <T> void addValues(Function<T,byte[]> func, T... value)
Adds the supplied values for this attribute by encoding them with the supplied function.- Type Parameters:
T
- type attribute to encode- Parameters:
func
- to encode value withvalue
- to encode and add, null values are discarded
-
addValues
public <T> void addValues(Function<T,byte[]> func, Collection<T> values)
Adds all the values in the supplied collection for this attribute by encoding them with the supplied function. SeeaddValues(Function, Object...)
.- Type Parameters:
T
- type attribute to encode- Parameters:
func
- to encode value withvalues
- to encode and add, null values are discarded
-
removeBinaryValues
public void removeBinaryValues(byte[]... value)
Removes the supplied byte array as a value from this attribute.- Parameters:
value
- to remove, null values are discarded
-
removeBinaryValues
public void removeBinaryValues(Collection<byte[]> values)
Removes all the byte arrays in the supplied collection as values from this attribute.- Parameters:
values
- to remove, null values are discarded
-
removeStringValues
public void removeStringValues(String... value)
Removes the supplied string as a value from this attribute.- Parameters:
value
- to remove, null values are discarded
-
removeStringValues
public void removeStringValues(Collection<String> values)
Removes all the strings in the supplied collection as values from this attribute.- Parameters:
values
- to remove, null values are discarded
-
removeBufferValues
public void removeBufferValues(ByteBuffer... values)
Removes all the buffers in the supplied collection as values from this attribute.- Parameters:
values
- to remove, null values are discarded
-
removeBufferValues
public void removeBufferValues(Collection<ByteBuffer> values)
Removes all the buffers in the supplied collection as values from this attribute.- Parameters:
values
- to remove, null values are discarded
-
hasValue
public boolean hasValue(byte[] value)
Returns whether the supplied value exists in this attribute.- Parameters:
value
- to find- Returns:
- whether value exists
-
hasValue
public boolean hasValue(String value)
Returns whether the supplied value exists in this attribute.- Parameters:
value
- to find- Returns:
- whether value exists
-
hasValue
public <T> boolean hasValue(Function<T,byte[]> func, T value)
Returns whether the supplied value exists in this attribute.- Type Parameters:
T
- type attribute to encode- Parameters:
func
- to encode value withvalue
- to find- Returns:
- whether value exists
-
size
public int size()
Returns the number of values in this ldap attribute.- Returns:
- number of values in this ldap attribute
-
clear
public void clear()
Removes all the values in this ldap attribute.
-
sort
public static LdapAttribute sort(LdapAttribute la)
Returns a new attribute whose values are sorted. String values are sorted naturally. Binary values are sorted usingByteBuffer.compareTo(ByteBuffer)
.- Parameters:
la
- attribute to sort- Returns:
- sorted attribute
-
toByteArray
private byte[] toByteArray(String value, boolean throwOnError)
Converts the supplied string value to a byte array respecting thebinary
flag. If this attribute is binary, value is expected to be in base64 format. Otherwise, value is UTF-8 encoded.- Parameters:
value
- to convertthrowOnError
- whether to throw if a base64 decode error occurs- Returns:
- binary value
- Throws:
IllegalArgumentException
- if attribute is binary, value cannot be base64 decoded and throwOnError is true
-
builder
public static LdapAttribute.Builder builder()
Creates a builder for this class.- Returns:
- new builder
-
-