Coverage Report - org.chenillekit.access.internal.ChenillekitAccessInternalUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
ChenillekitAccessInternalUtils
50%
19/38
36%
10/28
0
 
 1  
 /**
 2  
  *
 3  
  */
 4  
 package org.chenillekit.access.internal;
 5  
 
 6  
 import org.apache.tapestry5.EventContext;
 7  
 import org.apache.tapestry5.annotations.OnEvent;
 8  
 import org.apache.tapestry5.services.ContextValueEncoder;
 9  
 import org.apache.tapestry5.services.TransformMethodSignature;
 10  
 import org.chenillekit.access.ChenilleKitAccessConstants;
 11  
 
 12  
 /**
 13  
  *        @version $Id: ChenillekitAccessInternalUtils.java 374 2008-12-12 16:47:49Z mlusetti $
 14  
  */
 15  
 public class ChenillekitAccessInternalUtils
 16  
 {
 17  1
         private static final String[] NO_GROUPS = new String[0];
 18  
 
 19  0
         private ChenillekitAccessInternalUtils() {}
 20  
 
 21  
         /**
 22  
          * Build a string representing the context to be stored inside a cookie.
 23  
          *
 24  
          * @param context the context to be stored in the cookie
 25  
          * @return a String rapresentation of the {@link EventContext}
 26  
          */
 27  
         public static final String getContextAsString(EventContext context)
 28  
         {
 29  3
                 String res = "";
 30  
 
 31  3
                 for (int i = 0; i < context.getCount(); i++)
 32  
                 {
 33  0
                         res = res + context.get(String.class, i) + "####";
 34  
                 }
 35  
 
 36  3
                 return res;
 37  
         }
 38  
 
 39  
         /**
 40  
          *
 41  
          * @param valueEncoder
 42  
          * @param contextString
 43  
          * @return
 44  
          */
 45  
         public static final EventContext getContextFromString(ContextValueEncoder valueEncoder,
 46  
                                                         String contextString)
 47  
         {
 48  0
                 String[] contextElements = contextString.split("####");
 49  
 
 50  0
                 return new CKUrlEventContext(valueEncoder, contextElements);
 51  
         }
 52  
 
 53  
         /**
 54  
          * check if user has required role to access page/component/event.
 55  
          *
 56  
          * @param userRoleWeigh     role weigh the user have
 57  
          * @param requiredRoleWeigh role weigh required for page/component/event access
 58  
          *
 59  
          * @return true if user fulfill the required role
 60  
          */
 61  
         public static final boolean hasUserRequiredRole(int userRoleWeigh, int requiredRoleWeigh)
 62  
         {
 63  0
                 return userRoleWeigh >= requiredRoleWeigh;
 64  
         }
 65  
 
 66  
         /**
 67  
          * check if user has required group to access page/component/event.
 68  
          *
 69  
          * @param userGroups     groups the user have
 70  
          * @param requiredGroups groups required for page/component/event access
 71  
          *
 72  
          * @return true if user has the required group
 73  
          */
 74  
         public static final boolean hasUserRequiredGroup(String[] userGroups, String[] requiredGroups)
 75  
         {
 76  0
                 boolean hasGroup = false;
 77  
 
 78  
                 /**
 79  
                  * if no group required
 80  
                  */
 81  0
                 if (requiredGroups.length == 0)
 82  0
                         return true;
 83  
 
 84  0
                 for (String requiredGroup : requiredGroups)
 85  
                 {
 86  0
                         for (String userGroup : userGroups)
 87  
                         {
 88  0
                                 if (userGroup.equalsIgnoreCase(requiredGroup))
 89  
                                 {
 90  0
                                         hasGroup = true;
 91  0
                                         break;
 92  
                                 }
 93  
                         }
 94  
 
 95  0
                         if (hasGroup)
 96  0
                                 break;
 97  
                 }
 98  
 
 99  0
                 return hasGroup;
 100  
         }
 101  
 
 102  
         /**
 103  
          *
 104  
          * @param componentId
 105  
          * @param eventType
 106  
          * @param suffix
 107  
          * @return
 108  
          */
 109  
         public static final String buildMetaForHandlerMethod(String componentId, String eventType, String suffix)
 110  
         {
 111  10
                 return ChenilleKitAccessConstants.RESTRICTED_EVENT_HANDLER_PREFIX
 112  
                                         + "-" + componentId + "-" + eventType + "-" + suffix;
 113  
 
 114  
         }
 115  
 
 116  
         /**
 117  
          * build an CSV string from group array.
 118  
          *
 119  
          * @return CSV string
 120  
          */
 121  
         public static final String getStringArrayAsString(String[] groups)
 122  
         {
 123  2
                 String csvString = "";
 124  
 
 125  4
                 for (int i = 0; i < groups.length; i++)
 126  
                 {
 127  2
                         if (i == 0)
 128  2
                                 csvString += groups[i];
 129  
                         else
 130  0
                                 csvString += "," + groups[i];
 131  
                 }
 132  
 
 133  2
                 return csvString;
 134  
         }
 135  
 
 136  
         /**
 137  
          *
 138  
          * @param groups
 139  
          * @return
 140  
          */
 141  
         public static final String[] getStringAsStringArray(String groups)
 142  
         {
 143  0
                 if ( ChenilleKitAccessConstants.NO_RESTRICTION.equals(groups)) return NO_GROUPS;
 144  0
                 return groups.split(",");
 145  
         }
 146  
 
 147  
         /**
 148  
          * This code is taken deliberatly from
 149  
          * http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/OnEventWorker.java?view=log
 150  
          */
 151  
         public static final String extractComponentId(TransformMethodSignature method, OnEvent annotation)
 152  
         {
 153  2
                 if (annotation != null) return annotation.component();
 154  
 
 155  
                 // Method name started with "on". Extract the component id, if present.
 156  
 
 157  1
                 String name = method.getMethodName();
 158  
 
 159  1
                 int fromx = name.indexOf("From");
 160  
 
 161  1
                 if (fromx < 0) return "";
 162  
 
 163  1
                 return name.substring(fromx + 4);
 164  
         }
 165  
 
 166  
         /**
 167  
          * This code is taken deliberatly from:
 168  
          * http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/OnEventWorker.java?view=log
 169  
          */
 170  
         public static final String extractEventType(TransformMethodSignature method, OnEvent annotation)
 171  
         {
 172  2
                 if (annotation != null) return annotation.value();
 173  
 
 174  
                 // Method name started with "on". Extract the event type.
 175  
 
 176  1
                 String name = method.getMethodName();
 177  
 
 178  1
                 int fromx = name.indexOf("From");
 179  
 
 180  1
                 return fromx == -1 ? name.substring(2) : name.substring(2, fromx);
 181  
         }
 182  
 
 183  
 }