Coverage Report - org.chenillekit.access.services.impl.RestrictedWorker
 
Classes in this File Line Coverage Branch Coverage Complexity
RestrictedWorker
89%
25/28
80%
8/10
0
 
 1  
 /*
 2  
  *  Apache License
 3  
  *  Version 2.0, January 2004
 4  
  *  http://www.apache.org/licenses/
 5  
  *
 6  
  *  Copyright 2008 by chenillekit.org
 7  
  *
 8  
  *  Licensed under the Apache License, Version 2.0 (the "License");
 9  
  *  you may not use this file except in compliance with the License.
 10  
  *  You may obtain a copy of the License at
 11  
  *
 12  
  *  http://www.apache.org/licenses/LICENSE-2.0
 13  
  */
 14  
 
 15  
 /**
 16  
  *
 17  
  */
 18  
 
 19  
 package org.chenillekit.access.services.impl;
 20  
 
 21  
 import org.apache.tapestry5.annotations.OnEvent;
 22  
 import org.apache.tapestry5.model.MutableComponentModel;
 23  
 import org.apache.tapestry5.services.ClassTransformation;
 24  
 import org.apache.tapestry5.services.ComponentClassTransformWorker;
 25  
 import org.apache.tapestry5.services.TransformMethodSignature;
 26  
 import org.chenillekit.access.ChenilleKitAccessConstants;
 27  
 import org.chenillekit.access.ChenilleKitAccessException;
 28  
 import org.chenillekit.access.annotations.Restricted;
 29  
 import org.chenillekit.access.internal.ChenillekitAccessInternalUtils;
 30  
 
 31  
 /**
 32  
  *
 33  
  * @version $Id: AccessControlWorker.java 88 2008-06-16 16:43:40Z homburgs $
 34  
  */
 35  1
 public class RestrictedWorker implements ComponentClassTransformWorker
 36  
 {
 37  
         // FIXME How do we get the Logger from the IoC?
 38  
         // private final Logger logger;
 39  
 
 40  
 
 41  
         /* (non-Javadoc)
 42  
          * @see org.apache.tapestry5.services.ComponentClassTransformWorker#transform(org.apache.tapestry5.services.ClassTransformation, org.apache.tapestry5.model.MutableComponentModel)
 43  
          */
 44  
         public void transform(ClassTransformation transformation, MutableComponentModel model)
 45  
         {
 46  23
                 processPageRestriction(transformation, model);
 47  
 
 48  23
                 processEventHandlerRestrictions(transformation, model);
 49  
 
 50  
                 // processComponentsRestrictions(transformation, model);
 51  23
         }
 52  
 
 53  
         /**
 54  
          * Read and process restriction on page classes annotated with {@link Restricted} annotation
 55  
          *
 56  
          * @param transformation Contains class-specific information used when transforming a raw component class
 57  
          *                       into an executable component class.
 58  
          * @param model          Mutable version of {@link org.apache.tapestry5.model.ComponentModel} used during
 59  
          *                       the transformation phase.
 60  
          */
 61  
         private void processPageRestriction(ClassTransformation transformation, MutableComponentModel model)
 62  
         {
 63  23
                 Restricted pageRestricted = transformation.getAnnotation(Restricted.class);
 64  23
                 if (pageRestricted != null)
 65  
                 {
 66  2
                         String roleWeigh = Integer.toString(pageRestricted.role());
 67  2
                         String[] groups = pageRestricted.groups();
 68  
 
 69  2
                         model.setMeta(ChenilleKitAccessConstants.RESTRICTED_PAGE_ROLE, roleWeigh);
 70  
 
 71  2
                         if (groups.length > 0)
 72  0
                                 model.setMeta(ChenilleKitAccessConstants.RESTRICTED_PAGE_GROUP,
 73  
                                                 ChenillekitAccessInternalUtils.getStringArrayAsString(groups));
 74  
                 }
 75  23
         }
 76  
 
 77  
         /**
 78  
          * inject meta datas about annotated methods
 79  
          *
 80  
          * @param transformation Contains class-specific information used when transforming a raw component class
 81  
          *                       into an executable component class.
 82  
          * @param model          Mutable version of {@link org.apache.tapestry5.model.ComponentModel} used during
 83  
          *                       the transformation phase.
 84  
          */
 85  
         private void processEventHandlerRestrictions(ClassTransformation transformation, MutableComponentModel model)
 86  
         {
 87  23
                 for (TransformMethodSignature method : transformation.findMethodsWithAnnotation(Restricted.class))
 88  
                 {
 89  2
                         String methodName = method.getMethodName();
 90  2
                         Restricted restricted = transformation.getMethodAnnotation(method, Restricted.class);
 91  2
                         OnEvent event = transformation.getMethodAnnotation(method, OnEvent.class);
 92  2
                         if (methodName.startsWith("on") || event != null)
 93  
                         {
 94  2
                                 String componentId = ChenillekitAccessInternalUtils.extractComponentId(method, event);
 95  2
                                 String eventType = ChenillekitAccessInternalUtils.extractEventType(method, event);
 96  
 
 97  2
                                 String groupMeta = ChenillekitAccessInternalUtils.buildMetaForHandlerMethod(componentId,
 98  
                                                 eventType,
 99  
                                                 ChenilleKitAccessConstants.RESTRICTED_EVENT_HANDLER_GROUPS_SUFFIX);
 100  
 
 101  2
                                 String roleMeta = ChenillekitAccessInternalUtils.buildMetaForHandlerMethod(componentId,
 102  
                                                 eventType,
 103  
                                                 ChenilleKitAccessConstants.RESTRICTED_EVENT_HANDLER_ROLE_SUFFIX);
 104  
 
 105  2
                                 model.setMeta(groupMeta, ChenillekitAccessInternalUtils.getStringArrayAsString(restricted.groups()));
 106  2
                                 model.setMeta(roleMeta, Integer.toString(restricted.role()));
 107  2
                         }
 108  
                         else
 109  
                         {
 110  0
                                 throw new ChenilleKitAccessException("Cannot put Restricted annotation on a non event handler method");
 111  
                         }
 112  2
                 }
 113  23
         }
 114  
 
 115  
         /**
 116  
          * inject meta datas about annotated methods
 117  
          *
 118  
          * @param transformation Contains class-specific information used when transforming a raw component class
 119  
          *                       into an executable component class.
 120  
          * @param model          Mutable version of {@link org.apache.tapestry5.model.ComponentModel} used during
 121  
          *                       the transformation phase.
 122  
          */
 123  
         private void processComponentsRestrictions(ClassTransformation transformation, MutableComponentModel model)
 124  
         {
 125  0
                 throw new RuntimeException("NOT YET IMPLEMENTED");
 126  
         }
 127  
 }