org.tynamo.security.ShiroAnnotationWorker Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tapestry-security Show documentation
Show all versions of tapestry-security Show documentation
Tynamo Security package for Tapestry 5 based on Shiro Security
package org.tynamo.security;
import java.lang.annotation.Annotation;
import java.util.List;
import org.apache.tapestry5.model.MutableComponentModel;
import org.apache.tapestry5.plastic.MethodAdvice;
import org.apache.tapestry5.plastic.MethodInvocation;
import org.apache.tapestry5.plastic.PlasticClass;
import org.apache.tapestry5.plastic.PlasticMethod;
import org.apache.tapestry5.services.Environment;
import org.apache.tapestry5.services.transform.ComponentClassTransformWorker2;
import org.apache.tapestry5.services.transform.TransformationSupport;
import org.tynamo.shiro.extension.authz.aop.AopHelper;
import org.tynamo.shiro.extension.authz.aop.DefaultSecurityInterceptor;
import org.tynamo.shiro.extension.authz.aop.SecurityInterceptor;
/**
* Transform components based on annotation.
*
* Support annotation on method.
*
* The following rules
*
* - Annotations on methods are not inherited.
* - The annotations only in target class, unlike services
*
*
*
* @see org.tynamo.security.services.SecurityModule#buildSecurityFilter(org.slf4j.Logger,
* org.apache.tapestry5.services.ComponentEventLinkEncoder,
* org.apache.tapestry5.services.ComponentClassResolver,
* org.tynamo.security.services.ClassInterceptorsCache)
*/
public class ShiroAnnotationWorker implements ComponentClassTransformWorker2
{
private final Environment environment;
public ShiroAnnotationWorker(Environment environment)
{
this.environment = environment;
}
@Override
public void transform(PlasticClass plasticClass, TransformationSupport support, MutableComponentModel model)
{
for (Class extends Annotation> annotationClass : AopHelper.getAutorizationAnnotationClasses())
{
List methodsToTransform = plasticClass.getMethodsWithAnnotation(annotationClass);
for (PlasticMethod tm : methodsToTransform) processTransform(tm, tm.getAnnotation(annotationClass));
}
}
private void processTransform(PlasticMethod tm, Annotation annotation)
{
final SecurityInterceptor interceptor = new DefaultSecurityInterceptor(annotation);
MethodAdvice advice = new MethodAdvice()
{
public void advise(MethodInvocation invocation)
{
environment.push(MethodInvocation.class, invocation);
try
{
interceptor.intercept();
}
finally
{
environment.pop(MethodInvocation.class);
}
invocation.proceed();
}
};
tm.addAdvice(advice);
}
}