| opendj-core/pom.xml | ●●●●● patch | view | raw | blame | history | |
| opendj-core/src/main/java/org/openidentityplatform/opendj/ldif/JSONEntryReader.java | ●●●●● patch | view | raw | blame | history | |
| opendj-core/src/main/java/org/openidentityplatform/opendj/ldif/JSONEntryWriter.java | ●●●●● patch | view | raw | blame | history |
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> opendj-core/src/main/java/org/openidentityplatform/opendj/ldif/JSONEntryReader.java
New file @@ -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; } } opendj-core/src/main/java/org/openidentityplatform/opendj/ldif/JSONEntryWriter.java
New file @@ -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; } }