Search in sources :

Example 6 with WonMessage

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

the class Hint2MailParserAction method doRun.

@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
    EventListenerContext ctx = getEventListenerContext();
    if (event instanceof HintFromMatcherEvent && ctx.getBotContextWrapper() instanceof MailBotContextWrapper) {
        MailBotContextWrapper botContextWrapper = (MailBotContextWrapper) ctx.getBotContextWrapper();
        Match match = ((HintFromMatcherEvent) event).getMatch();
        WonMessage message = ((HintFromMatcherEvent) event).getWonMessage();
        URI responseTo = match.getFromNeed();
        URI remoteNeedUri = match.getToNeed();
        MimeMessage originalMail = botContextWrapper.getMimeMessageForURI(responseTo);
        logger.debug("Found a hint for URI: " + responseTo + " sending a mail to the creator: " + MailContentExtractor.getFromAddressString(originalMail));
        WonMimeMessage answerMessage = mailGenerator.createHintMail(originalMail, remoteNeedUri);
        botContextWrapper.addMailIdWonURIRelation(answerMessage.getMessageID(), new WonURI(message.getReceiverURI(), UriType.CONNECTION));
        sendChannel.send(new GenericMessage<>(answerMessage));
    }
}
Also used : EventListenerContext(won.bot.framework.eventbot.EventListenerContext) HintFromMatcherEvent(won.bot.framework.eventbot.event.impl.wonmessage.HintFromMatcherEvent) WonURI(won.bot.framework.eventbot.action.impl.mail.model.WonURI) MailBotContextWrapper(won.bot.framework.bot.context.MailBotContextWrapper) MimeMessage(javax.mail.internet.MimeMessage) WonMessage(won.protocol.message.WonMessage) WonURI(won.bot.framework.eventbot.action.impl.mail.model.WonURI) URI(java.net.URI) Match(won.protocol.model.Match)

Example 7 with WonMessage

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

the class SendFeedbackForHintAction method doRun.

@Override
public void doRun(final Event event, EventListener executingListener) throws Exception {
    if (event instanceof HintFromMatcherEvent) {
        // TODO: the hint with a match object is not really suitable here. Would be better to
        // use connection object instead
        HintFromMatcherEvent hintEvent = (HintFromMatcherEvent) event;
        hintEvent.getWonMessage().getReceiverURI();
        boolean feedbackValue = random.nextBoolean();
        WonMessage message = createFeedbackMessage(hintEvent.getWonMessage().getReceiverURI(), feedbackValue);
        logger.debug("sending {} feedback for hint {} in message {}", new Object[] { (feedbackValue ? "positive" : "negative"), event, message.getMessageURI() });
        getEventListenerContext().getWonMessageSender().sendWonMessage(message);
    }
}
Also used : HintFromMatcherEvent(won.bot.framework.eventbot.event.impl.wonmessage.HintFromMatcherEvent) WonMessage(won.protocol.message.WonMessage)

Example 8 with WonMessage

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

the class ExecuteSendMessageCommandAction method doRun.

/**
 * Constructs the message via <code>createWonMessage</code> and registers a listener for the response from the
 * local WoN node and, if <code>messageIsSenttoRemoteNode</code> is true, a listener for the response from the
 * remote WoN node.
 * @param event
 * @param executingListener
 */
@Override
public final void doRun(Event event, EventListener executingListener) {
    T messageCommandEvent = (T) event;
    try {
        // create the message
        WonMessage message = createWonMessage(messageCommandEvent);
        if (message == null) {
            // assume that the implementation class handles logging and error event creation.
            return;
        }
        // register listeners for the ResponseMessage generated by the own WoN node
        EventBotActionUtils.makeAndSubscribeResponseListener(message, responseEvent -> {
            if (responseEvent instanceof SuccessResponseEvent) {
                SuccessResponseEvent successEvent = (SuccessResponseEvent) responseEvent;
                logger.debug(makeLogMessageString(event) + " succeeded on local WoN node");
                Event eventToPublish = createLocalNodeSuccessEvent(messageCommandEvent, message, successEvent);
                if (eventToPublish != null) {
                    getEventListenerContext().getEventBus().publish(eventToPublish);
                }
            }
        }, responseEvent -> {
            if (responseEvent instanceof FailureResponseEvent) {
                FailureResponseEvent failureEvent = (FailureResponseEvent) responseEvent;
                logger.info(makeLogMessageString(event) + " failed on local WoN node with message: {}", WonRdfUtils.MessageUtils.getTextMessage(((FailureResponseEvent) responseEvent).getFailureMessage()));
                Event eventToPublish = createLocalNodeFailureEvent(messageCommandEvent, message, failureEvent);
                if (eventToPublish != null) {
                    getEventListenerContext().getEventBus().publish(eventToPublish);
                }
            }
        }, getEventListenerContext());
        // register listeners for the ResponseMessage generated by the remote WoN node
        if (messageIsSentToRemoteNode) {
            EventBotActionUtils.makeAndSubscribeRemoteResponseListener(message, responseEvent -> {
                if (responseEvent instanceof SuccessResponseEvent) {
                    SuccessResponseEvent successEvent = (SuccessResponseEvent) responseEvent;
                    logger.debug(makeLogMessageString(event) + " succeeded on remote WoN node");
                    Event eventToPublish = createRemoteNodeSuccessEvent(messageCommandEvent, message, successEvent);
                    if (eventToPublish != null) {
                        getEventListenerContext().getEventBus().publish(eventToPublish);
                    }
                }
            }, responseEvent -> {
                if (responseEvent instanceof FailureResponseEvent) {
                    FailureResponseEvent failureEvent = (FailureResponseEvent) responseEvent;
                    logger.info(makeLogMessageString(event) + " failed on remote WoN node with message: {}", WonRdfUtils.MessageUtils.getTextMessage(((FailureResponseEvent) responseEvent).getFailureMessage()));
                    Event eventToPublish = createRemoteNodeFailureEvent(messageCommandEvent, message, failureEvent);
                    if (eventToPublish != null) {
                        getEventListenerContext().getEventBus().publish(eventToPublish);
                    }
                }
            }, getEventListenerContext());
        }
        // send the message
        getEventListenerContext().getWonMessageSender().sendWonMessage(message);
        if (logger.isDebugEnabled()) {
            logger.debug(makeLogMessageString(event));
        }
    } catch (Exception e) {
        logger.warn("error executing messageCommandEvent: ", e);
        getEventListenerContext().getEventBus().publish(createMessageNotSentEvent(messageCommandEvent, e.getMessage()));
    }
}
Also used : SuccessResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.SuccessResponseEvent) WonMessage(won.protocol.message.WonMessage) RemoteNeedSpecificEvent(won.bot.framework.eventbot.event.RemoteNeedSpecificEvent) MessageCommandEvent(won.bot.framework.eventbot.event.impl.command.MessageCommandEvent) SuccessResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.SuccessResponseEvent) ConnectionSpecificEvent(won.bot.framework.eventbot.event.ConnectionSpecificEvent) FailureResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent) MessageCommandSuccessEvent(won.bot.framework.eventbot.event.impl.command.MessageCommandSuccessEvent) MessageCommandFailureEvent(won.bot.framework.eventbot.event.impl.command.MessageCommandFailureEvent) Event(won.bot.framework.eventbot.event.Event) NeedSpecificEvent(won.bot.framework.eventbot.event.NeedSpecificEvent) MessageCommandNotSentEvent(won.bot.framework.eventbot.event.impl.command.MessageCommandNotSentEvent) FailureResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent) WonMessageBuilderException(won.protocol.exception.WonMessageBuilderException)

Example 9 with WonMessage

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

the class DelayedDuplicateMessageSenderDecorator method getWonMessageSender.

@Override
public WonMessageSender getWonMessageSender() {
    final WonMessageSender delegate = super.getWonMessageSender();
    return new WonMessageSender() {

        @Override
        public void sendWonMessage(WonMessage message) throws WonMessageSenderException {
            delegate.sendWonMessage(message);
            try {
                Thread.sleep(delay);
            } catch (InterruptedException e) {
                logger.warn("caught while waiting the delay time before sending duplicate message", e);
            }
            delegate.sendWonMessage(message);
        }
    };
}
Also used : WonMessage(won.protocol.message.WonMessage) WonMessageSender(won.protocol.message.sender.WonMessageSender)

Example 10 with WonMessage

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

the class WonMessageFromHeaderToBodySerializingCamelProcessor method process.

@Override
public void process(final Exchange exchange) throws Exception {
    logger.debug("processing won message");
    Map headers = exchange.getIn().getHeaders();
    // if the wonMessage header is there, don't change it - that way we can re-route internal messages
    WonMessage wonMessage = (WonMessage) headers.get(WonCamelConstants.MESSAGE_HEADER);
    if (wonMessage == null) {
        throw new WonMessageProcessingException("No WonMessage found in header '" + WonCamelConstants.MESSAGE_HEADER + "'");
    }
    StringWriter writer = new StringWriter();
    RDFDataMgr.write(writer, wonMessage.getCompleteDataset(), Lang.TRIG);
    exchange.getIn().setBody(writer.toString());
    logger.debug("wrote serialized wonMessage to message body");
}
Also used : WonMessageProcessingException(won.protocol.message.processor.exception.WonMessageProcessingException) StringWriter(java.io.StringWriter) WonMessage(won.protocol.message.WonMessage) Map(java.util.Map)

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