| 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 | /** |
| 41 | * The definition of an attribute type in the schema. |
| 42 | * <A HREF="http://www.ietf.org/rfc/rfc2252.txt" |
| 43 | * TARGET="_blank">RFC 2252, Lightweight Directory Access Protocol (v3): |
| 44 | * Attribute Syntax Definitions</A> covers the types of information |
| 45 | * to specify when defining an attribute type. According to the RFC, |
| 46 | * the description of an attribute type can include the following: |
| 47 | * <P> |
| 48 | * |
| 49 | * <UL> |
| 50 | * <LI>an OID identifying the attribute type |
| 51 | * <LI>a name identifying the attribute type |
| 52 | * <LI>a description of the attribute type |
| 53 | * <LI>the name of the parent attribute type |
| 54 | * <LI>the syntax used by the attribute (for example, |
| 55 | * <CODE>cis</CODE> or <CODE>int</CODE>) |
| 56 | * <LI>an indication of whether the attribute type is single-valued |
| 57 | * or multi-valued |
| 58 | * </UL> |
| 59 | * <P> |
| 60 | * |
| 61 | * When you construct an <CODE>LDAPAttributeSchema</CODE> object, you can |
| 62 | * specify these types of information as arguments to the constructor or |
| 63 | * in the AttributeTypeDescription format specified in RFC 2252. |
| 64 | * When an LDAP client searches an LDAP server for the schema, the server |
| 65 | * returns schema information as an object with attribute values in this |
| 66 | * format. |
| 67 | * <P> |
| 68 | * |
| 69 | * There a number of additional optional description fields which |
| 70 | * are not explicitly accessible through LDAPAttributeSchema, but which |
| 71 | * can be managed with setQualifier, getQualifier, and getQualifierNames: |
| 72 | * <P> |
| 73 | * |
| 74 | * <UL> |
| 75 | * <LI>EQUALITY |
| 76 | * <LI>ORDERING |
| 77 | * <LI>SUBSTR |
| 78 | * <LI>COLLECTIVE |
| 79 | * <LI>NO-USER-MODIFICATION |
| 80 | * <LI>USAGE |
| 81 | * <LI>OBSOLETE |
| 82 | * </UL> |
| 83 | * <P> |
| 84 | * |
| 85 | * To get the name, OID, and description of this attribute type |
| 86 | * definition, use the <CODE>getName</CODE>, <CODE>getOID</CODE>, and |
| 87 | * <CODE>getDescription</CODE> methods inherited from the abstract class |
| 88 | * <CODE>LDAPSchemaElement</CODE>. Optional and custom qualifiers are |
| 89 | * accessed with <CODE>getQualifier</CODE> and <CODE>getQualifierNames</CODE> |
| 90 | * from <CODE>LDAPSchemaElement</CODE>. |
| 91 | * <P> |
| 92 | * |
| 93 | * To add or remove this attribute type definition from the |
| 94 | * schema, use the <CODE>add</CODE> and <CODE>remove</CODE> |
| 95 | * methods, which this class inherits from the <CODE>LDAPSchemaElement</CODE> |
| 96 | * abstract class. |
| 97 | * <P> |
| 98 | * RFC 2252 defines AttributeTypeDescription as follows: |
| 99 | * <P> |
| 100 | * <PRE> |
| 101 | * AttributeTypeDescription = "(" whsp |
| 102 | * numericoid whsp ; AttributeType identifier |
| 103 | * [ "NAME" qdescrs ] ; name used in AttributeType |
| 104 | * [ "DESC" qdstring ] ; description |
| 105 | * [ "OBSOLETE" whsp ] |
| 106 | * [ "SUP" woid ] ; derived from this other |
| 107 | * ; AttributeType |
| 108 | * [ "EQUALITY" woid ; Matching Rule name |
| 109 | * [ "ORDERING" woid ; Matching Rule name |
| 110 | * [ "SUBSTR" woid ] ; Matching Rule name |
| 111 | * [ "SYNTAX" whsp noidlen whsp ] ; see section 4.3 |
| 112 | * [ "SINGLE-VALUE" whsp ] ; default multi-valued |
| 113 | * [ "COLLECTIVE" whsp ] ; default not collective |
| 114 | * [ "NO-USER-MODIFICATION" whsp ]; default user modifiable |
| 115 | * [ "USAGE" whsp AttributeUsage ]; default userApplications |
| 116 | * whsp ")" |
| 117 | * |
| 118 | * AttributeUsage = |
| 119 | * "userApplications" / |
| 120 | * "directoryOperation" / |
| 121 | * "distributedOperation" / ; DSA-shared |
| 122 | * "dSAOperation" ; DSA-specific, value depends on server |
| 123 | * </PRE> |
| 124 | * |
| 125 | * @version 1.0 |
| 126 | * @see org.ietf.ldap.LDAPSchemaElement |
| 127 | **/ |
| 128 | |
| 129 | public class LDAPAttributeSchema extends LDAPSchemaElement { |
| 130 | |
| 131 | static final long serialVersionUID = 2482595821879862595L; |
| 132 | |
| 133 | /** |
| 134 | * Constructs a blank element. |
| 135 | */ |
| 136 | protected LDAPAttributeSchema() { |
| 137 | super(); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Constructs an attribute type definition, using the specified |
| 142 | * information |
| 143 | * @param names names of the attribute type |
| 144 | * @param oid object identifier (OID) of the attribute type |
| 145 | * in dotted-string format (for example, "1.2.3.4") |
| 146 | * @param description description of attribute type |
| 147 | * @param syntaxString syntax of this attribute type in dotted-string |
| 148 | * format (for example, "1.2.3.4.5") |
| 149 | * @param single <CODE>true</CODE> if the attribute type is single-valued |
| 150 | * @param superior superior attribute as a name or OID; <CODE>null</CODE> |
| 151 | * if there is no superior |
| 152 | * @param obsolete true if this attribute is obsolete |
| 153 | * @param equality Object Identifier of the equality matching rule |
| 154 | * for the attribute in dotted-decimal format; MAY be null |
| 155 | * @param ordering Object Identifier of the ordering matching rule |
| 156 | * for the attribute in dotted-decimal format; MAY be null |
| 157 | * @param substring Object Identifier of the substring matching rule |
| 158 | * for the attribute in dotted-decimal format; MAY be null |
| 159 | * @param collective true if this is a collective attribute. |
| 160 | * @param userMod true if the attribute is modifiable by users. |
| 161 | * @param usage One of the following: |
| 162 | * <UL> |
| 163 | * <LI>USER_APPLICATIONS |
| 164 | * <LI>DIRECTORY_OPERATION |
| 165 | * <LI>DISTRIBUTED_OPERATION |
| 166 | * <LI>DSA_OPERATION |
| 167 | * </UL> |
| 168 | */ |
| 169 | public LDAPAttributeSchema( String[] names, |
| 170 | String oid, |
| 171 | String description, |
| 172 | String syntaxString, |
| 173 | boolean single, |
| 174 | String superior, |
| 175 | boolean obsolete, |
| 176 | String equality, |
| 177 | String ordering, |
| 178 | String substring, |
| 179 | String collective, |
| 180 | boolean userMod, |
| 181 | int usage ) { |
| 182 | super( names, oid, description ); |
| 183 | attrName = "attributetypes"; |
| 184 | syntaxElement.syntax = syntaxElement.syntaxCheck( syntaxString ); |
| 185 | syntaxElement.syntaxString = syntaxString; |
| 186 | setQualifier( SYNTAX, syntaxElement.syntaxString ); |
| 187 | if ( single ) { |
| 188 | setQualifier( SINGLE, "" ); |
| 189 | } |
| 190 | if ( (superior != null) && (superior.length() > 0) ) { |
| 191 | setQualifier( SUPERIOR, superior ); |
| 192 | } |
| 193 | if ( obsolete ) { |
| 194 | setQualifier( OBSOLETE, "" ); |
| 195 | } |
| 196 | if ( (equality != null) && (equality.length() > 0) ) { |
| 197 | setQualifier( EQUALITY, equality ); |
| 198 | } |
| 199 | if ( (ordering != null) && (ordering.length() > 0) ) { |
| 200 | setQualifier( ORDERING, ordering ); |
| 201 | } |
| 202 | if ( (substring != null) && (substring.length() > 0) ) { |
| 203 | setQualifier( SUBSTR, substring ); |
| 204 | } |
| 205 | if ( (collective != null) && (collective.length() > 0) ) { |
| 206 | setQualifier( COLLECTIVE, collective ); |
| 207 | } |
| 208 | if ( !userMod ) { |
| 209 | setQualifier( NO_USER_MODIFICATION, "" ); |
| 210 | } |
| 211 | setQualifier( USAGE, String.valueOf( usage ) ); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Constructs an attribute type definition based on a description in |
| 216 | * the AttributeTypeDescription format. For information on this format, |
| 217 | * (see <A HREF="http://www.ietf.org/rfc/rfc2252.txt" |
| 218 | * TARGET="_blank">RFC 2252, Lightweight Directory Access Protocol (v3): |
| 219 | * Attribute Syntax Definitions</A>. This is the format that LDAP servers |
| 220 | * and clients use to exchange schema information. (For example, when |
| 221 | * you search an LDAP server for its schema, the server returns an entry |
| 222 | * with the attributes "objectclasses" and "attributetypes". The |
| 223 | * values of "attributetypes" are attribute type descriptions |
| 224 | * in this format.) |
| 225 | * <P> |
| 226 | * |
| 227 | * @param raw definition of the attribute type in the |
| 228 | * AttributeTypeDescription format |
| 229 | */ |
| 230 | public LDAPAttributeSchema( String raw ) { |
| 231 | attrName = "attributetypes"; |
| 232 | parseValue( raw ); |
| 233 | String val = (String)properties.get( SYNTAX ); |
| 234 | if ( val != null ) { |
| 235 | syntaxElement.syntaxString = val; |
| 236 | syntaxElement.syntax = syntaxElement.syntaxCheck( val ); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Returns the value of a property of the object or null if there is none |
| 242 | * |
| 243 | * @param key the name of the property |
| 244 | * @return the value of a property of the object or null if there is none |
| 245 | */ |
| 246 | private String getStringProperty( String key ) { |
| 247 | String[] val = getQualifier( key ); |
| 248 | return ((val != null) && (val.length > 0)) ? val[0] : null; |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Returns the Object Identifier of the equality matching rule in effect |
| 253 | * for this attribute, or null if there is none |
| 254 | * @return the Object Identifier of the equality matching rule in effect |
| 255 | * for this attribute, or null if there is none |
| 256 | */ |
| 257 | public String getEqualityMatchingRule() { |
| 258 | return getStringProperty( EQUALITY ); |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Returns the Object Identifier of the order matching rule in effect |
| 263 | * for this attribute, or null if there is none |
| 264 | * @return the Object Identifier of the order matching rule in effect |
| 265 | * for this attribute, or null if there is none |
| 266 | */ |
| 267 | public String getOrderMatchingRule() { |
| 268 | return getStringProperty( ORDERING ); |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Returns the Object Identifier of the substring matching rule in effect |
| 273 | * for this attribute, or null if there is none |
| 274 | * @return the Object Identifier of the substring matching rule in effect |
| 275 | * for this attribute, or null if there is none |
| 276 | */ |
| 277 | public String getSubstringMatchingRule() { |
| 278 | return getStringProperty( SUBSTR ); |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Gets the name of the attribute that this attribute inherits from, |
| 283 | * if any. |
| 284 | * @return the name of the attribute from which this attribute |
| 285 | * inherits, or <CODE>null</CODE> if it does not have a superior. |
| 286 | */ |
| 287 | public String getSuperior() { |
| 288 | return getStringProperty( SUPERIOR ); |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Gets the syntax of the schema element |
| 293 | * @return one of the following values: |
| 294 | * <UL> |
| 295 | * <LI><CODE>cis</CODE> (case-insensitive string) |
| 296 | * <LI><CODE>ces</CODE> (case-exact string) |
| 297 | * <LI><CODE>binary</CODE> (binary data) |
| 298 | * <LI><CODE>int</CODE> (integer) |
| 299 | * <LI><CODE>telephone</CODE> (telephone number -- identical to cis, |
| 300 | * but blanks and dashes are ignored during comparisons) |
| 301 | * <LI><CODE>dn</CODE> (distinguished name) |
| 302 | * <LI><CODE>unknown</CODE> (not a known syntax) |
| 303 | * </UL> |
| 304 | * @deprecated use getSyntaxString instead |
| 305 | */ |
| 306 | public int getSyntax() { |
| 307 | return syntaxElement.syntax; |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Gets the usage property of the object |
| 312 | * |
| 313 | * @return the usage property of the object; one of the following: |
| 314 | * <UL> |
| 315 | * <LI>USER_APPLICATIONS |
| 316 | * <LI>DIRECTORY_OPERATION |
| 317 | * <LI>DISTRIBUTED_OPERATION |
| 318 | * <LI>DSA_OPERATION |
| 319 | * </UL> |
| 320 | */ |
| 321 | public int getUsage() { |
| 322 | int usage; |
| 323 | try { |
| 324 | usage = Integer.parseInt( getStringProperty( USAGE ) ); |
| 325 | } catch ( Exception e ) { |
| 326 | usage = USER_APPLICATIONS; |
| 327 | } |
| 328 | return usage; |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Gets the syntax of the attribute type in dotted-decimal format, |
| 333 | * for example "1.2.3.4.5" |
| 334 | * @return The attribute syntax in dotted-decimal format. |
| 335 | */ |
| 336 | public String getSyntaxString() { |
| 337 | return syntaxElement.syntaxString; |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * Prepares a value in RFC 2252 format for submission to a server |
| 342 | * |
| 343 | * @param quotingBug <CODE>true</CODE> if SUP and SYNTAX values are to |
| 344 | * be quoted. This is required to work with bugs in certain LDAP servers. |
| 345 | * @return a String ready for submission to an LDAP server. |
| 346 | */ |
| 347 | String getValue( boolean quotingBug ) { |
| 348 | String s = getValuePrefix(); |
| 349 | String val = getValue( SUPERIOR, false ); |
| 350 | if ( val.length() > 0 ) { |
| 351 | s += val + ' '; |
| 352 | } |
| 353 | val = getOptionalValues( MATCHING_RULES ); |
| 354 | if ( val.length() > 0 ) { |
| 355 | s += val + ' '; |
| 356 | } |
| 357 | val = getValue( SYNTAX, quotingBug ); |
| 358 | if ( val.length() > 0 ) { |
| 359 | s += val + ' '; |
| 360 | } |
| 361 | if ( isSingleValued() ) { |
| 362 | s += SINGLE + ' '; |
| 363 | } |
| 364 | val = getOptionalValues( NOVALS ); |
| 365 | if ( val.length() > 0 ) { |
| 366 | s += val + ' '; |
| 367 | } |
| 368 | val = getOptionalValues( new String[] {USAGE} ); |
| 369 | if ( val.length() > 0 ) { |
| 370 | s += val + ' '; |
| 371 | } |
| 372 | val = getCustomValues(); |
| 373 | if ( val.length() > 0 ) { |
| 374 | s += val + ' '; |
| 375 | } |
| 376 | s += ')'; |
| 377 | return s; |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * Determines if the attribute type is collective |
| 382 | * |
| 383 | * @return <code>true</code> if collective |
| 384 | */ |
| 385 | public boolean isCollective() { |
| 386 | return properties != null ? properties.containsKey( COLLECTIVE ) : false; |
| 387 | } |
| 388 | |
| 389 | /** |
| 390 | * Determines if the attribute type is single-valued |
| 391 | * |
| 392 | * @return <code>true</code> if single-valued, |
| 393 | * <code>false</code> if multi-valued. |
| 394 | */ |
| 395 | public boolean isSingleValued() { |
| 396 | return properties != null ? properties.containsKey( SINGLE ) : false; |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * Determines if the attribute type is modifiable by users |
| 401 | * |
| 402 | * @return <code>true</code> if modifiable by users |
| 403 | */ |
| 404 | public boolean isUserModifiable() { |
| 405 | return properties != null ? !properties.containsKey( NO_USER_MODIFICATION ) : true; |
| 406 | } |
| 407 | |
| 408 | /** |
| 409 | * Gets the definition of the attribute type in Directory format |
| 410 | * |
| 411 | * @return definition of the attribute type in Directory format |
| 412 | */ |
| 413 | public String toString() { |
| 414 | return getValue( false ); |
| 415 | } |
| 416 | |
| 417 | /** |
| 418 | * Gets the attribute name for a schema element |
| 419 | * |
| 420 | * @return The attribute name of the element |
| 421 | */ |
| 422 | String getAttributeName() { |
| 423 | return "attributetypes"; |
| 424 | } |
| 425 | |
| 426 | // Predefined qualifiers |
| 427 | public static final String EQUALITY = "EQUALITY"; |
| 428 | public static final String ORDERING = "ORDERING"; |
| 429 | public static final String SUBSTR = "SUBSTR"; |
| 430 | public static final String SINGLE = "SINGLE-VALUE"; |
| 431 | public static final String COLLECTIVE = "COLLECTIVE"; |
| 432 | public static final String NO_USER_MODIFICATION = "NO-USER-MODIFICATION"; |
| 433 | public static final String USAGE = "USAGE"; |
| 434 | |
| 435 | // Usage constants |
| 436 | /** An ordinary user attribute |
| 437 | */ |
| 438 | public static final int USER_APPLICATIONS = 0; |
| 439 | |
| 440 | /** An operational attribute used for a directory operation or which |
| 441 | * holds a directory specific value |
| 442 | */ |
| 443 | public static final int DIRECTORY_OPERATION = 1; |
| 444 | |
| 445 | /** An operational attribute used to hold server (DSA) information that |
| 446 | * is shared among servers holding replicas of the entry |
| 447 | */ |
| 448 | public static final int DISTRIBUTED_OPERATION = 2; |
| 449 | |
| 450 | /** An operational attribute used to hold server (DSA) information that |
| 451 | * is local to a server |
| 452 | */ |
| 453 | public static final int DSA_OPERATION = 3; |
| 454 | |
| 455 | // Qualifiers known to not have values; prepare a Hashtable |
| 456 | static String[] NOVALS = { SINGLE, |
| 457 | COLLECTIVE, |
| 458 | NO_USER_MODIFICATION }; |
| 459 | static { |
| 460 | for( int i = 0; i < NOVALS.length; i++ ) { |
| 461 | novalsTable.put( NOVALS[i], NOVALS[i] ); |
| 462 | } |
| 463 | } |
| 464 | static final String[] MATCHING_RULES = { EQUALITY, |
| 465 | ORDERING, |
| 466 | SUBSTR }; |
| 467 | // Qualifiers which we output explicitly in toString() |
| 468 | static final String[] IGNOREVALS = { SINGLE, |
| 469 | OBSOLETE, |
| 470 | SUPERIOR, |
| 471 | SINGLE, |
| 472 | COLLECTIVE, |
| 473 | NO_USER_MODIFICATION, |
| 474 | SYNTAX}; |
| 475 | |
| 476 | protected LDAPSyntaxSchemaElement syntaxElement = |
| 477 | new LDAPSyntaxSchemaElement(); |
| 478 | } |