1

When an exception is handled on AJAX request, the user has to be redirected to another page. There is a class with static method that executes the redirection. There is a problem when the request is ajax. Here is the code of the mentioned functionality:

public static void redirect(FacesContext fc, String view, int paramValue){
    ExternalContext ec = fc.getExternalContext();
    String path = ec.encodeResourceURL(fc.getApplication().getViewHandler().getActionURL(fc, view) + "?p=" + paramValue);
    try {
        ec.redirect(path);
    } catch(Exception e) {
        throw new RuntimeException();
    }
}

The line 'ec.redirect(path)' don't throws any exception. In the browser debug console I can see the next 500 Internal Server Error:

Stack Trace
javax.servlet.ServletException
javax.faces.webapp.FacesServlet.service(FacesServlet.java:659)
io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:61)
io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:113)
io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:56)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:45)
io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:61)
io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58)
io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:70)
io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:240)
io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:227)
io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:73)
io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:146)
 io.undertow.server.Connectors.executeRootHandler(Connectors.java:177)
 io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:727)
 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
java.lang.Thread.run(Thread.java:745)

The user is not redirected. If is maded another ajax request from the same page the process is repeated and the server 500 error is launched in the browser console.

I'm using wildfly 8.1 and Mojarra 2.2.6-jbossorg-4 20140501-1134. How can I redirect properly on ajax request?

Edit

Fox exception handling is used a custom ExceptionHandlerWrapper and Deltaspike Exception Handler:

public class CustomExceptionHandlerWrapper extends ExceptionHandlerWrapper {

@Override
public void handle() throws FacesException {
    FacesContext context = FacesContext.getCurrentInstance();
    Iterator<ExceptionQueuedEvent> it = getUnhandledExceptionQueuedEvents().iterator();
    while (it.hasNext()) {
        try {
            ExceptionQueuedEvent evt = it.next();
            ExceptionToCatchEvent etce = null;

            if (context == null || !context.getPartialViewContext().isAjaxRequest()) {
                etce = new ExceptionToCatchEvent(evt.getContext().getException(), WebRequestLiteral.INSTANCE);
            } else {
                etce = new ExceptionToCatchEvent(evt.getContext().getException(), FacesRequestLiteral.INSTANCE);
            }
            beanManager.fireEvent(etce);
        } finally {
            it.remove();
        }

    }
    getWrapped().handle();
}
}

Deltaspike Handler (Documentation):

public class FacesExceptionHandler {
void showFacesMessage(@Handles @FacesRequest ExceptionEvent<Throwable> evt, FacesContext fc) {
    Throwable exception = evt.getException();
    if(exception  instanceof NotLoggedException) {
        evt.handled();
        JSF.redirect(FacesContext.getCurrentInstance(), "/login.jsf", 1);
    }
}

}

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Tvori
  • 298
  • 8
  • 19
  • Do you want to redirect to a target error page configured in web.xml on errors/exceptions during asynchronous requests? – Tiny Dec 21 '14 at 12:01
  • I want to redirect programatically when an list of expected exceptions are throwed. The request is asynchronous (ajax). – Tvori Dec 22 '14 at 14:39
  • @BalusC the question was edited to explain the way of how is handled the exception – Tvori Dec 22 '14 at 15:17

0 Answers0