Search in sources :

Example 1 with SuccessResponseEvent

use of won.bot.framework.eventbot.event.impl.wonmessage.SuccessResponseEvent in project webofneeds by researchstudio-sat.

the class EventBotActionUtils method makeAndSubscribeRemoteResponseListener.

/**
 * Creates a listener that waits for the remote response to the specified message. If a SuccessResponse is received,
 * the successCallback is executed, if a FailureResponse is received, the failureCallback is executed.
 * @param outgoingMessage
 * @param successCallback
 * @param failureCallback
 * @param context
 * @return
 */
public static EventListener makeAndSubscribeRemoteResponseListener(final WonMessage outgoingMessage, final EventListener successCallback, final EventListener failureCallback, EventListenerContext context) {
    // create an event listener that processes the remote response to the wonMessage we're about to send
    EventListener listener = new ActionOnFirstEventListener(context, OriginalMessageUriRemoteResponseEventFilter.forWonMessage(outgoingMessage), new BaseEventBotAction(context) {

        @Override
        protected void doRun(final Event event, EventListener executingListener) throws Exception {
            if (event instanceof SuccessResponseEvent) {
                successCallback.onEvent(event);
            } else if (event instanceof FailureResponseEvent) {
                failureCallback.onEvent(event);
            }
        }
    });
    context.getEventBus().subscribe(SuccessResponseEvent.class, listener);
    context.getEventBus().subscribe(FailureResponseEvent.class, listener);
    return listener;
}
Also used : SuccessResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.SuccessResponseEvent) SuccessResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.SuccessResponseEvent) FailureResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent) Event(won.bot.framework.eventbot.event.Event) ActionOnFirstEventListener(won.bot.framework.eventbot.listener.impl.ActionOnFirstEventListener) EventListener(won.bot.framework.eventbot.listener.EventListener) ActionOnFirstEventListener(won.bot.framework.eventbot.listener.impl.ActionOnFirstEventListener) FailureResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException)

Example 2 with SuccessResponseEvent

use of won.bot.framework.eventbot.event.impl.wonmessage.SuccessResponseEvent 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 3 with SuccessResponseEvent

use of won.bot.framework.eventbot.event.impl.wonmessage.SuccessResponseEvent in project webofneeds by researchstudio-sat.

the class EventBotActionUtils method makeAndSubscribeResponseListener.

// ************************************************ EventListener ***************************************************
/**
 * Creates a listener that waits for the response to the specified message. If a SuccessResponse is received,
 * the successCallback is executed, if a FailureResponse is received, the failureCallback is executed.
 * @param outgoingMessage
 * @param successCallback
 * @param failureCallback
 * @param context
 * @return
 */
public static EventListener makeAndSubscribeResponseListener(final WonMessage outgoingMessage, final EventListener successCallback, final EventListener failureCallback, EventListenerContext context) {
    // create an event listener that processes the response to the wonMessage we're about to send
    EventListener listener = new ActionOnFirstEventListener(context, OriginalMessageUriResponseEventFilter.forWonMessage(outgoingMessage), new BaseEventBotAction(context) {

        @Override
        protected void doRun(final Event event, EventListener executingListener) throws Exception {
            if (event instanceof SuccessResponseEvent) {
                successCallback.onEvent(event);
            } else if (event instanceof FailureResponseEvent) {
                failureCallback.onEvent(event);
            }
        }
    });
    context.getEventBus().subscribe(SuccessResponseEvent.class, listener);
    context.getEventBus().subscribe(FailureResponseEvent.class, listener);
    return listener;
}
Also used : SuccessResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.SuccessResponseEvent) SuccessResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.SuccessResponseEvent) FailureResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent) Event(won.bot.framework.eventbot.event.Event) ActionOnFirstEventListener(won.bot.framework.eventbot.listener.impl.ActionOnFirstEventListener) EventListener(won.bot.framework.eventbot.listener.EventListener) ActionOnFirstEventListener(won.bot.framework.eventbot.listener.impl.ActionOnFirstEventListener) FailureResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException)

Aggregations

Event (won.bot.framework.eventbot.event.Event)3 FailureResponseEvent (won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent)3 SuccessResponseEvent (won.bot.framework.eventbot.event.impl.wonmessage.SuccessResponseEvent)3 IOException (java.io.IOException)2 MessagingException (javax.mail.MessagingException)2 EventListener (won.bot.framework.eventbot.listener.EventListener)2 ActionOnFirstEventListener (won.bot.framework.eventbot.listener.impl.ActionOnFirstEventListener)2 ConnectionSpecificEvent (won.bot.framework.eventbot.event.ConnectionSpecificEvent)1 NeedSpecificEvent (won.bot.framework.eventbot.event.NeedSpecificEvent)1 RemoteNeedSpecificEvent (won.bot.framework.eventbot.event.RemoteNeedSpecificEvent)1 MessageCommandEvent (won.bot.framework.eventbot.event.impl.command.MessageCommandEvent)1 MessageCommandFailureEvent (won.bot.framework.eventbot.event.impl.command.MessageCommandFailureEvent)1 MessageCommandNotSentEvent (won.bot.framework.eventbot.event.impl.command.MessageCommandNotSentEvent)1 MessageCommandSuccessEvent (won.bot.framework.eventbot.event.impl.command.MessageCommandSuccessEvent)1 WonMessageBuilderException (won.protocol.exception.WonMessageBuilderException)1 WonMessage (won.protocol.message.WonMessage)1