Coverage Report - org.openpermis.basic.InternalSubject
 
Classes in this File Line Coverage Branch Coverage Complexity
InternalSubject
96%
30/31
90%
9/10
1.571
InternalSubject$RoleKey
88%
8/9
50%
2/4
1.571
InternalSubject$RoleSubjectKey
100%
8/8
50%
2/4
1.571
 
 1  
 /*
 2  
  * Copyright (c) 2009, Swiss Federal Department of Defence Civil Protection and Sport
 3  
  *                     (http://www.vbs.admin.ch)
 4  
  * Copyright (c) 2009, Ergon Informatik AG (http://www.ergon.ch)
 5  
  * All rights reserved.
 6  
  *
 7  
  * Licensed under the Open Permis License which accompanies this distribution,
 8  
  * and is available at http://www.openpermis.org/BSDlicenceKent.txt
 9  
  */
 10  
 package org.openpermis.basic;
 11  
 
 12  
 import java.net.URI;
 13  
 import java.util.HashMap;
 14  
 import java.util.HashSet;
 15  
 import java.util.Map;
 16  
 import java.util.Set;
 17  
 
 18  
 import org.openpermis.Subject;
 19  
 import org.openpermis.policy.Role;
 20  
 import org.openpermis.policy.TimeStamp;
 21  
 import org.openpermis.policy.bean.basic.BasicPart;
 22  
 
 23  
 
 24  
 /**
 25  
  * A subject that only knows explicitly assigned roles and issuers.
 26  
  * @since 0.1.0
 27  
  */
 28  
 public class InternalSubject
 29  
         extends BasicPart
 30  
         implements Subject
 31  
 {
 32  
         
 33  
         //---- Static
 34  
         
 35  
         /**
 36  
          * @since 0.1.0
 37  
          */
 38  
         private static final long serialVersionUID = -5706090972924620861L;
 39  
         
 40  
         /**
 41  
          * @since 0.1.0
 42  
          */
 43  
         private class RoleKey {
 44  
                 
 45  
                 private final String name;
 46  
                 private final URI hierarchy;
 47  
                 
 48  
 
 49  86
                 public RoleKey (String name, URI hierarchy) {
 50  86
                         this.name = name;
 51  86
                         this.hierarchy = hierarchy;
 52  86
                 }
 53  
                 
 54  
                 @Override
 55  
                 public String toString () {
 56  0
                         return this.name + "@" + this.hierarchy;
 57  
                 }
 58  
                 
 59  
                 @Override
 60  
                 public boolean equals (Object obj) {
 61  61
                         final RoleKey that = (RoleKey) obj;
 62  61
                         return this.name.equals(that.name) && this.hierarchy.equals(that.hierarchy);
 63  
                 }
 64  
                 
 65  
                 @Override
 66  
                 public int hashCode () {
 67  133
                         final int factor = 29;
 68  133
                         return this.name.hashCode() + factor * this.hierarchy.hashCode();
 69  
                 }
 70  
 
 71  
         }
 72  
         
 73  
         /**
 74  
          * @since 0.3.0
 75  
          */
 76  
         private class RoleSubjectKey {
 77  
                 
 78  
                 private final RoleKey role;
 79  
                 private final Subject subject;
 80  
                 
 81  
 
 82  25
                 public RoleSubjectKey (RoleKey role, Subject subject) {
 83  25
                         this.role = role;
 84  25
                         this.subject = subject;
 85  25
                 }
 86  
                 
 87  
                 @Override
 88  
                 public boolean equals (Object obj) {
 89  8
                         final RoleSubjectKey that = (RoleSubjectKey) obj;
 90  8
                         return this.role.equals(that.role) && this.subject.equals(that.subject);
 91  
                 }
 92  
                 
 93  
                 @Override
 94  
                 public int hashCode () {
 95  25
                         final int factor = 29;
 96  25
                         return this.role.hashCode() + factor * this.subject.hashCode();
 97  
                 }
 98  
 
 99  
         }
 100  
 
 101  
         //---- State
 102  
         
 103  
         /**
 104  
          * A map registering which roles this subject has and who issued them.
 105  
          */
 106  
         private final Map<RoleKey, Set<Subject>> roleIssuerMap;
 107  
         
 108  
         /**
 109  
          * A map registering the validity of role assignments.
 110  
          */
 111  
         private final Map<RoleSubjectKey, TimePeriod> roleSubjectPeriodMap;
 112  
         
 113  
         //---- Constructors
 114  
         
 115  
         /**
 116  
          * Creates a subject without any roles.
 117  
          * @param identity the identity of this subject.
 118  
          * @since 0.1.0
 119  
          */
 120  34
         public InternalSubject (URI identity) {
 121  34
                 setIdentity(identity);
 122  34
                 this.roleIssuerMap = new HashMap<RoleKey, Set<Subject>>();
 123  34
                 this.roleSubjectPeriodMap = new HashMap<RoleSubjectKey, TimePeriod>();
 124  34
         }
 125  
         
 126  
         //---- Methods
 127  
         
 128  
         /**
 129  
          * 
 130  
          * Assigns a role to this subject.
 131  
          * @param issuer the {@link Subject} assigning the role to this subject.
 132  
          * @param roleName the name of the role that is being assigned.
 133  
          * @param hierarchyUri the {@link URI} of the role hierarchy that contains the
 134  
          *  role being assigned.
 135  
          * @param validity a {@link TimePeriod}.
 136  
          * @since 0.3.0
 137  
          */
 138  
         public void assignRole (
 139  
                 Subject issuer, String roleName, URI hierarchyUri, TimePeriod validity
 140  
         ) {
 141  17
                 final RoleKey key = new RoleKey(roleName, hierarchyUri);
 142  17
                 Set<Subject> issuerSet = this.roleIssuerMap.get(key);
 143  17
                 if (issuerSet == null) {
 144  15
                         issuerSet = new HashSet<Subject>(1);
 145  15
                         this.roleIssuerMap.put(key, issuerSet);
 146  
                 }
 147  17
                 issuerSet.add(issuer);
 148  
 
 149  17
                 this.roleSubjectPeriodMap.put(new RoleSubjectKey(key, issuer), validity);                
 150  17
         }
 151  
 
 152  
         /**
 153  
          * @since 0.3.0
 154  
          */
 155  
         private RoleKey getRoleKey (Role role) {
 156  69
                 return new RoleKey(role.getName(), role.getRoleHierarchy().getIdentity());
 157  
         }
 158  
         
 159  
         //---- BasicPart
 160  
         
 161  
         /**
 162  
          * @since 0.1.0
 163  
          */
 164  
         @Override
 165  
         protected boolean comparablePart (BasicPart part) {
 166  1
                 return part instanceof Subject;
 167  
         }
 168  
 
 169  
         //---- Subject
 170  
         
 171  
         /**
 172  
          * @since 0.1.0
 173  
          */
 174  
         public Set<Role> getAssignedRoles (TimeStamp timeStamp, Set<Role> roles) {
 175  25
                 final Set<Role> assignedRoles = new HashSet<Role>();
 176  25
                 for (final Role role : roles) {
 177  45
                         if (this.roleIssuerMap.containsKey(getRoleKey(role))) {
 178  21
                                 assignedRoles.add(role);
 179  
                         }
 180  
                 }
 181  25
                 return assignedRoles;
 182  
         }
 183  
 
 184  
         /**
 185  
          * @since 0.1.0
 186  
          */
 187  
         public Set<Subject> getIssuersOf (Role role) {
 188  16
                 final Set<Subject> issuerSet = new HashSet<Subject>();
 189  16
                 final RoleKey key = getRoleKey(role);
 190  16
                 if (this.roleIssuerMap.containsKey(key)) {
 191  15
                         issuerSet.addAll(this.roleIssuerMap.get(key));
 192  
                 }
 193  16
                 return issuerSet;
 194  
         }
 195  
 
 196  
         /**
 197  
          * @since 0.3.0
 198  
          */
 199  
         public TimePeriod getValidityOf (Role role, Subject issuer) {
 200  8
                 final RoleKey roleKey = getRoleKey(role);
 201  8
                 final RoleSubjectKey key = new RoleSubjectKey(roleKey, issuer);
 202  8
                 final TimePeriod validity = this.roleSubjectPeriodMap.get(key);
 203  8
                 if (validity != null) {
 204  8
                         return validity;
 205  
                 }
 206  0
                 return TimePeriod.EMPTY;
 207  
         }
 208  
 
 209  
 }