1 //
2 //  ========================================================================
3 //  Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4 //  ------------------------------------------------------------------------
5 //  All rights reserved. This program and the accompanying materials
6 //  are made available under the terms of the Eclipse Public License v1.0
7 //  and Apache License v2.0 which accompanies this distribution.
8 //
9 //      The Eclipse Public License is available at
10 //      http://www.eclipse.org/legal/epl-v10.html
11 //
12 //      The Apache License v2.0 is available at
13 //      http://www.opensource.org/licenses/apache2.0.php
14 //
15 //  You may elect to redistribute this code under either of these licenses.
16 //  ========================================================================
17 //
18 
19 package org.eclipse.jetty.security;
20 
21 import java.util.Set;
22 import java.util.concurrent.CopyOnWriteArraySet;
23 
24 /**
25  *
26  * Badly named class that holds the role and user data constraint info for a
27  * path/http method combination, extracted and combined from security
28  * constraints.
29  *
30  * @version $Rev: 4793 $ $Date: 2009-03-19 00:00:01 +0100 (Thu, 19 Mar 2009) $
31  */
32 public class RoleInfo
33 {
34     private boolean _isAnyRole;
35     private boolean _checked;
36     private boolean _forbidden;
37     private UserDataConstraint _userDataConstraint;
38 
39     private final Set<String> _roles = new CopyOnWriteArraySet<String>();
40 
RoleInfo()41     public RoleInfo()
42     {
43     }
44 
isChecked()45     public boolean isChecked()
46     {
47         return _checked;
48     }
49 
setChecked(boolean checked)50     public void setChecked(boolean checked)
51     {
52         this._checked = checked;
53         if (!checked)
54         {
55             _forbidden=false;
56             _roles.clear();
57             _isAnyRole=false;
58         }
59     }
60 
isForbidden()61     public boolean isForbidden()
62     {
63         return _forbidden;
64     }
65 
setForbidden(boolean forbidden)66     public void setForbidden(boolean forbidden)
67     {
68         this._forbidden = forbidden;
69         if (forbidden)
70         {
71             _checked = true;
72             _userDataConstraint = null;
73             _isAnyRole=false;
74             _roles.clear();
75         }
76     }
77 
isAnyRole()78     public boolean isAnyRole()
79     {
80         return _isAnyRole;
81     }
82 
setAnyRole(boolean anyRole)83     public void setAnyRole(boolean anyRole)
84     {
85         this._isAnyRole=anyRole;
86         if (anyRole)
87         {
88             _checked = true;
89             _roles.clear();
90         }
91     }
92 
getUserDataConstraint()93     public UserDataConstraint getUserDataConstraint()
94     {
95         return _userDataConstraint;
96     }
97 
setUserDataConstraint(UserDataConstraint userDataConstraint)98     public void setUserDataConstraint(UserDataConstraint userDataConstraint)
99     {
100         if (userDataConstraint == null) throw new NullPointerException("Null UserDataConstraint");
101         if (this._userDataConstraint == null)
102         {
103             this._userDataConstraint = userDataConstraint;
104         }
105         else
106         {
107             this._userDataConstraint = this._userDataConstraint.combine(userDataConstraint);
108         }
109     }
110 
getRoles()111     public Set<String> getRoles()
112     {
113         return _roles;
114     }
115 
addRole(String role)116     public void addRole(String role)
117     {
118         _roles.add(role);
119     }
120 
combine(RoleInfo other)121     public void combine(RoleInfo other)
122     {
123         if (other._forbidden)
124             setForbidden(true);
125         else if (!other._checked) // TODO is this the right way around???
126             setChecked(true);
127         else if (other._isAnyRole)
128             setAnyRole(true);
129         else if (!_isAnyRole)
130         {
131             for (String r : other._roles)
132                 _roles.add(r);
133         }
134 
135         setUserDataConstraint(other._userDataConstraint);
136     }
137 
138     @Override
toString()139     public String toString()
140     {
141         return "{RoleInfo"+(_forbidden?",F":"")+(_checked?",C":"")+(_isAnyRole?",*":_roles)+"}";
142     }
143 }
144