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, MutableComponentModel model)
053            {
054                    for (Class<? extends Annotation> annotationClass : AopHelper.getAutorizationAnnotationClasses())
055                    {
056                            List<TransformMethod> methodsToTransform = transformation.matchMethodsWithAnnotation(annotationClass);
057                            
058                            for (TransformMethod tm : methodsToTransform) processTransform(tm, tm.getAnnotation(annotationClass));
059                    }
060            }
061    
062            private void processTransform(TransformMethod tm, Annotation annotation)
063            {
064                    final SecurityInterceptor interceptor = new DefaultSecurityInterceptor(annotation);
065    
066                    ComponentMethodAdvice advice = new ComponentMethodAdvice()
067                    {
068                            public void advise(ComponentMethodInvocation invocation)
069                            {
070                                    interceptor.intercept();
071                                    invocation.proceed();
072                            }
073                    };
074    
075                    tm.addAdvice(advice);
076    
077            }
078    
079    }