public MongoDataStore(String uri) { MongoURI mongoUri = new MongoURI(uri); try { this.mongo = new Mongo(mongoUri); } catch (UnknownHostException e) { throw new MongoException("Unknown host", e); } this.db = mongo.getDB(mongoUri.getDatabase()); }
@Inject public MongoStore(SeyrenConfig seyrenConfig) { try { String uri = seyrenConfig.getMongoUrl(); MongoURI mongoUri = new MongoURI(uri); DB mongo = mongoUri.connectDB(); if (mongoUri.getUsername() != null) { mongo.authenticate(mongoUri.getUsername(), mongoUri.getPassword()); } this.mongo = mongo; } catch (Exception e) { throw new RuntimeException(e); } }
public MongoTemplate fetchMongoTemplate() { try { Mongo mongo; if (MONGO_MAP.containsKey(mongoUri)) { mongo = MONGO_MAP.get(mongoUri); } else { mongo = new Mongo(new MongoURI(mongoUri)); MONGO_MAP.put(mongoUri, mongo); } MongoURI uri = new MongoURI(mongoUri); return new MongoTemplate( new SimpleMongoDbFactory( mongo, uri.getDatabase(), new UserCredentials(uri.getUsername(), parseChars(uri.getPassword())))); } catch (Exception e) { logger.error("mongo db error ,uri={}", mongoUri, e); return null; } }
public static void main(String[] args) throws IOException, URISyntaxException, InterruptedException { final int port = System.getenv("PORT") != null ? Integer.valueOf(System.getenv("PORT")) : 8080; final URI baseUri = UriBuilder.fromUri("http://0.0.0.0/").port(port).build(); final Application application = Application.builder( ResourceConfig.builder().packages(BarServer.class.getPackage().getName()).build()) .build(); application.addModules(new JsonJacksonModule()); final HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(baseUri, application); httpServer .getServerConfiguration() .addHttpHandler(new StaticHttpHandler("src/main/webapp"), CONTENT_PATH); for (NetworkListener networkListener : httpServer.getListeners()) { if (System.getenv("FILE_CACHE_ENABLED") == null) { networkListener.getFileCache().setEnabled(false); } } Runtime.getRuntime() .addShutdownHook( new Thread() { @Override public void run() { httpServer.stop(); } }); MongoURI mongolabUri = new MongoURI( System.getenv("MONGOLAB_URI") != null ? System.getenv("MONGOLAB_URI") : "mongodb://127.0.0.1:27017/hello"); Mongo m = new Mongo(mongolabUri); mongoDB = m.getDB(mongolabUri.getDatabase()); if ((mongolabUri.getUsername() != null) && (mongolabUri.getPassword() != null)) { mongoDB.authenticate(mongolabUri.getUsername(), mongolabUri.getPassword()); } contentUrl = System.getenv("CONTENT_URL") != null ? System.getenv("CONTENT_URL") : CONTENT_PATH; Thread.currentThread().join(); }
/** * @deprecated use {@link #setMongoURI(Configuration, String, MongoClientURI)} instead * @param conf the Configuration * @param key the key for the setting * @param value the value for the setting */ @Deprecated public static void setMongoURI(final Configuration conf, final String key, final MongoURI value) { conf.set(key, value.toString()); // todo - verify you can toString a // URI object }
/** * @deprecated use {@link #getCollectionWithAuth(MongoClientURI, MongoClientURI)} instead * @param authURI the URI with which to authenticate * @param uri the MongoDB URI * @return the authenticated DBCollection */ @Deprecated public static DBCollection getCollectionWithAuth(final MongoURI uri, final MongoURI authURI) { return getCollectionWithAuth( new MongoClientURI(uri.toString()), new MongoClientURI(authURI.toString())); }
/** * @deprecated use {@link #getCollection(MongoClientURI)} * @param uri the MongoDB URI * @return the DBCollection in the URI */ @Deprecated public static DBCollection getCollection(final MongoURI uri) { return getCollection(new MongoClientURI(uri.toString())); }
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String email = request.getParameter("email"); String password = request.getParameter("password"); String verifypassword = request.getParameter("verifypassword"); Map<String, String> myResponse = new HashMap<String, String>(); PrintWriter out = response.getWriter(); if (email.matches( "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@" + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$")) // make sure email is properly // formatted { try { MongoURI mongoURI = new MongoURI(System.getenv("MONGOHQ_URL")); DB db = mongoURI.connectDB(); // instance of databse db.authenticate(mongoURI.getUsername(), mongoURI.getPassword()); // authenticates d // Set<string> accounts = db.getCollectionName("accounts"); // Mongo mongo = new Mongo("localhost", 27017); //creates new instance of mongo // DB db = mongo.getDB("fourup"); //gets fourup database DBCollection accounts = db.getCollection("accounts"); // creates collection for accounts BasicDBObject query = new BasicDBObject(); // creates a basic object named query query.put("email", email); // sets email to email DBCursor cursor = accounts.find(query); if (cursor.size() > 0) // check if email has already been registered { myResponse.put("Status", "Error"); myResponse.put("Error", "Account already exists using this email address."); } else // since email doesn't currently exist in DB, go ahead and register user { if (password.equals( verifypassword)) // check that both of the passwords entered match each other { BasicDBObject document = new BasicDBObject(); int salt = getSalt(); String hpass = passwrdHash(password, salt); document.put("email", email); document.put("salt", salt); document.put("password", hpass); // this is where we need to hash the password accounts.insert(document); myResponse.put("Status", "Sucess"); myResponse.put("Sucess", "Account has been Created"); AccountObject user = new AccountObject(email, hpass); // set session HttpSession session = request.getSession(); session.setAttribute("currentUser", email); // return cookie Cookie cookie = new Cookie("fourupCookie", email); // add the login information here response.addCookie(cookie); // redirect to homepage String message = "this is a test"; myResponse.put("html", "<html></html>"); response.setContentType("application/json"); response.setStatus(HttpServletResponse.SC_OK); // response.sendRedirect("index.html"); //should add check to index page for cookie with // login information } else { myResponse.put("Status", "Failed"); myResponse.put("Failed", "Passwords do not match."); } } } catch (MongoException e) { out.write(e.getMessage()); } } else { myResponse.put("Status", "Invalid"); myResponse.put( "Invalid", "The email address has not been entered correctly."); // should output error } String strResponse = new Gson().toJson(myResponse); response.getWriter().write(strResponse); response.getWriter().close(); }