Search in sources :

Example 1 with NavigationHandler

use of jakarta.faces.application.NavigationHandler in project core by weld.

the class MyExceptionHandler method handle.

@Override
public void handle() throws FacesException {
    if (getUnhandledExceptionQueuedEvents().iterator().hasNext()) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        NavigationHandler navHandler = facesContext.getApplication().getNavigationHandler();
        navHandler.handleNavigation(facesContext, null, "/error.jsf?faces-redirect=true");
    }
}
Also used : FacesContext(jakarta.faces.context.FacesContext) NavigationHandler(jakarta.faces.application.NavigationHandler)

Example 2 with NavigationHandler

use of jakarta.faces.application.NavigationHandler in project myfaces-tobago by apache.

the class TobagoExceptionHandler method handle.

@Override
public void handle() throws FacesException {
    final FacesContext facesContext = FacesContext.getCurrentInstance();
    if (facesContext.getPartialViewContext().isAjaxRequest()) {
        final Iterator<ExceptionQueuedEvent> events = getUnhandledExceptionQueuedEvents().iterator();
        if (events.hasNext()) {
            final Throwable exception = events.next().getContext().getException();
            if (!(exception instanceof AbortProcessingException)) {
                final String errorPageLocation = WebXmlUtils.getErrorPageLocation(exception);
                if (errorPageLocation != null && (facesContext.getCurrentPhaseId() != PhaseId.RENDER_RESPONSE || !facesContext.getExternalContext().isResponseCommitted())) {
                    try {
                        final HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
                        final HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
                        request.setAttribute("javax.servlet.error.exception", exception);
                        request.setAttribute("javax.servlet.error.exception_type", exception.getClass());
                        request.setAttribute("javax.servlet.error.message", exception.getMessage());
                        request.setAttribute("javax.servlet.error.request_uri", request.getRequestURI());
                        request.setAttribute("javax.servlet.error.status_code", HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                        renderErrorPage(facesContext, errorPageLocation);
                        cleanupExceptionQueuedEvents();
                    } catch (final IOException e) {
                        throw new FacesException(e);
                    }
                } else {
                    LOG.debug("Can't return an error page. errorPageLocation='{}'", errorPageLocation);
                }
            }
        }
    } else {
        final Iterator<ExceptionQueuedEvent> iterator = getUnhandledExceptionQueuedEvents().iterator();
        while (iterator.hasNext()) {
            final ExceptionQueuedEvent event = iterator.next();
            final ExceptionQueuedEventContext context = (ExceptionQueuedEventContext) event.getSource();
            Throwable cause = this.getWrapped().getRootCause(context.getException());
            if (cause == null) {
                cause = context.getException();
            }
            final NavigationHandler nav = facesContext.getApplication().getNavigationHandler();
            if (cause instanceof ViewExpiredException || cause != null && cause.getCause() instanceof ViewExpiredException) {
                final ViewExpiredException viewExpiredException = (ViewExpiredException) (cause instanceof ViewExpiredException ? cause : cause.getCause());
                try {
                    facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "The view has been expired!", "Please check the given data or try to start from the beginning."));
                    nav.handleNavigation(facesContext, null, viewExpiredException.getViewId());
                    facesContext.renderResponse();
                    LOG.debug("Handling ViewExpiredException on viewId: {}", viewExpiredException.getViewId());
                } finally {
                    iterator.remove();
                }
            } else {
                try {
                    final boolean error404 = cause instanceof FileNotFoundException || cause != null && cause.getCause() instanceof FileNotFoundException;
                    final FacesMessage message;
                    if (error404) {
                        message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "The page was not found!", "The requested page was not found!");
                        facesContext.getExternalContext().setResponseStatus(HttpServletResponse.SC_NOT_FOUND);
                        LOG.warn("Handling 404 exception.");
                    } else {
                        message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "An unknown error has occurred!" + " xxx", "An unknown error has occurred!" + " xxx");
                        facesContext.getExternalContext().setResponseStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                        LOG.warn("Handling 500 exception.", cause);
                    }
                    facesContext.addMessage(null, message);
                    final String viewId = "/tobago/error.xhtml";
                    // when the rendering was not yet started, we can forward to an error page
                    if (event.getContext().getPhaseId().getOrdinal() < PhaseId.RENDER_RESPONSE.getOrdinal()) {
                        nav.handleNavigation(facesContext, null, viewId);
                        facesContext.renderResponse();
                    } else {
                        final HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
                        // undo rendering, if you can.
                        response.resetBuffer();
                        final ViewHandler viewHandler = facesContext.getApplication().getViewHandler();
                        final ViewDeclarationLanguage vdl = viewHandler.getViewDeclarationLanguage(facesContext, viewId);
                        final UIViewRoot viewRoot = viewHandler.createView(facesContext, viewId);
                        vdl.buildView(facesContext, viewRoot);
                        facesContext.getApplication().publishEvent(facesContext, PreRenderViewEvent.class, viewRoot);
                        vdl.renderView(facesContext, viewRoot);
                    }
                } catch (Exception e) {
                    LOG.error("Exception while exception handling!", e);
                } finally {
                    facesContext.responseComplete();
                    iterator.remove();
                }
            }
        }
    }
    super.handle();
}
Also used : FacesContext(jakarta.faces.context.FacesContext) ExceptionQueuedEvent(jakarta.faces.event.ExceptionQueuedEvent) ViewHandler(jakarta.faces.application.ViewHandler) FileNotFoundException(java.io.FileNotFoundException) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) IOException(java.io.IOException) ViewDeclarationLanguage(jakarta.faces.view.ViewDeclarationLanguage) NavigationHandler(jakarta.faces.application.NavigationHandler) FacesException(jakarta.faces.FacesException) IOException(java.io.IOException) AbortProcessingException(jakarta.faces.event.AbortProcessingException) FileNotFoundException(java.io.FileNotFoundException) FacesException(jakarta.faces.FacesException) ViewExpiredException(jakarta.faces.application.ViewExpiredException) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ViewExpiredException(jakarta.faces.application.ViewExpiredException) ExceptionQueuedEventContext(jakarta.faces.event.ExceptionQueuedEventContext) AbortProcessingException(jakarta.faces.event.AbortProcessingException) FacesMessage(jakarta.faces.application.FacesMessage) UIViewRoot(jakarta.faces.component.UIViewRoot)

Example 3 with NavigationHandler

use of jakarta.faces.application.NavigationHandler in project myfaces-tobago by apache.

the class AjaxUtils method navigate.

public static void navigate(final FacesContext facesContext, final Object outcome) {
    final Application application = facesContext.getApplication();
    final NavigationHandler navigationHandler = application.getNavigationHandler();
    navigationHandler.handleNavigation(facesContext, null, outcome != null ? outcome.toString() : null);
    facesContext.renderResponse();
}
Also used : NavigationHandler(jakarta.faces.application.NavigationHandler) Application(jakarta.faces.application.Application)

Example 4 with NavigationHandler

use of jakarta.faces.application.NavigationHandler in project mojarra by eclipse-ee4j.

the class ActionListenerImpl method invokeNavigationHandling.

private void invokeNavigationHandling(FacesContext context, UIComponent source, MethodExpression expression, String outcome) {
    NavigationHandler navHandler = context.getApplication().getNavigationHandler();
    String toFlowDocumentId = (String) source.getAttributes().get(TO_FLOW_DOCUMENT_ID_ATTR_NAME);
    if (toFlowDocumentId == null) {
        navHandler.handleNavigation(context, expression != null ? expression.getExpressionString() : null, outcome);
    } else {
        navHandler.handleNavigation(context, expression != null ? expression.getExpressionString() : null, outcome, toFlowDocumentId);
    }
}
Also used : NavigationHandler(jakarta.faces.application.NavigationHandler)

Example 5 with NavigationHandler

use of jakarta.faces.application.NavigationHandler in project mojarra by eclipse-ee4j.

the class ApplicationConfigProcessor method setNavigationHandler.

private void setNavigationHandler(ServletContext servletContext, FacesContext facesContext, Application application, Node navigationHandler) {
    if (navigationHandler != null) {
        String handler = getNodeText(navigationHandler);
        if (handler != null) {
            Class<?> rootType = findRootType(servletContext, facesContext, handler, navigationHandler, new Class[] { ConfigurableNavigationHandler.class, NavigationHandler.class });
            boolean[] didPerformInjection = { false };
            NavigationHandler instance = (NavigationHandler) createInstance(servletContext, facesContext, handler, rootType != null ? rootType : NavigationHandler.class, application.getNavigationHandler(), navigationHandler, true, didPerformInjection);
            if (instance != null) {
                if (didPerformInjection[0]) {
                    navigationHandlers.add(instance);
                }
                if (LOGGER.isLoggable(FINE)) {
                    LOGGER.log(FINE, format("Calling Application.setNavigationHandlers({0})", handler));
                }
                application.setNavigationHandler(instance);
            }
        }
    }
}
Also used : Util.getLocaleFromString(com.sun.faces.util.Util.getLocaleFromString) ConfigurableNavigationHandler(jakarta.faces.application.ConfigurableNavigationHandler) NavigationHandler(jakarta.faces.application.NavigationHandler)

Aggregations

NavigationHandler (jakarta.faces.application.NavigationHandler)26 FacesContext (jakarta.faces.context.FacesContext)14 PrintWriter (java.io.PrintWriter)11 ConfigurableNavigationHandler (jakarta.faces.application.ConfigurableNavigationHandler)7 Application (jakarta.faces.application.Application)6 FacesException (jakarta.faces.FacesException)3 NavigationCase (jakarta.faces.application.NavigationCase)3 ViewHandler (jakarta.faces.application.ViewHandler)3 TCKNavigationHandler (com.sun.ts.tests.jsf.common.navigation.TCKNavigationHandler)2 UIViewRoot (jakarta.faces.component.UIViewRoot)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 DocumentInfo (com.sun.faces.config.manager.documents.DocumentInfo)1 Util.getLocaleFromString (com.sun.faces.util.Util.getLocaleFromString)1 MethodExpression (jakarta.el.MethodExpression)1 ApplicationFactory (jakarta.faces.application.ApplicationFactory)1 ApplicationWrapper (jakarta.faces.application.ApplicationWrapper)1 FacesMessage (jakarta.faces.application.FacesMessage)1