Search in sources :

Example 1 with JSONObjectReader

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;
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) InputStream(java.io.InputStream) JSONObjectReader(com.unboundid.util.json.JSONObjectReader) TreeMap(java.util.TreeMap)

Example 2 with JSONObjectReader

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);
        }
    }
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) JSONObjectReader(com.unboundid.util.json.JSONObjectReader) DN(com.unboundid.ldap.sdk.DN) FileInputStream(java.io.FileInputStream) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with JSONObjectReader

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());
    }
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) ArrayList(java.util.ArrayList) JSONObjectReader(com.unboundid.util.json.JSONObjectReader) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.testng.annotations.Test)

Example 4 with JSONObjectReader

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();
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) JSONObjectReader(com.unboundid.util.json.JSONObjectReader) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.testng.annotations.Test)

Example 5 with JSONObjectReader

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);
        }
    }
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) JSONObjectReader(com.unboundid.util.json.JSONObjectReader)

Aggregations

JSONObject (com.unboundid.util.json.JSONObject)8 JSONObjectReader (com.unboundid.util.json.JSONObjectReader)8 FileInputStream (java.io.FileInputStream)4 Test (org.testng.annotations.Test)4 File (java.io.File)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ArrayList (java.util.ArrayList)2 DN (com.unboundid.ldap.sdk.DN)1 ResultCode (com.unboundid.ldap.sdk.ResultCode)1 NotNull (com.unboundid.util.NotNull)1 JSONArray (com.unboundid.util.json.JSONArray)1 JSONField (com.unboundid.util.json.JSONField)1 JSONString (com.unboundid.util.json.JSONString)1 InputStream (java.io.InputStream)1 ParseException (java.text.ParseException)1 LinkedHashMap (java.util.LinkedHashMap)1 TreeMap (java.util.TreeMap)1