Search in sources :

Example 1 with ExceptionQueuedEvent

use of javax.faces.event.ExceptionQueuedEvent in project deltaspike by apache.

the class BridgeExceptionHandlerWrapper method processEvent.

@Override
public void processEvent(SystemEvent event) throws AbortProcessingException {
    //needed because #handle gets called too late in this case
    if (event instanceof ExceptionQueuedEvent) {
        ExceptionQueuedEvent exceptionQueuedEvent = (ExceptionQueuedEvent) event;
        FacesContext facesContext = exceptionQueuedEvent.getContext().getContext();
        if (facesContext.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE && exceptionQueuedEvent.getContext().inBeforePhase()) {
            Throwable exception = getRootCause(exceptionQueuedEvent.getContext().getException());
            if (exception instanceof AccessDeniedException) {
                processAccessDeniedException(exception);
            } else {
                ExceptionToCatchEvent exceptionToCatchEvent = new ExceptionToCatchEvent(exception);
                exceptionToCatchEvent.setOptional(true);
                this.beanManager.fireEvent(exceptionToCatchEvent);
                if (exceptionToCatchEvent.isHandled()) {
                    return;
                }
            }
        }
    }
    super.processEvent(event);
}
Also used : ExceptionQueuedEvent(javax.faces.event.ExceptionQueuedEvent) FacesContext(javax.faces.context.FacesContext) ErrorViewAwareAccessDeniedException(org.apache.deltaspike.security.api.authorization.ErrorViewAwareAccessDeniedException) AccessDeniedException(org.apache.deltaspike.security.api.authorization.AccessDeniedException) ExceptionToCatchEvent(org.apache.deltaspike.core.api.exception.control.event.ExceptionToCatchEvent)

Example 2 with ExceptionQueuedEvent

use of javax.faces.event.ExceptionQueuedEvent in project oxTrust by GluuFederation.

the class GlobalExceptionHandler method handle.

public void handle() throws FacesException {
    final Iterator<ExceptionQueuedEvent> i = getUnhandledExceptionQueuedEvents().iterator();
    while (i.hasNext()) {
        ExceptionQueuedEvent event = i.next();
        ExceptionQueuedEventContext context = (ExceptionQueuedEventContext) event.getSource();
        Throwable t = context.getException();
        final FacesContext fc = FacesContext.getCurrentInstance();
        final ExternalContext externalContext = fc.getExternalContext();
        final ConfigurableNavigationHandler nav = (ConfigurableNavigationHandler) fc.getApplication().getNavigationHandler();
        try {
            if (isForbidden(t))
                performRedirect(externalContext, "/login");
            else
                performRedirect(externalContext, "/error");
            fc.renderResponse();
        } finally {
            i.remove();
        }
    }
    getWrapped().handle();
}
Also used : ExceptionQueuedEvent(javax.faces.event.ExceptionQueuedEvent) FacesContext(javax.faces.context.FacesContext) ExceptionQueuedEventContext(javax.faces.event.ExceptionQueuedEventContext) ExternalContext(javax.faces.context.ExternalContext) ConfigurableNavigationHandler(javax.faces.application.ConfigurableNavigationHandler)

Example 3 with ExceptionQueuedEvent

use of javax.faces.event.ExceptionQueuedEvent in project deltaspike by apache.

the class DefaultErrorViewAwareExceptionHandlerWrapper method handle.

@Override
public void handle() throws FacesException {
    lazyInit();
    Iterator<ExceptionQueuedEvent> exceptionQueuedEventIterator = getUnhandledExceptionQueuedEvents().iterator();
    while (exceptionQueuedEventIterator.hasNext()) {
        ExceptionQueuedEventContext exceptionQueuedEventContext = (ExceptionQueuedEventContext) exceptionQueuedEventIterator.next().getSource();
        @SuppressWarnings({ "ThrowableResultOfMethodCallIgnored" }) Throwable throwable = exceptionQueuedEventContext.getException();
        String viewId = null;
        if (!isExceptionToHandle(throwable)) {
            continue;
        }
        FacesContext facesContext = exceptionQueuedEventContext.getContext();
        Flash flash = facesContext.getExternalContext().getFlash();
        if (throwable instanceof ViewExpiredException) {
            viewId = ((ViewExpiredException) throwable).getViewId();
        } else if (throwable instanceof ContextNotActiveException) {
            //(it's recorded below - see flash.put(throwable.getClass().getName(), throwable);)
            if (flash.containsKey(ContextNotActiveException.class.getName())) {
                //TODO show it in case of project-stage development
                break;
            }
            if (facesContext.getViewRoot() != null) {
                viewId = facesContext.getViewRoot().getViewId();
            } else {
                viewId = BeanProvider.getContextualReference(ViewConfigResolver.class).getDefaultErrorViewConfigDescriptor().getViewId();
            }
        }
        if (viewId != null) {
            UIViewRoot uiViewRoot = facesContext.getApplication().getViewHandler().createView(facesContext, viewId);
            if (uiViewRoot == null) {
                continue;
            }
            if (facesContext.isProjectStage(javax.faces.application.ProjectStage.Development) || ProjectStageProducer.getInstance().getProjectStage() == ProjectStage.Development || ProjectStageProducer.getInstance().getProjectStage() instanceof TestStage) {
                throwable.printStackTrace();
            }
            facesContext.setViewRoot(uiViewRoot);
            exceptionQueuedEventIterator.remove();
            //record the current exception -> to check it at the next call or to use it on the error-page
            flash.put(throwable.getClass().getName(), throwable);
            flash.keep(throwable.getClass().getName());
            this.viewNavigationHandler.navigateTo(DefaultErrorView.class);
            break;
        }
    }
    this.wrapped.handle();
}
Also used : ExceptionQueuedEvent(javax.faces.event.ExceptionQueuedEvent) FacesContext(javax.faces.context.FacesContext) ViewExpiredException(javax.faces.application.ViewExpiredException) ExceptionQueuedEventContext(javax.faces.event.ExceptionQueuedEventContext) ContextNotActiveException(javax.enterprise.context.ContextNotActiveException) Flash(javax.faces.context.Flash) UIViewRoot(javax.faces.component.UIViewRoot) TestStage(org.apache.deltaspike.core.api.projectstage.TestStage)

Example 4 with ExceptionQueuedEvent

use of javax.faces.event.ExceptionQueuedEvent in project deltaspike by apache.

the class BridgeExceptionHandlerWrapper method handle.

@Override
public void handle() throws FacesException {
    FacesContext context = FacesContext.getCurrentInstance();
    if (context == null || context.getResponseComplete()) {
        return;
    }
    Iterable<ExceptionQueuedEvent> exceptionQueuedEvents = getUnhandledExceptionQueuedEvents();
    if (exceptionQueuedEvents != null && exceptionQueuedEvents.iterator() != null) {
        Iterator<ExceptionQueuedEvent> iterator = exceptionQueuedEvents.iterator();
        while (iterator.hasNext()) {
            Throwable throwable = iterator.next().getContext().getException();
            Throwable rootCause = getRootCause(throwable);
            if (rootCause instanceof AccessDeniedException) {
                processAccessDeniedException(rootCause);
                iterator.remove();
                continue;
            } else {
                ExceptionToCatchEvent event = new ExceptionToCatchEvent(rootCause, exceptionQualifier);
                event.setOptional(true);
                beanManager.fireEvent(event);
                if (event.isHandled()) {
                    iterator.remove();
                }
            }
            // a handle method might redirect and set responseComplete
            if (context.getResponseComplete()) {
                break;
            }
        }
    }
    super.handle();
}
Also used : FacesContext(javax.faces.context.FacesContext) ExceptionQueuedEvent(javax.faces.event.ExceptionQueuedEvent) ErrorViewAwareAccessDeniedException(org.apache.deltaspike.security.api.authorization.ErrorViewAwareAccessDeniedException) AccessDeniedException(org.apache.deltaspike.security.api.authorization.AccessDeniedException) ExceptionToCatchEvent(org.apache.deltaspike.core.api.exception.control.event.ExceptionToCatchEvent)

Aggregations

FacesContext (javax.faces.context.FacesContext)4 ExceptionQueuedEvent (javax.faces.event.ExceptionQueuedEvent)4 ExceptionQueuedEventContext (javax.faces.event.ExceptionQueuedEventContext)2 ExceptionToCatchEvent (org.apache.deltaspike.core.api.exception.control.event.ExceptionToCatchEvent)2 AccessDeniedException (org.apache.deltaspike.security.api.authorization.AccessDeniedException)2 ErrorViewAwareAccessDeniedException (org.apache.deltaspike.security.api.authorization.ErrorViewAwareAccessDeniedException)2 ContextNotActiveException (javax.enterprise.context.ContextNotActiveException)1 ConfigurableNavigationHandler (javax.faces.application.ConfigurableNavigationHandler)1 ViewExpiredException (javax.faces.application.ViewExpiredException)1 UIViewRoot (javax.faces.component.UIViewRoot)1 ExternalContext (javax.faces.context.ExternalContext)1 Flash (javax.faces.context.Flash)1 TestStage (org.apache.deltaspike.core.api.projectstage.TestStage)1