0

First: I know this problem actually requires a redesign of the application, which will be done later, but first I am looking into a short-term workaround.

Framework: Java EE 7.

Problem:

I have a service which injects HttpServletRequest to read 1 (optional) request header (nothing else is done with it). This makes it only useful inside a RequestContext, not usable in unmanaged beans (eg. a Runnable).

When using BeanManager to programatically lookup this service I ofcourse get this error: WELD-001303: No active contexts for scope type javax.enterprise.context.RequestScoped.

Questions:

  • Can I "ignore" this error and just let the field be set/kept as null?
  • Is it possible to create a custom Producer for HttpServletRequest which - in certain circumstances - ignores WELD-001303 errors and returns null?
  • I know I can probably create a custom Scope which can tackle this problem for me, but that no longer is a small short-term workaround. Or is it easier than creating a Scope, Context, Extension and registering it?
  • What is the class called which actually produces the HttpServletRequest when injected?
  • Are there more ideas to try?
Bossk
  • 707
  • 8
  • 24
  • Can you post some code? I'm having trouble following. It sounds like you need to access the request in a non-managed thread... totally doable, but you'll end up having to manage your own state. – Jonathan S. Fisher Oct 18 '22 at 13:51
  • Using CDI 1.1, you can emulate the behaviour already built into CDI 2.0 when using ActivateRequestContext annotation. Anyway, you may create a Dependent producer that produces a null value of the header you are interested in (if a string, with the proper annotation), after catching the contextnotactiveexception. – Paulo Araújo Oct 31 '22 at 14:27

0 Answers0