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.Arrays;
17  
18  import org.eclipse.jetty.util.LazyList;
19  
20  /**
21   * 
22   * Badly named class that holds the role and user data constraint info for a
23   * path/http method combination, extracted and combined from security
24   * constraints.
25   * 
26   * @version $Rev: 4793 $ $Date: 2009-03-19 00:00:01 +0100 (Thu, 19 Mar 2009) $
27   */
28  public class RoleInfo
29  {
30      private final static String[] NO_ROLES={};
31      private boolean _isAnyRole;
32      private boolean _checked;
33      private boolean _forbidden;
34      private UserDataConstraint _userDataConstraint;
35  
36      private String[] _roles = NO_ROLES;
37  
38      public RoleInfo()
39      {    
40      }
41      
42      public boolean isChecked()
43      {
44          return _checked;
45      }
46  
47      public void setChecked(boolean checked)
48      {
49          this._checked = checked;
50          if (!checked)
51          {
52              _forbidden=false;
53              _roles=NO_ROLES;
54              _isAnyRole=false;
55          }
56      }
57  
58      public boolean isForbidden()
59      {
60          return _forbidden;
61      }
62  
63      public void setForbidden(boolean forbidden)
64      {
65          this._forbidden = forbidden;
66          if (forbidden)
67          {
68              _checked = true;
69              _userDataConstraint = null;
70              _isAnyRole=false;
71              _roles=NO_ROLES;
72          }
73      }
74  
75      public boolean isAnyRole()
76      {
77          return _isAnyRole;
78      }
79  
80      public void setAnyRole(boolean anyRole)
81      {
82          this._isAnyRole=anyRole;
83          if (anyRole)
84          {
85              _checked = true;
86              _roles=NO_ROLES;
87          }
88      }
89  
90      public UserDataConstraint getUserDataConstraint()
91      {
92          return _userDataConstraint;
93      }
94  
95      public void setUserDataConstraint(UserDataConstraint userDataConstraint)
96      {
97          if (userDataConstraint == null) throw new NullPointerException("Null UserDataConstraint");
98          if (this._userDataConstraint == null)
99          {
100             this._userDataConstraint = userDataConstraint;
101         }
102         else
103         {
104             this._userDataConstraint = this._userDataConstraint.combine(userDataConstraint);
105         }
106     }
107 
108     public String[] getRoles()
109     {
110         return _roles;
111     }
112     
113     public void addRole(String role)
114     {
115         _roles=(String[])LazyList.addToArray(_roles,role,String.class);
116     }
117 
118     public void combine(RoleInfo other)
119     {
120         if (other._forbidden)
121             setForbidden(true);
122         else if (!other._checked) // TODO is this the right way around???
123             setChecked(true);
124         else if (other._isAnyRole)
125             setAnyRole(true);
126         else if (!_isAnyRole)
127         {
128             for (String r : other._roles)
129                 _roles=(String[])LazyList.addToArray(_roles,r,String.class);
130         }
131         
132         setUserDataConstraint(other._userDataConstraint);
133     }
134     
135     public String toString()
136     {
137         return "{RoleInfo"+(_forbidden?",F":"")+(_checked?",C":"")+(_isAnyRole?",*":Arrays.asList(_roles).toString())+"}";
138     }
139 }