Package org.whiteflagprotocol.java
Class WfMessage
java.lang.Object
org.whiteflagprotocol.java.WfMessage
Whiteflag message class
This class represents a Whiteflag message. It provides static factory methods to create Whiteflag messages in different ways from various data, and it contains all methods to create, encode and decode a message.
The class builds upon the WfBasicMessage
class that represents
a Whiteflag message in its basic form with a header and a body containing
pre-defined message fields. This class encapsulates the base message, adds
metadata and additional methods. Metadata includes the message originmator
and recipient (if any), to be provided through the WfAccount
interface. The additional methods include message encryption and decryption
using the `org.whiteflagprotocol.java.crypto` package.
- Since:
- 1.1
- Whiteflag version:
- v1-draft.6
-
Method Summary
Modifier and TypeMethodDescriptionaddMetadata
(String key, String value) Adds metadata to the Whiteflag message if not already existingstatic final WfMessage
Creates a new Whiteflag message object from field valuesfinal WfMessage
copy()
Copies the base of this message into a new message, without any metadatastatic final WfMessage
Creates a new empty Whiteflag message object of the specified typestatic final WfMessage
decode
(byte[] binMessage) Creates a new Whiteflag message from a byte array with an binary encoded messagestatic final WfMessage
Creates a new Whiteflag message from a hexadecimal string represaentation of an encoded messagestatic final WfMessage
decode
(WfBinaryBuffer encodedMsg) Creates a new Whiteflag message from a binary bufferstatic final WfMessage
Creates a new Whiteflag message from an encyrpted binary buffer and metadatastatic final WfMessage
Creates a new Whiteflag message from an encyrpted binary buffer and metadatastatic final WfMessage
decrypt
(WfBinaryBuffer encryptedMsg, WfAccount originator, WfAccount recipient, byte[] initVector) Creates a new Whiteflag message from an encyrpted binary buffer and metadatastatic final WfMessage
deserialize
(String serializedMsg) Creates a new Whiteflag message object from a serialized messagestatic final WfMessage
deserializeJson
(String jsonMessage) Creates a new Whiteflag message object from a serialized JSON messagefinal WfBinaryBuffer
encode()
Returns the cached encoded/encrypted message, or else it encodes/encrypts and caches the messagefinal WfBinaryBuffer
encrypt()
Returns the encoded/encrypted message; identical to encode()Gets the value of the specified fieldGets the field names of this messagefinal byte[]
Gets the non-secret initialisation vector used for encryption and decryptiongetMetadata
(String key) Returns the requested metadata value of the Whiteflag messageReturns metadata keys of the Whiteflag messagefinal int
Gets the number of fields in this messagefinal WfAccount
Gets the originator of this message by their accountfinal WfAccount
Gets the recipient of this message by theri accountgetType()
Gets the type of this messageboolean
isValid()
Checks if this message contains valid datafinal boolean
Checks if the specified message field contains valid datafinal boolean
Checks if the provided data is valid for the specified message fieldfinal String
Returns the cached serialized message, or else it serialzes and caches Whiteflag messageboolean
Sets the value of the specified fieldfinal byte[]
setInitVector
(byte[] initVector) Sets the non-secret initialisation vector used for encryption and decryption, if not already setfinal byte[]
setInitVector
(String initVector) Sets the non-secret initialisation vector used for encryption and decryption, if not already setprotected final void
setMetadata
(Map<String, String> metadata) Sets the metadatafinal String
setOriginator
(WfAccount originator) Sets the originator sending this message and adds its blockchain address to the metadatafinal String
setRecipient
(WfAccount recipient) Sets the intended recipient of this message (if any) and adds its blockchain address to the metadatafinal byte[]
Returns a byte array with the binary encoded messagefinal String
Returns a hexadimal string representation of the binary encoded messagefinal String
toJson()
Returns the serialised JSON representation of the Whiteflag messagetoString()
Returns the message as a concatinated string of field values
-
Method Details
-
create
Creates a new empty Whiteflag message object of the specified type- Parameters:
messageCode
- a string with the code indicating the message type to be created- Returns:
- a new Whiteflag message
- Throws:
WfException
- if the message cannot be created
-
deserialize
Creates a new Whiteflag message object from a serialized message- Parameters:
serializedMsg
- the uncompressed serialized message- Returns:
- a new Whiteflag message
- Throws:
WfException
- if the serialization of the message is invalid- Since:
- 1.1
-
deserializeJson
Creates a new Whiteflag message object from a serialized JSON message- Parameters:
jsonMessage
- the serialized JSON message- Returns:
- a new Whiteflag message
- Throws:
WfException
- if the serialization of the message is invalid
-
decode
Creates a new Whiteflag message from a hexadecimal string represaentation of an encoded message- Parameters:
hexMessage
- a hexadecimal string representation of the encoded message- Returns:
- a new Whiteflag message
- Throws:
WfException
- if the message cannot be decoded- Since:
- 1.1
-
decode
Creates a new Whiteflag message from a byte array with an binary encoded message- Parameters:
binMessage
- a byte array with the binary encoded message- Returns:
- a new Whiteflag message
- Throws:
WfException
- if the message cannot be decoded- Since:
- 1.1
-
decode
Creates a new Whiteflag message from a binary buffer- Parameters:
encodedMsg
- a binary buffer with the encoded message- Returns:
- a new Whiteflag message
- Throws:
WfException
- if the message cannot be decoded- Since:
- 1.1
-
decrypt
public static final WfMessage decrypt(String encryptedMsg, WfAccount originator, WfAccount recipient, String initVector) throws WfException Creates a new Whiteflag message from an encyrpted binary buffer and metadata- Parameters:
encryptedMsg
- a hexadecimal string with the encrypted messageoriginator
- the originator of the messagerecipient
- the intended recipient of the messageinitVector
- the initialisation vector used to encrypt the message- Returns:
- a new Whiteflag message
- Throws:
WfException
- if the message cannot be decrypted or decoded- Since:
- 1.1
-
decrypt
public static final WfMessage decrypt(byte[] encryptedMsg, WfAccount originator, WfAccount recipient, byte[] initVector) throws WfException Creates a new Whiteflag message from an encyrpted binary buffer and metadata- Parameters:
encryptedMsg
- a hexadecimal string with with the encrypted messageoriginator
- the originator of the messagerecipient
- the intended recipient of the messageinitVector
- a byte array with initialisation vector used to encrypt the message- Returns:
- a new Whiteflag message
- Throws:
WfException
- if the message cannot be decrypted or decoded- Since:
- 1.1
-
decrypt
public static final WfMessage decrypt(WfBinaryBuffer encryptedMsg, WfAccount originator, WfAccount recipient, byte[] initVector) throws WfException Creates a new Whiteflag message from an encyrpted binary buffer and metadata- Parameters:
encryptedMsg
- a binary buffer with the encrypted messageoriginator
- the originator of the messagerecipient
- the intended recipient of the messageinitVector
- a byte array with the initialisation vector used to encrypt the message- Returns:
- a new Whiteflag message
- Throws:
WfException
- if the message cannot be decrypted or decoded- Since:
- 1.1
-
compile
Creates a new Whiteflag message object from field values- Parameters:
fieldValues
- a string array with the values for the message fields- Returns:
- a new Whiteflag message
- Throws:
WfException
- if any of the provided values is invalid
-
getType
Gets the type of this message- Returns:
- the message type
-
isValid
public boolean isValid()Checks if this message contains valid data- Returns:
- TRUE if all message fields contain valid data, else FALSE
-
isValid
Checks if the specified message field contains valid data- Parameters:
fieldname
- the name of the field- Returns:
- TRUE if the field contains valid data, else FALSE
-
isValid
Checks if the provided data is valid for the specified message field- Parameters:
fieldname
- the name of the fielddata
- the value to be checked- Returns:
- TRUE if the field contains valid data, else FALSE
-
getNoFields
public final int getNoFields()Gets the number of fields in this message- Returns:
- the number of message fields
-
getFieldNames
Gets the field names of this message- Returns:
- a string set with all field names
-
get
Gets the value of the specified field- Parameters:
fieldname
- the name of the requested field- Returns:
- the field value, or NULL if field does not exist
-
set
Sets the value of the specified field- Parameters:
fieldname
- the name of the fielddata
- data to be set as the field value- Returns:
- TRUE if field value is set, FALSE if field does not exits, isalready set, or data is invalid
-
addMetadata
Adds metadata to the Whiteflag message if not already existing- Parameters:
key
- a string with the metadata keyvalue
- a string with the metadata value- Returns:
- null if successful, otherwise the value of the already existing key
-
getMetadata
Returns the requested metadata value of the Whiteflag message- Parameters:
key
- a string with the metadata key- Returns:
- the value associated with the requested metadata key
-
getMetadataKeys
Returns metadata keys of the Whiteflag message- Returns:
- a string set with all metadata keys
-
copy
Copies the base of this message into a new message, without any metadata- Returns:
- a base copy of this Whiteflag message
- Since:
- 1.1
-
setOriginator
Sets the originator sending this message and adds its blockchain address to the metadata- Parameters:
originator
- the originator information- Returns:
- null if address newly added to metadata, otherwise the existing value that was replaced
- Since:
- 1.1
-
getOriginator
Gets the originator of this message by their account- Returns:
- the account of the originator
- Since:
- 1.1
-
setRecipient
Sets the intended recipient of this message (if any) and adds its blockchain address to the metadata- Parameters:
recipient
- the account of the recipient of this message- Returns:
- null if address newly added to metadata, otherwise the existing value that was replaced
- Since:
- 1.1
-
getRecipient
Gets the recipient of this message by theri account- Returns:
- the account of the recipient
- Since:
- 1.1
-
setInitVector
Sets the non-secret initialisation vector used for encryption and decryption, if not already set- Parameters:
initVector
- a hexadecimal string with the initialisation vector- Returns:
- a byte array with the initialisation vector; if already set this is the existing initialisation vector
- Since:
- 1.1
-
setInitVector
public final byte[] setInitVector(byte[] initVector) Sets the non-secret initialisation vector used for encryption and decryption, if not already set- Parameters:
initVector
- a byte array with the initialisation vector- Returns:
- a byte array with the initialisation vector; if already set this is the existing initialisation vector
- Since:
- 1.1
-
getInitVector
public final byte[] getInitVector()Gets the non-secret initialisation vector used for encryption and decryption- Returns:
- a byte array with the initialisation vector
- Since:
- 1.1
-
toString
Returns the message as a concatinated string of field values -
serialize
Returns the cached serialized message, or else it serialzes and caches Whiteflag message- Returns:
- the serialized message, i.e. the concatinated string of field values
- Throws:
WfException
- if any of the field does not contain valid data
-
encode
Returns the cached encoded/encrypted message, or else it encodes/encrypts and caches the message- Returns:
- a binary buffer with the encoded/endypted message
- Throws:
WfException
- if any field does not contain valid data- Since:
- 1.1
-
encrypt
Returns the encoded/encrypted message; identical to encode()- Returns:
- a byte array with the compressed binary encoded message
- Throws:
WfException
- if any field does not contain valid data- Since:
- 1.1
-
toByteArray
Returns a byte array with the binary encoded message- Returns:
- a byte array with the binary encoded message
- Throws:
WfException
- if any field does not contain valid data- Since:
- 1.1
-
toHexString
Returns a hexadimal string representation of the binary encoded message- Returns:
- a hexadecimal string representation of the binary encoded
- Throws:
WfException
- if any field does not contain valid data- Since:
- 1.1
-
toJson
Returns the serialised JSON representation of the Whiteflag message- Returns:
- the serialised JSON representation
- Throws:
WfException
- if the message cannot be serialised
-
setMetadata
Sets the metadata- Parameters:
metadata
- the key-to-value mapping of the metadata
-