Search in sources :

Example 1 with ExceptionQueuedEventContext

use of javax.faces.event.ExceptionQueuedEventContext in project ART-TIME by Artezio.

the class ExceptionHandler method handle.

@Override
public void handle() throws FacesException {
    final Iterator<ExceptionQueuedEvent> iterator = getUnhandledExceptionQueuedEvents().iterator();
    List<String> errorMessages = new ArrayList<String>();
    while (iterator.hasNext()) {
        ExceptionQueuedEvent exceptionEvent = iterator.next();
        ExceptionQueuedEventContext exceptionEventContext = (ExceptionQueuedEventContext) exceptionEvent.getSource();
        Throwable incomingException = exceptionEventContext.getException();
        List<String> messages = findErrorMessages(incomingException);
        if (!messages.isEmpty()) {
            errorMessages.addAll(messages);
            iterator.remove();
        }
    }
    if (!errorMessages.isEmpty()) {
        MessagesUtil.removeMessagesLessThan(FacesMessage.SEVERITY_ERROR);
        for (String message : errorMessages) {
            MessagesUtil.addError(null, message);
        }
        resetDataTablesRowIndex(getViewRoot());
    }
    getWrapped().handle();
}
Also used : ExceptionQueuedEvent(javax.faces.event.ExceptionQueuedEvent) ExceptionQueuedEventContext(javax.faces.event.ExceptionQueuedEventContext)

Example 2 with ExceptionQueuedEventContext

use of javax.faces.event.ExceptionQueuedEventContext 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 3 with ExceptionQueuedEventContext

use of javax.faces.event.ExceptionQueuedEventContext 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 (isSecurityException(t)) {
                performRedirect(externalContext, "/login");
            } else if (isConversationException(t)) {
                log.error(t.getMessage(), t);
                performRedirect(externalContext, "/conversation_error");
            }
            if (isViewExpiredException(t)) {
                storeRequestURI();
                performRedirect(externalContext, "/login");
            } else {
                log.error(t.getMessage(), t);
                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 4 with ExceptionQueuedEventContext

use of javax.faces.event.ExceptionQueuedEventContext in project ART-TIME by Artezio.

the class ExceptionHandlerTest method testHandle_ifErrorMessageIsNull.

@Test
public final void testHandle_ifErrorMessageIsNull() {
    PowerMock.mockStatic(MessagesUtil.class);
    PowerMock.mockStatic(ExceptionHandler.class);
    exceptionHandler = createMockBuilder(ExceptionHandler.class).addMockedMethod("findErrorMessages").addMockedMethod("getUnhandledExceptionQueuedEvents").addMockedMethod("getWrapped").createMock();
    wrappedExceptionHandler = EasyMock.createMock(javax.faces.context.ExceptionHandler.class);
    PowerMock.mockStatic(MessagesUtil.class);
    Throwable exceptionForSearch = new ArithmeticException();
    ExceptionQueuedEventContext exceptionEventContext = new ExceptionQueuedEventContext(FacesContext.getCurrentInstance(), exceptionForSearch);
    ExceptionQueuedEvent exceptionEvent = new ExceptionQueuedEvent(exceptionEventContext);
    Iterable<ExceptionQueuedEvent> exceptionEvents = new LinkedList<ExceptionQueuedEvent>(Arrays.asList(exceptionEvent));
    expect(exceptionHandler.getUnhandledExceptionQueuedEvents()).andReturn(exceptionEvents);
    expect(exceptionHandler.findErrorMessages(exceptionForSearch)).andReturn(new ArrayList<String>());
    expect(exceptionHandler.getWrapped()).andReturn(wrappedExceptionHandler);
    wrappedExceptionHandler.handle();
    PowerMock.expectLastCall();
    PowerMock.replay(exceptionHandler, wrappedExceptionHandler, MessagesUtil.class);
    exceptionHandler.handle();
    PowerMock.verify(exceptionHandler, wrappedExceptionHandler, MessagesUtil.class);
}
Also used : ExceptionQueuedEvent(javax.faces.event.ExceptionQueuedEvent) ExceptionQueuedEventContext(javax.faces.event.ExceptionQueuedEventContext) Matchers.anyString(org.mockito.Matchers.anyString) LinkedList(java.util.LinkedList) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 5 with ExceptionQueuedEventContext

use of javax.faces.event.ExceptionQueuedEventContext in project ART-TIME by Artezio.

the class ExceptionHandlerTest method testHandle.

@Test
public final void testHandle() {
    PowerMock.mockStatic(MessagesUtil.class);
    PowerMock.mockStatic(ExceptionHandler.class);
    exceptionHandler = createMockBuilder(ExceptionHandler.class).addMockedMethod("findErrorMessages").addMockedMethod("getUnhandledExceptionQueuedEvents").addMockedMethod("getWrapped").createMock();
    wrappedExceptionHandler = EasyMock.createMock(javax.faces.context.ExceptionHandler.class);
    PowerMock.mockStatic(MessagesUtil.class);
    Throwable exceptionForSearch = new ArithmeticException();
    ExceptionQueuedEventContext exceptionEventContext = new ExceptionQueuedEventContext(FacesContext.getCurrentInstance(), exceptionForSearch);
    ExceptionQueuedEvent exceptionEvent = new ExceptionQueuedEvent(exceptionEventContext);
    Iterable<ExceptionQueuedEvent> exceptionEvents = new LinkedList<ExceptionQueuedEvent>(Arrays.asList(exceptionEvent));
    List<String> messages = Arrays.asList("Exception message");
    expect(exceptionHandler.getUnhandledExceptionQueuedEvents()).andReturn(exceptionEvents);
    expect(exceptionHandler.findErrorMessages(exceptionForSearch)).andReturn(messages);
    MessagesUtil.removeMessagesLessThan(FacesMessage.SEVERITY_ERROR);
    MessagesUtil.addError(null, messages.get(0));
    expect(exceptionHandler.getWrapped()).andReturn(wrappedExceptionHandler);
    wrappedExceptionHandler.handle();
    PowerMock.replay(exceptionHandler, wrappedExceptionHandler, MessagesUtil.class);
    exceptionHandler.handle();
    PowerMock.verify(exceptionHandler, wrappedExceptionHandler, MessagesUtil.class);
}
Also used : ExceptionQueuedEvent(javax.faces.event.ExceptionQueuedEvent) ExceptionQueuedEventContext(javax.faces.event.ExceptionQueuedEventContext) Matchers.anyString(org.mockito.Matchers.anyString) LinkedList(java.util.LinkedList) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

ExceptionQueuedEvent (javax.faces.event.ExceptionQueuedEvent)8 ExceptionQueuedEventContext (javax.faces.event.ExceptionQueuedEventContext)8 FacesContext (javax.faces.context.FacesContext)5 ExternalContext (javax.faces.context.ExternalContext)3 LinkedList (java.util.LinkedList)2 Flash (javax.faces.context.Flash)2 Test (org.junit.Test)2 Matchers.anyString (org.mockito.Matchers.anyString)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 AuthorizationException (fi.otavanopisto.security.AuthorizationException)1 FileNotFoundException (java.io.FileNotFoundException)1 EJBException (javax.ejb.EJBException)1 ELException (javax.el.ELException)1 ContextNotActiveException (javax.enterprise.context.ContextNotActiveException)1 CreationException (javax.enterprise.inject.CreationException)1 FacesException (javax.faces.FacesException)1 ConfigurableNavigationHandler (javax.faces.application.ConfigurableNavigationHandler)1 NavigationHandler (javax.faces.application.NavigationHandler)1 ViewExpiredException (javax.faces.application.ViewExpiredException)1 UIViewRoot (javax.faces.component.UIViewRoot)1