Package org.forgerock.am.cts.api.tokens
Class Token
- java.lang.Object
-
- org.forgerock.am.cts.api.tokens.Token
-
public class Token extends Object
A simple domain value responsible for modelling a Core Token Service Token. This container is intended to be generic and has little knowledge of the data it is storing. The Token is backed by the CoreTokenField enumeration which contains the known fields that can be written to. Every value being stored must be mapped to one of these CoreTokenFields. There are some read only fields which can only be set during initialisation of the Token. The Token models the data stored in two ways. Firstly the primary fields of the Token are accessible via getters and setters. All other fields are accessed in a more generic way.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clearAttribute(CoreTokenField field)
Clear a set attribute.Token
copyWithEtag(ETag etag)
Make a shallow copy of this token and add the given etag.<T> T
getAttribute(CoreTokenField field)
Accessor for the CoreTokenField attributes.Collection<CoreTokenField>
getAttributeNames()
The Token supports being accessed in a generic way allowing a caller to iterate over all fields that are assigned in this token.byte[]
getBlob()
A binary representation of the Token being stored can be placed in the Token.Optional<ETag>
getETag()
A read only copy of the backing CTS implementation ETag.Calendar
getExpiryTimestamp()
<T> Set<T>
getMultiAttribute(CoreTokenField field)
Accessor for the multi-value CoreTokenField attributes.String
getTokenId()
TokenType
getType()
String
getUserId()
static boolean
isFieldReadOnly(CoreTokenField field)
Trying to change a read-only field will trigger a runtime exception.<T> void
setAttribute(CoreTokenField field, T value)
Mutator for the non-primary fields of the Token.void
setBlob(byte[] data)
void
setExpiryTimestamp(Calendar expiryDate)
<T> void
setMultiAttribute(CoreTokenField field, T value)
Mutator for the fields of the Token.void
setUserId(String userId)
PartialToken
toPartialToken()
Converts thisToken
into aPartialToken
containing all of the populated fields.String
toString()
Returns a formatted version of the Token which is intended to be human readable.
-
-
-
Constructor Detail
-
Token
public Token(String tokenId, TokenType type)
Create an instance of the Token.- Parameters:
tokenId
- Required field which cannot be null.type
- Required to define the type of Token.
-
Token
public Token(Token copy)
Copy constructor will create a shallow-copy of the provided Token.- Parameters:
copy
- Non null Token to copy.
-
-
Method Detail
-
copyWithEtag
public Token copyWithEtag(ETag etag)
Make a shallow copy of this token and add the given etag.- Parameters:
etag
- the etag to add to the token- Returns:
- A
Token
-
getTokenId
public String getTokenId()
- Returns:
- The unique id for this Token.
-
getType
public TokenType getType()
- Returns:
- The TokenType of the Token.
-
getUserId
public String getUserId()
- Returns:
- The unique id for the user.
-
setUserId
public void setUserId(String userId)
- Parameters:
userId
- The unique id for the user.
-
getExpiryTimestamp
public Calendar getExpiryTimestamp()
- Returns:
- The timestamp (with timezone information) when the Token is due to expire.
-
setExpiryTimestamp
public void setExpiryTimestamp(Calendar expiryDate)
- Parameters:
expiryDate
- Assign the timestamp of when the Token will expire.
-
getBlob
public byte[] getBlob()
A binary representation of the Token being stored can be placed in the Token. This ensures that attributes that are not modelled at the Token level can still be persisted.- Returns:
- The serialised binary object for this Token.
-
setBlob
public void setBlob(byte[] data)
- Parameters:
data
- Assign the binary data that represents the object being stored in the Token.
-
getAttributeNames
public Collection<CoreTokenField> getAttributeNames()
The Token supports being accessed in a generic way allowing a caller to iterate over all fields that are assigned in this token.- Returns:
- A non null, non modifiable list of the CoreTokenField fields currently assigned.
-
getAttribute
public <T> T getAttribute(CoreTokenField field)
Accessor for the CoreTokenField attributes.- Type Parameters:
T
- The type of the value stored. Note: this is not enforced and the caller is responsible for determining the correct type to request. SeeCoreTokenFieldTypes
for more details.- Parameters:
field
- The CoreTokenField to request the value for.- Returns:
- The value assigned which may be null.
-
getMultiAttribute
public <T> Set<T> getMultiAttribute(CoreTokenField field)
Accessor for the multi-value CoreTokenField attributes.- Type Parameters:
T
- The type of the value stored in the Set. Note: this is not enforced and the caller is responsible for determining the correct type to request. SeeCoreTokenFieldTypes
for more details.- Parameters:
field
- The CoreTokenField to request the value for.- Returns:
- A possible empty, non null Set containing the values assigned to the multi-value attribute.
-
setMultiAttribute
public <T> void setMultiAttribute(CoreTokenField field, T value)
Mutator for the fields of the Token.- Type Parameters:
T
- The type of the value stored in the Set. SeeCoreTokenFieldTypes
for more details.- Parameters:
field
- The CoreTokenField field to store the value against.value
- The possibly null value to store in this Token.
-
setAttribute
public <T> void setAttribute(CoreTokenField field, T value)
Mutator for the non-primary fields of the Token.- Type Parameters:
T
- The type of the value stored. SeeCoreTokenFieldTypes
for more details.- Parameters:
field
- The CoreTokenField field to store the value against.value
- The possibly null value to store in this Token.
-
clearAttribute
public void clearAttribute(CoreTokenField field)
Clear a set attribute. Regardless of whether the attribute was set, it will be cleared as a result of this call.- Parameters:
field
- Non null field to clear.
-
isFieldReadOnly
public static boolean isFieldReadOnly(CoreTokenField field)
Trying to change a read-only field will trigger a runtime exception.- Parameters:
field
- The CoreTokenField to check.- Returns:
- True if the field is read only, and cannot be changed once assigned.
-
toString
public String toString()
Returns a formatted version of the Token which is intended to be human readable. Some of the field values have been formatted to ensure that their contents are more meaningful. Note: This is not a machine readable/parsable format. This function is only intended for debugging use only as its conversion logic will not be performant.
-
toPartialToken
public PartialToken toPartialToken()
Converts thisToken
into aPartialToken
containing all of the populated fields.- Returns:
- A
PartialToken
. - Since:
- 14.0.0
-
getETag
public Optional<ETag> getETag()
A read only copy of the backing CTS implementation ETag.Requests to the
CTSPersistentStore
which include theCTSOptions.ETAG_READ_OPTION
option should populate this Token with an ETag.This concurrency check can then be included in subsequent modify requests to verify that the Token stored in the backing implementation has not been modified by another caller in the mean time.
Note: The value of this field (
getETag()
) is not used by the backing CTS implementation, but rather the value passed in using theOptions
.Note: This feature may not be available or not configured in the backing implementation of the CTS.
- Returns:
- An
Optional
which if present indicates that an ETag was received for this Token from the backing CTS implementation. - See Also:
CTSOptions.ETAG_READ_OPTION
,CTSOptions.ETAG_ASSERT_OPTION
-
-