Class SwiftMessage

  • All Implemented Interfaces:
    JsonSerializable, java.io.Serializable

    public class SwiftMessage
    extends java.lang.Object
    implements java.io.Serializable, JsonSerializable
    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).

    See Also:
    Serialized Form
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method 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​(java.lang.Object o)  
      Field field​(java.lang.String name)
      return first results of fields() or null if none
      java.util.List<Field> fields​(java.lang.String... names)
      Get all fields with the given name in the block 4.
      java.lang.Integer fragmentCount()
      Gets the total number of fragments of a fragmented message as informed in tag 203.
      java.lang.Integer fragmentNumber()
      Gets the number of this fragment
      static SwiftMessage fromJson​(java.lang.String json)
      This method deserializes the JSON data into a message object.
      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​(java.lang.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.
      BIC getCorrespondentBIC()
      Returns the correspondent BIC code from the headers.
      For an outgoing message, the BIC address identifies the receiver of the message.
      MessageIOType getDirection()
      Returns the message direction from block 2 or null if block 2 is not found or incomplete.
      java.util.List<SwiftTagListBlock> getLinkages()
      Return the message's LINK sequences if any.
      java.lang.String getMIR()
      The MIR (Message Input Reference) is a String of 28 characters, always local to the sender of the message.
      java.lang.String getMOR()
      The MOR (Message Output Reference) is a String of 28 characters, always local to the receiver of the message.
      MtId getMtId()
      Returns the MT message identification.
      Composed by the business process, message type and variant.
      java.lang.String getMUR()
      Gets MUR (Message User Reference) from field 108 in the user header block (block 3) or in the text block (block 4).
      java.lang.String getPDE()
      Gets PDE (Possible Duplicate Emission) flag from the trailer block or null if the trailer or the PDE field is not present
      java.lang.String getPDM()
      Gets PDM from the trailer block or null if the trailer or the PDM field is not present
      java.lang.String getReceiver()  
      java.lang.String getSender()  
      java.lang.String getServiceTypeIdentifier()
      Gets the Service Type Identifier (field 111 from block 3).
      java.lang.String getSignature()
      Gets the signature of the message (looks for an S block then the MDG tag)
      java.util.List<java.lang.String> getTagNames()
      Get a list of unique tagname contained in this message
      java.lang.String getType()
      Tell the message type associated with this object if a block 2 is present.
      int getTypeInt()
      Gets message type as an integer or -1 if an error occurs or it is not set.
      java.lang.String getUETR()
      Gets the Unique End to End Transaction Reference (field 121 from block 3).
      java.lang.String getUID​(java.util.Calendar created, java.lang.Long id)
      Gets a UID (Unique Identifier) for the message appending a suffix to the UUID generated with getUUID().
      UnparsedTextList getUnparsedTexts()
      returns the unparsed text list
      java.lang.Integer getUnparsedTextsSize()
      returns the size of the unparsed text list
      SwiftBlockUser getUserBlock​(java.lang.Integer blockNumber)
      Get a user defined block by number, may be null if not set
      SwiftBlockUser getUserBlock​(java.lang.String blockName)
      Get a user defined block by name, may be null if not set
      int getUserBlockPosition​(java.lang.String blockName)
      Finds the position of a given User Defined Block in the internal list
      java.util.List<SwiftBlockUser> getUserBlocks()
      Get the list of List of SwiftBlockUser user defined blocks.
      java.lang.String getUUID()
      Gets a UUID (User Unique Identifier) for the message conformed by: Direction: A single-character direction indicator; "I" for an outgoing message (input to the network) and "O" for an incoming message (output from the network).
      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).
      java.lang.Boolean isFragment()
      Checks if the message is a fragment
      boolean isGpi()
      Returns true if the message is part of the Global Payments Initiative (gpi) and thus requires the mandatory fields 111 and UETR for tracking within the SWIFT gpi service.
      boolean isIncoming()
      Returns true if the message is incoming (received from SWIFT), false other case; using the direction attribute.
      boolean isInput()
      Synonym to isOutgoing().
      java.lang.Boolean isLastFragment()
      Checks if the message is the last fragment
      java.lang.Boolean isLinked()
      Checks if the message is linked to other message based on the presence of a LINK sequence.
      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()
      Synonym to isIncoming().
      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 isType​(int type)
      Returns true if the message type is equal to the given number.
      boolean isType​(int... types)
      Returns true if the message type is equal to one of the given numbers.
      java.lang.String message()
      Serializes this message object into a String containing the FIN message.
      static SwiftMessage parse​(java.lang.String fin)
      Parses a the string content into a SwiftMessage.
      SwiftMessage removeEmptyBlocks()
      Checks all blocks (1 to 5) and if a block is empty, it is removed from the message.
      void removeUserBlock​(java.lang.Integer blockNumber)
      removes a user defined block to the message (if the block does not exists, nothing is done)
      void removeUserBlock​(java.lang.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
      SwiftMessage setMUR​(java.lang.String mur)
      Sets the MUR (Message User Reference) in the user header block.
      SwiftMessage setPDE()
      Sets the Possible Duplicate Emission tag with no value, in the trailer block (block 5),
      void setServiceTypeIdentifier​(java.lang.String serviceTypeIdentifier)
      Sets or updates the Service Type Identifier (field 111 in block 3).
      SwiftMessage setSignature​(java.lang.String signature)
      Sets the signature for the message (adds an S block with the MDG tag)
      java.lang.String setUETR()
      Creates and sets the Unique End to End Transaction Reference (field 121 in block 3).
      void setUETR​(java.lang.String uniqueEndToEndTransactionReference)
      Sets or updates the Unique End to End Transaction Reference (field 121 in block 3).
      void setUnparsedTexts​(UnparsedTextList texts)
      sets the list of unparsed texts
      void setUserBlocks​(java.util.List<SwiftBlockUser> userBlocks)
      Set the list of user defined blocks.
      This method is mainly needed for persistence services.
      void setVariant​(MTVariant variant)
      Sets or updates a variant (STP, REMIT, COV) in field 119 in block 3.
      java.lang.String toJson()
      Get a json representation of this object.
      AbstractMT toMT()
      Get the MTxxx instance that corresponds to the current message type.
      java.lang.String toString()
      Commons-lang reflection toString implementation
      java.lang.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​(SwiftMessage message)
      adds a new unparsed text from a message
      void unparsedTextAddText​(java.lang.String text)
      adds a new unparsed text
      SwiftMessage unparsedTextGetAsMessage​(java.lang.Integer index)
      get an unparsed text as a parsed swift message
      java.lang.String unparsedTextGetText​(java.lang.Integer index)
      get an unparsed text
      java.lang.Boolean unparsedTextIsMessage​(java.lang.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
      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.
      void visit​(IMessageVisitor visitor)
      Visit the current message with the given visitor.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • SwiftMessage

        public SwiftMessage()
        Default constructor. Must be called since here is performed default handler registration
        See Also:
        SwiftMessage(boolean)
      • SwiftMessage

        public SwiftMessage​(boolean initBlocks)
        Constructor that initializes blocks
        Parameters:
        initBlocks - when false the message will not have any blocks when constructed, if true blocks are created, just like with default constructor
      • SwiftMessage

        public SwiftMessage​(boolean initBlocks,
                            UnparsedTextList unparsedText)
        Constructor for an unparsed text list that initializes blocks
        Parameters:
        initBlocks - when false the message will not have any blocks when constructed, if true blocks are created, just like with default consturctor
        unparsedText - the list of unparsed texts
        See Also:
        SwiftMessage()
      • SwiftMessage

        public SwiftMessage​(UnparsedTextList unparsedText)
        Constructor for an unparsed text list
        Parameters:
        unparsedText - the list of unparsed texts
        See Also:
        SwiftMessage()
    • Method Detail

      • parse

        public static SwiftMessage parse​(java.lang.String fin)
                                  throws java.io.IOException
        Parses a the string content into a SwiftMessage.

        If the file contains more than a message it will parse the first one. If the string is empty, does not contain any MT message, the message type is not set or an error occurs reading and parsing the message content; this method returns null.

        The implementation uses the default parser behavior which is lenient and will do a best effort to read as much from the message content as possible regardless of the content and block boundaries being valid or not. For instance, it will read the headers even if the value length is incorrect, and it will read the text block (block 4) even if it is missing the closing hyphen and bracket. For more options check SwiftParser.setConfiguration(SwiftParserConfiguration)

        Parameters:
        fin - string a string containing a swift MT message
        Returns:
        parser message or null if string content could not be parsed
        Throws:
        java.io.IOException - if an error occurs in the parser during reading
        Since:
        7.8.8
      • visit

        public 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.

        To serialize in SWIFT native format with block boundaries check SwiftWriter.writeBlock3(SwiftBlock3, java.io.Writer)

        Parameters:
        block - the block containing the tags to visit
        visitor - the visitor to use
        Throws:
        java.lang.IllegalArgumentException - if parameter block or visitor are null
        Since:
        5.0
      • visit

        public 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.

        To serialize in SWIFT native format with block boundaries check SwiftWriter.writeBlock4(SwiftBlock4, java.io.Writer)

        Parameters:
        block - the block containing the tags to visit
        visitor - the visitor to use
        Throws:
        java.lang.IllegalArgumentException - if parameter block or visitor are null
        Since:
        5.0
      • visit

        public 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.

        To serialize in SWIFT native format with block boundaries check SwiftWriter.writeBlock5(SwiftBlock5, java.io.Writer)

        Parameters:
        block - the block containing the tags to visit
        visitor - the visitor to use
        Throws:
        java.lang.IllegalArgumentException - if parameter block or visitor are null
        Since:
        5.0
      • visit

        public 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.
        Parameters:
        block - the block containing the tags to visit
        visitor - the visitor to use
        Throws:
        java.lang.IllegalArgumentException - if parameter block or visitor are null
        Since:
        5.0
      • fromJson

        public static SwiftMessage fromJson​(java.lang.String json)
        This method deserializes the JSON data into a message object.
        Parameters:
        json - JSON data
        Returns:
        message object
        Since:
        7.9.8
        See Also:
        toJson()
      • equals

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • getBlock

        public SwiftBlock getBlock​(int b)
        Get the block number specified by b.
        Parameters:
        b - the block number to retrieve, must be greater or equal to 1 and smaller or equal to 5.
        Returns:
        the block number specified in this message
        Throws:
        java.lang.IllegalArgumentException - if b < 1 or b > 5
      • toString

        public java.lang.String toString()
        Commons-lang reflection toString implementation
        Overrides:
        toString in class java.lang.Object
      • addBlock

        public void addBlock​(SwiftBlock b)
        Add a block to this message.

        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

        Parameters:
        b - the block to add, may be null in which case nothing happens
        Throws:
        java.lang.IllegalArgumentException - b is null or the method getInt in the block returns a value out of range (non user blocks)
      • getType

        public java.lang.String getType()
        Tell the message type associated with this object if a block 2 is present.
        Returns:
        a String containing the SWIFT numeric code for the message types or null if the message does not have a block 2.
        See Also:
        SwiftBlock2.getMessageType()
      • visit

        public void visit​(IMessageVisitor visitor)
        Visit the current message with the given visitor. Writes all present blocks 1 to 5, and also the user blocks if any.
        Parameters:
        visitor - the visitor to use
        Throws:
        java.lang.IllegalArgumentException - if parameter visitor is null
        See Also:
        SwiftWriter.writeMessage(SwiftMessage, java.io.Writer)
      • getBlockCount

        public int getBlockCount()
        Get the number of blocks in this message, including the user blocks
        Returns:
        an int greater or equal to zero
      • getBlock1

        public SwiftBlock1 getBlock1()
        Get block number 1 of this message, may be null if not set
        Returns:
        the block 1 of the message or null
      • setBlock1

        public void setBlock1​(SwiftBlock1 block1)
        Set the block 1 of the message
        Parameters:
        block1 - the content of the block 1
      • getBlock2

        public SwiftBlock2 getBlock2()
        Get block number 2 of this message, may be null if not set
        Returns:
        the block 2 of the message or null
      • setBlock2

        public void setBlock2​(SwiftBlock2 block2)
        Set the block 2 of the message
        Parameters:
        block2 - the content of the block 1
      • getBlock3

        public SwiftBlock3 getBlock3()
        Get block number 3 of this message, may be null if not set
        Returns:
        the block 3 of the message or null
      • setBlock3

        public void setBlock3​(SwiftBlock3 block3)
        Set the block 3 of the message
        Parameters:
        block3 - the content of the block 1
      • getBlock4

        public SwiftBlock4 getBlock4()
        Get block number 4 of this message, may be null if not set
        Returns:
        the block 4 of the message or null
      • setBlock4

        public void setBlock4​(SwiftBlock4 block4)
        Set the block 4 of the message
        Parameters:
        block4 - the content of the block 1
      • getBlock5

        public SwiftBlock5 getBlock5()
        Get block number 5 of this message, may be null if not set
        Returns:
        the block 5 of the message or null
      • setBlock5

        public void setBlock5​(SwiftBlock5 block5)
        Set the block 5 of the message
        Parameters:
        block5 - the content of the block 5
      • getUserBlockPosition

        public int getUserBlockPosition​(java.lang.String blockName)
        Finds the position of a given User Defined Block in the internal list
        Parameters:
        blockName - the block name to find may be empty or null, in which case this method does nothing
        Returns:
        the position or -1 if not found
        Since:
        5.0
      • getUserBlocks

        public java.util.List<SwiftBlockUser> getUserBlocks()
        Get the list of List of SwiftBlockUser user defined blocks. The requested object may be null if the message was cleared or not initialized.
        Returns:
        the list or user blocks or null
        Since:
        5.0
      • setUserBlocks

        public void setUserBlocks​(java.util.List<SwiftBlockUser> userBlocks)
        Set the list of user defined blocks.
        This method is mainly needed for persistence services.
        Parameters:
        userBlocks - the new list of user defined blocks
        Throws:
        java.lang.IllegalArgumentException - if parameter userBlocks is null
        java.lang.IllegalArgumentException - if parameter userBlocks has elements of class other than SwiftBlockUser
        Since:
        5.0
        See Also:
        SwiftBlockUser
      • getUserBlock

        public SwiftBlockUser getUserBlock​(java.lang.String blockName)
        Get a user defined block by name, may be null if not set
        Parameters:
        blockName - the name of the block to find
        Returns:
        the requested user defined block or null
        Throws:
        java.lang.IllegalArgumentException - if parameter blockName is null
        java.lang.IllegalArgumentException - if parameter blockName has an invalid block name
        Since:
        5.0
      • getUserBlock

        public SwiftBlockUser getUserBlock​(java.lang.Integer blockNumber)
        Get a user defined block by number, may be null if not set
        Parameters:
        blockNumber - the number of the block to find
        Returns:
        the requested user defined block or null
        Throws:
        java.lang.IllegalArgumentException - if parameter userBlock is null
        java.lang.IllegalArgumentException - if parameter userBlock has an invalid block name
        Since:
        5.0
      • addUserBlock

        public void addUserBlock​(SwiftBlockUser userBlock)
        Add a user defined block to the message (if the block already exists, it is replaced)
        Parameters:
        userBlock - the user defined block
        Throws:
        java.lang.IllegalArgumentException - if parameter userBlock is null
        java.lang.IllegalArgumentException - if parameter userBlock has an invalid block name
        Since:
        5.0
      • removeUserBlock

        public void removeUserBlock​(java.lang.Integer blockNumber)
        removes a user defined block to the message (if the block does not exists, nothing is done)
        Parameters:
        blockNumber - the block number to remove
        Throws:
        java.lang.IllegalArgumentException - if parameter blockNumber is null
        java.lang.IllegalArgumentException - if parameter blockNumber is invalid
        Since:
        5.0
        See Also:
        SwiftBlockUser.isValidName(Integer)
      • removeUserBlock

        public void removeUserBlock​(java.lang.String blockName)
        removes a user defined block to the message (if the block does not exists, nothing is done)
        Parameters:
        blockName - the block name to remove
        Throws:
        java.lang.IllegalArgumentException - if parameter blockName is null
        java.lang.IllegalArgumentException - if parameter blockName is invalid
        Since:
        5.0
      • clear

        public void clear()
        remove all blocks from these message, including user blocks
      • isFragment

        public java.lang.Boolean isFragment()
        Checks if the message is a fragment
        Returns:
        true if the message is a fragment
        Since:
        5.0
      • isLastFragment

        public java.lang.Boolean isLastFragment()
        Checks if the message is the last fragment
        Returns:
        true if the message is the last fragment of a fragmented message
        Since:
        5.0
      • fragmentCount

        public java.lang.Integer fragmentCount()
        Gets the total number of fragments of a fragmented message as informed in tag 203.
        Returns:
        the total number of fragments or zero if the message is not fragmented
        Since:
        5.0
      • fragmentNumber

        public java.lang.Integer fragmentNumber()
        Gets the number of this fragment
        Returns:
        the number of this fragment
        Throws:
        java.lang.UnsupportedOperationException - if the message is not a part of a fragmented message
        Since:
        5.0
      • getSignature

        public java.lang.String getSignature()
        Gets the signature of the message (looks for an S block then the MDG tag)
        Returns:
        the signature of the message (or null if none exists)
        Since:
        7.10.4
      • setSignature

        public SwiftMessage setSignature​(java.lang.String signature)
        Sets the signature for the message (adds an S block with the MDG tag)
        Parameters:
        signature - the signature to set in block S
        Returns:
        this
        Since:
        7.10.4
      • unparsedTextVerify

        protected void unparsedTextVerify()
        verifies that the unparsed text list exists
      • getUnparsedTexts

        public UnparsedTextList getUnparsedTexts()
        returns the unparsed text list
        Returns:
        the unparsed text attached to this message
      • setUnparsedTexts

        public void setUnparsedTexts​(UnparsedTextList texts)
        sets the list of unparsed texts
        Parameters:
        texts - the new list of unparsed texts (may be null)
      • getUnparsedTextsSize

        public java.lang.Integer getUnparsedTextsSize()
        returns the size of the unparsed text list
        Returns:
        the count of unparsed texts attached to this message
      • unparsedTextIsMessage

        public java.lang.Boolean unparsedTextIsMessage​(java.lang.Integer index)
        decides if a specific text (by index) is likely a SWIFT FIN message. Exceptions are inherited from base implementation methods.
        Parameters:
        index - the unparsed text number
        Returns:
        true if the unparsed text at position index is a full SWIFT message
        Throws:
        java.lang.IllegalArgumentException - if parameter index is null
        java.lang.IndexOutOfBoundsException - if parameter index is out of bounds
      • unparsedTextGetText

        public java.lang.String unparsedTextGetText​(java.lang.Integer index)
        get an unparsed text
        Parameters:
        index - the unparsed text number
        Returns:
        the requested text
        Throws:
        java.lang.IllegalArgumentException - if parameter index is null
        java.lang.IndexOutOfBoundsException - if parameter index is out of bounds
      • unparsedTextGetAsMessage

        public SwiftMessage unparsedTextGetAsMessage​(java.lang.Integer index)
        get an unparsed text as a parsed swift message
        Parameters:
        index - the unparsed text number
        Returns:
        the unparsed text at position index parsed into a SwiftMessage object
        Throws:
        java.lang.IllegalArgumentException - if parameter index is null
      • unparsedTextAddText

        public void unparsedTextAddText​(java.lang.String text)
        adds a new unparsed text
        Parameters:
        text - the unparsed text to append
        Throws:
        java.lang.IllegalArgumentException - if parameter text is null
      • unparsedTextAddText

        public void unparsedTextAddText​(SwiftMessage message)
        adds a new unparsed text from a message
        Parameters:
        message - the message to be appended
        Throws:
        java.lang.IllegalArgumentException - if parameter message is null
      • isCOV

        public boolean isCOV()
        Checks if the message is a cover payment, based on the content of the User Header (block3).
        Returns:
        true if 119:COV is found at User Header (block3)
      • isSTP

        public boolean isSTP()
        Checks if the message is Straight Through Processing (STP), based on the content of the User Header (block3).
        Returns:
        true if 119:STP is found at User Header (block3)
      • isREMIT

        public boolean isREMIT()
        Checks if the message is a remit, based on the content of the User Header (block3).
        Returns:
        true if 119:REMIT is found at User Header (block3)
        Since:
        7.7
      • fields

        public java.util.List<Field> fields​(java.lang.String... names)
        Get all fields with the given name in the block 4. Only direct naming is supported, 55a notation is NOT SUPPORTED.
        Parameters:
        names - list of names to add in fields to search
        Returns:
        a list with fields matching the given names. an empty list if none found
        Throws:
        java.lang.IllegalArgumentException - if names is null
      • removeEmptyBlocks

        public SwiftMessage removeEmptyBlocks()
        Checks all blocks (1 to 5) and if a block is empty, it is removed from the message.
        Returns:
        the message
        Since:
        6.4, 8.0.3 returns this
      • getTypeInt

        public int getTypeInt()
        Gets message type as an integer or -1 if an error occurs or it is not set.
        Returns:
        the message type number or -1 if the message type is invalid or block 2 not present (for instance if the message is a service message)
        Since:
        6.4.1
      • isOutgoing

        public boolean isOutgoing()
        Returns true if the message is outgoing (sent to SWIFT), false other case; using the direction attribute. If block 2 is missign or direction cannot be determined, returns false.
        Returns:
        true if message is outgoing
        Since:
        7.8.4
      • isInput

        public boolean isInput()
        Synonym to isOutgoing().
        Returns:
        true if message is outgoing
        Since:
        7.8.4
        See Also:
        isOutgoing()
      • isIncoming

        public boolean isIncoming()
        Returns true if the message is incoming (received from SWIFT), false other case; using the direction attribute. If block 2 is missign or direction cannot be determined, returns false.
        Returns:
        true if message is incoming
        Since:
        7.8.4
      • isOutput

        public boolean isOutput()
        Synonym to isIncoming().
        Returns:
        true if message is incoming
        Since:
        7.8.4
        See Also:
        isIncoming()
      • getPDE

        public java.lang.String getPDE()
        Gets PDE (Possible Duplicate Emission) flag from the trailer block or null if the trailer or the PDE field is not present

        Notice the PDE tag could hold no value, so in that case empty string is returned, meaning the flag is set but with no value.

        Returns:
        PDE
        Since:
        7.0
      • setPDE

        public SwiftMessage setPDE()
        Sets the Possible Duplicate Emission tag with no value, in the trailer block (block 5),

        If the field exists, its value will be overwritten.

        Returns:
        the swift message object
        Since:
        8.0.2
      • getPDM

        public java.lang.String getPDM()
        Gets PDM from the trailer block or null if the trailer or the PDM field is not present
        Returns:
        PDM
        Since:
        7.0
      • getMIR

        public java.lang.String getMIR()
        The MIR (Message Input Reference) is a String of 28 characters, always local to the sender of the message. It includes the date the sender sent the message to SWIFT, followed by the full LT address of the sender of the message, and the sender's session and sequence to SWIFT: YYMMDD BANKBEBBAXXX 2222 123456. It is only available in incoming messages (received from SWIFT).
        Returns:
        MIR string
        Since:
        7.0
      • getMOR

        public java.lang.String getMOR()
        The MOR (Message Output Reference) is a String of 28 characters, always local to the receiver of the message. It includes the message output date, the address of the receiver, the output session number, and the output sequence number.: YYMMDD BANKBEBBAXXX 2222 123456. It is only available in incoming messages (received from SWIFT).
        Returns:
        MOR string
        Since:
        9.2.7
      • getMUR

        public java.lang.String getMUR()
        Gets MUR (Message User Reference) from field 108 in the user header block (block 3) or in the text block (block 4). Notice for user to user messages this field is located at the user header, however for system messages (category 0) the field is located at the text block.

        The MUR is the Message User Reference used by applications for reconciliation with ACK. It is a free-format field in which users may specify their own reference of up to 16 characters of the permitted character set.

        Returns:
        the value of field 108 if found, or null when not found neither in block 3 or block 4
        Since:
        7.0
      • setMUR

        public SwiftMessage setMUR​(java.lang.String mur)
        Sets the MUR (Message User Reference) in the user header block.

        If a MUR field is present, its value will be overwritten.

        The MUR is the Message User Reference used by applications for reconciliation with ACK. It is a free-format field in which users may specify their own reference of up to 16 characters of the permitted character set, and it is contained in a 108 field at the message user header (block 3).

        Parameters:
        mur - a non blank MUR value to set, if value is blank this method does nothing
        Returns:
        this
        Since:
        7.10.4
      • getUUID

        public java.lang.String getUUID()
        Gets a UUID (User Unique Identifier) for the message conformed by:
        • Direction: A single-character direction indicator; "I" for an outgoing message (input to the network) and "O" for an incoming message (output from the network). Defaults to "I".
        • The correspondent BIC 11 code; the receiver for an outgoing messages and the sender for an incoming message.
        • Message type: the 3-character number identifying the specific message.
        • Reference: field 20 or field 20C:SEME returned by SwiftMessageUtils.reference(SwiftMessage)

        Notice despite the name this identifier is unique only in the context of a specific message management platform, since all its values could be repeated from one installation to another. To make it completely unique in your application context, consider using getUID(Calendar, Long)

        Returns:
        UUID
        Since:
        7.0
      • getUID

        public java.lang.String getUID​(java.util.Calendar created,
                                       java.lang.Long id)
        Gets a UID (Unique Identifier) for the message appending a suffix to the UUID generated with getUUID().

        The suffix is a system-generated value that can help uniquely identify a message. The suffix generated by this method is similar to the suffix used by SWIFT Alliance Lite. The first part is the creation date of the message in YYMMDD format, a six-digit number. The second part consists of 1-10 left padded digit number generated from the container application/system incremental identifier.

        Parameters:
        created - optional creation date, if provided, the YYMMDD will be appended as first part of the suffix
        id - optional incremental identifier number from the application, if provided it will be appended as second part of the suffix
        Returns:
        the created UID
        Since:
        7.9.5
      • field

        public Field field​(java.lang.String name)
        return first results of fields() or null if none
        Parameters:
        name - name of field in block 4
        Returns:
        null if not found
        See Also:
        fields(String...)
      • isLinked

        public java.lang.Boolean isLinked()
        Checks if the message is linked to other message based on the presence of a LINK sequence.
        Returns:
        true if the message has a LINK sequence, false if it hasn't, and null if cannot determine
        Since:
        7.4
      • getLinkages

        public java.util.List<SwiftTagListBlock> getLinkages()
        Return the message's LINK sequences if any.
        Returns:
        a block containing the found linkage sequences or null if cannot determine
        Since:
        7.4
      • toJson

        public java.lang.String toJson()
        Get a json representation of this object.
        Generated JSON string will contain additional properties with version number and timestamp, while the actual SwiftMessage serialization is put into a data element.

        Example:

         { "version": 2, "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"
          }
          "block4": {
              "tags": [
                  { "20": "REFERENCE" },
                  { "23B": "CRED" },
                  { "32A": "130204USD1234567,89" },
                  { "50K": "/12345678901234567890\nFOOBANKXXXXX" },
                  { "59": "/12345678901234567890\nJOE DOE" },
                  { "71A": "OUR" }
              ]
            }
          }
          
        Specified by:
        toJson in interface JsonSerializable
        Returns:
        the message content as JSON
        Since:
        7.5
      • toXml

        public final java.lang.String toXml()
        Gets a proprietary XML representation of this message.
        Notice: it is neither a standard nor the MX version of this MT.
        Returns:
        the MT message serialized into the proprietary XML
        Since:
        7.8.4
        See Also:
        XMLWriterVisitor, XMLParser
      • toMT

        public AbstractMT toMT()
        Get the MTxxx instance that corresponds to the current message type.

        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 ServiceMessage21.

        Returns:
        created specific MT object or null if the message type is not set or an error occurs during message creation
      • isType

        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()

        Parameters:
        type - message type number to check
        Returns:
        true if message type matches, false if does not match or cannot be determined because the message content is invalid
        Since:
        7.8.9
      • isType

        public boolean isType​(int... types)
        Returns true if the message type is equal to one of the given numbers. The implementation uses getTypeInt()
        Parameters:
        types - message type numbers to check
        Returns:
        true if message type matches, false if does not match or cannot be determined because the message content is invalid
        Since:
        7.7
      • isCategory

        public final boolean isCategory​(MtCategory... categories)
        Returns true if the message category is equal to one of the given by parameter
        Parameters:
        categories - the categories 0 to 9 to check
        Returns:
        true if message category, false if does not match or cannot be determined because the message content is invalid or the categories parameter contains values other than 0 to 9
        Since:
        7.8.8
      • getCategory

        public final MtCategory getCategory()
        Returns the message category from the message type. This implementation uses getType() to retrieve the message type of the message.
        Returns:
        the category found as the first digit of the message type or null if block 2 is not found or the message type is not category number
      • isServiceMessage

        public final boolean isServiceMessage()
        Returns true if message service id is anything but 01 = GPA/FIN Message (system and user-to-user)
        Returns:
        true if message is a service message, false otherwise
        Since:
        7.8.8
      • isServiceMessage21

        public boolean isServiceMessage21()
        Returns true if message service id is 21 = GPA/FIN Message (ACK/NAK/UAK/UNK)
        Returns:
        true if it is a service message for acknowledgment, false if not or header is null and service id cannot be determined
        Since:
        7.8.9
      • isAck

        public boolean isAck()
        Returns true if this message is an ACK. This is determined by testing first if it is a system message, and second the value of tag 451
        Returns:
        true if ACK, false otherwise
        Since:
        7.8
      • isNack

        public boolean isNack()
        Returns true if this message is an NACK. This is determined by testing first if it is a system message, and second the value of tag 451
        Returns:
        true if NACK, false otherwise
        Since:
        7.8
      • getVariant

        public MTVariant getVariant()
        Returns:
        the corresponding MT variant or null if flag field is not present
        Since:
        7.8
      • setVariant

        public void setVariant​(MTVariant variant)
        Sets or updates a variant (STP, REMIT, COV) in field 119 in block 3.

        If the field already exists, its value will be updated; otherwise a new field will be created

        Parameters:
        variant - the variant (validation flag) to set in field 119
        Since:
        7.10.0
      • getTagNames

        public java.util.List<java.lang.String> getTagNames()
        Get a list of unique tagname contained in this message
        Returns:
        the list of tagnames or an empty list, does not return null ever
        Since:
        7.8
      • getMtId

        public MtId getMtId()
        Returns the MT message identification.
        Composed by the business process, message type and variant. Example: fin.103.STP
        Returns:
        the constructed message id or null if message is a service message
        Since:
        7.8.4
      • getCorrespondentBIC

        public BIC getCorrespondentBIC()
        Returns the correspondent BIC code from the headers.
        For an outgoing message, the BIC address identifies the receiver of the message. Where for an incoming message it identifies the sender of the message.
        Returns:
        the correspondent BIC code or null if headers are not properly set
        Since:
        7.9.5
      • getServiceTypeIdentifier

        public java.lang.String getServiceTypeIdentifier()
        Gets the Service Type Identifier (field 111 from block 3).

        This field is used by the SWIFT gpi service to track payments messages (category 1 and 2).

        Returns:
        the Service Type Identifier value or null if block3 or field 111 in block3 are not present
        Since:
        7.10.0
      • setServiceTypeIdentifier

        public void setServiceTypeIdentifier​(java.lang.String serviceTypeIdentifier)
        Sets or updates the Service Type Identifier (field 111 in block 3).

        If the field already exists, its value will be updated; otherwise a new field will be created

        This field is used by the SWIFT gpi service to track payments messages (category 1 and 2).

        Parameters:
        serviceTypeIdentifier - the value for field 111
        Since:
        7.10.0
      • getUETR

        public java.lang.String getUETR()
        Gets the Unique End to End Transaction Reference (field 121 from block 3).

        This field is used by the SWIFT gpi service to track payments messages (category 1 and 2).

        Returns:
        the UETR value or null if block3 or field 121 in block3 are not present
        Since:
        7.10.0
      • setUETR

        public void setUETR​(java.lang.String uniqueEndToEndTransactionReference)
        Sets or updates the Unique End to End Transaction Reference (field 121 in block 3).

        If the field already exists, its value will be updated; otherwise a new field will be created

        This field is used by the SWIFT gpi service to track payments messages (category 1 and 2).

        Parameters:
        uniqueEndToEndTransactionReference - the value for field 121
        Since:
        7.10.0
      • setUETR

        public java.lang.String setUETR()
        Creates and sets the Unique End to End Transaction Reference (field 121 in block 3).

        If the field already exists, its value will be updated

        This field is used by the SWIFT gpi service to track payments messages (category 1 and 2).

        Returns:
        the UETR created (new value of field 121 in block3 after the operation)
        Since:
        7.10.0
      • isGpi

        public boolean isGpi()
        Returns true if the message is part of the Global Payments Initiative (gpi) and thus requires the mandatory fields 111 and UETR for tracking within the SWIFT gpi service.

        Notice this only reflects the mandatory GPI service message types for outgoing messages. More message types would be included as part of the GPI service if the application provider chooses to support the optional g4C and gFIT services.

        Returns:
        true if the message type is 103, 199, 299, 192, 196, 202COV or 205COV
        Since:
        7.10.0
        See Also:
        setUETR()
      • message

        public java.lang.String message()
        Serializes this message object into a String containing the FIN message.
        Returns:
        a string with the FIN format representation of the message
        Since:
        9.2.13