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);
}
}
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;
}
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;
}
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;
}
}
}
Aggregations