List of usage examples for com.mongodb DBObject get
Object get(String key);
From source file:com.andiandy.m101j.week2.hw3.SessionDAO.java
License:Apache License
public String findUserNameBySessionId(String sessionId) { DBObject session = getSession(sessionId); if (session == null) { return null; } else {/* ww w . ja v a 2 s . co m*/ return session.get("username").toString(); } }
From source file:com.andiandy.m101j.week2.hw3.UserDAO.java
License:Apache License
public DBObject validateLogin(String username, String password) { DBObject user = null; // XXX look in the user collection for a user that has this username // assign the result to the user variable. user = usersCollection.findOne(new BasicDBObject("_id", username)); if (user == null) { System.out.println("User not in database"); return null; }/*from w w w .jav a 2s. com*/ String hashedAndSalted = user.get("password").toString(); String salt = hashedAndSalted.split(",")[1]; if (!hashedAndSalted.equals(makePasswordHash(password, salt))) { System.out.println("Submitted password is not a match"); return null; } return user; }
From source file:com.andiandy.m101j.week3.hw2_3.UserDAO.java
License:Apache License
public DBObject validateLogin(String username, String password) { DBObject user; user = usersCollection.findOne(new BasicDBObject("_id", username)); if (user == null) { System.out.println("User not in database"); return null; }/* www.j a v a2 s . com*/ String hashedAndSalted = user.get("password").toString(); String salt = hashedAndSalted.split(",")[1]; if (!hashedAndSalted.equals(makePasswordHash(password, salt))) { System.out.println("Submitted password is not a match"); return null; } return user; }
From source file:com.andreig.jetty.WriteServlet.java
License:GNU General Public License
@Override protected void doDelete(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { log.fine("doDelete()"); if (!can_write(req)) { res.sendError(SC_UNAUTHORIZED);/*from www . j a va 2 s .c o m*/ return; } InputStream is = req.getInputStream(); String db_name = req.getParameter("dbname"); String col_name = req.getParameter("colname"); if (db_name == null || col_name == null) { String names[] = req2mongonames(req); if (names != null) { db_name = names[0]; col_name = names[1]; } if (db_name == null || col_name == null) { error(res, SC_BAD_REQUEST, Status.get("param name missing")); return; } } DB db = mongo.getDB(db_name); // mongo auth String user = req.getParameter("user"); String passwd = req.getParameter("passwd"); if (user != null && passwd != null && (!db.isAuthenticated())) { boolean auth = db.authenticate(user, passwd.toCharArray()); if (!auth) { res.sendError(SC_UNAUTHORIZED); return; } } DBCollection col = db.getCollection(col_name); BufferedReader r = null; DBObject q = 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; } } finally { if (r != null) r.close(); } // search if (do_search) { DBCursor c = col.find(q); long l = c.count(); String todelete[] = new String[(int) l]; int n = 0; while (c.hasNext()) { DBObject o = c.next(); ObjectId oid = (ObjectId) o.get("_id"); String id = oid.toStringMongod(); todelete[n++] = id; } c.close(); search.get_writer().delete(todelete); } WriteResult wr = col.remove(q, write_concern); // return operation status if (do_return) { out_str(req, wr.toString()); if (wr.getError() == null) { res.setStatus(SC_BAD_REQUEST); return; } } res.setStatus(SC_OK); }
From source file:com.andreig.jetty.WriteServlet.java
License:GNU General Public License
@Override protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { log.fine("doPost()"); if (!can_write(req)) { res.sendError(SC_UNAUTHORIZED);/*from ww w.j av a 2 s .c o m*/ return; } InputStream is = req.getInputStream(); String db_name = req.getParameter("dbname"); String col_name = req.getParameter("colname"); if (db_name == null || col_name == null) { String names[] = req2mongonames(req); if (names != null) { db_name = names[0]; col_name = names[1]; } if (db_name == null || col_name == null) { error(res, SC_BAD_REQUEST, Status.get("param name missing")); return; } } boolean upsert = Boolean.parseBoolean(req.getParameter("upsert")); boolean multi = Boolean.parseBoolean(req.getParameter("multi")); DB db = mongo.getDB(db_name); // mongo auth String user = req.getParameter("user"); String passwd = req.getParameter("passwd"); if (user != null && passwd != null && (!db.isAuthenticated())) { boolean auth = db.authenticate(user, passwd.toCharArray()); if (!auth) { res.sendError(SC_UNAUTHORIZED); return; } } DBCollection col = db.getCollection(col_name); BufferedReader r = null; DBObject q = null, o = null; try { r = new BufferedReader(new InputStreamReader(is)); String q_s = r.readLine(); if (q_s == null) { error(res, SC_BAD_REQUEST, Status.get("no data")); return; } String o_s = r.readLine(); if (o_s == null) { error(res, SC_BAD_REQUEST, Status.get("obj to update missing")); return; } try { q = (DBObject) JSON.parse(q_s); o = (DBObject) JSON.parse(o_s); } catch (JSONParseException e) { error(res, SC_BAD_REQUEST, Status.get("can not parse data")); return; } } finally { if (r != null) r.close(); } // // search if (do_search) { String fn = col.getFullName(); DBCursor c = col.find(q); int cnt = c.count(); if (!multi) c.limit(1); long l = multi ? cnt : 1; String toupdate[] = new String[(int) l]; int n = 0; boolean insert = false; if (upsert && !multi && cnt == 0) insert = true; while (c.hasNext()) { DBObject _o = c.next(); ObjectId oid = (ObjectId) _o.get("_id"); String id = oid.toStringMongod(); toupdate[n++] = id; } c.close(); List<String> flds = Config.search_index_fields.get(fn); boolean commit = false; Document doc = null; Search _writer = search.get_writer(); if (flds != null && flds.size() > 0) { doc = new Document(); try { for (String fld : flds) { String val = (String) o.get(fld); if (val == null) continue; Search.add_searchable_s(doc, fld, val); commit = true; } if (commit) _writer.commit(doc); } catch (ClassCastException e) { error(res, SC_BAD_REQUEST, Status.get("searchable fields must be type String")); return; } catch (CorruptIndexException e) { error(res, SC_BAD_REQUEST, Status.get("Search corrupt index" + e)); return; } } if (commit && insert) log.warning("upsert with search not implemented yet"); else _writer.update(toupdate, doc); } WriteResult wr = col.update(q, o, upsert, multi, write_concern); // return operation status if (do_return) { out_str(req, wr.toString()); if (wr.getError() == null) { res.setStatus(SC_BAD_REQUEST); return; } } res.setStatus(SC_CREATED); }
From source file:com.andreig.jetty.WriteServlet.java
License:GNU General Public License
@Override protected void doPut(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { log.fine("doPut()"); if (!can_write(req)) { res.sendError(SC_UNAUTHORIZED);//from w w w .jav a 2s. 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) { String names[] = req2mongonames(req); if (names != null) { db_name = names[0]; col_name = names[1]; } if (db_name == null || col_name == null) { error(res, SC_BAD_REQUEST, Status.get("param name missing")); return; } } DB db = mongo.getDB(db_name); // mongo auth String user = req.getParameter("user"); String passwd = req.getParameter("passwd"); if (user != null && passwd != null && (!db.isAuthenticated())) { boolean auth = db.authenticate(user, passwd.toCharArray()); if (!auth) { res.sendError(SC_UNAUTHORIZED); return; } } DBCollection col = db.getCollection(col_name); BufferedReader r = null; ArrayList<DBObject> ar = new ArrayList<DBObject>(); try { r = new BufferedReader(new InputStreamReader(is)); String data; while ((data = r.readLine()) != null) { if (data != null) { DBObject o; try { o = (DBObject) JSON.parse(data); ar.add(o); } catch (JSONParseException e) { error(res, SC_BAD_REQUEST, Status.get("can not parse data")); return; } } } } finally { if (r != null) r.close(); } if (ar.size() == 0) { error(res, SC_BAD_REQUEST, Status.get("can not parse data")); return; } WriteResult wr = col.insert(ar, write_concern); // search if (do_search) { String fn = col.getFullName(); List<String> flds = Config.search_index_fields.get(fn); if (flds != null && flds.size() > 0) { Search _writer = search.get_writer(); try { for (DBObject o : ar) { boolean commit = false; Document doc = new Document(); for (String fld : flds) { String val = (String) o.get(fld); if (val == null) continue; Search.add_searchable_s(doc, fld, val); commit = true; } if (commit) { ObjectId id = (ObjectId) o.get("_id"); String sid = id.toStringMongod(); Search.add_storable(doc, "_id", sid); Search.add_searchable_s(doc, "_dbid_", fn); _writer.commit(doc); } } } catch (ClassCastException e) { error(res, SC_BAD_REQUEST, Status.get("searchable fields must be type String")); return; } catch (CorruptIndexException e) { error(res, SC_BAD_REQUEST, Status.get("Search corrupt index" + e)); return; } } } // return operation status if (do_return) { out_str(req, wr.toString()); if (wr.getError() == null) { res.setStatus(SC_BAD_REQUEST); return; } } res.setStatus(SC_CREATED); }
From source file:com.androidnatic.maps.model.HeatPoint.java
License:Open Source License
public static HeatPoint fromDBObject(DBObject cur) { HeatPoint toReturn = new HeatPoint(); toReturn.intensity = (Integer) cur.get("intensity"); toReturn.intensity *= 100 / 37.0;// w ww.j ava2s.c o m toReturn.lon = ((Double) ((BasicDBList) cur.get("loc")).get(1)).floatValue(); toReturn.lat = ((Double) ((BasicDBList) cur.get("loc")).get(0)).floatValue(); return toReturn; }
From source file:com.aperigeek.dropvault.web.dao.MongoFileService.java
License:Open Source License
public Resource getResourceAt(Resource parent, String... path) throws ResourceNotFoundException { DBCollection col = mongo.getDataBase().getCollection("files"); DBObject obj = null; ObjectId id = parent.getId();/* w w w . j a va 2s .co m*/ for (String name : path) { if ("".equals(name) || ".".equals(name)) { continue; } DBObject filter = new BasicDBObjectBuilder().add("name", name).add("parent", id).get(); obj = col.findOne(filter); if (obj == null) { throw new ResourceNotFoundException(); } id = (ObjectId) obj.get("_id"); } return buildResource(obj); }
From source file:com.aperigeek.dropvault.web.dao.MongoFileService.java
License:Open Source License
public Resource getParent(Resource res) { DBCollection files = mongo.getDataBase().getCollection("files"); DBObject resQuery = new BasicDBObject("_id", res.getId()); DBObject dbRes = files.findOne(resQuery); DBObject dbParent = files.findOne(new BasicDBObject("_id", dbRes.get("parent"))); return buildResource(dbParent); }
From source file:com.aperigeek.dropvault.web.dao.MongoFileService.java
License:Open Source License
public void move(String username, Resource source, String dest) throws ResourceNotFoundException { String[] path = dest.split("/"); Resource parent = getRootFolder(username); for (int i = 0; i < path.length - 1; i++) { parent = getChild(parent, path[i]); if (parent == null) { throw new ResourceNotFoundException(); }//w w w. j a v a 2s . c o m } DBCollection files = mongo.getDataBase().getCollection("files"); DBObject update = new BasicDBObject("$set", new BasicDBObjectBuilder().append("parent", parent.getId()) .append("name", path[path.length - 1]).get()); DBObject filter = new BasicDBObject("_id", source.getId()); DBObject current = files.findOne(filter); files.update(new BasicDBObject("_id", (ObjectId) current.get("parent")), new BasicDBObject("$set", new BasicDBObject("modificationDate", new Date()))); files.update(filter, update); files.update(new BasicDBObject("_id", parent.getId()), new BasicDBObject("$set", new BasicDBObject("modificationDate", new Date()))); }