From 91d2c2f00090718004ea0e04419678e2b4da0c5e Mon Sep 17 00:00:00 2001
From: Matthew Swift <matthew.swift@forgerock.com>
Date: Thu, 22 Sep 2016 22:06:04 +0000
Subject: [PATCH] OPENDJ-2877: add methods for generating LDIF from entries and change records
---
opendj-core/src/main/java/org/forgerock/opendj/ldap/Entries.java | 12 ++++++
opendj-core/src/main/java/org/forgerock/opendj/ldif/LDIF.java | 39 +++++++++++++++++++
opendj-core/src/main/java/org/forgerock/opendj/ldap/AbstractEntry.java | 15 -------
3 files changed, 52 insertions(+), 14 deletions(-)
diff --git a/opendj-core/src/main/java/org/forgerock/opendj/ldap/AbstractEntry.java b/opendj-core/src/main/java/org/forgerock/opendj/ldap/AbstractEntry.java
index 3c85008..7abbfc5 100644
--- a/opendj-core/src/main/java/org/forgerock/opendj/ldap/AbstractEntry.java
+++ b/opendj-core/src/main/java/org/forgerock/opendj/ldap/AbstractEntry.java
@@ -237,20 +237,7 @@
@Override
public String toString() {
- final StringBuilder builder = new StringBuilder();
- builder.append('"');
- builder.append(getName());
- builder.append("\":{");
- boolean firstValue = true;
- for (final Attribute attribute : getAllAttributes()) {
- if (!firstValue) {
- builder.append(',');
- }
- builder.append(attribute);
- firstValue = false;
- }
- builder.append('}');
- return builder.toString();
+ return Entries.toLDIF(this);
}
private boolean isAssignable(final AttributeDescription from, final AttributeDescription to) {
diff --git a/opendj-core/src/main/java/org/forgerock/opendj/ldap/Entries.java b/opendj-core/src/main/java/org/forgerock/opendj/ldap/Entries.java
index 21596a0..0c59c87 100644
--- a/opendj-core/src/main/java/org/forgerock/opendj/ldap/Entries.java
+++ b/opendj-core/src/main/java/org/forgerock/opendj/ldap/Entries.java
@@ -933,6 +933,18 @@
}
/**
+ * Returns the LDIF representation of {@code entry}. All attributes will be included and no wrapping will be
+ * performed. This method can be useful when debugging applications.
+ *
+ * @param entry
+ * The entry to be converted to LDIF.
+ * @return The LDIF representation of {@code entry}.
+ */
+ public static String toLDIF(final Entry entry) {
+ return LDIF.toLDIF(entry);
+ }
+
+ /**
* Returns a read-only view of {@code entry} and its attributes. Query
* operations on the returned entry and its attributes "read-through" to the
* underlying entry or attribute, and attempts to modify the returned entry
diff --git a/opendj-core/src/main/java/org/forgerock/opendj/ldif/LDIF.java b/opendj-core/src/main/java/org/forgerock/opendj/ldif/LDIF.java
index 6b6fba0..da1aa21 100644
--- a/opendj-core/src/main/java/org/forgerock/opendj/ldif/LDIF.java
+++ b/opendj-core/src/main/java/org/forgerock/opendj/ldif/LDIF.java
@@ -18,6 +18,7 @@
import static com.forgerock.opendj.ldap.CoreMessages.*;
import java.io.IOException;
+import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@@ -788,6 +789,44 @@
};
}
+ /**
+ * Returns the LDIF representation of {@code entry}. All attributes will be included and no wrapping will be
+ * performed. This method can be useful when debugging applications.
+ *
+ * @param entry
+ * The entry to be converted to LDIF.
+ * @return The LDIF representation of {@code entry}.
+ */
+ public static String toLDIF(final Entry entry) {
+ try (final StringWriter writer = new StringWriter();
+ final LDIFEntryWriter ldifWriter = new LDIFEntryWriter(writer)) {
+ ldifWriter.writeEntry(entry);
+ ldifWriter.flush();
+ return writer.toString();
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ /**
+ * Returns the LDIF representation of {@code change}. No wrapping will be performed. This method can be useful when
+ * debugging applications.
+ *
+ * @param change
+ * The change record to be converted to LDIF.
+ * @return The LDIF representation of {@code change}.
+ */
+ public static String toLDIF(final ChangeRecord change) {
+ try (final StringWriter writer = new StringWriter();
+ final LDIFChangeRecordWriter ldifWriter = new LDIFChangeRecordWriter(writer)) {
+ ldifWriter.writeChangeRecord(change).toString();
+ ldifWriter.flush();
+ return writer.toString();
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
private static List<byte[][]> readEntriesAsList(final EntryReader reader) throws IOException {
final List<byte[][]> entries = new ArrayList<>();
--
Gitblit v1.10.0