use of won.bot.framework.eventbot.event.impl.command.MessageCommandEvent 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()));
}
}
use of won.bot.framework.eventbot.event.impl.command.MessageCommandEvent in project webofneeds by researchstudio-sat.
the class ExecuteSendMessageCommandAction method makeLogMessageString.
private String makeLogMessageString(Event event) {
StringBuilder sb = new StringBuilder();
MessageCommandEvent messageCommandEvent = (MessageCommandEvent) event;
sb.append("sending message of type ").append(messageCommandEvent.getWonMessageType());
if (event instanceof NeedSpecificEvent) {
sb.append(" on behalf of need ").append(((NeedSpecificEvent) event).getNeedURI());
}
if (event instanceof RemoteNeedSpecificEvent) {
sb.append(" to need ").append(((RemoteNeedSpecificEvent) event).getRemoteNeedURI());
}
if (event instanceof ConnectionSpecificEvent) {
sb.append(" in connection ").append(((ConnectionSpecificEvent) event).getConnectionURI());
}
return sb.toString();
}
use of won.bot.framework.eventbot.event.impl.command.MessageCommandEvent in project webofneeds by researchstudio-sat.
the class ExecuteMessageCommandAction method makeLogMessageString.
private String makeLogMessageString(Event event) {
StringBuilder sb = new StringBuilder();
MessageCommandEvent messageCommandEvent = (MessageCommandEvent) event;
sb.append("sending message of type ").append(messageCommandEvent.getWonMessageType());
if (event instanceof AtomSpecificEvent) {
sb.append(" on behalf of atom ").append(((AtomSpecificEvent) event).getAtomURI());
}
if (event instanceof TargetAtomSpecificEvent) {
sb.append(" to atom ").append(((TargetAtomSpecificEvent) event).getTargetAtomURI());
}
if (event instanceof ConnectionSpecificEvent) {
sb.append(" in connection ").append(((ConnectionSpecificEvent) event).getConnectionURI());
}
return sb.toString();
}
use of won.bot.framework.eventbot.event.impl.command.MessageCommandEvent in project webofneeds by researchstudio-sat.
the class ExecuteMessageCommandAction 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 msgToSend = createWonMessage(messageCommandEvent);
if (msgToSend == null) {
// creation.
return;
}
// sign and set messageURI
final WonMessage message = getEventListenerContext().getWonMessageSender().prepareMessage(msgToSend);
// 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 (more on loglevel 'debug'): {}", WonRdfUtils.MessageUtils.getTextMessage(((FailureResponseEvent) responseEvent).getFailureMessage()));
if (logger.isDebugEnabled()) {
logger.debug("failed message: \n {}", RdfUtils.toString(message.getCompleteDataset()));
}
Event eventToPublish = createRemoteNodeFailureEvent(messageCommandEvent, message, failureEvent);
if (eventToPublish != null) {
getEventListenerContext().getEventBus().publish(eventToPublish);
}
}
}, getEventListenerContext());
}
// send the message
getEventListenerContext().getWonMessageSender().sendMessage(message);
if (logger.isDebugEnabled()) {
logger.debug(makeLogMessageString(event));
}
} catch (Exception e) {
logger.warn("error executing messageCommandEvent: ", e);
getEventListenerContext().getEventBus().publish(createMessageNotSentEvent(messageCommandEvent, e.getMessage()));
}
}
Aggregations