Search in sources :

Example 6 with IServerSession

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));
}
Also used : ISession(org.eclipse.scout.rt.shared.ISession) JobEvent(org.eclipse.scout.rt.platform.job.listener.JobEvent) SessionJobEventFilter(org.eclipse.scout.rt.shared.job.filter.event.SessionJobEventFilter) IServerSession(org.eclipse.scout.rt.server.IServerSession) IJobManager(org.eclipse.scout.rt.platform.job.IJobManager) JobEventData(org.eclipse.scout.rt.platform.job.listener.JobEventData) Test(org.junit.Test)

Example 7 with IServerSession

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());
}
Also used : IServerSession(org.eclipse.scout.rt.server.IServerSession) Test(org.junit.Test)

Example 8 with IServerSession

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);
    }
}
Also used : RunWithSubject(org.eclipse.scout.rt.testing.platform.runner.RunWithSubject) SafeStatementInvoker(org.eclipse.scout.rt.testing.platform.runner.SafeStatementInvoker) BeanMetaData(org.eclipse.scout.rt.platform.BeanMetaData) UserAgent(org.eclipse.scout.rt.shared.ui.UserAgent) IServerSession(org.eclipse.scout.rt.server.IServerSession) IBean(org.eclipse.scout.rt.platform.IBean) RunWithSubject(org.eclipse.scout.rt.testing.platform.runner.RunWithSubject) Subject(javax.security.auth.Subject)

Example 9 with IServerSession

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());
    }
}
Also used : IAccessControlService(org.eclipse.scout.rt.shared.services.common.security.IAccessControlService) IClientNotificationService(org.eclipse.scout.rt.shared.clientnotification.IClientNotificationService) IServerSession(org.eclipse.scout.rt.server.IServerSession)

Example 10 with IServerSession

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;
    }
}
Also used : IServerSession(org.eclipse.scout.rt.server.IServerSession)

Aggregations

IServerSession (org.eclipse.scout.rt.server.IServerSession)18 Test (org.junit.Test)12 TestHttpSession (org.eclipse.scout.rt.testing.server.TestHttpSession)6 IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)4 Subject (javax.security.auth.Subject)3 HttpSession (javax.servlet.http.HttpSession)3 ISession (org.eclipse.scout.rt.shared.ISession)3 InvocationHandler (java.lang.reflect.InvocationHandler)2 Method (java.lang.reflect.Method)2 WebMethod (javax.jws.WebMethod)2 BooleanHolder (org.eclipse.scout.rt.platform.holders.BooleanHolder)2 Holder (org.eclipse.scout.rt.platform.holders.Holder)2 ITransaction (org.eclipse.scout.rt.platform.transaction.ITransaction)2 IClientNotificationService (org.eclipse.scout.rt.shared.clientnotification.IClientNotificationService)2 UserAgent (org.eclipse.scout.rt.shared.ui.UserAgent)2 Principal (java.security.Principal)1 Date (java.util.Date)1 Iterator (java.util.Iterator)1 Locale (java.util.Locale)1 ClientNotificationDispatcher (org.eclipse.scout.rt.client.clientnotification.ClientNotificationDispatcher)1