List of usage examples for org.json.simple JSONObject remove
V remove(Object key);
From source file:org.apache.metron.parsers.GrokParser.java
@SuppressWarnings("unchecked") private Optional<MessageParserResult<JSONObject>> parseSingleLine(byte[] rawMessage) { List<JSONObject> messages = new ArrayList<>(); Map<Object, Throwable> errors = new HashMap<>(); String originalMessage = null; try {/*from www . ja va 2s. c o m*/ originalMessage = new String(rawMessage, "UTF-8"); LOG.debug("Grok parser parsing message: {}", originalMessage); Match gm = grok.match(originalMessage); gm.captures(); JSONObject message = new JSONObject(); message.putAll(gm.toMap()); if (message.size() == 0) { Throwable rte = new RuntimeException( "Grok statement produced a null message. Original message was: " + originalMessage + " and the parsed message was: " + message + " . Check the pattern at: " + grokPath); errors.put(originalMessage, rte); } else { message.put("original_string", originalMessage); for (String timeField : timeFields) { String fieldValue = (String) message.get(timeField); if (fieldValue != null) { message.put(timeField, toEpoch(fieldValue)); } } if (timestampField != null) { message.put(Constants.Fields.TIMESTAMP.getName(), formatTimestamp(message.get(timestampField))); } message.remove(patternLabel); postParse(message); messages.add(message); LOG.debug("Grok parser parsed message: {}", message); } } catch (Exception e) { LOG.error(e.getMessage(), e); Exception innerException = new IllegalStateException( "Grok parser Error: " + e.getMessage() + " on " + originalMessage, e); return Optional.of(new DefaultMessageParserResult<>(innerException)); } return Optional.of(new DefaultMessageParserResult<JSONObject>(messages, errors)); }
From source file:org.apache.metron.parsing.parsers.BasicYafParser.java
private List<JSONObject> parseWithGrok(byte[] msg) { _LOG.trace("[Metron] Starting to parse incoming message with grok"); JSONObject jsonMessage = new JSONObject(); List<JSONObject> messages = new ArrayList<>(); try {/*from w w w.j a v a2s .c o m*/ String rawMessage = new String(msg, "UTF-8"); Match gm = grok.match(rawMessage); gm.captures(); Map grokMap = gm.toMap(); jsonMessage.putAll(gm.toMap()); jsonMessage.put("original_string", rawMessage); String startTime = (String) grokMap.get("start_time"); long timestamp = 0L; if (startTime != null) { timestamp = toEpoch(startTime); jsonMessage.put("timestamp", timestamp); } else { jsonMessage.put("timestamp", "0"); } String endTime = (String) grokMap.get("end_time"); if (endTime != null) { jsonMessage.put("end_time", toEpoch(endTime)); } else { jsonMessage.put("end_time", "0"); } jsonMessage.remove("YAF_DELIMITED"); jsonMessage.remove("start_time"); messages.add(jsonMessage); } catch (Exception e) { e.printStackTrace(); return null; } return messages; }
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();//from ww w. j a 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.dia.kafka.solr.consumer.SolrKafkaConsumer.java
/** * Converts ISATools messages into Solr documents * * @param jsonObject//w w w . j a v a2s . com * @return */ private SolrInputDocument convertIsaMsgSolr(JSONObject jsonObject) { System.out.format("[%s] Processing msg from %s\n", this.getClass().getSimpleName(), DataSource.ISATOOLS); final SolrInputDocument inputDoc = new SolrInputDocument(); jsonObject.remove(SOURCE_TAG); Iterator<?> keys = jsonObject.keySet().iterator(); List<String> invNames = new ArrayList<String>(); List<String> invMid = new ArrayList<String>(); List<String> invLastNames = new ArrayList<String>(); while (keys.hasNext()) { String key = (String) keys.next(); String cleanKey = updateCommentPreffix(key); if (!ISA_IGNORED_SET.contains(cleanKey)) { if (cleanKey.equals("DA_Number")) { inputDoc.addField("id", jsonObject.get(key)); } else if (cleanKey.equals("Study_Person_First_Name")) { // dealing with investigators' names invNames.add(jsonObject.get(key).toString()); } else if (cleanKey.equals("Study_Person_Mid_Initials")) { invMid.add(jsonObject.get(key).toString()); } else if (cleanKey.equals("Description")) { invLastNames.add(jsonObject.get(key).toString()); } else if (cleanKey.equals("Tags")) { inputDoc.addField("Study_Tags", jsonObject.get(key)); } else if (cleanKey.equals("Created_with_configuration")) { inputDoc.addField("Created_With_Configuration", jsonObject.get(key)); } else {//if (!key.startsWith("_")){ inputDoc.addField(cleanKey, jsonObject.get(key)); } } } JSONArray jsonArray = new JSONArray(); for (int cnt = 0; cnt < invLastNames.size(); cnt++) { StringBuilder sb = new StringBuilder(); sb.append(invNames.get(cnt) != null ? invNames.get(cnt) : "").append(" "); sb.append(invMid.get(cnt) != null ? invMid.get(cnt) : "").append(" "); sb.append(invLastNames.get(cnt) != null ? invLastNames.get(cnt) : ""); jsonArray.add(sb.toString()); } if (!jsonArray.isEmpty()) inputDoc.addField("Investigator", jsonArray.toJSONString()); return inputDoc; }
From source file:org.dia.kafka.solr.consumer.SolrKafkaConsumer.java
/** * Converts OODT messages into Solr documents * {@code {"TargetName":["target"],"FileLocation":["\/data\/archive\/035131.pdf"],"ProjectName":["project"],"DataAssetGroupName":["data asset"],"Compound":["compound"],"Investigator":["investigator"],"ProductStructure":["Flat"],"MimeType":["application\/octet-stream","application\/pdf","application","pdf"],"AssetType":["Processed"],"CAS.ProductName":["035131.pdf"],"CAS.ProductReceivedTime":["2015-06-09T08:31:11.749-07:00"],"Filename":["035131.pdf"],"DataSource":"OODT","SampleType":["type"],"AssayPurpose":["assay"],"Vendor":["vendor"],"CAS.ProductId":["8ed237e5-0ebc-11e5-8684-d7284d57b93d"],"ProductType":["Processed"],"ProjectDesciption":["description"],"ProductName":["DA0000023,Processed,035131.pdf"],"ExperimentType":["experiment1"],"DataAssetGroupNumber":["DA0000023"],"Department":["dep"],"Platform":["aplatform"]}} * * @param jsonObject// ww w . j a v a 2 s . c o m * @return */ private SolrInputDocument convertOodtMsgSolr(JSONObject jsonObject) { System.out.format("[%s] Processing msg from %s\n", this.getClass().getSimpleName(), DataSource.OODT); final SolrInputDocument inputDoc = new SolrInputDocument(); jsonObject.remove(SOURCE_TAG); Iterator<?> keys = jsonObject.keySet().iterator(); while (keys.hasNext()) { String key = (String) keys.next(); if (!Constants.OODT_IGNORED_SET.contains(key)) { if (key.equals("DataAssetGroupNumber")) { inputDoc.addField("id", jsonObject.get(key)); } else if (key.equals("ProjectDesciption")) { inputDoc.addField("Study_Description", jsonObject.get(key)); } else if (key.equals("ProjectName")) { inputDoc.addField("Study_Title", jsonObject.get(key)); } else {//if (!key.startsWith("_")){ inputDoc.addField(key, jsonObject.get(key)); } } } return inputDoc; }
From source file:org.dia.kafka.solr.consumer.SolrKafkaConsumer.java
/** * {@code//from ww w . j ava 2s .com * {"studies":[ * { * "ParticipantAliasSourceProperty" : "null", * "ParticipantAliasProperty" : "null", * "Description" : "Some Description", * "EndDate" : "null", * "_labkeyurl_Label" : "/labkey/project/Project Name/start.view?", * "AssayPlan" : "null", * "Grant" : "Some Foundation", * "StartDate" : "Tue Jan 01 00:00:00 EST 2008", * "Investigator" : "Some PI, PhD", * "ParticipantAliasDatasetId" : "null", * "Label" : "Interactive Example - Study", * "Species" : "null", * "Container" : "6f743c1b-9506-1032-b9bf-51af32c4d069", * "_labkeyurl_Container" : "/labkey/project/Study Name/begin.view?", * } * ] * } * } * * @param jsonObject * @return * @throws IOException */ private SolrInputDocument convertLabkeyMsgSolr(JSONObject jsonObject) { System.out.format("[%s] Processing msg from %s\n", this.getClass().getSimpleName(), DataSource.LABKEY); final SolrInputDocument inputDoc = new SolrInputDocument(); jsonObject.remove(SOURCE_TAG); Iterator<?> keys = jsonObject.keySet().iterator(); while (keys.hasNext()) { String key = (String) keys.next(); if (key.equals("Container")) { inputDoc.addField("id", jsonObject.get(key)); } else if (key.equals("Description")) { inputDoc.addField("Study_Description", jsonObject.get(key)); } else if (key.equals("_labkeyurl_Container")) { inputDoc.addField("Labkeyurl_Container", jsonObject.get(key)); } else if (key.equals("_labkeyurl_Label")) { inputDoc.addField("Labkeyurl_Label", jsonObject.get(key)); } else if (key.equals("Investigator")) { inputDoc.addField("Investigator", StringUtils.split(jsonObject.get(key).toString(), ";")); } else if (key.equals("Grant")) { inputDoc.addField("Study_Grant_Number", jsonObject.get(key)); } else { inputDoc.addField(key, jsonObject.get(key).toString()); } } return inputDoc; }
From source file:org.imsglobal.lti.toolProvider.service.ToolSettings.java
/** * Get the tool settings./*from ww w. j a v a2 s . c o m*/ * * @param int $mode Mode for request (optional, default is current level only) * * @return mixed The array of settings if successful, otherwise false */ @SuppressWarnings("rawtypes") public Map<String, List<String>> get(int mode) { JSONObject response = new JSONObject(); Map<String, List<String>> parameter = new HashMap<String, List<String>>(); if (mode == MODE_ALL_LEVELS) { LTIUtil.setParameter(parameter, "bubble", "all"); } else if (mode == MODE_DISTINCT_NAMES) { LTIUtil.setParameter(parameter, "bubble", "distinct"); } LTIMessage http = this.send("GET", parameter); JSONObject responseJson = http.getResponseJson(); if (!http.isOk()) { response = null; } else if (simple) { response = responseJson; } else if (responseJson.containsKey("@graph")) { JSONObject graph = (JSONObject) responseJson.get("@graph"); for (Object level : graph.keySet()) { JSONObject jlevel = (JSONObject) level; JSONObject settings = (JSONObject) jlevel.get("custom"); settings.remove("@id"); String type = (String) jlevel.get("@type"); String theLevel = LEVEL_NAMES.get(type); response.put(theLevel, settings); } } //response is a JSONObject, with keys pointing to JSON objects. Need to parse this down to //Map<String, List<String>> to match the rest of the library, and get it out of JSON return parseToMap(response); }
From source file:org.imsglobal.lti2.LTI2Util.java
@SuppressWarnings({ "unchecked", "unused" }) public static Object getSettings(HttpServletRequest request, String scope, JSONObject link_settings, JSONObject binding_settings, JSONObject proxy_settings, String link_url, String binding_url, String proxy_url) {//from w w w. j a v a 2s . c om // Check to see if we are doing the bubble String bubbleStr = request.getParameter("bubble"); String acceptHdr = request.getHeader("Accept"); String contentHdr = request.getContentType(); if (bubbleStr != null && bubbleStr.equals("all") && acceptHdr.indexOf(StandardServices.TOOLSETTINGS_FORMAT) < 0) { return "Simple format does not allow bubble=all"; } if (SCOPE_LtiLink.equals(scope) || SCOPE_ToolProxyBinding.equals(scope) || SCOPE_ToolProxy.equals(scope)) { // All good } else { return "Bad Setttings Scope=" + scope; } boolean bubble = bubbleStr != null && "GET".equals(request.getMethod()); boolean distinct = bubbleStr != null && "distinct".equals(bubbleStr); boolean bubbleAll = bubbleStr != null && "all".equals(bubbleStr); // Check our output format boolean acceptComplex = acceptHdr == null || acceptHdr.indexOf(StandardServices.TOOLSETTINGS_FORMAT) >= 0; if (distinct && link_settings != null && scope.equals(SCOPE_LtiLink)) { Iterator<String> i = link_settings.keySet().iterator(); while (i.hasNext()) { String key = (String) i.next(); if (binding_settings != null) binding_settings.remove(key); if (proxy_settings != null) proxy_settings.remove(key); } } if (distinct && binding_settings != null && scope.equals(SCOPE_ToolProxyBinding)) { Iterator<String> i = binding_settings.keySet().iterator(); while (i.hasNext()) { String key = (String) i.next(); if (proxy_settings != null) proxy_settings.remove(key); } } // Lets get this party started... JSONObject jsonResponse = null; if ((distinct || bubbleAll) && acceptComplex) { jsonResponse = new JSONObject(); jsonResponse.put(LTI2Constants.CONTEXT, StandardServices.TOOLSETTINGS_CONTEXT); JSONArray graph = new JSONArray(); boolean started = false; if (link_settings != null && SCOPE_LtiLink.equals(scope)) { JSONObject cjson = new JSONObject(); cjson.put(LTI2Constants.JSONLD_ID, link_url); cjson.put(LTI2Constants.TYPE, SCOPE_LtiLink); cjson.put(LTI2Constants.CUSTOM, link_settings); graph.add(cjson); started = true; } if (binding_settings != null && (started || SCOPE_ToolProxyBinding.equals(scope))) { JSONObject cjson = new JSONObject(); cjson.put(LTI2Constants.JSONLD_ID, binding_url); cjson.put(LTI2Constants.TYPE, SCOPE_ToolProxyBinding); cjson.put(LTI2Constants.CUSTOM, binding_settings); graph.add(cjson); started = true; } if (proxy_settings != null && (started || SCOPE_ToolProxy.equals(scope))) { JSONObject cjson = new JSONObject(); cjson.put(LTI2Constants.JSONLD_ID, proxy_url); cjson.put(LTI2Constants.TYPE, SCOPE_ToolProxy); cjson.put(LTI2Constants.CUSTOM, proxy_settings); graph.add(cjson); } jsonResponse.put(LTI2Constants.GRAPH, graph); } else if (distinct) { // Simple format output jsonResponse = proxy_settings; if (SCOPE_LtiLink.equals(scope)) { jsonResponse.putAll(binding_settings); jsonResponse.putAll(link_settings); } else if (SCOPE_ToolProxyBinding.equals(scope)) { jsonResponse.putAll(binding_settings); } } else { // bubble not specified jsonResponse = new JSONObject(); jsonResponse.put(LTI2Constants.CONTEXT, StandardServices.TOOLSETTINGS_CONTEXT); JSONObject theSettings = null; String endpoint = null; if (SCOPE_LtiLink.equals(scope)) { endpoint = link_url; theSettings = link_settings; } else if (SCOPE_ToolProxyBinding.equals(scope)) { endpoint = binding_url; theSettings = binding_settings; } if (SCOPE_ToolProxy.equals(scope)) { endpoint = proxy_url; theSettings = proxy_settings; } if (acceptComplex) { JSONArray graph = new JSONArray(); JSONObject cjson = new JSONObject(); cjson.put(LTI2Constants.JSONLD_ID, endpoint); cjson.put(LTI2Constants.TYPE, scope); cjson.put(LTI2Constants.CUSTOM, theSettings); graph.add(cjson); jsonResponse.put(LTI2Constants.GRAPH, graph); } else { jsonResponse = theSettings; } } return jsonResponse; }
From source file:org.jasig.ssp.reference.AbstractReferenceTest.java
/** * Tests supported HTTP Get methods using a positive test. Should be authenticated before running. * Note: contentToPostPut should have a UUID named "id" typical to SSP db convention * Do not expect passed objects to have the same dates and created/modified fields after this test. * @param urlToTest/*w w w . j av a 2 s . c o m*/ * @param individualUUIDToTest * @param contentToPostPut */ public final void referencePositiveSupportedMethodTest(final String urlToTest, final String individualUUIDToTest, final JSONObject contentToPostPut) { int checkResultCount = -2; //get all (store results to check at the end) Response checkItemCount = expect().statusCode(200).log().ifError().contentType("application/json").when() .get(urlToTest); String result = checkItemCount.getBody().jsonPath().getJsonObject("results").toString(); if (StringUtils.isNotBlank(result)) { checkResultCount = Integer.parseInt(result); } else { LOGGER.error("Get all method failed at beginning of Positive Test! No results returned."); fail("GET all failed."); } //get /id expect().statusCode(200).log().ifError().contentType("application/json").when() .get(urlToTest + "/" + individualUUIDToTest); contentToPostPut.remove("id"); //post Response postResponse = expect().statusCode(200).log().ifError().given().contentType("application/json") .body(contentToPostPut).when().post(urlToTest); final String postContentUUID = postResponse.getBody().jsonPath().getJsonObject("id").toString(); //get more complete data from post using get (MSSQL version is not reliable without this extra get) postResponse = expect().statusCode(200).log().ifError().contentType("application/json").when() .get(urlToTest + "/" + postContentUUID); final Map parsedPostResponse = postResponse.getBody().jsonPath().getJsonObject(""); contentToPostPut.put("id", postContentUUID); contentToPostPut.put("createdBy", getCurrentLoginCreatedModifiedBy()); contentToPostPut.put("modifiedBy", getCurrentLoginCreatedModifiedBy()); contentToPostPut.put("createdDate", parsedPostResponse.get("createdDate")); contentToPostPut.put("modifiedDate", parsedPostResponse.get("modifiedDate")); //verify post worked expect().statusCode(200).log().ifError().contentType("application/json").body("", equalTo(contentToPostPut)) .when().get(urlToTest + "/" + postContentUUID); contentToPostPut.remove("id"); contentToPostPut.put("name", ("testReferencePut" + testPassDeConflictNumber)); //put expect().statusCode(200).log().ifError().given().contentType("application/json").body(contentToPostPut) .when().put(urlToTest + "/" + postContentUUID); //get more complete data from put using get final Response putResponse = expect().statusCode(200).log().ifError().contentType("application/json").when() .get(urlToTest + "/" + postContentUUID); contentToPostPut.put("id", postContentUUID); contentToPostPut.put("modifiedDate", putResponse.getBody().jsonPath().getJsonObject("modifiedDate")); //verify put worked expect().statusCode(200).log().ifError().contentType("application/json").body("", equalTo(contentToPostPut)) .when().get(urlToTest + "/" + postContentUUID); //delete expect().statusCode(200).log().ifError().when().delete(urlToTest + "/" + postContentUUID); contentToPostPut.put("objectStatus", "INACTIVE"); //get verify delete worked final Response deleteCheckResponse = expect().statusCode(200).log().ifError() .contentType("application/json").when().get(urlToTest + "/" + postContentUUID); contentToPostPut.put("modifiedDate", deleteCheckResponse.getBody().jsonPath().getJsonObject("modifiedDate")); //verify delete is still intact but inactive expect().statusCode(200).log().ifError().contentType("application/json").body("", equalTo(contentToPostPut)) .when().get(urlToTest + "/" + postContentUUID); //get (verify result # matches expected active) expect().statusCode(200).log().ifError().contentType("application/json") .body("results", equalTo(checkResultCount)).given().queryParam("status", "ACTIVE").when() .get(urlToTest); //get (verify result # matches expected inactive) expect().statusCode(200).log().ifError().contentType("application/json").body("results", equalTo(1)).given() .queryParam("status", "INACTIVE").when().get(urlToTest); }
From source file:org.jasig.ssp.reference.AbstractReferenceTest.java
/** * Tests supported HTTP methods with negative tests. Should be authenticated before running. * Note: invalid content can be valid and this method will make changes to make it invalid * Do not expect passed objects to be valid after this test. * @param urlToTest/*from w w w . j a v a 2 s. c om*/ * @param invalidContentToPostPut * @param validContentToVerify */ public final void referenceNegativeSupportedMethodTest(final String urlToTest, final JSONObject invalidContentToPostPut, final JSONObject validContentToVerify) { int checkResultCount = 0; //get all (store results to check at the end) Response checkItemCount = expect().statusCode(200).log().ifError().contentType("application/json").when() .get(urlToTest); String result = checkItemCount.getBody().jsonPath().getJsonObject("results").toString(); if (StringUtils.isNotBlank(result)) { checkResultCount = Integer.parseInt(result); } else { LOGGER.error("Get all method failed in Negative Test! No results returned."); fail("GET all failed Negative Tests."); } //get invalid id expect().statusCode(404).contentType("application/json").when() .get(urlToTest + "/70b982b0-68d7-11e3-949a-0800200c9a66"); invalidContentToPostPut.remove("id"); final String name = invalidContentToPostPut.get("name").toString(); invalidContentToPostPut.remove("name"); //post empty name expect().statusCode(400).given().contentType("application/json").body(invalidContentToPostPut).when() .post(urlToTest); invalidContentToPostPut.put("name", name); if (invalidContentToPostPut.containsKey("code")) { invalidContentToPostPut.remove("code"); //post empty code expect().statusCode(500).given().contentType("application/json").body(invalidContentToPostPut).when() .post(urlToTest); } invalidContentToPostPut.put("objectStatus", ""); //put expect().statusCode(500).given().contentType("application/json").body(invalidContentToPostPut).when() .put(urlToTest + "/" + validContentToVerify.get("id")); //verify put didn't work expect().statusCode(200).contentType("application/json").body("", equalTo(validContentToVerify)).when() .get(urlToTest + "/" + validContentToVerify.get("id")); //delete expect().statusCode(404).when().delete(urlToTest + "/70b982b0-68d7-11e3-949a-0800200c9a66"); //get all (verify result # is unchanged) expect().statusCode(200).log().ifError().contentType("application/json") .body("results", equalTo(checkResultCount)).when().get(urlToTest); }