List of usage examples for com.mongodb DBCursor close
@Override public void close()
From source file:com.caci.dummyserver.MongoRepository.java
public GetObjectsResult getObjects(String table, Collection<Pair<String, String>> keys, Collection<String> fields, Collection<Pair<String, Collection<String>>> filters, Collection<String> sort, Integer offset, Integer limit) { DB db = mongo.getDB("Objects"); DBCollection col = db.getCollection(table); GetObjectsResult result = new GetObjectsResult(); DBObject queryObject = makeQueryObject(keys, filters); DBCursor cursor = col.find(queryObject); try {/*w w w . ja v a 2 s . co m*/ if (sort != null && !sort.isEmpty()) { DBObject sortObj = new BasicDBObject(); for (String fieldName : sort) { if (fieldName.startsWith("-")) { sortObj.put("data." + fieldName.substring(1), -1); } else { sortObj.put("data." + fieldName, 1); } } cursor.sort(sortObj); } result.setTotal(cursor.count()); if (offset != null && offset > 0) { cursor.skip(offset); } if (limit != null && limit > 0) { cursor.limit(limit); } while (cursor.hasNext()) { DBObject obj = cursor.next(); obj = pruneDBObject(getData(obj), fields); result.getResults().add(obj.toString()); } } finally { cursor.close(); } return result; }
From source file:com.callidusrobotics.droptables.model.DocumentDao.java
License:Open Source License
protected List<DBObject> getDocuments(DBCursor cursor) { List<DBObject> result = new LinkedList<DBObject>(); try {//from w ww .ja v a2 s . c o m while (cursor.hasNext()) { result.add(flatten(cursor.next())); } } finally { cursor.close(); } return result; }
From source file:com.cedac.security.acls.mongo.MongoAclService.java
License:Apache License
@Override public List<ObjectIdentity> findChildren(ObjectIdentity parentIdentity) { LOG.debug(ACL, "Looking for children of object identity {}", parentIdentity); DBObject query = queryByParentIdentity(parentIdentity); DBObject projection = new BasicDBObject(objectIdFieldName, true); DBCursor cursor = null; try {//from w ww .j a v a 2 s .co m cursor = getAclCollection().find(query, projection); if (cursor.count() == 0) { LOG.debug(ACL, "No child object found for identity {}", parentIdentity); return null; } LOG.trace(ACL, "Streaming cursor in order to retrieve child object identities"); List<ObjectIdentity> oids = new ArrayList<ObjectIdentity>(); while (cursor.hasNext()) { oids.add(toObjectIdentity((DBObject) cursor.next().get(objectIdFieldName))); } return oids; } finally { if (cursor != null) { cursor.close(); } } }
From source file:com.cedac.security.oauth2.provider.approval.MongoApprovalStore.java
License:Apache License
@Override public List<Approval> getApprovals(String userName, String clientId) { BasicDBObject query = new BasicDBObject(userIdFieldName, userName).append(clientIdFieldName, clientId); DBCursor cursor = null; try {/*from ww w . j a va 2s. c om*/ List<Approval> approvals = new ArrayList<Approval>(); cursor = getApprovalsCollection().find(query); while (cursor.hasNext()) { DBObject dbo = cursor.next(); approvals.add(new Approval((String) dbo.get(userIdFieldName), (String) dbo.get(clientIdFieldName), (String) dbo.get(scopeFieldName), (Date) dbo.get(expiresAtFieldName), Approval.ApprovalStatus.valueOf((String) dbo.get(statusFieldName)), (Date) dbo.get(lastModifiedAtFieldName))); } return approvals; } finally { if (cursor != null) { cursor.close(); } } }
From source file:com.cedac.security.oauth2.provider.client.MongoClientDetailsService.java
License:Apache License
public List<ClientDetails> listClientDetails() { DBCursor cursor = null; try {/* ww w .j a va 2 s .co m*/ cursor = getClientDetailsCollection().find(); List<ClientDetails> clientDetails = new ArrayList<ClientDetails>(); while (cursor.hasNext()) { clientDetails.add(toClientDetails(cursor.next())); } return clientDetails; } finally { if (cursor != null) { cursor.close(); } } }
From source file:com.cedac.security.oauth2.provider.token.store.MongoTokenStore.java
License:Apache License
public Collection<OAuth2AccessToken> findTokensByClientId(String clientId) { List<OAuth2AccessToken> accessTokens = new ArrayList<OAuth2AccessToken>(); DBObject query = new BasicDBObject(clientIdFieldName, clientId); DBObject projection = new BasicDBObject(tokenFieldName, 1); DBCursor cursor = null; try {// ww w. j av a 2 s . c o m cursor = getAccessTokenCollection().find(query, projection); if (cursor.count() > 0) { while (cursor.hasNext()) { OAuth2AccessToken token = mapAccessToken(cursor.next()); if (token != null) { accessTokens.add(token); } } } else { LOG.info("Failed to find access token for clientId {}", clientId); } return accessTokens; } finally { if (cursor != null) { cursor.close(); } } }
From source file:com.cedac.security.oauth2.provider.token.store.MongoTokenStore.java
License:Apache License
public Collection<OAuth2AccessToken> findTokensByUserName(String userName) { List<OAuth2AccessToken> accessTokens = new ArrayList<OAuth2AccessToken>(); DBObject query = new BasicDBObject(usernameFieldName, userName); DBObject projection = new BasicDBObject(tokenFieldName, 1); DBCursor cursor = null; try {//w ww. j a va2 s. com cursor = getAccessTokenCollection().find(query, projection); if (cursor.count() > 0) { while (cursor.hasNext()) { OAuth2AccessToken token = mapAccessToken(cursor.next()); if (token != null) { accessTokens.add(token); } } } else { LOG.info("Failed to find access token for username {}.", userName); } return accessTokens; } finally { if (cursor != null) { cursor.close(); } } }
From source file:com.cedac.security.oauth2.provider.token.store.MongoTokenStore.java
License:Apache License
public Collection<OAuth2AccessToken> findTokensByClientIdAndUserName(String clientId, String userName) { List<OAuth2AccessToken> accessTokens = new ArrayList<OAuth2AccessToken>(); DBObject query = new BasicDBObject(clientIdFieldName, clientId).append(usernameFieldName, userName); DBObject projection = new BasicDBObject(tokenFieldName, 1); DBCursor cursor = null; try {// w ww .j a va 2s .co m cursor = getAccessTokenCollection().find(query, projection); if (cursor.count() > 0) { while (cursor.hasNext()) { OAuth2AccessToken token = mapAccessToken(cursor.next()); if (token != null) { accessTokens.add(token); } } } else { LOG.info("Failed to find access token for clientId {} and username {}.", clientId, userName); } return accessTokens; } finally { if (cursor != null) { cursor.close(); } } }
From source file:com.cyslab.craftvm.rest.mongo.AggregateServlet.java
License:GNU General Public License
@Override protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { log.trace("doPost()"); if (!can_read(req)) { res.sendError(SC_UNAUTHORIZED);//w ww . ja v a2 s. c om return; } InputStream is = req.getInputStream(); String db_name = req.getParameter("dbname"); String col_name = req.getParameter("colname"); if (db_name == null || col_name == null) { error(res, SC_BAD_REQUEST, Status.get("param name missing")); return; } String skip = req.getParameter("skip"); String limit = req.getParameter("limit"); DB db = mongo.getDB(db_name); DBCollection col = db.getCollection(col_name); BufferedReader r = null; DBObject q = null, sort = null; try { r = new BufferedReader(new InputStreamReader(is)); String data = r.readLine(); if (data == null) { error(res, SC_BAD_REQUEST, Status.get("no data")); return; } try { q = (DBObject) JSON.parse(data); } catch (JSONParseException e) { error(res, SC_BAD_REQUEST, Status.get("can not parse data")); return; } // sort param data = r.readLine(); if (data != null) { try { sort = (DBObject) JSON.parse(data); } catch (JSONParseException e) { error(res, SC_BAD_REQUEST, Status.get("can not parse sort arg")); return; } } } finally { if (r != null) r.close(); } DBCursor c; if (sort == null) c = col.find(q); else c = col.find(q).sort(sort); if (c == null) { error(res, SC_NOT_FOUND, Status.get("no documents found")); return; } res.setIntHeader("X-Documents-Count", c.count()); if (limit != null) { try { c.limit(Math.min(Integer.parseInt(limit), MAX_FIELDS_TO_RETURN)); } catch (NumberFormatException e) { error(res, SC_BAD_REQUEST, Status.get("can not parse limit")); c.close(); return; } } else c.limit(MAX_FIELDS_TO_RETURN); if (skip != null) { try { c.skip(Integer.parseInt(skip)); } catch (NumberFormatException e) { error(res, SC_BAD_REQUEST, Status.get("can not parse skip")); c.close(); return; } } StringBuilder buf = tl.get(); // reset buf buf.setLength(0); int no = 0; buf.append("["); while (c.hasNext()) { DBObject o = c.next(); JSON.serialize(o, buf); buf.append(","); no++; } if (no > 0) buf.setCharAt(buf.length() - 1, ']'); else buf.append(']'); res.setIntHeader("X-Documents-Returned", no); out_str(req, buf.toString(), "application/json"); }
From source file:com.cyslab.craftvm.rest.mongo.AggregateServlet.java
License:GNU General Public License
@Override protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { log.trace("doGet()"); if (!can_read(req)) { res.sendError(SC_UNAUTHORIZED);// w w w. j a v a 2s. c o m return; } String db_name = req.getParameter("dbname"); String col_name = req.getParameter("colname"); if (db_name == null || col_name == null) { error(res, SC_BAD_REQUEST, Status.get("param name missing")); return; } String skip = req.getParameter("skip"); String limit = req.getParameter("limit"); DB db = mongo.getDB(db_name); DBCollection col = db.getCollection(col_name); DBCursor c = col.find(); if (c == null) { error(res, SC_NOT_FOUND, Status.get("no documents found")); return; } res.setIntHeader("X-Documents-Count", c.count()); if (limit != null) { try { c.limit(Math.min(Integer.parseInt(limit), MAX_FIELDS_TO_RETURN)); } catch (NumberFormatException e) { error(res, SC_BAD_REQUEST, Status.get("can not parse limit")); c.close(); return; } } else c.limit(MAX_FIELDS_TO_RETURN); if (skip != null) { try { c.skip(Integer.parseInt(skip)); } catch (NumberFormatException e) { error(res, SC_BAD_REQUEST, Status.get("can not parse skip")); c.close(); return; } } StringBuilder buf = tl.get(); buf.setLength(0); int no = 0; buf.append("["); while (c.hasNext()) { DBObject o = c.next(); JSON.serialize(o, buf); buf.append(","); no++; } if (no > 0) buf.setCharAt(buf.length() - 1, ']'); else buf.append(']'); res.setIntHeader("X-Documents-Returned", no); out_str(req, buf.toString(), "application/json"); }