List of usage examples for org.json.simple JSONObject toJSONString
public String toJSONString()
From source file:com.fujitsu.dc.test.jersey.box.odatacol.schema.complextype.ComplexTypeUtils.java
/** * ComplexType?.// w w w. j av a2 s . c om * @param token * @param cell Cell?? * @param box Box?? * @param col Collection?? * @param complexTypeName ComplexType?? * @param body * @param code ?? * @return ? */ public static TResponse update(String token, String cell, String box, String col, String complexTypeName, JSONObject body, int code) { return Http.request("box/odatacol/update.txt").with("token", token) .with("accept", MediaType.APPLICATION_JSON).with("contentType", MediaType.APPLICATION_JSON) .with("cell", cell).with("box", box).with("collection", col + "/\\$metadata") .with("entityType", "ComplexType").with("ifMatch", "*").with("id", complexTypeName) .with("body", body.toJSONString()).returns().debug().statusCode(code); }
From source file:net.modsec.ms.connector.ConnRequestHandler.java
/** * Stops the modsecurity instance if not stopped before. * @param json// w w w .java 2s. com */ @SuppressWarnings("unchecked") public static void onStopRequest(JSONObject json) { log.info("onStopRequest called.. : " + json.toJSONString()); MSConfig config = MSConfig.getInstance(); String cmd = config.getConfigMap().get("MSStop"); JSONObject resp = executeShScript(cmd, json); if (((String) resp.get("status")).equals("0")) { resp.put("message", "Modsecuity Sucessfully Stoped"); } log.info("Sending Json :" + resp.toJSONString()); ConnectorService.getConnectorProducer().send(resp.toJSONString()); resp.clear(); }
From source file:net.modsec.ms.connector.ConnRequestHandler.java
/** * Starts the modsecurity instance if not running. * @param json //from w w w . ja v a 2s . c o m */ @SuppressWarnings("unchecked") public static void onStartRequest(JSONObject json) { log.info("onStartRequest called.. : " + json.toJSONString()); MSConfig config = MSConfig.getInstance(); String cmd = config.getConfigMap().get("MSStart"); JSONObject resp = executeShScript(cmd, json); if (((String) resp.get("status")).equals("0")) { resp.put("message", "Modsecuity Sucessfully Started"); } log.info("Sending Json :" + resp.toJSONString()); ConnectorService.getConnectorProducer().send(resp.toJSONString()); resp.clear(); }
From source file:net.modsec.ms.connector.ConnRequestHandler.java
/** * Restarts the instance./*from w w w. j ava2 s . co m*/ * @param json */ @SuppressWarnings("unchecked") public static void onRestartRequest(JSONObject json) { log.info("onRestartRequest called.. : " + json.toJSONString()); MSConfig config = MSConfig.getInstance(); String cmd = config.getConfigMap().get("MSRestart"); JSONObject resp = executeShScript(cmd, json); if (((String) resp.get("status")).equals("0")) { resp.put("message", "Modsecuity Sucessfully Restarted"); } log.info("Sending Json :" + resp.toJSONString()); ConnectorService.getConnectorProducer().send(resp.toJSONString()); resp.clear(); }
From source file:net.modsec.ms.connector.ConnRequestHandler.java
/** * Checks the modsecurity current status whether its running or stopped. * @param json/* w ww . j a v a 2s.c om*/ */ @SuppressWarnings("unchecked") public static void onStatusRequest(JSONObject json) { log.info("onStatusRequest called.. : " + json.toJSONString()); MSConfig config = MSConfig.getInstance(); String cmd = config.getConfigMap().get("MSStatus"); JSONObject resp = executeShScript(cmd, json); if (((String) resp.get("status")).equals("0")) { log.info("Message After Execution:" + (String) resp.get("message")); if (((String) resp.get("message")).contains("running")) { resp.put("msStatus", "1"); } else { resp.put("msStatus", "0"); } } log.info("Sending Json :" + resp.toJSONString()); ConnectorService.getConnectorProducer().send(resp.toJSONString()); resp.clear(); }
From source file:example.Test.java
public static void testJSON() { String textFile = "MobyDick.txt"; int numberOfAddedLines = 8000; System.out.println(// w w w . ja va 2s.com "This is a Test for the efficiency and accuracy of JSON encoding and decoding of the String Phone Lattice data structure"); System.out.println( " It tests how quickly a String Phone Lattice data structure can encode its contents into a JSON object,\n How quickly it can decode a JSON object resembling a String Phone Lattice data structure,\n and how accurately it can decode those JSON objects "); System.out.print( "As a bonus, it will also be testing the speed in which the JSONObjects from the JSON Simple library can be converted to and from String objects"); System.out.println("All time measurements are given in milliseconds "); System.out.println("The test will be parsing the words off of " + textFile + " "); System.out.println("The test will now start "); TextIO.readFile("src/" + textFile); String added = ""; for (int i = 0; i < numberOfAddedLines; i++) { added += " " + TextIO.getln().trim(); } String[] AddedWords = added.split(" "); System.out.println("Done parsing words "); System.out.println("**" + AddedWords.length + "** added words "); StringPhoneLattice root = new StringPhoneLattice(); for (String word : AddedWords) { root.addWord(word); } long startEncode, endEncode, startString, endString, startJSON, endJSON, startDecode, endDecode; startEncode = System.currentTimeMillis(); JSONObject json = root.toJSON(); endEncode = System.currentTimeMillis(); System.out.println("Time to encode to JSON: "); System.out.println("*" + (endEncode - startEncode) + "* "); startString = System.currentTimeMillis(); String stringJSON = json.toJSONString(); endString = System.currentTimeMillis(); System.out.println("Time to encode to JSON Object to String object: "); System.out.println("*" + (endString - startString) + "* "); startJSON = System.currentTimeMillis(); JSONObject json1 = (JSONObject) JSONValue.parse(stringJSON); //This is null! endJSON = System.currentTimeMillis(); System.out.println("Time to encode to String Object to JSON object: "); System.out.println("*" + (endJSON - startJSON) + "* "); startDecode = System.currentTimeMillis(); StringPhoneLattice root1 = new StringPhoneLattice(json1); endDecode = System.currentTimeMillis(); System.out.println("Time to encode to JSON Object to StringPhoneLattice object: "); System.out.println("*" + (endDecode - startDecode) + "* "); if (root1.toJSON().toJSONString().equals(stringJSON)) { System.out.println( "JSON Encoding and decoding of StringPhoneLattice data structure is completely accurate "); // System.out.println(root1.toJSON().toJSONString()); } else { System.out.println("JSON Encoding and decoding of StringPhoneLattice data structure is inaccurate "); System.out.println("Here is the original JSON output: "); System.out.println(json.toJSONString() + " "); System.out.println("Here is the JSON output after conversions: "); System.out.println(root1.toJSON().toJSONString() + " "); } }
From source file:co.alligo.toasted.routes.Announce.java
public static String announce(Request req, Response res, Servers servers) { JSONObject json = new JSONObject(); JSONObject result = new JSONObject(); String ip = req.ip();//ww w . j ava 2s . co m String port; res.header("Content-Type", "application/json"); if (!(req.headers("x-forwarded-for") == null)) { ip = req.headers("x-forwarded-for"); } if (req.attribute("port") == null) { result.put("code", 1); result.put("msg", "Invalid parameters, valid parameters are 'port' (int) and 'shutdown' (bool)"); json.put("result", result); return json.toJSONString(); } else { port = req.attribute("port").toString(); } if ("true".equals(req.attribute("shutdown").toString())) { servers.delServer(ip, port); } else { if (checkGameServer(ip, port)) { servers.addServer(ip, port); result.put("code", 0); result.put("msg", "Added server to list."); System.out.println("Added server to list" + ip + ":" + port); json.put("result", result); return json.toJSONString(); } else { result.put("code", 0); result.put("msg", "Failed to contact game server, are the ports open and forwarded correctly?"); json.put("result", result); return json.toJSONString(); } } return "500 Internal Server Error"; }
From source file:it.polimi.geinterface.network.MessageUtils.java
public static String addLogField(String message, String logId) { JSONParser parser = new JSONParser(); try {/*from w w w. ja v a 2s. c o m*/ JSONObject msg = (JSONObject) parser.parse(message); JSONObject m = (JSONObject) msg.get(JsonStrings.MESSAGE); MessageType msgType = MessageType.valueOf((String) m.get(JsonStrings.MSG_TYPE)); if (msgType.equals(MessageType.CHECK_OUT)) { m.put(JsonStrings.LOG_ID, logId); JSONObject newJsonMsg = new JSONObject(); newJsonMsg.put(JsonStrings.MESSAGE, m); return newJsonMsg.toJSONString(); } } catch (ParseException e) { e.printStackTrace(); } return message; }
From source file:com.oic.connection.Connections.java
public static void mapBroadCastMessage(JSONObject json, int mapid) { synchronized (userConnections) { try {// w w w . ja v a 2s . c o m for (int i = 0; i < userConnections.size(); i++) { WebSocketListener webSocket = userConnections.get(i); OicCharacter c = webSocket.getCharacter(); Session session = webSocket.getSession(); try { if (c.getMap().getMapId() == mapid) { if (session.isOpen()) { session.getRemote().sendString(json.toJSONString()); } else { session.close(); userConnections.remove(webSocket); } } } catch (NullPointerException e) { session.close(); userConnections.remove(webSocket); } } } catch (IOException e) { e.printStackTrace(); } } }
From source file:net.modsec.ms.connector.ConnRequestHandler.java
/** * Reads the modsecurity configuration file on modsecurity machine. * @param json/*from w w w . j a va 2s. c om*/ */ @SuppressWarnings("unchecked") public static void onReadMSConfig(JSONObject json) { log.info("onReadMSConfig called.. : " + json.toJSONString()); MSConfig serviceCfg = MSConfig.getInstance(); String fileName = serviceCfg.getConfigMap().get("MSConfigFile"); InputStream ins; BufferedReader br; try { File file = new File(fileName); DataInputStream in; @SuppressWarnings("resource") FileChannel channel = new RandomAccessFile(file, "rw").getChannel(); FileLock lock = channel.lock(); try { ins = new FileInputStream(file); in = new DataInputStream(ins); br = new BufferedReader(new InputStreamReader(in)); String line = ""; while ((line = br.readLine()) != null) { //log.info("Line :" + line); for (ModSecConfigFields field : ModSecConfigFields.values()) { if (line.startsWith(field.toString())) { if (line.trim().split(" ")[0].equals(field.toString())) { json.put(field.toString(), line.trim().split(" ")[1].replace("\"", "")); } } } } log.info("ModSecurity Configurations configurations Loaded ... "); } finally { lock.release(); } br.close(); in.close(); ins.close(); } catch (FileNotFoundException e1) { json.put("status", "1"); json.put("message", "configuration file not found"); e1.printStackTrace(); } catch (IOException | NullPointerException e) { json.put("status", "1"); json.put("message", "configuration file is corrupt"); e.printStackTrace(); } log.info("Sending Json :" + json.toJSONString()); ConnectorService.getConnectorProducer().send(json.toJSONString()); json.clear(); }