List of usage examples for org.json.simple JSONObject toJSONString
public String toJSONString()
From source file:com.aerothai.database.job.JobResource.java
/** * Retrieves representation of an instance of com.aerothai.JobResource * @return an instance of java.lang.String *//*from w ww . j a v a 2 s . c o m*/ @GET @Produces("application/json") public String listJobAt(@PathParam("id") int id) { String response = null; System.out.println("List Job At ID"); try { JSONObject jobData = null; JobService jobService = new JobService(); jobData = jobService.GetJobAt(id); response = jobData.toJSONString(); } catch (Exception e) { System.out.println("error"); } return response; }
From source file:com.grantedbyme.example.ServletCallback.java
public void doProcess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("application/json; charset=UTF-8"); // setup default response HashMap<String, Object> resultHashMap = new HashMap<>(); resultHashMap.put("success", false); resultHashMap.put("error", 0); JSONObject result = null;//from w w w . j a v a 2s .co m // get request parameters String reqSignature = request.getParameter("signature"); String reqPayload = request.getParameter("payload"); String reqMessage = request.getParameter("message"); // process request if (reqSignature != null && reqPayload != null) { // read private key String privateKey = null; InputStream privateKeyInputStream = getClass().getResourceAsStream("/private_key.pem"); try { privateKey = IOUtils.toString(privateKeyInputStream); } finally { privateKeyInputStream.close(); } // read server key String serverKey = null; InputStream serverKeyInputStream = getClass().getResourceAsStream("/server_key.pem"); try { serverKey = IOUtils.toString(serverKeyInputStream); } finally { serverKeyInputStream.close(); } // _log(serverKey); // initialize BouncyCastle security provider Security.insertProviderAt(new org.bouncycastle.jce.provider.BouncyCastleProvider(), 0); // decrypt request HashMap<String, Object> params = new HashMap<>(); params.put("signature", reqSignature); params.put("payload", reqPayload); params.put("message", reqMessage); /*System.out.println("Signature: " + reqSignature); System.out.println("Payload: " + reqPayload); System.out.println("Message: " + reqMessage);*/ JSONObject requestJSON = CryptoUtil.decryptAndVerify(new JSONObject(params), serverKey, privateKey); if (requestJSON != null) { _log(requestJSON.toJSONString()); if (requestJSON.containsKey("operation")) { String operation = (String) requestJSON.get("operation"); if (operation.equals("ping")) { resultHashMap.put("success", true); } else if (operation.equals("unlink_account")) { if (requestJSON.containsKey("authenticator_secret_hash")) { // TODO: implement } resultHashMap.put("success", false); } else if (operation.equals("rekey_account")) { if (requestJSON.containsKey("authenticator_secret_hash")) { // TODO: implement } resultHashMap.put("success", false); } else if (operation.equals("revoke_challenge")) { if (requestJSON.containsKey("challenge")) { // TODO: implement } resultHashMap.put("success", false); } else { // operation not handled } } } result = CryptoUtil.encryptAndSign(new JSONObject(resultHashMap), serverKey, privateKey); } // write out response PrintWriter out = response.getWriter(); if (result != null) { out.print(result.toJSONString()); } else { out.print(new JSONObject(resultHashMap).toJSONString()); } out.close(); }
From source file:org.kitodo.data.elasticsearch.index.type.BatchType.java
@SuppressWarnings("unchecked") @Override//from www . ja va 2 s .c om public HttpEntity createDocument(Batch batch) { JSONObject batchObject = new JSONObject(); batchObject.put("title", batch.getTitle()); String type = batch.getType() != null ? batch.getType().toString() : null; batchObject.put("type", type); batchObject.put("processes", addObjectRelation(batch.getProcesses())); return new NStringEntity(batchObject.toJSONString(), ContentType.APPLICATION_JSON); }
From source file:cloud.google.com.windows.example.ExampleCode.java
private void replaceMetadata(Metadata input, JSONObject newMetadataItem) { // Transform the JSON object into a string that the API can use. String newItemString = newMetadataItem.toJSONString(); // Get the list containing all of the Metadata entries for this instance. List<Items> items = input.getItems(); // If the instance has no metadata, items can be returned as null. if (items == null) { items = new LinkedList<Items>(); input.setItems(items);/* w ww. ja v a 2 s.co m*/ } // Find the "windows-keys" entry and update it. for (Items item : items) { if (item.getKey().compareTo("windows-keys") == 0) { // Replace item's value with the new entry. // To prevent race conditions, production code may want to maintain a // list where the oldest entries are removed once the 32KB limit is // reached for the metadata entry. item.setValue(newItemString); return; } } // "windows.keys" entry doesn't exist in the metadata - append it. // This occurs when running password-reset for the first time on an instance. items.add(new Items().setKey("windows-keys").setValue(newItemString)); }
From source file:org.opencastproject.adminui.endpoint.TasksEndpointTest.java
@Test public void testCreateTask() throws ParseException, IOException { InputStream stream = TasksEndpointTest.class.getResourceAsStream("/taskNew.json"); InputStreamReader reader = new InputStreamReader(stream); JSONObject metadata = (JSONObject) new JSONParser().parse(reader); given().formParam("metadata", "empty").expect().log().all().statusCode(HttpStatus.SC_BAD_REQUEST).when() .post(rt.host("/new")); String result = given().formParam("metadata", metadata.toJSONString()).expect().log().all() .statusCode(HttpStatus.SC_CREATED).when().post(rt.host("/new")).asString(); Assert.assertEquals("5,10", result); }
From source file:com.aerothai.database.user.UserResource.java
/** * Retrieves representation of an instance of com.aerothai.UserResource * @return an instance of java.lang.String *///from ww w .ja v a2 s .c o m @GET @Produces("application/json") public String listUserAt(@PathParam("id") int id) { String response = null; System.out.println("List User At ID :" + id); try { JSONObject userData = null; UserService userService = new UserService(); userData = userService.GetUserAt(id); response = userData.toJSONString(); } catch (Exception e) { System.out.println("error"); } return response; }
From source file:com.fujitsu.dc.test.utils.UserDataUtils.java
/** * OData??????.//from ww w . j a v a 2 s . co m * @param token (???) * @param code ?? * @param body * @param cell ?? * @param box ?? * @param collection ?? * @param entityType * @param id OData?UUID * @param ifMatch If-Match * @return ? */ public static TResponse mergeAnyAuthSchema(String token, int code, JSONObject body, String cell, String box, String collection, String entityType, String id, String ifMatch) { TResponse res = Http.request("box/odatacol/merge-anyAuthSchema.txt").with("cell", cell).with("box", box) .with("collection", collection).with("entityType", entityType).with("id", id) .with("accept", MediaType.APPLICATION_JSON).with("contentType", MediaType.APPLICATION_JSON) .with("token", token).with("ifMatch", ifMatch).with("body", body.toJSONString()).returns() .statusCode(code).debug(); return res; }
From source file:com.netflix.raigad.resources.ElasticsearchAdmin.java
@GET @Path("/shard_allocation_enable/{type}") public Response esShardAllocationEnable(@PathParam("type") String type) throws IOException, InterruptedException, JSONException { logger.info("Enabling Shard Allocation through REST call ..."); if (!type.equalsIgnoreCase("transient") && !type.equalsIgnoreCase("persistent")) throw new IOException("Parameter must be equal to transient or persistent"); //URL//from ww w . j ava 2 s . c om String url = "http://127.0.0.1:" + config.getHttpPort() + "/_cluster/settings"; JSONObject settings = new JSONObject(); JSONObject property = new JSONObject(); property.put(SHARD_REALLOCATION_PROPERTY, "all"); settings.put(type, property); String RESPONSE = SystemUtils.runHttpPutCommand(url, settings.toJSONString()); return Response.ok(RESPONSE, MediaType.APPLICATION_JSON).build(); }
From source file:at.uni_salzburg.cs.ckgroup.cscpp.engine.parser.ComputationTask.java
@SuppressWarnings("unchecked") @Override//from w ww. j ava 2 s .c o m public String toJSONString() { JSONObject obj = new JSONObject(); obj.put("point", getPosition()); obj.put("tolerance", getTolerance()); // TODO arrival time, delay, and lifetime in JSON string? return obj.toJSONString(); }
From source file:com.fujitsu.dc.test.utils.UserDataUtils.java
/** * OData??????./*from ww w. jav a 2 s. c o m*/ * @param token (???) * @param code ?? * @param body * @param cell ?? * @param box ?? * @param collection ?? * @param entityType * @param id OData?UUID * @param ifMatch If-Match * @return ? */ public static TResponse updateAnyAuthSchema(String token, int code, JSONObject body, String cell, String box, String collection, String entityType, String id, String ifMatch) { TResponse res = Http.request("box/odatacol/update-anyAuthSchema.txt").with("cell", cell).with("box", box) .with("collection", collection).with("entityType", entityType).with("id", id) .with("accept", MediaType.APPLICATION_JSON).with("contentType", MediaType.APPLICATION_JSON) .with("token", token).with("ifMatch", ifMatch).with("body", body.toJSONString()).returns() .statusCode(code).debug(); return res; }