Search in sources :

Example 11 with WonMessageDirection

use of won.protocol.message.WonMessageDirection 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

WonMessageDirection (won.protocol.message.WonMessageDirection)11 URI (java.net.URI)10 WonMessage (won.protocol.message.WonMessage)8 Connection (won.protocol.model.Connection)3 StopWatch (org.springframework.util.StopWatch)2 Socket (won.protocol.model.Socket)2 StringWriter (java.io.StringWriter)1 Optional (java.util.Optional)1 Graph (org.apache.jena.graph.Graph)1 WonAclEvaluator (won.auth.WonAclEvaluator)1 AclEvalResult (won.auth.model.AclEvalResult)1 OperationRequest (won.auth.model.OperationRequest)1 ForbiddenMessageException (won.protocol.exception.ForbiddenMessageException)1 IllegalMessageSignerException (won.protocol.exception.IllegalMessageSignerException)1 NoSuchSocketException (won.protocol.exception.NoSuchSocketException)1 WonMessageNotWellFormedException (won.protocol.exception.WonMessageNotWellFormedException)1 WonMessageProcessingException (won.protocol.exception.WonMessageProcessingException)1 WonProtocolException (won.protocol.exception.WonProtocolException)1 EnvelopePropertyCheckResult (won.protocol.message.WonMessage.EnvelopePropertyCheckResult)1 WonMessageType (won.protocol.message.WonMessageType)1