Search in sources :

Example 1 with UserAgent

use of org.eclipse.scout.rt.shared.ui.UserAgent in project scout.rt by eclipse.

the class AbstractServiceTunnel method interceptRequest.

protected void interceptRequest(ServiceTunnelRequest request) {
    UserAgent userAgent = UserAgent.CURRENT.get();
    if (userAgent == null) {
        LOG.warn("No UserAgent set on calling context; include default in service-request");
        userAgent = UserAgents.createDefault();
    }
    request.setUserAgent(userAgent.createIdentifier());
    ISession session = ISession.CURRENT.get();
    if (session != null) {
        request.setSessionId(session.getId());
    }
    request.setClientNodeId(INode.ID);
}
Also used : ISession(org.eclipse.scout.rt.shared.ISession) UserAgent(org.eclipse.scout.rt.shared.ui.UserAgent)

Example 2 with UserAgent

use of org.eclipse.scout.rt.shared.ui.UserAgent in project scout.rt by eclipse.

the class ServerRunContextTest method testRunContextsCopyCurrent.

@Test
public void testRunContextsCopyCurrent() {
    final IServerSession session = mock(IServerSession.class);
    final UserAgent userAgent = UserAgents.create().build();
    final Locale locale = Locale.CANADA_FRENCH;
    final Subject subject = new Subject();
    ServerRunContexts.empty().withSession(session).withUserAgent(userAgent).withLocale(locale).withSubject(subject).run(new IRunnable() {

        @Override
        public void run() throws Exception {
            RunContext runContext = RunContexts.copyCurrent();
            assertThat(runContext, CoreMatchers.instanceOf(ServerRunContext.class));
            ServerRunContext serverCtx = (ServerRunContext) runContext;
            assertSame(session, serverCtx.getSession());
            assertSame(userAgent, serverCtx.getUserAgent());
            assertSame(locale, serverCtx.getLocale());
            assertSame(subject, serverCtx.getSubject());
        }
    });
}
Also used : Locale(java.util.Locale) UserAgent(org.eclipse.scout.rt.shared.ui.UserAgent) IServerSession(org.eclipse.scout.rt.server.IServerSession) RunContext(org.eclipse.scout.rt.platform.context.RunContext) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) Subject(javax.security.auth.Subject) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) Test(org.junit.Test)

Example 3 with UserAgent

use of org.eclipse.scout.rt.shared.ui.UserAgent 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 4 with UserAgent

use of org.eclipse.scout.rt.shared.ui.UserAgent in project scout.rt by eclipse.

the class ClientRunContextTest method testCurrentUserAgent.

@Test
public void testCurrentUserAgent() {
    final UserAgent userAgent = UserAgents.create().build();
    ClientRunContexts.empty().withUserAgent(null).run(new IRunnable() {

        @Override
        public void run() throws Exception {
            assertNull(UserAgent.CURRENT.get());
            assertNull(RunContexts.copyCurrent().getLocale());
        }
    });
    ClientRunContexts.empty().withUserAgent(userAgent).run(new IRunnable() {

        @Override
        public void run() throws Exception {
            assertSame(userAgent, UserAgent.CURRENT.get());
            assertEquals(userAgent, ClientRunContexts.copyCurrent().getUserAgent());
            final UserAgent customUserAgent = UserAgents.create().build();
            assertEquals(customUserAgent, ClientRunContexts.copyCurrent().withUserAgent(customUserAgent).getUserAgent());
        }
    });
}
Also used : UserAgent(org.eclipse.scout.rt.shared.ui.UserAgent) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) Test(org.junit.Test)

Example 5 with UserAgent

use of org.eclipse.scout.rt.shared.ui.UserAgent in project scout.rt by eclipse.

the class ClientRunContextTest method testRunContextsCopyCurrent.

@Test
public void testRunContextsCopyCurrent() {
    final IClientSession session = mock(IClientSession.class);
    final UserAgent sessionUserAgent = UserAgents.create().build();
    final Locale sessionLocale = Locale.CANADA_FRENCH;
    final Subject sessionSubject = new Subject();
    final IDesktop sessionDesktop = mock(IDesktop.class);
    when(session.getUserAgent()).thenReturn(sessionUserAgent);
    when(session.getLocale()).thenReturn(sessionLocale);
    when(session.getSubject()).thenReturn(sessionSubject);
    when(session.getDesktopElseVirtualDesktop()).thenReturn(sessionDesktop);
    ClientRunContexts.empty().withSession(session, true).run(new IRunnable() {

        @Override
        public void run() throws Exception {
            RunContext runContext = RunContexts.copyCurrent();
            assertThat(runContext, CoreMatchers.instanceOf(ClientRunContext.class));
            ClientRunContext clientCtx = (ClientRunContext) runContext;
            assertSame(session, clientCtx.getSession());
            assertSame(sessionLocale, clientCtx.getLocale());
            assertSame(sessionUserAgent, clientCtx.getUserAgent());
            assertSame(sessionDesktop, clientCtx.getDesktop());
        }
    });
}
Also used : NlsLocale(org.eclipse.scout.rt.platform.nls.NlsLocale) Locale(java.util.Locale) UserAgent(org.eclipse.scout.rt.shared.ui.UserAgent) IClientSession(org.eclipse.scout.rt.client.IClientSession) RunContext(org.eclipse.scout.rt.platform.context.RunContext) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) Subject(javax.security.auth.Subject) IDesktop(org.eclipse.scout.rt.client.ui.desktop.IDesktop) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) Test(org.junit.Test)

Aggregations

UserAgent (org.eclipse.scout.rt.shared.ui.UserAgent)7 Subject (javax.security.auth.Subject)4 AssertionException (org.eclipse.scout.rt.platform.util.Assertions.AssertionException)4 IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)4 Test (org.junit.Test)4 Locale (java.util.Locale)3 IClientSession (org.eclipse.scout.rt.client.IClientSession)3 IDesktop (org.eclipse.scout.rt.client.ui.desktop.IDesktop)2 RunContext (org.eclipse.scout.rt.platform.context.RunContext)2 NlsLocale (org.eclipse.scout.rt.platform.nls.NlsLocale)2 IServerSession (org.eclipse.scout.rt.server.IServerSession)2 BeanMetaData (org.eclipse.scout.rt.platform.BeanMetaData)1 IBean (org.eclipse.scout.rt.platform.IBean)1 ISession (org.eclipse.scout.rt.shared.ISession)1 RunWithSubject (org.eclipse.scout.rt.testing.platform.runner.RunWithSubject)1 SafeStatementInvoker (org.eclipse.scout.rt.testing.platform.runner.SafeStatementInvoker)1