001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 * 019 */ 020 package org.apache.directory.server.utils; 021 022 023 import java.util.List; 024 025 import org.apache.directory.server.i18n.I18n; 026 import org.apache.directory.shared.ldap.constants.MetaSchemaConstants; 027 import org.apache.directory.shared.ldap.constants.SchemaConstants; 028 import org.apache.directory.shared.ldap.entry.DefaultServerAttribute; 029 import org.apache.directory.shared.ldap.entry.DefaultServerEntry; 030 import org.apache.directory.shared.ldap.entry.EntryAttribute; 031 import org.apache.directory.shared.ldap.entry.ServerEntry; 032 import org.apache.directory.shared.ldap.exception.LdapException; 033 import org.apache.directory.shared.ldap.schema.AttributeType; 034 import org.apache.directory.shared.ldap.schema.DITContentRule; 035 import org.apache.directory.shared.ldap.schema.DITStructureRule; 036 import org.apache.directory.shared.ldap.schema.LdapComparator; 037 import org.apache.directory.shared.ldap.schema.LdapSyntax; 038 import org.apache.directory.shared.ldap.schema.MatchingRule; 039 import org.apache.directory.shared.ldap.schema.MatchingRuleUse; 040 import org.apache.directory.shared.ldap.schema.NameForm; 041 import org.apache.directory.shared.ldap.schema.Normalizer; 042 import org.apache.directory.shared.ldap.schema.ObjectClass; 043 import org.apache.directory.shared.ldap.schema.SchemaManager; 044 import org.apache.directory.shared.ldap.schema.SchemaObject; 045 import org.apache.directory.shared.ldap.schema.SyntaxChecker; 046 import org.apache.directory.shared.ldap.schema.registries.Schema; 047 import org.apache.directory.shared.ldap.util.DateUtils; 048 049 050 /** 051 * A factory that generates an entry using the meta schema for schema 052 * elements. 053 * 054 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 055 * @version $Rev$ 056 */ 057 public class AttributesFactory 058 { 059 public ServerEntry getAttributes( SchemaObject obj, Schema schema, SchemaManager schemaManager ) throws LdapException 060 { 061 if ( obj instanceof LdapSyntax ) 062 { 063 return getAttributes( ( LdapSyntax ) obj, schema, schemaManager ); 064 } 065 else if ( obj instanceof MatchingRule ) 066 { 067 return getAttributes( ( MatchingRule ) obj, schema, schemaManager ); 068 } 069 else if ( obj instanceof AttributeType ) 070 { 071 return getAttributes( ( AttributeType ) obj, schema, schemaManager ); 072 } 073 else if ( obj instanceof ObjectClass ) 074 { 075 return getAttributes( ( ObjectClass ) obj, schema, schemaManager ); 076 } 077 else if ( obj instanceof MatchingRuleUse ) 078 { 079 return getAttributes( ( MatchingRuleUse ) obj, schema, schemaManager ); 080 } 081 else if ( obj instanceof DITStructureRule ) 082 { 083 return getAttributes( ( DITStructureRule ) obj, schema, schemaManager ); 084 } 085 else if ( obj instanceof DITContentRule ) 086 { 087 return getAttributes( ( DITContentRule ) obj, schema, schemaManager ); 088 } 089 else if ( obj instanceof NameForm ) 090 { 091 return getAttributes( ( NameForm ) obj, schema, schemaManager ); 092 } 093 094 throw new IllegalArgumentException( I18n.err( I18n.ERR_698, obj.getClass() ) ); 095 } 096 097 098 public ServerEntry getAttributes( Schema schema, SchemaManager schemaManager ) throws LdapException 099 { 100 ServerEntry entry = new DefaultServerEntry( schemaManager ); 101 102 entry.put( SchemaConstants.OBJECT_CLASS_AT, SchemaConstants.TOP_OC, MetaSchemaConstants.META_SCHEMA_OC ); 103 entry.put( SchemaConstants.CN_AT, schema.getSchemaName() ); 104 entry.put( SchemaConstants.CREATORS_NAME_AT, schema.getOwner() ); 105 entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime() ); 106 107 if ( schema.isDisabled() ) 108 { 109 entry.put( MetaSchemaConstants.M_DISABLED_AT, "TRUE" ); 110 } 111 112 String[] dependencies = schema.getDependencies(); 113 114 if ( dependencies != null && dependencies.length > 0 ) 115 { 116 EntryAttribute attr = new DefaultServerAttribute( schemaManager.lookupAttributeTypeRegistry( MetaSchemaConstants.M_DEPENDENCIES_AT ) ); 117 118 for ( String dependency:dependencies ) 119 { 120 attr.add( dependency ); 121 } 122 123 entry.put( attr ); 124 } 125 126 return entry; 127 } 128 129 130 public ServerEntry getAttributes( SyntaxChecker syntaxChecker, Schema schema, SchemaManager schemaManager ) 131 { 132 ServerEntry entry = new DefaultServerEntry( schemaManager ); 133 134 entry.put( SchemaConstants.OBJECT_CLASS_AT, SchemaConstants.TOP_OC, MetaSchemaConstants.META_SYNTAX_CHECKER_OC ); 135 entry.put( MetaSchemaConstants.M_OID_AT, syntaxChecker.getOid() ); 136 entry.put( MetaSchemaConstants.M_FQCN_AT, syntaxChecker.getClass().getName() ); 137 entry.put( SchemaConstants.CREATORS_NAME_AT, schema.getOwner() ); 138 entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime() ); 139 140 return entry; 141 } 142 143 144 public ServerEntry getAttributes( LdapSyntax syntax, Schema schema, SchemaManager schemaManager ) throws LdapException 145 { 146 ServerEntry entry = new DefaultServerEntry( schemaManager ); 147 148 entry.put( SchemaConstants.OBJECT_CLASS_AT, SchemaConstants.TOP_OC, MetaSchemaConstants.META_SYNTAX_OC ); 149 entry.put( MetaSchemaConstants.X_HUMAN_READABLE_AT, getBoolean( syntax.isHumanReadable() ) ); 150 entry.put( SchemaConstants.CREATORS_NAME_AT, schema.getOwner() ); 151 entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime() ); 152 injectCommon( syntax, entry, schemaManager ); 153 154 return entry; 155 } 156 157 158 public ServerEntry getAttributes( String oid, Normalizer normalizer, Schema schema, SchemaManager schemaManager ) 159 { 160 ServerEntry entry = new DefaultServerEntry( schemaManager ); 161 162 entry.put( SchemaConstants.OBJECT_CLASS_AT, SchemaConstants.TOP_OC, MetaSchemaConstants.META_NORMALIZER_OC ); 163 entry.put( MetaSchemaConstants.M_OID_AT, oid ); 164 entry.put( MetaSchemaConstants.M_FQCN_AT, normalizer.getClass().getName() ); 165 entry.put( SchemaConstants.CREATORS_NAME_AT, schema.getOwner() ); 166 entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime() ); 167 return entry; 168 } 169 170 171 public ServerEntry getAttributes( String oid, LdapComparator<? super Object> comparator, Schema schema, SchemaManager schemaManager ) 172 { 173 ServerEntry entry = new DefaultServerEntry( schemaManager ); 174 175 entry.put( SchemaConstants.OBJECT_CLASS_AT, SchemaConstants.TOP_OC, MetaSchemaConstants.META_COMPARATOR_OC ); 176 entry.put( MetaSchemaConstants.M_OID_AT, oid ); 177 entry.put( MetaSchemaConstants.M_FQCN_AT, comparator.getClass().getName() ); 178 entry.put( SchemaConstants.CREATORS_NAME_AT, schema.getOwner() ); 179 entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime() ); 180 return entry; 181 } 182 183 184 /** 185 * 186 * @param matchingRule 187 * @return Attributes 188 * @throws LdapException 189 */ 190 public ServerEntry getAttributes( MatchingRule matchingRule, Schema schema, SchemaManager schemaManager ) throws LdapException 191 { 192 ServerEntry entry = new DefaultServerEntry( schemaManager ); 193 194 entry.put( SchemaConstants.OBJECT_CLASS_AT, SchemaConstants.TOP_OC, MetaSchemaConstants.META_MATCHING_RULE_OC ); 195 entry.put( MetaSchemaConstants.M_SYNTAX_AT, matchingRule.getSyntaxOid() ); 196 entry.put( SchemaConstants.CREATORS_NAME_AT, schema.getOwner() ); 197 entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime() ); 198 injectCommon( matchingRule, entry, schemaManager ); 199 return entry; 200 } 201 202 203 public ServerEntry getAttributes( MatchingRuleUse matchingRuleUse, Schema schema, SchemaManager schemaManager ) 204 { 205 ServerEntry entry = new DefaultServerEntry( schemaManager ); 206 207 entry.put( SchemaConstants.OBJECT_CLASS_AT, SchemaConstants.TOP_OC, "" ); 208 entry.put( SchemaConstants.CREATORS_NAME_AT, schema.getOwner() ); 209 entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime() ); 210 return entry; 211 } 212 213 214 public ServerEntry getAttributes( DITStructureRule dITStructureRule, Schema schema, SchemaManager schemaManager ) 215 { 216 ServerEntry entry = new DefaultServerEntry( schemaManager ); 217 218 entry.put( SchemaConstants.OBJECT_CLASS_AT, SchemaConstants.TOP_OC, "" ); 219 entry.put( SchemaConstants.CREATORS_NAME_AT, schema.getOwner() ); 220 entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime() ); 221 return entry; 222 } 223 224 225 public ServerEntry getAttributes( DITContentRule dITContentRule, Schema schema, SchemaManager schemaManager ) 226 { 227 ServerEntry entry = new DefaultServerEntry( schemaManager ); 228 229 entry.put( SchemaConstants.OBJECT_CLASS_AT, SchemaConstants.TOP_OC, "" ); 230 entry.put( SchemaConstants.CREATORS_NAME_AT, schema.getOwner() ); 231 entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime() ); 232 return entry; 233 } 234 235 236 public ServerEntry getAttributes( NameForm nameForm, Schema schema, SchemaManager schemaManager ) 237 { 238 ServerEntry entry = new DefaultServerEntry( schemaManager ); 239 240 entry.put( SchemaConstants.OBJECT_CLASS_AT, SchemaConstants.TOP_OC, "" ); 241 entry.put( SchemaConstants.CREATORS_NAME_AT, schema.getOwner() ); 242 entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime() ); 243 return entry; 244 } 245 246 247 /** 248 * <pre> 249 * objectclass ( 1.3.6.1.4.1.18060.0.4.0.3.3 250 * NAME 'metaAttributeType' 251 * DESC 'meta definition of the AttributeType object' 252 * SUP metaTop 253 * STRUCTURAL 254 * MUST ( m-name $ m-syntax ) 255 * MAY ( m-supAttributeType $ m-obsolete $ m-equality $ m-ordering $ 256 * m-substr $ m-singleValue $ m-collective $ m-noUserModification $ 257 * m-usage $ m-extensionAttributeType ) 258 * ) 259 * </pre> 260 * 261 * @param attributeType 262 * @return Attributes 263 * @throws LdapException 264 */ 265 public ServerEntry getAttributes( AttributeType attributeType, Schema schema, SchemaManager schemaManager ) throws LdapException 266 { 267 ServerEntry entry = new DefaultServerEntry( schemaManager ); 268 269 entry.put( SchemaConstants.OBJECT_CLASS_AT, SchemaConstants.TOP_OC, MetaSchemaConstants.META_ATTRIBUTE_TYPE_OC ); 270 entry.put( MetaSchemaConstants.M_SYNTAX_AT, attributeType.getSyntaxOid() ); 271 entry.put( MetaSchemaConstants.M_COLLECTIVE_AT, getBoolean( attributeType.isCollective() ) ); 272 entry.put( MetaSchemaConstants.M_NO_USER_MODIFICATION_AT, getBoolean( ! attributeType.isUserModifiable() ) ); 273 entry.put( MetaSchemaConstants.M_SINGLE_VALUE_AT, getBoolean( attributeType.isSingleValued() ) ); 274 entry.put( MetaSchemaConstants.M_USAGE_AT, attributeType.getUsage().toString() ); 275 entry.put( SchemaConstants.CREATORS_NAME_AT, schema.getOwner() ); 276 entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime() ); 277 278 injectCommon( attributeType, entry, schemaManager ); 279 280 String superiorOid = attributeType.getSuperiorOid(); 281 282 if ( superiorOid != null ) 283 { 284 entry.put( MetaSchemaConstants.M_SUP_ATTRIBUTE_TYPE_AT, superiorOid ); 285 } 286 287 if ( attributeType.getEqualityOid() != null ) 288 { 289 entry.put( MetaSchemaConstants.M_EQUALITY_AT, attributeType.getEqualityOid() ); 290 } 291 292 if ( attributeType.getSubstringOid() != null ) 293 { 294 entry.put( MetaSchemaConstants.M_SUBSTR_AT, attributeType.getSubstringOid() ); 295 } 296 297 if ( attributeType.getOrderingOid() != null ) 298 { 299 entry.put( MetaSchemaConstants.M_ORDERING_AT, attributeType.getOrderingOid() ); 300 } 301 302 return entry; 303 } 304 305 306 /** 307 * Creates the attributes of an entry representing an objectClass. 308 * 309 * <pre> 310 * objectclass ( 1.3.6.1.4.1.18060.0.4.0.3.2 311 * NAME 'metaObjectClass' 312 * DESC 'meta definition of the objectclass object' 313 * SUP metaTop 314 * STRUCTURAL 315 * MUST m-oid 316 * MAY ( m-name $ m-obsolete $ m-supObjectClass $ m-typeObjectClass $ m-must $ 317 * m-may $ m-extensionObjectClass ) 318 * ) 319 * </pre> 320 * 321 * @param objectClass the objectClass to produce a meta schema entry for 322 * @return the attributes of the metaSchema entry representing the objectClass 323 * @throws LdapException if there are any problems 324 */ 325 public ServerEntry getAttributes( ObjectClass objectClass, Schema schema, SchemaManager schemaManager ) throws LdapException 326 { 327 ServerEntry entry = new DefaultServerEntry( schemaManager ); 328 329 entry.put( SchemaConstants.OBJECT_CLASS_AT, SchemaConstants.TOP_OC, MetaSchemaConstants.META_OBJECT_CLASS_OC ); 330 entry.put( MetaSchemaConstants.M_TYPE_OBJECT_CLASS_AT, objectClass.getType().toString() ); 331 entry.put( SchemaConstants.CREATORS_NAME_AT, schema.getOwner() ); 332 entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime() ); 333 334 injectCommon( objectClass, entry, schemaManager ); 335 336 // handle the superior objectClasses 337 if ( objectClass.getSuperiorOids() != null && objectClass.getSuperiorOids().size() != 0 ) 338 { 339 EntryAttribute attr = new DefaultServerAttribute( schemaManager.lookupAttributeTypeRegistry( MetaSchemaConstants.M_SUP_OBJECT_CLASS_AT ) ); 340 341 for ( String superior:objectClass.getSuperiorOids() ) 342 { 343 attr.add( superior ); 344 } 345 346 entry.put( attr ); 347 } 348 349 // add the must list 350 if ( objectClass.getMustAttributeTypeOids() != null && objectClass.getMustAttributeTypeOids().size() != 0 ) 351 { 352 EntryAttribute attr = new DefaultServerAttribute( schemaManager.lookupAttributeTypeRegistry( MetaSchemaConstants.M_MUST_AT ) ); 353 354 for ( String mustOid :objectClass.getMustAttributeTypeOids() ) 355 { 356 attr.add( mustOid ); 357 } 358 359 entry.put( attr ); 360 } 361 362 // add the may list 363 if ( objectClass.getMayAttributeTypeOids() != null && objectClass.getMayAttributeTypeOids().size() != 0 ) 364 { 365 EntryAttribute attr = new DefaultServerAttribute( schemaManager.lookupAttributeTypeRegistry( MetaSchemaConstants.M_MAY_AT ) ); 366 367 for ( String mayOid :objectClass.getMayAttributeTypeOids() ) 368 { 369 attr.add( mayOid ); 370 } 371 372 entry.put( attr ); 373 } 374 375 return entry; 376 } 377 378 379 private final void injectCommon( SchemaObject object, ServerEntry entry, SchemaManager schemaManager ) throws LdapException 380 { 381 injectNames( object.getNames(), entry, schemaManager ); 382 entry.put( MetaSchemaConstants.M_OBSOLETE_AT, getBoolean( object.isObsolete() ) ); 383 entry.put( MetaSchemaConstants.M_OID_AT, object.getOid() ); 384 385 if ( object.getDescription() != null ) 386 { 387 entry.put( MetaSchemaConstants.M_DESCRIPTION_AT, object.getDescription() ); 388 } 389 } 390 391 392 private final void injectNames( List<String> names, ServerEntry entry, SchemaManager schemaManager ) throws LdapException 393 { 394 if ( ( names == null ) || ( names.size() == 0 ) ) 395 { 396 return; 397 } 398 399 EntryAttribute attr = new DefaultServerAttribute( schemaManager.lookupAttributeTypeRegistry( MetaSchemaConstants.M_NAME_AT ) ); 400 401 for ( String name:names ) 402 { 403 attr.add( name ); 404 } 405 406 entry.put( attr ); 407 } 408 409 410 private final String getBoolean( boolean value ) 411 { 412 if ( value ) 413 { 414 return "TRUE"; 415 } 416 else 417 { 418 return "FALSE"; 419 } 420 } 421 }