| 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
| 2 | * |
| 3 | * ***** BEGIN LICENSE BLOCK ***** |
| 4 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
| 5 | * |
| 6 | * The contents of this file are subject to the Mozilla Public License Version |
| 7 | * 1.1 (the "License"); you may not use this file except in compliance with |
| 8 | * the License. You may obtain a copy of the License at |
| 9 | * http://www.mozilla.org/MPL/ |
| 10 | * |
| 11 | * Software distributed under the License is distributed on an "AS IS" basis, |
| 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
| 13 | * for the specific language governing rights and limitations under the |
| 14 | * License. |
| 15 | * |
| 16 | * The Original Code is mozilla.org code. |
| 17 | * |
| 18 | * The Initial Developer of the Original Code is |
| 19 | * Netscape Communications Corporation. |
| 20 | * Portions created by the Initial Developer are Copyright (C) 1999 |
| 21 | * the Initial Developer. All Rights Reserved. |
| 22 | * |
| 23 | * Contributor(s): |
| 24 | * |
| 25 | * Alternatively, the contents of this file may be used under the terms of |
| 26 | * either the GNU General Public License Version 2 or later (the "GPL"), or |
| 27 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
| 28 | * in which case the provisions of the GPL or the LGPL are applicable instead |
| 29 | * of those above. If you wish to allow use of your version of this file only |
| 30 | * under the terms of either the GPL or the LGPL, and not to allow others to |
| 31 | * use your version of this file under the terms of the MPL, indicate your |
| 32 | * decision by deleting the provisions above and replace them with the notice |
| 33 | * and other provisions required by the GPL or the LGPL. If you do not delete |
| 34 | * the provisions above, a recipient may use your version of this file under |
| 35 | * the terms of any one of the MPL, the GPL or the LGPL. |
| 36 | * |
| 37 | * ***** END LICENSE BLOCK ***** */ |
| 38 | package org.ietf.ldap; |
| 39 | |
| 40 | import java.util.Enumeration; |
| 41 | import java.util.Vector; |
| 42 | |
| 43 | /** |
| 44 | * The definition of an object class in the schema. |
| 45 | * <A HREF="http://ds.internic.net/rfc/rfc2252.txt" |
| 46 | * TARGET="_blank">RFC 2252, Lightweight Directory Access Protocol (v3): |
| 47 | * Attribute Syntax Definitions</A> covers the types of information |
| 48 | * that need to be specified in the definition of an object class. |
| 49 | * According to the RFC, the description of an object class can |
| 50 | * include the following information: |
| 51 | * <P> |
| 52 | * |
| 53 | * <UL> |
| 54 | * <LI>an OID identifying the object class |
| 55 | * <LI>a name identifying the object class |
| 56 | * <LI>a description of the object class |
| 57 | * <LI>the name of the parent object class |
| 58 | * <LI>the list of attribute types that are required in this object class |
| 59 | * <LI>the list of attribute types that are allowed (optional) in this |
| 60 | * object class |
| 61 | * </UL> |
| 62 | * <P> |
| 63 | * |
| 64 | * When you construct an <CODE>LDAPObjectSchema</CODE> object, you can specify |
| 65 | * these types of information as arguments to the constructor or in the |
| 66 | * ObjectClassDescription format specified in RFC 2252. |
| 67 | * When an LDAP client searches an LDAP server for the schema, the server |
| 68 | * returns schema information as an object with attribute values in this |
| 69 | * format. |
| 70 | * <P> |
| 71 | * |
| 72 | * RFC 2252 also notes that you can specify whether or not an object class |
| 73 | * is abstract, structural, or auxiliary in the object description. |
| 74 | * Abstract object classes are used only to derive other object classes. |
| 75 | * Entries cannot belong to an abstract object class. <CODE>top</CODE> |
| 76 | * is an abstract object class. Entries must belong to a structural |
| 77 | * object class, so most object classes are structural object classes. |
| 78 | * Objects of the <CODE>LDAPObjectClassSchema</CODE> class are structural |
| 79 | * object classes by default. Auxiliary object classes can be used to |
| 80 | * add attributes to entries of different types. For example, an |
| 81 | * auxiliary object class might be used to specify personal preference |
| 82 | * attributes. An entry can not contain just that object class, but may |
| 83 | * include it along with a structural object class, for example |
| 84 | * inetOrgPerson. |
| 85 | * If the definition of an object (in ObjectClassDescription format) |
| 86 | * specifies the AUXILIARY keyword, an <CODE>LDAPObjectClassSchema</CODE> |
| 87 | * object created from that description represents an auxiliary object class. |
| 88 | * <P> |
| 89 | * |
| 90 | * You can get the name, OID, and description of this object class |
| 91 | * definition by using the <CODE>getName</CODE>, <CODE>getOID</CODE>, and |
| 92 | * <CODE>getDescription</CODE> methods inherited from the abstract class |
| 93 | * <CODE>LDAPSchemaElement</CODE>. Optional and custom qualifiers are |
| 94 | * accessed with <CODE>getQualifier</CODE> and <CODE>getQualifierNames</CODE> |
| 95 | * from <CODE>LDAPSchemaElement</CODE>. |
| 96 | |
| 97 | * <P> |
| 98 | * |
| 99 | * To add or remove this object class definition from the |
| 100 | * schema, use the <CODE>add</CODE> and <CODE>remove</CODE> |
| 101 | * methods, which this class inherits from the <CODE>LDAPSchemaElement</CODE> |
| 102 | * abstract class. |
| 103 | * <P> |
| 104 | * RFC 2252 defines ObjectClassDescription as follows: |
| 105 | * <P> |
| 106 | * <PRE> |
| 107 | * ObjectClassDescription = "(" whsp |
| 108 | * numericoid whsp ; ObjectClass identifier |
| 109 | * [ "NAME" qdescrs ] |
| 110 | * [ "DESC" qdstring ] |
| 111 | * [ "OBSOLETE" whsp ] |
| 112 | * [ "SUP" oids ] ; Superior ObjectClasses |
| 113 | * [ ( "ABSTRACT" / "STRUCTURAL" / "AUXILIARY" ) whsp ] |
| 114 | * ; default structural |
| 115 | * [ "MUST" oids ] ; AttributeTypes |
| 116 | * [ "MAY" oids ] ; AttributeTypes |
| 117 | * whsp ")" |
| 118 | * </PRE> |
| 119 | * |
| 120 | * @version 1.0 |
| 121 | * @see org.ietf.ldap.LDAPSchemaElement |
| 122 | **/ |
| 123 | public class LDAPObjectClassSchema extends LDAPSchemaElement { |
| 124 | |
| 125 | static final long serialVersionUID = -1732784695071118656L; |
| 126 | |
| 127 | /** |
| 128 | * Constructs an object class definition, using the specified |
| 129 | * information. |
| 130 | * @param names names of the object class |
| 131 | * @param oid object identifier (OID) of the object class |
| 132 | * in dotted-string format (for example, "1.2.3.4") |
| 133 | * @param description description of the object class |
| 134 | * @param superiors names of parent object classes |
| 135 | * (the object classes that this object class inherits from) |
| 136 | * @param required array of names of attributes required |
| 137 | * in this object class |
| 138 | * @param optional array of names of optional attributes |
| 139 | * allowed in this object class |
| 140 | * @param type either ABSTRACT, STRUCTURAL, or AUXILIARY |
| 141 | * @param obsolete <code>true</code> if the rule is obsolete |
| 142 | */ |
| 143 | public LDAPObjectClassSchema( String[] names, |
| 144 | String oid, |
| 145 | String[] superiors, |
| 146 | String description, |
| 147 | String[] required, |
| 148 | String[] optional, |
| 149 | int type, |
| 150 | boolean obsolete ) { |
| 151 | super( names, oid, description ); |
| 152 | attrName = "objectclasses"; |
| 153 | setQualifier( SUPERIOR, superiors ); |
| 154 | if ( required != null ) { |
| 155 | for( int i = 0; i < required.length; i++ ) { |
| 156 | must.addElement( required[i] ); |
| 157 | } |
| 158 | } |
| 159 | if ( optional != null ) { |
| 160 | for( int i = 0; i < optional.length; i++ ) { |
| 161 | may.addElement( optional[i] ); |
| 162 | } |
| 163 | } |
| 164 | if ( (superiors != null) && (superiors.length > 1) ) { |
| 165 | setQualifier( SUPERIOR, superiors ); |
| 166 | } |
| 167 | if ( obsolete ) { |
| 168 | setQualifier( OBSOLETE, "" ); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Constructs an object class definition based on a description in |
| 174 | * the ObjectClassDescription format. For information on this format, |
| 175 | * (see <A HREF="http://ds.internic.net/rfc/rfc2252.txt" |
| 176 | * TARGET="_blank">RFC 2252, Lightweight Directory Access Protocol (v3): |
| 177 | * Attribute Syntax Definitions</A>. This is the format that LDAP servers |
| 178 | * and clients use to exchange schema information. (For example, when |
| 179 | * you search an LDAP server for its schema, the server returns an entry |
| 180 | * with the attributes "objectclasses" and "attributetypes". The |
| 181 | * values of the "objectclasses" attribute are object class descriptions |
| 182 | * in this format.) |
| 183 | * <P> |
| 184 | * |
| 185 | * @param raw definition of the object in the ObjectClassDescription |
| 186 | * format |
| 187 | */ |
| 188 | public LDAPObjectClassSchema( String raw ) { |
| 189 | attrName = "objectclasses"; |
| 190 | parseValue( raw ); |
| 191 | setQualifier( TYPE, typeToString( getType() ) ); |
| 192 | Object o = properties.get( "MAY" ); |
| 193 | if ( o != null ) { |
| 194 | if ( o instanceof Vector ) { |
| 195 | may = (Vector<String>)o; |
| 196 | } else { |
| 197 | may.addElement((String)o); |
| 198 | } |
| 199 | } |
| 200 | o = properties.get( "MUST" ); |
| 201 | if ( o != null ) { |
| 202 | if ( o instanceof Vector ) { |
| 203 | must = (Vector<String>)o; |
| 204 | } else { |
| 205 | must.addElement((String)o); |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Gets an enumeration of names of optional attributes allowed |
| 212 | * in this object class. |
| 213 | * @return an enumeration of the names of optional attributes |
| 214 | * allowed in this object class. |
| 215 | */ |
| 216 | public Enumeration<String> getOptionalAttributes() { |
| 217 | return may.elements(); |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Gets an enumeration of the names of the required attributes for |
| 222 | * this object class. |
| 223 | * @return an enumeration of the names of the required attributes |
| 224 | * for this object class. |
| 225 | */ |
| 226 | public Enumeration<String> getRequiredAttributes() { |
| 227 | return must.elements(); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Gets the names of all object classes that this class inherits |
| 232 | * from. Typically only one, but RFC 2252 allows multiple |
| 233 | * inheritance. |
| 234 | * @return the names of the object classes from which this class |
| 235 | * inherits. |
| 236 | */ |
| 237 | public String[] getSuperiors() { |
| 238 | return getQualifier( SUPERIOR ); |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Gets the type of the object class. |
| 243 | * @return STRUCTURAL, ABSTRACT, or AUXILIARY. |
| 244 | */ |
| 245 | public int getType() { |
| 246 | int type = STRUCTURAL; |
| 247 | if ( properties.containsKey( "AUXILIARY" ) ) { |
| 248 | type = AUXILIARY; |
| 249 | } else if ( properties.containsKey( "ABSTRACT" ) ) { |
| 250 | type = ABSTRACT; |
| 251 | } |
| 252 | return type; |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Prepares a value in RFC 2252 format for submitting to a server. |
| 257 | * |
| 258 | * @param quotingBug <CODE>true</CODE> if SUP and SYNTAX values are to |
| 259 | * be quoted. That is to satisfy bugs in certain LDAP servers. |
| 260 | * @return a String ready for submission to an LDAP server. |
| 261 | */ |
| 262 | String getValue( boolean quotingBug ) { |
| 263 | String s = getValuePrefix(); |
| 264 | String val = getValue( SUPERIOR, quotingBug ); |
| 265 | if ( (val != null) && (val.length() > 0) ) { |
| 266 | s += val + ' '; |
| 267 | } |
| 268 | String[] vals = getQualifier( TYPE ); |
| 269 | if ( (vals != null) && (vals.length > 0) ) { |
| 270 | s += vals[0] + ' '; |
| 271 | } |
| 272 | val = getOptionalValues( NOVALS ); |
| 273 | if ( val.length() > 0 ) { |
| 274 | s += val + ' '; |
| 275 | } |
| 276 | if ( must.size() > 0 ) { |
| 277 | s += "MUST " + vectorToList( must ); |
| 278 | s += ' '; |
| 279 | } |
| 280 | if ( may.size() > 0 ) { |
| 281 | s += "MAY " + vectorToList( may ); |
| 282 | s += ' '; |
| 283 | } |
| 284 | val = getCustomValues(); |
| 285 | if ( val.length() > 0 ) { |
| 286 | s += val + ' '; |
| 287 | } |
| 288 | s += ')'; |
| 289 | return s; |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Gets the definition of the object class in Directory format |
| 294 | * |
| 295 | * @return definition of the object class in Directory format |
| 296 | */ |
| 297 | public String toString() { |
| 298 | return getValue(); |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Gets the attribute name for a schema element |
| 303 | * |
| 304 | * @return The attribute name of the element |
| 305 | */ |
| 306 | String getAttributeName() { |
| 307 | return "objectclasses"; |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Creates a list within parentheses, with $ as delimiter |
| 312 | * |
| 313 | * @param vals values for list |
| 314 | * @return a String with a list of values. |
| 315 | */ |
| 316 | protected String vectorToList( Vector<String> vals ) { |
| 317 | String val = "( "; |
| 318 | for( int i = 0; i < vals.size(); i++ ) { |
| 319 | val += vals.elementAt(i) + ' '; |
| 320 | if ( i < (vals.size() - 1) ) { |
| 321 | val += "$ "; |
| 322 | } |
| 323 | } |
| 324 | val += ')'; |
| 325 | return val; |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Returns the object class type as a String |
| 330 | * |
| 331 | * @param type one of STRUCTURAL, ABSTRACT, or AUXILIARY |
| 332 | * @return one of "STRUCTURAL", "ABSTRACT", "AUXILIARY", or <CODE>null</CODE> |
| 333 | */ |
| 334 | protected String typeToString( int type ) { |
| 335 | switch( type ) { |
| 336 | case STRUCTURAL: return "STRUCTURAL"; |
| 337 | case ABSTRACT: return "ABSTRACT"; |
| 338 | case AUXILIARY: return "AUXILIARY"; |
| 339 | default: return null; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | public static final int STRUCTURAL = 0; |
| 344 | public static final int ABSTRACT = 1; |
| 345 | public static final int AUXILIARY = 2; |
| 346 | |
| 347 | private Vector<String> must = new Vector<>(); |
| 348 | private Vector<String> may = new Vector<>(); |
| 349 | private int type = STRUCTURAL; |
| 350 | |
| 351 | // Qualifiers known to not have values; prepare a Hashtable |
| 352 | static final String[] NOVALS = { "ABSTRACT", "STRUCTURAL", |
| 353 | "AUXILIARY", "OBSOLETE" }; |
| 354 | static { |
| 355 | for( int i = 0; i < NOVALS.length; i++ ) { |
| 356 | novalsTable.put( NOVALS[i], NOVALS[i] ); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | // Qualifiers which we output explicitly in toString() |
| 361 | static final String[] IGNOREVALS = { "ABSTRACT", "STRUCTURAL", |
| 362 | "AUXILIARY", "MUST", "MAY", |
| 363 | "SUP", "OBSOLETE"}; |
| 364 | // Key for type in the properties Hashtable |
| 365 | static final String TYPE = "TYPE"; |
| 366 | } |
| 367 |