List of usage examples for com.mongodb MongoClientURI getPassword
@Nullable public char[] getPassword()
From source file:com.zjy.mongo.splitter.MongoCollectionSplitter.java
License:Apache License
protected DB getConfigDB() { Mongo mongo;/* www.j av a2 s. c o m*/ MongoClientURI inputURI = MongoConfigUtil.getInputURI(getConfiguration()); MongoClientURI authURI = MongoConfigUtil.getAuthURI(getConfiguration()); final DBCollection inputCollection; if (authURI != null && authURI.getUsername() != null && authURI.getPassword() != null) { inputCollection = MongoConfigUtil.getCollectionWithAuth(inputURI, authURI); } else { inputCollection = MongoConfigUtil.getCollection(inputURI); } DB db = inputCollection.getDB(); mongo = db.getMongo(); if (authURI != null) { if (authURI.getUsername() != null && authURI.getPassword() != null) { authDB = mongo.getDB(authURI.getDatabase()); } } return mongo.getDB("config"); }
From source file:com.zjy.mongo.util.MongoConfigUtil.java
License:Apache License
/** * Get an authenticated DBCollection from a MongodB URI. * @param authURI the URI with which to authenticate * @param uri the MongoDB URI// ww w . j ava 2s.com * @return the authenticated DBCollection */ public static DBCollection getCollectionWithAuth(final MongoClientURI uri, final MongoClientURI authURI) { //Make sure auth uri is valid and actually has a username/pw to use if (authURI == null || authURI.getUsername() == null || authURI.getPassword() == null) { throw new IllegalArgumentException( "auth URI is empty or does not contain a valid username/password combination."); } DBCollection coll; try { Mongo mongo = getMongoClient(authURI); coll = mongo.getDB(uri.getDatabase()).getCollection(uri.getCollection()); return coll; } catch (Exception e) { throw new IllegalArgumentException("Couldn't connect and authenticate to get collection", e); } }
From source file:de.adorsys.oauth.tokenstore.mongodb.MongoDbProvider.java
License:Apache License
@SuppressWarnings("ReplaceAllDot") private String createLogUri(MongoClientURI clientURI) { StringBuilder sb = new StringBuilder(); String password = clientURI.getPassword() == null ? null : new String(clientURI.getPassword()).replaceAll(".", "x"); String username = clientURI.getUsername(); for (String host : clientURI.getHosts()) { sb.append("mongodb://"); if (username != null) { sb.append(username);/*from w w w. j a v a 2 s . c o m*/ if (password != null) { sb.append(':').append(password); } sb.append('@'); } sb.append(host).append(" "); } return sb.toString(); }
From source file:mx.org.cedn.avisosconagua.mongo.UpdateIssueDate.java
License:Open Source License
public static void main(String[] arg) throws Exception { MongoClientURI mongoClientURI = new MongoClientURI(System.getenv("MONGOHQ_URL")); MongoClient mongoClient = new MongoClient(mongoClientURI); DB mongoDB = mongoClient.getDB(mongoClientURI.getDatabase()); String GENERATED_COL = "GeneratedFiles"; SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm"); SimpleDateFormat isoformater = new SimpleDateFormat("YYYY-MM-dd HH:mm"); if (null != mongoClientURI.getUsername()) { mongoDB.authenticate(mongoClientURI.getUsername(), mongoClientURI.getPassword()); }/*from www .j av a 2 s . c om*/ DBCollection col = mongoDB.getCollection(GENERATED_COL); DBCursor cursor = col.find(); for (DBObject obj : cursor) { String date = (String) obj.get("issueDate"); Date fec = null; try { fec = sdf.parse(date); } catch (ParseException npe) { } if (null != fec) { date = isoformater.format(fec); DBObject act = col.findOne(obj); obj.put("issueDate", date); col.update(act, obj); } } }
From source file:org.alfresco.mongo.MongoClientFactory.java
License:Open Source License
/** * Create an instance of the factory. The URI given must not contain a database name or user/password details. * This forces the client URI to be an instance that can be shared between instances of this factory. * /* w ww. j ava 2s .c o m*/ * @param mongoClientURI the client URI, which <b>must not</b> reference a database, username or password * @param username the username to use when connecting (<tt>null</tt> allowed and empty string is ignored) * @param password the user password for the database (<tt>null</tt> allowed and empty string is ignored) * * @throws IllegalArgumentException if the arguments are null when not allowed or contain invalid information */ public MongoClientFactory(MongoClientURI mongoClientURI, String username, String password) throws UnknownHostException { validateMongoClientURI(mongoClientURI); if (mongoClientURI.getDatabase() != null) { throw new IllegalArgumentException( "The provided 'mongoClientURI' instance may not reference a specific database: " + MongoClientFactory.toStringSafe(mongoClientURI)); } else if (mongoClientURI.getUsername() != null) { throw new IllegalArgumentException( "The provided 'mongoClientURI' instance may not reference a specific username: " + MongoClientFactory.toStringSafe(mongoClientURI)); } else if (mongoClientURI.getPassword() != null) { throw new IllegalArgumentException( "The provided 'mongoClientURI' instance may not reference a specific password: " + MongoClientFactory.toStringSafe(mongoClientURI)); } // Reformat the URI if credentials were supplied if (username != null && username.length() > 0) { String userPwdCombo = username; if (password != null && password.length() > 0) { userPwdCombo = username + ":" + password; } String mongoClientURIstr = mongoClientURI.getURI().replace("mongodb://", "mongodb://" + userPwdCombo + "@"); mongoClientURI = new MongoClientURI(mongoClientURIstr); } // Construct the client mongoClient = new MongoClient(mongoClientURI); // Done if (logger.isInfoEnabled()) { logger.info("New MongoDB client created using URL: " + MongoClientFactory.toStringSafe(mongoClientURI)); } }
From source file:org.basex.modules.MongoDB.java
License:BSD License
/** * Mongodb connection with options.//from w w w .j ava 2s. c o m * @param url * @param options * @return * @throws QueryException */ public Str connection(final Str url, final Map options) throws QueryException { MongoClientURI uri = new MongoClientURI(url.toJava()); String handler = "mongoClient" + mongoClients.size(); try { MongoClient mongoClient = new MongoClient(uri); mongoClients.put(handler, mongoClient); return mongoConnect(handler, uri.getDatabase(), uri.getUsername(), uri.getPassword(), options); } catch (final MongoException ex) { throw MongoDBErrors.mongoExceptionError(ex); } catch (UnknownHostException ex) { throw MongoDBErrors.generalExceptionError(ex); } }
From source file:org.basex.modules.nosql.MongoDB.java
License:BSD License
/** * Mongodb connection with options.// w ww .j a va2 s .c o m * @param url mongodb url like: "mongodb://127.0.0.1:27017/basex" * @param options nosql options * @return Str * @throws QueryException query exception */ public Str connect(final Str url, final Map options) throws QueryException { MongoClientURI uri = new MongoClientURI(url.toJava()); String handler = "mongoClient" + mongoClients.size(); try { MongoClient mongoClient = new MongoClient(uri); mongoClients.put(handler, mongoClient); return mongoConnect(handler, uri.getDatabase(), uri.getUsername(), uri.getPassword(), options); } catch (final MongoException ex) { throw MongoDBErrors.mongoExceptionError(ex); } catch (UnknownHostException ex) { throw MongoDBErrors.generalExceptionError(ex); } }
From source file:org.netbeans.modules.mongodb.native_tools.MongoDumpExecAction.java
License:Open Source License
private void parseOptionsFromURI(MongoClientURI uri, Map<String, String> options) { if (uri.getUsername() != null && uri.getUsername().isEmpty() == false) { options.put(MongoDumpOptions.USERNAME, uri.getUsername()); }//from w w w .ja v a 2s. co m if (uri.getPassword() != null && uri.getPassword().length > 0) { options.put(MongoDumpOptions.PASSWORD, new String(uri.getPassword())); } if (uri.getHosts() != null && uri.getHosts().isEmpty() == false) { final String hostWithPort = uri.getHosts().get(0); final Pattern p = Pattern.compile("(.*)(:(\\d+))?"); final Matcher m = p.matcher(hostWithPort); if (m.matches()) { final String host = m.group(1); final String port = m.group(3); if (host.isEmpty() == false) { options.put(MongoDumpOptions.HOST, host); if (port != null) { options.put(MongoDumpOptions.PORT, port); } } } } if (uri.getDatabase() != null && uri.getDatabase().isEmpty() == false) { options.put(MongoDumpOptions.DB, uri.getDatabase()); } if (uri.getCollection() != null && uri.getCollection().isEmpty() == false) { options.put(MongoDumpOptions.COLLECTION, uri.getCollection()); } }
From source file:org.netbeans.modules.mongodb.native_tools.MongoRestoreExecAction.java
License:Open Source License
private void parseOptionsFromURI(MongoClientURI uri, Map<String, String> options) { if (uri.getUsername() != null && uri.getUsername().isEmpty() == false) { options.put(MongoRestoreOptions.USERNAME, uri.getUsername()); }//w ww.j av a 2 s. co m if (uri.getPassword() != null && uri.getPassword().length > 0) { options.put(MongoRestoreOptions.PASSWORD, new String(uri.getPassword())); } if (uri.getHosts() != null && uri.getHosts().isEmpty() == false) { final String hostWithPort = uri.getHosts().get(0); final Pattern p = Pattern.compile("(.*)(:(\\d+))?"); final Matcher m = p.matcher(hostWithPort); if (m.matches()) { final String host = m.group(1); final String port = m.group(3); if (host.isEmpty() == false) { options.put(MongoRestoreOptions.HOST, host); if (port != null) { options.put(MongoRestoreOptions.PORT, port); } } } } if (uri.getDatabase() != null && uri.getDatabase().isEmpty() == false) { options.put(MongoRestoreOptions.DB, uri.getDatabase()); } if (uri.getCollection() != null && uri.getCollection().isEmpty() == false) { options.put(MongoRestoreOptions.COLLECTION, uri.getCollection()); } }
From source file:org.netbeans.modules.mongodb.native_tools.MongoShellExecAction.java
License:Open Source License
private void parseOptionsFromURI(ProcessCreator.Builder builder, MongoClientURI uri) { if (uri.getUsername() != null && uri.getUsername().isEmpty() == false) { builder.option("--username", uri.getUsername()); }// ww w .j a va2 s. c o m if (uri.getPassword() != null && uri.getPassword().length > 0) { builder.option("--password", new String(uri.getPassword())); } if (uri.getHosts() != null && uri.getHosts().isEmpty() == false) { final String hostWithPort = uri.getHosts().get(0); final Pattern p = Pattern.compile("(.*)(:(\\d+))?"); final Matcher m = p.matcher(hostWithPort); if (m.matches()) { final String host = m.group(1); final String port = m.group(3); if (host.isEmpty() == false) { builder.option("--host", host); if (port != null) { builder.option("--port", port); } } } } }