jsfomnifaces

How to properly resolve NPE when using OmniFaces @Param on bean with custom CDI interceptor


I want to use OmniFaces @Param on a view controller annotated with a custom CDI interceptor:

@Named
@javax.faces.view.ViewScoped
@CustomInterceptor
public class Controller implements Serializable {

  @Param(required = true)
  private String token;
  
  @PostConstruct
  public void initialize() {
    // use this.token
  }
}

The custom CDI interceptor:

@CustomAnnotation
@Interceptor
@Priority(Interceptor.Priority.APPLICATION)
@Dependent
public class CustomInterceptor implements Serializable {

  @PostConstruct
  private Object initialize(InvocationContext invocationContext) throws Exception {
    // some code
    return invocationContext.proceed();
  }

  @AroundInvoke
  private Object intercept(InvocationContext invocationContext) throws Exception {
    // some code
    return invocationContext.proceed();
  }

  @PreDestroy
  private Object shutdown(InvocationContext invocationContext) throws Exception {
    // some code
    return invocationContext.proceed();
  }
}

And the custom annotation:

@Inherited
@InterceptorBinding
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface CustomAnnotation {}

On runtime request of the view i am getting a NPE with this stack trace:

SEVERE [javax.enterprise.resource.webcontainer.jsf.application] (default task-1) Error Rendering View[view.xhtml]: java.lang.NullPointerException
  at deployment//org.omnifaces.cdi.param.ParamProducer.doBeanValidation(ParamProducer.java:455)
  at deployment//org.omnifaces.cdi.param.ParamProducer.validateBean(ParamProducer.java:359)
  at deployment//org.omnifaces.cdi.param.ParamProducer.invokeValidators(ParamProducer.java:338)
  at deployment//org.omnifaces.cdi.param.ParamProducer.lambda$validateValues$1(ParamProducer.java:325)
  at deployment//org.omnifaces.cdi.param.ParamProducer.runWithSimulatedLabelAndValueOnViewRoot(ParamProducer.java:258)
  at deployment//org.omnifaces.cdi.param.ParamProducer.validateValues(ParamProducer.java:325)
  at deployment//org.omnifaces.cdi.param.ParamProducer.produce(ParamProducer.java:107)
  at deployment//org.omnifaces.cdi.param.ParamExtension$ParamInjectionTarget.inject(ParamExtension.java:123)
  at org.jboss.weld.core@3.1.5.Final//org.jboss.weld.bean.ManagedBean.create(ManagedBean.java:161)
  at org.jboss.weld.core@3.1.5.Final//org.jboss.weld.util.bean.IsolatedForwardingBean.create(IsolatedForwardingBean.java:45)
  at com.sun.jsf-impl@2.3.14.SP02//com.sun.faces.application.view.ViewScopeContextManager.createBean(ViewScopeContextManager.java:142)
  at com.sun.jsf-impl@2.3.14.SP02//com.sun.faces.application.view.ViewScopeContext.get(ViewScopeContext.java:114)
  at org.jboss.weld.core@3.1.5.Final//org.jboss.weld.contexts.PassivatingContextWrapper$AbstractPassivatingContextWrapper.get(PassivatingContextWrapper.java:84)
  at org.jboss.weld.core@3.1.5.Final//org.jboss.weld.bean.ContextualInstanceStrategy$DefaultContextualInstanceStrategy.get(ContextualInstanceStrategy.java:100)
  at org.jboss.weld.core@3.1.5.Final//org.jboss.weld.bean.ContextualInstance.get(ContextualInstance.java:50)
  at org.jboss.weld.core@3.1.5.Final//org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:694)
  at org.jboss.weld.core@3.1.5.Final//org.jboss.weld.module.web.el.AbstractWeldELResolver.lookup(AbstractWeldELResolver.java:107)
  at org.jboss.weld.core@3.1.5.Final//org.jboss.weld.module.web.el.AbstractWeldELResolver.getValue(AbstractWeldELResolver.java:90)
  at org.jboss.as.jsf-injection//org.jboss.as.jsf.injection.weld.ForwardingELResolver.getValue(ForwardingELResolver.java:46)
  at javax.el.api@2.0.0.Final//javax.el.CompositeELResolver.getValue(CompositeELResolver.java:136)
  at com.sun.jsf-impl@2.3.14.SP02//com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:156)
  at com.sun.jsf-impl@2.3.14.SP02//com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:184)
  at org.glassfish.jakarta.el@3.0.3.jbossorg-2//com.sun.el.parser.AstIdentifier.getValue(AstIdentifier.java:91)
  at org.glassfish.jakarta.el@3.0.3.jbossorg-2//com.sun.el.parser.AstValue.getBase(AstValue.java:125)
  at org.glassfish.jakarta.el@3.0.3.jbossorg-2//com.sun.el.parser.AstValue.getValue(AstValue.java:173)
  at org.glassfish.jakarta.el@3.0.3.jbossorg-2//com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:183)
  at org.jboss.weld.core@3.1.5.Final//org.jboss.weld.module.web.el.WeldValueExpression.getValue(WeldValueExpression.java:50)
  at org.jboss.weld.core@3.1.5.Final//org.jboss.weld.module.web.el.WeldValueExpression.getValue(WeldValueExpression.java:50)
  at com.sun.jsf-impl@2.3.14.SP02//com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:73)
  at javax.faces.api@3.0.0.SP04//javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:170)
  at javax.faces.api@3.0.0.SP04//javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:157)
  at javax.faces.api@3.0.0.SP04//javax.faces.component.UIOutput.getValue(UIOutput.java:140)
  at javax.faces.api@3.0.0.SP04//javax.faces.component.UIInput.getValue(UIInput.java:287)
  at deployment//org.primefaces.util.ComponentUtils.getValueToRender(ComponentUtils.java:104)
  at deployment//org.primefaces.util.ComponentUtils.getValueToRender(ComponentUtils.java:68)
  at deployment//org.primefaces.component.inputtext.InputTextRenderer.encodeMarkup(InputTextRenderer.java:96)
  at deployment//org.primefaces.component.inputtext.InputTextRenderer.encodeEnd(InputTextRenderer.java:67)
  at javax.faces.api@3.0.0.SP04//javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:595)
  at javax.faces.api@3.0.0.SP04//javax.faces.component.UIComponent.encodeAll(UIComponent.java:1654)
  at javax.faces.api@3.0.0.SP04//javax.faces.render.Renderer.encodeChildren(Renderer.java:152)
  at javax.faces.api@3.0.0.SP04//javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:566)
  at javax.faces.api@3.0.0.SP04//javax.faces.component.UIComponent.encodeAll(UIComponent.java:1647)
  at javax.faces.api@3.0.0.SP04//javax.faces.component.UIComponent.encodeAll(UIComponent.java:1650)
  at javax.faces.api@3.0.0.SP04//javax.faces.component.UIComponent.encodeAll(UIComponent.java:1650)
  at com.sun.jsf-impl@2.3.14.SP02//com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:468)
  at com.sun.jsf-impl@2.3.14.SP02//com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:170)
  at javax.faces.api@3.0.0.SP04//javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:132)
  at javax.faces.api@3.0.0.SP04//javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:132)
  at javax.faces.api@3.0.0.SP04//javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:132)
  at deployment//org.omnifaces.viewhandler.OmniViewHandler.renderView(OmniViewHandler.java:155)
  at javax.faces.api@3.0.0.SP04//javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:132)
  at com.sun.jsf-impl@2.3.14.SP02//com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:102)
  at com.sun.jsf-impl@2.3.14.SP02//com.sun.faces.lifecycle.Phase.doPhase(Phase.java:76)
  at com.sun.jsf-impl@2.3.14.SP02//com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:199)
  at javax.faces.api@3.0.0.SP04//javax.faces.webapp.FacesServlet.executeLifecyle(FacesServlet.java:708)
  at javax.faces.api@3.0.0.SP04//javax.faces.webapp.FacesServlet.service(FacesServlet.java:451)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:74)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:129)
  at io.opentracing.contrib.opentracing-jaxrs2//io.opentracing.contrib.jaxrs2.server.SpanFinishingFilter.doFilter(SpanFinishingFilter.java:52)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
  at deployment//org.omnifaces.facesviews.FacesViewsForwardingFilter.filterExtensionLessToExtension(FacesViewsForwardingFilter.java:180)
  at deployment//org.omnifaces.facesviews.FacesViewsForwardingFilter.filterExtensionLess(FacesViewsForwardingFilter.java:137)
  at deployment//org.omnifaces.facesviews.FacesViewsForwardingFilter.filterExtensionLess(FacesViewsForwardingFilter.java:123)
  at deployment//org.omnifaces.facesviews.FacesViewsForwardingFilter.doFilter(FacesViewsForwardingFilter.java:84)
  at deployment//org.omnifaces.filter.HttpFilter.doFilter(HttpFilter.java:108)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:84)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.handlers.ServletChain$1.handleRequest(ServletChain.java:68)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
  at org.wildfly.security.elytron-web.undertow-server@1.8.0.Final//org.wildfly.elytron.web.undertow.server.ElytronRunAsHandler.lambda$handleRequest$1(ElytronRunAsHandler.java:68)
  at org.wildfly.security.elytron-private@1.14.1.Final//org.wildfly.security.auth.server.FlexibleIdentityAssociation.runAsFunctionEx(FlexibleIdentityAssociation.java:103)
  at org.wildfly.security.elytron-private@1.14.1.Final//org.wildfly.security.auth.server.Scoped.runAsFunctionEx(Scoped.java:161)
  at org.wildfly.security.elytron-private@1.14.1.Final//org.wildfly.security.auth.server.Scoped.runAs(Scoped.java:73)
  at org.wildfly.security.elytron-web.undertow-server@1.8.0.Final//org.wildfly.elytron.web.undertow.server.ElytronRunAsHandler.handleRequest(ElytronRunAsHandler.java:67)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.handlers.RedirectDirHandler.handleRequest(RedirectDirHandler.java:68)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:117)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
  at io.undertow.core@2.2.4.Final//io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
  at io.undertow.core@2.2.4.Final//io.undertow.security.handlers.AuthenticationConstraintHandler.handleRequest(AuthenticationConstraintHandler.java:53)
  at io.undertow.core@2.2.4.Final//io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.handlers.security.ServletSecurityConstraintHandler.handleRequest(ServletSecurityConstraintHandler.java:59)
  at io.undertow.core@2.2.4.Final//io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
  at org.wildfly.security.elytron-web.undertow-server-servlet@1.8.0.Final//org.wildfly.elytron.web.undertow.server.servlet.CleanUpHandler.handleRequest(CleanUpHandler.java:38)
  at io.undertow.core@2.2.4.Final//io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
  at org.wildfly.extension.undertow@22.0.1.Final//org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
  at io.undertow.core@2.2.4.Final//io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
  at org.wildfly.extension.undertow@22.0.1.Final//org.wildfly.extension.undertow.deployment.GlobalRequestControllerHandler.handleRequest(GlobalRequestControllerHandler.java:68)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.handlers.SendErrorPageHandler.handleRequest(SendErrorPageHandler.java:52)
  at io.undertow.core@2.2.4.Final//io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:269)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:78)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:133)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:130)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
  at org.wildfly.extension.undertow@22.0.1.Final//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1530)
  at org.wildfly.extension.undertow@22.0.1.Final//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1530)
  at org.wildfly.extension.undertow@22.0.1.Final//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1530)
  at org.wildfly.extension.undertow@22.0.1.Final//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1530)
  at org.wildfly.extension.undertow@22.0.1.Final//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1530)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:249)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:78)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:99)
  at io.undertow.core@2.2.4.Final//io.undertow.server.Connectors.executeRootHandler(Connectors.java:387)
  at io.undertow.core@2.2.4.Final//io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:841)
  at org.jboss.threads@2.4.0.Final//org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
  at org.jboss.threads@2.4.0.Final//org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1990)
  at org.jboss.threads@2.4.0.Final//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1486)
  at org.jboss.threads@2.4.0.Final//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1377)
  at org.jboss.xnio@3.8.4.Final//org.xnio.XnioWorker$WorkerThreadFactory$1$1.run(XnioWorker.java:1280)
  at java.base/java.lang.Thread.run(Thread.java:834)

I am running on OpenJDK 11.0.9.1+1, WildFly 22.0.1.Final, OmniFaces 3.10.1 (and if it does matter PrimeFaces 10.0.0-RC1)

Because of this question i added disableBeanValidation = true to @Param and the NPE is not thrown. But this feels like i am doing something wrong in the first place (also the Javadoc on this is somewhat outdated). Also, if i ommit the custom CDI interceptor (what is not an option for me) the NPE is not thrown at all.

How to resolve this properly? Am i doing something wrong or is this a bug, maybe?


Solution

  • It was a bug in OmniFaces @Param which manifested since version 3.6 during the work to remove the @Inject requirement from the @Param. It didn't work properly when the managed bean is proxied as an interceptor target. The Object#getClass() of the managed bean returned the proxied class and the BeanManager#resolve() couldn't find a Bean<X> for this proxied class and returned null. This unexpected condition eventually resulted in a NullPointerException while performing bean validation.

    It has been fixed as per issue 624 which will be available in OmniFaces 3.11 and 4.0-M8.

    In the meantime, a work around is to add back the @Inject to your @Param.

    @Inject @Param(required = true)
    private String token;