Skip to content

Commit

Permalink
Add-sender-receiver-identifier-to-MessageMetadataStrategy
Browse files Browse the repository at this point in the history
  • Loading branch information
ptorres-prowide committed Sep 20, 2023
1 parent 92c6a00 commit 2fa50fe
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 46 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,31 @@ public interface MessageMetadataStrategy {
* Extracts the message trade date (only meaningful for some types of messages)
*/
Optional<Calendar> tradeDate(AbstractMessage message);

/**
* Extracts the sender information from the message.
* This default implementation returns empty.
* @since 9.3.18
*/
default Optional<String> sender(AbstractMessage message) {
return Optional.empty();
}

/**
* Extracts the receiver information from the message.
* This default implementation returns empty.
* @since 9.3.18
*/
default Optional<String> receiver(AbstractMessage message) {
return Optional.empty();
}

/**
* Extracts the identifier from the message.
* This default implementation returns empty.
* @since 9.3.18
*/
default Optional<String> identifier(AbstractMessage message) {
return Optional.empty();
}
}
15 changes: 10 additions & 5 deletions src/main/java/com/prowidesoftware/swift/model/MtSwiftMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -282,20 +282,25 @@ 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<String> identifier = metadataStrategy.identifier(model.toMT());
identifier.ifPresent(this::setIdentifier);
}

setFileFormat(FileFormat.FIN);
setSender(bic11(model.getSender()));

Optional<String> sender = metadataStrategy.sender(model.toMT());
sender.ifPresent(s -> setSender(bic11(s)));

setChecksum(SwiftMessageUtils.calculateChecksum(model));
setChecksumBody(SwiftMessageUtils.calculateChecksum(model.getBlock4()));
setLastModified(Calendar.getInstance());
setMur(model.getMUR());
}

private void extractMetadata(final SwiftMessage model, final MessageMetadataStrategy metadataStrategy) {
setReceiver(bic11(model.getReceiver()));

Optional<String> receiver = metadataStrategy.receiver(model.toMT());
receiver.ifPresent(r -> setReceiver(bic11(r)));
setDirection(model.getDirection());

setPde(model.getPDE());
Expand Down
46 changes: 8 additions & 38 deletions src/main/java/com/prowidesoftware/swift/model/SwiftMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -1056,49 +1056,19 @@ public boolean isREMIT() {
}

/**
* Gets the message sender BIC from the message headers.
* <p>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.
* <p>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.
* <p>For outgoing messages this is the receiver address at block 2,
* and for incoming messages this is logical terminal at block 1.
* <p>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);
}

/**
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,69 @@ public static Calendar tradeDate(final SwiftMessage m) {
return null;
}

/**
* Gets the message sender BIC from the message headers.
* <p>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.
* <p>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.
* <p>For outgoing messages this is the receiver address at block 2,
* and for incoming messages this is logical terminal at block 1.
* <p>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.
* <p>Please notice <strong>this is not the SWIFT trailer CHK field</strong>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ public boolean isIncoming() {
*/
public String getSender() {
if (getSwiftMessage() != null) {
return getSwiftMessage().getSender();
return SwiftMessageUtils.sender(getSwiftMessage());
}
return null;
}
Expand Down Expand Up @@ -472,7 +472,7 @@ public void setSender(BIC bic) {
*/
public String getReceiver() {
if (getSwiftMessage() != null) {
return getSwiftMessage().getReceiver();
return SwiftMessageUtils.receiver(getSwiftMessage());
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,32 @@ public Optional<Calendar> tradeDate(AbstractMessage message) {
return Optional.ofNullable(SwiftMessageUtils.tradeDate(asSwiftMessage(message)));
}

/**
* Extracts the MT sender, if present, using {@link SwiftMessageUtils#sender(SwiftMessage)}
*/
@Override
public Optional<String> sender(AbstractMessage message) {
return Optional.ofNullable(SwiftMessageUtils.sender(asSwiftMessage(message)));
}

/**
* Extracts the MT receiver, if any, using {@link SwiftMessageUtils#receiver(SwiftMessage)}
*/
@Override
public Optional<String> receiver(AbstractMessage message) {
return Optional.ofNullable(SwiftMessageUtils.receiver(asSwiftMessage(message)));
}

/**
* Extracts the MT identifier, if any, using {@link SwiftMessageUtils#identifier(SwiftMessage)}
*/
@Override
public Optional<String> 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();
}
Expand Down

0 comments on commit 2fa50fe

Please sign in to comment.