001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    package org.tynamo.security;
020    
021    import org.apache.tapestry5.model.MutableComponentModel;
022    import org.apache.tapestry5.services.*;
023    import org.tynamo.shiro.extension.authz.aop.AopHelper;
024    import org.tynamo.shiro.extension.authz.aop.DefaultSecurityInterceptor;
025    import org.tynamo.shiro.extension.authz.aop.SecurityInterceptor;
026    
027    import java.lang.annotation.Annotation;
028    import java.util.List;
029    
030    
031    /**
032     * Transform components based on annotation.
033     * <p/>
034     * Support annotation on method.
035     * <p/>
036     * The following rules
037     * <ul>
038     * <li>Annotations on methods are <b>not</b> inherited.</li>
039     * <li>The annotations only in target class, unlike services </li>
040     * <ul>
041     * <p/>
042     *
043     * @see org.tynamo.security.services.SecurityModule#buildSecurityFilter(org.slf4j.Logger,
044     *      org.apache.tapestry5.services.ComponentEventLinkEncoder,
045     *      org.apache.tapestry5.services.ComponentClassResolver,
046     *      org.tynamo.security.services.ClassInterceptorsCache)
047     */
048    public class ShiroAnnotationWorker implements ComponentClassTransformWorker
049    {
050    
051            @Override
052            public void transform(ClassTransformation transformation,
053                                  MutableComponentModel model)
054            {
055    
056                    for (Class<? extends Annotation> annotationClass : AopHelper.getAutorizationAnnotationClasses())
057                    {
058    
059                            List<TransformMethodSignature> methodsToTransform =
060                                            transformation.findMethodsWithAnnotation(annotationClass);
061    
062                            for (TransformMethodSignature tm : methodsToTransform)
063                            {
064                                    Annotation annotation = transformation.getMethodAnnotation(tm, annotationClass);
065                                    processTransform(transformation, tm, annotation);
066                            }
067                    }
068            }
069    
070            private void processTransform(ClassTransformation transformation,
071                                          TransformMethodSignature tm, Annotation annotation)
072            {
073                    final SecurityInterceptor interceptor = new DefaultSecurityInterceptor(annotation);
074    
075                    ComponentMethodAdvice advice = new ComponentMethodAdvice()
076                    {
077                            public void advise(ComponentMethodInvocation invocation)
078                            {
079                                    interceptor.intercept();
080                                    invocation.proceed();
081                            }
082                    };
083    
084                    transformation.advise(tm, advice);
085    
086            }
087    
088    }