Search in sources :

Example 1 with EnvelopePropertyCheckResult

use of won.protocol.message.WonMessage.EnvelopePropertyCheckResult in project webofneeds by researchstudio-sat.

the class UriConsistencyCheckingWonMessageProcessor method process.

@Override
public WonMessage process(WonMessage message) {
    StopWatch sw = new StopWatch();
    if (message == null) {
        throw new WonMessageProcessingException("No WonMessage object found in exchange");
    }
    sw.start("validMessageURI");
    if (!WonMessageUtils.isValidMessageUri(message.getMessageURIRequired())) {
        throw new WonMessageNotWellFormedException("Not a valid message URI: " + message.getMessageURI());
    }
    sw.stop();
    sw.start("envelopePropertyCheck");
    EnvelopePropertyCheckResult result = message.checkEnvelopeProperties();
    if (!result.isValid()) {
        throw new WonMessageNotWellFormedException(result.getMessage());
    }
    sw.stop();
    sw.start("getNodeInfo");
    Optional<URI> senderNode = messageRoutingInfoService.senderNode(message);
    Optional<URI> recipientNode = messageRoutingInfoService.recipientNode(message);
    if (!senderNode.isPresent() && !message.getMessageTypeRequired().isHintMessage()) {
        throw new WonMessageProcessingException("Cannot determine sender node for " + message.toShortStringForDebug());
    }
    if (!recipientNode.isPresent()) {
        throw new WonMessageProcessingException("Cannot determine recipient node for " + message.toShortStringForDebug());
    }
    WonNodeInfo senderNodeInfo = null;
    WonNodeInfo recipientNodeInfo = null;
    if (senderNode.isPresent() && !message.getMessageType().isHintMessage()) {
        // do not check the sender node for a hint
        // TODO: change this behaviour as soon as a matcher uses a WoN node
        senderNodeInfo = wonNodeInformationService.getWonNodeInformation(senderNode.get());
    }
    if (recipientNode != null) {
        recipientNodeInfo = wonNodeInformationService.getWonNodeInformation(recipientNode.get());
    }
    if (senderNodeInfo == null && !message.getMessageType().isHintMessage()) {
        throw new WonMessageProcessingException("Could not load sender WonNodeInfo (won node " + senderNode.get() + ")");
    }
    if (recipientNodeInfo == null) {
        throw new WonMessageProcessingException("Could not load recipient WonNodeInfo (won node " + recipientNode.get() + ")");
    }
    sw.stop();
    if (!message.getMessageType().isHintMessage()) {
        sw.start("senderAtomUriCheck");
        checkAtomUri(message.getSenderAtomURI(), senderNodeInfo);
        sw.stop();
        sw.start("senderSocketUriCheck");
        checkSocketUri(message.getSenderSocketURI(), senderNodeInfo);
        sw.stop();
        // there is no way atom or connection uri can be on the recipient node and the
        // recipient node is different from the sender node
        sw.start("atomUriCheck");
        checkAtomUri(message.getAtomURI(), senderNodeInfo);
        sw.stop();
        sw.start("connectionUriCheck");
        checkConnectionUri(message.getConnectionURI(), senderNodeInfo);
        // Check that atom URI for create_atom message corresponds to local pattern
        sw.stop();
        sw.start("createMsgAtomUriCheck");
        checkCreateMsgAtomURI(message, senderNodeInfo);
        sw.stop();
    }
    sw.start("recipientAtomUriCheck");
    checkAtomUri(message.getRecipientAtomURI(), recipientNodeInfo);
    sw.stop();
    sw.start("recipientSocketUriCheck");
    checkSocketUri(message.getRecipientSocketURI(), recipientNodeInfo);
    sw.stop();
    sw.start("signerCheck");
    WonMessageDirection statedDirection = message.getEnvelopeType();
    if (statedDirection.isFromSystem()) {
        if (!Objects.equals(message.getSenderNodeURIRequired(), message.getSignerURIRequired())) {
            RDFDataMgr.write(System.out, message.getCompleteDataset(), Lang.TRIG);
            throw new WonMessageNotWellFormedException("WonMessage " + message.toShortStringForDebug() + " is FROM_SYSTEM but not signed by its node");
        }
    }
    sw.stop();
    if (logger.isDebugEnabled()) {
        logger.debug(LogMarkers.TIMING, "URI Consistency check timing for message {}:\n{}", message.getMessageURIRequired(), sw.prettyPrint());
    }
    return message;
}
Also used : WonMessageProcessingException(won.protocol.exception.WonMessageProcessingException) EnvelopePropertyCheckResult(won.protocol.message.WonMessage.EnvelopePropertyCheckResult) WonNodeInfo(won.protocol.service.WonNodeInfo) WonMessageNotWellFormedException(won.protocol.exception.WonMessageNotWellFormedException) URI(java.net.URI) WonMessageDirection(won.protocol.message.WonMessageDirection) StopWatch(org.springframework.util.StopWatch)

Aggregations

URI (java.net.URI)1 StopWatch (org.springframework.util.StopWatch)1 WonMessageNotWellFormedException (won.protocol.exception.WonMessageNotWellFormedException)1 WonMessageProcessingException (won.protocol.exception.WonMessageProcessingException)1 EnvelopePropertyCheckResult (won.protocol.message.WonMessage.EnvelopePropertyCheckResult)1 WonMessageDirection (won.protocol.message.WonMessageDirection)1 WonNodeInfo (won.protocol.service.WonNodeInfo)1