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:edu.vt.vbi.patric.common.DataApiHandler.java

public SolrQuery buildSolrQuery(Map<String, String> key, String sorts, String facets, int start, int end,
        boolean highlight) {

    SolrQuery query = new SolrQuery();
    SolrInterface solr = new SolrInterface();

    // Processing key map
    query.setQuery(StringHelper.stripQuoteAndParseSolrKeywordOperator(key.get("keyword")));

    if (key.containsKey("filter") && key.get("filter") != null) {
        query.addFilterQuery(key.get("filter"));
    }//from  w w  w .  j a va 2s.  c  o m
    if (key.containsKey("filter2") && key.get("filter2") != null) {
        query.addFilterQuery(key.get("filter2"));
    }

    // use SolrJoin if possible
    if (key.containsKey("join")) {
        query.addFilterQuery(key.get("join"));
    }

    if (key.containsKey("fields") && !key.get("fields").equals("")) {
        query.addField(key.get("fields"));
    }

    // sort conditions
    if (sorts != null && !sorts.equals("") && !sorts.equals("[]")) {
        try {
            JSONArray sorter = (JSONArray) new JSONParser().parse(sorts);
            for (Object aSort : sorter) {
                JSONObject jsonSort = (JSONObject) aSort;
                query.addSort(SolrQuery.SortClause.create(jsonSort.get("property").toString(),
                        jsonSort.get("direction").toString().toLowerCase()));
            }
        } catch (ParseException e) {
            LOGGER.error(e.getMessage(), e);
        }
    }

    // facet conditions
    if (facets != null && !facets.equals("") && !facets.equals("{}")) {
        query.setFacet(true).setFacetMinCount(1).setFacetLimit(-1).setFacetSort(FacetParams.FACET_SORT_COUNT)
                .set("json.nl", "map");

        try {
            JSONObject facetConditions = (JSONObject) new JSONParser().parse(facets);

            if (facetConditions.containsKey("facet.sort")) {
                String facetSort = facetConditions.get("facet.sort").toString();
                if (facetSort.equals("index")) {
                    query.setFacetSort(FacetParams.FACET_SORT_INDEX);
                }
            }

            if (facetConditions.containsKey("field_facets")) {
                String[] fieldFacets = facetConditions.get("field_facets").toString().split(",");
                query.addFacetField(fieldFacets);
            }

            if (facetConditions.containsKey("date_range_facets")
                    && !facetConditions.get("date_range_facets").equals("")) {
                String[] dateRangeFacets = facetConditions.get("date_range_facets").toString().split(",");
                for (String field : dateRangeFacets) {
                    query.addDateRangeFacet(field, solr.getRangeStartDate(), solr.getRangeEndDate(),
                            solr.getRangeDate());
                }
            }
        } catch (ParseException e) {
            LOGGER.error(e.getMessage(), e);
        }
    }

    // start & end
    query.setStart(start); // setting starting index

    if (end == -1) {
        query.setRows(MAX_ROWS);
    } else {
        query.setRows(end);
    }

    // highlight
    if (highlight) {
        query.set("hl", "on").set("hl.fl", "*");
    }

    return query;
}

From source file:com.ge.research.semtk.load.utility.ImportSpecHandler.java

/**
 * If a column has transforms in the mapping, apply them to the input raw text
 * @param raw - untransformed string//  ww w  . j  a  v  a2s .c  o m
 * @param mappingJson - single mapping entry
 * @return
 */
private String applyTransforms(String raw, JSONObject mappingJson) {
    if (mappingJson.containsKey("transformList")) {
        String ret = raw;
        JSONArray transforms = (JSONArray) mappingJson.get("transformList");
        for (int i = 0; i < transforms.size(); i += 1) {
            ret = transformsAvailable.get((String) transforms.get(i)).applyTransform(ret);
        }
        return ret;

    } else {
        return raw;
    }
}

From source file:com.pearson.eidetic.driver.threads.RefreshAwsAccountVolumes.java

private ConcurrentHashMap<Region, ArrayList<Volume>> refreshCopyVolumeSnapshots(Volume volume,
        JSONObject eideticParameters, ConcurrentHashMap<Region, ArrayList<Volume>> localCopyVolumeSnapshots,
        Region region) {/* ww  w .java 2s.  c o m*/
    if (volume == null || eideticParameters == null || region == null) {
        return localCopyVolumeSnapshots;
    }
    if (!eideticParameters.containsKey("CopySnapshot")) {
        //and it previously did
        if (volCopyHasTag_.containsKey(volume)) {
            //We leave volCopyHasTag_ at false
            return localCopyVolumeSnapshots;
            //else it previously did not, and we do not worry
        } else {
            //we continue along to the next volume.
            return localCopyVolumeSnapshots;
        }
        //It does have CopySnapshot
    } else {
        //Did it previously?
        if (volCopyHasTag_.containsKey(volume)) {
            //Yeah it does, set to true
            try {
                volCopyHasTag_.replace(volume, true);
            } catch (Exception e) {
                logger.info("awsAccountNickname=\"" + uniqueAwsAccountIdentifier_
                        + "\",Event=\"Error\", Error=\"error adding vol to CopyVolumeSnapshots\", Volume_id=\""
                        + volume.getVolumeId() + "\", stacktrace=\"" + e.toString() + System.lineSeparator()
                        + StackTrace.getStringFromStackTrace(e) + "\"");
            }
        } else {
            //It did not, we add to localCopyVolumeSnapshots
            try {
                localCopyVolumeSnapshots.get(region).add(volume);
            } catch (Exception e) {
                logger.info("awsAccountNickname=\"" + uniqueAwsAccountIdentifier_
                        + "\",Event=\"Error\", Error=\"error adding vol to CopyVolumeSnapshots\", Volume_id=\""
                        + volume.getVolumeId() + "\", stacktrace=\"" + e.toString() + System.lineSeparator()
                        + StackTrace.getStringFromStackTrace(e) + "\"");
            }

            try {
                volCopyHasTag_.put(volume, true);
            } catch (Exception e) {
                logger.info("awsAccountNickname=\"" + uniqueAwsAccountIdentifier_
                        + "\",Event=\"Error\", Error=\"error adding vol to CopyVolumeSnapshots\", Volume_id=\""
                        + volume.getVolumeId() + "\", stacktrace=\"" + e.toString() + System.lineSeparator()
                        + StackTrace.getStringFromStackTrace(e) + "\"");
            }
        }
    }
    return localCopyVolumeSnapshots;
}

From source file:com.nubits.nubot.options.NuBotOptions.java

private String toStringNoKeys() {
    String toRet = "";

    GsonBuilder gson = new GsonBuilder().setPrettyPrinting();
    //gson.registerTypeAdapter(NuBotOptions.class, new NuBotOptionsSerializer());
    Gson parser = gson.create();/*from   ww  w  . j a  va  2  s. c o m*/

    String serializedOptionsStr = parser.toJson(this);
    org.json.simple.parser.JSONParser p = new org.json.simple.parser.JSONParser();
    try {
        JSONObject serializedOptionsJSON = (JSONObject) (p.parse(serializedOptionsStr));

        //Replace sensitive information
        String[] sensitiveKeys = { "apisecret", "apikey", "rpcpass", "apiSecret", "apiKey", "rpcPass" };
        String replaceString = "hidden";

        for (int i = 0; i < sensitiveKeys.length; i++) {
            if (serializedOptionsJSON.containsKey(sensitiveKeys[i])) {
                serializedOptionsJSON.replace(sensitiveKeys[i], replaceString);
            }
        }

        toRet = serializedOptionsJSON.toString();
    } catch (org.json.simple.parser.ParseException e) {
        LOG.error(e.toString());
    }

    return toRet;
}

From source file:com.opensoc.parsing.parsers.BasicBroParser.java

@SuppressWarnings("unchecked")
public JSONObject parse(byte[] msg) {

    _LOG.trace("[OpenSOC] Starting to parse incoming message");

    String raw_message = null;/*  ww w  . ja  v  a  2s . co m*/

    try {

        raw_message = new String(msg, "UTF-8");
        _LOG.trace("[OpenSOC] Received message: " + raw_message);

        JSONObject cleaned_message = cleaner.Clean(raw_message);
        _LOG.debug("[OpenSOC] Cleaned message: " + raw_message);

        if (cleaned_message == null || cleaned_message.isEmpty())
            throw new Exception("Unable to clean message: " + raw_message);

        String key = cleaned_message.keySet().iterator().next().toString();

        if (key == null)
            throw new Exception("Unable to retrieve key for message: " + raw_message);

        JSONObject payload = (JSONObject) cleaned_message.get(key);

        String originalString = " |";
        for (Object k : payload.keySet()) {
            originalString = originalString + " " + k.toString() + ":" + payload.get(k).toString();
        }
        originalString = key.toUpperCase() + originalString;
        payload.put("original_string", originalString);

        if (payload == null)
            throw new Exception("Unable to retrieve payload for message: " + raw_message);

        if (payload.containsKey("ts")) {
            String ts = payload.remove("ts").toString();
            payload.put("timestamp", ts);
            _LOG.trace("[OpenSOC] Added ts to: " + payload);
        }

        if (payload.containsKey("id.orig_h")) {
            String source_ip = payload.remove("id.orig_h").toString();
            payload.put("ip_src_addr", source_ip);
            _LOG.trace("[OpenSOC] Added ip_src_addr to: " + payload);
        } else if (payload.containsKey("tx_hosts")) {
            JSONArray txHosts = (JSONArray) payload.remove("tx_hosts");
            if (txHosts != null && !txHosts.isEmpty()) {
                payload.put("ip_src_addr", txHosts.get(0));
                _LOG.trace("[OpenSOC] Added ip_src_addr to: " + payload);
            }
        }

        if (payload.containsKey("id.resp_h")) {
            String source_ip = payload.remove("id.resp_h").toString();
            payload.put("ip_dst_addr", source_ip);
            _LOG.trace("[OpenSOC] Added ip_dst_addr to: " + payload);
        } else if (payload.containsKey("rx_hosts")) {
            JSONArray rxHosts = (JSONArray) payload.remove("rx_hosts");
            if (rxHosts != null && !rxHosts.isEmpty()) {
                payload.put("ip_dst_addr", rxHosts.get(0));
                _LOG.trace("[OpenSOC] Added ip_dst_addr to: " + payload);
            }
        }

        if (payload.containsKey("id.orig_p")) {
            String source_port = payload.remove("id.orig_p").toString();
            payload.put("ip_src_port", source_port);
            _LOG.trace("[OpenSOC] Added ip_src_port to: " + payload);
        }
        if (payload.containsKey("id.resp_p")) {
            String dest_port = payload.remove("id.resp_p").toString();
            payload.put("ip_dst_port", dest_port);
            _LOG.trace("[OpenSOC] Added ip_dst_port to: " + payload);
        }

        //         if (payload.containsKey("host")) {
        //
        //            String host = payload.get("host").toString().trim();
        //            String tld = tldex.extractTLD(host);
        //
        //            payload.put("tld", tld);
        //            _LOG.trace("[OpenSOC] Added tld to: " + payload);
        //
        //         }
        //         if (payload.containsKey("query")) {
        //            String host = payload.get("query").toString();
        //            String[] parts = host.split("\\.");
        //            int length = parts.length;
        //            if (length >= 2) {
        //               payload.put("tld", parts[length - 2] + "."
        //                     + parts[length - 1]);
        //               _LOG.trace("[OpenSOC] Added tld to: " + payload);
        //            }
        //         }

        _LOG.trace("[OpenSOC] Inner message: " + payload);

        payload.put("protocol", key);
        _LOG.debug("[OpenSOC] Returning parsed message: " + payload);

        return payload;

    } catch (Exception e) {

        _LOG.error("Unable to Parse Message: " + raw_message);
        e.printStackTrace();
        return null;
    }

}

From source file:com.pearson.eidetic.driver.threads.MonitorSnapshotVolumeTime.java

public String getPeriod(JSONObject eideticParameters, Volume vol) {
    if ((eideticParameters == null)) {
        return null;
    }/*  w w w .  j a  va  2  s.c  o m*/
    JSONObject createSnapshot = null;
    if (eideticParameters.containsKey("CreateSnapshot")) {
        createSnapshot = (JSONObject) eideticParameters.get("CreateSnapshot");
    }
    if (createSnapshot == null) {
        logger.error("awsAccountNickname=\"" + awsAccount_.getUniqueAwsAccountIdentifier()
                + "\",Event=Error, Error=\"Malformed Eidetic Tag\", Volume_id=\"" + vol.getVolumeId() + "\"");
        return null;
    }

    String period = null;
    if (createSnapshot.containsKey("Interval")) {
        try {
            period = createSnapshot.get("Interval").toString();
        } catch (Exception e) {
            logger.error("awsAccountNickname=\"" + awsAccount_.getUniqueAwsAccountIdentifier()
                    + "\",Event=Error, Error=\"Malformed Eidetic Tag\", Volume_id=\"" + vol.getVolumeId()
                    + "\", stacktrace=\"" + e.toString() + System.lineSeparator()
                    + StackTrace.getStringFromStackTrace(e) + "\"");
        }
    }

    return period;
}

From source file:com.pearson.eidetic.driver.threads.MonitorSnapshotVolumeTime.java

public String getRunAt(JSONObject eideticParameters, Volume vol) {
    if ((eideticParameters == null)) {
        return null;
    }//from w ww. ja  v a2s .  co  m
    JSONObject createSnapshot = null;
    if (eideticParameters.containsKey("CreateSnapshot")) {
        createSnapshot = (JSONObject) eideticParameters.get("CreateSnapshot");
    }
    if (createSnapshot == null) {
        logger.error("awsAccountNickname=\"" + awsAccount_.getUniqueAwsAccountIdentifier()
                + "\",Event=Error, Error=\"Malformed Eidetic Tag\", Volume_id=\"" + vol.getVolumeId() + "\"");
        return null;
    }

    String runAt = null;
    if (createSnapshot.containsKey("RunAt")) {
        try {
            runAt = createSnapshot.get("RunAt").toString();
        } catch (Exception e) {
            logger.error("awsAccountNickname=\"" + awsAccount_.getUniqueAwsAccountIdentifier()
                    + "\",Event=Error, Error=\"Malformed Eidetic Tag\", Volume_id=\"" + vol.getVolumeId()
                    + "\", stacktrace=\"" + e.toString() + System.lineSeparator()
                    + StackTrace.getStringFromStackTrace(e) + "\"");
        }
    }

    return runAt;
}

From source file:com.ge.research.semtk.load.utility.ImportSpecHandler.java

/**
 * Build a value from a mapping array and a data record
 * @param mappingArrayJson/*from   www.  j  a va  2s. c  o  m*/
 * @param record
 * @return result or Null if any column is empty
 * @throws Exception
 */
private String buildMappingString(JSONArray mappingArrayJson, ArrayList<String> record) throws Exception {
    String ret = "";

    // loop through the mapping array
    Iterator<JSONObject> it = mappingArrayJson.iterator();
    for (int i = 0; i < mappingArrayJson.size(); i++) {
        JSONObject mapItem = (JSONObject) mappingArrayJson.get(i);

        if (mapItem.containsKey("textId")) {
            String text = null;
            // get text id
            String id = mapItem.get("textId").toString();

            // look up text
            try {
                text = this.textsAvailable.get(id);
            } catch (Exception e) {
                throw new Exception("Failed to look up textId: " + id);
            }

            // if all is well, append the value
            if (!StringUtils.isEmpty(text)) {
                ret += text;
            }

        } else if (mapItem.containsKey("colId")) {
            String colText = null;
            Integer pos = null;
            String id = mapItem.get("colId").toString();
            try {
                String textColLabel = this.colsAvailable.get(id);
                pos = this.headerPositioningInfo.get(textColLabel);
                colText = record.get(pos); // set the text equal to the correct column.                
            } catch (Exception e) {
                colText = "";
                if (pos == null) {
                    throw new Exception("Cannot find column in header list.");
                }
            }

            if (!StringUtils.isBlank(colText)) {
                ret += this.applyTransforms(colText, mapItem);
            } else {
                // found an empty column
                return null;
            }
        } else {
            throw new Exception("importSpec mapping item has no known type: " + mapItem.toString());
        }
    }
    return ret;
}

From source file:com.pearson.eidetic.driver.threads.MonitorSnapshotVolumeTime.java

public Integer getKeep(JSONObject eideticParameters, Volume vol) {
    if ((eideticParameters == null) || (vol == null)) {
        return null;
    }//w w  w .  jav a  2  s . c o m

    JSONObject createSnapshot = null;
    if (eideticParameters.containsKey("CreateSnapshot")) {
        createSnapshot = (JSONObject) eideticParameters.get("CreateSnapshot");
    }
    if (createSnapshot == null) {
        logger.error("Event=Error, Error=\"Malformed Eidetic Tag\", Volume_id=\"" + vol.getVolumeId() + "\"");
        return null;
    }

    Integer keep = null;
    if (createSnapshot.containsKey("Retain")) {
        try {
            keep = Integer.parseInt(createSnapshot.get("Retain").toString());
        } catch (Exception e) {
            logger.error("awsAccountNickname=\"" + awsAccount_.getUniqueAwsAccountIdentifier()
                    + "\",Event=Error, Error=\"Malformed Eidetic Tag\", Volume_id=\"" + vol.getVolumeId()
                    + "\", stacktrace=\"" + e.toString() + System.lineSeparator()
                    + StackTrace.getStringFromStackTrace(e) + "\"");
        }
    }

    return keep;
}

From source file:com.ge.research.semtk.load.utility.ImportSpecHandler.java

/**
 * Sets this.colsUsed to number of times each column is used.  Skipping ZEROS.
 *//*from   w  w  w.j  a v  a 2s .  c o  m*/
private void setupColsUsed() {
    // clear cols used
    colsUsed = new HashMap<String, Integer>();

    JSONArray nodes = (JSONArray) importspec.get("nodes");

    for (int i = 0; i < nodes.size(); i++) {
        JSONObject node = (JSONObject) nodes.get(i);

        // check mappings in nodes
        if (node.containsKey("mapping")) {
            JSONArray mapItems = (JSONArray) node.get("mapping");
            for (int j = 0; j < mapItems.size(); j++) {
                JSONObject item = (JSONObject) mapItems.get(j);
                if (item.containsKey("colId")) {
                    String colId = (String) item.get("colId");
                    if (colsUsed.containsKey(colId)) {
                        colsUsed.put(colId, colsUsed.get(colId) + 1);
                    } else {
                        colsUsed.put(colId, 1);
                    }
                }
            }
        }
        if (node.containsKey("props")) {
            JSONArray propItems = (JSONArray) node.get("props");
            for (int p = 0; p < propItems.size(); p++) {
                JSONObject prop = (JSONObject) propItems.get(p);

                // check mappings in props
                if (prop.containsKey("mapping")) {
                    JSONArray mapItems = (JSONArray) prop.get("mapping");
                    for (int j = 0; j < mapItems.size(); j++) {
                        JSONObject item = (JSONObject) mapItems.get(j);
                        if (item.containsKey("colId")) {
                            String colId = (String) item.get("colId");
                            if (colsUsed.containsKey(colId)) {
                                colsUsed.put(colId, colsUsed.get(colId) + 1);
                            } else {
                                colsUsed.put(colId, 1);
                            }
                        }
                    }
                }
            }
        }
    }
}