Coverage Report - nu.localhost.tapestry.acegi.services.internal.AcegiWorker
 
Classes in this File Line Coverage Branch Coverage Complexity
AcegiWorker
0%
0/67
0%
0/10
0
 
 1  
 /*
 2  
  * Copyright 2007 Ivan Dubrov
 3  
  * Copyright 2007, 2008 Robin Helgelin
 4  
  *
 5  
  * Licensed under the Apache License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  *
 9  
  *     http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 
 18  
 package nu.localhost.tapestry.acegi.services.internal;
 19  
 
 20  
 import java.lang.reflect.Modifier;
 21  
 
 22  
 import org.acegisecurity.ConfigAttributeDefinition;
 23  
 import org.acegisecurity.SecurityConfig;
 24  
 import org.acegisecurity.annotation.Secured;
 25  
 import org.apache.tapestry.model.MutableComponentModel;
 26  
 import org.apache.tapestry.services.ClassTransformation;
 27  
 import org.apache.tapestry.services.ComponentClassTransformWorker;
 28  
 import org.apache.tapestry.services.TransformConstants;
 29  
 import org.apache.tapestry.services.TransformMethodSignature;
 30  
 
 31  
 /**
 32  
  * @author Ivan Dubrov
 33  
  */
 34  
 public class AcegiWorker implements ComponentClassTransformWorker {
 35  
     private SecurityChecker securityChecker;
 36  
 
 37  0
     public AcegiWorker(final SecurityChecker securityChecker) {
 38  0
         this.securityChecker = securityChecker;
 39  0
     }
 40  
 
 41  
     public final void transform(final ClassTransformation transformation,
 42  
             final MutableComponentModel model) {
 43  
         // Secure methods
 44  0
         for (TransformMethodSignature method : transformation
 45  0
                 .findMethodsWithAnnotation(Secured.class)) {
 46  0
             transformMethod(transformation, method);
 47  0
         }
 48  
 
 49  
         // Secure pages
 50  0
         Secured annotation = transformation.getAnnotation(Secured.class);
 51  0
         if (annotation != null) {
 52  0
             transformPage(transformation, annotation);
 53  
         }
 54  0
     }
 55  
 
 56  
     private void transformPage(final ClassTransformation transformation,
 57  
             final Secured annotation) {
 58  
         // Security checker
 59  0
         final String interField = transformation.addInjectedField(
 60  0
                 SecurityChecker.class, "_$checker", securityChecker);
 61  
 
 62  
         // Attribute definition
 63  0
         final String configField = createConfigAttributeDefinitionField(
 64  0
                 transformation, annotation);
 65  
 
 66  
         // Interceptor token
 67  0
         final String tokenField = transformation.addField(Modifier.PRIVATE,
 68  0
                 "org.acegisecurity.intercept.InterceptorStatusToken",
 69  0
                 "_$token");
 70  
 
 71  
         // Extend class
 72  0
         transformation.extendMethod(
 73  0
                 TransformConstants.BEGIN_RENDER_SIGNATURE,
 74  0
                 tokenField + " = " + interField + ".checkBefore(" + configField
 75  0
                 + ");");
 76  0
         transformation.extendMethod(
 77  0
                 TransformConstants.CLEANUP_RENDER_SIGNATURE,
 78  0
                 interField + ".checkAfter(" + tokenField + ", null);");
 79  
 
 80  0
     }
 81  
 
 82  
     private void transformMethod(final ClassTransformation transformation,
 83  
             final TransformMethodSignature method) {
 84  
         // Security checker
 85  0
         final String interField = transformation.addInjectedField(SecurityChecker.class, "_$checker", securityChecker);
 86  0
         // Interceptor status token
 87  0
         final String statusToken = transformation.addField(Modifier.PRIVATE,
 88  0
                 "org.acegisecurity.intercept.InterceptorStatusToken", "_$token");
 89  0
 
 90  0
         // Attribute definition
 91  0
         final Secured annotation = transformation.getMethodAnnotation(method, Secured.class);
 92  0
         final String configField = createConfigAttributeDefinitionField(transformation, annotation);
 93  0
 
 94  0
         // Prefix and extend method
 95  0
         transformation.prefixMethod(method, statusToken + " = " + interField + ".checkBefore(" + configField + ");");
 96  0
         transformation.extendExistingMethod(method, interField + ".checkAfter(" + statusToken + ", null);");
 97  0
     }
 98  0
 
 99  0
     private String createConfigAttributeDefinitionField(
 100  0
             final ClassTransformation transformation,
 101  0
             final Secured annotation) {
 102  0
         ConfigAttributeDefinition configAttributeDefinition =
 103  0
             new ConfigAttributeDefinition();
 104  0
         for (String auth : annotation.value()) {
 105  0
             configAttributeDefinition.addConfigAttribute(
 106  0
                     new SecurityConfig(auth));
 107  0
         }
 108  0
         return transformation.addInjectedField(ConfigAttributeDefinition.class,
 109  0
                 "_$configAttributeDefinition", configAttributeDefinition);
 110  0
     }
 111  0
 }