diff --git a/CHANGELOG.md b/CHANGELOG.md index 7af04bd49..012efa429 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Prowide Core - CHANGELOG +#### 9.3.19-SNAPSHOT + * Added default methods for sender, receiver, and identifier extraction to the MessageExtractionStrategy. + #### 9.3.18 - September 2023 * Added support for an optional `pw-swift-core.properties` to customize the behavior of the SafeXmlUtils class diff --git a/src/main/java/com/prowidesoftware/swift/model/MessageMetadataStrategy.java b/src/main/java/com/prowidesoftware/swift/model/MessageMetadataStrategy.java index fc33a357a..3d23040e6 100644 --- a/src/main/java/com/prowidesoftware/swift/model/MessageMetadataStrategy.java +++ b/src/main/java/com/prowidesoftware/swift/model/MessageMetadataStrategy.java @@ -48,4 +48,31 @@ public interface MessageMetadataStrategy { * Extracts the message trade date (only meaningful for some types of messages) */ Optional tradeDate(AbstractMessage message); + + /** + * Extracts the sender information from the message. + * This default implementation returns empty. + * @since 9.3.18 + */ + default Optional sender(AbstractMessage message) { + return Optional.empty(); + } + + /** + * Extracts the receiver information from the message. + * This default implementation returns empty. + * @since 9.3.18 + */ + default Optional receiver(AbstractMessage message) { + return Optional.empty(); + } + + /** + * Extracts the identifier from the message. + * This default implementation returns empty. + * @since 9.3.18 + */ + default Optional identifier(AbstractMessage message) { + return Optional.empty(); + } } diff --git a/src/main/java/com/prowidesoftware/swift/model/MtSwiftMessage.java b/src/main/java/com/prowidesoftware/swift/model/MtSwiftMessage.java index efaaec069..c7b5b2f60 100644 --- a/src/main/java/com/prowidesoftware/swift/model/MtSwiftMessage.java +++ b/src/main/java/com/prowidesoftware/swift/model/MtSwiftMessage.java @@ -282,12 +282,15 @@ private void updateAttributes(final SwiftMessage model, final MessageMetadataStr } else { // any other case we just update the metadata from the received message extractMetadata(model, metadataStrategy); - if (model.getMtId() != null) { - setIdentifier(model.getMtId().id()); - } + Optional identifier = metadataStrategy.identifier(model.toMT()); + identifier.ifPresent(this::setIdentifier); } + setFileFormat(FileFormat.FIN); - setSender(bic11(model.getSender())); + + Optional sender = metadataStrategy.sender(model.toMT()); + sender.ifPresent(s -> setSender(bic11(s))); + setChecksum(SwiftMessageUtils.calculateChecksum(model)); setChecksumBody(SwiftMessageUtils.calculateChecksum(model.getBlock4())); setLastModified(Calendar.getInstance()); @@ -295,7 +298,9 @@ private void updateAttributes(final SwiftMessage model, final MessageMetadataStr } private void extractMetadata(final SwiftMessage model, final MessageMetadataStrategy metadataStrategy) { - setReceiver(bic11(model.getReceiver())); + + Optional receiver = metadataStrategy.receiver(model.toMT()); + receiver.ifPresent(r -> setReceiver(bic11(r))); setDirection(model.getDirection()); setPde(model.getPDE()); diff --git a/src/main/java/com/prowidesoftware/swift/model/SwiftMessage.java b/src/main/java/com/prowidesoftware/swift/model/SwiftMessage.java index e7eb1f821..61075bd83 100644 --- a/src/main/java/com/prowidesoftware/swift/model/SwiftMessage.java +++ b/src/main/java/com/prowidesoftware/swift/model/SwiftMessage.java @@ -1056,49 +1056,19 @@ public boolean isREMIT() { } /** - * Gets the message sender BIC from the message headers. - *

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 - * - * @return the proper sender address or null if blocks 1 or 2 are not found or incomplete + * @see SwiftMessageUtils#sender(SwiftMessage) + * @since 9.3.18 */ public String getSender() { - try { - if (isServiceMessage() || getDirection() == MessageIOType.outgoing) { - return this.block1 == null ? null : this.block1.getLogicalTerminal(); - } else if ((getDirection() == MessageIOType.incoming) && (this.block2 != null)) { - return ((SwiftBlock2Output) this.block2).getMIRLogicalTerminal(); - } - } catch (final Exception e) { - log.severe("Exception occurred while retrieving sender's BIC from message data: " + e); - } - return null; + return SwiftMessageUtils.sender(this); } /** - * Gets the message receiver BIC from the message headers. - *

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) always returns null - * - * @return the proper receiver address or null if blocks 1 or 2 are not found or incomplete + * @see SwiftMessageUtils#receiver(SwiftMessage) + * @since 9.3.18 */ public String getReceiver() { - try { - if (isServiceMessage()) { - return null; - } else if (getDirection() == MessageIOType.incoming) { - return this.block1.getLogicalTerminal(); - } else if (getDirection() == MessageIOType.outgoing) { - return ((SwiftBlock2Input) this.block2).getReceiverAddress(); - } else { - return null; - } - } catch (final Exception e) { - log.severe("Exception occurred while retrieving receiver's BIC from message data: " + e); - return null; - } + return SwiftMessageUtils.receiver(this); } /** @@ -1810,13 +1780,13 @@ public MtId getMtId() { */ public BIC getCorrespondentBIC() { if (isOutgoing()) { - final String receiver = getReceiver(); + final String receiver = SwiftMessageUtils.receiver(this); if (receiver != null) { return new BIC(receiver); } } if (isIncoming()) { - final String sender = getSender(); + final String sender = SwiftMessageUtils.sender(this); if (sender != null) { return new BIC(sender); } diff --git a/src/main/java/com/prowidesoftware/swift/model/SwiftMessageUtils.java b/src/main/java/com/prowidesoftware/swift/model/SwiftMessageUtils.java index bf37e881f..8607188a2 100644 --- a/src/main/java/com/prowidesoftware/swift/model/SwiftMessageUtils.java +++ b/src/main/java/com/prowidesoftware/swift/model/SwiftMessageUtils.java @@ -287,6 +287,69 @@ public static Calendar tradeDate(final SwiftMessage m) { return null; } + /** + * Gets the message sender BIC from the message headers. + *

For outgoing messages this is 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 + * + * @return the proper sender address or null if blocks 1 or 2 are not found or incomplete + * @since 9.3.18 + */ + public static String sender(final SwiftMessage m) { + try { + if (m.isServiceMessage() || m.getDirection() == MessageIOType.outgoing) { + return m.getBlock1() == null ? null : m.getBlock1().getLogicalTerminal(); + } else if ((m.getDirection() == MessageIOType.incoming) && (m.getBlock2() != null)) { + return ((SwiftBlock2Output) m.getBlock2()).getMIRLogicalTerminal(); + } + } catch (final Exception e) { + log.severe("Exception occurred while retrieving sender's BIC from message data: " + e); + } + return null; + } + + /** + * Gets the message receiver BIC from the message headers. + *

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) always returns null + * + * @return the proper receiver address or null if blocks 1 or 2 are not found or incomplete + * @since 9.3.18 + */ + public static String receiver(final SwiftMessage m) { + try { + if (m.isServiceMessage()) { + return null; + } else if (m.getDirection() == MessageIOType.incoming) { + return m.getBlock1().getLogicalTerminal(); + } else if (m.getDirection() == MessageIOType.outgoing) { + return ((SwiftBlock2Input) m.getBlock2()).getReceiverAddress(); + } else { + return null; + } + } catch (final Exception e) { + log.severe("Exception occurred while retrieving receiver's BIC from message data: " + e); + return null; + } + } + + /** + * Get a string in the form of businessprocess.messagetype.variant + * + * @return a string with the MT message type identification + * @since 9.3.18 + */ + public static String identifier(final SwiftMessage m) { + try { + return m.getMtId().id(); + } catch (final Exception e) { + log.severe("Exception occurred while retrieving identifier from message data: " + e); + return null; + } + } + /** * Proprietary checksum for message integrity verification or duplicates detection. *

Please notice this is not the SWIFT trailer CHK field. diff --git a/src/main/java/com/prowidesoftware/swift/model/mt/AbstractMT.java b/src/main/java/com/prowidesoftware/swift/model/mt/AbstractMT.java index 048edd1fc..881666f56 100644 --- a/src/main/java/com/prowidesoftware/swift/model/mt/AbstractMT.java +++ b/src/main/java/com/prowidesoftware/swift/model/mt/AbstractMT.java @@ -421,7 +421,7 @@ public boolean isIncoming() { */ public String getSender() { if (getSwiftMessage() != null) { - return getSwiftMessage().getSender(); + return SwiftMessageUtils.sender(getSwiftMessage()); } return null; } @@ -472,7 +472,7 @@ public void setSender(BIC bic) { */ public String getReceiver() { if (getSwiftMessage() != null) { - return getSwiftMessage().getReceiver(); + return SwiftMessageUtils.receiver(getSwiftMessage()); } return null; } diff --git a/src/main/java/com/prowidesoftware/swift/model/mt/DefaultMtMetadataStrategy.java b/src/main/java/com/prowidesoftware/swift/model/mt/DefaultMtMetadataStrategy.java index cdc93efc1..e26b83750 100644 --- a/src/main/java/com/prowidesoftware/swift/model/mt/DefaultMtMetadataStrategy.java +++ b/src/main/java/com/prowidesoftware/swift/model/mt/DefaultMtMetadataStrategy.java @@ -59,8 +59,32 @@ public Optional tradeDate(AbstractMessage message) { return Optional.ofNullable(SwiftMessageUtils.tradeDate(asSwiftMessage(message))); } + /** + * Extracts the MT sender, if present, using {@link SwiftMessageUtils#sender(SwiftMessage)} + */ + @Override + public Optional sender(AbstractMessage message) { + return Optional.ofNullable(SwiftMessageUtils.sender(asSwiftMessage(message))); + } + + /** + * Extracts the MT receiver, if any, using {@link SwiftMessageUtils#receiver(SwiftMessage)} + */ + @Override + public Optional receiver(AbstractMessage message) { + return Optional.ofNullable(SwiftMessageUtils.receiver(asSwiftMessage(message))); + } + + /** + * Extracts the MT identifier, if any, using {@link SwiftMessageUtils#identifier(SwiftMessage)} + */ + @Override + public Optional identifier(AbstractMessage message) { + return Optional.ofNullable(SwiftMessageUtils.identifier(asSwiftMessage(message))); + } + private SwiftMessage asSwiftMessage(AbstractMessage message) { - if (message.isMT()) { + if (message != null && message.isMT()) { AbstractMT mt = (AbstractMT) message; return mt.getSwiftMessage(); }