/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 2001
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
package org.ietf.ldap;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.Hashtable;
/**
* This object represents the schema of an LDAP v3 server.
* You can use the fetchSchema method to retrieve
* the schema used by a server. (The server must support LDAP v3
* and the capability to retrieve the schema over the LDAP protocol.)
*
* * After you retrieve the schema, you can use this object to get * the object class, attribute type, and matching rule descriptions * in the schema. You can also add your own object classes, * attribute types, and matching rules to the schema. *
*
* To remove any object classes, attribute types, and matching rules
* that you added, call the remove methods of the
* LDAPObjectClassSchema, LDAPAttributeSchema,
* and LDAPMatchingRuleSchema classes. (This method is
* inherited from the LDAPSchemaElement class.)
*
* * The following class is an example of an LDAP client that can * fetch the schema, get and print object class descriptions and * attribute type descriptions, and add object classes and attribute * types to the schema over the LDAP protocol. *
* *
* import org.ietf.ldap.*;
* public class TestSchema {
* public static void main( String[] args ) {
* String HOSTNAME = "ldap.netscape.com";
* int PORT_NUMBER = DEFAULT_PORT;
* String ROOT_DN = "cn=Directory Manager";
* String ROOT_PASSWORD = "23skidoo";
*
* LDAPConnection ld = new LDAPConnection();
*
* // Construct a new LDAPSchema object to get the schema.
* LDAPSchema dirSchema = new LDAPSchema();
*
* try {
* // Connect to the server.
* ld.connect( HOSTNAME, PORT_NUMBER );
*
* // Get the schema from the directory.
* dirSchema.fetchSchema( ld );
*
* // Get and print the inetOrgPerson object class description.
* LDAPObjectClassSchema objClass = dirSchema.getObjectClass(
* "inetOrgPerson" );
* if ( objClass != null ) {
* System.out.println("inetOrgPerson := "+objClass.toString());
* }
*
* // Get and print the definition of the userPassword attribute.
* LDAPAttributeSchema attrType = dirSchema.getAttribute(
* "userpassword" );
* if ( attrType != null ) {
* System.out.println("userPassword := " + attrType.toString());
* }
*
* // Create a new object class definition.
* String[] requiredAttrs = {"cn", "mail"};
* String[] optionalAttrs = {"sn", "phoneNumber"};
* LDAPObjectClassSchema newObjClass =
* new LDAPObjectClassSchema( "newInetOrgPerson",
* "1.2.3.4.5.6.7",
* "top",
* "Experiment",
* requiredAttrs,
* optionalAttrs );
*
* // Authenticate as root DN to get permissions to edit the schema.
* ld.authenticate( ROOT_DN, ROOT_PASSWORD );
*
* // Add the new object class to the schema.
* newObjClass.add( ld );
*
* // Create a new attribute type "hairColor".
* LDAPAttributeSchema newAttrType =
* new LDAPAttributeSchema( "hairColor",
* "1.2.3.4.5.4.3.2.1",
* "Blonde, red, etc",
* LDAPAttributeSchema.cis,
* false );
* // Add a custom qualifier
* newObjClass.setQualifier( "X-OWNER", "John Jacobson" );
*
* // Add the new attribute type to the schema.
* newAttrType.add( ld );
*
* // Fetch the schema again to verify that changes were made.
* dirSchema.fetchSchema( ld );
*
* // Get and print the new attribute type.
* newAttrType = dirSchema.getAttribute( "hairColor" );
* if ( newAttrType != null ) {
* System.out.println("hairColor := " + newAttrType.toString());
* }
*
* // Get and print the new object class.
* newObjClass = dirSchema.getObjectClass( "newInetOrgPerson" );
* if ( newObjClass != null ) {
* System.out.println("newInetOrgPerson := " +newObjClass.toString());
* }
*
* ld.disconnect();
*
* } catch ( Exception e ) {
* System.err.println( e.toString() );
* System.exit( 1 );
* }
*
* System.exit( 0 );
* }
* }
*
*
* If you are using the Netscape Directory Server 3.0, you can also
* verify that the class and attribute type have been added through
* the directory server manager (go to Schema | Edit or View Attributes
* or Schema | Edit or View Object Classes).
*
*
* To remove the classes and attribute types added by the example,
* see the examples under the LDAPSchemaElement class.
*
*
* @see org.ietf.ldap.LDAPAttributeSchema
* @see org.ietf.ldap.LDAPObjectClassSchema
* @see org.ietf.ldap.LDAPMatchingRuleSchema
* @see org.ietf.ldap.LDAPSchemaElement
* @version 1.0
* @author Rob Weltman
**/
public class LDAPSchema implements Serializable {
static final long serialVersionUID = -3911737419783579398L;
/**
* Constructs a new LDAPSchema object.
* Once you construct the object, you can get
* the schema by calling fetchSchema.
*
*
* You can also print out the schema by using the
* main method. For example, you can enter
* the following command:
*
* java org.ietf.ldap.LDAPSchema myhost.mydomain.com 389
*
*
* Note that you need to call fetchSchema
* to get the schema from the server. Constructing the
* object does not fetch the schema.
*
*
* @see org.ietf.ldap.LDAPSchema#fetchSchema
* @see org.ietf.ldap.LDAPSchema#main
*/
public LDAPSchema() {
}
public LDAPSchema( LDAPEntry entry ) {
initialize( entry );
}
/**
* Adds a schema element definition to the schema object. If the schema
* object already contains an element with the same class and OID as el,
* IllegalArgumentException is thrown. Changes are applied to a directory
* by calling saveSchema.
*
* @param el A non-null schema definition
*/
public void add( LDAPSchemaElement el ) {
add( el, true );
}
/**
* Adds a schema element definition to the schema object. If the schema
* object already contains an element with the same class and OID as el,
* IllegalArgumentException is thrown. Changes are applied to a directory
* by calling saveSchema.
*
* @param el A non-null schema definition
* @param isChange true if called by a client, meaning that
* a directory is to be changed and not just this object
*/
private void add( LDAPSchemaElement el,
boolean isChange ) {
String ID = el.getID();
if ( ID != null ) {
LDAPSchemaElement oldEl = _IDToElement.get( ID );
if ( (oldEl != null) && namesTheSame( oldEl, el ) ) {
throw new IllegalArgumentException( "Schema element already " +
"defined for " + ID +
"; old: " +
oldEl +
" ; new: " + el );
}
_IDToElement.put( ID, el );
}
if ( isChange ) {
_changeList.add( getModificationForElement( el,
LDAPModification.ADD,
null ) );
}
String name;
try {
name = (el.getNames().length > 0) ?
el.getNames()[0].toLowerCase() : "";
} catch ( Exception e ) {
System.err.println( el );
e.printStackTrace();
return;
}
if ( el instanceof LDAPAttributeSchema ) {
_attributes.put( name, (LDAPAttributeSchema) el );
} else if ( el instanceof LDAPObjectClassSchema ) {
_objectClasses.put( name, (LDAPObjectClassSchema) el );
} else if ( el instanceof LDAPMatchingRuleSchema ) {
_matchingRules.put( name, (LDAPMatchingRuleSchema) el );
} else if ( el instanceof LDAPMatchingRuleUseSchema ) {
_matchingRuleUses.put( name, (LDAPMatchingRuleUseSchema) el );
} else if ( el instanceof LDAPNameFormSchema ) {
_nameForms.put( name, (LDAPNameFormSchema) el );
} else if ( el instanceof LDAPSyntaxSchema ) {
if ( name.length() < 1 ) {
name = el.getID();
}
_syntaxes.put( name, (LDAPSyntaxSchema) el );
} else if ( el instanceof LDAPDITContentRuleSchema ) {
_contentRules.put( name, (LDAPDITContentRuleSchema) el );
} else if ( el instanceof LDAPDITStructureRuleSchema ) {
_structureRulesByName.put( name, (LDAPDITStructureRuleSchema) el );
_structureRulesById.put( ((LDAPDITStructureRuleSchema)el).getRuleID(), (LDAPDITStructureRuleSchema) el );
}
}
/**
* Retrieve the entire schema from the root of a Directory Server
*
* @param ld an active connection to a Directory Server
* @exception LDAPException on failure.
*/
public void fetchSchema( LDAPConnection ld ) throws LDAPException {
fetchSchema( ld, "" );
}
/**
* Retrieve the schema for a specific entry
*
* @param ld an active connection to a Directory Server
* @param dn the entry for which to fetch schema
* @exception LDAPException on failure.
*/
public void fetchSchema( LDAPConnection ld, String dn )
throws LDAPException {
clear();
/* Find the subschemasubentry value for this DN */
String entryName = getSchemaDN( ld, dn );
/* Get the entire schema definition entry */
LDAPEntry entry = readSchema( ld, entryName );
initialize( entry );
}
/**
* Get an enumeration of the names of the attribute types in this schema.
* @return an enumeration of attribute names (all lower-case).
*/
public EnumerationLDAPAttributeSchema object representing
* the attribute type definition, or null if not found.
*/
public LDAPAttributeSchema getAttributeSchema( String name ) {
return _attributes.get( name.toLowerCase() );
}
/**
* Gets an enumeration of the attribute type definitions in this schema.
* @return an enumeration of attribute type definitions.
*/
public EnumerationLDAPDITContentRuleSchema object representing
* the rule, or null if not found.
*/
public LDAPDITContentRuleSchema getDITContentRuleSchema( String name ) {
return _contentRules.get( name.toLowerCase() );
}
/**
* Get an enumeration of the content rules in this schema.
* @return an enumeration of content rule objects
*/
public EnumerationLDAPDITStructureRuleSchema object representing
* the rule, or null if not found.
*/
public LDAPDITStructureRuleSchema getDITStructureRuleSchema( String name ) {
return _structureRulesByName.get(name.toLowerCase() );
}
/**
* Gets the definition of a structure rule with the specified name.
* @param ID ID of the rule to find
* @return an LDAPDITStructureRuleSchema object representing
* the rule, or null if not found.
*/
public LDAPDITStructureRuleSchema getDITStructureRuleSchema( int ID ) {
return _structureRulesById.get( ID );
}
/**
* Get an enumeration of the structure rules in this schema.
* @return an enumeration of structure rule objects
*/
public EnumerationLDAPMatchingRuleSchema object representing
* the matching rule definition, or null if not found.
*/
public LDAPMatchingRuleSchema getMatchingRuleSchema( String name ) {
return _matchingRules.get( name.toLowerCase() );
}
/**
* Gets an enumeration ofthe matching rule definitions in this schema.
* @return an enumeration of matching rule definitions.
*/
public EnumerationLDAPMatchingRuleUseSchema object representing
* the matching rule definition, or null if not found.
*/
public LDAPMatchingRuleUseSchema getMatchingRuleUseSchema( String name ) {
return _matchingRuleUses.get( name.toLowerCase() );
}
/**
* Gets an enumeration ofthe matching rule use definitions in this schema.
* @return an enumeration of matching rule use definitions.
*/
public EnumerationLDAPNameFormSchema object representing
* the syntax definition, or null if not found.
*/
public LDAPNameFormSchema getNameFormSchema( String name ) {
return _nameForms.get( name.toLowerCase() );
}
/**
* Get an enumeration of the name forms in this schema.
* @return an enumeration of name form objects
*/
public EnumerationLDAPObjectClassSchema object representing
* the object class definition, or null if not found.
*/
public LDAPObjectClassSchema getObjectClassSchema( String name ) {
return _objectClasses.get( name.toLowerCase() );
}
/**
* Gets an enumeration ofthe object class definitions in this schema.
* @return an enumeration of object class definitions.
*/
public EnumerationLDAPSyntaxSchema object representing
* the syntax definition, or null if not found.
*/
public LDAPSyntaxSchema getSyntaxSchema( String name ) {
return _syntaxes.get( name.toLowerCase() );
}
/**
* Get an enumeration of the syntaxes in this schema.
* @return an enumeration of syntax objects
*/
public Enumerationtrue if called by a client, meaning that
* a directory is to be changed and not just this object
*/
private void modify( LDAPSchemaElement el,
boolean isChange ) {
String ID = el.getID();
if ( isChange ) {
// Remove if already present and then add
LDAPSchemaElement oldEl = ( ID != null ) ?
(LDAPSchemaElement)_IDToElement.get( ID ) : null;
if ( oldEl != null ) {
_changeList.add(
getModificationForElement( oldEl,
LDAPModification.DELETE,
null ) );
}
_changeList.add(
getModificationForElement( el,
LDAPModification.ADD,
null ) );
}
if ( ID != null ) {
_IDToElement.put( ID, el );
}
String name = (el.getNames().length > 0) ?
el.getNames()[0].toLowerCase() : "";
if ( el instanceof LDAPAttributeSchema ) {
_attributes.put( name, (LDAPAttributeSchema) el );
} else if ( el instanceof LDAPObjectClassSchema ) {
_objectClasses.put( name, (LDAPObjectClassSchema) el );
} else if ( el instanceof LDAPMatchingRuleSchema ) {
_matchingRules.put( name, (LDAPMatchingRuleSchema) el );
} else if ( el instanceof LDAPMatchingRuleUseSchema ) {
_matchingRuleUses.put( name, (LDAPMatchingRuleUseSchema) el );
} else if ( el instanceof LDAPNameFormSchema ) {
_nameForms.put( name, (LDAPNameFormSchema) el );
} else if ( el instanceof LDAPSyntaxSchema ) {
if ( name.length() < 1 ) {
name = el.getID();
}
_syntaxes.put( name, (LDAPSyntaxSchema) el );
} else if ( el instanceof LDAPDITContentRuleSchema ) {
_contentRules.put( name, (LDAPDITContentRuleSchema) el );
} else if ( el instanceof LDAPDITStructureRuleSchema ) {
_structureRulesByName.put( name, (LDAPDITStructureRuleSchema) el );
_structureRulesById.put( ((LDAPDITStructureRuleSchema)el).getRuleID(), (LDAPDITStructureRuleSchema) el );
}
}
/**
* Removes a schema element definition of the schema object. If the
* schema object does not contain an element with the same class and OID
* as el, this is a noop. Changes are applied to a directory by calling
* saveSchema.
*
* @param el A non-null schema definition
*/
public void remove( LDAPSchemaElement el ) {
remove( el, true );
}
/**
* Removes a schema element definition of the schema object. If the
* schema object does not contain an element with the same class and OID
* as el, this is a noop. Changes are applied to a directory by calling
* saveSchema.
*
* @param el A non-null schema definition
* @param isChange true if called by a client, meaning that
* a directory is to be changed and not just this object
*/
private void remove( LDAPSchemaElement el, boolean isChange ) {
String ID = el.getID();
if ( isChange ) {
// Remove it if present
if ( (ID != null) && _IDToElement.containsKey( ID ) ) {
_changeList.add(
getModificationForElement( el,
LDAPModification.DELETE,
null ) );
}
}
if ( ID != null ) {
_IDToElement.remove( ID );
}
String name = (el.getNames().length > 0) ?
el.getNames()[0].toLowerCase() : "";
if ( el instanceof LDAPAttributeSchema ) {
_attributes.remove( name );
} else if ( el instanceof LDAPObjectClassSchema ) {
_objectClasses.remove( name );
} else if ( el instanceof LDAPMatchingRuleSchema ) {
_matchingRules.remove( name );
} else if ( el instanceof LDAPMatchingRuleUseSchema ) {
_matchingRuleUses.remove( name );
} else if ( el instanceof LDAPNameFormSchema ) {
_nameForms.remove( name );
} else if ( el instanceof LDAPSyntaxSchema ) {
if ( name.length() < 1 ) {
name = el.getID();
}
_syntaxes.remove( name );
} else if ( el instanceof LDAPDITContentRuleSchema ) {
_contentRules.remove( name );
} else if ( el instanceof LDAPDITStructureRuleSchema ) {
_structureRulesByName.remove( name );
_structureRulesById.remove( ((LDAPDITStructureRuleSchema)el).getRuleID() );
}
}
/**
* Save the schema changes to the root of a Directory Server
*
* @param ld an active connection to a Directory Server
* @exception LDAPException on failure.
*/
public void saveSchema( LDAPConnection ld ) throws LDAPException {
saveSchema( ld, "" );
}
/**
* Save the schema changes to a specific entry
*
* @param ld an active connection to a Directory Server
* @param dn the entry at which to save schema
* @exception LDAPException on failure.
*/
public void saveSchema( LDAPConnection ld, String dn )
throws LDAPException {
// Get array of changes to be made
LDAPModification[] mods =
_changeList.toArray( new LDAPModification[0] );
if ( mods.length < 1 ) {
return;
}
/* Find the subschemasubentry value for this DN */
String entryName = getSchemaDN( ld, dn );
/* Make the changes; exception thrown on error */
ld.modify( entryName, mods );
/* If we got this far, the changes were saved and we can reset
the change list */
_changeList.clear();
}
/**
* Displays the schema (including the descriptions of its object
* classes, attribute types, and matching rules) in an easily
* readable format (not the same as the format expected by
* an LDAP server).
* @return a string containing the schema in printable format.
*/
public String toString() {
String s = "Object classes:\n";
Enumerationtrue if standards-compliant.
* @exception LDAPException on failure.
*/
static boolean isAttributeSyntaxStandardsCompliant( LDAPConnection ld )
throws LDAPException {
/* Check if this has already been investigated */
String schemaBug = (String)ld.getProperty( LDAPConnection.SCHEMA_BUG_PROPERTY );
if ( schemaBug != null ) {
return schemaBug.equalsIgnoreCase( "standard" );
}
boolean compliant = true;
/* Get the schema definitions for attributes */
String entryName = getSchemaDN( ld, "" );
String[] attrs = { "attributetypes" };
LDAPEntry entry = ld.read( entryName, attrs );
/* Get all attribute definitions, and check the first one */
LDAPAttribute attr = entry.getAttribute( "attributetypes" );
if ( attr != null ) {
Enumeration
* SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 or
* SYNTAX '1.3.6.1.4.1.1466.115.121.1.15'
* @param raw Definition of the attribute type in the
* AttributeTypeDescription format.
*/
static boolean isSyntaxQuoted( String raw ) {
int ind = raw.indexOf( " SYNTAX " );
if ( ind >= 0 ) {
ind += 8;
int l = raw.length() - ind;
// Extract characters
char[] ch = new char[l];
raw.getChars( ind, raw.length(), ch, 0 );
ind = 0;
// Skip to ' or start of syntax value
while( (ind < ch.length) && (ch[ind] == ' ') ) {
ind++;
}
if ( ind < ch.length ) {
return ( ch[ind] == '\'' );
}
}
return false;
}
/**
* Compares to schema elements for identical names; order and case are
* ignored
*
* @param el1 First element to compare
* @param el2 Second element to compare
* @return true if the elements have the same names
*/
private static boolean namesTheSame( LDAPSchemaElement el1,
LDAPSchemaElement el2 ) {
// Don't bother if either element is null
if ( (el1 == null) || (el2 == null) ) {
return false;
}
String[] names1 = el1.getNames();
String[] names2 = el2.getNames();
if ( (names1 == null) ^ (names2 == null) ) {
return false;
}
// If neither have any names, return true
if ( names1 == null ) {
return true;
}
if ( names1.length != names2.length ) {
return false;
}
if ( names1.length == 0 ) {
return true;
}
if ( names1.length == 1 ) {
return ( names1[0].equalsIgnoreCase( names2[0] ) );
}
Comparator
* java org.ietf.ldap.LDAPSchema myhost.mydomain.com 389
*
*
* @param args the host name and the port number of the LDAP server
* (for example, org.ietf.ldap.LDAPSchema directory.netscape.com
* 389)
*/
public static void main( String[] args ) {
if ( args.length < 2 ) {
System.err.println( "Usage: org.ietf.ldap.LDAPSchema HOST PORT" );
System.exit(1 );
}
int port = Integer.parseInt( args[1] );
LDAPConnection ld = new LDAPConnection();
try {
ld.connect( args[0], port );
LDAPSchema schema = new LDAPSchema();
schema.fetchSchema( ld );
ld.disconnect();
System.out.println( "Object classes: " );
printEnum( schema.getObjectClassSchemas() );
System.out.println( "\nAttributes: " );
printEnum( schema.getAttributeSchemas() );
System.out.println( "\nMatching rules: " );
printEnum( schema.getMatchingRuleSchemas() );
System.out.println( "\nSyntaxes: " );
printEnum( schema.getSyntaxSchemas() );
System.exit( 0 );
} catch ( LDAPException e ) {
System.err.println( e );
}
}
private Hashtable