web-servicesaopinterceptorhessian

Intercept Hessian remote call to add information


I suppose this might not be possible but just in case...

We have a frontend Java application that communicates with a (ours as well) backend application through Hessian. For logging and auditing purposes we need to pass the user_id of the User that is logged in (in the frontend) to the backend. An obvious solution would of course be to add a String parameter to all method calls but that's a lot of work and not very clean.

Would there be a way to intercept the call, decorate it with the user_id (retrieve from the Session) and on the backend to get that user_id (and then set it on ThreadLocal or such)?


Solution

  • It was possible:

    public class CustomHessionProxyFactoryBean extends HessianProxyFactoryBean {
    
    public CustomHessionProxyFactoryBean() {
        setProxyFactory(new CustomHessianProxyFactory());
    }
    
    private static class CustomHessianProxyFactory extends HessianProxyFactory {
    
        @Override
        public Object create(Class<?> api, URL url, ClassLoader loader) {
            return Proxy.newProxyInstance(loader, new Class[] { api, HessianRemoteObject.class }, new CustomHessianProxy(url, this, api));
        }
    
        private static class CustomHessianProxy extends HessianProxy {
    
            protected CustomHessianProxy(URL url, HessianProxyFactory factory) {
                super(url, factory);
            }
    
            public CustomHessianProxy(URL url, HessianProxyFactory factory, Class<?> type) {
                super(url, factory, type);
            }
    
            @Override
            protected void addRequestHeaders(HessianConnection conn) {
                super.addRequestHeaders(conn);
    
                if(StringUtils.isNotBlank(SsoIdUtil.getThreadSsoId())){
                    conn.addHeader("some_key", "some_value_from_threadlocal");
                }
            }
        }
    }
    

    }