From 9a19a43849c1673ef63a5a233af07054016ff3bb Mon Sep 17 00:00:00 2001
From: vharseko <vharseko@3a-systems.ru>
Date: Thu, 21 Oct 2021 11:10:16 +0000
Subject: [PATCH] ADD JSONEntryWriter JSONEntryReader (#196)
---
opendj-core/src/main/java/org/openidentityplatform/opendj/ldif/JSONEntryWriter.java | 75 +++++++++++++++++++++++++
opendj-core/src/main/java/org/openidentityplatform/opendj/ldif/JSONEntryReader.java | 61 ++++++++++++++++++++
opendj-core/pom.xml | 5 +
3 files changed, 141 insertions(+), 0 deletions(-)
diff --git a/opendj-core/pom.xml b/opendj-core/pom.xml
index a40b4db..f6b5040 100644
--- a/opendj-core/pom.xml
+++ b/opendj-core/pom.xml
@@ -103,6 +103,11 @@
<artifactId>bctls-fips</artifactId>
<version>${bctls.fips.version}</version>
</dependency>
+
+ <dependency>
+ <groupId>com.fasterxml.jackson.core</groupId>
+ <artifactId>jackson-databind</artifactId>
+ </dependency>
</dependencies>
diff --git a/opendj-core/src/main/java/org/openidentityplatform/opendj/ldif/JSONEntryReader.java b/opendj-core/src/main/java/org/openidentityplatform/opendj/ldif/JSONEntryReader.java
new file mode 100644
index 0000000..4073fc8
--- /dev/null
+++ b/opendj-core/src/main/java/org/openidentityplatform/opendj/ldif/JSONEntryReader.java
@@ -0,0 +1,61 @@
+package org.openidentityplatform.opendj.ldif;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.List;
+import java.util.Map;
+
+import org.forgerock.opendj.ldap.DecodeException;
+import org.forgerock.opendj.ldap.Entry;
+import org.forgerock.opendj.ldap.LinkedHashMapEntry;
+import org.forgerock.opendj.ldif.EntryReader;
+
+import com.fasterxml.jackson.core.JsonParseException;
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.core.JsonToken;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+
+public final class JSONEntryReader implements EntryReader {
+
+ static ObjectMapper mapper = new ObjectMapper();
+
+ JsonParser parser ;
+ JsonToken token=null;
+
+ public JSONEntryReader(final InputStream in) throws JsonParseException, IOException {
+ parser=mapper.getFactory().createParser(in);
+ if ( parser.nextToken() != JsonToken.START_ARRAY ) {
+ throw new JsonParseException(parser, "invalid format" );
+ }
+ token=parser.nextToken();
+ }
+
+ @Override
+ public void close() throws IOException {
+ parser.close();
+ }
+
+ @Override
+ public boolean hasNext() throws DecodeException, IOException {
+ return token == JsonToken.START_OBJECT ;
+ }
+
+
+ @Override
+ public Entry readEntry() throws DecodeException, IOException {
+ if (hasNext()) {
+ final Map<String,List<Map<String,String>>> entry=mapper.readValue(parser,new TypeReference<Map<String,List<Map<String,String>>>>() {});
+ final Entry res=new LinkedHashMapEntry(entry.keySet().iterator().next());
+ for (Map<String,String> attrs : entry.get(res.getName().toString())) {
+ for (java.util.Map.Entry<String,String> attr : attrs.entrySet()) {
+ res.addAttribute(attr.getKey(), attr.getValue());
+ }
+ }
+ token=parser.nextToken();
+ return res;
+ }
+ return null;
+ }
+}
diff --git a/opendj-core/src/main/java/org/openidentityplatform/opendj/ldif/JSONEntryWriter.java b/opendj-core/src/main/java/org/openidentityplatform/opendj/ldif/JSONEntryWriter.java
new file mode 100644
index 0000000..364c1a8
--- /dev/null
+++ b/opendj-core/src/main/java/org/openidentityplatform/opendj/ldif/JSONEntryWriter.java
@@ -0,0 +1,75 @@
+package org.openidentityplatform.opendj.ldif;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.PrintWriter;
+import java.util.AbstractMap;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.TreeMap;
+
+import org.forgerock.opendj.ldap.Attribute;
+import org.forgerock.opendj.ldap.ByteSequence;
+import org.forgerock.opendj.ldap.ByteString;
+import org.forgerock.opendj.ldap.Entry;
+import org.forgerock.opendj.ldif.EntryWriter;
+import org.forgerock.util.Reject;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+public final class JSONEntryWriter implements EntryWriter {
+ static ObjectMapper mapper = new ObjectMapper();
+
+ final PrintWriter out;
+ public JSONEntryWriter(final OutputStream out) {
+ this.out=new PrintWriter(out);
+ this.out.println("[");
+ }
+
+ @Override
+ public void close() throws IOException {
+ this.out.println("]");
+ out.close();
+ }
+
+ @Override
+ public void flush() throws IOException {
+ out.flush();
+ }
+
+
+ @Override
+ public JSONEntryWriter writeComment(final CharSequence comment) throws IOException {
+ return this;
+ }
+
+ boolean firstEntry=true;
+ @Override
+ public JSONEntryWriter writeEntry(final Entry entry) throws IOException {
+ Reject.ifNull(entry);
+
+ this.out.println(((firstEntry)?" ":",")+"{\""+entry.getName().toString()+"\":[");
+ firstEntry=false;
+
+ final TreeMap<String,AbstractMap.SimpleEntry<String,ByteSequence>> attr=new TreeMap<>(); //sort by key:value
+ for (final Attribute attribute : entry.getAllAttributes()) {
+ final String attributeDescription = attribute.getAttributeDescriptionAsString();
+ if (attribute.isEmpty()) {
+ attr.put(attributeDescription, new AbstractMap.SimpleEntry<String,ByteSequence>(attributeDescription,ByteString.empty()) );
+ } else {
+ for (final ByteString value : attribute) {
+ attr.put(attributeDescription+value, new AbstractMap.SimpleEntry<String,ByteSequence>(attributeDescription,value));
+ }
+ }
+ }
+ boolean first=true;
+ for (AbstractMap.SimpleEntry<String,ByteSequence> kv : attr.values()) {
+ final Map<String, String> params = new HashMap<>(1);
+ params.put(kv.getKey(),kv.getValue().toString());
+ this.out.println(" "+((first)?" ":",")+mapper.writeValueAsString(params));
+ first=false;
+ }
+ this.out.println("]}");
+ return this;
+ }
+}
--
Gitblit v1.10.0