use of org.apache.wicket.protocol.ws.api.registry.IKey in project wicket by apache.
the class AbstractWebSocketProcessor method onConnect.
/**
* A helper that registers the opened connection in the application-level registry.
*
* @param connection
* the web socket connection to use to communicate with the client
* @see #onOpen(Object)
*/
protected final void onConnect(final IWebSocketConnection connection) {
IKey key = getRegistryKey();
connectionRegistry.setConnection(getApplication(), getSessionId(), key, connection);
if (connectionFilter != null) {
ConnectionRejected connectionRejected = connectionFilter.doFilter(servletRequest);
if (connectionRejected != null) {
broadcastMessage(new AbortedMessage(getApplication(), getSessionId(), key));
connectionRegistry.removeConnection(getApplication(), getSessionId(), key);
connection.close(connectionRejected.getCode(), connectionRejected.getReason());
return;
}
}
broadcastMessage(new ConnectedMessage(getApplication(), getSessionId(), key));
}
use of org.apache.wicket.protocol.ws.api.registry.IKey in project wicket by apache.
the class AbstractWebSocketProcessor method broadcastMessage.
/**
* Exports the Wicket thread locals and broadcasts the received message from the client to all
* interested components and behaviors in the page with id {@code #pageId}
* <p>
* Note: ConnectedMessage and ClosedMessage messages are notification-only. I.e. whatever the
* components/behaviors write in the WebSocketRequestHandler will be ignored because the protocol
* doesn't expect response from the user.
* </p>
*
* @param message
* the message to broadcast
*/
public final void broadcastMessage(final IWebSocketMessage message) {
IKey key = getRegistryKey();
IWebSocketConnection connection = connectionRegistry.getConnection(application, sessionId, key);
if (connection != null && (connection.isOpen() || message instanceof ClosedMessage)) {
Application oldApplication = ThreadContext.getApplication();
Session oldSession = ThreadContext.getSession();
RequestCycle oldRequestCycle = ThreadContext.getRequestCycle();
WebResponse webResponse = webSocketSettings.newWebSocketResponse(connection);
try {
WebSocketRequestMapper requestMapper = new WebSocketRequestMapper(application.getRootRequestMapper());
RequestCycle requestCycle = createRequestCycle(requestMapper, webResponse);
ThreadContext.setRequestCycle(requestCycle);
ThreadContext.setApplication(application);
Session session;
if (oldSession == null || message instanceof IWebSocketPushMessage) {
ISessionStore sessionStore = application.getSessionStore();
session = sessionStore.lookup(webRequest);
ThreadContext.setSession(session);
} else {
session = oldSession;
}
IPageManager pageManager = session.getPageManager();
Page page = getPage(pageManager);
if (page != null) {
WebSocketRequestHandler requestHandler = webSocketSettings.newWebSocketRequestHandler(page, connection);
WebSocketPayload payload = createEventPayload(message, requestHandler);
if (!(message instanceof ConnectedMessage || message instanceof ClosedMessage || message instanceof AbortedMessage)) {
requestCycle.scheduleRequestHandlerAfterCurrent(requestHandler);
}
IRequestHandler broadcastingHandler = new WebSocketMessageBroadcastHandler(pageId, resourceName, payload);
requestMapper.setHandler(broadcastingHandler);
requestCycle.processRequestAndDetach();
} else {
LOG.debug("Page with id '{}' has been expired. No message will be broadcast!", pageId);
}
} catch (Exception x) {
LOG.error("An error occurred during processing of a WebSocket message", x);
} finally {
try {
webResponse.close();
} finally {
ThreadContext.setApplication(oldApplication);
ThreadContext.setRequestCycle(oldRequestCycle);
ThreadContext.setSession(oldSession);
}
}
} else {
LOG.debug("Either there is no connection({}) or it is closed.", connection);
}
}
use of org.apache.wicket.protocol.ws.api.registry.IKey in project wicket by apache.
the class AbstractWebSocketProcessor method onClose.
@Override
public void onClose(int closeCode, String message) {
IKey key = getRegistryKey();
broadcastMessage(new ClosedMessage(getApplication(), getSessionId(), key));
connectionRegistry.removeConnection(getApplication(), getSessionId(), key);
}
use of org.apache.wicket.protocol.ws.api.registry.IKey in project wicket by apache.
the class WebSocketPushBroadcaster method broadcast.
/**
* Processes the given message in the page and session identified by the given Web Socket connection.
* The message is sent as an event to the Page and components of the session allowing the components
* to be updated.
*
* This method can be invoked from any thread, even a non-wicket thread. By default all processing
* is done in the caller thread. Use
* {@link WebSocketSettings#setWebSocketPushMessageExecutor(org.apache.wicket.protocol.ws.concurrent.Executor)}
* to move processing to background threads.
*
* If the given connection is no longer open then the broadcast is silently ignored.
*
* @param connection
* The Web Socket connection that identifies the page and session
* @param message
* The push message event
*/
public void broadcast(ConnectedMessage connection, IWebSocketPushMessage message) {
Args.notNull(connection, "connection");
Args.notNull(message, "message");
Application application = connection.getApplication();
String sessionId = connection.getSessionId();
IKey key = connection.getKey();
IWebSocketConnection wsConnection = registry.getConnection(application, sessionId, key);
if (wsConnection == null) {
return;
}
process(application, singletonList(wsConnection), message);
}
use of org.apache.wicket.protocol.ws.api.registry.IKey in project wicket by apache.
the class AbstractWebSocketProcessor method onError.
@Override
public void onError(Throwable t) {
IKey key = getRegistryKey();
broadcastMessage(new ErrorMessage(getApplication(), getSessionId(), key, t));
}
Aggregations