List of usage examples for com.mongodb MongoCredential createMongoCRCredential
@SuppressWarnings("deprecation") @Deprecated public static MongoCredential createMongoCRCredential(final String userName, final String database, final char[] password)
From source file:com.jaeksoft.searchlib.crawler.database.DatabaseCrawlMongoDb.java
License:Open Source License
MongoClient getMongoClient() throws URISyntaxException, UnknownHostException { String user = getUser();//from w w w .j a v a 2 s . c o m String password = getPassword(); URI uri = new URI(getUrl()); MongoCredential credential = null; if (!StringUtils.isEmpty(user) && !StringUtils.isEmpty(password)) { credential = MongoCredential.createMongoCRCredential(user, databaseName, password.toCharArray()); return new MongoClient(new ServerAddress(uri.getHost(), uri.getPort()), Arrays.asList(credential)); } return new MongoClient(new ServerAddress(uri.getHost(), uri.getPort())); }
From source file:com.norconex.collector.core.data.store.impl.mongo.MongoCrawlDataStore.java
License:Apache License
protected static DB buildMongoDB(String crawlerId, MongoConnectionDetails connDetails) { String dbName = MongoUtil.getDbNameOrGenerate(connDetails.getDatabaseName(), crawlerId); int port = connDetails.getPort(); if (port <= 0) { port = ServerAddress.defaultPort(); }//from w w w. j a va 2s . com try { ServerAddress server = new ServerAddress(connDetails.getHost(), port); List<MongoCredential> credentialsList = new ArrayList<MongoCredential>(); if (StringUtils.isNoneBlank(connDetails.getUsername())) { MongoCredential credential = MongoCredential.createMongoCRCredential(connDetails.getUsername(), dbName, connDetails.getPassword().toCharArray()); credentialsList.add(credential); } MongoClient client = new MongoClient(server, credentialsList); return client.getDB(dbName); } catch (UnknownHostException e) { throw new CrawlDataStoreException(e); } }
From source file:com.openbravo.data.loader.Session.java
License:Open Source License
public void connectMongoDB() { if (!m_sappuser.isEmpty() && !m_spassword.isEmpty() && !m_database.isEmpty()) { try {// w w w . j a va 2 s . co m MongoCredential credential = MongoCredential.createMongoCRCredential(m_sappuser, m_database, m_spassword.toCharArray()); m_mongoClient = new MongoClient(new ServerAddress(m_host, m_port), Arrays.asList(credential)); } catch (UnknownHostException ex) { } } else { try { m_mongoClient = new MongoClient(new ServerAddress(m_host, m_port)); } catch (UnknownHostException ex) { } } }
From source file:com.qwazr.connectors.MongoDbConnector.java
License:Apache License
@Override public void load(File data_directory) { List<ServerAddress> serverAddresses = new ArrayList(); for (MongoServerAddress server : servers) { ServerAddress serverAddress = server.port == null ? new ServerAddress(server.hostname) : new ServerAddress(server.hostname, server.port); serverAddresses.add(serverAddress); }//from w w w. j a v a 2 s. co m if (credentials == null || credentials.isEmpty()) { mongoClient = new MongoClient(serverAddresses); } else { List<MongoCredential> mongoCredentials = new ArrayList<MongoCredential>(credentials.size()); for (MongoDbCredential credential : credentials) mongoCredentials.add(MongoCredential.createMongoCRCredential(credential.username, credential.database, credential.password.toCharArray())); mongoClient = new MongoClient(serverAddresses, mongoCredentials); } }
From source file:com.qwazr.library.mongodb.MongoDbConnector.java
License:Apache License
@Override public void load() { final List<ServerAddress> serverAddresses = new ArrayList(); for (MongoServerAddress server : servers) { ServerAddress serverAddress = server.port == null ? new ServerAddress(server.hostname) : new ServerAddress(server.hostname, server.port); serverAddresses.add(serverAddress); }// www . jav a 2 s .c o m if (credentials == null || credentials.isEmpty()) { mongoClient = new MongoClient(serverAddresses); } else { List<MongoCredential> mongoCredentials = new ArrayList<MongoCredential>(credentials.size()); for (MongoDbCredential credential : credentials) mongoCredentials.add(MongoCredential.createMongoCRCredential(credential.username, credential.database, credential.password.toCharArray())); mongoClient = new MongoClient(serverAddresses, mongoCredentials); } }
From source file:com.redhat.lightblue.mongo.config.MongoConfiguration.java
License:Open Source License
public static MongoCredential credentialFromJson(ObjectNode node) { String userName = null;// w w w . j a v a 2s.c o m String password = null; String source = null; JsonNode xnode = node.get("mechanism"); if (xnode == null) { throw new IllegalArgumentException("mechanism is required in credentials"); } String mech = xnode.asText(); xnode = node.get("userName"); if (xnode != null) { userName = xnode.asText(); } xnode = node.get("password"); if (xnode != null) { password = xnode.asText(); } xnode = node.get("source"); if (xnode != null) { source = xnode.asText(); } MongoCredential cr; if ("GSSAPI_MECHANISM".equals(mech)) { cr = MongoCredential.createGSSAPICredential(userName); } else if ("MONGODB_CR_MECHANISM".equals(mech)) { cr = MongoCredential.createMongoCRCredential(userName, source, password == null ? null : password.toCharArray()); } else if ("MONGODB_X509_MECHANISM".equals(mech)) { cr = MongoCredential.createMongoX509Credential(userName); } else if ("PLAIN_MECHANISM".equals(mech)) { cr = MongoCredential.createPlainCredential(userName, source, password == null ? null : password.toCharArray()); } else { throw new IllegalArgumentException("invalid mechanism:" + mech + ", must be one of " + "GSSAPI_MECHANISM, MONGODB_CR_MECHANISM, " + "MONGODB_X5090_MECHANISM, or PLAIN_MECHANISM"); } return cr; }
From source file:com.softinstigate.restheart.db.MongoDBClientSingleton.java
License:Open Source License
private void setup() throws UnknownHostException { if (initialized) { List<ServerAddress> servers = new ArrayList<>(); List<MongoCredential> credentials = new ArrayList<>(); for (Map<String, Object> mongoServer : mongoServers) { Object mongoHost = mongoServer.get(Configuration.MONGO_HOST_KEY); Object mongoPort = mongoServer.get(Configuration.MONGO_PORT_KEY); if (mongoHost != null && mongoHost instanceof String && mongoPort != null && mongoPort instanceof Integer) { servers.add(new ServerAddress((String) mongoHost, (int) mongoPort)); }//from w ww .j a v a2s .c o m } if (mongoCredentials != null) { for (Map<String, Object> mongoCredential : mongoCredentials) { Object mongoAuthDb = mongoCredential.get(Configuration.MONGO_AUTH_DB_KEY); Object mongoUser = mongoCredential.get(Configuration.MONGO_USER_KEY); Object mongoPwd = mongoCredential.get(Configuration.MONGO_PASSWORD_KEY); if (mongoAuthDb != null && mongoAuthDb instanceof String && mongoUser != null && mongoUser instanceof String && mongoPwd != null && mongoPwd instanceof String) { credentials.add(MongoCredential.createMongoCRCredential((String) mongoUser, (String) mongoAuthDb, ((String) mongoPwd).toCharArray())); } } } MongoClientOptions opts = MongoClientOptions.builder().readPreference(ReadPreference.primaryPreferred()) .writeConcern(WriteConcern.ACKNOWLEDGED).build(); mongoClient = new MongoClient(servers, credentials, opts); } }
From source file:com.softwear.plugins.mongodb.MongoDBScriptObject.java
License:BSD License
public MongoClient js_getMongo(String _host, int _port, String userName, String database, String password) { try {/* w ww.jav a2 s.c om*/ if (mongoClient == null) { char[] charPassword = password.toCharArray(); MongoCredential credential = MongoCredential.createMongoCRCredential(userName, database, charPassword); mongoClient = new MongoClient(new ServerAddress(_host, _port), Arrays.asList(credential)); //localhost 27017 mongoClient.setWriteConcern(WriteConcern.UNACKNOWLEDGED); } else { return mongoClient; } } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (MongoException e) { // TODO Auto-generated catch block e.printStackTrace(); } return mongoClient; }
From source file:com.squid.kraken.v4.persistence.MongoDBHelper.java
License:Open Source License
private static MongoClient getMongo(String mongoName) { MongoClient mongoInstance;//from w w w . j a v a 2 s. c o m String mongoHostProp = KrakenConfig.getProperty("kraken.mongodb.host"); String[] mongoHostList = mongoHostProp.split(","); String mongoPortProp = KrakenConfig.getProperty("kraken.mongodb.port"); String[] mongoPortList = mongoPortProp.split(","); MongoClientOptions opt = MongoClientOptions.builder().connectionsPerHost(250).build(); List<ServerAddress> addrs = new ArrayList<ServerAddress>(); for (int i = 0; i < mongoHostList.length; i++) { int mongoPort = Integer.parseInt(mongoPortList[i]); addrs.add(new ServerAddress(mongoHostList[i], mongoPort)); } // Auth String mongoUser = KrakenConfig.getProperty("kraken.mongodb.user", true); String mongoPwd = KrakenConfig.getProperty("kraken.mongodb.password", true); logger.info("kraken.mongodb.dbname : " + mongoName); logger.info("kraken.mongodb.user : " + mongoUser); List<MongoCredential> credentials = new ArrayList<MongoCredential>(); if (mongoUser != null) { credentials.add(MongoCredential.createMongoCRCredential(mongoUser, mongoName, mongoPwd.toCharArray())); } mongoInstance = new MongoClient(addrs, credentials, opt); return mongoInstance; }
From source file:com.stratio.deep.mongodb.reader.MongoReader.java
License:Apache License
/** * Init void./*from w ww . j ava 2 s .c om*/ * * @param partition the partition */ public void init(Partition partition) { try { List<ServerAddress> addressList = new ArrayList<>(); for (String s : (List<String>) ((DeepPartition) partition).splitWrapper().getReplicas()) { addressList.add(new ServerAddress(s)); } //Credentials List<MongoCredential> mongoCredentials = new ArrayList<>(); if (mongoDeepJobConfig.getUsername() != null && mongoDeepJobConfig.getPassword() != null) { MongoCredential credential = MongoCredential.createMongoCRCredential( mongoDeepJobConfig.getUsername(), mongoDeepJobConfig.getDatabase(), mongoDeepJobConfig.getPassword().toCharArray()); mongoCredentials.add(credential); } mongoClient = new MongoClient(addressList, mongoCredentials); mongoClient.setReadPreference(ReadPreference.valueOf(mongoDeepJobConfig.getReadPreference())); db = mongoClient.getDB(mongoDeepJobConfig.getDatabase()); collection = db.getCollection(mongoDeepJobConfig.getCollection()); dbCursor = collection.find(generateFilterQuery((MongoPartition) partition), mongoDeepJobConfig.getDBFields()); } catch (UnknownHostException e) { throw new DeepExtractorInitializationException(e); } }