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.shiro.extension.authz.aop;
020    
021    import org.apache.shiro.authz.aop.AuthorizingAnnotationHandler;
022    
023    import java.lang.annotation.Annotation;
024    
025    
026    /**
027     * Generic interceptor for use in different aop implementations.
028     * Created based on <b>method</b> annotation.
029     * <p/>
030     * To create the interceptor based on the class annotation, use
031     * {@link org.tynamo.shiro.extension.authz.annotations.utils.AnnotationFactory}
032     * for convert class annotation to method annotation.
033     *
034     */
035    public class DefaultSecurityInterceptor implements SecurityInterceptor
036    {
037    
038            private final AuthorizingAnnotationHandler handler;
039            private final Annotation annotation;
040    
041    
042            /**
043             * Used in cases where previously known {@link org.apache.shiro.authz.aop.AuthorizingAnnotationHandler} object.
044             * <p/>
045             * if the handler object is unknown use {@link #DefaultSecurityInterceptor(Annotation)} constructor
046             *
047             * @param handler
048             * @param annotation
049             */
050            public DefaultSecurityInterceptor(AuthorizingAnnotationHandler handler, Annotation annotation)
051            {
052                    this.handler = handler;
053                    this.annotation = annotation;
054            }
055    
056            /**
057             * Initialize {@link #handler} field use annotation.
058             *
059             * @param annotation annotation for create handler and use during
060             *                   {@link #intercept()} invocation.
061             */
062            public DefaultSecurityInterceptor(Annotation annotation)
063            {
064    
065                    this.annotation = annotation;
066                    AuthorizingAnnotationHandler handler = AopHelper.createHandler(annotation);
067                    if (handler == null)
068                    {
069                            throw new IllegalStateException("No handler for " + annotation + "annotation");
070                    }
071                    this.handler = handler;
072    
073            }
074    
075            /* (non-Javadoc)
076                     * @see org.tynamo.shiro.extension.authz.aop.SecurityInterceptor#intercept()
077                     */
078    
079            public void intercept()
080            {
081                    handler.assertAuthorized(getAnnotation());
082            }
083    
084            public Annotation getAnnotation()
085            {
086                    return annotation;
087            }
088    
089    
090    }