List of usage examples for org.json.simple JSONObject containsKey
boolean containsKey(Object key);
From source file:com.piusvelte.hydra.MySQLConnection.java
@SuppressWarnings("unchecked") @Override//from w w w . j a v a2 s . c om public JSONObject delete(String object, String selection) { JSONObject response = new JSONObject(); JSONArray errors = new JSONArray(); Statement s = null; ResultSet rs = null; try { s = mConnection.createStatement(); rs = s.executeQuery(String.format(DELETE_QUERY, object, selection).toString()); response.put("result", getResult(rs)); } catch (SQLException e) { errors.add(e.getMessage()); } finally { if (s != null) { if (rs != null) { try { rs.close(); } catch (SQLException e) { errors.add(e.getMessage()); } } try { s.close(); } catch (SQLException e) { errors.add(e.getMessage()); } } } response.put("errors", errors); if (!response.containsKey("result")) { JSONArray rows = new JSONArray(); JSONArray rowData = new JSONArray(); rows.add(rowData); response.put("result", rows); } return response; }
From source file:com.opensoc.parsing.TelemetryParserBolt.java
@SuppressWarnings("unchecked") public void execute(Tuple tuple) { LOG.trace("[OpenSOC] Starting to process a new incoming tuple"); byte[] original_message = null; try {//w w w . j a v a2 s . c o m original_message = tuple.getBinary(0); LOG.trace("[OpenSOC] 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("[OpenSOC] Attempting to transofrm binary message to JSON"); JSONObject transformed_message = _parser.parse(original_message); LOG.debug("[OpenSOC] 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("[OpenSOC] 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("[OpenSOC] 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:JSONParser.JSONOperations.java
public JSONObject statusJSONToModuleCount(JSONArray statusJSONArray) { JSONObject moduleWiseCount = new JSONObject(); for (Iterator<JSONObject> ticketIterator = statusJSONArray.iterator(); ticketIterator.hasNext();) { JSONObject ticket = ticketIterator.next(); String moduleName = ticket.get("moduleName").toString(); if (!moduleWiseCount.containsKey(moduleName)) { moduleWiseCount.put(moduleName, 1); } else {/* w w w .j a v a 2 s . c o m*/ int existingCount = (Integer) moduleWiseCount.get(moduleName); moduleWiseCount.put(moduleName, existingCount + 1); } } return moduleWiseCount; }
From source file:com.piusvelte.hydra.MSSQLConnection.java
@SuppressWarnings("unchecked") @Override//from w w w .j a va 2 s. c om public JSONObject delete(String object, String selection) { JSONObject response = new JSONObject(); JSONArray errors = new JSONArray(); Statement s = null; ResultSet rs = null; try { s = mConnection.createStatement(); rs = s.executeQuery(String.format(DELETE_QUERY, object, selection).toString()); response.put("result", getResult(rs)); } catch (SQLException e) { errors.add(e.getMessage()); e.printStackTrace(); } finally { if (s != null) { if (rs != null) { try { rs.close(); } catch (SQLException e) { errors.add(e.getMessage()); } } try { s.close(); } catch (SQLException e) { errors.add(e.getMessage()); } } } response.put("errors", errors); if (!response.containsKey("result")) { JSONArray rows = new JSONArray(); JSONArray rowData = new JSONArray(); rows.add(rowData); response.put("result", rows); } return response; }
From source file:com.pearson.eidetic.driver.threads.MonitorSnapshotVolumeTime.java
private HashMap<Date, ArrayList<Volume>> extractRunAt(ArrayList<Volume> volumes) { JSONParser parser = new JSONParser(); HashMap<Date, ArrayList<Volume>> returnHash = new HashMap(); for (Volume volume : volumes) { for (Tag tag : volume.getTags()) { String tagValue = null; if (tag.getKey().equalsIgnoreCase("Eidetic")) { tagValue = tag.getValue(); }/* w w w . ja v a2 s . c om*/ if (tagValue == null) { continue; } JSONObject eideticParameters; try { Object obj = parser.parse(tagValue); eideticParameters = (JSONObject) obj; } catch (Exception e) { logger.error("awsAccountNickname=\"" + awsAccount_.getUniqueAwsAccountIdentifier() + "\",Event=Error, Error=\"Malformed Eidetic Tag\", Volume_id=\"" + volume.getVolumeId() + "\", stacktrace=\"" + e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e) + "\""); continue; } JSONObject createSnapshot; try { createSnapshot = (JSONObject) eideticParameters.get("CreateSnapshot"); } catch (Exception e) { logger.error("awsAccountNickname=\"" + awsAccount_.getUniqueAwsAccountIdentifier() + "\",Event=Error, Error=\"Malformed Eidetic Tag\", Volume_id=\"" + volume.getVolumeId() + "\", stacktrace=\"" + e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e) + "\""); continue; } String runAt = null; if (createSnapshot.containsKey("RunAt")) { runAt = createSnapshot.get("RunAt").toString(); } Date date = null; try { date = dayFormat_.parse(runAt); } catch (ParseException e) { logger.error("awsAccountNickname=\"" + awsAccount_.getUniqueAwsAccountIdentifier() + "\",Event=Error, Error=\"Malformed Eidetic Tag\", Volume_id=\"" + volume.getVolumeId() + "\", stacktrace=\"" + e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e) + "\""); } if (date == null) { continue; } if (returnHash.keySet().contains(date)) { returnHash.get(date).add(volume); } else { ArrayList<Volume> newArrayList = new ArrayList(); newArrayList.add(volume); returnHash.put(date, newArrayList); } break; } } return returnHash; }
From source file:fr.bmartel.bboxapi.BboxApi.java
/** * //from ww w. j a v a 2s .c om * Retrieve full call log * * @param listener * listener for request result / failure * * @return true if request has been successfully initiated */ @Override public boolean getFullCallLog(final IFullCallLogListener listener) { ClientSocket clientSocket = new ClientSocket("gestionbbox.lan", 80); clientSocket.addClientSocketEventListener(new IHttpClientListener() { @Override public void onIncomingHttpFrame(HttpFrame frame, HttpStates httpStates, IClientSocket clientSocket) { if (httpStates == HttpStates.HTTP_FRAME_OK && frame.isHttpResponseFrame()) { // this is data coming from the server if (frame.getStatusCode() == 200) { String data = new String(frame.getBody().getBytes()); JSONArray obj = (JSONArray) JSONValue.parse(data); if (obj.size() > 0) { JSONObject item = (JSONObject) obj.get(0); if (item.containsKey("calllog")) { JSONArray sub_item = (JSONArray) item.get("calllog"); List<CallLog> logList = new ArrayList<CallLog>(); for (int i = 0; i < sub_item.size(); i++) { JSONObject callItem = (JSONObject) sub_item.get(i); if (callItem.containsKey("id") && callItem.containsKey("number") && callItem.containsKey("date") && callItem.containsKey("type") && callItem.containsKey("answered") && callItem.containsKey("duree")) { int id = Integer.parseInt(callItem.get("id").toString()); String number = callItem.get("number").toString(); long date = Long.parseLong(callItem.get("date").toString()); String type = callItem.get("type").toString(); int answered = Integer.parseInt(callItem.get("answered").toString()); int duree = Integer.parseInt(callItem.get("duree").toString()); boolean isAnswered = false; if (answered == 1) isAnswered = true; logList.add(new CallLog(id, number, date, CallType.getValue(type), isAnswered, duree)); } } listener.onFullCallLogReceived(logList); clientSocket.closeSocket(); return; } } } listener.onFullCallLogFailure(); clientSocket.closeSocket(); } } @Override public void onSocketError() { listener.onFullCallLogFailure(); } }); HashMap<String, String> headers = new HashMap<String, String>(); headers.put("Accept", "*/*"); headers.put("Host", "gestionbbox.lan"); headers.put("Cookie", token_header); HttpFrame frameRequest = new HttpFrame(HttpMethod.GET_REQUEST, new HttpVersion(1, 1), headers, "/api/v1/voip/fullcalllog/1", new ListOfBytes("")); clientSocket.write(frameRequest.toString().getBytes()); return false; }
From source file:fr.bmartel.bboxapi.BboxApi.java
/** * Voip data/*from w ww .j ava 2 s. c om*/ * * @param voipDataListener * listener called when voip data result has been received * @return true if request has been successfully initiated */ @Override public boolean voipData(final IVoipDataListener voipDataListener) { ClientSocket clientSocket = new ClientSocket("gestionbbox.lan", 80); clientSocket.addClientSocketEventListener(new IHttpClientListener() { @Override public void onIncomingHttpFrame(HttpFrame frame, HttpStates httpStates, IClientSocket clientSocket) { if (httpStates == HttpStates.HTTP_FRAME_OK && frame.isHttpResponseFrame()) { // this is data coming from the server if (frame.getStatusCode() == 200) { String data = new String(frame.getBody().getBytes()); JSONArray obj = (JSONArray) JSONValue.parse(data); if (obj.size() > 0) { JSONObject item = (JSONObject) obj.get(0); if (item.containsKey("voip")) { JSONArray sub_item = (JSONArray) item.get("voip"); if (sub_item.size() > 0) { JSONObject sub_item_first_element = (JSONObject) sub_item.get(0); if (sub_item_first_element.containsKey("id") && sub_item_first_element.containsKey("status") && sub_item_first_element.containsKey("callstate") && sub_item_first_element.containsKey("uri") && sub_item_first_element.containsKey("blockstate") && sub_item_first_element.containsKey("anoncallstate") && sub_item_first_element.containsKey("mwi") && sub_item_first_element.containsKey("message_count") && sub_item_first_element.containsKey("notanswered")) { int id = Integer.parseInt(sub_item_first_element.get("id").toString()); String status = sub_item_first_element.get("status").toString(); String callState = sub_item_first_element.get("callstate").toString(); String uri = sub_item_first_element.get("uri").toString(); int blockState = Integer .parseInt(sub_item_first_element.get("blockstate").toString()); int anoncallState = Integer .parseInt(sub_item_first_element.get("anoncallstate").toString()); int mwi = Integer.parseInt(sub_item_first_element.get("mwi").toString()); int messageCount = Integer .parseInt(sub_item_first_element.get("message_count").toString()); int notanswered = Integer .parseInt(sub_item_first_element.get("notanswered").toString()); Voip voip = new Voip(id, status, CallState.getValue(callState), uri, blockState, anoncallState, mwi, messageCount, notanswered); voipDataListener.onVoipDataReceived(voip); clientSocket.closeSocket(); return; } } } } } voipDataListener.onVoipDataFailure(); clientSocket.closeSocket(); } } @Override public void onSocketError() { voipDataListener.onVoipDataFailure(); } }); HashMap<String, String> headers = new HashMap<String, String>(); headers.put("Accept", "*/*"); headers.put("Host", "gestionbbox.lan"); headers.put("Cookie", token_header); HttpFrame frameRequest = new HttpFrame(HttpMethod.GET_REQUEST, new HttpVersion(1, 1), headers, "/api/v1/voip", new ListOfBytes("")); clientSocket.write(frameRequest.toString().getBytes()); return false; }
From source file:fr.bmartel.bboxapi.BboxApi.java
/** * Bbox device api/*from w w w . ja v a2s . co m*/ * * @param deviceListener * bbox device listener * @return true if request has been successfully initiated */ @Override public boolean bboxDevice(final IBboxDeviceListener deviceListener) { ClientSocket clientSocket = new ClientSocket("gestionbbox.lan", 80); clientSocket.addClientSocketEventListener(new IHttpClientListener() { @Override public void onIncomingHttpFrame(HttpFrame frame, HttpStates httpStates, IClientSocket clientSocket) { if (httpStates == HttpStates.HTTP_FRAME_OK && frame.isHttpResponseFrame()) { // this is data coming from the server if (frame.getStatusCode() == 200) { String data = new String(frame.getBody().getBytes()); JSONArray obj = (JSONArray) JSONValue.parse(data); if (obj.size() > 0) { JSONObject item = (JSONObject) obj.get(0); if (item.containsKey("device")) { JSONObject sub_item = (JSONObject) item.get("device"); if (sub_item.containsKey("now") && sub_item.containsKey("status") && sub_item.containsKey("numberofboots") && sub_item.containsKey("modelname") && sub_item.containsKey("user_configured") && sub_item.containsKey("display") && sub_item.containsKey("firstusedate") && sub_item.containsKey("uptime")) { // String now = // sub_item.get("now").toString(); int status = Integer.parseInt(sub_item.get("status").toString()); int bootNumber = Integer.parseInt(sub_item.get("numberofboots").toString()); String modelname = sub_item.get("modelname").toString(); int user_configured = Integer .parseInt(sub_item.get("user_configured").toString()); boolean userConfig = (user_configured == 0) ? false : true; JSONObject display = (JSONObject) sub_item.get("display"); int displayLuminosity = Integer.parseInt(display.get("luminosity").toString()); boolean displayState = (displayLuminosity == 100) ? true : false; String firstusedate = sub_item.get("firstusedate").toString(); BBoxDevice device = new BBoxDevice(status, bootNumber, modelname, userConfig, displayState, firstusedate); if (sub_item.containsKey("serialnumber")) { device.setSerialNumber(sub_item.get("serialnumber").toString()); } deviceListener.onBboxDeviceReceived(device); clientSocket.closeSocket(); return; } } } } deviceListener.onBboxDeviceFailure(); clientSocket.closeSocket(); } } @Override public void onSocketError() { deviceListener.onBboxDeviceFailure(); } }); HashMap<String, String> headers = new HashMap<String, String>(); headers.put("Accept", "*/*"); headers.put("Host", "gestionbbox.lan"); headers.put("Cookie", token_header); HttpFrame frameRequest = new HttpFrame(HttpMethod.GET_REQUEST, new HttpVersion(1, 1), headers, "/api/v1/device", new ListOfBytes("")); clientSocket.write(frameRequest.toString().getBytes()); return false; }
From source file:fr.bmartel.bboxapi.BboxApi.java
/** * /*w w w . j av a 2 s . c o m*/ * Retrieve all hosts * * @param listener * listener for request result / failure * * @return true if request has been successfully initiated */ @Override public boolean getHosts(final IHostsListener listener) { ClientSocket clientSocket = new ClientSocket("gestionbbox.lan", 80); clientSocket.addClientSocketEventListener(new IHttpClientListener() { @Override public void onIncomingHttpFrame(HttpFrame frame, HttpStates httpStates, IClientSocket clientSocket) { if (httpStates == HttpStates.HTTP_FRAME_OK && frame.isHttpResponseFrame()) { // this is data coming from the server if (frame.getStatusCode() == 200) { String data = new String(frame.getBody().getBytes()); JSONArray obj = (JSONArray) JSONValue.parse(data); if (obj.size() > 0) { JSONObject item = (JSONObject) obj.get(0); if (item.containsKey("hosts")) { JSONObject sub_item = (JSONObject) item.get("hosts"); if (sub_item.containsKey("list")) { JSONArray hostList = (JSONArray) sub_item.get("list"); List<Host> hostObjectList = new ArrayList<Host>(); for (int i = 0; i < hostList.size(); i++) { JSONObject hostEntry = (JSONObject) hostList.get(i); if (hostEntry.containsKey("id") && hostEntry.containsKey("hostname") && hostEntry.containsKey("macaddress") && hostEntry.containsKey("ipaddress") && hostEntry.containsKey("type") && hostEntry.containsKey("link") && hostEntry.containsKey("devicetype") && hostEntry.containsKey("firstseen") && hostEntry.containsKey("lastseen") && hostEntry.containsKey("lease") && hostEntry.containsKey("active")) { int id = Integer.parseInt(hostEntry.get("id").toString()); String hostname = hostEntry.get("hostname").toString(); String macaddress = hostEntry.get("macaddress").toString(); String ipaddress = hostEntry.get("ipaddress").toString(); String type = hostEntry.get("type").toString(); String link = hostEntry.get("link").toString(); String devicetype = hostEntry.get("devicetype").toString(); String firstseen = hostEntry.get("firstseen").toString(); String lastseen = hostEntry.get("lastseen").toString(); int lease = Integer.parseInt(hostEntry.get("lease").toString()); boolean active = (Integer .parseInt(hostEntry.get("active").toString()) == 0) ? false : true; hostObjectList.add(new Host(id, hostname, macaddress, ipaddress, type, link, devicetype, firstseen, lastseen, lease, active)); } } listener.onHostsReceived(hostObjectList); clientSocket.closeSocket(); return; } } } } listener.onHostsFailure(); clientSocket.closeSocket(); } } @Override public void onSocketError() { listener.onHostsFailure(); } }); HashMap<String, String> headers = new HashMap<String, String>(); headers.put("Accept", "*/*"); headers.put("Host", "gestionbbox.lan"); headers.put("Cookie", token_header); HttpFrame frameRequest = new HttpFrame(HttpMethod.GET_REQUEST, new HttpVersion(1, 1), headers, "/api/v1/hosts", new ListOfBytes("")); clientSocket.write(frameRequest.toString().getBytes()); return false; }
From source file:fr.bmartel.bboxapi.BboxApi.java
/** * /* w w w. j a va2s . c o m*/ * Retrieve summary api result * * @param listener * listener for request result / failure * * @return true if request has been successfully initiated */ @Override public boolean getSummary(final IApiSummaryListener listener) { ClientSocket clientSocket = new ClientSocket("gestionbbox.lan", 80); clientSocket.addClientSocketEventListener(new IHttpClientListener() { @Override public void onIncomingHttpFrame(HttpFrame frame, HttpStates httpStates, IClientSocket clientSocket) { if (httpStates == HttpStates.HTTP_FRAME_OK && frame.isHttpResponseFrame()) { // this is data coming from the server if (frame.getStatusCode() == 200) { String data = new String(frame.getBody().getBytes()); JSONArray obj = (JSONArray) JSONValue.parse(data); if (obj.size() > 0) { JSONObject item = (JSONObject) obj.get(0); if (item.containsKey("authenticated") && item.containsKey("display") && item.containsKey("internet") && item.containsKey("voip") && item.containsKey("iptv") && item.containsKey("hosts") && item.containsKey("wan")) { int authenticated = Integer.parseInt(item.get("authenticated").toString()); boolean displayState = false; int luminosity = 0; JSONObject displayObj = (JSONObject) item.get("display"); if (displayObj.containsKey("state") && displayObj.containsKey("luminosity")) { displayState = true; luminosity = Integer.parseInt(displayObj.get("luminosity").toString()); if (luminosity == 0) displayState = false; } int internetState = 0; JSONObject internetObj = (JSONObject) item.get("internet"); if (internetObj.containsKey("state")) { internetState = Integer.parseInt(internetObj.get("state").toString()); } JSONArray voipArr = (JSONArray) item.get("voip"); String voipStatus = ""; CallState callState = CallState.UNKNOWN; int message = 0; int notanswered = 0; if (voipArr.size() > 0) { JSONObject voipObject = (JSONObject) voipArr.get(0); if (voipObject.containsKey("status") && voipObject.containsKey("callstate") && voipObject.containsKey("message") && voipObject.containsKey("notanswered")) { voipStatus = voipObject.get("status").toString(); callState = CallState.getValue(voipObject.get("callstate").toString()); message = Integer.parseInt(voipObject.get("message").toString()); notanswered = Integer.parseInt(voipObject.get("notanswered").toString()); } } JSONArray iptvArr = (JSONArray) item.get("iptv"); String iptvAddr = ""; String iptvIpAddr = ""; int iptvReceipt = 0; int iptvNumber = 0; if (iptvArr.size() > 0) { JSONObject iptvObject = (JSONObject) iptvArr.get(0); if (iptvObject.containsKey("address") && iptvObject.containsKey("ipaddress") && iptvObject.containsKey("receipt") && iptvObject.containsKey("number")) { iptvAddr = iptvObject.get("address").toString(); iptvIpAddr = iptvObject.get("ipaddress").toString(); iptvReceipt = Integer.parseInt(iptvObject.get("receipt").toString()); iptvNumber = Integer.parseInt(iptvObject.get("number").toString()); } } JSONArray hostArr = (JSONArray) item.get("hosts"); List<Host> hostList = new ArrayList<Host>(); for (int i = 0; i < hostArr.size(); i++) { JSONObject hostItem = (JSONObject) hostArr.get(i); if (hostItem.containsKey("hostname") && hostItem.containsKey("ipaddress")) { hostList.add(new Host(0, hostItem.get("hostname").toString(), "", hostItem.get("ipaddress").toString(), "", "", "", "", "", 0, false)); } } JSONObject wanObject = (JSONObject) item.get("wan"); int rxOccupation = 0; int txOccupation = 0; if (wanObject.containsKey("ip")) { JSONObject ipObject = (JSONObject) wanObject.get("ip"); if (ipObject.containsKey("stats")) { JSONObject statsObject = (JSONObject) ipObject.get("stats"); if (statsObject.containsKey("rx") && statsObject.containsKey("tx")) { JSONObject rxObject = (JSONObject) statsObject.get("rx"); JSONObject txObject = (JSONObject) statsObject.get("tx"); if (rxObject.containsKey("occupation") && txObject.containsKey("occupation")) { rxOccupation = Integer .parseInt(rxObject.get("occupation").toString()); txOccupation = Integer .parseInt(txObject.get("occupation").toString()); } } } } ApiSummary summary = new ApiSummary(rxOccupation, txOccupation, hostList, iptvAddr, iptvIpAddr, iptvReceipt, iptvNumber, voipStatus, callState, message, notanswered, internetState, authenticated, displayState); listener.onApiSummaryReceived(summary); clientSocket.closeSocket(); return; } } } listener.onApiSummaryFailure(); clientSocket.closeSocket(); } } @Override public void onSocketError() { listener.onApiSummaryFailure(); } }); HashMap<String, String> headers = new HashMap<String, String>(); headers.put("Accept", "*/*"); headers.put("Host", "gestionbbox.lan"); headers.put("Cookie", token_header); HttpFrame frameRequest = new HttpFrame(HttpMethod.GET_REQUEST, new HttpVersion(1, 1), headers, "/api/v1/summary", new ListOfBytes("")); clientSocket.write(frameRequest.toString().getBytes()); return false; }