List of usage examples for com.mongodb.util JSON parse
public static Object parse(final String jsonString)
Parses a JSON string and returns a corresponding Java object.
From source file:eu.operando.core.pdb.mongo.UPPMongo.java
public String storeUPP(UserPrivacyPolicy upp) { String userId = upp.getUserId(); String result = null;/*w w w . j a v a2 s .c om*/ //upp.setUserPolicyID(uppId); try { // find if the upp already added - then reject the post BasicDBObject searchQuery = new BasicDBObject(); searchQuery.put("userId", upp.getUserId()); DBObject dbObj = this.uppTable.findOne(searchQuery); if (dbObj != null) { return null; } ObjectMapper mapper = new ObjectMapper(); String jsonInString = mapper.writeValueAsString(upp); Object obj = JSON.parse(jsonInString); DBObject document = (DBObject) obj; uppTable.insert(document); ObjectId id = (ObjectId) document.get("_id"); System.out.println("stored upp in " + id.toString() + document.get("userId")); result = getUPPById(document.get("userId").toString()); } catch (MongoException e) { e.printStackTrace(); return null; } catch (JsonGenerationException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return userId; }
From source file:eu.operando.core.udb.UserAccountMongo.java
/** * Update the user account information in the object in the collection. * @param userId The id of the user// w ww. java 2s . co m * @param userAcc The updated account information. * @return Whether the object was updated or not. */ public boolean updateUser(String userId, UserAccount userAcc) { boolean result = false; //upp.setUserPolicyID(regId); try { ObjectMapper mapper = new ObjectMapper(); String jsonInString = mapper.writeValueAsString(userAcc); Object obj = JSON.parse(jsonInString); DBObject document = (DBObject) obj; BasicDBObject searchQuery; try { searchQuery = new BasicDBObject(); searchQuery.put("userid", userId); } catch (IllegalArgumentException e) { e.printStackTrace(); return result; } WriteResult wr = userTable.update(searchQuery, document); result = wr.isUpdateOfExisting(); } catch (JsonGenerationException e) { result = false; } catch (JsonMappingException e) { result = false; } catch (IOException e) { result = false; } return result; }
From source file:eu.operando.core.udb.UserAccountMongo.java
/** * Store a new user account in the collection. * @param userAcc The user to add// www . jav a 2 s.c o m * @return Error message */ public String storeUser(UserAccount userAcc) { String result = null; try { ObjectMapper mapper = new ObjectMapper(); String jsonInString = mapper.writeValueAsString(userAcc); Object obj = JSON.parse(jsonInString); DBObject document = (DBObject) obj; result = getUserByFilter("{\"userid\": \"" + document.get("userid") + "\"}"); if (result != null) { return null; } userTable.insert(document); ObjectId id = (ObjectId) document.get("_id"); System.out.println("stored user in " + id.toString() + document.get("userid")); result = getUserById(document.get("userid").toString()); } catch (MongoException e) { result = null; } catch (JsonGenerationException e) { result = null; } catch (JsonMappingException e) { result = null; } catch (IOException e) { result = null; } return result; }
From source file:eu.vital.vitalcep.cep.CEP.java
public Boolean CEPStart(CEPType type, DolceSpecification dolceSpecification, String mqin, String mqout, String confFile, String sources, JSONObject credentials) throws FileNotFoundException, IOException { ConfigReader configReader = ConfigReader.getInstance(); mongoURL = configReader.get(ConfigReader.MONGO_URL); mongoDB = configReader.get(ConfigReader.MONGO_DB); cp = new CepProcess(dolceSpecification.toString(), mqin, mqout, confFile); cp.startCEP();// w w w.jav a2 s. c o m this.PID = cp.PID; CepContainer.putCepProc(cp); this.type = type.toString(); String T = type.toString(); if (cp.PID > 0) { this.fileName = cp.fileName; MongoClient mongo = new MongoClient(new MongoClientURI(mongoURL)); MongoDatabase db = mongo.getDatabase(mongoDB); try { Document doc = new Document(); doc.put("PID", PID); doc.put("mqin", mqin); doc.put("mqout", mqout); doc.put("dolceSpecification", dolceSpecification.toString()); doc.put("dolcefile", cp.cepFolder + "/" + cp.fileName); doc.put("cepType", T); doc.put("clientId", fileName); //doc.put("sensorId", id); doc.put("fileName", fileName); doc.put("cepFolder", cp.cepFolder); Date NOW = new Date(); switch (T) { case "DATA": doc.put("data", sources); break; case "QUERY": doc.put("querys", sources); break; case "CEPICO": case "ALERT": doc.put("lastRequest", getXSDDateTime(NOW)); BasicDBList sourcesCEPICO = (BasicDBList) JSON.parse(sources); doc.put("requests", sourcesCEPICO); doc.put("lastRequest", getXSDDateTime(NOW)); doc.put("username", credentials.getString("username")); doc.put("password", encrypt(credentials.getString("password"))); break; case "CONTINUOUS": BasicDBList sourcesB = (BasicDBList) JSON.parse(sources); BasicDBList propertiesB = (BasicDBList) JSON.parse(dolceSpecification.getEvents().toString()); doc.put("sources", sourcesB); doc.put("properties", propertiesB); doc.put("lastRequest", getXSDDateTime(NOW)); doc.put("username", credentials.getString("username")); doc.put("password", encrypt(credentials.getString("password"))); break; } doc.put("status", "OK"); db.getCollection("cepinstances").insertOne(doc); ObjectId idO = (ObjectId) doc.get("_id"); this.id = idO.toString(); if (id != null && !(T == "DATA" || T == "QUERY")) { Boolean insertIntoCollectorList = insertIntoCollectorList(doc, idO); if (!insertIntoCollectorList) { db.getCollection("cepinstances").updateOne(doc, new Document("$set", new Document("status", "no collector available"))); throw new ServerErrorException(500); } } } catch (JSONException | GeneralSecurityException | UnsupportedEncodingException | ServerErrorException ex) { String a = ""; } finally { if (db != null) db = null; if (mongo != null) { mongo.close(); mongo = null; } } } else { this.fileName = ""; return false; } return true; }
From source file:eu.vital.vitalcep.restApp.alert.Alerts.java
/** * Creates a filter.//www.ja v a2 s . com * * @param cepico * @param req * @return the filter id * @throws java.io.IOException */ @PUT @Path("createalert") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Response createAlert(String cepico, @Context HttpServletRequest req) throws IOException { StringBuilder ck = new StringBuilder(); Security slogin = new Security(); JSONObject credentials = new JSONObject(); Boolean token = slogin.login(req.getHeader("name"), req.getHeader("password"), false, ck); credentials.put("username", req.getHeader("name")); credentials.put("password", req.getHeader("password")); if (!token) { return Response.status(Response.Status.UNAUTHORIZED).build(); } this.cookie = ck.toString(); JSONObject jo = new JSONObject(cepico); if (!jo.has("source")) { return Response.status(Response.Status.BAD_REQUEST).build(); } MongoClient mongo = new MongoClient(new MongoClientURI(mongoURL)); MongoDatabase db = mongo.getDatabase(mongoDB); try { db.getCollection("alerts"); } catch (Exception e) { //System.out.println("Mongo is down"); mongo.close(); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); } finally { if (db != null) db = null; if (mongo != null) { mongo.close(); mongo = null; } } // create an empty query BasicDBObject query = new BasicDBObject(); BasicDBObject fields = new BasicDBObject().append("_id", false); fields.append("dolceSpecification", false); if (jo.has("dolceSpecification")) { //Filter oFilter = new Filter(filter); JSONObject dsjo = jo.getJSONObject("dolceSpecification"); String str = dsjo.toString();//"{\"dolceSpecification\": "+ dsjo.toString()+"}"; try { DolceSpecification ds = new DolceSpecification(str); if (ds instanceof DolceSpecification) { UUID uuid = UUID.randomUUID(); String randomUUIDString = uuid.toString(); String mqin = RandomStringUtils.randomAlphanumeric(8); String mqout = RandomStringUtils.randomAlphanumeric(8); Date NOW = new Date(); JSONArray requestArray; try { requestArray = createAlertRequests(jo.getJSONArray("source"), ds.getEvents(), getXSDDateTime(NOW)); } catch (Exception e) { return Response.status(Response.Status.BAD_REQUEST) .entity("not available getObservation Service for this sensor ").build(); } CEP cepProcess = new CEP(); if (!(cepProcess.CEPStart(CEP.CEPType.ALERT, ds, mqin, mqout, confFile, requestArray.toString(), credentials))) { return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); } String clientName = cepProcess.fileName; if (cepProcess.PID < 1) { return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); } DBObject dbObject = createAlertSensor(cepico, randomUUIDString, dsjo, cepProcess.id); Document doc = new Document(dbObject.toMap()); try { db.getCollection("alerts").insertOne(doc); JSONObject opState = createOperationalStateObservation(randomUUIDString); String sensorId = host + "/sensor/" + randomUUIDString; MessageProcessor_publisher Publisher_MsgProcc = new MessageProcessor_publisher(this.dmsURL, cookie, sensorId, "alertsobservations", mongoURL, mongoDB);//555 MQTT_connector_subscriper publisher = new MQTT_connector_subscriper(mqout, Publisher_MsgProcc); MqttConnectorContainer.addConnector(publisher.getClientName(), publisher); DBObject oPut = (DBObject) JSON.parse(opState.toString()); Document doc1 = new Document(oPut.toMap()); try { db.getCollection("alertsobservations").insertOne(doc1); String id = doc1.get("_id").toString(); } catch (MongoException ex) { return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); } JSONObject aOutput = new JSONObject(); aOutput.put("id", host + "/sensor/" + randomUUIDString); return Response.status(Response.Status.OK).entity(aOutput.toString()).build(); } catch (MongoException ex) { return Response.status(Response.Status.BAD_REQUEST).build(); } } else { return Response.status(Response.Status.BAD_REQUEST).build(); } } catch (JSONException | IOException e) { return Response.status(Response.Status.BAD_REQUEST).build(); } } return Response.status(Response.Status.BAD_REQUEST).build(); }
From source file:eu.vital.vitalcep.restApp.alert.Alerts.java
private DBObject createAlertSensor(String cepico, String randomUUIDString, JSONObject dsjo, String cepInstance) throws JSONException { DBObject dbObject = (DBObject) JSON.parse(cepico); dbObject.put("@context", "http://vital-iot.eu/contexts/sensor.jsonld"); dbObject.put("id", host + "/sensor/" + randomUUIDString); dbObject.put("type", "vital:AlertSensor"); dbObject.put("status", "vital:Running"); JSONArray observes = new JSONArray(); JSONArray compl = dsjo.getJSONArray("complex"); for (int i = 0; i < compl.length(); i++) { JSONObject oComplex = new JSONObject(compl.get(i).toString()); JSONObject oObserves = new JSONObject(); oObserves.put("type", "vital:ComplexEvent"); //oObserves.put("uri", host.toString() // +"/sensor/"+randomUUIDString // +"/"+oComplex.getString("id").toString()); oObserves.put("id", host + "/sensor/" + randomUUIDString + "/" + oComplex.getString("id").toString()); observes.put(oObserves);//from w w w. j a v a2 s . c om } DBObject dbObject2 = (DBObject) JSON.parse(observes.toString()); dbObject.put("ssn:observes", dbObject2); dbObject.put("cepinstance", cepInstance); return dbObject; }
From source file:eu.vital.vitalcep.restApp.alert.Alerts.java
private void createAlertObject(DBObject dbObject, String randomUUIDString, JSONObject dsjo) throws JSONException { dbObject.put("@context", "http://vital-iot.eu/contexts/sensor.jsonld"); dbObject.put("id", host + "/sensor/" + randomUUIDString); dbObject.put("uri", host + "/sensor/" + randomUUIDString); dbObject.removeField("type"); dbObject.put("type", "vital:AlertSensor"); dbObject.put("status", "vital:Running"); JSONArray observes = new JSONArray(); JSONArray compl = dsjo.getJSONArray("complex"); for (int i = 0; i < compl.length(); i++) { JSONObject oComplex = new JSONObject(compl.get(i).toString()); JSONObject oObserves = new JSONObject(); oObserves.put("type", "vital:ComplexEvent"); oObserves.put("uri", host + "/sensor/" + randomUUIDString + "/" + oComplex.getString("id").toString()); oObserves.put("id", host + "/sensor/" + randomUUIDString + "/" + oComplex.getString("id").toString()); observes.put(oObserves);//from w ww . j av a2 s.com } DBObject dbObject2 = (DBObject) JSON.parse(observes.toString()); dbObject.put("ssn:observes", dbObject2); }
From source file:eu.vital.vitalcep.restApp.cepRESTApi.CEPICO.java
/** * Creates a filter.// ww w . j a v a 2 s .c o m * * @param cepico * @param req * @return the filter id * @throws java.io.IOException */ @PUT @Path("createcepico") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Response createCEPICO(String cepico, @Context HttpServletRequest req) throws IOException { StringBuilder ck = new StringBuilder(); Security slogin = new Security(); JSONObject credentials = new JSONObject(); Boolean token = slogin.login(req.getHeader("name"), req.getHeader("password"), false, ck); credentials.put("username", req.getHeader("name")); credentials.put("password", req.getHeader("password")); if (!token) { return Response.status(Response.Status.UNAUTHORIZED).build(); } this.cookie = ck.toString(); JSONObject jo = new JSONObject(cepico); if (!jo.has("source")) { return Response.status(Response.Status.BAD_REQUEST).build(); } MongoClient mongo = new MongoClient(new MongoClientURI(mongoURL)); MongoDatabase db = mongo.getDatabase(mongoDB); try { db.getCollection("cepicos"); } catch (Exception e) { //System.out.println("Mongo is down"); mongo.close(); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); } if (jo.has("dolceSpecification")) { //Filter oFilter = new Filter(filter); JSONObject dsjo = jo.getJSONObject("dolceSpecification"); String str = dsjo.toString();//"{\"dolceSpecification\": "+ dsjo.toString()+"}"; try { DolceSpecification ds = new DolceSpecification(str); if (ds instanceof DolceSpecification) { UUID uuid = UUID.randomUUID(); String randomUUIDString = uuid.toString(); String mqin = RandomStringUtils.randomAlphanumeric(8); String mqout = RandomStringUtils.randomAlphanumeric(8); Date NOW = new Date(); JSONArray requestArray; try { requestArray = createCEPICORequests(jo.getJSONArray("source"), ds.getEvents(), getXSDDateTime(NOW)); } catch (Exception e) { mongo.close(); return Response.status(Response.Status.BAD_REQUEST) .entity("not available getObservation Service for this sensor ").build(); } CEP cepProcess = new CEP(); if (!(cepProcess.CEPStart(CEP.CEPType.CEPICO, ds, mqin, mqout, confFile, requestArray.toString(), credentials))) { mongo.close(); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); } if (cepProcess.PID < 1) { mongo.close(); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); } DBObject dbObject = createCEPSensor(cepico, randomUUIDString, dsjo, cepProcess.id); Document doc = new Document(dbObject.toMap()); try { db.getCollection("cepicos").insertOne(doc); JSONObject opState = createOperationalStateObservation(randomUUIDString); String sensorId = host + "/sensor/" + randomUUIDString; MessageProcessor_publisher Publisher_MsgProcc = new MessageProcessor_publisher(this.dmsURL, cookie, sensorId, "cepicosobservations", mongoURL, mongoDB);//555 MQTT_connector_subscriper publisher = new MQTT_connector_subscriper(mqout, Publisher_MsgProcc); MqttConnectorContainer.addConnector(publisher.getClientName(), publisher); DBObject oPut = (DBObject) JSON.parse(opState.toString()); Document doc1 = new Document(oPut.toMap()); try { db.getCollection("cepicosobservations").insertOne(doc1); String id = doc1.get("_id").toString(); } catch (MongoException ex) { mongo.close(); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); } JSONObject aOutput = new JSONObject(); aOutput.put("id", host + "/sensor/" + randomUUIDString); return Response.status(Response.Status.OK).entity(aOutput.toString()).build(); } catch (MongoException ex) { return Response.status(Response.Status.BAD_REQUEST).build(); } } else { mongo.close(); return Response.status(Response.Status.BAD_REQUEST).build(); } } catch (MongoException ex) { return Response.status(Response.Status.BAD_REQUEST).build(); } } else { mongo.close(); return Response.status(Response.Status.BAD_REQUEST).build(); } }
From source file:eu.vital.vitalcep.restApp.cepRESTApi.CEPICO.java
private DBObject createCEPSensor(String cepico, String randomUUIDString, JSONObject dsjo, String cepInstance) throws JSONException { DBObject dbObject = (DBObject) JSON.parse(cepico); dbObject.put("@context", "http://vital-iot.eu/contexts/sensor.jsonld"); dbObject.put("id", host + "/sensor/" + randomUUIDString); dbObject.put("type", "vital:CEPSensor"); dbObject.put("status", "vital:Running"); JSONArray observes = new JSONArray(); JSONArray compl = dsjo.getJSONArray("complex"); for (int i = 0; i < compl.length(); i++) { JSONObject oComplex = new JSONObject(compl.get(i).toString()); JSONObject oObserves = new JSONObject(); oObserves.put("type", "vital:ComplexEvent"); //oObserves.put("uri", host.toString() // +"/sensor/"+randomUUIDString // +"/"+oComplex.getString("id").toString()); oObserves.put("id", host + "/sensor/" + randomUUIDString + "/" + oComplex.getString("id").toString()); observes.put(oObserves);/* w w w . j av a2 s . com*/ } DBObject dbObject2 = (DBObject) JSON.parse(observes.toString()); dbObject.put("ssn:observes", dbObject2); dbObject.put("cepinstance", cepInstance); return dbObject; }
From source file:eu.vital.vitalcep.restApp.filteringApi.ContinuosFiltering.java
/** * Creates a filter./*w ww.j a v a 2s . c o m*/ * * @param filter * @param req * @return the filter id * @throws java.io.IOException */ @PUT @Path("createcontinuousfilter") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Response createcontinuousfilter(String filter, @Context HttpServletRequest req) throws IOException { StringBuilder ck = new StringBuilder(); Security slogin = new Security(); JSONObject credentials = new JSONObject(); Boolean token = slogin.login(req.getHeader("name"), req.getHeader("password"), false, ck); credentials.put("username", req.getHeader("name")); credentials.put("password", req.getHeader("password")); if (!token) { return Response.status(Response.Status.UNAUTHORIZED).build(); } this.cookie = ck.toString(); JSONObject jo = new JSONObject(filter); MongoClient mongo = new MongoClient(new MongoClientURI(mongoURL)); MongoDatabase db = mongo.getDatabase(mongoDB); try { db.getCollection("continuousfilters"); } catch (Exception e) { //System.out.println("Mongo is down"); db = null; if (mongo != null) { mongo.close(); mongo = null; } return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); } if (jo.has("dolceSpecification")) { JSONObject dsjo = jo.getJSONObject("dolceSpecification"); String str = dsjo.toString();//"{\"dolceSpecification\": "+ dsjo.toString()+"}"; try { DolceSpecification ds = new DolceSpecification(str); if (ds instanceof DolceSpecification) { UUID uuid = UUID.randomUUID(); String randomUUIDString = uuid.toString(); String mqin = RandomStringUtils.randomAlphanumeric(8); String mqout = RandomStringUtils.randomAlphanumeric(8); CEP cepProcess = new CEP(); if (!(cepProcess.CEPStart(CEP.CEPType.CONTINUOUS, ds, mqin, mqout, confFile, jo.getJSONArray("source").toString(), credentials))) { return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); } if (cepProcess.PID < 1) { return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); } DBObject dbObject = createCEPFilterSensor(filter, randomUUIDString, dsjo, cepProcess.id); Document doc = new Document(dbObject.toMap()); try { db.getCollection("continuousfilters").insertOne(doc); JSONObject aOutput = new JSONObject(); String sensorId = host + "/sensor/" + randomUUIDString; aOutput.put("id", sensorId); //MIGUEL MessageProcessor_publisher Publisher_MsgProcc = new MessageProcessor_publisher(this.dmsURL, this.cookie, sensorId, "continuosfiltersobservations", this.mongoURL, this.mongoDB);//555 MQTT_connector_subscriper publisher = new MQTT_connector_subscriper(mqout, Publisher_MsgProcc); MqttConnectorContainer.addConnector(publisher.getClientName(), publisher); //TODO --> DESTROY DEL CONNECTOR. // MqttConnectorContainer.deleteConnector(publisher // .getClientName()); JSONObject opState = createOperationalStateObservation(randomUUIDString); DBObject oPut = (DBObject) JSON.parse(opState.toString()); Document doc1 = new Document(oPut.toMap()); try { db.getCollection("continuosfiltersobservations").insertOne(doc1); String id = doc1.get("_id").toString(); db = null; if (mongo != null) { mongo.close(); mongo = null; } } catch (MongoException ex) { db = null; if (mongo != null) { mongo.close(); mongo = null; } return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); } return Response.status(Response.Status.OK).entity(aOutput.toString()).build(); } catch (MongoException ex) { db = null; if (mongo != null) { mongo.close(); mongo = null; } return Response.status(Response.Status.BAD_REQUEST).build(); } } else { return Response.status(Response.Status.BAD_REQUEST).build(); } } catch (JSONException | IOException e) { return Response.status(Response.Status.BAD_REQUEST).build(); } } return Response.status(Response.Status.BAD_REQUEST).build(); }