Solder Exception Handling does not work
ceefour Apr 15, 2012 6:16 PMI fire the exception by handling a @Before @RenderResponse JSF phase event :
public class FacesSecurity {
private transient Logger log = LoggerFactory.getLogger(FacesSecurity.class);
@Inject Identity identity;
@Inject Event<NotLoggedInException> notLoggedIn;
public void checkPermissions(@Observes @Before @RenderResponse PhaseEvent event) {
FacesContext faces = event.getFacesContext();
HttpServletRequest request = (HttpServletRequest) faces.getExternalContext().getRequest();
String viewId = faces.getViewRoot().getViewId();
log.debug("Checking permissions for {}:{} on {}", new Object[] {
request.getRemoteAddr(), request.getRemotePort(), viewId });
if ("/pages/article/post.xhtml".equalsIgnoreCase(viewId)) {
if (!identity.isLoggedIn()) {
log.debug("{}:{} is not logged in on {}", new Object[] {
request.getRemoteAddr(), request.getRemotePort(), viewId });
notLoggedIn.fire(new NotLoggedInException());
}
}
}
}
I tried simply "throw new NotLoggedInException()" however Solder Exception doesn't catch it at all, so at least with CDI Events I can still catch it using @Observes as a workaround (which works wonderfully, BTW, even if not semantically clean) :
public void handleNotLoggedIn(@Observes NotLoggedInException evt) {
ExternalContext external = faces.getExternalContext();
HttpServletRequest request = (HttpServletRequest) external.getRequest();
String viewId = faces.getViewRoot().getViewId();
log.debug("Redirecting {}:{} request {} to login page", new Object[] {
request.getRemoteAddr(), request.getRemotePort(), viewId });
faces.addMessage(null, new FacesMessage("Silakan login untuk melanjutkan."));
try {
external.redirect(external.encodeRedirectURL("login", null));
} catch (IOException e) {
throw new RuntimeException("Error during handling NotLoggedInException", e);
}
}
I tried handling using Solder Exception, but doesn't work at all:
@HandlesExceptions
public class NotLoggedInHandler {
private transient Logger log = LoggerFactory.getLogger(NotLoggedInHandler.class);
@Inject Messages messages;
@Inject FacesContext faces;
public void handleNotLoggedIn(@Handles @WebRequest CaughtException<NotLoggedInException> evt) {
messages.warn("Silakan login untuk melanjutkan.");
ExternalContext external = faces.getExternalContext();
try {
external.redirect(external.encodeRedirectURL("login", null));
evt.handled();
} catch (IOException e) {
throw new RuntimeException("Error during handling NotLoggedInException", e);
}
}
}
Despite this "happy" message:
04:49:43,031 INFO [org.jboss.solder.exception.control.extension] (MSC service thread 1-4) Adding handler Qualifiers: [@org.jboss.solder.servlet.WebRequest()] TraversalMode: DEPTH_FIRST Handles Type: class org.jboss.seam.security.NotLoggedInException Precedence: 0 [method] public org.soluvas.security.faces.NotLoggedInHandler.handleNotLoggedIn(CaughtException<NotLoggedInException>) to known handlers
Any suggestions ?