Coverage report

  %line %branch
org.apache.jetspeed.om.page.SecurityConstraintImpl
0% 
0% 

 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  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  
 package org.apache.jetspeed.om.page;
 18  
 
 19  
 import java.util.Collection;
 20  
 import java.util.Iterator;
 21  
 import java.util.List;
 22  
 import java.util.StringTokenizer;
 23  
 
 24  
 import org.apache.jetspeed.om.common.SecurityConstraint;
 25  
 import org.apache.jetspeed.page.impl.DatabasePageManagerUtils;
 26  
 
 27  
 
 28  
 /**
 29  
  * <p>
 30  
  * SecurityConstraintImpl
 31  
  * </p>
 32  
  * <p>
 33  
  *
 34  
  * </p>
 35  
  * @author <a href="mailto:rwatler@finali.com">Randy Watler</a>
 36  
  * @version $Id: SecurityConstraintImpl.java 516448 2007-03-09 16:25:47Z ate $
 37  
  *
 38  
  */
 39  0
 public class SecurityConstraintImpl implements SecurityConstraint
 40  
 {
 41  
     private int id;
 42  
     private int applyOrder;
 43  
     private List usersList;
 44  
     private List rolesList;
 45  
     private List groupsList;
 46  
     private List permissionsList;
 47  
 
 48  
     private String users;
 49  
     private String roles;
 50  
     private String groups;
 51  
     private String permissions;
 52  
 
 53  
     /**
 54  
      * getApplyOrder
 55  
      *
 56  
      * @return apply order for constraints
 57  
      */
 58  
     public int getApplyOrder()
 59  
     {
 60  0
         return applyOrder;
 61  
     }
 62  
 
 63  
     /**
 64  
      * setApplyOrder
 65  
      *
 66  
      * @param order apply order for constraints
 67  
      */
 68  
     public void setApplyOrder(int order)
 69  
     {
 70  0
         applyOrder = order;
 71  0
     }
 72  
 
 73  
     /**
 74  
      * getUsersAsString
 75  
      *
 76  
      * @return users CSV list
 77  
      */
 78  
     public String getUsersAsString()
 79  
     {
 80  
         // get from users list if not immediately available
 81  0
         if ((users == null) && (usersList != class="keyword">null) && !usersList.isEmpty())
 82  
         {
 83  0
             users = formatCSVList(usersList);
 84  
         }
 85  0
         return users;
 86  
     }
 87  
 
 88  
     /**
 89  
      * setUsersAsString
 90  
      *
 91  
      * @param users users CSV list
 92  
      */
 93  
     public void setUsersAsString(String users)
 94  
     {
 95  
         // set and propagate to users list setting
 96  0
         this.users = users;
 97  0
         usersList = parseCSVList(users);
 98  0
     }
 99  
 
 100  
     /**
 101  
      * getRolesAsString
 102  
      *
 103  
      * @return roles CSV list
 104  
      */
 105  
     public String getRolesAsString()
 106  
     {
 107  
         // get from roles list if not immediately available
 108  0
         if ((roles == null) && (rolesList != class="keyword">null) && !rolesList.isEmpty())
 109  
         {
 110  0
             roles = formatCSVList(rolesList);
 111  
         }
 112  0
         return roles;
 113  
     }
 114  
     
 115  
     /**
 116  
      * setRolesAsString
 117  
      *
 118  
      * @param roles roles CSV list
 119  
      */
 120  
     public void setRolesAsString(String roles)
 121  
     {
 122  
         // set and propagate to roles list setting
 123  0
         this.roles = roles;
 124  0
         rolesList = parseCSVList(roles);
 125  0
     }
 126  
 
 127  
     /**
 128  
      * getGroupsAsString
 129  
      *
 130  
      * @return groups CSV list
 131  
      */
 132  
     public String getGroupsAsString()
 133  
     {
 134  
         // get from groups list if not immediately available
 135  0
         if ((groups == null) && (groupsList != class="keyword">null) && !groupsList.isEmpty())
 136  
         {
 137  0
             groups = formatCSVList(groupsList);
 138  
         }
 139  0
         return groups;
 140  
     }
 141  
     
 142  
     /**
 143  
      * setGroupsAsString
 144  
      *
 145  
      * @param groups groups CSV list
 146  
      */
 147  
     public void setGroupsAsString(String groups)
 148  
     {
 149  
         // set and propagate to groups list setting
 150  0
         this.groups = groups;
 151  0
         groupsList = parseCSVList(groups);
 152  0
     }
 153  
 
 154  
     /**
 155  
      * getPermissionsAsString
 156  
      *
 157  
      * @return permissions CSV list
 158  
      */
 159  
     public String getPermissionsAsString()
 160  
     {
 161  
         // get from permissions list if not immediately available
 162  0
         if ((permissions == null) && (permissionsList != class="keyword">null) && !permissionsList.isEmpty())
 163  
         {
 164  0
             permissions = formatCSVList(permissionsList);
 165  
         }
 166  0
         return permissions;
 167  
     }
 168  
     
 169  
     /**
 170  
      * setPermissionsAsString
 171  
      *
 172  
      * @param permissions permissions CSV list
 173  
      */
 174  
     public void setPermissionsAsString(String permissions)
 175  
     {
 176  
         // set and propagate to permissions list setting
 177  0
         this.permissions = permissions;
 178  0
         permissionsList = parseCSVList(permissions);
 179  0
     }
 180  
     
 181  
     /**
 182  
      * <p>
 183  
      * getUsers
 184  
      * </p>
 185  
      *
 186  
      * @see org.apache.jetspeed.om.common.SecurityConstraint#getUsers()
 187  
      * @return users list
 188  
      */
 189  
     public List getUsers()
 190  
     {
 191  0
         return usersList;
 192  
     }
 193  
     
 194  
     /**
 195  
      * <p>
 196  
      * setUsers
 197  
      * </p>
 198  
      *
 199  
      * @see org.apache.jetspeed.om.common.SecurityConstraint#setUsers(java.util.List)
 200  
      * @param users users list
 201  
      */
 202  
     public void setUsers(List users)
 203  
     {
 204  
         // set and clear potentially stale string representation
 205  0
         usersList = users;
 206  0
         this.users = null;
 207  0
     }
 208  
 
 209  
     /**
 210  
      * <p>
 211  
      * getRoles
 212  
      * </p>
 213  
      *
 214  
      * @see org.apache.jetspeed.om.common.SecurityConstraint#getRoles()
 215  
      * @return roles list
 216  
      */
 217  
     public List getRoles()
 218  
     {
 219  0
         return rolesList;
 220  
     }
 221  
     
 222  
     /**
 223  
      * <p>
 224  
      * setRoles
 225  
      * </p>
 226  
      *
 227  
      * @see org.apache.jetspeed.om.common.SecurityConstraint#setRoles(java.util.List)
 228  
      * @param roles roles list
 229  
      */
 230  
     public void setRoles(List roles)
 231  
     {
 232  
         // set and clear potentially stale string representation
 233  0
         rolesList = roles;
 234  0
         this.roles = null;
 235  0
     }
 236  
 
 237  
     /**
 238  
      * <p>
 239  
      * getGroups
 240  
      * </p>
 241  
      *
 242  
      * @see org.apache.jetspeed.om.common.SecurityConstraint#getGroups()
 243  
      * @return groups list
 244  
      */
 245  
     public List getGroups()
 246  
     {
 247  0
         return groupsList;
 248  
     }
 249  
     
 250  
     /**
 251  
      * <p>
 252  
      * setGroups
 253  
      * </p>
 254  
      *
 255  
      * @see org.apache.jetspeed.om.common.SecurityConstraint#setGroups(java.util.List)
 256  
      * @param groups groups list
 257  
      */
 258  
     public void setGroups(List groups)
 259  
     {
 260  
         // set and clear potentially stale string representation
 261  0
         groupsList = groups;
 262  0
         this.groups = null;
 263  0
     }
 264  
 
 265  
     /**
 266  
      * <p>
 267  
      * getPermissions
 268  
      * </p>
 269  
      *
 270  
      * @see org.apache.jetspeed.om.common.SecurityConstraint#getPermissions()
 271  
      * @return permissions list
 272  
      */
 273  
     public List getPermissions()
 274  
     {
 275  0
         return permissionsList;
 276  
     }
 277  
     
 278  
     /**
 279  
      * <p>
 280  
      * setPermissions
 281  
      * </p>
 282  
      *
 283  
      * @see org.apache.jetspeed.om.common.SecurityConstraint#setPermissions(java.util.List)
 284  
      * @param permissions permissions list
 285  
      */
 286  
     public void setPermissions(List permissions)
 287  
     {
 288  
         // set and clear potentially stale string representation
 289  0
         permissionsList = permissions;
 290  0
         this.permissions = null;
 291  0
     }
 292  
 
 293  
     /**
 294  
      * <p>
 295  
      * principalsMatch
 296  
      * </p>
 297  
      * <p>
 298  
      * Test user/role/group names against principal names.
 299  
      * </p>
 300  
      *
 301  
      * @param userPrincipals
 302  
      * @param rolePrincipals
 303  
      * @param groupPrincipals
 304  
      * @param allowDefault
 305  
      * @return match result
 306  
      */
 307  
     public boolean principalsMatch(List userPrincipals, List rolePrincipals, List groupPrincipals, class="keyword">boolean allowDefault)
 308  
     {
 309  
         // test match using users, roles, and groups list members
 310  
         // since these are the master representation in this impl
 311  0
         return ((allowDefault && (usersList == null) && (rolesList == class="keyword">null) && (groupsList == class="keyword">null)) ||
 312  
                 ((usersList != null) && (userPrincipals != class="keyword">null) &&
 313  
                  (containsAny(usersList, userPrincipals) || usersList.contains(WILD_CHAR))) ||
 314  
                 ((rolesList != null) && (rolePrincipals != class="keyword">null) &&
 315  
                  (containsAny(rolesList, rolePrincipals) || rolesList.contains(WILD_CHAR))) ||
 316  
                 ((groupsList != null) && (groupPrincipals != class="keyword">null) &&
 317  
                  (containsAny(groupsList, groupPrincipals) || groupsList.contains(WILD_CHAR))));
 318  
     }
 319  
 
 320  
     /**
 321  
      * <p>
 322  
      * actionMatch
 323  
      * </p>
 324  
      * <p>
 325  
      * Test permission names against action name.
 326  
      * </p>
 327  
      *
 328  
      * @param action
 329  
      * @return match result
 330  
      */
 331  
     public boolean actionMatch(String action)
 332  
     {
 333  
         // test match using permissions list member since
 334  
         // this is the master representation in this impl
 335  0
         return ((permissionsList != null) &&
 336  
                 (permissionsList.contains(action) || permissionsList.contains(WILD_CHAR)));
 337  
     }
 338  
 
 339  
     /**
 340  
      * <p>
 341  
      * parseCSVList
 342  
      * </p>
 343  
      * <p>
 344  
      * Utility to parse CSV string values into Lists.
 345  
      * </p>
 346  
      *
 347  
      * @param csv
 348  
      * @return parsed values list.
 349  
      */
 350  
     public static List parseCSVList(String csv)
 351  
     {
 352  0
         if (csv != null)
 353  
         {
 354  0
             List csvList = DatabasePageManagerUtils.createList();
 355  0
             if (csv.indexOf(',') != -1)
 356  
             {
 357  0
                 StringTokenizer csvTokens = new StringTokenizer(csv, ",");
 358  0
                 while (csvTokens.hasMoreTokens())
 359  
                 {
 360  0
                     csvList.add(csvTokens.nextToken().trim());
 361  
                 }
 362  0
             }
 363  
             else
 364  
             {
 365  0
                 csvList.add(csv);
 366  
             }
 367  0
             return csvList;
 368  
         }
 369  0
         return null;
 370  
     }
 371  
 
 372  
     /**
 373  
      * <p>
 374  
      * formatCSVList
 375  
      * </p>
 376  
      * <p>
 377  
      * Utility to format CSV List values into strings.
 378  
      * </p>
 379  
      *
 380  
      * @param list
 381  
      * @return formatted string value.
 382  
      */
 383  
     public static String formatCSVList(List list)
 384  
     {
 385  0
         if ((list != null) && !list.isEmpty())
 386  
         {
 387  0
             StringBuffer csv = new StringBuffer();
 388  0
             Iterator listIter = list.iterator();
 389  0
             while (listIter.hasNext())
 390  
             {
 391  0
                 if (csv.length() > 0)
 392  
                 {
 393  0
                     csv.append(",");
 394  
                 }
 395  0
                 csv.append((String)listIter.next());
 396  
             }
 397  0
             return csv.toString();
 398  
         }
 399  0
         return null;
 400  
     }
 401  
 
 402  
     /**
 403  
      * <p>
 404  
      * containsAny
 405  
      * </p>
 406  
      * <p>
 407  
      * Utility implementation for contains any test against two collections.
 408  
      * </p>
 409  
      *
 410  
      * @param collection0
 411  
      * @param collection1
 412  
      * @return contains any result.
 413  
      */
 414  
     public static boolean containsAny(Collection collection0, Collection collection1)
 415  
     {
 416  0
         if ((collection0 != null) && (collection1 != class="keyword">null))
 417  
         {
 418  0
             Iterator containsIter = collection1.iterator();
 419  0
             while (containsIter.hasNext())
 420  
             {
 421  0
                 if (collection0.contains(containsIter.next()))
 422  
                 {
 423  0
                     return true;
 424  
                 }
 425  
             }
 426  
         }
 427  0
         return false;
 428  
     }
 429  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.