0
votes

Im trying to use @aroundInvoke in my interceptor, checking some validationX to proceed with the execution of the method or else REDIRECT the user to a specific JSF page.

@AroundInvoke
public Object myInterceptor(InvocationContext invocation) throws Exception {
        if(validationX){//it passes the validation so proceed
            return invocation.proceed();    
        }else{//it doesnt passes the validation so go to 
            //DO SOMETHING TO REDIRECT TO SPECIFIC JSF
        }

    }

I've tried:

  • returning String with "JSF?faces-redirect=true" ----- I get converting error(String to whatever the metod should return).
  • FacesContext.getCurrentInstance().getExternalContext().dispatch("/JSF.jsf"); ----- I get nullPointerException with getCurrentInstance method.
  • FacesContext.getCurrentInstance().getExternalContext().redirect("/JSF.jsf"); -------The same NullPointerException.
    • Using HttpServletRequest and HttpServletResponse to generate a response but I get NullPointer Exception.

Thanks in advance!

1

1 Answers

3
votes

The Java EE interceptor isn't intented for that. It's intented to be used to decide whether to proceed the invocation of the current CDI/EJB bean method or not (and if necessary throw an exception to abort it), and/or to control/manipulate its return value. It's not intented to act as a JSF controller. For that a normal JSF managed bean should be used instead.

Or, if this concerns an exceptional circumstance, then you should just throw a specific exception and register an <error-page> in web.xml on exactly that exception type. If you can hook on specific URLs rather than specific method invocations, another way would be to perform the job in a simple servlet filter instead.

As the concrete functional requirement for which you incorrectly thought that this would be the right solution is not clear from the question, a more detailed answer can't be given.