Как разделить один экземпляр фильтра между несколькими сервлетами ServletContextHandler - PullRequest
0 голосов
/ 15 ноября 2018

В моем коде я использую класс фильтра из стороннего jar, который может быть создан один раз.Когда я создаю свой встроенный экземпляр сервера Jetty, у меня есть несколько ServletContextHandler, и для каждого из них я добавил над фильтром.Есть ли способ поделиться фильтром экземпляра между двумя или более обработчиками контекста?

См. Код ниже:

public static void main (String[] args) {
        try {
            Server jettyServer = new Server(8080);

            final HandlerList handlers = new HandlerList();

            ServletContextHandler restCtx = initRestCtx();

            ServletContextHandler wsCtx = initWsCtx();

            handlers.setHandlers(new Handler[]{
                    restCtx,
                    wsCtx,
                    new DefaultHandler()});

            jettyServer.setHandler(handlers);
            jettyServer.start();
        } catch (Exception e) {
            log.error(String.format("An issue occur when starting server: %s",e.getMessage()));
        }
    }

    private static ServletContextHandler initWsCtx() {
        ServletContextHandler wsCtx = createSessionBasedContextHandler();
        wsCtx.setContextPath("/ws");
        wsCtx.addFilter(new MyFilter(), "/*", EnumSet.of(DispatcherType.REQUEST));
        return wsCtx;
    }

    private static ServletContextHandler initRestCtx() {
        ServletContextHandler restCtx = createSessionBasedContextHandler();
        restCtx.setContextPath("/rest");
        restCtx.addFilter(new MyFilter(), "/*", EnumSet.of(DispatcherType.REQUEST));
        return restCtx;
    }

    private static ServletContextHandler createSessionBasedContextHandler() {
        ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
        servletContextHandler
                .getSessionHandler()
                .setMaxInactiveInterval(30);

        return servletContextHandler;
    }

Я упростил код здесь, поэтому у меня должно быть 2 разных контекста иЯ не могу сделать это одним контекстом.

...