List of usage examples for org.json JSONObject put
public JSONObject put(String key, Object value) throws JSONException
From source file:cz.karry.vpnc.LunaService.java
/** * sudo ip route flush 192.168.100.0/24 via 192.168.100.1 * sudo ip route flush default via 192.168.100.1 *///from w w w .j a va 2s. c om @LunaServiceThread.PublicMethod public void delRoute(ServiceMessage msg) throws JSONException, LSException { if ((!msg.getJSONPayload().has("network")) || (!msg.getJSONPayload().has("gateway"))) { msg.respondError("1", "Improperly formatted request."); return; } String network = msg.getJSONPayload().getString("network").toLowerCase(); String gateway = msg.getJSONPayload().getString("gateway").toLowerCase(); if (!gateway.matches(GATEWAY_REGEXP)) { msg.respondError("2", "Bad gateway format."); return; } if (!network.matches(NETWOK_REGEXP)) { msg.respondError("3", "Bad network format."); return; } String cmdStr = String.format("ip route flush %s via %s", network, gateway); CommandLine cmd = new CommandLine(cmdStr); if (!cmd.doCmd()) { msg.respondError("4", cmd.getResponse()); return; } JSONObject reply = new JSONObject(); reply.put("command", cmdStr); msg.respond(reply.toString()); }
From source file:cz.karry.vpnc.LunaService.java
@LunaServiceThread.PublicMethod public void connectionInfo(final ServiceMessage msg) throws JSONException, LSException { JSONObject jsonObj = msg.getJSONPayload(); if (!jsonObj.has("name")) { msg.respondError("1", "Improperly formatted request."); return;//from w ww. j a v a 2s. c o m } String name = jsonObj.getString("name"); JSONObject reply = new JSONObject(); reply.put("name", name); VpnConnection conn = vpnConnections.get(name); ConnectionState state = VpnConnection.ConnectionState.INACTIVE; String log = ""; if (conn != null) { state = conn.getConnectionState(); log = conn.getLog(); if (state == AbstractVpnConnection.ConnectionState.CONNECTED) { reply.put("localAddress", conn.getLocalAddress()); } } try { reply.put("profileName", name); reply.put("state", state); reply.put("log", log); //tcpLogger.log("refresh info: "+reply.toString()); msg.respond(reply.toString()); } catch (LSException ex) { tcpLogger.log(ex.getMessage(), ex); } catch (JSONException ex) { tcpLogger.log(ex.getMessage(), ex); } }
From source file:cz.karry.vpnc.LunaService.java
@LunaServiceThread.PublicMethod public void getRegisteredConnections(final ServiceMessage msg) throws JSONException, LSException { JSONObject reply = new JSONObject(); try {/*from www . ja v a 2 s. c om*/ reply.put("connections", vpnConnections.keySet()); msg.respond(reply.toString()); } catch (LSException ex) { tcpLogger.log(ex.getMessage(), ex); } catch (JSONException ex) { tcpLogger.log(ex.getMessage(), ex); } }
From source file:org.b3log.latke.event.EventManagerTestCase.java
/** * * @throws Exception exception//from w w w .j a va2 s . com */ @Test public void test() throws Exception { final EventManager eventManager = EventManager.getInstance(); final TestEventListener1 testEventListener1 = new TestEventListener1(); eventManager.registerListener(testEventListener1); final TestEventListener2 testEventListener2 = new TestEventListener2(); eventManager.registerListener(testEventListener2); final TestEventAsyncListener1 testEventAsyncListener1 = new TestEventAsyncListener1(); eventManager.registerListener(testEventAsyncListener1); final JSONObject eventData = new JSONObject(); eventData.put("prop1", 1); eventManager.fireEventSynchronously(new Event<JSONObject>("Test sync listener1", eventData)); eventManager.fireEventSynchronously(new Event<JSONObject>("Test sync listener2", eventData)); eventManager.<String>fireEventAsynchronously(new Event<JSONObject>("Test async listener1", eventData)); System.out.println("Doing somthing in main thread...."); final long sleepTime = 101; final long loopCnt = 40; try { for (int i = 0; i < loopCnt; i++) { System.out.println("In main thread: " + i); Thread.sleep(sleepTime); } } catch (final InterruptedException e) { throw new EventException(e); } System.out.println("Done in main thread"); }
From source file:com.swiftnav.sbp.tracking.MsgTrackingIq.java
@Override public JSONObject toJSON() { JSONObject obj = super.toJSON(); obj.put("channel", channel); obj.put("sid", sid.toJSON()); obj.put("corrs", SBPStruct.toJSONArray(corrs)); return obj;//from ww w .j a v a 2 s . co m }
From source file:com.google.walkaround.wave.server.attachment.AttachmentMetadataHandler.java
private void doRequest(HttpServletRequest req, HttpServletResponse resp) throws IOException { Map<AttachmentId, Optional<AttachmentMetadata>> result = attachments.getMetadata(getIds(req), MAX_REQUEST_TIME_MS);//from w w w . ja v a 2 s.c o m JSONObject json = new JSONObject(); try { for (Entry<AttachmentId, Optional<AttachmentMetadata>> entry : result.entrySet()) { JSONObject metadata = new JSONObject( entry.getValue().isPresent() ? entry.getValue().get().getMetadataJsonString() : INVALID_ATTACHMENT_ID_METADATA_STRING); String queryParams = "attachment=" + UriEscapers.uriQueryStringEscaper(false).escape(entry.getKey().getId()); metadata.put("url", "/download?" + queryParams); metadata.put("thumbnailUrl", "/thumbnail?" + queryParams); json.put(entry.getKey().getId(), metadata); } } catch (JSONException e) { throw new Error(e); } ServletUtil.writeJsonResult(resp.getWriter(), json.toString()); }
From source file:com.graphhopper.http.InvalidRequestServlet.java
@Override protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setStatus(HttpServletResponse.SC_NOT_FOUND); res.setContentType("text/plain"); res.setContentType("UTF-8"); JSONObject json = new JSONObject(); json.put("error_code", "404"); writeJson(req, res, json);//from w w w . j a v a 2 s .c o m }
From source file:edu.asu.msse.gnayak2.main.CollectionSkeleton.java
public String callMethod(String request) { JSONObject result = new JSONObject(); try {//from w w w . ja v a 2 s .c o m JSONObject theCall = new JSONObject(request); System.out.println(request); String method = theCall.getString("method"); int id = theCall.getInt("id"); JSONArray params = null; if (!theCall.isNull("params")) { params = theCall.getJSONArray("params"); System.out.println(params); } result.put("id", id); result.put("jsonrpc", "2.0"); if (method.equals("resetFromJsonFile")) { mLib.resetFromJsonFile(); result.put("result", true); System.out.println("resetFromJsonCalled"); } else if (method.equals("remove")) { String sName = params.getString(0); boolean removed = mLib.remove(sName); System.out.println(sName + " deleted"); result.put("result", removed); } else if (method.equals("add")) { MovieImpl movie = new MovieImpl(params.getString(0)); boolean added = mLib.add(movie); result.put("result", added); } else if (method.equals("get")) { String sName = params.getString(0); MovieImpl movie = mLib.get(sName); result.put("result", movie.toJson()); } else if (method.equals("getNames")) { String[] names = mLib.getNames(); JSONArray resArr = new JSONArray(); for (int i = 0; i < names.length; i++) { resArr.put(names[i]); } result.put("result", resArr); } else if (method.equals("saveToJsonFile")) { boolean saved = mLib.saveToJsonFile(); result.put("result", saved); } else if (method.equals("getModelInformation")) { //mLib.resetFromJsonFile(); result.put("result", mLib.getModelInformation()); } else if (method.equals("update")) { String movieJSONString = params.getString(0); Movie mo = new MovieImpl(movieJSONString); mLib.updateMovie(mo); } else if (method.equals("deleteAndAdd")) { String oldMovieJSONString = params.getString(0); String editedMovieJSONString = params.getString(1); boolean deletionSuccessful = false; boolean additionSuccessful = false; MovieImpl oldMovie = new MovieImpl(oldMovieJSONString); MovieImpl newMovie = new MovieImpl(editedMovieJSONString); deletionSuccessful = mLib.deleteMovie(oldMovie); additionSuccessful = mLib.add(newMovie); result.put("result", deletionSuccessful & additionSuccessful); } } catch (Exception ex) { System.out.println("exception in callMethod: " + ex.getMessage()); } System.out.println("returning: " + result.toString()); return "HTTP/1.0 200 Data follows\nServer:localhost:8080\nContent-Type:text/plain\nContent-Length:" + (result.toString()).length() + "\n\n" + result.toString(); }
From source file:edu.stanford.mobisocial.dungbeetle.feed.objects.AppObj.java
public static DbObject fromPickerResult(Context context, String action, ResolveInfo resolveInfo, Intent pickerResult) {/*from ww w.ja v a 2s . c o m*/ long[] contactIds = pickerResult.getLongArrayExtra("contacts"); String pkgName = resolveInfo.activityInfo.packageName; String className = resolveInfo.activityInfo.name; /** * TODO: * * Identity Firewall Goes Here. * Membership details can be randomized in one of many ways. * The app (scrabble) may see games a set of gamers play together. * The app may always see random ids * The app may always see stable ids * * Can also permute the cursor and member order. */ JSONArray participantIds = new JSONArray(); participantIds.put(App.instance().getLocalPersonId()); for (long id : contactIds) { Maybe<Contact> annoyingContact = Contact.forId(context, id); try { Contact contact = annoyingContact.get(); participantIds.put(contact.personId); } catch (NoValError e) { participantIds.put(Contact.UNKNOWN); } } JSONObject json = new JSONObject(); try { json.put(Multiplayer.OBJ_MEMBERSHIP, (participantIds)); json.put(ANDROID_ACTION, action); json.put(ANDROID_PACKAGE_NAME, pkgName); json.put(ANDROID_CLASS_NAME, className); } catch (JSONException e) { Log.d(TAG, "What? Impossible!", e); } return new DbObject(TYPE, json); }
From source file:edu.stanford.mobisocial.dungbeetle.feed.objects.AppObj.java
public static DbObject forConnectedApp(Context context, ResolveInfo info) { /**/*from ww w .ja v a2 s .co m*/ * TODO: * * Identity Firewall Goes Here. * Membership details can be randomized in one of many ways. * The app (scrabble) may see games a set of gamers play together. * The app may always see random ids * The app may always see stable ids * * Can also permute the cursor and member order. */ JSONObject json = new JSONObject(); try { json.put(ANDROID_ACTION, Multiplayer.ACTION_CONNECTED); json.put(ANDROID_PACKAGE_NAME, info.activityInfo.packageName); json.put(ANDROID_CLASS_NAME, info.activityInfo.name); } catch (JSONException e) { Log.d(TAG, "What? Impossible!", e); } return new DbObject(TYPE, json); }