use of org.eclipse.scout.rt.server.IServerSession in project scout.rt by eclipse.
the class SessionJobEventFilterTest method test.
@Test
public void test() {
IServerSession session1 = mock(IServerSession.class);
IServerSession session2 = mock(IServerSession.class);
SessionJobEventFilter filter = new SessionJobEventFilter(session1);
// Tests JobEvent of an event without a job associated
JobEvent event = new JobEvent(mock(IJobManager.class), JobEventType.JOB_STATE_CHANGED, new JobEventData().withFuture(null));
assertFalse(filter.accept(event));
// Tests JobEvent with job without RunContext
event = new JobEvent(mock(IJobManager.class), JobEventType.JOB_STATE_CHANGED, new JobEventData().withFuture(Jobs.schedule(mock(IRunnable.class), Jobs.newInput())));
assertFalse(filter.accept(event));
// Tests JobEvent with job with RunContext
event = new JobEvent(mock(IJobManager.class), JobEventType.JOB_STATE_CHANGED, new JobEventData().withFuture(Jobs.schedule(mock(IRunnable.class), Jobs.newInput().withRunContext(RunContexts.empty()))));
assertFalse(filter.accept(event));
// Tests JobEvent with job with ClientRunContext without session
event = new JobEvent(mock(IJobManager.class), JobEventType.JOB_STATE_CHANGED, new JobEventData().withFuture(Jobs.schedule(mock(IRunnable.class), Jobs.newInput().withRunContext(ServerRunContexts.empty()))));
assertFalse(filter.accept(event));
// Tests JobEvent with job with ClientRunContext with correct session
event = new JobEvent(mock(IJobManager.class), JobEventType.JOB_STATE_CHANGED, new JobEventData().withFuture(Jobs.schedule(mock(IRunnable.class), Jobs.newInput().withRunContext(ServerRunContexts.empty().withSession(session1)))));
assertTrue(filter.accept(event));
// Tests JobEvent with job with ClientRunContext with wrong session
event = new JobEvent(mock(IJobManager.class), JobEventType.JOB_STATE_CHANGED, new JobEventData().withFuture(Jobs.schedule(mock(IRunnable.class), Jobs.newInput().withRunContext(ServerRunContexts.empty().withSession(session2)))));
assertFalse(filter.accept(event));
// Tests adaptable to the session
assertSame(session1, filter.getAdapter(ISession.class));
}
use of org.eclipse.scout.rt.server.IServerSession in project scout.rt by eclipse.
the class SessionInspectorTest method testSessionInspectorWithServerSession.
@Test
public void testSessionInspectorWithServerSession() {
IServerSession serverSession = ServerSessionProvider.currentSession();
SessionInspector inspector = new SessionInspector(ProcessInspector.instance(), serverSession);
assertEquals(ProcessInspector.instance(), inspector.getProcessInspector());
assertEquals(serverSession, inspector.getServerSession());
assertEquals(serverSession.getId(), inspector.getInfo().getSessionId());
assertEquals(serverSession.getUserId(), inspector.getInfo().getUserId());
}
use of org.eclipse.scout.rt.server.IServerSession in project scout.rt by eclipse.
the class ServerRunContextStatement method evaluateWithServerRunContext.
private void evaluateWithServerRunContext() throws Throwable {
final Subject currentSubject = Subject.getSubject(AccessController.getContext());
if (currentSubject == null) {
Assertions.fail("Subject must not be null. Use the annotation '{}' to execute your test under a particular user. ", RunWithSubject.class.getSimpleName());
}
UserAgent userAgent = UserAgents.createDefault();
Class<? extends ISession> sessionClass = m_serverSessionAnnotation.value();
IBean<? extends ISession> sessionBean = BEANS.getBeanManager().uniqueBean(sessionClass);
if (sessionBean != null) {
sessionClass = sessionBean.getBeanClazz();
}
final IBean serverSessionBean = BEANS.getBeanManager().registerBean(new BeanMetaData(sessionClass).withOrder(-Long.MAX_VALUE));
try {
// Obtain the server session for the given subject. Depending on the session provider, a new session is created or a cached session returned.
final IServerSession serverSession = BEANS.get(m_serverSessionAnnotation.provider()).provide(ServerRunContexts.copyCurrent().withSubject(currentSubject).withUserAgent(userAgent));
// Run the test on behalf of a ServerRunContext.
final SafeStatementInvoker invoker = new SafeStatementInvoker(m_next);
ServerRunContexts.copyCurrent().withSession(serverSession).withSubject(// set the test subject explicitly in case it is different to the session subject
currentSubject).withUserAgent(userAgent).run(invoker);
invoker.throwOnError();
} finally {
BEANS.getBeanManager().unregisterBean(serverSessionBean);
}
}
use of org.eclipse.scout.rt.server.IServerSession in project scout.rt by eclipse.
the class BridgeToServerLogoutService method logout.
@Override
public void logout() {
BEANS.get(IAccessControlService.class).clearCacheOfCurrentUser();
// Manually stop session, because we don't have a HTTP session in "bridge" mode (see org.eclipse.scout.rt.server.ServiceTunnelServlet.ScoutSessionBindingListener)
IServerSession session = ServerSessionProvider.currentSession();
try {
session.stop();
} catch (Exception e) {
LOG.warn("Failed to stop session.", e);
} finally {
BEANS.get(IClientNotificationService.class).unregisterSession(IClientNodeId.CURRENT.get(), session.getId(), session.getUserId());
}
}
use of org.eclipse.scout.rt.server.IServerSession in project scout.rt by eclipse.
the class ServerSessionCache method getOrCreate.
/**
* Looks up the scout session on the given {@link HttpSession} stored in an attribute with key IServerSession. Creates
* a new scout session using the given sessionProvider, if none exists.
*
* @param sessionLifecycleHandler
* for creating and destroying scout sessions
* @param httpSession
* {@link HttpSession}
* @return new or existing {@link IServerSession}
*/
public IServerSession getOrCreate(IServerSessionLifecycleHandler sessionLifecycleHandler, HttpSession httpSession) {
Object scoutSession = httpSession.getAttribute(SERVER_SESSION_KEY);
if (scoutSession instanceof IServerSession) {
return (IServerSession) scoutSession;
}
// lock by scout sessionId to prevent creation of scout session more than once per scoutSessionId
ServerSessionEntry sessionContext = getSessionContext(sessionLifecycleHandler.getId(), sessionLifecycleHandler);
synchronized (sessionContext) {
IServerSession session = sessionContext.getOrCreateScoutSession();
sessionContext.addHttpSessionId(httpSession.getId());
httpSession.setAttribute(SERVER_SESSION_KEY, session);
httpSession.setAttribute(UNBIND_LISTENER_KEY, new ScoutSessionBindingListener(sessionLifecycleHandler.getId()));
if (LOG.isDebugEnabled()) {
LOG.debug("Scout ServerSession Session added to HttpSession [scoutSessionId={}, httpSessionId={}]", sessionLifecycleHandler.getId(), httpSession.getId());
}
return session;
}
}
Aggregations