Search in sources :

Example 26 with WonMessage

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

the class HintMessageProcessor method process.

public void process(Exchange exchange) throws Exception {
    Message message = exchange.getIn();
    WonMessage wonMessage = (WonMessage) message.getHeader(WonCamelConstants.MESSAGE_HEADER);
    logger.debug("STORING message with id {}", wonMessage.getMessageURI());
    URI needURIFromWonMessage = wonMessage.getReceiverNeedURI();
    URI wonNodeFromWonMessage = wonMessage.getReceiverNodeURI();
    URI otherNeedURIFromWonMessage = URI.create(RdfUtils.findOnePropertyFromResource(wonMessage.getMessageContent(), wonMessage.getMessageURI(), WON.HAS_MATCH_COUNTERPART).asResource().getURI());
    double wmScore = RdfUtils.findOnePropertyFromResource(wonMessage.getMessageContent(), wonMessage.getMessageURI(), WON.HAS_MATCH_SCORE).asLiteral().getDouble();
    URI wmOriginator = wonMessage.getSenderNodeURI();
    if (wmScore < 0 || wmScore > 1)
        throw new IllegalArgumentException("score is not in [0,1]");
    if (wmOriginator == null)
        throw new IllegalArgumentException("originator is not set");
    URI facet = WonRdfUtils.FacetUtils.getFacet(wonMessage);
    if (facet == null) {
        // get the first one of the need's supported facets. TODO: implement some sort of strategy for choosing a facet here (and in the matcher)
        Collection<URI> facets = dataService.getSupportedFacets(needURIFromWonMessage);
        if (facets.isEmpty())
            throw new IllegalArgumentException("hint does not specify facets, falling back to using one of the need's supported facets failed as the need does not support any facets");
        // add the facet to the model.
        facet = facets.iterator().next();
    }
    // create Connection in Database
    Connection con = connectionRepository.findOneByNeedURIAndRemoteNeedURIAndTypeURIForUpdate(needURIFromWonMessage, otherNeedURIFromWonMessage, facet);
    if (con == null) {
        URI connectionUri = wonNodeInformationService.generateConnectionURI(wonNodeFromWonMessage);
        con = dataService.createConnection(connectionUri, needURIFromWonMessage, otherNeedURIFromWonMessage, null, facet, ConnectionState.SUGGESTED, ConnectionEventType.MATCHER_HINT);
    }
    // build message to send to owner, put in header
    // set the receiver to the newly generated connection uri
    wonMessage.addMessageProperty(WONMSG.RECEIVER_PROPERTY, con.getConnectionURI());
}
Also used : Message(org.apache.camel.Message) WonMessage(won.protocol.message.WonMessage) WonMessage(won.protocol.message.WonMessage) Connection(won.protocol.model.Connection) URI(java.net.URI)

Example 27 with WonMessage

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

the class NeedMessageFromSystemProcessor method process.

@Override
public void process(Exchange exchange) throws Exception {
    Message message = exchange.getIn();
    WonMessage wonMessage = (WonMessage) message.getHeader(WonCamelConstants.MESSAGE_HEADER);
    URI receiverNeedURI = wonMessage.getReceiverNeedURI();
    URI senderNeedURI = wonMessage.getSenderNeedURI();
    if (receiverNeedURI == null) {
        throw new MissingMessagePropertyException(URI.create(WONMSG.RECEIVER_NEED_PROPERTY.toString()));
    }
    if (senderNeedURI == null) {
        throw new MissingMessagePropertyException(URI.create(WONMSG.SENDER_NEED_PROPERTY.toString()));
    }
    if (!receiverNeedURI.equals(senderNeedURI)) {
        throw new IllegalArgumentException("sender need uri " + senderNeedURI + " does not equal receiver need uri " + receiverNeedURI);
    }
    Need need = needRepository.findOneByNeedURI(senderNeedURI);
    if (need == null) {
        throw new IllegalArgumentException("need not found - cannot send need message to: " + senderNeedURI);
    }
    need.getEventContainer().getEvents().add(messageEventRepository.findOneByMessageURIforUpdate(wonMessage.getMessageURI()));
    need = needRepository.save(need);
}
Also used : Need(won.protocol.model.Need) Message(org.apache.camel.Message) WonMessage(won.protocol.message.WonMessage) MissingMessagePropertyException(won.protocol.message.processor.exception.MissingMessagePropertyException) WonMessage(won.protocol.message.WonMessage) URI(java.net.URI)

Example 28 with WonMessage

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

the class OpenMessageFromNodeProcessor method process.

public void process(final Exchange exchange) throws Exception {
    Message message = exchange.getIn();
    WonMessage wonMessage = (WonMessage) message.getHeader(WonCamelConstants.MESSAGE_HEADER);
    URI connectionURIFromWonMessage = wonMessage.getReceiverURI();
    Connection con = null;
    if (connectionURIFromWonMessage == null) {
        // the opener didn't know about the connection
        // this happens, for example, when both parties get a hint. Both create a connection, but they don't know
        // about each other.
        // That's why we first try to find a connection with the same needs and facet:
        // let's extract the facet, we'll need it multiple times here
        URI facet = WonRdfUtils.FacetUtils.getFacet(wonMessage);
        con = connectionRepository.findOneByNeedURIAndRemoteNeedURIAndTypeURIForUpdate(wonMessage.getReceiverNeedURI(), wonMessage.getSenderNeedURI(), facet);
        if (con == null) {
            // ok, we really do not know about the connection. create it.
            URI connectionUri = wonNodeInformationService.generateConnectionURI(wonMessage.getReceiverNodeURI());
            con = dataService.createConnection(connectionUri, wonMessage.getReceiverNeedURI(), wonMessage.getSenderNeedURI(), wonMessage.getSenderURI(), facet, ConnectionState.REQUEST_RECEIVED, ConnectionEventType.PARTNER_OPEN);
        }
    } else {
        // the opener knew about the connection. just load it.
        con = connectionRepository.findOneByConnectionURIForUpdate(connectionURIFromWonMessage);
    }
    // now perform checks
    if (con == null)
        throw new IllegalStateException("connection must not be null");
    if (con.getRemoteNeedURI() == null)
        throw new IllegalStateException("remote need uri must not be null");
    if (!con.getRemoteNeedURI().equals(wonMessage.getSenderNeedURI()))
        throw new IllegalStateException("the remote need uri of the connection must be equal to the sender need uri of the message");
    if (wonMessage.getSenderURI() == null)
        throw new IllegalStateException("the sender uri must not be null");
    // it is possible that we didn't store the reference to the remote conneciton yet. Now we can do it.
    if (con.getRemoteConnectionURI() == null) {
        // Set it from the message (it's the sender of the message)
        con.setRemoteConnectionURI(wonMessage.getSenderURI());
    }
    if (!con.getRemoteConnectionURI().equals(wonMessage.getSenderURI()))
        throw new IllegalStateException("the sender uri of the message must be equal to the remote connection uri");
    con.setState(con.getState().transit(ConnectionEventType.PARTNER_OPEN));
    connectionRepository.save(con);
    // set the receiver to the local connection uri
    wonMessage.addMessageProperty(WONMSG.RECEIVER_PROPERTY, con.getConnectionURI());
}
Also used : Message(org.apache.camel.Message) WonMessage(won.protocol.message.WonMessage) WonMessage(won.protocol.message.WonMessage) Connection(won.protocol.model.Connection) URI(java.net.URI)

Example 29 with WonMessage

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

the class SendMessageFromSystemProcessor method process.

public void process(final Exchange exchange) throws Exception {
    Message message = exchange.getIn();
    WonMessage wonMessage = (WonMessage) message.getHeader(WonCamelConstants.MESSAGE_HEADER);
    URI connectionUri = wonMessage.getSenderURI();
    if (connectionUri == null) {
        throw new MissingMessagePropertyException(URI.create(WONMSG.SENDER_PROPERTY.toString()));
    }
    Connection con = connectionRepository.findOneByConnectionURIForUpdate(connectionUri);
    if (con.getState() != ConnectionState.CONNECTED) {
        throw new IllegalMessageForConnectionStateException(connectionUri, "CONNECTION_MESSAGE", con.getState());
    }
    URI remoteMessageUri = wonNodeInformationService.generateEventURI(wonMessage.getReceiverNodeURI());
    if (wonMessage.getReceiverURI() == null) {
        // set the sender uri in the envelope TODO: TwoMsgs: do not set sender here
        wonMessage.addMessageProperty(WONMSG.RECEIVER_PROPERTY, con.getRemoteConnectionURI());
    }
    // add the information about the remote message to the locally stored one
    wonMessage.addMessageProperty(WONMSG.HAS_CORRESPONDING_REMOTE_MESSAGE, remoteMessageUri);
    // the persister will pick it up later
    // put the factory into the outbound message factory header. It will be used to generate the outbound message
    // after the wonMessage has been processed and saved, to make sure that the outbound message contains
    // all the data that we also store locally
    OutboundMessageFactory outboundMessageFactory = new OutboundMessageFactory(remoteMessageUri, con);
    exchange.getIn().setHeader(WonCamelConstants.OUTBOUND_MESSAGE_FACTORY_HEADER, outboundMessageFactory);
}
Also used : Message(org.apache.camel.Message) WonMessage(won.protocol.message.WonMessage) MissingMessagePropertyException(won.protocol.message.processor.exception.MissingMessagePropertyException) WonMessage(won.protocol.message.WonMessage) Connection(won.protocol.model.Connection) IllegalMessageForConnectionStateException(won.protocol.exception.IllegalMessageForConnectionStateException) URI(java.net.URI)

Example 30 with WonMessage

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

the class ResponseResenderProcessor method process.

@Override
public void process(final Exchange exchange) throws Exception {
    WonMessage originalMessage = (WonMessage) exchange.getIn().getHeader(WonCamelConstants.ORIGINAL_MESSAGE_HEADER);
    if (originalMessage == null) {
        logger.debug("Processing an exception. camel header {} was null, assuming original message in header {}", WonCamelConstants.ORIGINAL_MESSAGE_HEADER, WonCamelConstants.MESSAGE_HEADER);
        originalMessage = (WonMessage) exchange.getIn().getHeader(WonCamelConstants.MESSAGE_HEADER);
    }
    if (originalMessage == null) {
        logger.warn("Could not obtain original message from camel headers {} or {} for error {}", new Object[] { WonCamelConstants.ORIGINAL_MESSAGE_HEADER, WonCamelConstants.MESSAGE_HEADER, exchange.getProperty(Exchange.EXCEPTION_CAUGHT) });
        return;
    }
    logger.warn("an error occurred while processing WON message {}", originalMessage.getMessageURI());
    // get the event that was found to be already processed
    MessageEventPlaceholder event = messageEventRepository.findOneByMessageURI(originalMessage.getMessageURI());
    // get response to this event:
    URI responseURI = event.getResponseMessageURI();
    Dataset responseDataset = event.getDatasetHolder().getDataset();
    WonMessage responseMessage = new WonMessage(responseDataset);
    Exception e = (Exception) exchange.getProperty(Exchange.EXCEPTION_CAUGHT);
    if (WonMessageDirection.FROM_OWNER == originalMessage.getEnvelopeType()) {
        sendSystemMessageToOwner(responseMessage);
    } else if (WonMessageDirection.FROM_EXTERNAL == originalMessage.getEnvelopeType()) {
        sendSystemMessage(responseMessage);
    } else {
        logger.info(String.format("cannot resend response message for direction of original message, " + "expected FROM_OWNER or FROM_EXTERNAL, but found %s. Original cause is logged.", originalMessage.getEnvelopeType()), e);
    }
}
Also used : Dataset(org.apache.jena.query.Dataset) WonMessage(won.protocol.message.WonMessage) URI(java.net.URI) MessageEventPlaceholder(won.protocol.model.MessageEventPlaceholder)

Aggregations

WonMessage (won.protocol.message.WonMessage)89 URI (java.net.URI)47 Connection (won.protocol.model.Connection)19 Test (org.junit.Test)18 Message (org.apache.camel.Message)17 Dataset (org.apache.jena.query.Dataset)17 Exchange (org.apache.camel.Exchange)13 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)13 DefaultExchange (org.apache.camel.impl.DefaultExchange)13 WonMessageProcessingException (won.protocol.message.processor.exception.WonMessageProcessingException)11 EventListenerContext (won.bot.framework.eventbot.EventListenerContext)10 MissingMessagePropertyException (won.protocol.message.processor.exception.MissingMessagePropertyException)9 Event (won.bot.framework.eventbot.event.Event)8 FailureResponseEvent (won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent)8 EventListener (won.bot.framework.eventbot.listener.EventListener)7 Need (won.protocol.model.Need)7 WonNodeInformationService (won.protocol.service.WonNodeInformationService)7 Model (org.apache.jena.rdf.model.Model)5 WonURI (won.bot.framework.eventbot.action.impl.mail.model.WonURI)5 DefaultNeedModelWrapper (won.protocol.util.DefaultNeedModelWrapper)4