public class SwiftMessage extends Object implements Serializable
Base class for swift messages.
This class is a generic data structure container for SWIFT messages.
This is a low level java representation of an MT. If you are looking for
a class more suitable to be persisted see MtSwiftMessage
Instances of this class may have a list of unparsed texts (UnparsedTextList).
For easy access, methods have been created that first ensure the lists exists (the
real object is created and then call the base method).
However, not all the base list methods have been implemented. If you need to use not
exposed functionality, retrieve the underlying list with (see getUnparsedTexts method).
Modifier and Type | Field and Description |
---|---|
protected Long |
id
Identification of the message when persisted
|
Constructor and Description |
---|
SwiftMessage()
Default constructor.
|
SwiftMessage(boolean initBlocks)
Constructor that initializes blocks
TODO document hibernate didn't work
|
SwiftMessage(boolean initBlocks,
UnparsedTextList unparsedText)
Constructor for an unparsed text list that initializes blocks
|
SwiftMessage(UnparsedTextList unparsedText)
Constructor for an unparsed text list
|
Modifier and Type | Method and Description |
---|---|
void |
addBlock(SwiftBlock b)
Add a block to this message.
|
void |
addUserBlock(SwiftBlockUser userBlock)
Add a user defined block to the message (if the block already exists, it is replaced)
|
void |
clear()
remove all blocks from these message, including user blocks
|
boolean |
equals(Object obj) |
Field |
field(String name)
return first results of fields() or
null if none |
List<Field> |
fields(String... names)
Get all fields with the given name in the block 4.
|
Integer |
fragmentCount()
Gets the total number of fragments of a fragmented message as informed in tag 203.
|
Integer |
fragmentNumber()
Gets the number of this fragment
|
SwiftBlock |
getBlock(int b)
Get the block number specified by b.
|
SwiftBlock1 |
getBlock1()
Get block number 1 of this message, may be
null if not set |
SwiftBlock2 |
getBlock2()
Get block number 2 of this message, may be
null if not set |
SwiftBlock3 |
getBlock3()
Get block number 3 of this message, may be
null if not set |
SwiftBlock4 |
getBlock4()
Get block number 4 of this message, may be
null if not set |
SwiftBlock5 |
getBlock5()
Get block number 5 of this message, may be
null if not set |
int |
getBlockCount()
Get the number of blocks in this message, including the user blocks
|
int |
getBlockCount(Boolean includeUserBlocks)
Get the number of blocks in this message, optionally including the user blocks.
A block is summed if it is not null and is not empty. |
MtCategory |
getCategory()
Returns the message category from the message type.
|
MessageIOType |
getDirection()
Returns the message direction from block 2 or null if block 2 is not found or incomplete
|
Long |
getId()
Get the unique identifier of this message
|
List<SwiftTagListBlock> |
getLinkages()
Return the message's LINK sequences if any.
|
String |
getMIR()
Gets MIR from the application header block or null if the the message is not incoming or the application header block is null
|
MtId |
getMtId()
Returns the MT message identification.
Composed by the business process, message type and variant. |
String |
getMUR()
Gets MUR from the user header block or null if the user header or MUR are not present.
|
SequenceNode |
getParsedSequences() |
String |
getPDE()
Gets PDE from the trailer block or null if the trailer or the PDE field is not present
|
String |
getPDM()
Gets PDM from the trailer block or null if the trailer or the PDM field is not present
|
String |
getReceiver()
Gets the message receiver BIC from the message headers.
|
String |
getSender()
Gets the message sender BIC from the message headers.
|
List<String> |
getTagNames()
Get a list of unique tagname contained in this message
|
String |
getType()
Tell the message type associated with this object if a block 2 is present.
|
int |
getTypeInt()
get message type as an int or -1 if an error occurs or it is not set
|
UnparsedTextList |
getUnparsedTexts()
returns the unparsed text list
|
Integer |
getUnparsedTextsSize()
returns the size of the unparsed text list
|
SwiftBlockUser |
getUserBlock(Integer blockNumber)
Get a user defined block by number, may be
null if not set |
SwiftBlockUser |
getUserBlock(String blockName)
Get a user defined block by name, may be
null if not set |
int |
getUserBlockPosition(String blockName)
Finds the position of a given User Defined Block in the internal list
|
List<SwiftBlockUser> |
getUserBlocks()
Get the list of List of
SwiftBlockUser user defined blocks. |
String |
getUUID()
Gets UUID
|
MTVariant |
getVariant() |
int |
hashCode() |
boolean |
isAck()
Returns true if this message is an ACK.
|
boolean |
isCategory(MtCategory... categories)
Returns true if the message category is equal to one of the given by parameter
|
boolean |
isCOV()
Checks if the message is a cover payment, based on the content of the User Header (block3).
|
Boolean |
isFragment()
Checks if the message is a fragment
|
boolean |
isIncoming()
Returns true if the message is incoming (received from SWIFT), false other case; using the direction attribute.
|
boolean |
isInput() |
Boolean |
isLastFragment()
Checks if the message is the last fragment
|
Boolean |
isLinked()
Checks if the message is linked to other message based on the presence of a LINK sequence.
|
boolean |
isMT(String type)
Deprecated.
this method has been deprecated in favor of
isType(int) which provides a safer API just passing an int number for the message type |
boolean |
isNack()
Returns true if this message is an NACK.
|
boolean |
isOutgoing()
Returns true if the message is outgoing (sent to SWIFT), false other case; using the direction attribute.
|
boolean |
isOutput() |
boolean |
isREMIT()
Checks if the message is a remit, based on the content of the User Header (block3).
|
boolean |
isServiceMessage()
Returns true if message service id is anything but 01 = GPA/FIN Message (system and user-to-user)
|
boolean |
isServiceMessage21()
Returns true if message service id is 21 = GPA/FIN Message (ACK/NAK/UAK/UNK)
|
boolean |
isSTP()
Checks if the message is Straight Through Processing (STP), based on the content of the User Header (block3).
|
boolean |
isSystemMessage()
Deprecated.
|
boolean |
isType(int... types)
Returns true if the message type is equal to one of the given numbers.
|
boolean |
isType(int type)
Returns true if the message type is equal to the given number.
|
static SwiftMessage |
parse(String fin)
Parses a the string content into a SwiftMessage.
|
void |
removeEmptyBlocks()
Checks all blocks (1 to 5) and if a block is empty, it is removed from the message.
|
void |
removeUserBlock(Integer blockNumber)
removes a user defined block to the message (if the block does not exists, nothing is done)
|
void |
removeUserBlock(String blockName)
removes a user defined block to the message (if the block does not exists, nothing is done)
|
void |
setBlock1(SwiftBlock1 block1)
Set the block 1 of the message
|
void |
setBlock2(SwiftBlock2 block2)
Set the block 2 of the message
|
void |
setBlock3(SwiftBlock3 block3)
Set the block 3 of the message
|
void |
setBlock4(SwiftBlock4 block4)
Set the block 4 of the message
|
void |
setBlock5(SwiftBlock5 block5)
Set the block 5 of the message
|
void |
setId(Long id)
Set the unique identifier of this message
|
void |
setParsedSequences(SequenceNode parsedSequences) |
void |
setUnparsedTexts(UnparsedTextList texts)
sets the list of unparsed texts
|
protected void |
setUserBlocks(List<SwiftBlockUser> userBlocks)
Set the list of user defined blocks.
This method is mainly needed for persistence services. |
String |
toJson()
Get a json representation of this object.
|
AbstractMT |
toMT()
Get the MTxxx instance that corresponds to the current message type.
|
String |
toString()
Commons-lang reflection toString implementation
|
String |
toXml()
Gets a proprietary XML representation of this message.
Notice: it is neither a standard nor the MX version of this MT. |
void |
unparsedTextAddText(String text)
adds a new unparsed text
|
void |
unparsedTextAddText(SwiftMessage message)
adds a new unparsed text from a message
|
SwiftMessage |
unparsedTextGetAsMessage(Integer index)
get an unparsed text as a parsed swift message
|
String |
unparsedTextGetText(Integer index)
get an unparsed text
|
Boolean |
unparsedTextIsMessage(Integer index)
decides if a specific text (by index) is likely a SWIFT FIN message.
|
protected void |
unparsedTextVerify()
verifies that the unparsed text list exists
|
void |
visit(IMessageVisitor visitor)
Visit the current message with the given visitor.
|
static void |
visit(SwiftBlock3 block,
IMessageVisitor visitor)
Visit a Block 3 (SwiftBlock3), i.e: call the tag method for block 3
This method is called from
visit(IMessageVisitor) but may be used independently, in such case,
the startBlockX and endBlockX in the visitor will not be called. |
static void |
visit(SwiftBlock4 block,
IMessageVisitor visitor)
Visit a Block 4 (SwiftBlock4), i.e: call the tag method for block 4
This method is called from
visit(IMessageVisitor) but may be used independently, in such case,
the startBlockX and endBlockX in the visitor will not be called. |
static void |
visit(SwiftBlock5 block,
IMessageVisitor visitor)
Visit a Block 5 (SwiftBlock5), i.e: call the tag method for block 4
This method is called from
visit(IMessageVisitor) but may be used independently, in such case,
the startBlockX and endBlockX in the visitor will not be called. |
static void |
visit(SwiftBlockUser block,
IMessageVisitor visitor)
Visit a User Defined Block (SwiftBlockUser), i.e: call the tag method for block 4
This method is called from
visit(IMessageVisitor) but may be used independently, in such case,
the startBlockX and endBlockX in the visitor will not be called. |
protected Long id
public SwiftMessage()
SwiftMessage(boolean)
public SwiftMessage(boolean initBlocks)
initBlocks
- when false
the message will not have any blocks when constructed, if true
blocks are created, just like with default constructorpublic SwiftMessage(boolean initBlocks, UnparsedTextList unparsedText)
initBlocks
- when false
the message will not have any blocks when constructed, if true
blocks are created, just like with default consturctorunparsedText
- the list of unparsed textsSwiftMessage()
public SwiftMessage(UnparsedTextList unparsedText)
unparsedText
- the list of unparsed textsSwiftMessage()
public static final SwiftMessage parse(String fin) throws IOException
fin
- string a string containing a swift MT messageIOException
public SwiftBlock getBlock(int b)
b
- the block number to retrieve, must be greater or equal to 1 and smaller or equal to 5.IllegalArgumentException
- if b < 1 or b > 5public String toString()
public void addBlock(SwiftBlock b)
Notes: on user blocks, no checks are done, on swift blocks, block number must be non null and have a value from 1-5 both inclusive
b
- the block to add, may be null
in which case nothing happensIllegalArgumentException
- b
is null
or the method getInt in the block returns a value out of range (non user blocks)@Deprecated @ProwideDeprecated(phase2=_2018) public boolean isMT(String type)
isType(int)
which provides a safer API just passing an int number for the message typetype
- must be a valid registered handler idtrue
if this message is successfully identified as the given MT and false
in other case *IllegalArgumentException
- if parameter type is null
or not a valid type (i.e: 3 chars len)SwiftBlock2.getMessageType()
,
getType()
public String getType()
null
if the message does not have a block 2.SwiftBlock2.getMessageType()
public void visit(IMessageVisitor visitor)
visitor
- the visitor to useIllegalArgumentException
- if parameter visitor is null
SwiftWriter.writeMessage(SwiftMessage, java.io.Writer)
public static void visit(SwiftBlock3 block, IMessageVisitor visitor)
visit(IMessageVisitor)
but may be used independently, in such case,
the startBlockX and endBlockX in the visitor will not be called.
To serialize in SWIFT native format with block boundaries check SwiftWriter.writeBlock3(SwiftBlock3, java.io.Writer)
block
- the block containing the tags to visitvisitor
- the visitor to useIllegalArgumentException
- if parameter block or visitor are null
public static void visit(SwiftBlock4 block, IMessageVisitor visitor)
visit(IMessageVisitor)
but may be used independently, in such case,
the startBlockX and endBlockX in the visitor will not be called.
To serialize in SWIFT native format with block boundaries check SwiftWriter.writeBlock4(SwiftBlock4, java.io.Writer)
block
- the block containing the tags to visitvisitor
- the visitor to useIllegalArgumentException
- if parameter block or visitor are null
public static void visit(SwiftBlock5 block, IMessageVisitor visitor)
visit(IMessageVisitor)
but may be used independently, in such case,
the startBlockX and endBlockX in the visitor will not be called.
To serialize in SWIFT native format with block boundaries check SwiftWriter.writeBlock5(SwiftBlock5, java.io.Writer)
block
- the block containing the tags to visitvisitor
- the visitor to useIllegalArgumentException
- if parameter block or visitor are null
public static void visit(SwiftBlockUser block, IMessageVisitor visitor)
visit(IMessageVisitor)
but may be used independently, in such case,
the startBlockX and endBlockX in the visitor will not be called.block
- the block containing the tags to visitvisitor
- the visitor to useIllegalArgumentException
- if parameter block or visitor are null
public Long getId()
id
public void setId(Long id)
id
- the id to be setid
public int getBlockCount()
public int getBlockCount(Boolean includeUserBlocks)
null
and is not empty.
NOTE: that isEmpty() will be called in each block, the behavior of isEmpty is block
dependentincludeUserBlocks
- indicates whether or not user defined blocks should be countedSwiftBlock1.isEmpty()
,
SwiftBlock2Input.isEmpty()
,
SwiftBlock2Output.isEmpty()
,
SwiftTagListBlock.isEmpty()
,
SwiftTagListBlock.isEmpty()
,
SwiftTagListBlock.isEmpty()
public SwiftBlock1 getBlock1()
null
if not setnull
public void setBlock1(SwiftBlock1 block1)
block1
- the content of the block 1public SwiftBlock2 getBlock2()
null
if not setnull
public void setBlock2(SwiftBlock2 block2)
block2
- the content of the block 1public SwiftBlock3 getBlock3()
null
if not setnull
public void setBlock3(SwiftBlock3 block3)
block3
- the content of the block 1public SwiftBlock4 getBlock4()
null
if not setnull
public void setBlock4(SwiftBlock4 block4)
block4
- the content of the block 1public SwiftBlock5 getBlock5()
null
if not setnull
public void setBlock5(SwiftBlock5 block5)
block5
- the content of the block 5public int getUserBlockPosition(String blockName)
blockName
- the block name to find may be empty or null, in which case this method does nothing-1
if not foundpublic List<SwiftBlockUser> getUserBlocks()
SwiftBlockUser
user defined blocks.
The requested object may be null
if the message was cleared or not initialized.protected void setUserBlocks(List<SwiftBlockUser> userBlocks)
userBlocks
- the new list of user defined blocksIllegalArgumentException
- if parameter userBlocks is null
IllegalArgumentException
- if parameter userBlocks has elements of class other than SwiftBlockUserSwiftBlockUser
public SwiftBlockUser getUserBlock(String blockName)
null
if not setblockName
- the name of the block to findnull
IllegalArgumentException
- if parameter blockName is null
IllegalArgumentException
- if parameter blockName has an invalid block namepublic SwiftBlockUser getUserBlock(Integer blockNumber)
null
if not setblockNumber
- the number of the block to findnull
IllegalArgumentException
- if parameter userBlock is null
IllegalArgumentException
- if parameter userBlock has an invalid block namepublic void addUserBlock(SwiftBlockUser userBlock)
userBlock
- the user defined blockIllegalArgumentException
- if parameter userBlock is null
IllegalArgumentException
- if parameter userBlock has an invalid block namepublic void removeUserBlock(Integer blockNumber)
blockNumber
- the block number to removeIllegalArgumentException
- if parameter blockNumber is null
IllegalArgumentException
- if parameter blockNumber is invalidSwiftBlockUser.isValidName(Integer)
public void removeUserBlock(String blockName)
blockName
- the block name to removeIllegalArgumentException
- if parameter blockName is null
IllegalArgumentException
- if parameter blockName is invalidpublic void clear()
public Boolean isFragment()
public Boolean isLastFragment()
public Integer fragmentCount()
public Integer fragmentNumber()
UnsupportedOperationException
- if the message is not a part of a fragmented messageprotected void unparsedTextVerify()
public UnparsedTextList getUnparsedTexts()
public void setUnparsedTexts(UnparsedTextList texts)
texts
- the new list of unparsed texts (may be null)public Integer getUnparsedTextsSize()
public Boolean unparsedTextIsMessage(Integer index)
index
- the unparsed text numberIllegalArgumentException
- if parameter index is null
IndexOutOfBoundsException
- if parameter index is out of boundspublic String unparsedTextGetText(Integer index)
index
- the unparsed text numberIllegalArgumentException
- if parameter index is null
IndexOutOfBoundsException
- if parameter index is out of boundspublic SwiftMessage unparsedTextGetAsMessage(Integer index)
index
- the unparsed text numberIllegalArgumentException
- if parameter index is null
public void unparsedTextAddText(String text)
text
- the unparsed text to appendIllegalArgumentException
- if parameter text is null
public void unparsedTextAddText(SwiftMessage message)
message
- the message to be appendedIllegalArgumentException
- if parameter message is null
public boolean isCOV()
true
if 119:COV is found at User Header (block3)public boolean isSTP()
true
if 119:STP is found at User Header (block3)public boolean isREMIT()
true
if 119:REMIT is found at User Header (block3)public String getSender()
For outgoing messages this is the the logical terminal at block 1, and for incoming messages this is logical terminal at the MIR of block 2.
for service message (example acknowledges) always returns the logical terminal from block1
null
if blocks 1 or 2 are not found or incompletepublic String getReceiver()
For outgoing messages this is the receiver address at block 2, and for incoming messages this is logical terminal at block 1.
for service message (example acknowledges) returns always null
null
if blocks 1 or 2 are not found or incompletepublic List<Field> fields(String... names)
names
- list of names to add in fields to searchIllegalArgumentException
- if names is null
public void removeEmptyBlocks()
public int getTypeInt()
public MessageIOType getDirection()
public boolean isOutgoing()
public boolean isInput()
isOutgoing()
public boolean isIncoming()
public boolean isOutput()
isIncoming()
public String getPDE()
public String getPDM()
public String getMIR()
public String getMUR()
public String getUUID()
public SequenceNode getParsedSequences()
public void setParsedSequences(SequenceNode parsedSequences)
public Field field(String name)
null
if nonename
- null
if not foundfields(String...)
public Boolean isLinked()
null
if cannot determinepublic List<SwiftTagListBlock> getLinkages()
null
if cannot determinepublic String toJson()
{ "version" : 1, "timestamp" : "2016-08-26T23:57:36Z", data" : { "block1" : { "applicationId" : "F", "serviceId" : "01", "logicalTerminal" : "FOOSEDR0AXXX", "sessionNumber" : "0000", "sequenceNumber" : "000000" } , "block2" : { "messageType" : "103", "receiverAddress" : "FOORECV0XXXX", "messagePriority" : "N", "deliveryMonitoring" : "null", "obsolescencePeriod" : "null" } , "block3" : { }, "block4" : [ { "20" : "REFERENCE" }, { "23B" : "CRED" }, { "32A" : "130204USD1234567,89" }, { "50K" : "/12345678901234567890\nFOOBANKXXXXX" }, { "59" : "/12345678901234567890\nJOE DOE" }, { "71A" : "OUR" } ] ,"block5" : { } } }
public final String toXml()
XMLWriterVisitor
,
XMLParser
public AbstractMT toMT()
If you have a MT102 in a SwiftMessage, this method is the same as invoking
new MT102(SwiftMessage)
.
For messages with service id 21 = GPA/FIN Message (ACK/NAK/UAK/UNK) it will
return an instance of AckSystemMessage
.
public boolean isType(int type)
Returns true if the message type is equal to the given number.
Notice this method only checks the message type number but can be combined with any
message variant check such as isSTP()
, isREMIT()
or isCOV()
to determine the message kind precisely.
The implementation uses getTypeInt()
type
- message type number to checkpublic boolean isType(int... types)
getTypeInt()
types
- message type numbers to checkpublic final boolean isCategory(MtCategory... categories)
categories
- the categories 0 to 9 to checkpublic final MtCategory getCategory()
getType()
to retrieve the message type of the message.null
if block 2 is not found or the message type is not category numberpublic final boolean isServiceMessage()
@Deprecated @ProwideDeprecated(phase2=_2018) public boolean isSystemMessage()
IMPORTANT: Note despite the method name this will NOT return true for FIN system messages (category 0).
It is just useful to detect acknowledges.
To check for system messages use isCategory(MtCategory...)
instead, passing zero as parameter
@deprecated this method is kept for backward compatibility but was replace by isServiceMessage21()
which
reflects what the method returns properly. Notice a "system message" is actually a message with category 0, which is
not the same as a "service message".
public boolean isServiceMessage21()
public boolean isAck()
public boolean isNack()
public MTVariant getVariant()
public List<String> getTagNames()
null
everpublic MtId getMtId()
null
if message is a service message