use of com.unboundid.util.json.JSONObjectReader in project ldapsdk by pingidentity.
the class OIDRegistry method getDefault.
/**
* Retrieves the default instance of this OID registry.
*
* @return The default instance of this OID registry.
*/
@NotNull()
public static OIDRegistry getDefault() {
OIDRegistry oidRegistry = DEFAULT_INSTANCE.get();
if (oidRegistry == null) {
synchronized (DEFAULT_INSTANCE) {
oidRegistry = DEFAULT_INSTANCE.get();
if (oidRegistry == null) {
final Map<OID, OIDRegistryItem> items = new TreeMap<>();
try (InputStream inputStream = OIDRegistry.class.getClassLoader().getResourceAsStream(OID_REGISTRY_JSON_RESOURCE_NAME);
JSONObjectReader jsonObjectReader = new JSONObjectReader(inputStream)) {
while (true) {
final JSONObject o = jsonObjectReader.readObject();
if (o == null) {
break;
}
try {
final OIDRegistryItem item = new OIDRegistryItem(o);
items.put(new OID(item.getOID()), item);
} catch (final Exception e) {
Debug.debugException(e);
}
}
} catch (final Exception e) {
Debug.debugException(e);
}
oidRegistry = new OIDRegistry(Collections.unmodifiableMap(items));
DEFAULT_INSTANCE.set(oidRegistry);
}
}
}
return oidRegistry;
}
use of com.unboundid.util.json.JSONObjectReader in project ldapsdk by pingidentity.
the class LDAPCompareTestCase method readJSONObjects.
/**
* Reads the contents of the specified file as a set of JSON objects.
*
* @param f The file from which the JSON objects are to be read.
*
* @return A map containing the JSON objects that were read, indexed by
* the DN of the target entry.
*
* @throws Exception If a problem is encountered while reading the file or
* parsing its contents as JSON objects.
*/
private static Map<DN, JSONObject> readJSONObjects(final File f) throws Exception {
final Map<DN, JSONObject> m = new LinkedHashMap<>();
try (FileInputStream inputStream = new FileInputStream(f);
JSONObjectReader jsonObjectReader = new JSONObjectReader(inputStream)) {
while (true) {
final JSONObject o = jsonObjectReader.readObject();
if (o == null) {
return m;
}
final DN dn = new DN(o.getFieldAsString("entry-dn"));
m.put(dn, o);
}
}
}
use of com.unboundid.util.json.JSONObjectReader in project ldapsdk by pingidentity.
the class OIDRegistryTestCase method testOIDRegistryJSONFile.
/**
* Examines the contents of the oid-registry.json file and ensures that all
* of the definitions it contains are found in the default OID registry.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testOIDRegistryJSONFile() throws Exception {
final OIDRegistry oidRegistry = OIDRegistry.getDefault();
assertNotNull(oidRegistry);
final File baseDir = new File(System.getProperty("basedir"));
final File resourceDir = new File(baseDir, "resource");
final File registryFile = new File(resourceDir, "oid-registry.json");
final List<JSONObject> missingObjects = new ArrayList<>();
try (FileInputStream inputStream = new FileInputStream(registryFile);
JSONObjectReader reader = new JSONObjectReader(inputStream)) {
while (true) {
final JSONObject o = reader.readObject();
if (o == null) {
break;
}
final String oid = o.getFieldAsString("oid");
assertNotNull(oid, "JSON object " + o + " does not contain an OID");
if (oidRegistry.get(oid) == null) {
missingObjects.add(o);
}
}
}
if (!missingObjects.isEmpty()) {
final StringBuilder errorMessage = new StringBuilder();
errorMessage.append("The OID registry was missing information about " + "one or more JSON objects:");
errorMessage.append(StaticUtils.EOL);
for (final JSONObject o : missingObjects) {
errorMessage.append(StaticUtils.EOL);
errorMessage.append(o.toSingleLineString());
errorMessage.append(StaticUtils.EOL);
try {
new OIDRegistryItem(o);
} catch (final Exception e) {
errorMessage.append(e.getMessage());
errorMessage.append(StaticUtils.EOL);
}
}
fail(errorMessage.toString());
}
}
use of com.unboundid.util.json.JSONObjectReader in project ldapsdk by pingidentity.
the class LDAPSearchTestCase method testJSONOutputFormat.
/**
* Provides test coverage for the tool when configured to use the JSON output
* format.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testJSONOutputFormat() throws Exception {
final File outputFile = createTempFile();
assertEquals(LDAPSearch.main(NULL_OUTPUT_STREAM, NULL_OUTPUT_STREAM, "--hostname", "localhost", "--port", String.valueOf(ds.getListenPort()), "--baseDN", "dc=example,dc=com", "--scope", "sub", "--outputFormat", "JSON", "--requestedAttribute", "uid", "--requestedAttribute", "givenName", "--requestedAttribute", "sn", "--outputFile", outputFile.getAbsolutePath(), "(objectClass=person)"), ResultCode.SUCCESS);
final JSONObjectReader reader = new JSONObjectReader(new FileInputStream(outputFile));
while (true) {
final JSONObject o = reader.readObject();
if (o == null) {
break;
}
}
reader.close();
}
use of com.unboundid.util.json.JSONObjectReader in project ldapsdk by pingidentity.
the class OIDLookupTestCase method getJSONObjects.
/**
* Parses the provided output to extract all of the JSON objects that it
* contains.
*
* @param output The raw output generated by the tool.
*
* @return A list of the JSON objects included in the output.
*
* @throws Exception If an unexpected problem occurs.
*/
private static List<JSONObject> getJSONObjects(final byte[] output) throws Exception {
try (ByteArrayInputStream in = new ByteArrayInputStream(output);
JSONObjectReader reader = new JSONObjectReader(in)) {
List<JSONObject> objects = new ArrayList<>();
while (true) {
final JSONObject o = reader.readObject();
if (o == null) {
return objects;
}
objects.add(o);
}
}
}
Aggregations