/** * Deserialize a {@link ProfileConfig}. * * @param json A String containing JSON. * @return The Profile definition. * @throws IOException */ public static ProfileConfig fromJSON(String json) throws IOException { return JSONUtils.INSTANCE.load(json, ProfileConfig.class); }
while ((line = br.readLine()) != null) { Map<String, Object> inDoc = JSONUtils.INSTANCE .load(line, JSONUtils.MAP_SUPPLIER); Object id = inDoc.get("_id"); Object type = inDoc.get("_type"); String createRaw = String .format("{ \"create\" : { \"_id\": \"%s\", \"_type\": \"%s\" } }", id, type); String outData = JSONUtils.INSTANCE.toJSON(inDoc.get("_source"), false); outRecs.add(createRaw); outRecs.add(outData);
public static void writeSensorIndexingConfigToZookeeper(String sensorType, Map<String, Object> sensorIndexingConfig, String zookeeperUrl) throws Exception { writeSensorIndexingConfigToZookeeper(sensorType, JSONUtils.INSTANCE.toJSONPretty(sensorIndexingConfig), zookeeperUrl); }
public byte[] toJSONPretty(String config) throws IOException { return toJSONPretty(readTree(config)); }
/** * Serialize the profile definition to a JSON string. * * @return The Profiler configuration serialized as a JSON string. * @throws JsonProcessingException */ public String toJSON() throws JsonProcessingException { return JSONUtils.INSTANCE.toJSON(this, true); } }
private static List<byte[]> getInputMessages(String path){ try{ List<byte[]> ret = TestUtils.readSampleData(path); { //we want one of the fields without a destination IP to ensure that enrichments can function Map<String, Object> sansDestinationIp = JSONUtils.INSTANCE.load(new String(ret.get(ret.size() -1)) , JSONUtils.MAP_SUPPLIER); sansDestinationIp.remove(Constants.Fields.DST_ADDR.getName()); ret.add(JSONUtils.INSTANCE.toJSONPretty(sansDestinationIp)); } return ret; }catch(IOException ioe){ return null; } }
@Test public void applyPatch_modifies_complex_source_json_doc() throws IOException { String actual = new String(JSONUtils.INSTANCE.applyPatch(patchComplexJson, complexJson)); assertThat(JSONUtils.INSTANCE.load(actual, JSONUtils.MAP_SUPPLIER), equalTo(JSONUtils.INSTANCE.load(expectedComplexJson, JSONUtils.MAP_SUPPLIER))); }
/** * Reads Json data for the specified config type and config name (if applicable) from zookeeper, * applies the patch from patchData, and writes it back to Zookeeper in a pretty print format. * Patching JSON flattens existing formatting, so this will keep configs readable. The * curatorclient should be started already. * * @param configurationType GLOBAL, PARSER, etc. * @param configName e.g. bro, yaf, snort * @param patchData a JSON patch in the format specified by RFC 6902 * @param client access to zookeeeper */ public static void applyConfigPatchToZookeeper( ConfigurationType configurationType, Optional<String> configName, byte[] patchData, CuratorFramework client) throws Exception { byte[] configData = readConfigBytesFromZookeeper(configurationType, configName, client); byte[] prettyPatchedConfig = JSONUtils.INSTANCE.applyPatch(patchData, configData); // ensure the patch produces a valid result; otherwise exception thrown during deserialization String prettyPatchedConfigStr = new String(prettyPatchedConfig); configurationType.deserialize(prettyPatchedConfigStr); writeConfigToZookeeper(configurationType, configName, prettyPatchedConfig, client); }
/** * Serialize a {@link ProfilerConfig} to a JSON string. * * @return The Profiler configuration serialized as a JSON string. * @throws JsonProcessingException */ public String toJSON() throws JsonProcessingException { return JSONUtils.INSTANCE.toJSON(this, true); } }
public void testEnvelopedData(ParserDriver driver) throws IOException { Map<String, Object> inputRecord = new HashMap<String, Object>() {{ put(Constants.Fields.ORIGINAL.getName(), "real_original_string"); put("data", "field1_val,100"); put("metadata_field", "metadata_val"); }}; ProcessorResult<List<byte[]>> results = driver.run(ImmutableList.of(JSONUtils.INSTANCE.toJSONPretty(inputRecord))); Assert.assertFalse(results.failed()); List<byte[]> resultList = results.getResult(); Assert.assertEquals(1, resultList.size()); Map<String, Object> outputRecord = JSONUtils.INSTANCE.load(new String(resultList.get(0)), JSONUtils.MAP_SUPPLIER); Assert.assertEquals("field1_val", outputRecord.get("field1")); Assert.assertEquals(inputRecord.get(Constants.Fields.ORIGINAL.getName()), outputRecord.get(Constants.Fields.ORIGINAL.getName())); Assert.assertEquals(inputRecord.get(MetadataUtil.METADATA_PREFIX + ".metadata_field"), outputRecord.get("metadata_field")); }
@Test public void applyPatch_modifies_source_json_doc() throws IOException { String actual = new String(JSONUtils.INSTANCE.applyPatch(patchJson, sourceJson)); assertThat(JSONUtils.INSTANCE.load(actual, JSONUtils.MAP_SUPPLIER), equalTo(JSONUtils.INSTANCE.load(expectedJson, JSONUtils.MAP_SUPPLIER))); }
public byte[] applyPatch(byte[] patch, byte[] source) throws IOException { JsonNode patchNode = readTree(patch); JsonNode sourceNode = readTree(source); return toJSONPretty(JsonPatch.apply(patchNode, sourceNode)); }
default Document getPatchedDocument(RetrieveLatestDao retrieveLatestDao, PatchRequest request, Optional<Long> optionalTimestamp ) throws OriginalNotFoundException, IOException { String guid = request.getGuid(); String sensorType = request.getSensorType(); String documentID = null; Long timestamp = optionalTimestamp.orElse(System.currentTimeMillis()); Map<String, Object> originalSource = request.getSource(); if (originalSource == null) { // no document source provided, lookup the latest Document toPatch = retrieveLatestDao.getLatest(guid, sensorType); if(toPatch != null && toPatch.getDocument() != null) { originalSource = toPatch.getDocument(); documentID = toPatch.getDocumentID().orElse(null); } else { String error = format("Document does not exist, but is required; guid=%s, sensorType=%s", guid, sensorType); throw new OriginalNotFoundException(error); } } Map<String, Object> patchedSource = JSONUtils.INSTANCE.applyPatch(request.getPatch(), originalSource); return new Document(patchedSource, guid, sensorType, timestamp, documentID); } }
/** * Deserialize a {@link ProfilerConfig}. * * @param json A String containing JSON. * @return The Profiler configuration. * @throws IOException */ public static ProfilerConfig fromJSON(String json) throws IOException { return JSONUtils.INSTANCE.load(json, ProfilerConfig.class); }
JSONObject broTemplate = JSONUtils.INSTANCE.load(new File(broTemplatePath), JSONObject.class); addTestFieldMappings(broTemplate, "bro_doc"); String broTemplateJson = JSONUtils.INSTANCE.toJSON(broTemplate, true); HttpEntity broEntity = new NStringEntity(broTemplateJson, ContentType.APPLICATION_JSON); Response response = lowLevelClient.performRequest("PUT", "/_template/bro_template", Collections.emptyMap(), broEntity); JSONObject snortTemplate = JSONUtils.INSTANCE.load(new File(snortTemplatePath), JSONObject.class); addTestFieldMappings(snortTemplate, "snort_doc"); String snortTemplateJson = JSONUtils.INSTANCE.toJSON(snortTemplate, true); HttpEntity snortEntity = new NStringEntity(snortTemplateJson, ContentType.APPLICATION_JSON); response = lowLevelClient.performRequest("PUT", "/_template/snort_template", Collections.emptyMap(), snortEntity);
public String toJSON() throws JsonProcessingException { return JSONUtils.INSTANCE.toJSON(this, true); }
public void testEnvelopedData_withMetadataPrefix(ParserDriver driver) throws IOException { Map<String, Object> inputRecord = new HashMap<String, Object>() {{ put(Constants.Fields.ORIGINAL.getName(), "real_original_string"); put("data", "field1_val,100"); put("metadata_field", "metadata_val"); }}; ProcessorResult<List<byte[]>> results = driver.run(ImmutableList.of(JSONUtils.INSTANCE.toJSONPretty(inputRecord))); Assert.assertFalse(results.failed()); List<byte[]> resultList = results.getResult(); Assert.assertEquals(1, resultList.size()); Map<String, Object> outputRecord = JSONUtils.INSTANCE.load(new String(resultList.get(0)), JSONUtils.MAP_SUPPLIER); Assert.assertEquals("field1_val", outputRecord.get("field1")); Assert.assertEquals(inputRecord.get(Constants.Fields.ORIGINAL.getName()), outputRecord.get(Constants.Fields.ORIGINAL.getName())); Assert.assertEquals(inputRecord.get("metadata_field"), outputRecord.get("metadata_field")); } /**
public static void writeSensorEnrichmentConfigToZookeeper(String sensorType, SensorEnrichmentConfig sensorEnrichmentConfig, String zookeeperUrl) throws Exception { writeSensorEnrichmentConfigToZookeeper(sensorType, JSONUtils.INSTANCE.toJSONPretty(sensorEnrichmentConfig), zookeeperUrl); }
/** * Update JSON given a JSON Patch (see RFC 6902 at https://tools.ietf.org/html/rfc6902) * Operations: * <ul> * <li>add</li> * <li>remove</li> * <li>replace</li> * <li>move</li> * <li>copy</li> * <li>test</li> * </ul> * * @param patch Array of JSON patches, e.g. [{ "op": "move", "from": "/a", "path": "/c" }] * @param source Source JSON to apply patch to * @return new json after applying the patch */ public byte[] applyPatch(String patch, String source) throws IOException { JsonNode patchNode = readTree(patch); JsonNode sourceNode = readTree(source); return toJSONPretty(JsonPatch.apply(patchNode, sourceNode)); }
@Override public Object deserialize(String s) throws IOException { return JSONUtils.INSTANCE.load(s, JSONUtils.MAP_SUPPLIER); }