I'm trying to use CDI in a JASPIC SAM for the Servlet Container profile. The SAM is called with an HttpServletRequest and HttpServletResponse, and an HttpSession can be easily obtained from them.
Unfortunately, despite the availability of these objects, not all servers have the CDI request and session scope activated at this point. In JBoss AS it's active at this point, and in GlassFish more or less as well (it barks a little, but obtaining scoped beans from the bean manager works). However in WebLogic the scope clearly isn't active and the infamous "WELD-001303 No active contexts for scope type javax.enterprise.context.RequestScoped" is thrown whenever I'm trying to access a request scoped bean that is obtained from the bean manager. When I lookup a session scoped bean a similar exception is already thrown at the point of looking up the bean.
Weld seems to have some API available to manually activate this scope (http://docs.jboss.org/weld/reference/latest/en-US/html/contexts.html), but can this be done in a portable way?
Well, using Weld API is not portable in the sense of EE. However, any Java EE 6 app server must provide CDI implementation. It's also possible to configre Weld for servlet containers and pre-EE6 app servers (with limited functionality) - see http://docs.jboss.org/weld/reference/1.1.5.Final/en-US/html/environments.html#d0e5223.