Search in sources :

Example 1 with ServerRunContext

use of org.eclipse.scout.rt.server.context.ServerRunContext in project scout.rt by eclipse.

the class ClusterSynchronizationService method onMessage.

@Override
public void onMessage(IMessage<IClusterNotificationMessage> message) {
    final IClusterNotificationMessage notificationMessage = message.getTransferObject();
    if (isEnabled()) {
        // Do not progress notifications sent by node itself
        String originNode = notificationMessage.getProperties().getOriginNode();
        if (m_nodeId.equals(originNode)) {
            return;
        }
        getStatusInfoInternal().updateReceiveStatus(notificationMessage);
        getStatusInfoInternal(notificationMessage.getNotification().getClass()).updateReceiveStatus(notificationMessage);
        ServerRunContext serverRunContext = ServerRunContexts.empty();
        serverRunContext.withSubject(m_subject);
        serverRunContext.withSession(BEANS.get(ServerSessionProviderWithCache.class).provide(serverRunContext.copy()));
        serverRunContext.run(new IRunnable() {

            @Override
            public void run() throws Exception {
                NotificationHandlerRegistry reg = BEANS.get(NotificationHandlerRegistry.class);
                reg.notifyNotificationHandlers(notificationMessage.getNotification());
            }
        });
    }
}
Also used : NotificationHandlerRegistry(org.eclipse.scout.rt.shared.notification.NotificationHandlerRegistry) ServerRunContext(org.eclipse.scout.rt.server.context.ServerRunContext) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable)

Example 2 with ServerRunContext

use of org.eclipse.scout.rt.server.context.ServerRunContext in project scout.rt by eclipse.

the class ServiceTunnelServlet method doGet.

// === HTTP-GET ===
@Override
protected void doGet(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws IOException, ServletException {
    if (Subject.getSubject(AccessController.getContext()) == null) {
        servletResponse.sendError(HttpServletResponse.SC_FORBIDDEN);
        return;
    }
    lazyInit(servletRequest, servletResponse);
    createServletRunContext(servletRequest, servletResponse).run(new IRunnable() {

        @Override
        public void run() throws Exception {
            ServerRunContext serverRunContext = ServerRunContexts.copyCurrent();
            serverRunContext.withUserAgent(UserAgents.createDefault());
            serverRunContext.withSession(lookupServerSessionOnHttpSession(Sessions.randomSessionId(), serverRunContext));
            invokeAdminService(serverRunContext);
        }
    }, ServletExceptionTranslator.class);
}
Also used : ServerRunContext(org.eclipse.scout.rt.server.context.ServerRunContext) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) ServletException(javax.servlet.ServletException) InterruptedIOException(java.io.InterruptedIOException) SocketException(java.net.SocketException) IOException(java.io.IOException)

Example 3 with ServerRunContext

use of org.eclipse.scout.rt.server.context.ServerRunContext in project scout.rt by eclipse.

the class ServiceTunnelServletTest method testLookupScoutServerSessionOnHttpSessionMultipleThreads.

/**
 * Calls {@link ServiceTunnelServlet#lookupServerSessionOnHttpSession(ServerRunContext) in 4 different threads within
 * the same HTTP session. Test ensures that the same server session is returned in all threads and that
 * {@link ServerSessionProvider#provide(ServerRunContext)}} is called only once.
 */
@Test
public void testLookupScoutServerSessionOnHttpSessionMultipleThreads() throws ServletException {
    final Map<String, IServerSession> cache = new HashMap<>();
    final TestServerSession testServerSession = new TestServerSession();
    testServerSession.start("testSessionId");
    testServerSession.setSharedContextVariable("userId", String.class, "testUser");
    HttpServletRequest requestMock = mock(HttpServletRequest.class);
    HttpSession testHttpSession = mock(HttpSession.class);
    when(requestMock.getSession()).thenReturn(testHttpSession);
    when(requestMock.getSession(true)).thenReturn(testHttpSession);
    ICacheEntry cacheEntryMock = mock(ICacheEntry.class);
    when(cacheEntryMock.getValue()).thenReturn(testServerSession);
    when(cacheEntryMock.isActive()).thenReturn(true);
    doAnswer(putValueInCache(cache)).when(testHttpSession).setAttribute(ArgumentMatchers.eq(IServerSession.class.getName()), ArgumentMatchers.any());
    when(testHttpSession.getAttribute(IServerSession.class.getName())).thenAnswer(getCachedValue(cache));
    doAnswer(slowCreateTestsession(testServerSession)).when(m_serverSessionProviderSpy).provide(ArgumentMatchers.anyString(), ArgumentMatchers.any(ServerRunContext.class));
    List<HttpSessionLookupCallable> jobs = new ArrayList<>();
    for (int i = 0; i < 4; i++) {
        jobs.add(new HttpSessionLookupCallable(m_testServiceTunnelServlet, requestMock, m_responseMock));
    }
    List<IFuture<?>> futures = scheduleAndJoinJobs(jobs);
    Set<IServerSession> serverSessions = new HashSet<IServerSession>();
    for (IFuture<?> future : futures) {
        serverSessions.add((IServerSession) future.awaitDoneAndGet());
    }
    assertEquals(CollectionUtility.hashSet(testServerSession), serverSessions);
    verify(m_serverSessionProviderSpy, times(1)).provide(ArgumentMatchers.anyString(), ArgumentMatchers.any(ServerRunContext.class));
}
Also used : HashMap(java.util.HashMap) HttpSession(javax.servlet.http.HttpSession) ArrayList(java.util.ArrayList) ICacheEntry(org.eclipse.scout.rt.server.commons.cache.ICacheEntry) IFuture(org.eclipse.scout.rt.platform.job.IFuture) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServerRunContext(org.eclipse.scout.rt.server.context.ServerRunContext) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with ServerRunContext

use of org.eclipse.scout.rt.server.context.ServerRunContext in project scout.rt by eclipse.

the class BridgeToServerBeanDecorator method ensureRunInServerContext.

protected Object ensureRunInServerContext(final IBeanInvocationContext<T> context) {
    if (PropertyMap.isSet(PropertyMap.PROP_SERVER_SCOPE)) {
        // already in a server scope
        return continueCall(context);
    }
    // bridge to server scope
    ClientNotificationCollector collector = new ClientNotificationCollector();
    ServerRunContext bridgeRunContext = ServerRunContexts.copyCurrent().withClientNotificationCollector(collector).withClientNodeId(INode.ID);
    ISession currentSession = ISession.CURRENT.get();
    IServerSession bridgeSession = null;
    if (currentSession != null) {
        bridgeSession = BEANS.get(ServerSessionProviderWithCache.class).provide(currentSession.getId(), bridgeRunContext);
    }
    Object result = bridgeRunContext.withSession(bridgeSession).call(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            try {
                return continueCall(context);
            } catch (Exception e) {
                ITransaction.CURRENT.get().addFailure(e);
                throw e;
            }
        }
    });
    ClientNotificationDispatcher clientNotificationDispatcher = BEANS.get(ClientNotificationDispatcher.class);
    List<ClientNotificationMessage> values = collector.consume();
    if (!values.isEmpty()) {
        clientNotificationDispatcher.dispatchNotifications(values);
    }
    return result;
}
Also used : ISession(org.eclipse.scout.rt.shared.ISession) ServerRunContext(org.eclipse.scout.rt.server.context.ServerRunContext) IServerSession(org.eclipse.scout.rt.server.IServerSession) ClientNotificationCollector(org.eclipse.scout.rt.server.clientnotification.ClientNotificationCollector) ClientNotificationMessage(org.eclipse.scout.rt.shared.clientnotification.ClientNotificationMessage) ClientNotificationDispatcher(org.eclipse.scout.rt.client.clientnotification.ClientNotificationDispatcher)

Example 5 with ServerRunContext

use of org.eclipse.scout.rt.server.context.ServerRunContext in project scout.rt by eclipse.

the class ServiceTunnelServlet method doPost.

protected ServiceTunnelResponse doPost(ServiceTunnelRequest serviceRequest) throws ServletException {
    ClientNotificationCollector collector = new ClientNotificationCollector();
    ServerRunContext serverRunContext = ServerRunContexts.copyCurrent().withLocale(serviceRequest.getLocale()).withUserAgent(UserAgents.createByIdentifier(serviceRequest.getUserAgent())).withClientNotificationCollector(collector).withClientNodeId(serviceRequest.getClientNodeId());
    if (serviceRequest.getSessionId() != null) {
        serverRunContext.withSession(lookupServerSessionOnHttpSession(serviceRequest.getSessionId(), serverRunContext));
    }
    final IRegistrationHandle registrationHandle = registerForCancellation(serverRunContext, serviceRequest);
    try {
        ServiceTunnelResponse serviceResponse = invokeService(serverRunContext, serviceRequest);
        // include client notifications in response (piggyback)
        serviceResponse.setNotifications(collector.consume());
        return serviceResponse;
    } finally {
        registrationHandle.unregister();
    }
}
Also used : ServerRunContext(org.eclipse.scout.rt.server.context.ServerRunContext) ClientNotificationCollector(org.eclipse.scout.rt.server.clientnotification.ClientNotificationCollector) IRegistrationHandle(org.eclipse.scout.rt.server.context.RunMonitorCancelRegistry.IRegistrationHandle) ServiceTunnelResponse(org.eclipse.scout.rt.shared.servicetunnel.ServiceTunnelResponse)

Aggregations

ServerRunContext (org.eclipse.scout.rt.server.context.ServerRunContext)6 IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)3 IOException (java.io.IOException)2 ServletException (javax.servlet.ServletException)2 ClientNotificationCollector (org.eclipse.scout.rt.server.clientnotification.ClientNotificationCollector)2 InterruptedIOException (java.io.InterruptedIOException)1 SocketException (java.net.SocketException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpSession (javax.servlet.http.HttpSession)1 ClientNotificationDispatcher (org.eclipse.scout.rt.client.clientnotification.ClientNotificationDispatcher)1 IFuture (org.eclipse.scout.rt.platform.job.IFuture)1 IServerSession (org.eclipse.scout.rt.server.IServerSession)1 ICacheEntry (org.eclipse.scout.rt.server.commons.cache.ICacheEntry)1 IRegistrationHandle (org.eclipse.scout.rt.server.context.RunMonitorCancelRegistry.IRegistrationHandle)1 ISession (org.eclipse.scout.rt.shared.ISession)1 ClientNotificationMessage (org.eclipse.scout.rt.shared.clientnotification.ClientNotificationMessage)1 NotificationHandlerRegistry (org.eclipse.scout.rt.shared.notification.NotificationHandlerRegistry)1