List of usage examples for com.google.gson JsonObject addProperty
public void addProperty(String property, Character value)
From source file:at.tugraz.kmi.medokyservice.fca.util.ImportExport.java
License:Open Source License
private JsonElement domains2JSON() { BlockingDeque<Domain> domains = Database.getInstance().getAll(Domain.class); JsonArray jsD = new JsonArray(); for (Domain d : domains) { IncidenceMatrix mat = d.getMapping(); Map<FCAObject, Set<FCAAttribute>> mapping = mat.getObjects(); JsonObject jso = prepare(d);/*from w ww . j av a 2s . c om*/ JsonObject jsM = new JsonObject(); for (long mId : mat.getItemMetadata().keySet()) { jsM.addProperty(Long.toString(mId), mat.getItemMetadata().get(mId).getId()); } jso.add(SECTION_M, jsM); List<Long> ownerIDs = new LinkedList<Long>(); for (User o : d.getOwners()) { ownerIDs.add(o.getId()); } jso.add(OWNER, gson.toJsonTree(ownerIDs, new TypeToken<List<Long>>() { }.getType())); jso.addProperty(GLOBAL, d.isGlobal()); jso.addProperty(APPROVED, d.isApproved()); JsonObject jsMapping = new JsonObject(); for (FCAObject o : mapping.keySet()) { LinkedList<Long> aIds = new LinkedList<Long>(); for (FCAAttribute a : mapping.get(o)) { aIds.add(a.getId()); } jsMapping.add(Long.toString(o.getId()), gson.toJsonTree(aIds, new TypeToken<List<Long>>() { }.getType())); } jso.add(MAPPING, jsMapping); jsD.add(jso); } return jsD; }
From source file:at.tugraz.kmi.medokyservice.fca.util.ImportExport.java
License:Open Source License
private JsonElement courses2JSON() { BlockingDeque<Course> courses = Database.getInstance().getAll(Course.class); JsonArray jsC = new JsonArray(); for (Course o : courses) { JsonObject jso = prepare(o); Set<Domain> domains = o.getDomains(); LinkedList<Long> dIds = new LinkedList<Long>(); for (Domain d : domains) dIds.add(d.getId());/*www. j ava 2 s. c o m*/ jso.add(SECTION_D, gson.toJsonTree(dIds, new TypeToken<List<Long>>() { }.getType())); jso.addProperty(E_CID, o.getExternalCourseID()); List<Long> participants = new LinkedList<Long>(); for (User u : o.getParticipants()) participants.add(u.getId()); jso.add(PARTICIPANTS, gson.toJsonTree(participants, new TypeToken<List<Long>>() { }.getType())); jsC.add(jso); } return jsC; }
From source file:at.yawk.votifier.VotifierKeyPair.java
License:Mozilla Public License
public JsonObject toJson(JsonObject target) { target.addProperty("public_key", getPubEncoded()); target.addProperty("private_key", getPrivEncoded()); return target; }
From source file:au.edu.apsr.pids.to.Handle.java
License:Apache License
/** * Create a Handle object object using the provided Identifier and * handle values// w w w .ja va2s . c o m * * @return Handle * the Handle object * @param identifier * the agent Identifier object * @param hv * an array of HandleValue objects to be added to the handle * @throws DAOException * @throws HandleException * @throws IOException */ public static Handle create(Identifier identifier, HandleValue[] hv) throws DAOException, HandleException, IOException { Handle handleObject = new Handle(); HandleConfig hc = HandleConfig.getHandleConfig(); handleObject.setHandle(hc.getPrefix() + '/' + Handle.getNextSuffix()); AdminRecord admin = handleObject.createAdminRecord(Constants.NA_HANDLE_PREFIX + hc.getPrefix(), Constants.ADMIN_GROUP_IDX); HandleValue values[] = new HandleValue[hv.length + 2]; // load the passed values into the value array int i; for (i = 0; i < hv.length; i++) { values[i] = hv[i]; } // add the admin values values[i] = new HandleValue(); values[i].setIndex(Constants.ADMIN_IDX); values[i].setType(Common.STD_TYPE_HSADMIN); values[i].setData(Encoder.encodeAdminRecord(admin)); values[i].setTTL(Constants.DEFAULT_TTL); values[i + 1] = new HandleValue(); values[i + 1].setIndex(Constants.AGENT_IDX); values[i + 1].setType(Constants.XT_AGENTID); values[i + 1].setData(Util.encodeString(identifier.getHandle())); values[i + 1].setTTL(Constants.DEFAULT_TTL); AbstractResponse response = handleObject.createHandle(values); if (response.responseCode == AbstractMessage.RC_SUCCESS) { JsonObject obj = new JsonObject(); obj.addProperty("status", "success"); obj.addProperty("handle", handleObject.getHandle()); obj.addProperty("identifier", identifier.getIdentifier()); obj.addProperty("authDomain", identifier.getAuthDomain()); obj.addProperty("appId", identifier.getAppid()); String jsonStr = new Gson().toJson(obj); log.info(jsonStr); return handleObject; } else { log.info("Failed to create handle: " + response); return null; } }
From source file:au.edu.apsr.pids.to.Handle.java
License:Apache License
/** * Create an agent admin Handle object object using the provided identifier * string and authentication domain string * //w w w .j a v a 2s . c o m * @return Handle * the Handle object * @param identifier * the agent identifier string * @param authDomain * the agent authentication domain string * @throws DAOException * @throws HandleException */ public static Handle createAdmin(String identifier, String authDomain, String appId) throws DAOException, HandleException { Handle handleObject = new Handle(); HandleConfig hc = HandleConfig.getHandleConfig(); handleObject.setHandle(hc.getPrefix() + '/' + Handle.getNextSuffix()); AdminRecord admin = handleObject.createAdminRecord(Constants.NA_HANDLE_PREFIX + hc.getPrefix(), Constants.ADMIN_GROUP_IDX); HandleValue values[] = new HandleValue[4]; values[0] = new HandleValue(); values[0].setIndex(Constants.AGENT_DESC_IDX); values[0].setType(Constants.XT_TYPE_DESC); values[0].setAnyoneCanRead(false); values[0].setData(Util.encodeString(identifier + Identifier.separator + authDomain)); values[0].setTTL(Constants.DEFAULT_TTL); values[1] = new HandleValue(); values[1].setIndex(Constants.ADMIN_IDX); values[1].setType(Common.STD_TYPE_HSADMIN); values[1].setData(Encoder.encodeAdminRecord(admin)); values[1].setTTL(Constants.DEFAULT_TTL); values[2] = new HandleValue(); values[2].setIndex(Constants.AGENT_IDX); values[2].setType(Constants.XT_AGENTID); values[2].setData(Util.encodeString(handleObject.getHandle())); values[2].setTTL(Constants.DEFAULT_TTL); values[3] = new HandleValue(); values[3].setIndex(Constants.AGENT_DESC_APPIDX); values[3].setType(Constants.XT_APPID); values[3].setAnyoneCanRead(false); values[3].setData(Util.encodeString(appId)); values[3].setTTL(Constants.DEFAULT_TTL); AbstractResponse response = handleObject.createHandle(values); if (response.responseCode == AbstractMessage.RC_SUCCESS) { JsonObject obj = new JsonObject(); obj.addProperty("status", "success"); obj.addProperty("handle", handleObject.getHandle()); obj.addProperty("identifier", identifier); obj.addProperty("authDomain", authDomain); obj.addProperty("appId", appId); String jsonStr = new Gson().toJson(obj); log.info(jsonStr); log.info("Successfully created admin handle: " + handleObject.getHandle()); return handleObject; } else { log.info("Failed to create admin handle: " + response); return null; } }
From source file:au.edu.ausstage.tweetgatherer.MessageProcessor.java
License:Open Source License
/** * A method that is run in the thread/*from w ww. j a v a2s. c om*/ * Takes a tweet from the queue and processes it */ public void run() { STweet tweet = null; try { // infinite loop to keep the thread running while (true) { // take a tweet from the queue tweet = newTweets.take(); // get the tweet id number String tweetId = Long.toString(tweet.getStatusId()); // get a hash of the tweet id String tweetIdHash = HashUtils.hashValue(tweetId); // get the user STweetUser user = tweet.getUser(); // get the user id String userId = Long.toString(user.getUserId()); // get a hash of the user id String userIdHash = HashUtils.hashValue(userId); // get the JSON value of the tweet JsonObject jsonTweetObject = sanitiseMessage(tweet.getJSON(), tweetIdHash); // get the JSON value of the user JsonObject jsonUserObject = sanitiseUser(user.getJSON(), userIdHash); // replace the user with the sanitised version jsonTweetObject.remove("user"); jsonTweetObject.addProperty("user", ""); /* * Write the file */ if (FileUtils.writeNewFile(logFiles + "/" + tweetIdHash, jsonTweetObject.toString() + "\n" + jsonUserObject.toString()) == false) { System.err.println("ERROR: Unable to write file to the specified log file"); System.err.println(" twitter message with id '" + tweetId + "' is now lost"); } else { System.out.println("INFO: New Message written to the log directory:"); System.out.println(" " + tweetIdHash); } /* * get the hash tags from this message */ // get the list of hash tags from the tweet JsonElement elem = jsonTweetObject.get("text"); List<String> hashtags = extractHashTags.extractHashtags(elem.getAsString()); // convert all of the found hash tags to lower case ListIterator<String> hashtagsIterator = hashtags.listIterator(); while (hashtagsIterator.hasNext() == true) { // get the next tage in the list String tmp = (String) hashtagsIterator.next(); tmp = tmp.toLowerCase(); // replace the value we retrieved with this updated one hashtagsIterator.set(tmp); } // see if this is a company performance if (isCompanyPerformance(hashtags, jsonTweetObject, jsonUserObject, tweetIdHash) == false) { // this isn't a company performance // is it using a performance specific id? if (isPerformance(hashtags, jsonTweetObject, jsonUserObject, tweetIdHash) == false) { //this isn't a valid performance System.err.println("ERROR: Unable to find a matching performance for this message"); // send an exception report if (emailManager.sendMessageWithAttachment(EMAIL_SUBJECT, EMAIL_MESSAGE, logFiles + "/" + tweetIdHash) == false) { System.err.println("ERROR: Unable to send the exception report"); } } } } } catch (InterruptedException ex) { // thread has been interrupted System.out.println("INFO: The Message processing thread has stopped"); } }
From source file:au.edu.ausstage.tweetgatherer.MessageProcessor.java
License:Open Source License
/** * A private method to sanitise the message * * @param jsonTweetObject the original message object * @param idHash the hash of the message id * * @return the sanitised message object *///from ww w.j a va 2 s. c o m private JsonObject sanitiseMessage(JsonObject jsonTweetObject, String idHash) { // remove the id and replace it with the hash jsonTweetObject.remove("id"); jsonTweetObject.addProperty("id", idHash); // replace the "in_reply_to_user_id" field with a hash if present if (jsonTweetObject.has("in_reply_to_user_id") == true) { // get a hash of the value JsonElement elem = jsonTweetObject.get("in_reply_to_user_id"); if (elem.isJsonNull() == false) { String hash = HashUtils.hashValue(elem.getAsString()); // update the value jsonTweetObject.remove("in_reply_to_user_id"); jsonTweetObject.addProperty("in_reply_to_user_id", hash); } } // replace the "in_reply_to_status_id" field with a hash if present if (jsonTweetObject.has("in_reply_to_status_id") == true) { // get a hash of the value JsonElement elem = jsonTweetObject.get("in_reply_to_status_id"); if (elem.isJsonNull() == false) { String hash = HashUtils.hashValue(elem.getAsString()); // update the value jsonTweetObject.remove("in_reply_to_status_id"); jsonTweetObject.addProperty("in_reply_to_status_id", hash); } } // replace the "retweeted_status" field with a hash if present if (jsonTweetObject.has("retweeted_status") == true) { // get a hash of the value JsonElement elem = jsonTweetObject.get("retweeted_status"); if (elem.isJsonNull() == false) { String hash = HashUtils.hashValue(elem.getAsString()); // update the value jsonTweetObject.remove("retweeted_status"); jsonTweetObject.addProperty("retweeted_status", hash); } } // return the sanitised object return jsonTweetObject; }
From source file:au.edu.ausstage.tweetgatherer.MessageProcessor.java
License:Open Source License
/** * A private method to sanitise the message * * @param jsonTweetObject the original user object * @param idHash the hash of the user id * * @return the sanitised user object *//*w ww . j a va2 s . c o m*/ private JsonObject sanitiseUser(JsonObject jsonTweetObject, String idHash) { // remove the id and replace it with the hash jsonTweetObject.remove("id"); jsonTweetObject.addProperty("id", idHash); // loop through the list of sanitised fields for (int i = 0; i < SANITISED_FIELDS.length; i++) { if (jsonTweetObject.has(SANITISED_FIELDS[i]) == true) { jsonTweetObject.remove(SANITISED_FIELDS[i]); jsonTweetObject.addProperty(SANITISED_FIELDS[i], ""); } } // return the sanitised object return jsonTweetObject; }
From source file:au.edu.unsw.cse.soc.federatedcloud.CloudResourceBaseDeploymentEngine.java
License:Open Source License
@Path("/deploy") @POST//from w ww .j a va 2 s . c o m @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) public Response deployCloudResourceDescription(@QueryParam("description_id") String description_id, @QueryParam("description_json") @DefaultValue("{}") String description) { log.info("Deployment Request Received for id: " + description_id); try { log.info("Input Resource description: " + URLDecoder.decode(description, "UTF-8")); } catch (UnsupportedEncodingException e) { log.error(e.getMessage(), e); } try { CloudResourceDescription desc = DataModelUtil.buildCouldResourceDescriptionFromJSON(description_id); CloudResourceBaseDeploymentEngine engine = new CloudResourceBaseDeploymentEngine(); String response = engine.deployCloudResourceDescription(desc); log.info("Deployed Resource:" + desc.toString()); JsonObject json = new JsonObject(); json.addProperty("result", response); return Response.status(Response.Status.OK).entity(json.toString()).build(); } catch (Exception e) { log.error(e.getMessage(), e); return Response.status(Response.Status.OK).entity(e.getMessage()).build(); } }
From source file:au.edu.unsw.cse.soc.federatedcloud.cloudRessourceBase.RecommenderAPI.java
License:Open Source License
private JsonObject returnDummyObject() { JsonObject json = new JsonObject(); JsonArray descriptionsJsonArray = new JsonArray(); JsonObject desciption1 = new JsonObject(); desciption1.addProperty("Id", "1"); desciption1.addProperty("Name", "SE-Bucket"); desciption1.addProperty("TargetEnv", "IaaS"); desciption1.addProperty("Deployer", "au.edu.unsw.cse.soc.federatedcloud.deployers.aws.AWSS3Deployer"); descriptionsJsonArray.add(desciption1); JsonObject desciption2 = new JsonObject(); desciption2.addProperty("Id", "2"); desciption2.addProperty("Name", "Rackspace-VM"); desciption2.addProperty("TargetEnv", "IaaS"); desciption2.addProperty("Deployer", "au.edu.unsw.cse.soc.federatedcloud.deployers.rackspace.RackspaceDeployer"); descriptionsJsonArray.add(desciption2); JsonObject desciption3 = new JsonObject(); desciption3.addProperty("Id", "3"); desciption3.addProperty("Name", "GoogleDrive"); desciption3.addProperty("TargetEnv", "SaaS"); desciption3.addProperty("Deployer", "au.edu.unsw.cse.soc.federatedcloud.deployers.GoogleDriveDeployer"); descriptionsJsonArray.add(desciption3); json.add("CloudResourceDescriptions", descriptionsJsonArray); return json;// w w w . j av a2s. c o m }