A user on OTN forum was trying to extend the AdfcExceptionHandler in which he was trying to do redirect to an error page and facing Response Already Committed exception. On examining the scenario i could see that the phase in which the exception handler was being called was RENDER_RESPONSE and by this time it is too late to do a redirect as part of the response has already been submitted (one can check this by using response.iscommitted() method). So redirects/forwards should never be issued from the AdfcExceptionHandler in a conventional way.
The question also arises why someone needs to extend the AdfcExceptionHandler in the first place, but, we need to do this as default exception handling will not handle exceptions that occur during render response phase for example: let’s say that during the application execution the connectivity to the DB goes down, the exceptions raised then will not be handled by the default exception handling mechanism.
So we cannot use conventional redirects but we have to do redirects somehow, the solution then is to use javascript redirect from the exception handler. The code for the exception handler is shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | package com.blogspot.ramannanda.view.handlers; import java.sql.SQLException; import java.util.HashMap; import java.util.Map; import javax.faces.context.ExternalContext; import javax.faces.context.FacesContext; import javax.faces.event.PhaseId; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import oracle.adf.view.rich.context.ExceptionHandler; import oracle.adfinternal.controller.application.AdfcExceptionHandler; import oracle.jbo.DMLException; import org.apache.myfaces.trinidad.render.ExtendedRenderKitService; import org.apache.myfaces.trinidad.util.Service; public class MyAppUIErrorHandler extends AdfcExceptionHandler { public MyAppUIErrorHandler() { super(); } @Override public void handleException(FacesContext facesContext, Throwable throwable, PhaseId phaseId) throws Throwable { ExternalContext ctx = FacesContext.getCurrentInstance().getExternalContext(); HttpServletRequest request = (HttpServletRequest)ctx.getRequest(); if (phaseId.getOrdinal() == PhaseId.RENDER_RESPONSE.getOrdinal()) { //handle null pointer or sqlexception or runtime DMLException that might occur if (throwable instanceof DMLException || throwable instanceof SQLException || throwable instanceof NullPointerException) { String contextPath = request.getContextPath(); FacesContext context = FacesContext.getCurrentInstance(); StringBuffer script = new StringBuffer(); //set the window.location.href property this causes the redirect. script.append("window.location.href= '").append(contextPath).append("/error.html';"); ExtendedRenderKitService erks = Service.getRenderKitService(context, ExtendedRenderKitService.class); erks.addScript(context, script.toString()); //encoding script is required as just adding script does not work here erks.encodeScripts(context); return; } else { super.handleException(facesContext, throwable, phaseId); } } } } |
So by using this code we are doing redirect to error.html page, I am also checking for phaseId here as the handler will be called for each phase in turn and we can do the handling in render response phase.
Note: Note that i have used encodeScripts method after adding the script and this is required because this method outputs any required scripts by RenderKit which in our case tends to be the script we added.