perwendel / spark

A simple expressive web framework for java. Spark has a kotlin DSL https://github.com/perwendel/spark-kotlin
Apache License 2.0
9.63k stars 1.56k forks source link

Add support for session listeners #1190

Open buckelieg opened 3 years ago

buckelieg commented 3 years ago

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
                    }
                });
buckelieg commented 3 years ago

Possibly implemented with this PR