<!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;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>I defined an application exception and an Intercepter in server side:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </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 {    </p><p>    private static final long serialVersionUID = 1L;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>    public static final NotExistingSessionTokenException INSTANCE;</p><p>    static {</p><p>        INSTANCE = new NotExistingSessionTokenException();</p><p>    }</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>    private static final String MSG = "Fake Session Exception";</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>    private NotExistingSessionTokenException() {</p><p>        super(MSG);</p><p>    }</p><p>}</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>public class SessionTokenInterceptor {</p><p>    Logger log = Logger.getLogger(SessionTokenInterceptor.class);</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>    @PersistenceContext</p><p>    protected EntityManager em;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>    @Resource</p><p>    private EJBContext ejbContext;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>    @AroundInvoke</p><p>    public Object processSessionToken(final InvocationContext invocationContext) throws Exception{</p><p>        //1. session token</p><p>        //log.info("retrieve SESSION TOKEN from invocation context instead of ejbContext");</p><p>        String sessionToken = (String)invocationContext.getContextData().get(ServerClientSharedConstants.SESSION_TOKEN_KEY);</p><p>        //log.debug("SESSION_TOKEN:"+sessionToken);//good!</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>        //2. JAAS username</p><p>        //log.info("retrieve JAAS username");</p><p>        String username = ejbContext.getCallerPrincipal().getName();//JAAS username</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>        //3. check</p><p>        if(sessionToken==null){</p><p>            log.error("User["+username+"] has no SESSION TOKEN, so is NOT allowed to do more in this system.");</p><p>            throw NullSessionTokenException.INSTANCE;</p><p>        }</p><p>        if(! isSessionTokenExisting(sessionToken)){</p><p>            log.error("User["+username+"] is using invalid SESSION TOKEN, so is NOT allowed to do more in this system.");</p><p>            throw <span style="color: #0000ff;"><strong>NotExistingSessionTokenException.INSTANCE;</strong></span></p><p>        }</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>        //4. call original method and return its result so that other interceptor can continue</p><p>        try{</p><p>             return invocationContext.proceed();</p><p>        }finally{</p><p>        }</p><p>    }</p><p>}</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </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;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </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;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>import java.lang.reflect.UndeclaredThrowableException;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </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;"> </p><p>import com.ybxiang.nms.common.exception.NotExistingSessionTokenException;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>public class ClientExceptionInterceptor implements EJBClientInterceptor{</p><p>    private final static Logger log = Logger.getLogger(ClientExceptionInterceptor.class);</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>    @Override</p><p>    public void handleInvocation(EJBClientInvocationContext context)</p><p>            throws Exception {</p><p>        context.sendRequest();</p><p>    }</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>    @Override</p><p>    public Object handleInvocationResult(EJBClientInvocationContext context)</p><p>            throws Exception {</p><p>        String methodName = context.getInvokedMethod().getName();</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>        for(int i=0;i<ignoredMehtods_handleInvocationResult.length;i++){</p><p>            if(methodName.equals(ignoredMehtods_handleInvocationResult[i])){</p><p>                return context.getResult();</p><p>            }</p><p>        }</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>        Object result = null;</p><p>        try{</p><p>            result = context.getResult();</p><p>        }catch(<span style="color: #0000ff;"><strong>NotExistingSessionTokenException </strong></span>e){</p><p>           <span style="color: #0000ff;"><strong> log.error("NotExistingSessionTokenException is NOT catched!");//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ What I want!</strong></span></p><p>        <span style="color: #ff0000;"><strong>}catch(javax.ejb.EJBException e){        </strong></span>    </p><p>            if(e.getCause() instanceof java.lang.reflect.UndeclaredThrowableException){</p><p>                UndeclaredThrowableException cause = (UndeclaredThrowableException)e.getCause();</p><p>                Throwable t = cause.getUndeclaredThrowable();</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>                if(t instanceof <span style="color: #0000ff;"><strong>NotExistingSessionTokenException</strong></span>){</p><p>                    <span style="color: #339966;"><strong>log.error("NotExistingSessionTokenException is catched!");//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ What I NOT want!</strong></span></p><p>                }else{</p><p>                    log.error("bla bla...);</p><p>                }</p><p>            }</p><p>            else{</p><p>            }</p><p>            throw e;</p><p>        }catch(Exception e){</p><p>            log.error("UNknown Exceptoin:",e);</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>            throw e;</p><p>        }finally{</p><p>        }        </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>        return result;</p><p>    }</p><p>}</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </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;"> </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>