perwendel / spark

A simple expressive web framework for java. Spark has a kotlin DSL https://github.com/perwendel/spark-kotlin

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add support for session listeners

buckelieg opened this issue · comments

Currently ther is no possibility to add custom dsession listeners (for e.g. to track session expired events) with embedded spark configuration.
It would be great to have such API to add custom listeners either on the configuration level or right from session insatnce, like this:

req.session().listener(new HttpSessionListener() {
                    @Override
                    public void sessionCreated(HttpSessionEvent se) {
                        // do custom things here
                    }

                    @Override
                    public void sessionDestroyed(HttpSessionEvent se) {
                        // do custom things here
                    }
                });

When trying to use raw session with embedded server it gives NullPointerException since JettyHandler provides no SessionDataStoreFactory and NullSessionDataStore is used in Jetty by default and no servlet context is available:

                                 v // servlet context is null
session.raw().getServletContext().addListener(new HttpSessionListener() {
                    @Override
                    public void sessionCreated(HttpSessionEvent se) {
                       // custom logic here
                    }

                    @Override
                    public void sessionDestroyed(HttpSessionEvent se) {
                        // custom logic here
                    }
                });

Possibly implemented with this PR