List of usage examples for org.json.simple JSONObject containsKey
boolean containsKey(Object key);
From source file:mml.handler.get.MMLGetHandler.java
/** * Get a resource from the database if it already exists * @param db the collection name/*from w ww .j av a2 s. c o m*/ * @param docID the resource docID * @return the resource or null * @throws MMLException */ public static AeseResource doGetResource(String db, String docID) throws MMLException { String res = null; JSONObject doc = null; AeseResource resource = null; try { res = Connector.getConnection().getFromDb(db, docID); } catch (Exception e) { throw new MMLException(e); } if (res != null) doc = (JSONObject) JSONValue.parse(res); if (doc != null) { String format = (String) doc.get(JSONKeys.FORMAT); if (format == null) throw new MMLException("doc missing format"); String version1 = (String) doc.get(JSONKeys.VERSION1); resource = new AeseResource(); if (version1 != null) resource.setVersion1(version1); if (doc.containsKey(JSONKeys.DESCRIPTION) && format.equals(Formats.TEXT)) resource.setDescription((String) doc.get(JSONKeys.DESCRIPTION)); resource.setFormat(format); resource.setContent((String) doc.get(JSONKeys.BODY)); } return resource; }
From source file:mml.handler.MMLHandler.java
private boolean isAnnotation(JSONObject jObj) { if (jObj.containsKey(JSONKeys.OFFSET) && jObj.containsKey(JSONKeys.LEN) && jObj.containsKey(JSONKeys.USER) && jObj.containsKey(JSONKeys.CONTENT)) return true; else/*w ww . j av a2 s.com*/ return false; }
From source file:com.intel.genomicsdb.GenomicsDBImporter.java
/** * Utility function that returns a list of ChromosomeInterval objects for * the column partition specified by the loader JSON file and rank/partition index * @param loaderJSONFile path to loader JSON file * @param partitionIdx rank/partition index * @return list of ChromosomeInterval objects for the specified partition * @throws ParseException when there is a bug in the JNI interface and a faulty JSON is returned */// ww w . jav a 2 s.c om public static ArrayList<ChromosomeInterval> getChromosomeIntervalsForColumnPartition( final String loaderJSONFile, final int partitionIdx) throws ParseException { final String chromosomeIntervalsJSONString = jniGetChromosomeIntervalsForColumnPartition(loaderJSONFile, partitionIdx); /* JSON format { "contigs": [ { "chr1": [ 100, 200] }, { "chr2": [ 500, 600] } ] } */ ArrayList<ChromosomeInterval> chromosomeIntervals = new ArrayList<ChromosomeInterval>(); JSONParser parser = new JSONParser(); JSONObject topObj = (JSONObject) (parser.parse(chromosomeIntervalsJSONString)); assert topObj.containsKey("contigs"); JSONArray listOfDictionaries = (JSONArray) (topObj.get("contigs")); for (Object currDictObj : listOfDictionaries) { JSONObject currDict = (JSONObject) currDictObj; assert currDict.size() == 1; //1 entry for (Object currEntryObj : currDict.entrySet()) { Map.Entry<String, JSONArray> currEntry = (Map.Entry<String, JSONArray>) currEntryObj; JSONArray currValue = currEntry.getValue(); assert currValue.size() == 2; chromosomeIntervals.add(new ChromosomeInterval(currEntry.getKey(), (Long) (currValue.get(0)), (Long) (currValue.get(1)))); } } return chromosomeIntervals; }
From source file:net.jakobnielsen.imagga.upload.convert.UploadConverter.java
@Override public String convert(String jsonString) { if (jsonString == null) { throw new ConverterException("The given JSON string is null"); }// w ww . j av a 2s . co m JSONObject json = (JSONObject) JSONValue.parse(jsonString); if (!json.containsKey(UPLOAD_FOR_PROCESSING)) { throw new ConverterException(UPLOAD_FOR_PROCESSING + " key missing from json : " + jsonString); } JSONObject json2 = (JSONObject) json.get(UPLOAD_FOR_PROCESSING); return json2.get("upload_code").toString(); }
From source file:net.modsec.ms.connector.ConnRequestHandler.java
/** * Parses the json, extracts the rules and deploy those rules into modsecurity. It takes the rule * string from the json and creates a rule file then copy it to modsecurity rule file folder. It alsp * restarts the modsecurity after deploying the rule * @param json reponses whether the rule is successfully deployed or not. *///from w w w . ja va2 s . c om @SuppressWarnings("unchecked") public static void onDeployMSRules(JSONObject json) { log.info("onDeployMSRules called.. : " + json.toJSONString()); MSConfig serviceCfg = MSConfig.getInstance(); JSONObject jsonResp = new JSONObject(); String ruleDirPath = serviceCfg.getConfigMap().get("RuleFileDir"); String ruleFileString = (String) json.get("ruleString"); String ruleFileName = ""; InputStream ins = null; FileOutputStream out = null; BufferedReader br = null; if (json.containsKey("groupName")) { ruleFileName += ((String) json.get("groupName")).toLowerCase() + ".conf"; } else { UUID randomName = UUID.randomUUID(); ruleFileName += randomName.toString() + ".conf"; } try { //modified string writing to modsecurity configurations log.info("Writing a rule to File :" + ruleFileName); File file = new File(ruleDirPath + "/" + ruleFileName); file.createNewFile(); out = new FileOutputStream(file); out.write(ruleFileString.getBytes()); out.close(); log.info("ModSecurity Rules Written ... "); //For Restarting modsecurity so that modified configuration can be applied JSONObject restartJson = new JSONObject(); restartJson.put("action", "restart"); String cmd = serviceCfg.getConfigMap().get("MSRestart"); String status = (String) executeShScript(cmd, restartJson).get("status"); //if rule file is giving syntax error while deploying rules on server end if (status.equals("1")) { if (file.delete()) { log.info("Successfully deleted conflicting file : " + file.getName()); executeShScript(cmd, restartJson); } else { log.info("unable to delete file : " + file.getName()); } jsonResp.put("action", "deployRules"); jsonResp.put("status", "1"); jsonResp.put("message", "Unable to deploy specified Rules. They either" + "conflicting to the already deployed rules"); } else { jsonResp.put("action", "deployRules"); jsonResp.put("status", "0"); jsonResp.put("message", "Rules Deployed!"); } } catch (FileNotFoundException e1) { jsonResp.put("action", "deployRules"); jsonResp.put("status", "1"); jsonResp.put("message", "Internal Service is down!"); e1.printStackTrace(); } catch (IOException | NullPointerException e) { jsonResp.put("action", "deployRules"); jsonResp.put("status", "0"); jsonResp.put("message", "Unable to create rule file on Server."); e.printStackTrace(); } log.info("Sending Json :" + jsonResp.toJSONString()); ConnectorService.getConnectorProducer().send(jsonResp.toJSONString()); jsonResp.clear(); }
From source file:net.jakobnielsen.imagga.color.convert.SimilarColorsConverter.java
@Override public List<RankSimilarity> convert(String jsonString) { if (jsonString == null) { throw new ConverterException("The given JSON string is null"); }/*from w ww . j a v a2s . c o m*/ JSONObject json = (JSONObject) JSONValue.parse(jsonString); if (!json.containsKey(RANK_SIMILARITY)) { throw new ConverterException(RANK_SIMILARITY + " key missing from json : " + jsonString); } JSONArray jsonArray = (JSONArray) json.get(RANK_SIMILARITY); List<RankSimilarity> rankResults = new ArrayList<RankSimilarity>(); for (Object co : jsonArray) { JSONObject o = (JSONObject) co; rankResults.add(new RankSimilarity(getLong("id", o), getDouble("dist", o))); } return rankResults; }
From source file:net.jakobnielsen.imagga.crop_slice.convert.ApiUsageConverter.java
@Override public ApiUsage convert(String jsonString) { if (jsonString == null) { throw new ConverterException("The given JSON string is null"); }//from w w w.j a v a2 s . c om JSONObject json = (JSONObject) JSONValue.parse(jsonString); if (!json.containsKey(API_USAGE)) { throw new ConverterException(API_USAGE + " key missing from json : " + jsonString); } json = (JSONObject) json.get(API_USAGE); return doConvert(json); }
From source file:copter.Engine.java
public String doAction(JSONObject jsonParam, WebSocket conn) { if (!jsonParam.containsKey("action")) { return "Missing 'action' param!"; }/* w ww .j av a2 s. c om*/ String action = (String) jsonParam.get("action"); switch (action) { case Constants.SET_PITCH_ACTION: int p = (int) (long) jsonParam.get("value"); this.setPitch(p); return "Pitch set to " + p; case Constants.SET_ROLL_ACTION: int r = (int) (long) jsonParam.get("value"); this.setPitch(r); return "Roll set to " + r; case Constants.SET_THROTTLE_ACTION: int th = (int) (long) jsonParam.get("value"); this.setPitch(th); return "Throttle set to " + th; case Constants.SET_YAW_ACTION: int y = (int) (long) jsonParam.get("value"); this.setPitch(y); return "Yaw set to " + y; case Constants.START_ENGINE_ACTION: this.StartEngine(); return "Engine started"; } return "Unknown 'action' param: " + action; }
From source file:net.jakobnielsen.imagga.crop_slice.convert.ApiUsageConverter.java
private ApiUsage doConvert(JSONObject json) { Map<String, Usage> usageMap = new HashMap<String, Usage>(); Date startTime = null;/*from w w w. j av a2s . c o m*/ Date endTime = null; Double totalPayable = null; for (Object okey : json.keySet().toArray()) { String key = (String) okey; if ("start_time".equals(key)) { JSONObject value = (JSONObject) json.get(key); if (value.containsKey(UNIX)) { startTime = new Date(getLong(UNIX, value)); } } else if ("end_time".equals(key)) { JSONObject value = (JSONObject) json.get(key); if (value.containsKey(UNIX)) { endTime = new Date(getLong(UNIX, value)); } } else if ("total_payable".equals(key)) { totalPayable = getDouble(key, json); } else { JSONObject value = (JSONObject) json.get(key); if (value.containsKey(COUNT)) { usageMap.put(key, new Usage(getLong(COUNT, value), getDouble("total_price", value))); } } } return new ApiUsage(startTime, endTime, usageMap, totalPayable); }
From source file:net.jakobnielsen.imagga.color.convert.ColorsConverter.java
private void addColors(String key, JSONObject infoJSON, List<ExtendedColor> imageColors) { if (infoJSON.containsKey(key)) { Object colorsObject = infoJSON.get(key); if (colorsObject instanceof JSONArray) { JSONArray colorsArrays = (JSONArray) colorsObject; for (Object ac : colorsArrays) { imageColors.add(createColor((JSONObject) ac)); }/* w w w. j a va 2 s . c om*/ } } }