0
votes

I have a class that perform as a class producers within my rest services which is so made:

public class WebResources{


    @Produces
    @RequestScoped
    public FacesContext produceFacesContext() {
        return FacesContext.getCurrentInstance();
    }

    @Produces
    public Logger produceLog(InjectionPoint injectionPoint) {
        return Logger.getLogger(injectionPoint.getMember().getDeclaringClass().getName());
    }   
}

Now I wish to produce an object of mine by using HttpServletRequest or HttpSession as a parameter. Something like this:

@Produces
@RequestScoped
public MyObject getSecurityContext(InjectionPoint injectionPoint)
{
    HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
    return Utils.getMyObject(request);
}

The error I receive on deploy phase is the following:

ERROR [org.jboss.as.cli.CommandContext] {"JBAS014671: Failed services" => {"jboss.deployment.unit.\"zuora.ear\".WeldStartService" => "org.jboss.msc.service.StartException in service jboss.deployment.unit.\"zuora.ear\".WeldStartService: Failed to start service Caused by: org.jboss.weld.exceptions.DefinitionException: WELD-001406 Cannot inject [parameter 1] of [method] @Produces @RequestScoped public it.infocert.zuora.rest.util.WebResources.getSecurityContext(InjectionPoint) in a non @Dependent scoped bean"}} {"JBAS014671: Failed services" => {"jboss.deployment.unit.\"zuora.ear\".WeldStartService" => "org.jboss.msc.service.StartException in service jboss.deployment.unit.\"zuora.ear\".WeldStartService: Failed to start service Caused by: org.jboss.weld.exceptions.DefinitionException: WELD-001406 Cannot inject [parameter 1] of [method] @Produces @RequestScoped public it.info

1
As I know there is no way to inject Request by using Weld alone, The way I know and use is RequestResponseHolderFilter from deltaspike. By using this filter you can inject HttpRequest with qualifier @Deltaspike. For more info please see deltaspike.apache.org/documentation/servlet.html. If you do not want/could use deltaspike, you can look in its servlet module source code. This is quite straightforward. You need to get every request and save it in ThreadLocal variable.temaleva
BTW: You can inject HttpServletRequest with CDI >= 1.1chkal

1 Answers

1
votes

You should remove the InjectionPoint parameter from your method signature. You cannot us InjectionPoint if you are producing a request scoped bean as multiple injection points will get the same (request-scoped) instance. You aren't using the injection point in your code, so it is safe to remove it.