mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Maxim Thomas
03.30.2025 61dac86bceb9d727e1bd707982c41ab9467c6d5a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions Copyright [year] [name of copyright owner]".
 *
 * Copyright 2006-2010 Sun Microsystems, Inc.
 * Portions Copyright 2014-2016 ForgeRock AS.
 */
package org.opends.server.tools.makeldif;
 
import static org.opends.messages.ToolMessages.*;
import static org.opends.server.util.StaticUtils.*;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
import org.forgerock.i18n.LocalizableMessage;
import org.forgerock.opendj.ldap.ByteString;
import org.forgerock.opendj.ldap.DN;
import org.forgerock.opendj.ldap.schema.AttributeType;
import org.forgerock.opendj.ldap.schema.CoreSchema;
import org.opends.server.types.Attribute;
import org.opends.server.types.Entry;
 
/**
 * This class defines a branch that should be included in the resulting LDIF.  A
 * branch may or may not have subordinate entries.
 */
public class Branch
{
  /** The DN for this branch entry. */
  private final DN branchDN;
  /**
   * The number of entries that should be created below this branch for each
   * subordinate template.
   */
  private int[] numEntriesPerTemplate;
  /** The names of the subordinate templates for this branch. */
  private String[] subordinateTemplateNames;
  /** The set of subordinate templates for this branch. */
  private Template[] subordinateTemplates;
  /** The set of template lines that correspond to the RDN components. */
  private final TemplateLine[] rdnLines;
  /** The set of extra lines that should be included in this branch entry. */
  private TemplateLine[] extraLines;
 
 
 
  /**
   * Creates a new branch with the provided information.
   *
   * @param  templateFile  The template file in which this branch appears.
   * @param  branchDN      The DN for this branch entry.
   */
  public Branch(TemplateFile templateFile, DN branchDN)
  {
    this(templateFile, branchDN, new String[0], new int[0],
         new TemplateLine[0]);
  }
 
 
 
  /**
   * Creates a new branch with the provided information.
   *
   * @param  templateFile              The template file in which this branch
   *                                   appears.
   * @param  branchDN                  The DN for this branch entry.
   * @param  subordinateTemplateNames  The names of the subordinate templates
   *                                   used to generate entries below this
   *                                   branch.
   * @param  numEntriesPerTemplate     The number of entries that should be
   *                                   created below this branch for each
   *                                   subordinate template.
   * @param  extraLines                The set of extra lines that should be
   *                                   included in this branch entry.
   */
  private Branch(TemplateFile templateFile, DN branchDN,
                String[] subordinateTemplateNames, int[] numEntriesPerTemplate,
                TemplateLine[] extraLines)
  {
    this.branchDN                 = branchDN;
    this.subordinateTemplateNames = subordinateTemplateNames;
    this.numEntriesPerTemplate    = numEntriesPerTemplate;
    this.extraLines               = extraLines;
 
    subordinateTemplates = null;
 
 
    // Get the RDN template lines based just on the entry DN.
    Entry entry = createEntry(branchDN);
 
    ArrayList<LocalizableMessage> warnings = new ArrayList<>();
    ArrayList<TemplateLine> lineList = new ArrayList<>();
 
    for (String ocName : entry.getObjectClasses().values())
    {
      try
      {
        String[] valueStrings = new String[] { ocName };
        Tag[] tags = new Tag[1];
        tags[0] = new StaticTextTag();
        tags[0].initializeForBranch(templateFile, this, valueStrings, 0, warnings);
 
        lineList.add(new TemplateLine(CoreSchema.getObjectClassAttributeType(), 0, tags));
      }
      catch (Exception e)
      {
        // This should never happen.
        e.printStackTrace();
      }
    }
 
    addLines(lineList, entry.getAllAttributes(), templateFile, warnings);
 
    rdnLines = new TemplateLine[lineList.size()];
    lineList.toArray(rdnLines);
  }
 
  private void addLines(List<TemplateLine> lineList, Iterable<Attribute> attrs, TemplateFile templateFile,
      List<LocalizableMessage> warnings)
  {
    for (Attribute a : attrs)
    {
      for (ByteString v : a)
      {
        try
        {
          String[] valueStrings = new String[] { v.toString() };
          Tag[] tags = new Tag[] { new StaticTextTag() };
          tags[0].initializeForBranch(templateFile, this, valueStrings, 0, warnings);
          lineList.add(new TemplateLine(a.getAttributeDescription().getAttributeType(), 0, tags));
        }
        catch (Exception e)
        {
          // This should never happen.
          e.printStackTrace();
        }
      }
    }
  }
 
 
 
  /**
   * Performs any necessary processing to ensure that the branch initialization
   * is completed.  In particular, it should make sure that all referenced
   * subordinate templates actually exist in the template file.
   *
   * @param  templates  The set of templates defined in the template file.
   *
   * @throws  MakeLDIFException  If any of the subordinate templates are not
   *                             defined in the template file.
   */
  public void completeBranchInitialization(Map<String,Template> templates)
         throws MakeLDIFException
  {
    if (subordinateTemplateNames == null)
    {
      subordinateTemplateNames = new String[0];
      subordinateTemplates     = new Template[0];
    }
    else
    {
      subordinateTemplates = new Template[subordinateTemplateNames.length];
      for (int i=0; i < subordinateTemplates.length; i++)
      {
        subordinateTemplates[i] =
             templates.get(toLowerCase(subordinateTemplateNames[i]));
        if (subordinateTemplates[i] == null)
        {
          throw new MakeLDIFException(ERR_MAKELDIF_UNDEFINED_BRANCH_SUBORDINATE.get(
              branchDN, subordinateTemplateNames[i]));
        }
      }
    }
  }
 
 
 
  /**
   * Retrieves the DN for this branch entry.
   *
   * @return  The DN for this branch entry.
   */
  public DN getBranchDN()
  {
    return branchDN;
  }
 
 
 
  /**
   * Retrieves the names of the subordinate templates for this branch.
   *
   * @return  The names of the subordinate templates for this branch.
   */
  public String[] getSubordinateTemplateNames()
  {
    return subordinateTemplateNames;
  }
 
 
 
  /**
   * Retrieves the set of subordinate templates used to generate entries below
   * this branch.  Note that the subordinate templates will not be available
   * until the <CODE>completeBranchInitialization</CODE> method has been called.
   *
   * @return  The set of subordinate templates used to generate entries below
   *          this branch.
   */
  public Template[] getSubordinateTemplates()
  {
    return subordinateTemplates;
  }
 
 
 
  /**
   * Retrieves the number of entries that should be created below this branch
   * for each subordinate template.
   *
   * @return  The number of entries that should be created below this branch for
   *          each subordinate template.
   */
  public int[] getNumEntriesPerTemplate()
  {
    return numEntriesPerTemplate;
  }
 
 
 
  /**
   * Adds a new subordinate template to this branch.  Note that this should not
   * be used after <CODE>completeBranchInitialization</CODE> has been called.
   *
   * @param  name        The name of the template to use to generate the
   *                     entries.
   * @param  numEntries  The number of entries to create based on the template.
   */
  public void addSubordinateTemplate(String name, int numEntries)
  {
    String[] newNames  = new String[subordinateTemplateNames.length+1];
    int[]    newCounts = new int[numEntriesPerTemplate.length+1];
 
    System.arraycopy(subordinateTemplateNames, 0, newNames, 0,
                     subordinateTemplateNames.length);
    System.arraycopy(numEntriesPerTemplate, 0, newCounts, 0,
                     numEntriesPerTemplate.length);
 
    newNames[subordinateTemplateNames.length] = name;
    newCounts[numEntriesPerTemplate.length]   = numEntries;
 
    subordinateTemplateNames = newNames;
    numEntriesPerTemplate    = newCounts;
  }
 
 
 
  /**
   * Retrieves the set of extra lines that should be included in this branch
   * entry.
   *
   * @return  The set of extra lines that should be included in this branch
   *          entry.
   */
  public TemplateLine[] getExtraLines()
  {
    return extraLines;
  }
 
 
 
  /**
   * Adds the provided template line to the set of extra lines for this branch.
   *
   * @param  line  The line to add to the set of extra lines for this branch.
   */
  public void addExtraLine(TemplateLine line)
  {
    TemplateLine[] newExtraLines = new TemplateLine[extraLines.length+1];
    System.arraycopy(extraLines, 0, newExtraLines, 0, extraLines.length);
    newExtraLines[extraLines.length] = line;
 
    extraLines = newExtraLines;
  }
 
 
 
  /**
   * Indicates whether this branch contains a reference to the specified
   * attribute type, either in the RDN components of the DN or in the extra
   * lines.
   *
   * @param  attributeType  The attribute type for which to make the
   *                        determination.
   *
   * @return  <CODE>true</CODE> if the branch does contain the specified
   *          attribute type, or <CODE>false</CODE> if it does not.
   */
  public boolean hasAttribute(AttributeType attributeType)
  {
    if (branchDN.rdn().hasAttributeType(attributeType))
    {
      return true;
    }
 
    for (TemplateLine l : extraLines)
    {
      if (l.getAttributeType().equals(attributeType))
      {
        return true;
      }
    }
 
    return false;
  }
 
 
 
  /**
   * Writes the entry for this branch, as well as all appropriate subordinate
   * entries.
   *
   * @param  entryWriter  The entry writer to which the entries should be
   *                      written.
   *
   * @return  The result that indicates whether processing should continue.
   *
   * @throws  IOException  If a problem occurs while attempting to write to the
   *                       LDIF writer.
   *
   * @throws  MakeLDIFException  If some other problem occurs.
   */
  public TagResult writeEntries(EntryWriter entryWriter)
         throws IOException, MakeLDIFException
  {
    // Create a new template entry and populate it based on the RDN attributes
    // and extra lines.
    TemplateEntry entry = new TemplateEntry(this);
 
    for (TemplateLine l : rdnLines)
    {
      TagResult r = l.generateLine(entry);
      if (!r.keepProcessingEntry()
          || !r.keepProcessingParent()
          || !r.keepProcessingTemplateFile())
      {
        return r;
      }
    }
 
    for (TemplateLine l : extraLines)
    {
      TagResult r = l.generateLine(entry);
      if (!r.keepProcessingEntry()
          || !r.keepProcessingParent()
          || !r.keepProcessingTemplateFile())
      {
        return r;
      }
    }
 
    if (! entryWriter.writeEntry(entry))
    {
      return TagResult.STOP_PROCESSING;
    }
 
 
    for (int i=0; i < subordinateTemplates.length; i++)
    {
      TagResult r =
           subordinateTemplates[i].writeEntries(entryWriter, branchDN,
                                                numEntriesPerTemplate[i]);
      if (!r.keepProcessingParent()
          || !r.keepProcessingTemplateFile())
      {
        if (r.keepProcessingTemplateFile())
        {
          // We don't want to propagate a "stop processing parent" all the way
          // up the chain.
          return TagResult.SUCCESS_RESULT;
        }
 
        return r;
      }
    }
 
    return TagResult.SUCCESS_RESULT;
  }
}