Search in sources :

Example 1 with ViewExpiredException

use of jakarta.faces.application.ViewExpiredException 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 2 with ViewExpiredException

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

the class RestoreViewPhase method execute.

/**
 * PRECONDITION: the necessary factories have been installed in the ServletContext attr set.
 * <P>
 *
 * POSTCONDITION: The facesContext has been initialized with a tree.
 */
@Override
public void execute(FacesContext facesContext) throws FacesException {
    LOGGER.fine("Entering RestoreViewPhase");
    if (facesContext == null) {
        throw new FacesException(getExceptionMessageString(NULL_CONTEXT_ERROR_MESSAGE_ID));
    }
    // If an app had explicitely set the tree in the context, use that;
    UIViewRoot viewRoot = facesContext.getViewRoot();
    if (viewRoot != null) {
        LOGGER.fine("Found a pre created view in FacesContext");
        facesContext.getViewRoot().setLocale(facesContext.getExternalContext().getRequestLocale());
        // Do per-component actions
        deliverPostRestoreStateEvent(facesContext);
        if (!facesContext.isPostback()) {
            facesContext.renderResponse();
        }
        return;
    }
    FacesException thrownException = null;
    try {
        // Reconstitute or create the request tree
        Map<String, Object> requestMap = facesContext.getExternalContext().getRequestMap();
        String viewId = (String) requestMap.get("jakarta.servlet.include.path_info");
        if (viewId == null) {
            viewId = facesContext.getExternalContext().getRequestPathInfo();
        }
        // path_info. Query the servlet path.
        if (viewId == null) {
            viewId = (String) requestMap.get("jakarta.servlet.include.servlet_path");
        }
        if (viewId == null) {
            viewId = facesContext.getExternalContext().getRequestServletPath();
        }
        if (viewId == null) {
            throw new FacesException(MessageUtils.getExceptionMessageString(NULL_REQUEST_VIEW_ERROR_MESSAGE_ID));
        }
        ViewHandler viewHandler = getViewHandler(facesContext);
        if (facesContext.isPostback() && !isErrorPage(facesContext)) {
            facesContext.setProcessingEvents(false);
            // try to restore the view
            viewRoot = viewHandler.restoreView(facesContext, viewId);
            if (viewRoot == null) {
                Object[] params = { viewId };
                throw new ViewExpiredException(getExceptionMessageString(RESTORE_VIEW_ERROR_MESSAGE_ID, params), viewId);
            }
            facesContext.setViewRoot(viewRoot);
            facesContext.setProcessingEvents(true);
            if (LOGGER.isLoggable(FINE)) {
                LOGGER.fine("Postback: restored view for " + viewId);
            }
        } else {
            if (LOGGER.isLoggable(FINE)) {
                LOGGER.fine("New request: creating a view for " + viewId);
            }
            String logicalViewId = viewHandler.deriveLogicalViewId(facesContext, viewId);
            ViewDeclarationLanguage vdl = viewHandler.getViewDeclarationLanguage(facesContext, logicalViewId);
            maybeTakeProtectedViewAction(facesContext, viewHandler, vdl, logicalViewId);
            ViewMetadata metadata = null;
            if (vdl != null) {
                // If we have one, get the ViewMetadata...
                metadata = vdl.getViewMetadata(facesContext, logicalViewId);
                if (metadata != null) {
                    // perhaps it's not supported
                    // and use it to create the ViewRoot. This will have, at most
                    // the UIViewRoot and its metadata facet.
                    viewRoot = metadata.createMetadataView(facesContext);
                    // Only skip to render response if there is no metadata
                    if (!hasMetadata(viewRoot)) {
                        facesContext.renderResponse();
                    }
                }
            }
            if (vdl == null || metadata == null) {
                facesContext.renderResponse();
            }
            if (viewRoot == null) {
                viewRoot = getViewHandler(facesContext).createView(facesContext, logicalViewId);
            }
            facesContext.setViewRoot(viewRoot);
        }
    } catch (Throwable fe) {
        if (fe instanceof FacesException) {
            thrownException = (FacesException) fe;
        } else {
            thrownException = new FacesException(fe);
        }
    } finally {
        if (thrownException == null) {
            FlowHandler flowHandler = facesContext.getApplication().getFlowHandler();
            if (flowHandler != null) {
                flowHandler.clientWindowTransition(facesContext);
            }
            deliverPostRestoreStateEvent(facesContext);
        } else {
            throw thrownException;
        }
    }
    LOGGER.fine("Exiting RestoreViewPhase");
}
Also used : ViewExpiredException(jakarta.faces.application.ViewExpiredException) Util.getViewHandler(com.sun.faces.util.Util.getViewHandler) ViewHandler(jakarta.faces.application.ViewHandler) MessageUtils.getExceptionMessageString(com.sun.faces.util.MessageUtils.getExceptionMessageString) ViewDeclarationLanguage(jakarta.faces.view.ViewDeclarationLanguage) FlowHandler(jakarta.faces.flow.FlowHandler) UIViewRoot(jakarta.faces.component.UIViewRoot) FacesException(jakarta.faces.FacesException) ViewMetadata(jakarta.faces.view.ViewMetadata)

Example 3 with ViewExpiredException

use of jakarta.faces.application.ViewExpiredException in project faces by jakartaee.

the class TestServlet method viewExpiredExceptionTest.

// ------------------------------------------------------------------- Tests
public void viewExpiredExceptionTest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    Throwable tckException = new TCKException();
    // ViewExpiredException(java.lang.String viewId)
    ViewExpiredException vOne = new ViewExpiredException("Geddy");
    if (this.checkViewId(vOne, "Geddy", out)) {
    // do nothing test passed.
    } else {
        return;
    }
    // ViewExpiredException(java.lang.String message,
    // java.lang.String viewId)
    ViewExpiredException vTwo = new ViewExpiredException("Vocals", "Geddy");
    if (this.checkViewId(vTwo, "Geddy", out) && this.checkMessage(vTwo, "Vocals", out)) {
    // do nothing test passed.
    } else {
        return;
    }
    // ViewExpiredException(java.lang.Throwable cause,
    // java.lang.String viewId)
    ViewExpiredException vThree = new ViewExpiredException(tckException, "Geddy");
    if (this.checkViewId(vThree, "Geddy", out) && this.checkCause(vThree, "TCKException", out)) {
    // do nothing test passed.
    } else {
        return;
    }
    // ViewExpiredException(java.lang.String message,
    // java.lang.Throwable cause,
    // java.lang.String viewId)
    ViewExpiredException vFour = new ViewExpiredException("Vocals", tckException, "Geddy");
    if (this.checkViewId(vFour, "Geddy", out) && this.checkMessage(vFour, "Vocals", out) && this.checkCause(vFour, "TCKException", out)) {
    // do nothing test passed.
    } else {
        return;
    }
    out.println(JSFTestUtil.PASS);
}
Also used : ViewExpiredException(jakarta.faces.application.ViewExpiredException) PrintWriter(java.io.PrintWriter)

Example 4 with ViewExpiredException

use of jakarta.faces.application.ViewExpiredException in project myfaces by apache.

the class StateUtils method decrypt.

public static byte[] decrypt(byte[] secure, ExternalContext externalContext) {
    Assert.notNull(externalContext, "externalContext");
    testConfiguration(externalContext);
    try {
        Mac mac = createMac(externalContext);
        Cipher cipher = createCipher(externalContext, Cipher.DECRYPT_MODE);
        // EtM (Encrypt-then-MAC) Composition Approach
        int macLenght = mac.getMacLength();
        mac.update(secure, 0, secure.length - macLenght);
        byte[] signedDigestHash = mac.doFinal();
        boolean isMacEqual = true;
        for (int i = 0; i < signedDigestHash.length; i++) {
            if (signedDigestHash[i] != secure[secure.length - macLenght + i]) {
                isMacEqual = false;
            // MYFACES-2934 Must compare *ALL* bytes of the hash,
            // otherwise a side-channel timing attack is theorically possible
            // but with a very very low probability, because the
            // comparison time is too small to be measured compared to
            // the overall request time and in real life applications,
            // there are too many uncertainties involved.
            // break;
            }
        }
        if (!isMacEqual) {
            throw new ViewExpiredException();
        }
        return cipher.doFinal(secure, 0, secure.length - macLenght);
    } catch (Exception e) {
        throw new FacesException(e);
    }
}
Also used : ViewExpiredException(jakarta.faces.application.ViewExpiredException) Cipher(javax.crypto.Cipher) Mac(javax.crypto.Mac) IOException(java.io.IOException) FacesException(jakarta.faces.FacesException) ViewExpiredException(jakarta.faces.application.ViewExpiredException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) FacesException(jakarta.faces.FacesException)

Example 5 with ViewExpiredException

use of jakarta.faces.application.ViewExpiredException in project myfaces by apache.

the class RestoreViewExecutor method execute.

@Override
public boolean execute(FacesContext facesContext) {
    if (facesContext == null) {
        throw new FacesException("FacesContext is null");
    }
    // init the View
    Application application = facesContext.getApplication();
    ViewHandler viewHandler = application.getViewHandler();
    if (JsfVersion.supports12()) {
        viewHandler.initView(facesContext);
    } else {
    // nothing to do
    }
    UIViewRoot viewRoot = facesContext.getViewRoot();
    RestoreViewSupport restoreViewSupport = getRestoreViewSupport();
    if (viewRoot != null) {
        if (log.isLoggable(Level.FINEST)) {
            log.log(Level.FINEST, "View already exists in the FacesContext");
        }
        viewRoot.setLocale(facesContext.getExternalContext().getRequestLocale());
        restoreViewSupport.processComponentBinding(facesContext, viewRoot);
        return false;
    }
    String viewId = restoreViewSupport.calculateViewId(facesContext);
    // Determine if this request is a postback or initial request
    if (restoreViewSupport.isPostback(facesContext)) {
        if (log.isLoggable(Level.FINEST)) {
            log.log(Level.FINEST, "Request is a postback");
        }
        viewRoot = viewHandler.restoreView(facesContext, viewId);
        if (viewRoot == null) {
            if (JsfVersion.supports12()) {
                throw new ViewExpiredException("The expected view was not returned for the view identifier: " + viewId, viewId);
            } else {
                throw new RuntimeException("The expected view was not returned for the view identifier: " + viewId);
            }
        }
        restoreViewSupport.processComponentBinding(facesContext, viewRoot);
    } else {
        if (log.isLoggable(Level.FINEST)) {
            log.log(Level.FINEST, "Request is not a postback. New UIViewRoot will be created");
        }
        viewRoot = viewHandler.createView(facesContext, viewId);
        facesContext.renderResponse();
    }
    facesContext.setViewRoot(viewRoot);
    return false;
}
Also used : ViewExpiredException(jakarta.faces.application.ViewExpiredException) ViewHandler(jakarta.faces.application.ViewHandler) Application(jakarta.faces.application.Application) UIViewRoot(jakarta.faces.component.UIViewRoot) FacesException(jakarta.faces.FacesException)

Aggregations

ViewExpiredException (jakarta.faces.application.ViewExpiredException)6 FacesException (jakarta.faces.FacesException)5 ViewHandler (jakarta.faces.application.ViewHandler)4 UIViewRoot (jakarta.faces.component.UIViewRoot)4 ViewDeclarationLanguage (jakarta.faces.view.ViewDeclarationLanguage)3 Application (jakarta.faces.application.Application)2 FlowHandler (jakarta.faces.flow.FlowHandler)2 ViewMetadata (jakarta.faces.view.ViewMetadata)2 IOException (java.io.IOException)2 MessageUtils.getExceptionMessageString (com.sun.faces.util.MessageUtils.getExceptionMessageString)1 Util.getViewHandler (com.sun.faces.util.Util.getViewHandler)1 FacesMessage (jakarta.faces.application.FacesMessage)1 NavigationHandler (jakarta.faces.application.NavigationHandler)1 FacesContext (jakarta.faces.context.FacesContext)1 AbortProcessingException (jakarta.faces.event.AbortProcessingException)1 ExceptionQueuedEvent (jakarta.faces.event.ExceptionQueuedEvent)1 ExceptionQueuedEventContext (jakarta.faces.event.ExceptionQueuedEventContext)1 ClientWindow (jakarta.faces.lifecycle.ClientWindow)1 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)1 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)1