List of usage examples for org.json.simple.parser ParseException printStackTrace
public void printStackTrace()
From source file:it.polimi.geinterface.network.MessageUtils.java
/** * Method retrieving message type form a {@link String} representing the message received. *///w w w .java 2 s .co m public static MessageType getMsgType(String msgJson) { JSONParser parser = new JSONParser(); JSONObject jsonMsg; String msgType = ""; try { jsonMsg = (JSONObject) parser.parse(msgJson); JSONObject message = (JSONObject) jsonMsg.get(JsonStrings.MESSAGE); msgType = ((String) message.get(JsonStrings.MSG_TYPE)).toUpperCase().trim(); } catch (ParseException e) { e.printStackTrace(); } for (MessageType type : MessageType.values()) if (type.name().equals(msgType)) return type; return null; }
From source file:it.polimi.geinterface.network.MessageUtils.java
/** * Method returning the topic to use to send a {@link MessageType#SYNC_RESP} in reply to a corresponding request *///from www . j a v a 2 s .co m public static String getRequestTopicFromMessage(String message) { JSONObject jsonMsg; JSONParser parser = new JSONParser(); MessageType type = getMsgType(message); if (type.ordinal() == MessageType.SYNC_REQ.ordinal() || type.ordinal() == MessageType.SYNC_RESP.ordinal()) { try { jsonMsg = (JSONObject) parser.parse(message); JSONObject msg = (JSONObject) jsonMsg.get(JsonStrings.MESSAGE); return (String) msg.get(JsonStrings.TOPIC_REPLY); } catch (ParseException e) { e.printStackTrace(); } } return null; }
From source file:it.polimi.geinterface.network.MessageUtils.java
/** * Method returning the "valid bit" value of a {@link MessageType#CHECK_OUT} *//*from www. j a va 2 s. c o m*/ public static boolean getValidBitFromMessage(String message) { JSONObject jsonMsg; JSONParser parser = new JSONParser(); MessageType type = getMsgType(message); if (type.ordinal() == MessageType.CHECK_OUT.ordinal()) { try { jsonMsg = (JSONObject) parser.parse(message); JSONObject msg = (JSONObject) jsonMsg.get(JsonStrings.MESSAGE); return (Boolean) msg.get(JsonStrings.VALID); } catch (ParseException e) { e.printStackTrace(); } } return false; }
From source file:it.polimi.geinterface.network.MessageUtils.java
/** * Method returning the {@link Group} "serialized" in a {@link MessageType#SYNC_REQ} */// w ww. jav a 2 s . co m public static JSONObject getGroupFromMessage(String msg) { JSONObject jsonMsg; JSONParser parser = new JSONParser(); MessageType type = getMsgType(msg); if (type.ordinal() == MessageType.SYNC_REQ.ordinal()) { try { jsonMsg = (JSONObject) parser.parse(msg); JSONObject msgObject = (JSONObject) jsonMsg.get(JsonStrings.MESSAGE); return (JSONObject) msgObject.get(JsonStrings.GROUP); } catch (ParseException e) { e.printStackTrace(); } } return null; }
From source file:it.polimi.geinterface.network.MessageUtils.java
/** * Method returning the {@link DistanceRange} reported in a message having type {@link MessageType#PROPERTIES_UPDATE} *//*w w w. ja v a 2 s. co m*/ public static DistanceRange getDistanceRangeFromMessage(String message) { JSONObject jsonMsg; JSONParser parser = new JSONParser(); MessageType type = getMsgType(message); if (type.ordinal() == MessageType.PROXIMITY_UPDATE.ordinal() || type.ordinal() == MessageType.SYNC_RESP.ordinal() || type.ordinal() == MessageType.SYNC_REQ.ordinal()) { try { jsonMsg = (JSONObject) parser.parse(message); JSONObject msg = (JSONObject) jsonMsg.get(JsonStrings.MESSAGE); String jsonDistance = (String) msg.get(JsonStrings.DISTANCE_RANGE); for (DistanceRange d : DistanceRange.values()) if (d.name().equals(jsonDistance)) return d; } catch (ParseException e) { e.printStackTrace(); } } return null; }
From source file:it.polimi.geinterface.network.MessageUtils.java
/** * Method returning a {@link Set} of oldProperties passing as parameter a message of type {@link MessageType#PROPERTIES_UPDATE} *//*from w ww .j a v a 2s. com*/ public static JSONObject getOldPropertiesFromMessage(String message) { JSONObject jsonMsg; JSONParser parser = new JSONParser(); MessageType type = getMsgType(message); if (type.ordinal() == MessageType.PROPERTIES_UPDATE.ordinal()) { try { jsonMsg = (JSONObject) parser.parse(message); JSONObject msg = (JSONObject) jsonMsg.get(JsonStrings.MESSAGE); JSONObject jsonEntity = (JSONObject) msg.get(JsonStrings.ENTITY); JSONObject jsonOldProp = (JSONObject) jsonEntity.get(JsonStrings.OLD_PROPERTIES); /* Set<String> oldProperties = new HashSet<String>(); for(Object o : jsonOldProp) if(!o.toString().equals("")) oldProperties.add(o.toString()); */ return jsonOldProp; } catch (ParseException e) { e.printStackTrace(); } } return null; }
From source file:it.polimi.geinterface.network.MessageUtils.java
/** * Method returning a list of {@link Entity} objects retrieved from a message having type {@link MessageType#PROX_BEACONS} or * {@link MessageType#SYNC_RESP}/*w w w. j a v a 2 s .c o m*/ */ public static ArrayList<Entity> getBeaconsFromMsg(String message) { JSONObject jsonMsg; JSONParser parser = new JSONParser(); MessageType type = getMsgType(message); if (type.ordinal() == MessageType.PROX_BEACONS.ordinal() || type.ordinal() == MessageType.SYNC_REQ.ordinal()) try { jsonMsg = (JSONObject) parser.parse(message); JSONObject msg = (JSONObject) jsonMsg.get(JsonStrings.MESSAGE); JSONArray beaconsJsonArray = (JSONArray) msg.get(JsonStrings.BEACONS); ArrayList<Entity> beacons = new ArrayList<>(); for (Object o : beaconsJsonArray) { JSONObject beaconJsonObject = (JSONObject) o; String beaconId = (String) beaconJsonObject.get(JsonStrings.BEACON_ID); String jsonDistance = (String) beaconJsonObject.get(JsonStrings.DISTANCE_RANGE); DistanceRange distance = null; for (DistanceRange d : DistanceRange.values()) if (d.name().equals(jsonDistance)) distance = d; Entity beacon = new Entity.Builder(beaconId, Type.BLE_BEACON).setDistance(distance).build(); beacons.add(beacon); } return beacons; } catch (ParseException e) { e.printStackTrace(); } return null; }
From source file:it.polimi.geinterface.network.MessageUtils.java
/** * Method that returns an {@link Entity} from a message, dealing woth different {@link MessageType}. * /*from w ww. ja v a2s.c om*/ * @param position - possible values are 1 or 2, returning the first or the second {@link Entity} contained in the message. * position = 2 makes sense only in case of {@link MessageType#PROXIMITY_UPDATE}. In case of other {@link MessageType} only * position = 1 can be passed. */ public static Entity getEntityFromMessage(int position, String message) { JSONObject jsonMsg; JSONParser parser = new JSONParser(); MessageType type = getMsgType(message); if (position == 1) { switch (type) { case PROXIMITY_UPDATE: try { jsonMsg = (JSONObject) parser.parse(message); JSONObject msg = (JSONObject) jsonMsg.get(JsonStrings.MESSAGE); JSONObject jsonEntity = (JSONObject) msg.get(JsonStrings.ENTITY_1); return createEntity(type, jsonEntity); } catch (ParseException e) { e.printStackTrace(); } break; case PROPERTIES_UPDATE: case PROX_BEACONS: case SYNC_RESP: case CHECK_IN: case CHECK_OUT: try { jsonMsg = (JSONObject) parser.parse(message); JSONObject msg = (JSONObject) jsonMsg.get(JsonStrings.MESSAGE); JSONObject jsonEntity = (JSONObject) msg.get(JsonStrings.ENTITY); return createEntity(type, jsonEntity); } catch (ParseException e) { e.printStackTrace(); } default: return null; } return null; } if (position == 2) { if (type.ordinal() == MessageType.PROXIMITY_UPDATE.ordinal()) { try { jsonMsg = (JSONObject) parser.parse(message); JSONObject msg = (JSONObject) jsonMsg.get(JsonStrings.MESSAGE); JSONObject jsonEntity = (JSONObject) msg.get(JsonStrings.ENTITY_2); return createEntity(type, jsonEntity); } catch (ParseException e) { e.printStackTrace(); } } return null; } return null; }
From source file:net.modsec.ms.connector.ConnRequestHandler.java
/** * Parses the incomming requests and process it accordingly. * @param msg/*from w w w .j a va 2 s.co m*/ */ public static void onConRequest(String msg) { log.info("onConRequest called :" + msg); JSONParser parser = new JSONParser(); try { Object obj = parser.parse(msg); JSONObject json = (JSONObject) obj; String action = (String) json.get("action"); if (action.equals("start")) { onStartRequest(json); } else if (action.equals("stop")) { onStopRequest(json); } else if (action.equals("restart")) { onRestartRequest(json); } else if (action.equals("status")) { onStatusRequest(json); } else if (action.equals("readMSConfig")) { onReadMSConfig(json); } else if (action.equals("writeMSConfig")) { onWriteMSConfig(json); } else if (action.equals("deployRules")) { onDeployMSRules(json); } } catch (ParseException e) { e.printStackTrace(); } }
From source file:com.aerospike.load.Parser.java
/** * Process column definitions in JSON formated file and create two lists for metadata and bindata and one list for metadataLoader * @param file Config file name/*from w w w. ja va2 s. c om*/ * @param metadataLoader Map of metadata for loader to use, given in config file * @param metadataColumnDefs List of column definitions for metadata of bins like key,set * @param binColumnDefs List of column definitions for bins * @param params User given parameters * @throws ParseException * @throws IOException */ public static boolean processJSONColumnDefinitions(File file, HashMap<String, String> metadataConfigs, List<ColumnDefinition> metadataColumnDefs, List<ColumnDefinition> binColumnDefs, Parameters params) { boolean processConfig = false; if (params.verbose) { log.setLevel(Level.DEBUG); } try { // Create parser object JSONParser jsonParser = new JSONParser(); // Read the json config file Object obj; obj = jsonParser.parse(new FileReader(file)); JSONObject jobj; // Check config file contains metadata for datafile if (obj == null) { log.error("Empty config File"); return processConfig; } else { jobj = (JSONObject) obj; log.debug("Config file contents:" + jobj.toJSONString()); } // Get meta data of loader // Search for input_type if ((obj = getJsonObject(jobj, Constants.VERSION)) != null) { metadataConfigs.put(Constants.VERSION, obj.toString()); } else { log.error("\"" + Constants.VERSION + "\" Key is missing in config file"); return processConfig; } if ((obj = getJsonObject(jobj, Constants.INPUT_TYPE)) != null) { // Found input_type, check for csv if (obj instanceof String && obj.toString().equals(Constants.CSV_FILE)) { // Found csv format metadataConfigs.put(Constants.INPUT_TYPE, obj.toString()); // Search for csv_style if ((obj = getJsonObject(jobj, Constants.CSV_STYLE)) != null) { // Found csv_style JSONObject cobj = (JSONObject) obj; // Number_Of_Columns in data file if ((obj = getJsonObject(cobj, Constants.COLUMNS)) != null) { metadataConfigs.put(Constants.COLUMNS, obj.toString()); } else { log.error("\"" + Constants.COLUMNS + "\" Key is missing in config file"); return processConfig; } // Delimiter for parsing data file if ((obj = getJsonObject(cobj, Constants.DELIMITER)) != null) metadataConfigs.put(Constants.DELIMITER, obj.toString()); // Skip first row of data file if it contains column names if ((obj = getJsonObject(cobj, Constants.IGNORE_FIRST_LINE)) != null) metadataConfigs.put(Constants.IGNORE_FIRST_LINE, obj.toString()); } else { log.error("\"" + Constants.CSV_STYLE + "\" Key is missing in config file"); return processConfig; } } else { log.error("\"" + obj.toString() + "\" file format is not supported in config file"); return processConfig; } } else { log.error("\"" + Constants.INPUT_TYPE + "\" Key is missing in config file"); return processConfig; } // Get metadata of records // Get key definition of records if ((obj = getJsonObject(jobj, Constants.KEY)) != null) { metadataColumnDefs.add(getColumnDefs((JSONObject) obj, Constants.KEY)); } else { log.error("\"" + Constants.KEY + "\" Key is missing in config file"); return processConfig; } // Get set definition of records. Optional because we can get "set" name from user. if ((obj = getJsonObject(jobj, Constants.SET)) != null) { if (obj instanceof String) { metadataColumnDefs.add(new ColumnDefinition(Constants.SET, obj.toString(), true, true, "string", "string", null, -1, -1, null, null)); } else { metadataColumnDefs.add(getColumnDefs((JSONObject) obj, Constants.SET)); } } // Get bin column definitions JSONArray binList; if ((obj = getJsonObject(jobj, Constants.BINLIST)) != null) { // iterator for bins binList = (JSONArray) obj; Iterator<?> i = binList.iterator(); // take each bin from the JSON array separately while (i.hasNext()) { JSONObject binObj = (JSONObject) i.next(); binColumnDefs.add(getColumnDefs(binObj, Constants.BINLIST)); } } else { return processConfig; } log.info(String.format("Number of columns: %d(metadata) + %d(bins)", (metadataColumnDefs.size()), binColumnDefs.size())); processConfig = true; } catch (IOException ie) { log.error("File:" + Utils.getFileName(file.getName()) + " Config i/o Error: " + ie.toString()); if (log.isDebugEnabled()) { ie.printStackTrace(); } } catch (ParseException pe) { log.error("File:" + Utils.getFileName(file.getName()) + " Config parsing Error: " + pe.toString()); if (log.isDebugEnabled()) { pe.printStackTrace(); } } catch (Exception e) { log.error("File:" + Utils.getFileName(file.getName()) + " Config unknown Error: " + e.toString()); if (log.isDebugEnabled()) { e.printStackTrace(); } } return processConfig; }