List of usage examples for com.mongodb DBObject removeField
Object removeField(String key);
From source file:org.ossmeter.platform.admin.LoggingInformation.java
License:Open Source License
@SuppressWarnings({ "rawtypes", "unchecked" }) @Get("json")// ww w . j a v a 2 s . c o m public String represent() { @SuppressWarnings("unchecked") Series<Header> responseHeaders = (Series<Header>) getResponse().getAttributes() .get("org.restlet.http.headers"); if (responseHeaders == null) { responseHeaders = new Series(Header.class); getResponse().getAttributes().put("org.restlet.http.headers", responseHeaders); } responseHeaders.add(new Header("Access-Control-Allow-Origin", "*")); responseHeaders.add(new Header("Access-Control-Allow-Methods", "GET")); try { Mongo mongo = new Mongo(); DB db = mongo.getDB("logging"); DBCollection col = db.getCollection("log"); ObjectMapper mapper = new ObjectMapper(); ArrayNode results = mapper.createArrayNode(); DBCursor cursor = col.find(); try { while (cursor.hasNext()) { DBObject p = cursor.next(); p.removeField("_id"); p.removeField("thread"); p.removeField("host"); p.removeField("class"); results.add(mapper.readTree(p.toString())); } } catch (JsonProcessingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { cursor.close(); } mongo.close(); return results.toString(); } catch (UnknownHostException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } return null; }
From source file:org.ossmeter.platform.client.api.ProjectImportResource.java
License:Open Source License
@Post("json") public Representation importProject(Representation entity) { try {//from ww w .j ava 2s . c o m ObjectMapper mapper = new ObjectMapper(); ObjectNode obj = (ObjectNode) mapper.readTree(entity.getText()); System.out.println(obj); String url = obj.get("url").toString(); url = url.replace("\"", ""); ProjectImporter importer = new ProjectImporter(); Project p = importer.importProject(url); if (p == null) { ObjectNode msg = mapper.createObjectNode(); msg.put("status", "error"); msg.put("msg", "Unable to import project."); // FIXME inc reason StringRepresentation rep = new StringRepresentation(msg.toString()); rep.setMediaType(MediaType.APPLICATION_JSON); getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST); return rep; } // Clean up the object DBObject project = p.getDbObject(); project.removeField("storage"); project.removeField("metricProviderData"); project.removeField("_superTypes"); project.removeField("_id"); // FIXME: Temporary solution project.removeField("licenses"); project.removeField("persons"); StringRepresentation rep = new StringRepresentation(p.getDbObject().toString()); rep.setMediaType(MediaType.APPLICATION_JSON); getResponse().setStatus(Status.SUCCESS_CREATED); return rep; } catch (IOException e) { e.printStackTrace(); // TODO StringRepresentation rep = new StringRepresentation(""); rep.setMediaType(MediaType.APPLICATION_JSON); getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST); return rep; } }
From source file:org.ossmeter.platform.client.api.ProjectListResource.java
License:Open Source License
public Representation doRepresent() { // Defaults// w w w .ja va 2 s . c om int pageSize = 10; int page = 0; // Ready query params String _page = getQueryValue("page"); String _size = getQueryValue("size"); if (_page != null && !"".equals(_page) && isInteger(_page)) { page = Integer.valueOf(_page); } if (_size != null && !"".equals(_size) && isInteger(_size)) { pageSize = Integer.valueOf(_size); } try { platform = new Platform(Configuration.getInstance().getMongoConnection()); } catch (UnknownHostException e1) { e1.printStackTrace(); ObjectNode m = mapper.createObjectNode(); m.put("apicall", "list-all-projects"); return Util.generateErrorMessageRepresentation(m, e1.getMessage()); } ProjectRepository projectRepo = platform.getProjectRepositoryManager().getProjectRepository(); DBCursor cursor = projectRepo.getProjects().getDbCollection().find().skip(page * pageSize).limit(pageSize); ArrayNode projects = mapper.createArrayNode(); while (cursor.hasNext()) { try { DBObject p = cursor.next(); p.removeField("storage"); p.removeField("metricProviderData"); p.removeField("_superTypes"); p.removeField("_id"); // FIXME: Temporary solution p.removeField("licenses"); p.removeField("persons"); projects.add(mapper.readTree(p.toString())); } catch (Exception e) { System.err.println("Error: " + e.getMessage()); ObjectNode m = mapper.createObjectNode(); m.put("apicall", "list-all-projects"); return Util.generateErrorMessageRepresentation(m, e.getMessage()); } } cursor.close(); StringRepresentation resp = new StringRepresentation(projects.toString()); resp.setMediaType(MediaType.APPLICATION_JSON); return resp; // // TODO // boolean paging = getRequest().getAttributes().containsKey("page"); // // Platform platform = Platform.getInstance(); // ProjectRepository projectRepo = platform.getProjectRepositoryManager().getProjectRepository(); // // Iterator<Project> it = projectRepo.getProjects().iterator(); // // ObjectMapper mapper = new ObjectMapper(); // ArrayNode projects = mapper.createArrayNode(); // // while (it.hasNext()) { // try { // Project project = it.next(); // DBObject p = project.getDbObject(); // // p.removeField("storage"); // p.removeField("metricProviderData"); // p.removeField("_superTypes"); // p.removeField("_id"); // // // FIXME: Temporary solution // p.removeField("licenses"); // p.removeField("persons"); // // projects.add(mapper.readTree(p.toString())); //TODO: There must be a better way.. // // } catch (Exception e) { // System.err.println("Error: " + e.getMessage()); // ObjectNode m = mapper.createObjectNode(); // m.put("apicall", "list-all-projects"); // return Util.generateErrorMessageRepresentation(m, e.getMessage()); // } // } // StringRepresentation resp = new StringRepresentation(projects.toString()); // resp.setMediaType(MediaType.APPLICATION_JSON); // return resp; }
From source file:org.restheart.hal.metadata.singletons.FilterTransformer.java
License:Open Source License
/** * * @param exchange//from w ww . j a v a 2s. c o m * @param context * @param contentToTransform * @param args properties to filter out as an array of strings (["prop1", * "prop2"] */ @Override public void tranform(final HttpServerExchange exchange, final RequestContext context, DBObject contentToTransform, final DBObject args) { if (args instanceof BasicDBList) { BasicDBList toremove = (BasicDBList) args; toremove.forEach(_prop -> { if (_prop instanceof String) { String prop = (String) _prop; contentToTransform.removeField(prop); } else { context.addWarning("property in the args list is not a string: " + _prop); } }); } else { context.addWarning( "transformer wrong definition: args property must be an arrary of string property names."); } }
From source file:org.restheart.handlers.schema.JsonSchemaTransformer.java
License:Open Source License
public static void escapeSchema(DBObject schema) { DBObject escaped = (DBObject) JsonUtils.escapeKeys(schema, false); List<String> keys = Lists.newArrayList(schema.keySet().iterator()); keys.stream().forEach(f -> schema.removeField(f)); schema.putAll(escaped);/*from www . j a v a 2s . c o m*/ }
From source file:org.restheart.handlers.schema.JsonSchemaTransformer.java
License:Open Source License
public static void unescapeSchema(DBObject schema) { DBObject unescaped = (DBObject) JsonUtils.unescapeKeys(schema); List<String> keys = Lists.newArrayList(schema.keySet().iterator()); keys.stream().forEach(f -> schema.removeField(f)); schema.putAll(unescaped);// w ww .j a v a 2 s . c o m }
From source file:org.semispace.semimeter.dao.mongo.SemiMeterDaoMongo.java
License:Apache License
private void deleteOldMinutes(long before24h, long before180min, long before15min) { Calendar cal = new GregorianCalendar(); cal.setTimeInMillis(before24h);/*from www . ja v a 2 s.co m*/ cal.set(Calendar.MILLISECOND, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MINUTE, 0); long targetHour = cal.getTimeInMillis(); DBCollection meterCollection = mongoTemplate.getCollection("meter"); DBCursor result = meterCollection.find(new BasicDBObject(), new BasicDBObject("_id", 1)); while (result.hasNext()) { DBObject doc = result.next(); try { //start a new "session" for each document. not sure if this actually helps anything consistency-wise meterCollection.getDB().requestStart(); //and fetch actual object (result only contains _id's) doc = meterCollection.findOne(doc); // TODO Double check log.trace("cleaning document : {}", doc); DBObject day = (DBObject) doc.get("day"); //log.trace("day: {}", day); DBObject hours = (DBObject) day.get("hours"); //log.trace("hours: {}", hours); Set<String> hrSet = new HashSet<String>(); hrSet.addAll(hours.keySet()); boolean docChanged = false; if (hrSet.isEmpty()) { log.trace("no hours in document, remove it: {}", doc); meterCollection.remove(new BasicDBObject("_id", doc.get("_id")), WriteConcern.UNACKNOWLEDGED); } else { for (String h : hrSet) { long hourmillis = Long.valueOf(h); log.trace("checking hour: {}", hourmillis); if (hourmillis < targetHour) { if (log.isTraceEnabled()) { log.trace("removing hour " + h + " because it is older than target" + targetHour); } docChanged = true; DBObject obj = (DBObject) hours.get(h); day.put("count", (Integer) day.get("count") - (Integer) obj.get("count")); hours.removeField(h); } else if (hourmillis == targetHour) { log.trace("current hour is targetHour, check minutes"); DBObject currentHour = (DBObject) hours.get(h); DBObject minutes = (DBObject) currentHour.get("minutes"); Set<String> keys = new HashSet<String>(); keys.addAll(minutes.keySet()); for (String m : keys) { long minutemillis = Long.valueOf(m); log.trace("checking minute: {}", minutemillis); if (minutemillis < before24h) { if (log.isTraceEnabled()) { log.trace("removing minute " + minutemillis + " because it is older than " + before24h); } docChanged = true; DBObject obj = (DBObject) minutes.get(m); DBObject hourObj = (DBObject) hours.get(h); day.put("count", (Integer) day.get("count") - (Integer) obj.get("count")); hourObj.put("count", (Integer) hourObj.get("count") - (Integer) obj.get("count")); minutes.removeField(m); } } if (minutes.keySet().isEmpty()) { log.trace("no more minutes, removing hour {}", h); hours.removeField(h); docChanged = true; } } } } docChanged |= updateTrendCounters(doc, before180min, before15min); if (docChanged) { meterCollection.save(doc); } } finally { meterCollection.getDB().requestDone(); } } }
From source file:org.sipfoundry.sipxconfig.admin.commserver.imdb.UserStatic.java
License:Contributor Agreement License
public void generate(Replicable entity, DBObject top) { if (entity instanceof User) { User user = (User) entity;/* www. j a v a 2 s. co m*/ String domainName = getSipDomain(); String externalMwi = user.getSettingValue(EXTERNAL_MWI); if (externalMwi != null) { top.put(STATIC, new UserStaticMapping(domainName, user.getUserName(), externalMwi)); } else { top.removeField(STATIC); } getDbCollection().save(top); } }
From source file:org.sipfoundry.sipxconfig.commserver.imdb.AbstractDataSetGenerator.java
License:Open Source License
protected void putOnlyIfNotNull(DBObject obj, String propName, Object prop) { if (prop instanceof String) { if (StringUtils.isNotBlank((String) prop)) { obj.put(propName, prop);/*from w w w.jav a 2s .co m*/ } } else if (prop != null) { obj.put(propName, prop); } else { obj.removeField(propName); } }
From source file:org.sipfoundry.sipxconfig.commserver.imdb.AbstractDataSetGenerator.java
License:Open Source License
protected void removeField(DBObject top, String field) { if (top.containsField(field)) { top.removeField(field); } }