Search in sources :

Example 1 with ChangeType

use of com.unboundid.ldap.sdk.ChangeType in project ldapsdk by pingidentity.

the class GetChangelogBatchExtendedRequestTestCase method testManyConstructorWithoutListener.

/**
 * Provides test coverage for the constructor which may be used to create an
 * instance of this extended request using a full set of information and
 * doesn't take an entry listener.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testManyConstructorWithoutListener() throws Exception {
    final List<String> includeBases = Arrays.asList("dc=example,dc=com", "o=example.com");
    final List<String> excludeBases = Arrays.asList("ou=People,dc=example,dc=com", "ou=Groups,ec=example,dc=com");
    final EnumSet<ChangeType> changeTypes = EnumSet.of(ChangeType.MODIFY);
    GetChangelogBatchExtendedRequest r = new GetChangelogBatchExtendedRequest(new EndOfChangelogStartingPoint(), -1, 0L, false, includeBases, excludeBases, changeTypes, true, new Control("1.2.3.4"), new Control("5.6.7.8"));
    assertNull(r.getEntryListener());
    r = new GetChangelogBatchExtendedRequest(r);
    r = r.duplicate();
    assertNotNull(r);
    assertNotNull(r.getStartingPoint());
    assertTrue(r.getStartingPoint() instanceof EndOfChangelogStartingPoint);
    assertEquals(r.getMaxChanges(), 0);
    assertEquals(r.getMaxWaitTimeMillis(), 0L);
    assertFalse(r.waitForMaxChanges());
    assertNotNull(r.getIncludeBaseDNs());
    assertEquals(r.getIncludeBaseDNs(), includeBases);
    assertNotNull(r.getExcludeBaseDNs());
    assertEquals(r.getExcludeBaseDNs(), excludeBases);
    assertNotNull(r.getChangeTypes());
    assertEquals(r.getChangeTypes(), changeTypes);
    assertTrue(r.continueOnMissingChanges());
    assertNull(r.getPareEntriesForUserDN());
    assertNull(r.getChangeSelectionCriteria());
    assertFalse(r.includeSoftDeletedEntryMods());
    assertFalse(r.includeSoftDeletedEntryDeletes());
    assertNotNull(r.getExtendedRequestName());
    assertNotNull(r.toString());
}
Also used : Control(com.unboundid.ldap.sdk.Control) ChangeType(com.unboundid.ldap.sdk.ChangeType) ASN1OctetString(com.unboundid.asn1.ASN1OctetString) Test(org.testng.annotations.Test)

Example 2 with ChangeType

use of com.unboundid.ldap.sdk.ChangeType in project ldapsdk by pingidentity.

the class GetChangelogBatchExtendedRequestTestCase method testManyConstructorWithListener.

/**
 * Provides test coverage for the constructor which may be used to create an
 * instance of this extended request using a full set of information and takes
 * an entry listener.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testManyConstructorWithListener() throws Exception {
    final List<String> includeBases = Arrays.asList("dc=example,dc=com");
    final List<String> excludeBases = Arrays.asList("ou=People,dc=example,dc=com");
    final EnumSet<ChangeType> changeTypes = EnumSet.of(ChangeType.ADD, ChangeType.DELETE, ChangeType.MODIFY_DN);
    GetChangelogBatchExtendedRequest r = new GetChangelogBatchExtendedRequest(new TestChangelogEntryListener(), new EndOfChangelogStartingPoint(), -1, -1L, true, includeBases, excludeBases, changeTypes, false, new Control("1.2.3.4"));
    assertNotNull(r.getEntryListener());
    assertTrue(r.getEntryListener() instanceof TestChangelogEntryListener);
    r = new GetChangelogBatchExtendedRequest(r);
    r = r.duplicate();
    assertNotNull(r);
    assertNotNull(r.getStartingPoint());
    assertTrue(r.getStartingPoint() instanceof EndOfChangelogStartingPoint);
    assertEquals(r.getMaxChanges(), 0);
    assertEquals(r.getMaxWaitTimeMillis(), 0L);
    assertTrue(r.waitForMaxChanges());
    assertNotNull(r.getIncludeBaseDNs());
    assertEquals(r.getIncludeBaseDNs(), includeBases);
    assertNotNull(r.getExcludeBaseDNs());
    assertEquals(r.getExcludeBaseDNs(), excludeBases);
    assertNotNull(r.getChangeTypes());
    assertEquals(r.getChangeTypes(), changeTypes);
    assertFalse(r.continueOnMissingChanges());
    assertNull(r.getPareEntriesForUserDN());
    assertNull(r.getChangeSelectionCriteria());
    assertFalse(r.includeSoftDeletedEntryMods());
    assertFalse(r.includeSoftDeletedEntryDeletes());
    assertNotNull(r.getExtendedRequestName());
    assertNotNull(r.toString());
}
Also used : Control(com.unboundid.ldap.sdk.Control) ChangeType(com.unboundid.ldap.sdk.ChangeType) ASN1OctetString(com.unboundid.asn1.ASN1OctetString) Test(org.testng.annotations.Test)

Example 3 with ChangeType

use of com.unboundid.ldap.sdk.ChangeType in project ldapsdk by pingidentity.

the class GetChangelogBatchExtendedRequestTestCase method testFullConstructorWithListener.

/**
 * Provides test coverage for the constructor which may be used to create an
 * instance of this extended request using a full set of information and a
 * non-{@code null} entry listener.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testFullConstructorWithListener() throws Exception {
    final List<String> includeBases = Arrays.asList("dc=example,dc=com");
    final List<String> excludeBases = Arrays.asList("ou=People,dc=example,dc=com");
    final EnumSet<ChangeType> changeTypes = EnumSet.of(ChangeType.ADD, ChangeType.DELETE, ChangeType.MODIFY_DN);
    GetChangelogBatchExtendedRequest r = new GetChangelogBatchExtendedRequest(new TestChangelogEntryListener(), new EndOfChangelogStartingPoint(), -1, -1L, true, includeBases, excludeBases, changeTypes, false, "uid=test.user,ou=People,dc=example,dc=com", new IgnoreAttributesChangeSelectionCriteria(true), false, true, new Control("1.2.3.4"));
    assertNotNull(r.getEntryListener());
    assertTrue(r.getEntryListener() instanceof TestChangelogEntryListener);
    r = new GetChangelogBatchExtendedRequest(r);
    r = r.duplicate();
    assertNotNull(r);
    assertNotNull(r.getStartingPoint());
    assertTrue(r.getStartingPoint() instanceof EndOfChangelogStartingPoint);
    assertEquals(r.getMaxChanges(), 0);
    assertEquals(r.getMaxWaitTimeMillis(), 0L);
    assertTrue(r.waitForMaxChanges());
    assertNotNull(r.getIncludeBaseDNs());
    assertEquals(r.getIncludeBaseDNs(), includeBases);
    assertNotNull(r.getExcludeBaseDNs());
    assertEquals(r.getExcludeBaseDNs(), excludeBases);
    assertNotNull(r.getChangeTypes());
    assertEquals(r.getChangeTypes(), changeTypes);
    assertFalse(r.continueOnMissingChanges());
    assertNotNull(r.getPareEntriesForUserDN());
    assertEquals(new DN(r.getPareEntriesForUserDN()), new DN("uid=test.user,ou=People,dc=example,dc=com"));
    assertNotNull(r.getChangeSelectionCriteria());
    assertFalse(r.includeSoftDeletedEntryMods());
    assertTrue(r.includeSoftDeletedEntryDeletes());
    assertNotNull(r.getExtendedRequestName());
    assertNotNull(r.toString());
}
Also used : Control(com.unboundid.ldap.sdk.Control) ChangeType(com.unboundid.ldap.sdk.ChangeType) DN(com.unboundid.ldap.sdk.DN) ASN1OctetString(com.unboundid.asn1.ASN1OctetString) Test(org.testng.annotations.Test)

Example 4 with ChangeType

use of com.unboundid.ldap.sdk.ChangeType in project ldapsdk by pingidentity.

the class LDIFDiff method doToolProcessing.

/**
 * {@inheritDoc}
 */
@Override()
@NotNull()
public ResultCode doToolProcessing() {
    // Get the change types to use for processing.
    final Set<ChangeType> changeTypes = EnumSet.noneOf(ChangeType.class);
    for (final String value : changeType.getValues()) {
        switch(StaticUtils.toLowerCase(value)) {
            case CHANGE_TYPE_ADD:
                changeTypes.add(ChangeType.ADD);
                break;
            case CHANGE_TYPE_DELETE:
                changeTypes.add(ChangeType.DELETE);
                break;
            case CHANGE_TYPE_MODIFY:
                changeTypes.add(ChangeType.MODIFY);
                break;
        }
    }
    // Get the schema to use when performing LDIF processing.
    final Schema schema;
    try {
        if (schemaPath.isPresent()) {
            schema = getSchema(schemaPath.getValues());
        } else if (PING_SERVER_AVAILABLE) {
            schema = getSchema(Collections.singletonList(StaticUtils.constructPath(PING_SERVER_ROOT, "config", "schema")));
        } else {
            schema = Schema.getDefaultStandardSchema();
        }
    } catch (final Exception e) {
        Debug.debugException(e);
        logCompletionMessage(true, ERR_LDIF_DIFF_CANNOT_GET_SCHEMA.get(StaticUtils.getExceptionMessage(e)));
        return ResultCode.LOCAL_ERROR;
    }
    // Identify the sets of include and exclude attributes.
    final Set<String> includeAttrs;
    if (includeAttribute.isPresent()) {
        final Set<String> s = new HashSet<>();
        for (final String includeAttr : includeAttribute.getValues()) {
            final String lowerName = StaticUtils.toLowerCase(includeAttr);
            s.add(lowerName);
            final AttributeTypeDefinition at = schema.getAttributeType(lowerName);
            if (at != null) {
                s.add(StaticUtils.toLowerCase(at.getOID()));
                for (final String name : at.getNames()) {
                    s.add(StaticUtils.toLowerCase(name));
                }
            }
        }
        includeAttrs = Collections.unmodifiableSet(s);
    } else {
        includeAttrs = Collections.emptySet();
    }
    final Set<String> excludeAttrs;
    if (excludeAttribute.isPresent()) {
        final Set<String> s = new HashSet<>();
        for (final String excludeAttr : excludeAttribute.getValues()) {
            final String lowerName = StaticUtils.toLowerCase(excludeAttr);
            s.add(lowerName);
            final AttributeTypeDefinition at = schema.getAttributeType(lowerName);
            if (at != null) {
                s.add(StaticUtils.toLowerCase(at.getOID()));
                for (final String name : at.getNames()) {
                    s.add(StaticUtils.toLowerCase(name));
                }
            }
        }
        excludeAttrs = Collections.unmodifiableSet(s);
    } else {
        excludeAttrs = Collections.emptySet();
    }
    // Read the source and target LDIF files into memory.
    final TreeMap<DN, Entry> sourceEntries;
    try {
        sourceEntries = readEntries(sourceLDIF.getValue(), sourceEncryptionPassphraseFile.getValue(), schema);
        out(INFO_LDIF_DIFF_READ_FROM_SOURCE_LDIF.get(sourceLDIF.getValue().getName(), sourceEntries.size()));
    } catch (final LDAPException e) {
        Debug.debugException(e);
        logCompletionMessage(true, ERR_LDIF_DIFF_CANNOT_READ_SOURCE_LDIF.get(sourceLDIF.getValue().getAbsolutePath(), e.getMessage()));
        return e.getResultCode();
    }
    final TreeMap<DN, Entry> targetEntries;
    try {
        targetEntries = readEntries(targetLDIF.getValue(), targetEncryptionPassphraseFile.getValue(), schema);
        out(INFO_LDIF_DIFF_READ_FROM_TARGET_LDIF.get(targetLDIF.getValue().getName(), targetEntries.size()));
        out();
    } catch (final LDAPException e) {
        Debug.debugException(e);
        logCompletionMessage(true, ERR_LDIF_DIFF_CANNOT_READ_TARGET_LDIF.get(targetLDIF.getValue().getAbsolutePath(), e.getMessage()));
        return e.getResultCode();
    }
    final String outputFilePath;
    if (outputLDIF.isPresent()) {
        outputFilePath = outputLDIF.getValue().getAbsolutePath();
    } else {
        outputFilePath = "{STDOUT}";
    }
    // Open the output file for writing.
    long addCount = 0L;
    long deleteCount = 0L;
    long modifyCount = 0L;
    try (OutputStream outputStream = openOutputStream();
        LDIFWriter ldifWriter = new LDIFWriter(outputStream)) {
        // output file.
        if (changeTypes.contains(ChangeType.ADD)) {
            try {
                addCount = writeAdds(sourceEntries, targetEntries, ldifWriter, schema, includeAttrs, excludeAttrs);
            } catch (final LDAPException e) {
                Debug.debugException(e);
                logCompletionMessage(true, ERR_LDIF_DIFF_ERROR_WRITING_OUTPUT.get(outputFilePath, e.getMessage()));
                return e.getResultCode();
            }
        }
        // remove references to entries that will be removed.
        if (changeTypes.contains(ChangeType.MODIFY)) {
            try {
                modifyCount = writeModifications(sourceEntries, targetEntries, ldifWriter, schema, includeAttrs, excludeAttrs);
            } catch (final LDAPException e) {
                Debug.debugException(e);
                logCompletionMessage(true, ERR_LDIF_DIFF_ERROR_WRITING_OUTPUT.get(outputFilePath, e.getMessage()));
                return e.getResultCode();
            }
        }
        // source entries) and write them to the output file.
        if (changeTypes.contains(ChangeType.DELETE)) {
            try {
                deleteCount = writeDeletes(sourceEntries, targetEntries, ldifWriter, schema, includeAttrs, excludeAttrs);
            } catch (final LDAPException e) {
                Debug.debugException(e);
                logCompletionMessage(true, ERR_LDIF_DIFF_ERROR_WRITING_OUTPUT.get(outputFilePath, e.getMessage()));
                return e.getResultCode();
            }
        }
        // If we've gotten here, then everything was successful.
        ldifWriter.flush();
        logCompletionMessage(false, INFO_LDIF_DIFF_COMPLETED.get());
        if (changeTypes.contains(ChangeType.ADD)) {
            out(INFO_LDIF_DIFF_COMPLETED_ADD_COUNT.get(addCount));
        }
        if (changeTypes.contains(ChangeType.MODIFY)) {
            out(INFO_LDIF_DIFF_COMPLETED_MODIFY_COUNT.get(modifyCount));
        }
        if (changeTypes.contains(ChangeType.DELETE)) {
            out(INFO_LDIF_DIFF_COMPLETED_DELETE_COUNT.get(deleteCount));
        }
        return ResultCode.SUCCESS;
    } catch (final LDAPException e) {
        Debug.debugException(e);
        logCompletionMessage(true, ERR_LDIF_DIFF_CANNOT_OPEN_OUTPUT.get(outputFilePath, e.getMessage()));
        return e.getResultCode();
    } catch (final Exception e) {
        Debug.debugException(e);
        logCompletionMessage(true, ERR_LDIF_DIFF_ERROR_WRITING_OUTPUT.get(outputFilePath, StaticUtils.getExceptionMessage(e)));
        return ResultCode.LOCAL_ERROR;
    }
}
Also used : Schema(com.unboundid.ldap.sdk.schema.Schema) PassphraseEncryptedOutputStream(com.unboundid.util.PassphraseEncryptedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) DN(com.unboundid.ldap.sdk.DN) ArgumentException(com.unboundid.util.args.ArgumentException) LDAPException(com.unboundid.ldap.sdk.LDAPException) AttributeTypeDefinition(com.unboundid.ldap.sdk.schema.AttributeTypeDefinition) Entry(com.unboundid.ldap.sdk.Entry) LDAPException(com.unboundid.ldap.sdk.LDAPException) ChangeType(com.unboundid.ldap.sdk.ChangeType) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) NotNull(com.unboundid.util.NotNull)

Example 5 with ChangeType

use of com.unboundid.ldap.sdk.ChangeType in project ldapsdk by pingidentity.

the class TransformLDIF method createTranslators.

/**
 * Creates the entry and change record translators that will be used to
 * perform the transformations.
 *
 * @param  entryTranslators         A list to which all created entry
 *                                  translators should be written.
 * @param  changeRecordTranslators  A list to which all created change record
 *                                  translators should be written.
 * @param  schema                   The schema to use when processing.
 * @param  excludedEntryCount       A counter used to keep track of the number
 *                                  of entries that have been excluded from
 *                                  the result set.
 */
private void createTranslators(@NotNull final List<LDIFReaderEntryTranslator> entryTranslators, @NotNull final List<LDIFReaderChangeRecordTranslator> changeRecordTranslators, @Nullable final Schema schema, @NotNull final AtomicLong excludedEntryCount) {
    if (scrambleAttribute.isPresent()) {
        final Long seed;
        if (randomSeed.isPresent()) {
            seed = randomSeed.getValue().longValue();
        } else {
            seed = null;
        }
        final ScrambleAttributeTransformation t = new ScrambleAttributeTransformation(schema, seed, processDNs.isPresent(), scrambleAttribute.getValues(), scrambleJSONField.getValues());
        entryTranslators.add(t);
        changeRecordTranslators.add(t);
    }
    if (sequentialAttribute.isPresent()) {
        final long initialValue;
        if (initialSequentialValue.isPresent()) {
            initialValue = initialSequentialValue.getValue().longValue();
        } else {
            initialValue = 0L;
        }
        final long incrementAmount;
        if (sequentialValueIncrement.isPresent()) {
            incrementAmount = sequentialValueIncrement.getValue().longValue();
        } else {
            incrementAmount = 1L;
        }
        for (final String attrName : sequentialAttribute.getValues()) {
            final ReplaceWithCounterTransformation t = new ReplaceWithCounterTransformation(schema, attrName, initialValue, incrementAmount, textBeforeSequentialValue.getValue(), textAfterSequentialValue.getValue(), processDNs.isPresent());
            entryTranslators.add(t);
        }
    }
    if (replaceValuesAttribute.isPresent()) {
        final ReplaceAttributeTransformation t = new ReplaceAttributeTransformation(schema, replaceValuesAttribute.getValue(), replacementValue.getValues());
        entryTranslators.add(t);
    }
    if (addAttributeName.isPresent()) {
        final AddAttributeTransformation t = new AddAttributeTransformation(schema, addAttributeBaseDN.getValue(), addAttributeScope.getValue(), addAttributeFilter.getValue(), new Attribute(addAttributeName.getValue(), schema, addAttributeValue.getValues()), (!addToExistingValues.isPresent()));
        entryTranslators.add(t);
    }
    if (renameAttributeFrom.isPresent()) {
        final Iterator<String> renameFromIterator = renameAttributeFrom.getValues().iterator();
        final Iterator<String> renameToIterator = renameAttributeTo.getValues().iterator();
        while (renameFromIterator.hasNext()) {
            final RenameAttributeTransformation t = new RenameAttributeTransformation(schema, renameFromIterator.next(), renameToIterator.next(), processDNs.isPresent());
            entryTranslators.add(t);
            changeRecordTranslators.add(t);
        }
    }
    if (flattenBaseDN.isPresent()) {
        final FlattenSubtreeTransformation t = new FlattenSubtreeTransformation(schema, flattenBaseDN.getValue(), flattenAddOmittedRDNAttributesToEntry.isPresent(), flattenAddOmittedRDNAttributesToRDN.isPresent(), flattenExcludeFilter.getValue());
        entryTranslators.add(t);
    }
    if (moveSubtreeFrom.isPresent()) {
        final Iterator<DN> moveFromIterator = moveSubtreeFrom.getValues().iterator();
        final Iterator<DN> moveToIterator = moveSubtreeTo.getValues().iterator();
        while (moveFromIterator.hasNext()) {
            final MoveSubtreeTransformation t = new MoveSubtreeTransformation(moveFromIterator.next(), moveToIterator.next());
            entryTranslators.add(t);
            changeRecordTranslators.add(t);
        }
    }
    if (redactAttribute.isPresent()) {
        final RedactAttributeTransformation t = new RedactAttributeTransformation(schema, processDNs.isPresent(), (!hideRedactedValueCount.isPresent()), redactAttribute.getValues());
        entryTranslators.add(t);
        changeRecordTranslators.add(t);
    }
    if (excludeAttribute.isPresent()) {
        final ExcludeAttributeTransformation t = new ExcludeAttributeTransformation(schema, excludeAttribute.getValues());
        entryTranslators.add(t);
        changeRecordTranslators.add(t);
    }
    if (excludeEntryBaseDN.isPresent() || excludeEntryScope.isPresent() || excludeEntryFilter.isPresent()) {
        final ExcludeEntryTransformation t = new ExcludeEntryTransformation(schema, excludeEntryBaseDN.getValue(), excludeEntryScope.getValue(), excludeEntryFilter.getValue(), (!excludeNonMatchingEntries.isPresent()), excludedEntryCount);
        entryTranslators.add(t);
    }
    if (excludeChangeType.isPresent()) {
        final Set<ChangeType> changeTypes = EnumSet.noneOf(ChangeType.class);
        for (final String changeTypeName : excludeChangeType.getValues()) {
            changeTypes.add(ChangeType.forName(changeTypeName));
        }
        changeRecordTranslators.add(new ExcludeChangeTypeTransformation(changeTypes));
    }
    if (excludeRecordsWithoutChangeType.isPresent()) {
        entryTranslators.add(new ExcludeAllEntriesTransformation());
    }
    entryTranslators.add(this);
}
Also used : Attribute(com.unboundid.ldap.sdk.Attribute) DN(com.unboundid.ldap.sdk.DN) ChangeType(com.unboundid.ldap.sdk.ChangeType) AtomicLong(java.util.concurrent.atomic.AtomicLong)

Aggregations

ChangeType (com.unboundid.ldap.sdk.ChangeType)11 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)7 Test (org.testng.annotations.Test)7 Control (com.unboundid.ldap.sdk.Control)6 DN (com.unboundid.ldap.sdk.DN)5 NotNull (com.unboundid.util.NotNull)3 Entry (com.unboundid.ldap.sdk.Entry)2 LDAPException (com.unboundid.ldap.sdk.LDAPException)2 LDIFAddChangeRecord (com.unboundid.ldif.LDIFAddChangeRecord)2 LDIFDeleteChangeRecord (com.unboundid.ldif.LDIFDeleteChangeRecord)2 LDIFModifyChangeRecord (com.unboundid.ldif.LDIFModifyChangeRecord)2 ArgumentException (com.unboundid.util.args.ArgumentException)2 FileOutputStream (java.io.FileOutputStream)2 ArrayList (java.util.ArrayList)2 ASN1Boolean (com.unboundid.asn1.ASN1Boolean)1 ASN1Element (com.unboundid.asn1.ASN1Element)1 ASN1Enumerated (com.unboundid.asn1.ASN1Enumerated)1 ASN1Integer (com.unboundid.asn1.ASN1Integer)1 ASN1Long (com.unboundid.asn1.ASN1Long)1 ASN1Sequence (com.unboundid.asn1.ASN1Sequence)1