Search in sources :

Example 1 with ISessionStore

use of org.apache.wicket.session.ISessionStore 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 2 with ISessionStore

use of org.apache.wicket.session.ISessionStore in project wicket by apache.

the class Session method setAttribute.

/**
 * Adds or replaces the attribute with the given name and value.
 *
 * @param name
 *            The name of the attribute
 * @param value
 *            The value of the attribute
 */
public final Session setAttribute(String name, Serializable value) {
    if (!isTemporary()) {
        RequestCycle cycle = RequestCycle.get();
        if (cycle == null) {
            throw new IllegalStateException("Cannot set the attribute: no RequestCycle available.  If you get this error when using WicketTester.startPage(Page), make sure to call WicketTester.createRequestCycle() beforehand.");
        }
        ISessionStore store = getSessionStore();
        Request request = cycle.getRequest();
        // extra check on session binding event
        if (value == this) {
            Object current = store.getAttribute(request, name);
            if (current == null) {
                String id = store.getSessionId(request, false);
                if (id != null) {
                    // this is a new instance. wherever it came from, bind
                    // the session now
                    store.bind(request, (Session) value);
                }
            }
        }
        // Set the actual attribute
        store.setAttribute(request, name, value);
    } else {
        // session instance gets shared across threads
        if (temporarySessionAttributes == null) {
            temporarySessionAttributes = new HashMap<>(3);
        }
        temporarySessionAttributes.put(name, value);
    }
    return this;
}
Also used : ISessionStore(org.apache.wicket.session.ISessionStore) RequestCycle(org.apache.wicket.request.cycle.RequestCycle) Request(org.apache.wicket.request.Request)

Example 3 with ISessionStore

use of org.apache.wicket.session.ISessionStore in project wicket by apache.

the class BufferedResponseMapper method getSessionId.

/**
 * @return the current session id for stateful pages and <code>null</code> for stateless pages
 *         and non-http threads
 */
protected String getSessionId() {
    String sessionId = null;
    if (Application.exists() && RequestCycle.get() != null) {
        ISessionStore sessionStore = Application.get().getSessionStore();
        IRequestCycle requestCycle = RequestCycle.get();
        Session session = sessionStore.lookup(requestCycle.getRequest());
        if (session != null) {
            sessionId = session.getId();
        }
    }
    return sessionId;
}
Also used : ISessionStore(org.apache.wicket.session.ISessionStore) IRequestCycle(org.apache.wicket.request.IRequestCycle) Session(org.apache.wicket.Session)

Example 4 with ISessionStore

use of org.apache.wicket.session.ISessionStore in project wicket by apache.

the class Session method bind.

/**
 * Force binding this session to the application's {@link ISessionStore session store} if not
 * already done so.
 * <p>
 * A Wicket application can operate in a session-less mode as long as stateless pages are used.
 * Session objects will be then created for each request, but they will only live for that
 * request. You can recognize temporary sessions by calling {@link #isTemporary()} which
 * basically checks whether the session's id is null. Hence, temporary sessions have no session
 * id.
 * </p>
 * <p>
 * By calling this method, the session will be bound (made not-temporary) if it was not bound
 * yet. It is useful for cases where you want to be absolutely sure this session object will be
 * available in next requests. If the session was already bound (
 * {@link ISessionStore#lookup(Request) returns a session}), this call will be a noop.
 * </p>
 */
public final void bind() {
    // modified call.
    if (RequestCycle.get() == null) {
        return;
    }
    ISessionStore store = getSessionStore();
    Request request = RequestCycle.get().getRequest();
    if (store.lookup(request) == null) {
        // explicitly create a session
        id = store.getSessionId(request, true);
        // bind it
        store.bind(request, this);
        if (temporarySessionAttributes != null) {
            for (Map.Entry<String, Serializable> entry : temporarySessionAttributes.entrySet()) {
                store.setAttribute(request, entry.getKey(), entry.getValue());
            }
            temporarySessionAttributes = null;
        }
    }
}
Also used : Serializable(java.io.Serializable) ISessionStore(org.apache.wicket.session.ISessionStore) Request(org.apache.wicket.request.Request) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ISessionStore (org.apache.wicket.session.ISessionStore)4 Session (org.apache.wicket.Session)2 Request (org.apache.wicket.request.Request)2 RequestCycle (org.apache.wicket.request.cycle.RequestCycle)2 Serializable (java.io.Serializable)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 HttpSession (javax.servlet.http.HttpSession)1 Application (org.apache.wicket.Application)1 Page (org.apache.wicket.Page)1 WebPage (org.apache.wicket.markup.html.WebPage)1 IPageManager (org.apache.wicket.page.IPageManager)1 WebApplication (org.apache.wicket.protocol.http.WebApplication)1 WebSocketPayload (org.apache.wicket.protocol.ws.api.event.WebSocketPayload)1 AbortedMessage (org.apache.wicket.protocol.ws.api.message.AbortedMessage)1 ClosedMessage (org.apache.wicket.protocol.ws.api.message.ClosedMessage)1 ConnectedMessage (org.apache.wicket.protocol.ws.api.message.ConnectedMessage)1 IWebSocketPushMessage (org.apache.wicket.protocol.ws.api.message.IWebSocketPushMessage)1 IKey (org.apache.wicket.protocol.ws.api.registry.IKey)1 IRequestCycle (org.apache.wicket.request.IRequestCycle)1