public class Attribute extends java.lang.Object implements ASN1Type
Attribute
object consists of an attribute type (specified by an
object identifier) and one or more attribute values:
Attribute ::= SEQUENCE { type AttributeType, values SET OF AttributeValue -- at least one value is required -- } AttributeType ::= OBJECT IDENTIFIER AttributeValue ::= ANY DEFINED BY type
When creating a new Attribute
object, the attribute type has to be specified
as ObjectID
, and the attribute value(s) have to be supplied
as array of ASN1Objects, e.g.:
Date signingDate = ...; ChoiceOfTime choiceOfTime = new ChoiceOfTime(date); ASN1Object[] asn1Objects = new ASN1Object[] { choiceOfTime.toASN1Object() }; Attribute attribute = new Attribute(ObjectID.signingTime, asn1Objects);The example above creates a PKCS#9 SigningTime attribute to be used for specifying the time a signer has signed a PKCS#7
SignedData
message.
Alternatively an Attribute may be created from an AttributeValue
object. AttributeValue
is an
abstract class allowing to implement and register
particular
attribute values. If an registered implementation of some particular AttributeValue exists an
Attribute may be created
immediately from a corresponding
implementation object. In this way, if an implementation of the SigningTime attribute has been
registered, the example from above alternatively may look like:
Date signingDate = ...; SigningTime signingTime = new SigningTime(date); Attribute attribute = new Attribute(signingTime);
AttributeValue
implementations also make it more
convenient to query an existing attribute for its value(s):
SigningTime signingTime = (SigningTime)attribute.getAttributeValue(); Date signingDate = signingTime.getDate();respectively (if more than one value are included -- will not occur for SigningTime):
AttributeValue[] signingTimes = attribute.getAttributeValues(); for (int i = 0; i < signingTimes.length; i++) { SigningTime signingTimes = (SigningTime)signingTimes[i]; ... }to be compared against method
getValue
returning the values
as array of ASN1Objects:
ASN1Object signingTimes = attribute.getValue(); ChoiceOfTime cot = new ChoiceOfTime(signingTimes.getValue()[0]); Date signingDate = cot.getDate();If no attribute value implementation is registered for a particular attribute type method
getAttributeValue
will return an UnknownAttributeValue
.AttributeValue
,
UnknownAttributeValue
Constructor and Description |
---|
Attribute()
Creates an empty Attribute.
|
Attribute(ASN1Object obj)
Creates an Attribute from an ASN1Object.
|
Attribute(AttributeValue attributeValue)
Creates an Attribute from the given AttributeValue.
|
Attribute(AttributeValue attributeValue,
boolean sorted)
Creates an Attribute from the given AttributeValue.
|
Attribute(ObjectID type,
ASN1Object[] value)
Creates an Attribute from attribute type (ObjectID) and attribute values.
|
Attribute(ObjectID type,
ASN1Object[] values,
boolean sorted)
Creates an Attribute from attribute type (ObjectID) and attribute values.
|
Modifier and Type | Method and Description |
---|---|
void |
addAttributeValue(AttributeValue attributeValue)
Adds an AttributeValue to the set of attribute values.
|
static AttributeValue |
create(ObjectID attributeType)
Returns the implementation of the specified AttributeValue defined through an
ASN.1 ObjectID (the attribute type).
|
void |
decode(ASN1Object obj)
Decodes an Attribute from the given ASN1Object.
|
boolean |
equals(java.lang.Object obj)
Compares two Attributes.
|
AttributeValue |
getAttributeValue()
Returns the value of this (single valued) Attribute.
|
AttributeValue[] |
getAttributeValues()
Returns the values of this (multi-valued) Attribute.
|
ObjectID |
getType()
Returns the type of this Attribute.
|
ASN1Object[] |
getValue()
Returns the value of this Attribute.
|
int |
hashCode()
Returns a hash code for this object.
|
static void |
register(ObjectID attributeType,
java.lang.Class cl)
Registers a class for implementing a particular attribute value.
|
void |
setAttributeValue(AttributeValue attributeValue)
Sets the AttributeValue to this single-valued attribute.
|
ASN1Object |
toASN1Object()
Returns the Attribute as an ASN1Object.
|
ASN1Object |
toASN1Object(boolean sorted)
Returns the Attribute as an ASN1Object.
|
java.lang.String |
toString()
Returns a string that represents the contents of this Attribute.
|
public Attribute()
public Attribute(ObjectID type, ASN1Object[] value)
type
- the type of the attribute as ObjectIDvalue
- the value of the attribute as array of ASN1Objectspublic Attribute(ObjectID type, ASN1Object[] values, boolean sorted)
type
- the type of the attribute as ObjectIDvalues
- the value of the attribute as array of ASN1Objectssorted
- whether to sort the SET of AttributeValue according there
encodingpublic Attribute(AttributeValue attributeValue) throws CodingException
The attribute type is derived from the supplied AttributeValue
.
attributeValue
- the value of the attribute as array of ASN1ObjectsCodingException
- since attribute values internally are maintained as ASN1Object
a CodingException might be thrown when getting the ASN.1
representation of the given AttributeValuepublic Attribute(AttributeValue attributeValue, boolean sorted) throws CodingException
The attribute type is derived from the supplied AttributeValue
.
This constructor may be used for creating a Attribute from an AttributeValue for which a
class implementation exists. Any further attribute value may be added to the set of attribute
values by calling method addAttributeValue
.
attributeValue
- the value of the attributesorted
- whether to sort the SET of AttributeValue according there
encodingCodingException
- since attribute values internally are maintained as ASN1Object
a CodingException might be thrown when getting the ASN.1
representation of the given AttributeValuepublic Attribute(ASN1Object obj) throws CodingException
toASN1Object()
method.obj
- the Attribute as ASN1ObjectCodingException
- if this ASN1Object could not be parsedpublic static AttributeValue create(ObjectID attributeType) throws java.lang.InstantiationException
This method belongs to the static part of this class.
attributeType
- the OID identifying the attribute type the AttributeValue belongs tojava.lang.InstantiationException
- if the internal factory
couldn't create an instance of requested typepublic static void register(ObjectID attributeType, java.lang.Class cl)
attributeType
- the OID identifying the attribute type the AttributeValue implementing class belongs tocl
- the class which implements the attribute value in mindjava.lang.IllegalArgumentException
- if the given class is not an extended
from AttributeValue
public void addAttributeValue(AttributeValue attributeValue) throws CodingException, java.lang.IllegalArgumentException
multipleAllowed
allowed, the given AttributeValue is added to the list of already included attribute values,
otherwise it will replace any existing one.attributeValue
- the attribute value to be addedCodingException
- since attribute values internally are maintained as ASN1Object
a CodingException might be thrown when getting the ASN.1
representation of the given AttributeValuejava.lang.IllegalArgumentException
- if the attributValue is of a type not matching to the type
of this Attributepublic void setAttributeValue(AttributeValue attributeValue) throws CodingException, java.lang.IllegalArgumentException
attributeValue
- the attribute value to be set for this attributeCodingException
- since attribute values internally are maintained as ASN1Object
a CodingException might be thrown when getting the ASN.1
representation of the given AttributeValuejava.lang.IllegalArgumentException
- if the attributValue is of a type not matching to the type
of this Attributepublic void decode(ASN1Object obj) throws CodingException
toASN1Object()
method.decode
in interface ASN1Type
obj
- the ASN.1 type as ASN1ObjectCodingException
- if the ASN1Object could not be parsedpublic ASN1Object toASN1Object()
toASN1Object
in interface ASN1Type
public ASN1Object toASN1Object(boolean sorted)
sorted
- whether to sort the SET of AttributeValue according there
encoding (overrides the value that already may have been
set via constructor
public ObjectID getType()
public ASN1Object[] getValue()
public AttributeValue getAttributeValue() throws CodingException
This method preferably may be called for getting the value of an Attribute
having only one single value. For getting all the values of an multi-valued
Attribute call method getAttributeValues
or
method getValue()
.
This method looks if there exists an registered
implementation of the attribute value belonging to the attribute type this
Attribute object represents. If an AttributeValue
implementation exists, this method returns the attribute value
as corresponding AttributeValue
descendant. If no implementation
exists, this method returns an UnknownAttributeValue
object to may be parsed for its ASN.1 representation.
null
if no attribute value has been setCodingException
- if an error occurs when internally decoding the attribute
value for parsing its structurepublic AttributeValue[] getAttributeValues() throws CodingException
This method preferably may be called for getting the values of an Attribute having more than only one single value.
For each included attribute value this method looks if there exists an registered
implementation of the attribute value
belonging to the attribute type this Attribute object represents. If an AttributeValue
implementation exists, this
method returns the attribute values as corresponding AttributeValue
descendants. If no implementation exists, this method returns an array of UnknownAttributeValue
objects to may
be parsed for their ASN.1 representation.
CodingException
- if an error occurs when internally decoding any attribute
value for parsing its structurepublic boolean equals(java.lang.Object obj)
equals
in class java.lang.Object
obj
- the other Attributetrue
, if the two Attributes are equal,
false
otherwisepublic int hashCode()
hashCode
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object