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();
}
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");
}
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);
}
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);
}
}
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;
}
Aggregations