}
}
@Override
public void reply() {
final Message incomingMessage = getIncomingMessage();
if (incomingMessage == null) {
throw new IllegalStateException("Cannot reply. Cannot find incoming message.");
}
if (!incomingMessage.hasResource(RequestDispatcher.class.getName())) {
throw new IllegalStateException("Cannot reply. Cannot find RequestDispatcher resource.");
}
final RequestDispatcher dispatcher = (RequestDispatcher)
incomingMessage.getResource(ResourceProvider.class, RequestDispatcher.class.getName()).get();
if (dispatcher == null) {
throw new IllegalStateException("Cannot reply. Cannot find RequestDispatcher resource.");
}
final Message msg = getIncomingMessage();
message.copyResource("Session", msg);
message.copyResource(RequestDispatcher.class.getName(), msg);
try {
dispatcher.dispatch(message);
}
catch (Exception e) {
throw new MessageDeliveryFailure("unable to deliver message: " + e.getMessage(), e);
}
}
@Override
public AsyncTask replyRepeating(final TimeUnit unit, final int interval) {
final Message msg = getIncomingMessage();
message.copyResource("Session", msg);
final RequestDispatcher dispatcher = (RequestDispatcher) msg.getResource(ResourceProvider.class, RequestDispatcher.class.getName()).get();
return _sendRepeatingWith(message, dispatcher, unit, interval);
}
@Override
public AsyncTask replyDelayed(final TimeUnit unit, final int interval) {
final Message msg = getIncomingMessage();
message.copyResource("Session", msg);
final RequestDispatcher dispatcher = (RequestDispatcher) msg.getResource(ResourceProvider.class, RequestDispatcher.class.getName()).get();
return _sendDelayedWith(message, dispatcher, unit, interval);
}
private Message getIncomingMessage() {
return ((ConversationMessageWrapper) message).getIncomingMessage();
}
@Override
public AsyncTask sendRepeatingWith(final RequestDispatcher viaThis, final TimeUnit unit, final int interval) {
return _sendRepeatingWith(message, viaThis, unit, interval);
}
@Override
public AsyncTask sendDelayedWith(final RequestDispatcher viaThis, final TimeUnit unit, final int interval) {
return _sendDelayedWith(message, viaThis, unit, interval);
}
private AsyncTask _sendRepeatingWith(final Message message, final RequestDispatcher viaThis, final TimeUnit unit, final int interval) {
final boolean isConversational = message instanceof ConversationMessageWrapper;
final AsyncTask task = TaskManagerFactory.get().scheduleRepeating(unit, interval, new HasAsyncTaskRef() {
AsyncTask task;
AsyncDelegateErrorCallback errorCallback;
final Runnable sender;
{
errorCallback = new AsyncDelegateErrorCallback(this, message.getErrorCallback());
if (isConversational) {
final Message incomingMsg = ((ConversationMessageWrapper) message).getIncomingMessage();
if (incomingMsg.hasPart(MessageParts.ReplyTo)) {
sender = new Runnable() {
final String replyTo = incomingMsg
.get(String.class, MessageParts.ReplyTo);
@Override
public void run() {
try {