| 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) 2000 |
| 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 name form in the schema. |
| 44 | * <A HREF="http://ds.internic.net/rfc/rfc2252.txt" |
| 45 | * TARGET="_blank">RFC 2252, Lightweight Directory Access Protocol (v3): |
| 46 | * Attribute Syntax Definitions</A> covers the types of information |
| 47 | * that need to be specified in the definition of a name form. |
| 48 | * According to the RFC, the description of a name form can |
| 49 | * include the following information: |
| 50 | * <P> |
| 51 | * |
| 52 | * <UL> |
| 53 | * <LI>an OID identifying the name form |
| 54 | * <LI>a name identifying the name form |
| 55 | * <LI>a description of the name form |
| 56 | * <LI>the structural object class of this name form |
| 57 | * <LI>the list of attribute types that are required in this name form |
| 58 | * <LI>the list of attribute types that are allowed (optional) in this |
| 59 | * name form |
| 60 | * </UL> |
| 61 | * <P> |
| 62 | * |
| 63 | * When you construct an <CODE>LDAPNameFormSchema</CODE> object, |
| 64 | * you can specify |
| 65 | * these types of information as arguments to the constructor or in the |
| 66 | * NameFormDescription 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 | * You can get the name, OID, and description of this name form |
| 73 | * definition by using the <CODE>getName</CODE>, <CODE>getOID</CODE>, and |
| 74 | * <CODE>getDescription</CODE> methods inherited from the abstract class |
| 75 | * <CODE>LDAPSchemaElement</CODE>. Optional and custom qualifiers are |
| 76 | * accessed with <CODE>getQualifier</CODE> and <CODE>getQualifierNames</CODE> |
| 77 | * from <CODE>LDAPSchemaElement</CODE>. |
| 78 | |
| 79 | * <P> |
| 80 | * |
| 81 | * To add or remove this name form definition from the |
| 82 | * schema, use the <CODE>add</CODE> and <CODE>remove</CODE> |
| 83 | * methods, which this class inherits from the <CODE>LDAPSchemaElement</CODE> |
| 84 | * abstract class. |
| 85 | * <P> |
| 86 | * RFC 2252 defines NameFormDescription as follows: |
| 87 | * <P> |
| 88 | * <PRE> |
| 89 | * NameFormDescription = "(" whsp |
| 90 | * numericoid whsp ; NameForm identifier |
| 91 | * [ "NAME" qdescrs ] |
| 92 | * [ "DESC" qdstring ] |
| 93 | * [ "OBSOLETE" whsp ] |
| 94 | * "OC" woid ; Structural ObjectClass |
| 95 | * [ "MUST" oids ] ; AttributeTypes |
| 96 | * [ "MAY" oids ] ; AttributeTypes |
| 97 | * whsp ")" |
| 98 | * </PRE> |
| 99 | * |
| 100 | * @version 1.0 |
| 101 | * @see org.ietf.ldap.LDAPSchemaElement |
| 102 | **/ |
| 103 | public class LDAPNameFormSchema extends LDAPSchemaElement { |
| 104 | |
| 105 | static final long serialVersionUID = 1665316286199590403L; |
| 106 | |
| 107 | /** |
| 108 | * Constructs a name form definition, using the specified |
| 109 | * information. |
| 110 | * @param names name of the name form |
| 111 | * @param oid object identifier (OID) of the name form |
| 112 | * in dotted-string format (for example, "1.2.3.4") |
| 113 | * @param description description of the name form |
| 114 | * @param obsolete <code>true</code> if the rule is obsolete |
| 115 | * @param objectClass the object to which this name form applies. |
| 116 | * This may either be specified by name or numeric oid. |
| 117 | * @param required array of names of attributes required |
| 118 | * in this name form |
| 119 | * @param optional array of names of optional attributes |
| 120 | * allowed in this name form |
| 121 | */ |
| 122 | public LDAPNameFormSchema( String[] names, |
| 123 | String oid, |
| 124 | String description, |
| 125 | boolean obsolete, |
| 126 | String objectClass, |
| 127 | String[] required, |
| 128 | String[] optional ) { |
| 129 | super( names, oid, description ); |
| 130 | attrName = "nameforms"; |
| 131 | if ( obsolete ) { |
| 132 | setQualifier( OBSOLETE, "" ); |
| 133 | } |
| 134 | this.objectClass = objectClass; |
| 135 | if ( required != null ) { |
| 136 | for( int i = 0; i < required.length; i++ ) { |
| 137 | must.addElement( required[i] ); |
| 138 | } |
| 139 | } |
| 140 | if ( optional != null ) { |
| 141 | for( int i = 0; i < optional.length; i++ ) { |
| 142 | may.addElement( optional[i] ); |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Constructs a name form definition based on a description in |
| 149 | * the NameFormDescription format. For information on this format, |
| 150 | * (see <A HREF="http://ds.internic.net/rfc/rfc2252.txt" |
| 151 | * TARGET="_blank">RFC 2252, Lightweight Directory Access Protocol (v3): |
| 152 | * Attribute Syntax Definitions</A>. This is the format that LDAP servers |
| 153 | * and clients use to exchange schema information. (For example, when |
| 154 | * you search an LDAP server for its schema, the server returns an entry |
| 155 | * with the attributes "objectclasses" and "attributetypes". The |
| 156 | * values of the "objectclasses" attribute are name form descriptions |
| 157 | * in this format.) |
| 158 | * <P> |
| 159 | * |
| 160 | * @param raw definition of the object in the NameFormDescription |
| 161 | * format |
| 162 | */ |
| 163 | public LDAPNameFormSchema( String raw ) { |
| 164 | attrName = "objectclasses"; |
| 165 | parseValue( raw ); |
| 166 | Object o = properties.get( "MAY" ); |
| 167 | if ( o != null ) { |
| 168 | if ( o instanceof Vector ) { |
| 169 | may = (Vector<String>)o; |
| 170 | } else { |
| 171 | may.addElement( (String)o ); |
| 172 | } |
| 173 | } |
| 174 | o = properties.get( "MUST" ); |
| 175 | if ( o != null ) { |
| 176 | if ( o instanceof Vector ) { |
| 177 | must = (Vector<String>)o; |
| 178 | } else { |
| 179 | must.addElement( (String)o ); |
| 180 | } |
| 181 | } |
| 182 | o = properties.get( "OC" ); |
| 183 | if ( o != null ) { |
| 184 | objectClass = (String)o; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Returns the name of the object class that this name form applies to. |
| 190 | * |
| 191 | * @return the name of the object class that this name form applies to. |
| 192 | */ |
| 193 | public String getObjectClass() { |
| 194 | return objectClass; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Gets the names of optional attributes allowed |
| 199 | * in this name form. |
| 200 | * @return the names of optional attributes |
| 201 | * allowed in this name form. |
| 202 | */ |
| 203 | public String[] getOptionalNamingAttributes() { |
| 204 | String[] vals = new String[may.size()]; |
| 205 | may.copyInto( vals ); |
| 206 | return vals; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Gets the names of the required attributes for |
| 211 | * this name form. |
| 212 | * @return the names of the required attributes |
| 213 | * for this name form. |
| 214 | */ |
| 215 | public String[] getRequiredNamingAttributes() { |
| 216 | String[] vals = new String[must.size()]; |
| 217 | must.copyInto( vals ); |
| 218 | return vals; |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Gets the attribute name for a schema element |
| 223 | * |
| 224 | * @return The attribute name of the element |
| 225 | */ |
| 226 | String getAttributeName() { |
| 227 | return "nameforms"; |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Prepares a value in RFC 2252 format for submitting to a server. |
| 232 | * |
| 233 | * @param quotingBug <CODE>true</CODE> if SUP and SYNTAX values are to |
| 234 | * be quoted. That is to satisfy bugs in certain LDAP servers. |
| 235 | * @return a String ready for submission to an LDAP server. |
| 236 | */ |
| 237 | String getValue( boolean quotingBug ) { |
| 238 | String s = getValuePrefix(); |
| 239 | String val = getOptionalValues( NOVALS ); |
| 240 | if ( val.length() > 0 ) { |
| 241 | s += val + ' '; |
| 242 | } |
| 243 | s += "OC " + objectClass + ' '; |
| 244 | if ( must.size() > 0 ) { |
| 245 | s += "MUST " + vectorToList( must ); |
| 246 | s += ' '; |
| 247 | } |
| 248 | if ( may.size() > 0 ) { |
| 249 | s += "MAY " + vectorToList( may ); |
| 250 | s += ' '; |
| 251 | } |
| 252 | val = getCustomValues(); |
| 253 | if ( val.length() > 0 ) { |
| 254 | s += val + ' '; |
| 255 | } |
| 256 | s += ')'; |
| 257 | return s; |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Gets the definition of the name form in Directory format |
| 262 | * |
| 263 | * @return definition of the name form in Directory format |
| 264 | */ |
| 265 | public String toString() { |
| 266 | return getValue( false ); |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Creates a list within parentheses, with $ as delimiter |
| 271 | * |
| 272 | * @param vals values for list |
| 273 | * @return a String with a list of values. |
| 274 | */ |
| 275 | protected String vectorToList( Vector<String> vals ) { |
| 276 | String val = "( "; |
| 277 | for( int i = 0; i < vals.size(); i++ ) { |
| 278 | val += vals.elementAt(i) + ' '; |
| 279 | if ( i < (vals.size() - 1) ) { |
| 280 | val += "$ "; |
| 281 | } |
| 282 | } |
| 283 | val += ')'; |
| 284 | return val; |
| 285 | } |
| 286 | |
| 287 | private Vector<String> must = new Vector<>(); |
| 288 | private Vector<String> may = new Vector<>(); |
| 289 | private String objectClass = null; |
| 290 | |
| 291 | // Qualifiers known to not have values; prepare a Hashtable |
| 292 | static final String[] NOVALS = { "OBSOLETE" }; |
| 293 | static { |
| 294 | for( int i = 0; i < NOVALS.length; i++ ) { |
| 295 | novalsTable.put( NOVALS[i], NOVALS[i] ); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | // Qualifiers which we output explicitly in toString() |
| 300 | static final String[] IGNOREVALS = { "MUST", "MAY", |
| 301 | "OBJECTCLASS", "OBSOLETE"}; |
| 302 | } |
| 303 |