<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body link="#355491" alink="#4262a1" vlink="#355491" style="background: #e2e2e2; margin: 0; padding: 20px;">

<div>
        <table cellpadding="0" bgcolor="#FFFFFF" border="0" cellspacing="0" style="border: 1px solid #dadada; margin-bottom: 30px; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                <tbody>
                        <tr>

                                <td>

                                        <table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border: solid 2px #ccc; background: #dadada; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                                                <tbody>
                                                        <tr>
                                                                <td bgcolor="#000000" valign="middle" height="58px" style="border-bottom: 1px solid #ccc; padding: 20px; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-top-right-radius: 5px; -webkit-border-top-left-radius: 5px;">
                                                                        <h1 style="color: #333333; font: bold 22px Arial, Helvetica, sans-serif; margin: 0; display: block !important;">
                                                                        <!-- To have a header image/logo replace the name below with your img tag -->
                                                                        <!-- Email clients will render the images when the message is read so any image -->
                                                                        <!-- must be made available on a public server, so that all recipients can load the image. -->
                                                                        <a href="https://community.jboss.org/index.jspa" style="text-decoration: none; color: #E1E1E1">JBoss Community</a></h1>
                                                                </td>

                                                        </tr>
                                                        <tr>
                                                                <td bgcolor="#FFFFFF" style="font: normal 12px Arial, Helvetica, sans-serif; color:#333333; padding: 20px;  -moz-border-radius-bottomleft: 4px; -moz-border-radius-bottomright: 4px; -webkit-border-bottom-right-radius: 5px; -webkit-border-bottom-left-radius: 5px;"><h3 style="margin: 10px 0 5px; font-size: 17px; font-weight: normal;">
    Why ApplicationException thrown from server-side interceptor is wrapped by JBoss AS/EJB container?
</h3>
<span style="margin-bottom: 10px;">
    created by <a href="https://community.jboss.org/people/ybxiang.china">xiang yingbing</a> in <i>EJB3</i> - <a href="https://community.jboss.org/message/766555#766555">View the full discussion</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">

<div class="jive-rendered-content"><p>Dear guys,</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>I defined an application exception and an Intercepter in server side:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>import javax.ejb.ApplicationException;</p><p>@ApplicationException(rollback=true)</p><p>public class <span style="color: #0000ff;"><strong>NotExistingSessionTokenException </strong></span>extends Exception {&#160;&#160;&#160; </p><p>&#160;&#160;&#160; private static final long serialVersionUID = 1L;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&#160;&#160;&#160; public static final NotExistingSessionTokenException INSTANCE;</p><p>&#160;&#160;&#160; static {</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; INSTANCE = new NotExistingSessionTokenException();</p><p>&#160;&#160;&#160; }</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&#160;&#160;&#160; private static final String MSG = "Fake Session Exception";</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&#160;&#160;&#160; private NotExistingSessionTokenException() {</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; super(MSG);</p><p>&#160;&#160;&#160; }</p><p>}</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>public class SessionTokenInterceptor {</p><p>&#160;&#160;&#160; Logger log = Logger.getLogger(SessionTokenInterceptor.class);</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&#160;&#160;&#160; @PersistenceContext</p><p>&#160;&#160;&#160; protected EntityManager em;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&#160;&#160;&#160; @Resource</p><p>&#160;&#160;&#160; private EJBContext ejbContext;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&#160;&#160;&#160; @AroundInvoke</p><p>&#160;&#160;&#160; public Object processSessionToken(final InvocationContext invocationContext) throws Exception{</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; //1. session token</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; //log.info("retrieve SESSION TOKEN from invocation context instead of ejbContext");</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; String sessionToken = (String)invocationContext.getContextData().get(ServerClientSharedConstants.SESSION_TOKEN_KEY);</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; //log.debug("SESSION_TOKEN:"+sessionToken);//good!</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; //2. JAAS username</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; //log.info("retrieve JAAS username");</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; String username = ejbContext.getCallerPrincipal().getName();//JAAS username</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; //3. check</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; if(sessionToken==null){</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; log.error("User["+username+"] has no SESSION TOKEN, so is NOT allowed to do more in this system.");</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; throw NullSessionTokenException.INSTANCE;</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; if(! isSessionTokenExisting(sessionToken)){</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; log.error("User["+username+"] is using invalid SESSION TOKEN, so is NOT allowed to do more in this system.");</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; throw <span style="color: #0000ff;"><strong>NotExistingSessionTokenException.INSTANCE;</strong></span></p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; //4. call original method and return its result so that other interceptor can continue</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; try{</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; return invocationContext.proceed();</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }finally{</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p>&#160;&#160;&#160; }</p><p>}</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>I used the interceptor on my session bean:</p><p>@Interceptors({<span style="color: #0000ff;"><strong>SessionTokenInterceptor</strong></span>.class})</p><p>public class SecuredRemoteSession implements ISecuredRemoteSession{</p><p>...</p><p>}</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>On client side, I catch this exception in an interceptor, but I got a wrapped Exception: javax.ejb.EJBException</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>import java.lang.reflect.UndeclaredThrowableException;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>import org.apache.log4j.Logger;</p><p>import org.eclipse.swt.widgets.Display;</p><p>import org.jboss.ejb.client.EJBClientInterceptor;</p><p>import org.jboss.ejb.client.EJBClientInvocationContext;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>import com.ybxiang.nms.common.exception.NotExistingSessionTokenException;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>public class ClientExceptionInterceptor implements EJBClientInterceptor{</p><p>&#160;&#160;&#160; private final static Logger log = Logger.getLogger(ClientExceptionInterceptor.class);</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&#160;&#160;&#160; @Override</p><p>&#160;&#160;&#160; public void handleInvocation(EJBClientInvocationContext context)</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; throws Exception {</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; context.sendRequest();</p><p>&#160;&#160;&#160; }</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&#160;&#160;&#160; @Override</p><p>&#160;&#160;&#160; public Object handleInvocationResult(EJBClientInvocationContext context)</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; throws Exception {</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; String methodName = context.getInvokedMethod().getName();</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; for(int i=0;i&lt;ignoredMehtods_handleInvocationResult.length;i++){</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if(methodName.equals(ignoredMehtods_handleInvocationResult[i])){</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; return context.getResult();</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; Object result = null;</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; try{</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; result = context.getResult();</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }catch(<span style="color: #0000ff;"><strong>NotExistingSessionTokenException </strong></span>e){</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0000ff;"><strong> log.error("NotExistingSessionTokenException is NOT catched!");//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ What I want!</strong></span></p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #ff0000;"><strong>}catch(javax.ejb.EJBException e){&#160;&#160;&#160;&#160;&#160;&#160;&#160; </strong></span>&#160;&#160;&#160; </p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if(e.getCause() instanceof java.lang.reflect.UndeclaredThrowableException){</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; UndeclaredThrowableException cause = (UndeclaredThrowableException)e.getCause();</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Throwable t = cause.getUndeclaredThrowable();</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if(t instanceof <span style="color: #0000ff;"><strong>NotExistingSessionTokenException</strong></span>){</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #339966;"><strong>log.error("NotExistingSessionTokenException is catched!");//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ What I NOT want!</strong></span></p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }else{</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; log.error("bla bla...);</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; else{</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; throw e;</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }catch(Exception e){</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; log.error("UNknown Exceptoin:",e);</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; throw e;</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }finally{</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }&#160;&#160;&#160;&#160;&#160;&#160;&#160; </p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; return result;</p><p>&#160;&#160;&#160; }</p><p>}</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Why Application Exception thrown in server-side interceptor is wrapped by EJB container?</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Thanks in advance.</p></div>

<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
    <p style="margin: 0;">Reply to this message by <a href="https://community.jboss.org/message/766555#766555">going to Community</a></p>
        <p style="margin: 0;">Start a new discussion in EJB3 at <a href="https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2029">Community</a></p>
</div></td>
                        </tr>
                    </tbody>
                </table>


                </td>
            </tr>
        </tbody>
    </table>

</div>

</body>
</html>