View Javadoc

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