diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b62c826..fedbe4f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - support for Kotlin - preconditions which observe mdib changes during the test run +- storing of IP addresses of inbound messages in the database ### Changed diff --git a/sdccc/src/main/java/com/draeger/medical/sdccc/messages/MessageStorage.java b/sdccc/src/main/java/com/draeger/medical/sdccc/messages/MessageStorage.java index a9f9756c..6584d989 100644 --- a/sdccc/src/main/java/com/draeger/medical/sdccc/messages/MessageStorage.java +++ b/sdccc/src/main/java/com/draeger/medical/sdccc/messages/MessageStorage.java @@ -46,6 +46,7 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Set; import java.util.Spliterator; import java.util.Spliterators; @@ -84,6 +85,7 @@ import org.somda.sdc.dpws.soap.CommunicationContext; import org.somda.sdc.dpws.soap.HttpApplicationInfo; import org.somda.sdc.dpws.soap.SoapConstants; +import org.somda.sdc.dpws.soap.TransportInfo; import org.somda.sdc.dpws.soap.wsaddressing.WsAddressingConstants; /** @@ -91,6 +93,7 @@ */ @Singleton public class MessageStorage implements AutoCloseable { + private static final Logger LOG = LogManager.getLogger(MessageStorage.class); private static final int FETCH_SIZE = 10; @@ -310,7 +313,52 @@ protected MessageContent convertMessageToMessageContent(final Message message) { mdibVersionGroups, actions, message.getID(), - isSOAP); + isSOAP, + getSender(message)); + } + + private String getSender(final Message message) { + final CommunicationContext communicationContext = message.getCommunicationContext(); + if (communicationContext == null) { + // NOTE: this should never happen. If it does, this should be considered a bug. + LOG.trace("Encountered message (uuid={}) without a CommunicationContext.", message.getID()); + testRunObserver.invalidateTestRun("Encountered message without a CommunicationContext."); + return null; + } + final TransportInfo transportInfo = communicationContext.getTransportInfo(); + if (transportInfo == null) { + // NOTE: this should never happen. If it does, this should be considered a bug. + LOG.trace("Encountered message (uuid={}) without a TransportInfo.", message.getID()); + testRunObserver.invalidateTestRun("Encountered message without a TransportInfo."); + return null; + } + if (message.getDirection() == CommunicationLog.Direction.INBOUND) { + final Optional remoteAddress = transportInfo.getRemoteAddress(); + if (remoteAddress.isEmpty()) { + // NOTE: this should never happen. If it does, this should be considered a bug. + LOG.trace("Encountered inbound message (uuid={}) without a remoteAddress.", message.getID()); + testRunObserver.invalidateTestRun("Encountered inbound message without a remoteAddress."); + return null; + } else { + return remoteAddress.orElseThrow(); + } + } else if (message.getDirection() == CommunicationLog.Direction.OUTBOUND) { + final Optional localAddress = transportInfo.getLocalAddress(); + if (localAddress.isEmpty()) { + // TODO: Fix: outbound messages often have a localAddress of 0.0.0.0 or null. After this is fixed, + // invalidate the TestRun in this case as well. + return null; + } else { + return localAddress.orElseThrow(); + } + } else { + testRunObserver.invalidateTestRun("Encountered unknown direction in message."); + LOG.trace( + "Encountered unknown direction {} in message with uuid={}", + message.getDirection(), + message.getID()); + return null; + } } private boolean processMessageBody( diff --git a/sdccc/src/main/java/com/draeger/medical/sdccc/messages/mapping/MessageContent.java b/sdccc/src/main/java/com/draeger/medical/sdccc/messages/mapping/MessageContent.java index e158cd62..42caac92 100644 --- a/sdccc/src/main/java/com/draeger/medical/sdccc/messages/mapping/MessageContent.java +++ b/sdccc/src/main/java/com/draeger/medical/sdccc/messages/mapping/MessageContent.java @@ -16,6 +16,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import javax.annotation.Nullable; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.ElementCollection; @@ -75,6 +76,9 @@ public class MessageContent { private String uuid; private boolean isSOAP; + @Column(nullable = true) + private String sender; + /** * This will be used by hibernate when creating the POJO from database entries. */ @@ -94,6 +98,7 @@ public MessageContent() {} * @param actions ws addressing actions * @param uuid identifier for ensuring, that a message was written to the database * @param isSOAP shall be true if a SOAP envelope was found and false otherwise + * @param sender the IP address of the message's sender or null if it could not be determined. */ public MessageContent( final String body, @@ -105,7 +110,8 @@ public MessageContent( final List mdibVersionGroups, final Set actions, final String uuid, - final boolean isSOAP) { + final boolean isSOAP, + @Nullable final String sender) { this.body = body; this.direction = direction; @@ -115,6 +121,7 @@ public MessageContent( this.actions = actions; this.uuid = uuid; this.isSOAP = isSOAP; + this.sender = sender; this.messageHash = MessageUtil.hashMessage(this.body); this.scheme = communicationContext.getTransportInfo().getScheme(); @@ -214,4 +221,8 @@ public String getRequestUri() { public List getMdibVersionGroups() { return this.mdibVersionGroups; } + + public String getSender() { + return this.sender; + } } diff --git a/sdccc/src/main/java/com/draeger/medical/sdccc/messages/mapping/MessageContent_.java b/sdccc/src/main/java/com/draeger/medical/sdccc/messages/mapping/MessageContent_.java index 9050c2a0..6896972d 100644 --- a/sdccc/src/main/java/com/draeger/medical/sdccc/messages/mapping/MessageContent_.java +++ b/sdccc/src/main/java/com/draeger/medical/sdccc/messages/mapping/MessageContent_.java @@ -40,6 +40,7 @@ public final class MessageContent_ { public static volatile SingularAttribute scheme; public static volatile SingularAttribute uuid; public static volatile SingularAttribute isSOAP; + public static volatile SingularAttribute sender; private MessageContent_() {} }