List of usage examples for org.json.simple JSONObject containsKey
boolean containsKey(Object key);
From source file:org.apache.metron.parsers.syslog.Syslog5424Parser.java
@Override public boolean validate(JSONObject message) { JSONObject value = message; if (!(value.containsKey("original_string"))) { LOG.trace("[Metron] Message does not have original_string: {}", message); return false; } else if (!(value.containsKey("timestamp"))) { LOG.trace("[Metron] Message does not have timestamp: {}", message); return false; } else {/*from w ww. j av a 2s . co m*/ LOG.trace("[Metron] Message conforms to schema: {}", message); return true; } }
From source file:org.apache.metron.parsing.TelemetryParserBolt.java
@SuppressWarnings("unchecked") public void execute(Tuple tuple) { LOG.trace("[Metron] Starting to process a new incoming tuple"); byte[] original_message = null; try {/*from w w w . j a v a2s . c o m*/ original_message = tuple.getBinary(0); LOG.trace("[Metron] Starting the parsing process"); if (original_message == null || original_message.length == 0) { LOG.error("Incomming tuple is null"); throw new Exception("Invalid message length"); } LOG.trace("[Metron] Attempting to transofrm binary message to JSON"); JSONObject transformed_message = _parser.parse(original_message); LOG.debug("[Metron] Transformed Telemetry message: " + transformed_message); if (transformed_message == null || transformed_message.isEmpty()) throw new Exception("Unable to turn binary message into a JSON"); LOG.trace("[Metron] Checking if the transformed JSON conforms to the right schema"); if (!checkForSchemaCorrectness(transformed_message)) { throw new Exception("Incorrect formatting on message: " + transformed_message); } else { LOG.trace("[Metron] JSON message has the right schema"); boolean filtered = false; if (_filter != null) { if (!_filter.emitTuple(transformed_message)) { filtered = true; } } if (!filtered) { String ip1 = null; if (transformed_message.containsKey("ip_src_addr")) ip1 = transformed_message.get("ip_src_addr").toString(); String ip2 = null; if (transformed_message.containsKey("ip_dst_addr")) ip2 = transformed_message.get("ip_dst_addr").toString(); String key = generateTopologyKey(ip1, ip2); JSONObject new_message = new JSONObject(); new_message.put("message", transformed_message); _collector.emit("message", new Values(key, new_message)); } _collector.ack(tuple); if (metricConfiguration != null) ackCounter.inc(); } } catch (Exception e) { e.printStackTrace(); LOG.error("Failed to parse telemetry message :" + original_message); _collector.fail(tuple); if (metricConfiguration != null) failCounter.inc(); JSONObject error = ErrorGenerator .generateErrorMessage("Parsing problem: " + new String(original_message), e); _collector.emit("error", new Values(error)); } }
From source file:org.apache.metron.profiler.storm.integration.ProfilerIntegrationTest.java
@Test public void testProfileWithTriageResult() throws Exception { uploadConfigToZookeeper(ProfilerConfig.fromJSON(profileWithTriageResult)); // start the topology and write test messages to kafka fluxComponent.submitTopology();/*w ww. j a va 2s . c om*/ List<String> telemetry = FileUtils.readLines(new File("src/test/resources/telemetry.json")); kafkaComponent.writeMessages(inputTopic, telemetry); // wait until the triage message is output to kafka assertEventually(() -> { outputMessages = kafkaComponent.readMessages(outputTopic); assertEquals(1, outputMessages.size()); }, timeout); // validate the triage message JSONObject message = (JSONObject) new JSONParser().parse(new String(outputMessages.get(0), "UTF-8")); assertEquals("profile-with-triage", message.get(PROFILE_FIELD)); assertEquals("global", message.get(ENTITY_FIELD)); assertEquals(76548935L, message.get(PERIOD_ID_FIELD)); assertEquals(1530978700000L, message.get(PERIOD_START_FIELD)); assertEquals(1530978720000L, message.get(PERIOD_END_FIELD)); assertEquals("profiler", message.get(Constants.SENSOR_TYPE)); assertEquals("true", message.get(ALERT_FIELD)); assertEquals(1.0, message.get("min")); assertEquals(1.0, message.get("max")); assertEquals(1.0, message.get("mean")); assertTrue(message.containsKey(TIMESTAMP_FIELD)); assertTrue(message.containsKey(Constants.GUID)); }
From source file:org.apache.metron.tagging.TelemetryTaggerBolt.java
@SuppressWarnings("unchecked") public void execute(Tuple tuple) { LOG.trace("[Metron] Starting to process message for alerts"); JSONObject original_message = null; try {/* w w w. jav a 2 s .co m*/ original_message = (JSONObject) tuple.getValue(0); if (original_message == null || original_message.isEmpty()) throw new Exception("Could not parse message from byte stream"); LOG.trace("[Metron] Received tuple: " + original_message); JSONObject alerts_tag = new JSONObject(); JSONArray alerts_list = _adapter.tag(original_message); LOG.trace("[Metron] Tagged message: " + alerts_list); if (alerts_list.size() != 0) { if (original_message.containsKey("alerts")) { JSONObject tag = (JSONObject) original_message.get("alerts"); JSONArray already_triggered = (JSONArray) tag.get("triggered"); alerts_list.addAll(already_triggered); LOG.trace("[Metron] Created a new string of alerts"); } alerts_tag.put("identifier", _identifier); alerts_tag.put("triggered", alerts_list); original_message.put("alerts", alerts_tag); LOG.debug("[Metron] Detected alerts: " + alerts_tag); } else { LOG.debug("[Metron] The following messages did not contain alerts: " + original_message); } _collector.ack(tuple); _collector.emit(new Values(original_message)); /*if (metricConfiguration != null) { emitCounter.inc(); ackCounter.inc(); }*/ } catch (Exception e) { e.printStackTrace(); LOG.error("Failed to tag message :" + original_message); e.printStackTrace(); _collector.fail(tuple); /* if (metricConfiguration != null) { failCounter.inc(); }*/ } }
From source file:org.apache.oozie.cli.OozieCLI.java
@VisibleForTesting void printBulkModifiedJobs(JSONObject json, String timeZoneId, String action) throws IOException { if (json.containsKey(JsonTags.WORKFLOWS_JOBS)) { JSONArray workflows = (JSONArray) json.get(JsonTags.WORKFLOWS_JOBS); if (workflows == null) { workflows = new JSONArray(); }/*from w w w .j ava 2 s . c om*/ List<WorkflowJob> wfs = JsonToBean.createWorkflowJobList(workflows); if (wfs.isEmpty()) { System.out.println("bulk modify command did not modify any jobs"); } else { System.out.println("the following jobs have been " + action); printJobs(wfs, timeZoneId, false); } } else if (json.containsKey(JsonTags.COORDINATOR_JOBS)) { JSONArray coordinators = (JSONArray) json.get(JsonTags.COORDINATOR_JOBS); if (coordinators == null) { coordinators = new JSONArray(); } List<CoordinatorJob> coords = JsonToBean.createCoordinatorJobList(coordinators); if (coords.isEmpty()) { System.out.println("bulk modify command did not modify any jobs"); } else { System.out.println("the following jobs have been " + action); printCoordJobs(coords, timeZoneId, false); } } else { JSONArray bundles = (JSONArray) json.get(JsonTags.BUNDLE_JOBS); if (bundles == null) { bundles = new JSONArray(); } List<BundleJob> bundleJobs = JsonToBean.createBundleJobList(bundles); if (bundleJobs.isEmpty()) { System.out.println("bulk modify command did not modify any jobs"); } else { System.out.println("the following jobs have been " + action); printBundleJobs(bundleJobs, timeZoneId, false); } } }
From source file:org.apache.sling.sli.subcommands.List.java
@Override protected void computeJSON(Object o) { JSONObject jsonObject = (JSONObject) o; for (Object key : jsonObject.keySet()) { Object child = jsonObject.get(key); if (child instanceof JSONObject) { JSONObject node = (JSONObject) child; StringBuilder line = new StringBuilder((String) key); if (node.containsKey(TYPE)) { line.append("\t\t").append(node.get(TYPE)); }/* www.ja va 2 s. c o m*/ System.out.println(line.toString()); } } }
From source file:org.apache.sqoop.json.util.ConfigInputSerialization.java
/** * Restore one MConfig from JSON Object. * * @param config JSON representation of the MConfig. * @return Restored MConfig./* w w w .j a va 2s .com*/ */ @SuppressWarnings({ "rawtypes", "unchecked" }) static MConfig restoreConfig(JSONObject config) { JSONArray inputs = (JSONArray) config.get(ConfigInputConstants.CONFIG_INPUTS); List<MInput<?>> mInputs = new ArrayList<MInput<?>>(); for (int i = 0; i < inputs.size(); i++) { JSONObject input = (JSONObject) inputs.get(i); MInputType type = MInputType.valueOf((String) input.get(ConfigInputConstants.CONFIG_INPUT_TYPE)); String name = (String) input.get(ConfigInputConstants.CONFIG_INPUT_NAME); Boolean sensitive = (Boolean) input.get(ConfigInputConstants.CONFIG_INPUT_SENSITIVE); MInput mInput = null; switch (type) { case STRING: { long size = (Long) input.get(ConfigInputConstants.CONFIG_INPUT_SIZE); mInput = new MStringInput(name, sensitive.booleanValue(), (short) size); break; } case MAP: { mInput = new MMapInput(name, sensitive.booleanValue()); break; } case INTEGER: { mInput = new MIntegerInput(name, sensitive.booleanValue()); break; } case BOOLEAN: { mInput = new MBooleanInput(name, sensitive.booleanValue()); break; } case ENUM: { String values = (String) input.get(ConfigInputConstants.CONFIG_INPUT_VALUES); mInput = new MEnumInput(name, sensitive.booleanValue(), values.split(",")); break; } default: // do nothing break; } // Propagate config ID mInput.setPersistenceId((Long) input.get(ConfigInputConstants.INPUT_ID)); // Propagate config optional value if (input.containsKey(ConfigInputConstants.CONFIG_INPUT_VALUE)) { switch (type) { case MAP: try { mInput.setValue((Map<String, String>) input.get(ConfigInputConstants.CONFIG_INPUT_VALUE)); } catch (ClassCastException e) { throw new SqoopException(SerializationError.SERIALIZATION_001, name + " requires a 'map' value."); } break; default: mInput.restoreFromUrlSafeValueString( (String) input.get(ConfigInputConstants.CONFIG_INPUT_VALUE)); break; } } mInputs.add(mInput); } MConfig mConfig = new MConfig((String) config.get(ConfigInputConstants.CONFIG_NAME), mInputs); mConfig.setPersistenceId((Long) config.get(ConfigInputConstants.CONFIG_ID)); return mConfig; }
From source file:org.apache.sqoop.json.util.TestConfigSerialization.java
@Test public void testInputEditableOptional() { // Inserted values Map<String, String> map = new HashMap<String, String>(); map.put("A", "B"); // Fill config with all values MConfig config = getConfig();/* www . ja v a 2 s . c o m*/ config.getStringInput("String").setValue("A"); config.getMapInput("Map").setValue(map); config.getIntegerInput("Integer").setValue(1); config.getBooleanInput("Boolean").setValue(true); config.getEnumInput("Enum").setValue("YES"); // Serialize that into JSON JSONObject jsonObject = ConfigInputSerialization.extractConfig(config, MConfigType.JOB, false); assertNotNull(jsonObject); // Make sure editable is optional // Remove the editable JSONArray inputs = (JSONArray) jsonObject.get(ConfigInputConstants.CONFIG_INPUTS); for (int i = 0; i < inputs.size(); i++) { JSONObject input = (JSONObject) inputs.get(i); if ((input.containsKey(ConfigInputConstants.CONFIG_INPUT_EDITABLE))) { input.remove(ConfigInputConstants.CONFIG_INPUT_EDITABLE); } } // Exchange the data on string level String serializedJson = jsonObject.toJSONString(); JSONObject retrievedJson = JSONUtils.parse(serializedJson); // Make sure editable isn't part of the JSON inputs = (JSONArray) retrievedJson.get(ConfigInputConstants.CONFIG_INPUTS); for (int i = 0; i < inputs.size(); i++) { JSONObject input = (JSONObject) inputs.get(i); assertFalse(input.containsKey(ConfigInputConstants.CONFIG_INPUT_EDITABLE)); } // And retrieve back from JSON representation MConfig retrieved = ConfigInputSerialization.restoreConfig(retrievedJson); // Verify all expected values assertEquals("A", retrieved.getStringInput("String").getValue()); assertEquals(map, retrieved.getMapInput("Map").getValue()); assertEquals(1, (int) retrieved.getIntegerInput("Integer").getValue()); assertEquals(true, retrieved.getBooleanInput("Boolean").getValue().booleanValue()); assertEquals("YES", retrieved.getEnumInput("Enum").getValue()); }
From source file:org.apache.storm.loadgen.CaptureLoad.java
static Map<String, Double> parseResources(String input) { Map<String, Double> topologyResources = new HashMap<>(); JSONParser parser = new JSONParser(); LOG.debug("Input to parseResources {}", input); try {// ww w . j av a 2 s .co m if (input != null) { Object obj = parser.parse(input); JSONObject jsonObject = (JSONObject) obj; if (jsonObject.containsKey(Config.TOPOLOGY_COMPONENT_RESOURCES_ONHEAP_MEMORY_MB)) { Double topoMemOnHeap = ObjectReader .getDouble(jsonObject.get(Config.TOPOLOGY_COMPONENT_RESOURCES_ONHEAP_MEMORY_MB), null); topologyResources.put(Config.TOPOLOGY_COMPONENT_RESOURCES_ONHEAP_MEMORY_MB, topoMemOnHeap); } if (jsonObject.containsKey(Config.TOPOLOGY_COMPONENT_RESOURCES_OFFHEAP_MEMORY_MB)) { Double topoMemOffHeap = ObjectReader .getDouble(jsonObject.get(Config.TOPOLOGY_COMPONENT_RESOURCES_OFFHEAP_MEMORY_MB), null); topologyResources.put(Config.TOPOLOGY_COMPONENT_RESOURCES_OFFHEAP_MEMORY_MB, topoMemOffHeap); } if (jsonObject.containsKey(Config.TOPOLOGY_COMPONENT_CPU_PCORE_PERCENT)) { Double topoCpu = ObjectReader .getDouble(jsonObject.get(Config.TOPOLOGY_COMPONENT_CPU_PCORE_PERCENT), null); topologyResources.put(Config.TOPOLOGY_COMPONENT_CPU_PCORE_PERCENT, topoCpu); } LOG.debug("Topology Resources {}", topologyResources); } } catch (org.json.simple.parser.ParseException e) { LOG.error("Failed to parse component resources is:" + e.toString(), e); return null; } return topologyResources; }
From source file:org.bunkr.core.descriptor.DescriptorBuilder.java
public static IDescriptor fromJSON(String s) { JSONObject jo = (JSONObject) JSONValue.parse(s); if (!jo.containsKey(KEY_NAME)) throw new IllegalArgumentException("Missing required key: " + KEY_NAME); if (!jo.containsKey(KEY_PARAMS)) throw new IllegalArgumentException("Missing required key: " + KEY_NAME); String name = (String) jo.get(KEY_NAME); JSONObject params = (JSONObject) jo.get(KEY_PARAMS); // put security descriptors here if (name.equals(PlaintextDescriptor.IDENTIFIER)) return new PlaintextDescriptor(); if (name.equals(PBKDF2Descriptor.IDENTIFIER)) return new PBKDF2Descriptor(params); if (name.equals(ScryptDescriptor.IDENTIFIER)) return new ScryptDescriptor(params); throw new IllegalArgumentException("Could not identify SecurityDescriptor type " + name); }