View Javadoc

1   /*
2    * Copyright (c) 2004 UNINETT FAS
3    *
4    * This program is free software; you can redistribute it and/or modify it
5    * under the terms of the GNU General Public License as published by the Free
6    * Software Foundation; either version 2 of the License, or (at your option)
7    * any later version.
8    *
9    * This program is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11   * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12   * more details.
13   *
14   * You should have received a copy of the GNU General Public License along with
15   * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16   * Place - Suite 330, Boston, MA 02111-1307, USA.
17   *
18   *
19   * $Id: AuthorizationAttribute.java,v 1.18 2005/08/31 07:36:51 catoolsen Exp $
20   */
21  
22  package no.feide.moria.authorization;
23  
24  
25  /***
26   * This class represents an LDAP attribute and is used for authorization of a
27   * web service. Both {@link AuthorizationManager} and {@link AuthorizationClient} have lists of attributes.
28   *
29   * @author Lars Preben S. Arnesen <lars.preben.arnesen@conduct.no>
30   * @version $Revision: 1.18 $
31   */
32  final class AuthorizationAttribute {
33  
34      /***
35       * Cached hash code.
36       */
37      private volatile int hashCode = 0;
38  
39  
40      /***
41       * Name of attribute.
42       */
43      private String name = null;
44  
45      /***
46       * Is this attribute allowed in use with SSO?
47       */
48      private boolean allowSSO = false;
49  
50      /***
51       * Security level.
52       */
53      private int secLevel = 2;
54  
55      /***
56       * Constructor. Name of attribute must be a non-empty string. Security
57       * level must be >= 0.
58       *
59       * @param name     Name of attribute.
60       * @param allowSSO Allow use of SSO with this attribute.
61       * @param secLevel The attribute's security level.
62       * @throws IllegalArgumentException
63       *             If <code>name</code> is <code>null</code> or
64       *             an empty string, or <code>secLevel</code> is not between
65       *             0 and 2, inclusive.
66       */
67      AuthorizationAttribute(final String name, final boolean allowSSO, final int secLevel) {
68  
69          if (name == null || name.equals("")) {
70              throw new IllegalArgumentException("Attribute name must be a non-empty string");
71          }
72  
73          if ((secLevel < 0) || (secLevel > 2)) {
74              throw new IllegalArgumentException("Security level for attribute \"" + name + "\" must be 0, 1 or 2; was: " + secLevel);
75          }
76  
77          this.secLevel = secLevel;
78          this.allowSSO = allowSSO;
79          this.name = name;
80      }
81  
82      /***
83       * Returns true if the supplied object is identical to this one.
84       *
85       * @param object The object to compare with.
86       * @return false if any of the attributes are different from the supplied
87       *         object.
88       */
89      public boolean equals(final Object object) {
90          if (object == this) {
91              return true;
92          }
93          if (object instanceof AuthorizationAttribute) {
94              final AuthorizationAttribute attr = (AuthorizationAttribute) object;
95              if (attr.getName().equals(name) && attr.getAllowSSO() == getAllowSSO() && attr.getSecLevel() == secLevel) {
96                  return true;
97              }
98          }
99          return false;
100     }
101 
102     /***
103      * Generates a hashCode from the object's attributes. 'name', 'secLevel'
104      * and 'allowSSO' are used for the computation.
105      *
106      * @return The hashcode for this object.
107      */
108     public int hashCode() {
109         if (hashCode == 0) {
110             int result = 17;
111             result = 37 * result + name.hashCode();
112             result = 37 * result + secLevel;
113             result = 37 * result + (allowSSO ? 0 : 1);
114             hashCode = result;
115         }
116         return hashCode;
117     }
118 
119     /***
120      * Gets the security level of this attribute.
121      *
122      * @return Security level.
123      */
124     public int getSecLevel() {
125         return secLevel;
126     }
127 
128     /***
129      * Gets the name of attribute.
130      *
131      * @return Name of the attribute.
132      */
133     public String getName() {
134         return name;
135     }
136 
137     /***
138      * Returns true if the attribute is allowed in use with SSO.
139      *
140      * @return True if the attribute can be used with SSO, else false.
141      */
142     public boolean getAllowSSO() {
143         return allowSSO;
144     }
145 
146     /***
147      * Returns a string representation of this object.
148      *
149      * @return The string representation of this object.
150      */
151     public String toString() {
152         return ("Attribute name: " + name + " secLevel: " + secLevel + " allowSSO: " + allowSSO);
153     }
154 }