/** * submit a command to the database * * @param database - databse name * @param collection - collection name * @param query - query for doc search * @param operation - operation to be done in DB (insert, remove or update) * @param command - command * @param all - if true, all docs will be affected by the command * @return */ public String submitDBCommand( String database, String collection, String query, String operation, String command, Boolean all) { WriteResult result = null; try { DBCollection DBCollection = getCollection(database, collection); if (operation.equals("insert")) { JSONObject teste = new JSONObject(command); Object teste1 = JSON.parse(teste.toString()); DBObject doc = (DBObject) teste1; result = DBCollection.save(doc); } else if (operation.equals("update")) { DBObject queryDB = (DBObject) JSON.parse(query); DBObject doc = (DBObject) JSON.parse(command); if (all) { result = DBCollection.update(queryDB, doc, false, true); } else { result = DBCollection.update(queryDB, doc); } } else if (operation.equals("remove")) { DBObject queryDB = (DBObject) JSON.parse(query); DBObject doc = (DBObject) JSON.parse(command); if (all) { result = DBCollection.update(queryDB, doc, false, true); } else { result = DBCollection.update(queryDB, doc); } } return "Result: " + result.toString(); } catch (Exception e) { logger.logp( Level.SEVERE, TAG, "executeDBCommand", e.getMessage() + " database:" + database + " collection: " + collection + ", result: " + result.toString()); e.printStackTrace(); } return null; } // database, collection, query, operation, command, all
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { StringBuilder buffer = new StringBuilder(); BufferedReader reader = request.getReader(); String line; while ((line = reader.readLine()) != null) { buffer.append(line); } String data = buffer.toString(); System.out.println(data); JSONObject params = new JSONObject(data); String email = (String) params.get("email"); String password = (String) params.get("password"); MongoClientURI uri = new MongoClientURI("mongodb://*****:*****@ds011459.mlab.com:11459/farmville"); MongoClient client = new MongoClient(uri); DB db = client.getDB(uri.getDatabase()); DBCollection users = db.getCollection("logindetails"); BasicDBObject newDocument = new BasicDBObject(); newDocument.append("$set", new BasicDBObject().append("password", password)); BasicDBObject searchQuery = new BasicDBObject().append("email", email); WriteResult result = users.update(searchQuery, newDocument); response.setHeader("Access-Control-Allow-Origin", "*"); response.setHeader("Access-Control-Allow-Methods", "POST"); response.setHeader("Access-Control-Allow-Headers", "Content-Type"); response.setHeader("Access-Control-Max-Age", "86400"); response.getWriter().write(result.toString()); }
@Test public void testRenameAttribute() throws Throwable { logger.debug("Start testRenameAttribute"); try { logger.debug("Create river {}", getRiver()); String script = "ctx.document.score2 = ctx.document.score; delete ctx.document.score;"; super.createRiver( "/test/elasticsearch/plugin/river/mongodb/script/test-mongodb-river-with-script.json", getRiver(), String.valueOf(getMongoPort1()), String.valueOf(getMongoPort2()), String.valueOf(getMongoPort3()), getDatabase(), getCollection(), script, getIndex(), getDatabase()); String mongoDocument = copyToStringFromClasspath( "/test/elasticsearch/plugin/river/mongodb/script/test-simple-mongodb-document.json"); DBObject dbObject = (DBObject) JSON.parse(mongoDocument); WriteResult result = mongoCollection.insert(dbObject); Thread.sleep(wait); String id = dbObject.get("_id").toString(); logger.info("WriteResult: {}", result.toString()); refreshIndex(); ActionFuture<IndicesExistsResponse> response = getNode().client().admin().indices().exists(new IndicesExistsRequest(getIndex())); assertThat(response.actionGet().isExists(), equalTo(true)); SearchResponse sr = getNode() .client() .prepareSearch(getIndex()) .setQuery(fieldQuery("_id", id)) .execute() .actionGet(); logger.debug("SearchResponse {}", sr.toString()); long totalHits = sr.getHits().getTotalHits(); logger.debug("TotalHits: {}", totalHits); assertThat(totalHits, equalTo(1l)); assertThat(sr.getHits().getHits()[0].sourceAsMap().containsKey("score2"), equalTo(true)); mongoCollection.remove(dbObject); } catch (Throwable t) { logger.error("testRenameAttribute failed.", t); t.printStackTrace(); throw t; } finally { super.deleteRiver(); super.deleteIndex(); } }
@Test public void testIgnoreScript() throws Throwable { logger.debug("Start testIgnoreScript"); try { logger.debug("Create river {}", getRiver()); String script = "ctx.ignore = true;"; super.createRiver( "/test/elasticsearch/plugin/river/mongodb/script/test-mongodb-river-with-script.json", getRiver(), String.valueOf(getMongoPort1()), String.valueOf(getMongoPort2()), String.valueOf(getMongoPort3()), getDatabase(), getCollection(), script, getIndex(), getDatabase()); String mongoDocument = copyToStringFromClasspath( "/test/elasticsearch/plugin/river/mongodb/script/test-simple-mongodb-document.json"); DBObject dbObject = (DBObject) JSON.parse(mongoDocument); WriteResult result = mongoCollection.insert(dbObject); Thread.sleep(wait); logger.info("WriteResult: {}", result.toString()); refreshIndex(); ActionFuture<IndicesExistsResponse> response = getNode().client().admin().indices().exists(new IndicesExistsRequest(getIndex())); assertThat(response.actionGet().isExists(), equalTo(true)); CountResponse countResponse = getNode().client().count(countRequest(getIndex())).actionGet(); logger.info("Document count: {}", countResponse.getCount()); assertThat(countResponse.getCount(), equalTo(0l)); mongoCollection.remove(dbObject); } catch (Throwable t) { logger.error("testIgnoreScript failed.", t); t.printStackTrace(); throw t; } finally { super.deleteRiver(); super.deleteIndex(); } }
/** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().write("<br />Insert Test User<br /><br />"); response.setHeader("Access-Control-Allow-Origin", "*"); response.setHeader("Access-Control-Allow-Methods", "POST"); response.setHeader("Access-Control-Allow-Headers", "Content-Type"); response.setHeader("Access-Control-Max-Age", "86400"); StringBuilder buffer = new StringBuilder(); BufferedReader reader = request.getReader(); String line; while ((line = reader.readLine()) != null) { buffer.append(line); } String data = buffer.toString(); // System.out.println("Data ::: " + data); JSONObject params = (JSONObject) JSON.parse(data); BasicDBObject user1 = new BasicDBObject(params); for (Object key : params.keySet().toArray()) { user1.put(key.toString(), params.get(key)); } MongoClientURI uri = new MongoClientURI("mongodb://*****:*****@ds031611.mongolab.com:31611/testbeerdb"); MongoClient client = new MongoClient(uri); DB db = client.getDB(uri.getDatabase()); DBCollection users = db.getCollection("users"); WriteResult result = users.insert(user1); response.getWriter().write(result.toString()); // doGet(request, response); }
@Test public void initialImport() throws Throwable { logger.debug("Start InitialImport"); try { createDatabase(); DBObject dbObject1 = new BasicDBObject(ImmutableMap.of("name", "Richard")); WriteResult result1 = mongoCollection.insert(dbObject1); logger.info("WriteResult: {}", result1.toString()); Thread.sleep(wait); // Make sure we're starting out with the river not setup GetResponse statusResponse = getNode() .client() .prepareGet("_river", river, MongoDBRiver.STATUS_ID) .execute() .actionGet(); Assert.assertFalse( statusResponse.isExists(), "Expected no river but found one " + XContentMapValues.extractValue( MongoDBRiver.TYPE + "." + MongoDBRiver.STATUS_FIELD, statusResponse.getSourceAsMap())); // Setup the river createRiver(); Thread.sleep(wait); // Check that it did an initial import successfully ActionFuture<IndicesExistsResponse> response = getNode().client().admin().indices().exists(new IndicesExistsRequest(getIndex())); assertThat(response.actionGet().isExists(), equalTo(true)); Assert.assertEquals( Status.RUNNING, MongoDBRiverHelper.getRiverStatus(getNode().client(), river)); assertThat( getNode().client().count(countRequest(getIndex())).actionGet().getCount(), equalTo(1l)); // Check that it syncs the oplog DBObject dbObject2 = new BasicDBObject(ImmutableMap.of("name", "Ben")); WriteResult result2 = mongoCollection.insert(dbObject2); logger.info("WriteResult: {}", result2.toString()); Thread.sleep(wait); refreshIndex(); Assert.assertEquals( Status.RUNNING, MongoDBRiverHelper.getRiverStatus(getNode().client(), river)); assertThat( getNode().client().count(countRequest(getIndex())).actionGet().getCount(), equalTo(2l)); mongoCollection.remove(dbObject1, WriteConcern.REPLICAS_SAFE); Thread.sleep(wait); refreshIndex(); assertThat( getNode().client().count(countRequest(getIndex())).actionGet().getCount(), equalTo(1L)); } catch (Throwable t) { logger.error("InitialImport failed.", t); t.printStackTrace(); throw t; } finally { cleanUp(); } }