Example usage for org.apache.shiro.spring.remoting SecureRemoteInvocationFactory HOST_KEY

List of usage examples for org.apache.shiro.spring.remoting SecureRemoteInvocationFactory HOST_KEY

Introduction

In this page you can find the example usage for org.apache.shiro.spring.remoting SecureRemoteInvocationFactory HOST_KEY.

Prototype

String HOST_KEY

To view the source code for org.apache.shiro.spring.remoting SecureRemoteInvocationFactory HOST_KEY.

Click Source Link

Usage

From source file:info.novatec.inspectit.cmr.spring.exporter.SessionAwareSecureRemoteInvocationExecutor.java

License:Apache License

/**
 * {@inheritDoc}//from   w  ww  . j a v  a  2 s. co m
 */
@Override
public Object invoke(final RemoteInvocation invocation, final Object targetObject)
        throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    try {
        Subject.Builder builder = new Subject.Builder(securityManager);
        String host = (String) invocation.getAttribute(SecureRemoteInvocationFactory.HOST_KEY);
        if (host != null) {
            builder.host(host);
        }
        Serializable sessionId = invocation.getAttribute(SecureRemoteInvocationFactory.SESSION_ID_KEY);
        if (sessionId != null) {
            builder.sessionId(sessionId);
        }

        Subject subject = builder.buildSubject();

        return subject.execute(new Callable<Object>() {
            public Object call() throws Exception {
                // This is the part which is significantly modified to set the session id thread local manually.
                Object result = SessionAwareSecureRemoteInvocationExecutor.super.invoke(invocation,
                        targetObject);

                Object sessionId = null;
                Session session = SecurityUtils.getSubject().getSession(false);
                if (null != session) {
                    sessionId = session.getId();
                }
                sessionIdThreadLocal.set(sessionId);

                return result;
            }
        });
    } catch (ExecutionException e) {
        Throwable cause = e.getCause();
        if ((cause instanceof NoSuchMethodException)) {
            throw ((NoSuchMethodException) cause); // NOPMD
        }
        if ((cause instanceof IllegalAccessException)) {
            throw ((IllegalAccessException) cause); // NOPMD
        }
        if ((cause instanceof InvocationTargetException)) {
            throw ((InvocationTargetException) cause); // NOPMD
        }
        throw new InvocationTargetException(cause); // NOPMD
    } catch (Throwable t) { // NOPMD
        throw new InvocationTargetException(t);
    }
}