Search in sources :

Example 1 with ConnectedMessage

use of org.apache.wicket.protocol.ws.api.message.ConnectedMessage 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));
}
Also used : IKey(org.apache.wicket.protocol.ws.api.registry.IKey) AbortedMessage(org.apache.wicket.protocol.ws.api.message.AbortedMessage) ConnectedMessage(org.apache.wicket.protocol.ws.api.message.ConnectedMessage)

Example 2 with ConnectedMessage

use of org.apache.wicket.protocol.ws.api.message.ConnectedMessage 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);
    }
}
Also used : IPageManager(org.apache.wicket.page.IPageManager) WebResponse(org.apache.wicket.request.http.WebResponse) ISessionStore(org.apache.wicket.session.ISessionStore) IRequestHandler(org.apache.wicket.request.IRequestHandler) RequestCycle(org.apache.wicket.request.cycle.RequestCycle) ClosedMessage(org.apache.wicket.protocol.ws.api.message.ClosedMessage) Page(org.apache.wicket.Page) WebPage(org.apache.wicket.markup.html.WebPage) WebSocketPayload(org.apache.wicket.protocol.ws.api.event.WebSocketPayload) IWebSocketPushMessage(org.apache.wicket.protocol.ws.api.message.IWebSocketPushMessage) ConnectedMessage(org.apache.wicket.protocol.ws.api.message.ConnectedMessage) AbortedMessage(org.apache.wicket.protocol.ws.api.message.AbortedMessage) IKey(org.apache.wicket.protocol.ws.api.registry.IKey) Application(org.apache.wicket.Application) WebApplication(org.apache.wicket.protocol.http.WebApplication) HttpSession(javax.servlet.http.HttpSession) Session(org.apache.wicket.Session)

Example 3 with ConnectedMessage

use of org.apache.wicket.protocol.ws.api.message.ConnectedMessage in project wicket by apache.

the class WebSocketTester method broadcast.

/**
 * Broadcasts/pushes a message to specific web socket connection
 *
 * @param application
 *          The application where the web socket connection is registered
 * @param sessionId
 *          The id of the http session with which the web socket connection is registered
 * @param key
 *          The key with which the web socket connection is registered
 * @param message
 *          The message to broadcast/push
 */
public void broadcast(Application application, String sessionId, IKey key, IWebSocketPushMessage message) {
    WebSocketSettings webSocketSettings = WebSocketSettings.Holder.get(application);
    WebSocketPushBroadcaster broadcaster = new WebSocketPushBroadcaster(webSocketSettings.getConnectionRegistry());
    ConnectedMessage wsMessage = new ConnectedMessage(application, sessionId, key);
    broadcaster.broadcast(wsMessage, message);
}
Also used : WebSocketSettings(org.apache.wicket.protocol.ws.WebSocketSettings) WebSocketPushBroadcaster(org.apache.wicket.protocol.ws.api.WebSocketPushBroadcaster) ConnectedMessage(org.apache.wicket.protocol.ws.api.message.ConnectedMessage)

Example 4 with ConnectedMessage

use of org.apache.wicket.protocol.ws.api.message.ConnectedMessage in project openmeetings by apache.

the class HashPage method onInitialize.

@Override
protected void onInitialize() {
    super.onInitialize();
    StringValue secure = p.get(HASH);
    StringValue invitation = p.get(INVITATION_HASH);
    WebSession ws = WebSession.get();
    ws.checkHashes(secure, invitation);
    String errorMsg = getString("invalid.hash");
    recContainer.setVisible(false);
    add(new EmptyPanel(PANEL_MAIN).setVisible(false));
    if (!invitation.isEmpty()) {
        Invitation i = ws.getInvitation();
        if (i == null) {
            errorMsg = getString("error.hash.invalid");
        } else if (!i.isAllowEntry()) {
            FastDateFormat sdf = FormatHelper.getDateTimeFormat(i.getInvitee());
            errorMsg = Valid.OneTime == i.getValid() ? getString("error.hash.used") : String.format("%s %s - %s, %s", getString("error.hash.period"), sdf.format(i.getValidFrom()), sdf.format(i.getValidTo()), i.getInvitee().getTimeZoneId());
        } else {
            Recording rec = i.getRecording();
            if (rec != null) {
                vi.setVisible(!i.isPasswordProtected());
                vp.setVisible(!i.isPasswordProtected());
                if (!i.isPasswordProtected()) {
                    vi.update(null, rec);
                    vp.update(null, rec);
                }
                recContainer.setVisible(true);
                error = false;
            }
            Room r = i.getRoom();
            if (r != null && !r.isDeleted()) {
                createRoom(r.getId());
                if (i.isPasswordProtected() && rp != null) {
                    mp.getChat().setVisible(false);
                    rp.setOutputMarkupPlaceholderTag(true).setVisible(false);
                }
            }
        }
    } else if (!secure.isEmpty()) {
        Long recId = getRecordingId(), roomId = ws.getRoomId();
        if (recId == null && roomId == null) {
            errorMsg = getString("1599");
        } else if (recId != null) {
            recContainer.setVisible(true);
            Recording rec = recDao.get(recId);
            vi.update(null, rec);
            vp.update(null, rec);
            error = false;
        } else {
            createRoom(roomId);
        }
    }
    StringValue swf = p.get(SWF);
    StringValue app = swf.isEmpty() ? p.get(APP) : swf;
    if (!app.isEmpty()) {
        if (APP_TYPE_NETWORK.equals(app.toString())) {
            replace(new NetTestPanel(PANEL_MAIN).add(AttributeModifier.append("class", "app")));
            error = false;
        }
        if (APP_TYPE_SETTINGS.equals(app.toString())) {
            replace(new VideoSettings(PANEL_MAIN).replace(new OmWebSocketPanel("ws-panel") {

                private static final long serialVersionUID = 1L;

                private WsClient c = null;

                @Override
                protected void onConnect(ConnectedMessage message) {
                    c = new WsClient(message.getSessionId(), message.getKey().hashCode());
                }

                @Override
                protected IWsClient getWsClient() {
                    return c;
                }
            }).add(new OmAjaxClientInfoBehavior() {

                private static final long serialVersionUID = 1L;

                @Override
                protected void onClientInfo(AjaxRequestTarget target, WebClientInfo info) {
                    super.onClientInfo(target, info);
                    target.appendJavaScript(String.format("VideoSettings.init(%s);VideoSettings.open();", VideoSettings.getInitJson("noclient")));
                }
            }));
            error = false;
        }
    }
    add(recContainer.add(vi.setShowShare(false).setOutputMarkupPlaceholderTag(true), vp.setOutputMarkupPlaceholderTag(true)), new InvitationPasswordDialog("i-pass", this));
    remove(urlParametersReceivingBehavior);
    add(new MessageDialog("access-denied", getString("invalid.hash"), errorMsg, DialogButtons.OK, DialogIcon.ERROR) {

        private static final long serialVersionUID = 1L;

        @Override
        public void onConfigure(JQueryBehavior behavior) {
            super.onConfigure(behavior);
            behavior.setOption("autoOpen", error);
            behavior.setOption("resizable", false);
        }

        @Override
        public void onClose(IPartialPageRequestHandler handler, DialogButton button) {
        // no-op
        }
    });
}
Also used : WebClientInfo(org.apache.wicket.protocol.http.request.WebClientInfo) DialogButton(com.googlecode.wicket.jquery.ui.widget.dialog.DialogButton) EmptyPanel(org.apache.wicket.markup.html.panel.EmptyPanel) OmWebSocketPanel(org.apache.openmeetings.web.common.OmWebSocketPanel) WsClient(org.apache.openmeetings.db.entity.basic.WsClient) IWsClient(org.apache.openmeetings.db.entity.basic.IWsClient) ConnectedMessage(org.apache.wicket.protocol.ws.api.message.ConnectedMessage) MessageDialog(com.googlecode.wicket.jquery.ui.widget.dialog.MessageDialog) StringValue(org.apache.wicket.util.string.StringValue) Room(org.apache.openmeetings.db.entity.room.Room) IWsClient(org.apache.openmeetings.db.entity.basic.IWsClient) JQueryBehavior(com.googlecode.wicket.jquery.core.JQueryBehavior) VideoSettings(org.apache.openmeetings.web.room.VideoSettings) Invitation(org.apache.openmeetings.db.entity.room.Invitation) OmAjaxClientInfoBehavior(org.apache.openmeetings.web.common.OmAjaxClientInfoBehavior) IPartialPageRequestHandler(org.apache.wicket.core.request.handler.IPartialPageRequestHandler) FastDateFormat(org.apache.commons.lang3.time.FastDateFormat) AjaxRequestTarget(org.apache.wicket.ajax.AjaxRequestTarget) WebSession(org.apache.openmeetings.web.app.WebSession) NetTestPanel(org.apache.openmeetings.web.room.NetTestPanel) Recording(org.apache.openmeetings.db.entity.record.Recording)

Example 5 with ConnectedMessage

use of org.apache.wicket.protocol.ws.api.message.ConnectedMessage in project wicket by apache.

the class WebSocketResource method onPayload.

void onPayload(WebSocketPayload<?> payload) {
    WebSocketRequestHandler webSocketHandler = payload.getHandler();
    if (payload instanceof WebSocketTextPayload) {
        WebSocketTextPayload textPayload = (WebSocketTextPayload) payload;
        TextMessage data = textPayload.getMessage();
        onMessage(webSocketHandler, data);
    } else if (payload instanceof WebSocketBinaryPayload) {
        WebSocketBinaryPayload binaryPayload = (WebSocketBinaryPayload) payload;
        BinaryMessage binaryData = binaryPayload.getMessage();
        onMessage(webSocketHandler, binaryData);
    } else if (payload instanceof WebSocketConnectedPayload) {
        WebSocketConnectedPayload connectedPayload = (WebSocketConnectedPayload) payload;
        ConnectedMessage message = connectedPayload.getMessage();
        onConnect(message);
    } else if (payload instanceof WebSocketClosedPayload) {
        WebSocketClosedPayload connectedPayload = (WebSocketClosedPayload) payload;
        ClosedMessage message = connectedPayload.getMessage();
        onClose(message);
    } else if (payload instanceof WebSocketErrorPayload) {
        WebSocketErrorPayload errorPayload = (WebSocketErrorPayload) payload;
        ErrorMessage message = errorPayload.getMessage();
        onError(webSocketHandler, message);
    } else if (payload instanceof WebSocketAbortedPayload) {
        WebSocketAbortedPayload abortedPayload = (WebSocketAbortedPayload) payload;
        AbortedMessage message = abortedPayload.getMessage();
        onAbort(message);
    } else if (payload instanceof WebSocketPushPayload) {
        WebSocketPushPayload pushPayload = (WebSocketPushPayload) payload;
        IWebSocketPushMessage message = pushPayload.getMessage();
        onPush(webSocketHandler, message);
    }
}
Also used : ClosedMessage(org.apache.wicket.protocol.ws.api.message.ClosedMessage) WebSocketAbortedPayload(org.apache.wicket.protocol.ws.api.event.WebSocketAbortedPayload) WebSocketConnectedPayload(org.apache.wicket.protocol.ws.api.event.WebSocketConnectedPayload) WebSocketTextPayload(org.apache.wicket.protocol.ws.api.event.WebSocketTextPayload) IWebSocketPushMessage(org.apache.wicket.protocol.ws.api.message.IWebSocketPushMessage) BinaryMessage(org.apache.wicket.protocol.ws.api.message.BinaryMessage) ConnectedMessage(org.apache.wicket.protocol.ws.api.message.ConnectedMessage) AbortedMessage(org.apache.wicket.protocol.ws.api.message.AbortedMessage) WebSocketBinaryPayload(org.apache.wicket.protocol.ws.api.event.WebSocketBinaryPayload) WebSocketPushPayload(org.apache.wicket.protocol.ws.api.event.WebSocketPushPayload) ErrorMessage(org.apache.wicket.protocol.ws.api.message.ErrorMessage) TextMessage(org.apache.wicket.protocol.ws.api.message.TextMessage) WebSocketErrorPayload(org.apache.wicket.protocol.ws.api.event.WebSocketErrorPayload) WebSocketClosedPayload(org.apache.wicket.protocol.ws.api.event.WebSocketClosedPayload)

Aggregations

ConnectedMessage (org.apache.wicket.protocol.ws.api.message.ConnectedMessage)6 AbortedMessage (org.apache.wicket.protocol.ws.api.message.AbortedMessage)4 ClosedMessage (org.apache.wicket.protocol.ws.api.message.ClosedMessage)3 IWebSocketPushMessage (org.apache.wicket.protocol.ws.api.message.IWebSocketPushMessage)3 WebSocketAbortedPayload (org.apache.wicket.protocol.ws.api.event.WebSocketAbortedPayload)2 WebSocketBinaryPayload (org.apache.wicket.protocol.ws.api.event.WebSocketBinaryPayload)2 WebSocketClosedPayload (org.apache.wicket.protocol.ws.api.event.WebSocketClosedPayload)2 WebSocketConnectedPayload (org.apache.wicket.protocol.ws.api.event.WebSocketConnectedPayload)2 WebSocketErrorPayload (org.apache.wicket.protocol.ws.api.event.WebSocketErrorPayload)2 WebSocketPayload (org.apache.wicket.protocol.ws.api.event.WebSocketPayload)2 WebSocketPushPayload (org.apache.wicket.protocol.ws.api.event.WebSocketPushPayload)2 WebSocketTextPayload (org.apache.wicket.protocol.ws.api.event.WebSocketTextPayload)2 BinaryMessage (org.apache.wicket.protocol.ws.api.message.BinaryMessage)2 ErrorMessage (org.apache.wicket.protocol.ws.api.message.ErrorMessage)2 TextMessage (org.apache.wicket.protocol.ws.api.message.TextMessage)2 IKey (org.apache.wicket.protocol.ws.api.registry.IKey)2 JQueryBehavior (com.googlecode.wicket.jquery.core.JQueryBehavior)1 DialogButton (com.googlecode.wicket.jquery.ui.widget.dialog.DialogButton)1 MessageDialog (com.googlecode.wicket.jquery.ui.widget.dialog.MessageDialog)1 HttpSession (javax.servlet.http.HttpSession)1