如何获取对Embedded Jetty请求范围的引用

时间:2016-12-27 22:32:54

标签: embedded-jetty

我在嵌入式Jetty设置上使用Errai Framework并运行Weld-SE环境。

Errai框架使用PicketLink进行安全性,后者反过来调整@RequestScoped中的一些关键bean。结果,我遇到了#34;范围错误没有活动的上下文"在调用安全API时。

我的问题是如何利用Jetty和/或Errai API在我的环境的右侧线程上手动初始化RequestScope?

1 个答案:

答案 0 :(得分:0)

对于遇到这种情况的人来说,可以在过滤器中启动@RequestScope:

public class WeldRequestScopeFilter implements Filter {

    private static final Logger logger = LoggerFactory.getLogger(
            WeldRequestScopeFilter.class.getSimpleName());

    @Override
    public void destroy() {
        logger.trace("Filter destroyed");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain) throws IOException, ServletException {

        final String url = ((HttpServletRequest) request).getRequestURL()
                .toString();
        final Map<String, Object> requestDataStore = new HashMap<>(10);

        final WeldContainer WELD = WeldContainer
                .instance(Application.WELD_CONTAINER_ID);
        final BoundRequestContext requestContext = WELD
                .select(BoundRequestContext.class).get();

        logger.info("Activating @RequestScoped for request on {}", url);
        requestContext.associate(requestDataStore);
        requestContext.activate();

        chain.doFilter(request, response);

        logger.info("Deactivating @RequestScoped for request on {}", url);
        requestContext.invalidate();
        requestContext.deactivate();
        requestContext.dissociate(requestDataStore);

    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        logger.trace("Filter initialized");
    }

}