Search in sources :

Example 6 with ClientSession

use of com.canoo.platform.server.client.ClientSession in project dolphin-platform by canoo.

the class ClientSessionImplTest method testAddAttribute.

@Test
public void testAddAttribute() {
    // given:
    ClientSession dolphinSession = new HttpClientSessionImpl(new HttpSessionMock());
    // when:
    dolphinSession.setAttribute("test-attribute", "Hello Dolphin Session");
    // then:
    Assert.assertEquals(1, dolphinSession.getAttributeNames().size());
    Assert.assertTrue(dolphinSession.getAttributeNames().contains("test-attribute"));
    Assert.assertEquals("Hello Dolphin Session", dolphinSession.getAttribute("test-attribute"));
}
Also used : HttpSessionMock(com.canoo.impl.server.util.HttpSessionMock) ClientSession(com.canoo.platform.server.client.ClientSession) HttpClientSessionImpl(com.canoo.dp.impl.server.client.HttpClientSessionImpl) Test(org.testng.annotations.Test)

Example 7 with ClientSession

use of com.canoo.platform.server.client.ClientSession in project dolphin-platform by canoo.

the class DolphinPlatformSpringTestBootstrap method createEventBus.

/**
 * Method to create a spring managed {@link RemotingEventBus} instance in singleton scope.
 *
 * @return the instance
 */
@Bean(name = "dolphinEventBus")
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
protected RemotingEventBus createEventBus(final TestConfiguration testConfiguration) {
    Assert.requireNonNull(testConfiguration, "testConfiguration");
    final DolphinContextProvider contextProvider = new DolphinContextProvider() {

        @Override
        public DolphinContext getContext(ClientSession clientSession) {
            return getCurrentDolphinContext();
        }

        @Override
        public DolphinContext getContextById(String clientSessionId) {
            return getCurrentDolphinContext();
        }

        @Override
        public DolphinContext getCurrentDolphinContext() {
            return testConfiguration.getDolphinTestContext();
        }
    };
    final DefaultDolphinEventBus eventBus = new DefaultDolphinEventBus();
    eventBus.init(contextProvider, new ClientSessionLifecycleHandlerImpl());
    return eventBus;
}
Also used : ClientSessionLifecycleHandlerImpl(com.canoo.dp.impl.server.client.ClientSessionLifecycleHandlerImpl) DolphinContextProvider(com.canoo.dp.impl.server.context.DolphinContextProvider) DefaultDolphinEventBus(com.canoo.dp.impl.server.event.DefaultDolphinEventBus) ClientSession(com.canoo.platform.server.client.ClientSession) Scope(org.springframework.context.annotation.Scope) Bean(org.springframework.context.annotation.Bean)

Example 8 with ClientSession

use of com.canoo.platform.server.client.ClientSession in project dolphin-platform by canoo.

the class ClientSessionModule method initialize.

@Override
public void initialize(final ServerCoreComponents coreComponents) throws ModuleInitializationException {
    Assert.requireNonNull(coreComponents, "coreComponents");
    final ServletContext servletContext = coreComponents.getInstance(ServletContext.class);
    final PlatformConfiguration configuration = coreComponents.getConfiguration();
    final ClasspathScanner classpathScanner = coreComponents.getInstance(ClasspathScanner.class);
    final ManagedBeanFactory beanFactory = coreComponents.getInstance(ManagedBeanFactory.class);
    final ClientSessionLifecycleHandlerImpl lifecycleHandler = new ClientSessionLifecycleHandlerImpl();
    coreComponents.provideInstance(ClientSessionLifecycleHandler.class, lifecycleHandler);
    coreComponents.provideInstance(ClientSessionProvider.class, new ClientSessionProvider() {

        @Override
        public ClientSession getCurrentClientSession() {
            return lifecycleHandler.getCurrentDolphinSession();
        }
    });
    final ClientSessionManager clientSessionManager = new ClientSessionManager(configuration, lifecycleHandler);
    final List<String> endpointList = configuration.getListProperty(ID_FILTER_URL_MAPPINGS, ID_FILTER_URL_MAPPINGS_DEFAULT_VALUE);
    final String[] endpoints = endpointList.toArray(new String[endpointList.size()]);
    final ClientSessionFilter filter = new ClientSessionFilter(clientSessionManager);
    final FilterRegistration.Dynamic createdFilter = servletContext.addFilter(DOLPHIN_CLIENT_ID_FILTER_NAME, filter);
    createdFilter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, endpoints);
    final HttpSessionCleanerListener sessionCleaner = new HttpSessionCleanerListener(clientSessionManager);
    servletContext.addListener(sessionCleaner);
    final Set<Class<?>> listeners = classpathScanner.getTypesAnnotatedWith(ServerListener.class);
    for (final Class<?> listenerClass : listeners) {
        if (ClientSessionListener.class.isAssignableFrom(listenerClass)) {
            final ClientSessionListener listener = (ClientSessionListener) beanFactory.createDependentInstance(listenerClass);
            lifecycleHandler.addSessionDestroyedListener(s -> listener.sessionDestroyed(s));
            lifecycleHandler.addSessionCreatedListener(s -> listener.sessionCreated(s));
        }
    }
    final ClientSessionMutextHolder mutextHolder = new ClientSessionMutextHolder();
    lifecycleHandler.addSessionDestroyedListener(s -> mutextHolder.sessionDestroyed(s));
    lifecycleHandler.addSessionCreatedListener(s -> mutextHolder.sessionCreated(s));
}
Also used : ClientSessionManager(com.canoo.dp.impl.server.client.ClientSessionManager) PlatformConfiguration(com.canoo.platform.core.PlatformConfiguration) ClientSessionLifecycleHandlerImpl(com.canoo.dp.impl.server.client.ClientSessionLifecycleHandlerImpl) HttpSessionCleanerListener(com.canoo.dp.impl.server.client.HttpSessionCleanerListener) ClasspathScanner(com.canoo.platform.server.spi.components.ClasspathScanner) ClientSessionProvider(com.canoo.dp.impl.server.client.ClientSessionProvider) ClientSession(com.canoo.platform.server.client.ClientSession) ManagedBeanFactory(com.canoo.platform.server.spi.components.ManagedBeanFactory) ServletContext(javax.servlet.ServletContext) ClientSessionListener(com.canoo.platform.server.client.ClientSessionListener) DispatcherType(javax.servlet.DispatcherType) ClientSessionFilter(com.canoo.dp.impl.server.client.ClientSessionFilter) FilterRegistration(javax.servlet.FilterRegistration) ClientSessionMutextHolder(com.canoo.dp.impl.server.client.ClientSessionMutextHolder)

Example 9 with ClientSession

use of com.canoo.platform.server.client.ClientSession in project dolphin-platform by canoo.

the class ClientScopeImpl method getLocalStore.

private Map<String, Object> getLocalStore() {
    ClientSession session = getClientSession();
    if (session == null) {
        throw new IllegalStateException("No dolphin request found! Looks like you try to use the " + ClientScopeImpl.class.getSimpleName() + " ouside of the dolphin context!");
    }
    Map<String, Object> localStore = session.getAttribute(CLIENT_STORE_ATTRIBUTE);
    if (localStore == null) {
        localStore = Collections.synchronizedMap(new HashMap<String, Object>());
        session.setAttribute(CLIENT_STORE_ATTRIBUTE, localStore);
    }
    return localStore;
}
Also used : HashMap(java.util.HashMap) ClientSession(com.canoo.platform.server.client.ClientSession)

Example 10 with ClientSession

use of com.canoo.platform.server.client.ClientSession in project dolphin-platform by canoo.

the class DolphinContextCommunicationHandler method handle.

public void handle(final HttpServletRequest request, final HttpServletResponse response) {
    Assert.requireNonNull(request, "request");
    Assert.requireNonNull(response, "response");
    final HttpSession httpSession = Assert.requireNonNull(request.getSession(), "request.getSession()");
    final ClientSession clientSession = sessionProvider.getCurrentClientSession();
    if (clientSession == null) {
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        LOG.error("No client session provided for request in http session {}", httpSession.getId());
        return;
    }
    final String userAgent = request.getHeader("user-agent");
    LOG.trace("receiving RPM request for client session {} in http session {} from client with user-agent {}", clientSession.getId(), httpSession.getId(), userAgent);
    final List<Command> commands = new ArrayList<>();
    try {
        commands.addAll(readCommands(request));
    } catch (final Exception e) {
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        LOG.error("Can not parse request! (DolphinContext " + clientSession.getId() + ")", e);
        return;
    }
    LOG.trace("Request for DolphinContext {} in http session {} contains {} commands", clientSession.getId(), httpSession.getId(), commands.size());
    try {
        DolphinContext context = getOrCreateContext(clientSession, commands);
        final List<Command> results = new ArrayList<>();
        try {
            results.addAll(handle(context, commands));
        } catch (final Exception e) {
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            LOG.error("Can not withoutResult the the received commands (DolphinContext " + context.getId() + ")", e);
            return;
        }
        LOG.trace("Sending RPM response for client session {} in http session {} from client with user-agent {}", context.getId(), httpSession.getId(), userAgent);
        LOG.trace("RPM response for client session {} in http session {} contains {} commands", context.getId(), httpSession.getId(), results.size());
        try {
            writeCommands(results, response);
        } catch (final Exception e) {
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            LOG.error("Can not writeRequestContent response!", e);
            return;
        }
    } catch (final Exception e) {
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        LOG.error("Can not find or create matching dolphin context in session " + httpSession.getId(), e);
        return;
    }
}
Also used : CreateContextCommand(com.canoo.dp.impl.remoting.commands.CreateContextCommand) Command(com.canoo.dp.impl.remoting.legacy.communication.Command) HttpSession(javax.servlet.http.HttpSession) ClientSession(com.canoo.platform.server.client.ClientSession) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Aggregations

ClientSession (com.canoo.platform.server.client.ClientSession)18 HttpClientSessionImpl (com.canoo.dp.impl.server.client.HttpClientSessionImpl)7 HttpSessionMock (com.canoo.impl.server.util.HttpSessionMock)7 Test (org.testng.annotations.Test)7 ClientSessionLifecycleHandlerImpl (com.canoo.dp.impl.server.client.ClientSessionLifecycleHandlerImpl)3 DolphinContextProvider (com.canoo.dp.impl.server.context.DolphinContextProvider)3 ClientSessionProvider (com.canoo.dp.impl.server.client.ClientSessionProvider)2 DolphinContext (com.canoo.dp.impl.server.context.DolphinContext)2 ClasspathScanner (com.canoo.platform.server.spi.components.ClasspathScanner)2 ManagedBeanFactory (com.canoo.platform.server.spi.components.ManagedBeanFactory)2 ServletContext (javax.servlet.ServletContext)2 HttpSession (javax.servlet.http.HttpSession)2 CreateContextCommand (com.canoo.dp.impl.remoting.commands.CreateContextCommand)1 Command (com.canoo.dp.impl.remoting.legacy.communication.Command)1 ClientSessionFilter (com.canoo.dp.impl.server.client.ClientSessionFilter)1 ClientSessionLifecycleHandler (com.canoo.dp.impl.server.client.ClientSessionLifecycleHandler)1 ClientSessionManager (com.canoo.dp.impl.server.client.ClientSessionManager)1 ClientSessionMutextHolder (com.canoo.dp.impl.server.client.ClientSessionMutextHolder)1 HttpSessionCleanerListener (com.canoo.dp.impl.server.client.HttpSessionCleanerListener)1 RemotingConfiguration (com.canoo.dp.impl.server.config.RemotingConfiguration)1