Search in sources :

Example 16 with IClientSession

use of org.eclipse.scout.rt.client.IClientSession in project scout.rt by eclipse.

the class ClientExceptionHandler method showExceptionInternal.

protected void showExceptionInternal(final Throwable t) {
    final IClientSession session = ClientSessionProvider.currentSession();
    if (session == null) {
        return;
    }
    if (session.getDesktop() == null || !session.getDesktop().isOpened()) {
        return;
    }
    // Prevent loops while displaying the exception.
    final Semaphore loopDetectionSemaphore = getLoopDetectionSemaphore(session);
    if (loopDetectionSemaphore.tryAcquire()) {
        try {
            // Synchronize with the model thread if not applicable.
            if (ModelJobs.isModelThread()) {
                showException(t);
            } else {
                try {
                    ModelJobs.schedule(new IRunnable() {

                        @Override
                        public void run() throws Exception {
                            showException(t);
                        }
                    }, ModelJobs.newInput(ClientRunContexts.copyCurrent()).withExceptionHandling(null, true).withName("Visualizing PlatformException")).awaitDone();
                } catch (final ThreadInterruptedError e) {
                // NOSONAR
                // NOOP
                }
            }
        } finally {
            loopDetectionSemaphore.release();
        }
    } else {
        Exception e = new Exception("Stacktrace and suppressed exception");
        // add original exception for analysis
        e.addSuppressed(t);
        LOG.warn("Loop detection in {}", getClass().getName(), e);
        if (ModelJobs.isModelThread()) {
            IMessageBox msgBox = MessageBoxes.createOk().withSeverity(IStatus.ERROR).withHeader(TEXTS.get("Error"));
            if (t instanceof VetoException) {
                IProcessingStatus status = ((VetoException) t).getStatus();
                msgBox.withHeader(status.getTitle()).withBody(status.getBody());
            }
            msgBox.show();
        }
    }
}
Also used : VetoException(org.eclipse.scout.rt.platform.exception.VetoException) IProcessingStatus(org.eclipse.scout.rt.platform.exception.IProcessingStatus) IClientSession(org.eclipse.scout.rt.client.IClientSession) ThreadInterruptedError(org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError) Semaphore(java.util.concurrent.Semaphore) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) PlatformException(org.eclipse.scout.rt.platform.exception.PlatformException) VetoException(org.eclipse.scout.rt.platform.exception.VetoException) IMessageBox(org.eclipse.scout.rt.client.ui.messagebox.IMessageBox)

Example 17 with IClientSession

use of org.eclipse.scout.rt.client.IClientSession in project scout.rt by eclipse.

the class ClientRunContextStatement method evaluateWithClientRunContext.

private void evaluateWithClientRunContext() 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());
    }
    Class<? extends ISession> sessionClass = m_clientSessionAnnotation.value();
    IBean<? extends ISession> sessionBean = BEANS.getBeanManager().uniqueBean(sessionClass);
    if (sessionBean != null) {
        sessionClass = sessionBean.getBeanClazz();
    }
    final IBean clientSessionBean = BEANS.getBeanManager().registerBean(new BeanMetaData(sessionClass).withOrder(-Long.MAX_VALUE));
    try {
        // Obtain the client session for the given subject. Depending on the session provider, a new session is created or a cached session returned.
        final IClientSession clientSession = BEANS.get(m_clientSessionAnnotation.provider()).provide(ClientRunContexts.copyCurrent().withSubject(currentSubject));
        // Run the test on behalf of a ClientRunContext.
        final SafeStatementInvoker invoker = new SafeStatementInvoker(m_next);
        ClientRunContexts.copyCurrent().withSession(clientSession, true).withSubject(// set the test subject explicitly in case it is different to the session subject
        currentSubject).run(invoker);
        invoker.throwOnError();
    } finally {
        BEANS.getBeanManager().unregisterBean(clientSessionBean);
    }
}
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) IClientSession(org.eclipse.scout.rt.client.IClientSession) IBean(org.eclipse.scout.rt.platform.IBean) RunWithSubject(org.eclipse.scout.rt.testing.platform.runner.RunWithSubject) Subject(javax.security.auth.Subject)

Example 18 with IClientSession

use of org.eclipse.scout.rt.client.IClientSession 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)

Example 19 with IClientSession

use of org.eclipse.scout.rt.client.IClientSession in project scout.rt by eclipse.

the class ClientRunContextTest method testCurrentSessionAndDerivedValues.

@Test
public void testCurrentSessionAndDerivedValues() {
    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(null, false).run(new IRunnable() {

        @Override
        public void run() throws Exception {
            assertNull(ISession.CURRENT.get());
            assertNull(NlsLocale.CURRENT.get());
            assertNull(UserAgent.CURRENT.get());
            assertNull(IDesktop.CURRENT.get());
            assertNull(ClientRunContexts.copyCurrent().getSession());
            assertNull(ClientRunContexts.copyCurrent().getLocale());
            assertNull(ClientRunContexts.copyCurrent().getUserAgent());
            assertNull(ClientRunContexts.copyCurrent().getDesktop());
        }
    });
    ClientRunContexts.empty().withSession(session, false).run(new IRunnable() {

        @Override
        public void run() throws Exception {
            assertSame(session, ISession.CURRENT.get());
            assertNull(NlsLocale.CURRENT.get());
            assertNull(UserAgent.CURRENT.get());
            assertNull(IDesktop.CURRENT.get());
            assertSame(session, ClientRunContexts.copyCurrent().getSession());
            assertNull(ClientRunContexts.copyCurrent().getLocale());
            assertNull(ClientRunContexts.copyCurrent().getUserAgent());
            assertNull(ClientRunContexts.copyCurrent().getDesktop());
            final UserAgent customUserAgent = UserAgents.create().build();
            assertSame(customUserAgent, ClientRunContexts.copyCurrent().withUserAgent(customUserAgent).getUserAgent());
            final Locale customLocale = Locale.ITALIAN;
            assertSame(customLocale, ClientRunContexts.copyCurrent().withLocale(customLocale).getLocale());
        }
    });
    ClientRunContexts.empty().withSession(session, true).run(new IRunnable() {

        @Override
        public void run() throws Exception {
            assertSame(session, ISession.CURRENT.get());
            assertSame(sessionLocale, NlsLocale.CURRENT.get());
            assertSame(sessionUserAgent, UserAgent.CURRENT.get());
            assertSame(sessionDesktop, IDesktop.CURRENT.get());
            assertSame(session, ClientRunContexts.copyCurrent().getSession());
            assertSame(sessionLocale, ClientRunContexts.copyCurrent().getLocale());
            assertSame(sessionUserAgent, ClientRunContexts.copyCurrent().getUserAgent());
            assertSame(sessionDesktop, ClientRunContexts.copyCurrent().getDesktop());
            final UserAgent customUserAgent = UserAgents.create().build();
            assertSame(customUserAgent, ClientRunContexts.copyCurrent().withUserAgent(customUserAgent).getUserAgent());
            final Locale customLocale = Locale.ITALIAN;
            assertSame(customLocale, ClientRunContexts.copyCurrent().withLocale(customLocale).getLocale());
            final IDesktop customDesktop = mock(IDesktop.class);
            assertSame(customDesktop, ClientRunContexts.copyCurrent().withDesktop(customDesktop).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) 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)

Example 20 with IClientSession

use of org.eclipse.scout.rt.client.IClientSession in project scout.rt by eclipse.

the class ClientUIPreferences method getUserAgentPrefix.

protected String getUserAgentPrefix() {
    UserAgent currentUserAgent = null;
    if (m_prefs != null && m_prefs.userScope() instanceof IClientSession) {
        currentUserAgent = ((IClientSession) m_prefs.userScope()).getUserAgent();
    } else {
        currentUserAgent = UserAgentUtility.getCurrentUserAgent();
    }
    if (currentUserAgent == null) {
        return "";
    }
    String uiLayer = null;
    if (!UiLayer.UNKNOWN.equals(currentUserAgent.getUiLayer())) {
        uiLayer = currentUserAgent.getUiLayer().getIdentifier();
    }
    String uiDeviceType = null;
    if (!UiDeviceType.UNKNOWN.equals(currentUserAgent.getUiDeviceType())) {
        uiDeviceType = currentUserAgent.getUiDeviceType().getIdentifier();
    }
    return StringUtility.concatenateTokens(uiLayer, ".", uiDeviceType, ".");
}
Also used : UserAgent(org.eclipse.scout.rt.shared.ui.UserAgent) IClientSession(org.eclipse.scout.rt.client.IClientSession)

Aggregations

IClientSession (org.eclipse.scout.rt.client.IClientSession)30 IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)11 Test (org.junit.Test)11 JobEvent (org.eclipse.scout.rt.platform.job.listener.JobEvent)4 Subject (javax.security.auth.Subject)3 HttpSession (javax.servlet.http.HttpSession)3 IDesktop (org.eclipse.scout.rt.client.ui.desktop.IDesktop)3 IFuture (org.eclipse.scout.rt.platform.job.IFuture)3 AssertionException (org.eclipse.scout.rt.platform.util.Assertions.AssertionException)3 ISession (org.eclipse.scout.rt.shared.ISession)3 UserAgent (org.eclipse.scout.rt.shared.ui.UserAgent)3 WeakReference (java.lang.ref.WeakReference)2 Locale (java.util.Locale)2 Callable (java.util.concurrent.Callable)2 IMessageBox (org.eclipse.scout.rt.client.ui.messagebox.IMessageBox)2 IJobManager (org.eclipse.scout.rt.platform.job.IJobManager)2 JobEventData (org.eclipse.scout.rt.platform.job.listener.JobEventData)2 NlsLocale (org.eclipse.scout.rt.platform.nls.NlsLocale)2 ThreadInterruptedError (org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError)2 SessionStoreTestForm (org.eclipse.scout.rt.ui.html.fixtures.SessionStoreTestForm)2