package org.devocative.wickomp.async;

import org.apache.wicket.protocol.ws.api.WebSocketRequestHandler;
import org.apache.wicket.protocol.ws.api.message.IWebSocketPushMessage;

public class WebSocketDelayedResponse implements IWebSocketPushMessage {
	private static final long serialVersionUID = 1131076920625578700L;

	private IAsyncResponse response;
	private Object result;
	private Exception exception;

	private boolean new_ = true;

	// ------------------------------

	public WebSocketDelayedResponse(IAsyncResponse response, Object result) {
		this.response = response;
		this.result = result;
	}

	public WebSocketDelayedResponse(IAsyncResponse response, Exception exception) {
		this.response = response;
		this.exception = exception;
	}

	// ------------------------------

	public void call(WebSocketRequestHandler handler) {
		if (exception != null) {
			response.onAsyncError(handler, exception);
		} else {
			response.onAsyncResult(handler, result);
		}
	}

	// ---------------

	public boolean isNew() {
		return new_;
	}

	public void setNew(boolean value) {
		this.new_ = value;
	}
}

@@ -169,16 +169,73 @@
 Message;
+%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A%0Apublic
@@ -368,16 +368,111 @@
 578700L;
+%0A%09private static final Logger logger = LoggerFactory.getLogger(WebSocketDelayedResponse.class);
 %0A%0A%09priva
@@ -1068,16 +1068,26 @@
  else %7B%0A
+%09%09%09try %7B%0A%09
 %09%09%09respo
@@ -1122,16 +1122,141 @@
 esult);%0A
+%09%09%09%7D catch (Exception e) %7B%0A%09%09%09%09logger.error(%22WebSocketDelayedResponse.call%22, e);%0A%09%09%09%09response.onAsyncError(handler, e);%0A%09%09%09%7D%0A
 %09%09%7D%0A%09%7D%0A%0A

handle exception and push error back to client