1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.server.core.schema;
21
22
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.HashSet;
26 import java.util.Set;
27
28 import javax.naming.NamingException;
29
30 import org.apache.directory.server.constants.MetaSchemaConstants;
31 import org.apache.directory.server.core.authn.AuthenticationInterceptor;
32 import org.apache.directory.server.core.authz.AciAuthorizationInterceptor;
33 import org.apache.directory.server.core.authz.DefaultAuthorizationInterceptor;
34 import org.apache.directory.server.core.entry.DefaultServerEntry;
35 import org.apache.directory.server.core.entry.ServerEntry;
36 import org.apache.directory.server.core.exception.ExceptionInterceptor;
37 import org.apache.directory.server.core.interceptor.context.OperationContext;
38 import org.apache.directory.server.schema.bootstrap.Schema;
39 import org.apache.directory.server.schema.registries.Registries;
40 import org.apache.directory.server.utils.AttributesFactory;
41 import org.apache.directory.shared.ldap.constants.SchemaConstants;
42 import org.apache.directory.shared.ldap.entry.Entry;
43 import org.apache.directory.shared.ldap.name.LdapDN;
44 import org.apache.directory.shared.ldap.schema.AttributeType;
45 import org.apache.directory.shared.ldap.schema.DITContentRule;
46 import org.apache.directory.shared.ldap.schema.DITStructureRule;
47 import org.apache.directory.shared.ldap.schema.MatchingRule;
48 import org.apache.directory.shared.ldap.schema.MatchingRuleUse;
49 import org.apache.directory.shared.ldap.schema.NameForm;
50 import org.apache.directory.shared.ldap.schema.ObjectClass;
51 import org.apache.directory.shared.ldap.schema.SchemaObject;
52 import org.apache.directory.shared.ldap.schema.Syntax;
53 import org.apache.directory.shared.ldap.schema.syntax.AbstractSchemaDescription;
54 import org.apache.directory.shared.ldap.schema.syntax.ComparatorDescription;
55 import org.apache.directory.shared.ldap.schema.syntax.NormalizerDescription;
56 import org.apache.directory.shared.ldap.schema.syntax.SyntaxCheckerDescription;
57 import org.apache.directory.shared.ldap.util.Base64;
58
59
60
61
62
63
64
65
66
67 public class SchemaSubentryModifier
68 {
69 private static final Collection<String> BYPASS;
70
71 static
72 {
73 Set<String> c = new HashSet<String>();
74
75 c.add( AuthenticationInterceptor.class.getName() );
76 c.add( AciAuthorizationInterceptor.class.getName() );
77 c.add( DefaultAuthorizationInterceptor.class.getName() );
78 c.add( ExceptionInterceptor.class.getName() );
79
80 c.add( SchemaInterceptor.class.getName() );
81
82
83
84
85 BYPASS = Collections.unmodifiableCollection( c );
86 }
87
88 private AttributesFactory factory = new AttributesFactory();
89 private final SchemaPartitionDao dao;
90
91
92 private Registries registries;
93
94
95
96
97
98
99
100
101
102 public SchemaSubentryModifier( Registries registries, SchemaPartitionDao dao )
103 {
104 this.registries = registries;
105 this.dao = dao;
106 }
107
108
109 private LdapDN getDn( SchemaObject obj ) throws NamingException
110 {
111 StringBuffer buf = new StringBuffer();
112 buf.append( "m-oid=" ).append( obj.getOid() ).append( ",ou=" );
113
114 if ( obj instanceof Syntax )
115 {
116 buf.append( "syntaxes" );
117 }
118 else if ( obj instanceof MatchingRule )
119 {
120 buf.append( SchemaConstants.MATCHING_RULES_AT );
121 }
122 else if ( obj instanceof AttributeType )
123 {
124 buf.append( SchemaConstants.ATTRIBUTE_TYPES_AT );
125 }
126 else if ( obj instanceof ObjectClass )
127 {
128 buf.append( SchemaConstants.OBJECT_CLASSES_AT );
129 }
130 else if ( obj instanceof MatchingRuleUse )
131 {
132 buf.append( SchemaConstants.MATCHING_RULE_USE_AT );
133 }
134 else if ( obj instanceof DITStructureRule )
135 {
136 buf.append( SchemaConstants.DIT_STRUCTURE_RULES_AT );
137 }
138 else if ( obj instanceof DITContentRule )
139 {
140 buf.append( SchemaConstants.DIT_CONTENT_RULES_AT );
141 }
142 else if ( obj instanceof NameForm )
143 {
144 buf.append( SchemaConstants.NAME_FORMS_AT );
145 }
146
147 buf.append( ",cn=" ).append( obj.getSchema() ).append( ",ou=schema" );
148 return new LdapDN( buf.toString() );
149 }
150
151
152 public void add( OperationContext opContext, ComparatorDescription comparatorDescription ) throws Exception
153 {
154 String schemaName = getSchema( comparatorDescription );
155 LdapDN dn = new LdapDN( "m-oid=" + comparatorDescription.getNumericOid() + ",ou=comparators,cn="
156 + schemaName + ",ou=schema" );
157 Entry entry = getEntry( dn, comparatorDescription );
158
159 opContext.add( (ServerEntry)entry, BYPASS );
160 }
161
162
163 public void add( OperationContext opContext, NormalizerDescription normalizerDescription ) throws Exception
164 {
165 String schemaName = getSchema( normalizerDescription );
166 LdapDN dn = new LdapDN( "m-oid=" + normalizerDescription.getNumericOid() + ",ou=normalizers,cn="
167 + schemaName + ",ou=schema" );
168 Entry entry = getEntry( dn, normalizerDescription );
169
170 opContext.add( (ServerEntry)entry, BYPASS );
171 }
172
173
174 public void add( OperationContext opContext, SyntaxCheckerDescription syntaxCheckerDescription ) throws Exception
175 {
176 String schemaName = getSchema( syntaxCheckerDescription );
177 LdapDN dn = new LdapDN( "m-oid=" + syntaxCheckerDescription.getNumericOid() + ",ou=syntaxCheckers,cn="
178 + schemaName + ",ou=schema" );
179 Entry entry = getEntry( dn, syntaxCheckerDescription );
180 opContext.add( (ServerEntry)entry, BYPASS );
181 }
182
183
184 public void addSchemaObject( OperationContext opContext, SchemaObject obj ) throws Exception
185 {
186 Schema schema = dao.getSchema( obj.getSchema() );
187 LdapDN dn = getDn( obj );
188 ServerEntry entry = factory.getAttributes( obj, schema,
189 opContext.getSession().getDirectoryService().getRegistries() );
190 entry.setDn( dn );
191
192 opContext.add( entry, BYPASS );
193 }
194
195
196 public void deleteSchemaObject( OperationContext opContext, SchemaObject obj ) throws Exception
197 {
198 LdapDN dn = getDn( obj );
199 opContext.delete( dn, BYPASS );
200 }
201
202
203 public void delete( OperationContext opContext, NormalizerDescription normalizerDescription ) throws Exception
204 {
205 String schemaName = getSchema( normalizerDescription );
206 LdapDN dn = new LdapDN( "m-oid=" + normalizerDescription.getNumericOid() + ",ou=normalizers,cn="
207 + schemaName + ",ou=schema" );
208 opContext.delete( dn, BYPASS );
209 }
210
211
212 public void delete( OperationContext opContext, SyntaxCheckerDescription syntaxCheckerDescription ) throws Exception
213 {
214 String schemaName = getSchema( syntaxCheckerDescription );
215 LdapDN dn = new LdapDN( "m-oid=" + syntaxCheckerDescription.getNumericOid() + ",ou=syntaxCheckers,cn="
216 + schemaName + ",ou=schema" );
217 opContext.delete( dn, BYPASS );
218 }
219
220
221 public void delete( OperationContext opContext, ComparatorDescription comparatorDescription ) throws Exception
222 {
223 String schemaName = getSchema( comparatorDescription );
224 LdapDN dn = new LdapDN( "m-oid=" + comparatorDescription.getNumericOid() + ",ou=comparators,cn="
225 + schemaName + ",ou=schema" );
226 opContext.delete( dn, BYPASS );
227 }
228
229
230 private Entry getEntry( LdapDN dn, ComparatorDescription comparatorDescription )
231 {
232 Entry entry = new DefaultServerEntry( registries, dn );
233
234 entry.put( SchemaConstants.OBJECT_CLASS_AT,
235 SchemaConstants.TOP_OC,
236 MetaSchemaConstants.META_TOP_OC,
237 MetaSchemaConstants.META_COMPARATOR_OC );
238
239 entry.put( MetaSchemaConstants.M_OID_AT, comparatorDescription.getNumericOid() );
240 entry.put( MetaSchemaConstants.M_FQCN_AT, comparatorDescription.getFqcn() );
241
242 if ( comparatorDescription.getBytecode() != null )
243 {
244 entry.put( MetaSchemaConstants.M_BYTECODE_AT,
245 Base64.decode( comparatorDescription.getBytecode().toCharArray() ) );
246 }
247
248 if ( comparatorDescription.getDescription() != null )
249 {
250 entry.put( MetaSchemaConstants.M_DESCRIPTION_AT, comparatorDescription.getDescription() );
251 }
252
253 return entry;
254 }
255
256
257 private Entry getEntry( LdapDN dn, NormalizerDescription normalizerDescription )
258 {
259 Entry entry = new DefaultServerEntry( registries, dn );
260
261 entry.put( SchemaConstants.OBJECT_CLASS_AT,
262 SchemaConstants.TOP_OC,
263 MetaSchemaConstants.META_TOP_OC,
264 MetaSchemaConstants.META_NORMALIZER_OC );
265
266 entry.put( MetaSchemaConstants.M_OID_AT, normalizerDescription.getNumericOid() );
267 entry.put( MetaSchemaConstants.M_FQCN_AT, normalizerDescription.getFqcn() );
268
269 if ( normalizerDescription.getBytecode() != null )
270 {
271 entry.put( MetaSchemaConstants.M_BYTECODE_AT,
272 Base64.decode( normalizerDescription.getBytecode().toCharArray() ) );
273 }
274
275 if ( normalizerDescription.getDescription() != null )
276 {
277 entry.put( MetaSchemaConstants.M_DESCRIPTION_AT, normalizerDescription.getDescription() );
278 }
279
280 return entry;
281 }
282
283
284 private String getSchema( AbstractSchemaDescription desc )
285 {
286 if ( desc.getExtensions().containsKey( MetaSchemaConstants.X_SCHEMA ) )
287 {
288 return desc.getExtensions().get( MetaSchemaConstants.X_SCHEMA ).get( 0 );
289 }
290
291 return MetaSchemaConstants.SCHEMA_OTHER;
292 }
293
294
295 private Entry getEntry( LdapDN dn, SyntaxCheckerDescription syntaxCheckerDescription )
296 {
297 Entry entry = new DefaultServerEntry( registries, dn );
298
299 entry.put( SchemaConstants.OBJECT_CLASS_AT,
300 SchemaConstants.TOP_OC,
301 MetaSchemaConstants.META_TOP_OC,
302 MetaSchemaConstants.META_SYNTAX_CHECKER_OC );
303
304 entry.put( MetaSchemaConstants.M_OID_AT, syntaxCheckerDescription.getNumericOid() );
305 entry.put( MetaSchemaConstants.M_FQCN_AT, syntaxCheckerDescription.getFqcn() );
306
307 if ( syntaxCheckerDescription.getBytecode() != null )
308 {
309 entry.put( MetaSchemaConstants.M_BYTECODE_AT,
310 Base64.decode( syntaxCheckerDescription.getBytecode().toCharArray() ) );
311 }
312
313 if ( syntaxCheckerDescription.getDescription() != null )
314 {
315 entry.put( MetaSchemaConstants.M_DESCRIPTION_AT, syntaxCheckerDescription.getDescription() );
316 }
317
318 return entry;
319 }
320 }