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:com.original.service.channel.config.Initializer.java
License:Open Source License
/** * // ww w . j a v a2 s . co m * @param db * @param collectionName * @param fileName * @param force * @throws IOException */ private void initCollection(DB db, String collectionName, String fileName, boolean force) throws IOException { DBCollection collection = db.getCollection(collectionName); if (collection.getCount() > 0 && !force) { logger.info(collectionName + " had existed!"); return; } if (force && collection.getCount() > 0) { collection.drop(); logger.info("force to init, drop the collection:" + collectionName); } BufferedReader br = null; if (fileName.startsWith("/")) { InputStream is = Initializer.class.getResourceAsStream(fileName); br = new BufferedReader(new InputStreamReader(is)); } else { br = new BufferedReader(new FileReader(fileName)); } StringBuffer fileText = new StringBuffer(); String line; while ((line = br.readLine()) != null) { fileText.append(line); } logger.info(collectionName + " Read:" + fileText); // System.out.println(profile); br.close(); if (fileText != null) { // convert JSON to DBObject directly List<String> list = parseJsonItemsFile(fileText); for (String txt : list) { logger.info(collectionName + " init item:" + txt); DBObject dbObject = (DBObject) JSON.parse(txt); collection.insert(dbObject); } } logger.info(collectionName + " init Done:" + collection.count()); }
From source file:com.ponysdk.mongodb.dao.MongoDAO.java
License:Apache License
private void saveOrUpdate(final Object object, final String nameSpace) { final ObjectMapper mapper = new ObjectMapper(); final StringWriter jsonOutput = new StringWriter(); try {// ww w . j a v a 2s .c o m mapper.writeValue(jsonOutput, object); final DBObject dbObject = (DBObject) JSON.parse(jsonOutput.toString()); final Identifiable m = (Identifiable) object; if (m.getID() != null) { db.getCollection(nameSpace).findAndModify(new BasicDBObject("_id", m.getID()), dbObject); } else { final ObjectId id = new ObjectId(); dbObject.put("_id", id); db.getCollection(nameSpace).save(dbObject); m.setID(id); } } catch (final Exception e) { e.printStackTrace(); } }
From source file:com.redhat.jielicious.storage.mongo.handler.MongoStorageHandler.java
License:Open Source License
private void putAll(String body, SecurityContext context, String systemId, String agentId, String jvmId, final String namespace, AsyncResponse asyncResponse, final String collectionSuffix) { if (!namespaceSet.contains(namespace)) { namespaceSet.add(namespace);/* w w w . j av a2s . c o m*/ } if (!indexSet.contains(namespace + collectionSuffix)) { ThermostatMongoStorage.getDatabase().getCollection(namespace + collectionSuffix) .createIndex(Indexes.descending("systemId"), new IndexOptions().background(true)); ThermostatMongoStorage.getDatabase().getCollection(namespace + collectionSuffix) .createIndex(Indexes.descending("agentId"), new IndexOptions().background(true)); ThermostatMongoStorage.getDatabase().getCollection(namespace + collectionSuffix) .createIndex(Indexes.descending("jvmId"), new IndexOptions().background(true)); indexSet.add(namespace + collectionSuffix); } try { BasicDBList inputList = (BasicDBList) JSON.parse(body); final List<Document> items = new ArrayList<>(); for (Object item : inputList) { items.add(Document.parse(new DocumentBuilder(item.toString()) .addTags(context.getUserPrincipal().getName()).addId("systemId", systemId) .addId("agentId", agentId).addId("jvmId", jvmId).build())); } TimedRequest<Boolean> timedRequest = new TimedRequest<>(); Boolean response = timedRequest.run(new TimedRequest.TimedRunnable<Boolean>() { @Override public Boolean run() { try { ThermostatMongoStorage.getDatabase().getCollection(namespace + collectionSuffix) .insertMany(items); } catch (Exception e) { return Boolean.FALSE; } return Boolean.TRUE; } }); asyncResponse.resume(Response.status(Response.Status.OK).entity("PUT: " + response.toString()).build()); } catch (Exception e) { asyncResponse.resume(Response.status(Response.Status.BAD_REQUEST).build()); } }
From source file:com.redhat.jielicious.storage.mongo.handler.MongoStorageHandler.java
License:Open Source License
private void postAll(String body, String offset, String limit, SecurityContext context, String systemId, String agentId, String jvmId, final String namespace, final String sort, AsyncResponse asyncResponse, final String collectionSuffix) { try {//from www . j a v a 2s .co m BasicDBList queries = (BasicDBList) JSON.parse(body); final int o = Integer.valueOf(offset); final int l = Integer.valueOf(limit); final String userName = context.getUserPrincipal().getName(); final Bson filter = MongoRequestFilters.buildPostFilter(queries, systemId, agentId, jvmId, Collections.singletonList(userName)); TimedRequest<String> timedRequest = new TimedRequest<>(); String documents = timedRequest.run(new TimedRequest.TimedRunnable<String>() { @Override public String run() { FindIterable<Document> documents = ThermostatMongoStorage.getDatabase() .getCollection(namespace + collectionSuffix).find(filter) .projection(fields(exclude("tags"), excludeId())).sort(createSortObject(sort)).limit(l) .skip(o).batchSize(l).cursorType(CursorType.NonTailable); return MongoResponseBuilder.buildJsonDocuments(documents); } }); asyncResponse.resume(Response.status(Response.Status.OK) .entity(MongoResponseBuilder.buildJsonResponseWithTime(documents, timedRequest.getElapsed())) .build()); } catch (Exception e) { e.printStackTrace(); asyncResponse.resume(Response.status(Response.Status.BAD_REQUEST).build()); } }
From source file:com.redhat.lightblue.mongo.metadata.BSONParser.java
License:Open Source License
/** * Override to convert partialFilterExpression index property from string to map. * *///from w w w .jav a 2 s . co m @Override public Index parseIndex(Object object) { Index index = super.parseIndex(object); String partialFilterExpression = (String) index.getProperties() .get(MongoCRUDController.PARTIAL_FILTER_EXPRESSION_OPTION_NAME); if (partialFilterExpression != null) { // convert string to json // https://github.com/lightblue-platform/lightblue-mongo/issues/329 index.getProperties().put(MongoCRUDController.PARTIAL_FILTER_EXPRESSION_OPTION_NAME, (Map<String, Object>) JSON.parse(partialFilterExpression)); } return index; }
From source file:com.redhat.thermostat.gateway.common.mongodb.executor.MongoExecutor.java
License:Open Source License
public MongoDataResultContainer execPostRequest(MongoCollection<DBObject> collection, String body, Set<String> realms) { MongoDataResultContainer metaDataContainer = new MongoDataResultContainer(); if (body.length() > 0) { List<DBObject> inputList = (List<DBObject>) JSON.parse(body); for (DBObject object : inputList) { object.removeField(KeycloakFields.REALMS_KEY); if (realms != null && !realms.isEmpty()) { object.put(KeycloakFields.REALMS_KEY, realms); }//from ww w.ja v a 2s.co m } collection.insertMany(inputList); } return metaDataContainer; }
From source file:com.redhat.thermostat.gateway.common.mongodb.MongoStorageHandler.java
License:Open Source License
public void addSystemObjects(MongoCollection<DBObject> collection, String systemId, String body) { if (body.length() > 0) { List<DBObject> inputList = (List<DBObject>) JSON.parse(body); for (DBObject o : inputList) { o.put(ThermostatFields.SYSTEM_ID, systemId); }/* w ww . java2s . c o m*/ collection.insertMany(inputList); } }
From source file:com.redhat.thermostat.gateway.common.mongodb.MongoStorageHandler.java
License:Open Source License
public void addJvmObjects(MongoCollection<DBObject> collection, String systemId, String jvmId, String body) { if (body.length() > 0) { List<DBObject> inputList = (List<DBObject>) JSON.parse(body); for (DBObject o : inputList) { o.put(ThermostatFields.SYSTEM_ID, systemId); o.put(ThermostatFields.JVM_ID, jvmId); }/*from w w w. j a v a 2 s .com*/ collection.insertMany(inputList); } }
From source file:com.redhat.thermostat.gateway.common.mongodb.MongoStorageHandler.java
License:Open Source License
public void updateOneSystemObject(MongoCollection<Document> collection, final String systemId, String queries, String body) {//from w ww . j a v a 2 s . c o m Bson sysQuery = buildEq(ThermostatFields.SYSTEM_ID, systemId); Bson query = buildAnd(sysQuery, MongoRequestFilters.buildQueriesFilter(queries)); BasicDBObject inputObject = (BasicDBObject) JSON.parse(body); BasicDBObject setObject = (BasicDBObject) inputObject.get(SET_FIELD_NAME); if (setObject.containsField(ThermostatFields.SYSTEM_ID)) { throw new UnsupportedOperationException( "Updating " + ThermostatFields.SYSTEM_ID + " field is not allowed"); } final Bson fields = new Document("$set", setObject); collection.updateMany(query, fields); }
From source file:com.redhat.thermostat.gateway.common.mongodb.MongoStorageHandler.java
License:Open Source License
public void updateOneJvmObject(MongoCollection<Document> collection, final String systemId, final String jvmId, String queries, String body) { Bson sysQuery = buildEq(ThermostatFields.SYSTEM_ID, systemId); Bson jvmQuery = buildEq(ThermostatFields.JVM_ID, jvmId); Bson query = buildAnd(buildAnd(sysQuery, jvmQuery), MongoRequestFilters.buildQueriesFilter(queries)); BasicDBObject inputObject = (BasicDBObject) JSON.parse(body); BasicDBObject setObject = (BasicDBObject) inputObject.get(SET_FIELD_NAME); if (setObject.containsField(ThermostatFields.SYSTEM_ID)) { throw new UnsupportedOperationException( "Updating " + ThermostatFields.SYSTEM_ID + " field is not allowed"); }//w w w .j av a 2 s . c om if (setObject.containsField(ThermostatFields.JVM_ID)) { throw new UnsupportedOperationException( "Updating " + ThermostatFields.JVM_ID + " field is not allowed"); } final Bson fields = new Document("$set", setObject); collection.updateMany(query, fields); }