com.nimbusds.common.ldap.JSONResultFormatter Maven / Gradle / Ivy
package com.nimbusds.common.ldap;
import java.util.*;
import java.util.stream.Collectors;
import com.unboundid.ldap.sdk.Attribute;
import com.unboundid.ldap.sdk.Entry;
import com.unboundid.ldap.sdk.SearchResult;
import com.unboundid.ldap.sdk.SearchResultReference;
import com.unboundid.ldap.sdk.schema.AttributeSyntaxDefinition;
import com.unboundid.ldap.sdk.schema.AttributeTypeDefinition;
import com.unboundid.ldap.sdk.schema.AttributeUsage;
import com.unboundid.ldap.sdk.schema.MatchingRuleDefinition;
import com.unboundid.ldap.sdk.schema.MatchingRuleUseDefinition;
import com.unboundid.ldap.sdk.schema.ObjectClassDefinition;
import com.unboundid.ldap.sdk.schema.ObjectClassType;
import com.unboundid.util.Base64;
/**
* Static methods to format complex LDAP result structures as JSON.
*
* See the related {@link LDIFResultFormatter} class for LDIF result
* formatting.
*/
public class JSONResultFormatter {
/**
* Formats an LDAP directory entry as a JSON object.
*
*
Format:
*
*
* {
* "DN" : "uid=user001,ou=people,dc=example,dc=com",
* "attribute-name-1" : [value-1, value-2, value-3, ...],
* "attribute-name-2" : [value-1, value-2, ...],
* "attribute-name-3" : [value-1, ...],
* ...
* }
*
*
* @param entry The directory entry. Must not be {@code null}.
* @param binary The name of the attributes to Base64 encode. Must
* not be {@code null}.
* @param omitDN If {@code true} the DN will be omitted from the
* returned map.
* @param normalize If {@code true} attribute names will be converted
* to lower case.
*
* @return A JSON object representing the directory entry.
*/
public static Map formatEntry(final Entry entry,
final Set binary,
final boolean omitDN,
final boolean normalize) {
Map jsonObject = new LinkedHashMap<>();
if (! omitDN)
jsonObject.put("DN", entry.getDN());
Collectionattributes = entry.getAttributes();
for (final Attribute a : attributes) {
List values = new LinkedList<>();
// Does the attribute require BASE-64 encoding
if (binary.contains(a.getName().toLowerCase())) {
// Apply BASE64 encoding if the attribute
// contains at least one binary value
for (final byte[] binVal: a.getValueByteArrays())
values.add(Base64.encode(binVal));
}
else {
// Default UTF-8 encoding (from LDAP SDK)
Collections.addAll(values, a.getValues());
}
String name = a.getBaseName();
if (normalize)
name = name.toLowerCase();
jsonObject.put(name, values);
}
return jsonObject;
}
/**
* Formats an LDAP directory entry as a JSON object.
*
* Format:
*
*
* {
* "DN" : "uid=user001,ou=people,dc=example,dc=com",
* "attribute-name-1" : [value-1, value-2, value-3, ...],
* "attribute-name-2" : [value-1, value-2, ...],
* "attribute-name-3" : [value-1, ...],
* ...
* }
*
*
* @param entry The directory entry. Must not be {@code null}.
* @param binary The name of the attributes to Base64 encode. Must
* not be {@code null}.
* @param normalize If {@code true} attribute names will be converted
* to lower case.
*
* @return A JSON object representing the directory entry.
*/
public static Map formatEntry(final Entry entry,
final Set binary,
final boolean normalize) {
return formatEntry(entry, binary, false, normalize);
}
/**
* Formats a LDAP search result as a JSON object containing matches
* and referrals.
*
* Format:
*
*
* {
* "matches" : [ { entry-1 }, { entry-2 }, { entry-3 }, ...],
* "referrals" : [ "url-1", "url-2", "url-3", ...],
* "page" : { "totalEntryCount" : n,
* "more" : true|false,
* "cookie" : "..." },
* "vlv" : { "totalEntryCount" : n,
* "offset" : n,
* "cookie" : "..." }
* }
*
*
* where {@code entry-n} are formatted by the {@link #formatEntry}
* method.
*
* @param sr The search result. Must not be {@code null}.
* @param binary The name of the attributes to Base64 encode. Must
* not be {@code null}.
* @param normalize If {@code true} attribute names will be converted
* to lower case.
*
* @return A JSON object containing the search result matches,
* referrals and optional control data.
*/
public static Map formatSearchResult(final SearchResult sr,
final Set binary,
final boolean normalize) {
Map jsonObject = new LinkedHashMap<>();
// A result consists of possible matching entries and referrals
List