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());
}
});
}
}
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);
}
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));
}
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;
}
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();
}
}
Aggregations