Example usage for org.json.simple JSONObject containsKey

List of usage examples for org.json.simple JSONObject containsKey

Introduction

In this page you can find the example usage for org.json.simple JSONObject containsKey.

Prototype

boolean containsKey(Object key);

Source Link

Document

Returns true if this map contains a mapping for the specified key.

Usage

From source file:org.jolokia.jvmagent.handler.JolokiaHttpHandlerTest.java

@Test
public void restrictorWithNoReverseDnsLookup() throws URISyntaxException, IOException, ParseException {
    Configuration config = getConfig(ConfigKey.RESTRICTOR_CLASS, TestReverseDnsLookupRestrictor.class.getName(),
            ConfigKey.ALLOW_DNS_REVERSE_LOOKUP, "false");
    InetSocketAddress address = new InetSocketAddress(8080);
    TestReverseDnsLookupRestrictor.expectedRemoteHostsToCheck = new String[] {
            address.getAddress().getHostAddress() };
    JSONObject resp = simpleMemoryGetReadRequest(config);
    assertFalse(resp.containsKey("error"));
}

From source file:org.jolokia.jvmagent.handler.JolokiaHttpHandlerTest.java

@Test
public void restrictorWithReverseDnsLookup() throws URISyntaxException, IOException, ParseException {
    Configuration config = getConfig(ConfigKey.RESTRICTOR_CLASS, TestReverseDnsLookupRestrictor.class.getName(),
            ConfigKey.ALLOW_DNS_REVERSE_LOOKUP, "true");
    InetSocketAddress address = new InetSocketAddress(8080);
    TestReverseDnsLookupRestrictor.expectedRemoteHostsToCheck = new String[] { address.getHostName(),
            address.getAddress().getHostAddress() };
    JSONObject resp = simpleMemoryGetReadRequest(config);
    assertFalse(resp.containsKey("error"));
}

From source file:org.jolokia.jvmagent.JolokiaHttpHandlerTest.java

@Test
public void customRestrictor() throws URISyntaxException, IOException, ParseException {
    System.setProperty("jolokia.test1.policy.location", "access-restrictor.xml");
    System.setProperty("jolokia.test2.policy.location", "access-restrictor");
    for (String[] params : new String[][] { { "classpath:/access-restrictor.xml", "not allowed" },
            { "file:///not-existing.xml", "No access" },
            { "classpath:/${prop:jolokia.test1.policy.location}", "not allowed" },
            { "classpath:/${prop:jolokia.test2.policy.location}.xml", "not allowed" } }) {
        Configuration config = getConfig(ConfigKey.POLICY_LOCATION, params[0]);
        JolokiaHttpHandler newHandler = new JolokiaHttpHandler(config);
        HttpExchange exchange = prepareExchange(
                "http://localhost:8080/jolokia/read/java.lang:type=Memory/HeapMemoryUsage");
        // Simple GET method
        expect(exchange.getRequestMethod()).andReturn("GET");
        Headers header = new Headers();
        ByteArrayOutputStream out = prepareResponse(handler, exchange, header);
        newHandler.start(false);//from   w  ww  .  j a  va2  s.  c o  m
        try {
            newHandler.doHandle(exchange);
        } finally {
            newHandler.stop();
        }
        JSONObject resp = (JSONObject) new JSONParser().parse(out.toString());
        assertTrue(resp.containsKey("error"));
        assertTrue(((String) resp.get("error")).contains(params[1]));
    }
}

From source file:org.kuali.mobility.people.dao.DirectoryDaoUMImpl.java

@Override
public Person lookupPerson(String personId) {
    Person person = null;/*from  w  w  w  .j  a v a2 s  .com*/

    BufferedReader reader = null;
    try {
        URL service = new URL(PERSON_LOOKUP_URL + personId);
        LOG.debug("Personlookupurl :" + PERSON_LOOKUP_URL + personId);

        String jsonData = null;
        jsonData = IOUtils.toString(service, DEFAULT_CHARACTER_SET);

        LOG.debug("Attempting to parse JSON using JSON.simple.");
        LOG.debug(jsonData);

        JSONParser parser = new JSONParser();

        Object rootObj = parser.parse(jsonData);

        JSONObject jsonObject = (JSONObject) rootObj;
        JSONObject jsonPerson = (JSONObject) jsonObject.get("person");

        if (jsonPerson == null) {
            LOG.debug("Results were not parsed, " + personId + " not found.");
        } else if (jsonPerson.containsKey("errors") && jsonPerson.get("errors") != null
                && jsonPerson.get("errors").hashCode() != 0) {
            // TODO get error description
            // Object v = raw.get("errors");
            LOG.debug("errors:" + jsonPerson.get("errors"));
        } else {
            person = parsePerson(jsonPerson);
        }

    } catch (UnsupportedEncodingException uee) {
        LOG.error(uee.toString());
    } catch (IOException ioe) {
        LOG.error(ioe.toString());
    } catch (ParseException pe) {
        LOG.error(pe.getLocalizedMessage(), pe);
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException logOrIgnore) {
                LOG.error(logOrIgnore.getLocalizedMessage());
            }
        }
    }
    return person;
}

From source file:org.mozilla.android.sync.SyncCryptographer.java

public CryptoStatusBundle decryptWBO(String jsonString) {

    // Get json from string
    JSONObject json = null;//from ww w .  java  2s .com
    JSONObject payload = null;
    try {
        json = getJSONObject(jsonString);
        payload = getJSONObject(json, KEY_PAYLOAD);

    } catch (Exception e) {
        return new CryptoStatusBundle(CryptoStatus.INVALID_JSON, jsonString);
    }

    // Check that paylod contains all pieces for crypto
    if (!payload.containsKey(KEY_CIPHER_TEXT) || !payload.containsKey(KEY_IV)
            || !payload.containsKey(KEY_HMAC)) {
        return new CryptoStatusBundle(CryptoStatus.INVALID_JSON, jsonString);
    }

    String id = (String) json.get(KEY_ID);
    if (id.equalsIgnoreCase(ID_CRYPTO_KEYS)) {
        // If this is a crypto keys bundle, handle it seperately
        return decryptKeysWBO(payload);
    } else if (keys == null) {
        // Otherwise, make sure we have crypto keys before continuing
        return new CryptoStatusBundle(CryptoStatus.MISSING_KEYS, jsonString);
    }

    byte[] clearText = decryptPayload(payload, this.keys);

    return new CryptoStatusBundle(CryptoStatus.OK, new String(clearText));

}

From source file:org.mozilla.android.sync.SyncCryptographer.java

private CryptoStatusBundle decryptKeysWBO(JSONObject payload) {

    // Get the keys to decrypt the crypto keys bundle
    KeyBundle cryptoKeysBundleKeys;/*from w  w w  .j a va2  s.  co  m*/
    try {
        cryptoKeysBundleKeys = getCryptoKeysBundleKeys();
    } catch (Exception e) {
        return new CryptoStatusBundle(CryptoStatus.MISSING_SYNCKEY_OR_USER, payload.toString());
    }

    byte[] cryptoKeysBundle = decryptPayload(payload, cryptoKeysBundleKeys);

    // Extract decrypted keys
    InputStream stream = new ByteArrayInputStream(cryptoKeysBundle);
    Reader in = new InputStreamReader(stream);
    JSONObject json = null;
    try {
        json = (JSONObject) new JSONParser().parse(in);
    } catch (Exception e) {
        e.printStackTrace();
    }

    // Verify that this is indeed the crypto/keys bundle and decryption worked
    String id = (String) json.get(KEY_ID);
    String collection = (String) json.get(KEY_COLLECTION);
    if (id.equalsIgnoreCase(ID_CRYPTO_KEYS) && collection.equalsIgnoreCase(CRYPTO_KEYS_COLLECTION)
            && json.containsKey(KEY_DEFAULT_COLLECTION)) {

        // Extract the keys and add them to this
        Object jsonKeysObj = json.get(KEY_DEFAULT_COLLECTION);
        if (jsonKeysObj.getClass() != JSONArray.class) {
            return new CryptoStatusBundle(CryptoStatus.INVALID_KEYS_BUNDLE, json.toString());
        }

        JSONArray jsonKeys = (JSONArray) jsonKeysObj;
        this.setKeys((String) jsonKeys.get(0), (String) jsonKeys.get(1));

        // Return the string containing the decrypted payload
        return new CryptoStatusBundle(CryptoStatus.OK, new String(cryptoKeysBundle));
    } else {
        return new CryptoStatusBundle(CryptoStatus.INVALID_KEYS_BUNDLE, json.toString());
    }
}

From source file:org.nbgames.core.json.JsonHelper.java

public static boolean optBoolean(JSONObject object, String key) {
    if (object.containsKey(key)) {
        return (boolean) object.get(key);
    } else {/*  w  ww .  j a v  a 2  s.com*/
        return false;
    }
}

From source file:org.nbgames.core.json.JsonHelper.java

public static int optInt(JSONObject object, String key) {
    if (object.containsKey(key)) {
        return getInt(object, key);
    } else {/*from  w  w w .  j a va 2  s.  c  o  m*/
        return 0;
    }
}

From source file:org.nbgames.core.json.JsonHelper.java

public static String optString(JSONObject object, String key) {
    if (object.containsKey(key)) {
        return (String) object.get(key);
    } else {/*from  w  ww  .  jav a2 s  .c  om*/
        return "";
    }
}

From source file:org.nbgames.core.json.JsonHelper.java

public static String parseLocalizedKey(JSONObject jsonObject, String key) {
    String value = "";
    String localizedKey = key + getLanguageSuffix();

    if (jsonObject.containsKey(localizedKey)) {
        value = (String) jsonObject.get(localizedKey);
    } else if (jsonObject.containsKey(key)) {
        value = (String) jsonObject.get(key);
    }/*from www  . j  av  a2  s  .c  o m*/

    return value;
}