| 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) 2001 |
| 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.Vector; |
| 41 | |
| 42 | /** |
| 43 | * The definition of a DIT content rule in the schema. |
| 44 | * <A HREF="http://www.ietf.org/rfc/rfc2252.txt" |
| 45 | * TARGET="_blank">RFC 2252, Lightweight Directory Access Protocol (v3): |
| 46 | * DIT Content Rule Description</A> covers the types of information |
| 47 | * to specify when defining a DIT content rule. According to the RFC, |
| 48 | * the description of a DIT content rule can include the following: |
| 49 | * <P> |
| 50 | * |
| 51 | * <UL> |
| 52 | * <LI>an OID identifying the attribute type |
| 53 | * <LI>a name identifying the attribute type |
| 54 | * <LI>a description of the attribute type |
| 55 | * <LI>the name of the parent attribute type |
| 56 | * <LI>the syntax used by the attribute (for example, |
| 57 | * <CODE>cis</CODE> or <CODE>int</CODE>) |
| 58 | * <LI>an indication of whether the attribute type is single-valued |
| 59 | * or multi-valued |
| 60 | * </UL> |
| 61 | * <P> |
| 62 | * |
| 63 | * When you construct an <CODE>LDAPDITContentRuleSchema</CODE> object, you can |
| 64 | * specify these types of information as arguments to the constructor or |
| 65 | * in the AttributeTypeDescription format specified in RFC 2252. |
| 66 | * When an LDAP client searches an LDAP server for the schema, the server |
| 67 | * returns schema information as an object with attribute values in this |
| 68 | * format. |
| 69 | * <P> |
| 70 | * |
| 71 | * There are a number of additional optional description fields which |
| 72 | * are not explicitly accessible through LDAPDITContentRuleSchema, but which |
| 73 | * can be managed with setQualifier, getQualifier, and getQualifierNames: |
| 74 | * <P> |
| 75 | * |
| 76 | * <UL> |
| 77 | * <LI>OBSOLETE |
| 78 | * </UL> |
| 79 | * <P> |
| 80 | * |
| 81 | * To get the name, OID, and description of this DIT content rule |
| 82 | * , use the <CODE>getName</CODE>, <CODE>getOID</CODE>, and |
| 83 | * <CODE>getDescription</CODE> methods inherited from the abstract class |
| 84 | * <CODE>LDAPSchemaElement</CODE>. Optional and custom qualifiers are |
| 85 | * accessed with <CODE>getQualifier</CODE> and <CODE>getQualifierNames</CODE> |
| 86 | * from <CODE>LDAPSchemaElement</CODE>. |
| 87 | * <P> |
| 88 | * |
| 89 | * To add or remove this attribute type definition from the |
| 90 | * schema, use the <CODE>add</CODE> and <CODE>remove</CODE> |
| 91 | * methods, which this class inherits from the <CODE>LDAPSchemaElement</CODE> |
| 92 | * abstract class. |
| 93 | * <P> |
| 94 | * RFC 2252 defines DITContentRuleDescription as follows: |
| 95 | * <P> |
| 96 | * <PRE> |
| 97 | * DITContentRuleDescription = "(" |
| 98 | * numericoid ; Structural ObjectClass identifier |
| 99 | * [ "NAME" qdescrs ] |
| 100 | * [ "DESC" qdstring ] |
| 101 | * [ "OBSOLETE" ] |
| 102 | * [ "AUX" oids ] ; Auxiliary ObjectClasses |
| 103 | * [ "MUST" oids ] ; AttributeType identifiers |
| 104 | * [ "MAY" oids ] ; AttributeType identifiers |
| 105 | * [ "NOT" oids ] ; AttributeType identifiers |
| 106 | * ")" |
| 107 | * </PRE> |
| 108 | * |
| 109 | * @version 1.0 |
| 110 | * @see org.ietf.ldap.LDAPSchemaElement |
| 111 | **/ |
| 112 | |
| 113 | public class LDAPDITContentRuleSchema extends LDAPSchemaElement { |
| 114 | |
| 115 | static final long serialVersionUID = -8588488481097270056L; |
| 116 | |
| 117 | /** |
| 118 | * Constructs a blank element. |
| 119 | */ |
| 120 | protected LDAPDITContentRuleSchema() { |
| 121 | super(); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Constructs a DIT content rule definition, using the specified |
| 126 | * information. |
| 127 | * @param names names of the attribute type |
| 128 | * @param oid object identifier (OID) of the attribute type |
| 129 | * in dotted-string format (for example, "1.2.3.4") |
| 130 | * @param description description of attribute type |
| 131 | * @param obsolete <code>true</code> if the rule is obsolete |
| 132 | * @param auxiliary a list of auxiliary object classes |
| 133 | * allowed for an entry to which this content rule applies. |
| 134 | * These may either be specified by name or numeric oid. |
| 135 | * @param required a list of user attribute types that an entry |
| 136 | * to which this content rule applies must contain in addition to |
| 137 | * its normal set of mandatory attributes. These may either be |
| 138 | * specified by name or numeric oid. |
| 139 | * @param optional a list of user attribute types that an entry |
| 140 | * to which this content rule applies may contain in addition to |
| 141 | * its normal set of optional attributes. These may either be |
| 142 | * specified by name or numeric oid. |
| 143 | * @param precluded a list consisting of a subset of the optional |
| 144 | * user attribute types of the structural and auxiliary object |
| 145 | * classes which are precluded from an entry to which this content rule |
| 146 | * applies. These may either be specified by name or numeric oid. |
| 147 | */ |
| 148 | public LDAPDITContentRuleSchema( String[] names, |
| 149 | String oid, |
| 150 | String description, |
| 151 | boolean obsolete, |
| 152 | String[] auxiliary, |
| 153 | String[] required, |
| 154 | String[] optional, |
| 155 | String[] precluded ) { |
| 156 | super( names, oid, description ); |
| 157 | if ( required != null ) { |
| 158 | for( int i = 0; i < required.length; i++ ) { |
| 159 | must.addElement( required[i] ); |
| 160 | } |
| 161 | } |
| 162 | if ( optional != null ) { |
| 163 | for( int i = 0; i < optional.length; i++ ) { |
| 164 | may.addElement( optional[i] ); |
| 165 | } |
| 166 | } |
| 167 | if ( auxiliary != null ) { |
| 168 | for( int i = 0; i < auxiliary.length; i++ ) { |
| 169 | aux.addElement( auxiliary[i] ); |
| 170 | } |
| 171 | } |
| 172 | if ( precluded != null ) { |
| 173 | for( int i = 0; i < precluded.length; i++ ) { |
| 174 | not.addElement( precluded[i] ); |
| 175 | } |
| 176 | } |
| 177 | if ( obsolete ) { |
| 178 | setQualifier( OBSOLETE, "" ); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Constructs a DIT content rule definition based on a description in |
| 184 | * the DITContentRuleDescription format. For information on this format, |
| 185 | * (see <A HREF="http://www.ietf.org/rfc/rfc2252.txt" |
| 186 | * >RFC 2252, Lightweight Directory Access Protocol (v3): |
| 187 | * DIT Content Rule Description</A>. This is the format that LDAP servers |
| 188 | * and clients use to exchange schema information. (For example, when |
| 189 | * you search an LDAP server for its schema, the server returns an entry |
| 190 | * with the attributes "objectclasses" and "attributetypes". The |
| 191 | * values of "attributetypes" are attribute type descriptions |
| 192 | * in this format.) |
| 193 | * <P> |
| 194 | * |
| 195 | * @param raw definition of the DIT content rule in the |
| 196 | * DITContentRuleDescription format |
| 197 | */ |
| 198 | public LDAPDITContentRuleSchema( String raw ) { |
| 199 | attrName = "ditContentRules"; |
| 200 | parseValue( raw ); |
| 201 | Object o = properties.get( MAY ); |
| 202 | if ( o != null ) { |
| 203 | if ( o instanceof Vector ) { |
| 204 | may = (Vector<String>)o; |
| 205 | } else { |
| 206 | may.addElement((String)o); |
| 207 | } |
| 208 | } |
| 209 | o = properties.get( MUST ); |
| 210 | if ( o != null ) { |
| 211 | if ( o instanceof Vector ) { |
| 212 | must = (Vector<String>)o; |
| 213 | } else { |
| 214 | must.addElement((String)o); |
| 215 | } |
| 216 | } |
| 217 | o = properties.get( NOT ); |
| 218 | if ( o != null ) { |
| 219 | if ( o instanceof Vector ) { |
| 220 | not = (Vector<String>)o; |
| 221 | } else { |
| 222 | not.addElement((String)o); |
| 223 | } |
| 224 | } |
| 225 | o = properties.get( AUX ); |
| 226 | if ( o != null ) { |
| 227 | if ( o instanceof Vector ) { |
| 228 | aux = (Vector<String>)o; |
| 229 | } else { |
| 230 | aux.addElement((String)o); |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Gets the names of the auxiliary object classes allowed |
| 237 | * in this content rule. |
| 238 | * @return the names of auxiliary object classes |
| 239 | * allowed in this content rule. |
| 240 | */ |
| 241 | public String[] getAuxiliaryClasses() { |
| 242 | String[] vals = new String[aux.size()]; |
| 243 | aux.copyInto( vals ); |
| 244 | return vals; |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Gets the names of optional attributes allowed |
| 249 | * in this content rule. |
| 250 | * @return the names of optional attributes |
| 251 | * allowed in this content rule. |
| 252 | */ |
| 253 | public String[] getOptionalAttributes() { |
| 254 | String[] vals = new String[may.size()]; |
| 255 | may.copyInto( vals ); |
| 256 | return vals; |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Gets the names of the precluded attributes for |
| 261 | * this content rule. |
| 262 | * @return the names of the precluded attributes |
| 263 | * for this content rule. |
| 264 | */ |
| 265 | public String[] getPrecludedAttributes() { |
| 266 | String[] vals = new String[not.size()]; |
| 267 | not.copyInto( vals ); |
| 268 | return vals; |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Gets the names of the required attributes for |
| 273 | * this content rule. |
| 274 | * @return the names of the required attributes |
| 275 | * for this content rule. |
| 276 | */ |
| 277 | public String[] getRequiredAttributes() { |
| 278 | String[] vals = new String[must.size()]; |
| 279 | must.copyInto( vals ); |
| 280 | return vals; |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Gets the attribute name for a schema element |
| 285 | * |
| 286 | * @return The attribute name of the element |
| 287 | */ |
| 288 | String getAttributeName() { |
| 289 | return "ldapditcontentrules"; |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Prepares a value in RFC 2252 format for submission to a server |
| 294 | * |
| 295 | * @return a String ready for submission to an LDAP server. |
| 296 | */ |
| 297 | String getValue() { |
| 298 | String s = getValuePrefix(); |
| 299 | String val = getOptionalValues( NOVALS ); |
| 300 | if ( val.length() > 0 ) { |
| 301 | s += val + ' '; |
| 302 | } |
| 303 | if ( aux.size() > 0 ) { |
| 304 | s += AUX + " " + vectorToList( aux ); |
| 305 | s += ' '; |
| 306 | } |
| 307 | if ( must.size() > 0 ) { |
| 308 | s += MUST + " " + vectorToList( must ); |
| 309 | s += ' '; |
| 310 | } |
| 311 | if ( may.size() > 0 ) { |
| 312 | s += MAY + " " + vectorToList( may ); |
| 313 | s += ' '; |
| 314 | } |
| 315 | if ( not.size() > 0 ) { |
| 316 | s += NOT + " " + vectorToList( not ); |
| 317 | s += ' '; |
| 318 | } |
| 319 | val = getCustomValues(); |
| 320 | if ( val.length() > 0 ) { |
| 321 | s += val + ' '; |
| 322 | } |
| 323 | s += ')'; |
| 324 | return s; |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Creates a list within parentheses, with $ as delimiter |
| 329 | * |
| 330 | * @param vals values for list |
| 331 | * @return a String with a list of values. |
| 332 | */ |
| 333 | protected String vectorToList( Vector<String> vals ) { |
| 334 | String val = "( "; |
| 335 | for( int i = 0; i < vals.size(); i++ ) { |
| 336 | val += vals.elementAt(i) + ' '; |
| 337 | if ( i < (vals.size() - 1) ) { |
| 338 | val += "$ "; |
| 339 | } |
| 340 | } |
| 341 | val += ')'; |
| 342 | return val; |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * Gets the definition of the rule in Directory format |
| 347 | * |
| 348 | * @return definition of the rule in Directory format |
| 349 | */ |
| 350 | public String toString() { |
| 351 | return getValue(); |
| 352 | } |
| 353 | |
| 354 | public final static String AUX = "AUX"; |
| 355 | public final static String MUST = "MUST"; |
| 356 | public final static String MAY = "MAY"; |
| 357 | public final static String NOT = "NOT"; |
| 358 | |
| 359 | // Qualifiers known to not have values; prepare a Hashtable |
| 360 | static final String[] NOVALS = { "OBSOLETE" }; |
| 361 | static { |
| 362 | for( int i = 0; i < NOVALS.length; i++ ) { |
| 363 | novalsTable.put( NOVALS[i], NOVALS[i] ); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | // Qualifiers which we output explicitly in toString() |
| 368 | static final String[] IGNOREVALS = { OBSOLETE, |
| 369 | AUX, |
| 370 | MUST, |
| 371 | MAY, |
| 372 | NOT |
| 373 | }; |
| 374 | |
| 375 | private Vector<String> must = new Vector<>(); |
| 376 | private Vector<String> may = new Vector<>(); |
| 377 | private Vector<String> aux = new Vector<>(); |
| 378 | private Vector<String> not = new Vector<>(); |
| 379 | } |