use of won.bot.framework.eventbot.event.impl.wonmessage.WonMessageReceivedOnConnectionEvent in project webofneeds by researchstudio-sat.
the class OpenConnectionDebugAction method doRun.
@Override
public void doRun(final Event event, EventListener executingListener) throws Exception {
if (!(event instanceof ConnectFromOtherNeedEvent)) {
return;
}
if (event instanceof WonMessageReceivedOnConnectionEvent) {
WonMessage msg = ((WonMessageReceivedOnConnectionEvent) event).getWonMessage();
String message = WonRdfUtils.MessageUtils.getTextMessage(msg);
if (message == null) {
message = "";
}
Matcher ignoreMatcher = PATTERN_IGNORE.matcher(message);
if (ignoreMatcher.find()) {
logger.debug("not reacting to incoming message of type {} as the welcome message contained 'ignore'", msg.getMessageType());
return;
}
Matcher waitMatcher = PATTERN_WAIT.matcher(message);
final boolean wait = waitMatcher.find();
int waitSeconds = 15;
if (wait && waitMatcher.groupCount() == 2) {
waitSeconds = Integer.parseInt(waitMatcher.group(2));
}
Matcher denyMatcher = PATTERN_DENY.matcher(message);
final boolean deny = denyMatcher.find();
ConnectionSpecificEvent connectEvent = (ConnectionSpecificEvent) event;
logger.debug("auto-replying to connect for connection {}", connectEvent.getConnectionURI());
URI connectionUri = connectEvent.getConnectionURI();
String finalWelcomeMessage = welcomeMessage;
if (wait || deny) {
finalWelcomeMessage = welcomeMessage + " " + (deny ? "Denying" : "Accepting") + " your request " + (wait ? " after a timeout of " + waitSeconds + " seconds" : "");
} else {
finalWelcomeMessage = welcomeMessage + " " + welcomeHelpMessage;
}
final WonMessage toSend = deny ? createCloseWonMessage(connectionUri, finalWelcomeMessage) : createOpenWonMessage(connectionUri, finalWelcomeMessage);
Runnable task = () -> {
getEventListenerContext().getWonMessageSender().sendWonMessage(toSend);
};
if (wait) {
Date when = new Date(System.currentTimeMillis() + waitSeconds * 1000);
getEventListenerContext().getTaskScheduler().schedule(task, when);
} else {
task.run();
}
}
}
Aggregations