List of usage examples for com.mongodb MongoCredential createCredential
public static MongoCredential createCredential(final String userName, final String database, final char[] password)
From source file:org.log4mongo.MongoDbAppender.java
License:Apache License
/** * @see org.apache.log4j.AppenderSkeleton#activateOptions() */// w w w . j a va2 s .c o m @Override public void activateOptions() { try { // Close previous connections if reactivating if (mongo != null) { close(); } MongoCredential credentials = null; if (userName != null && userName.trim().length() > 0) { credentials = MongoCredential.createCredential(userName, databaseName, password.toCharArray()); password = null; } mongo = getMongo(getServerAddresses(hostname, port), (credentials != null) ? Arrays.asList(credentials) : null); MongoDatabase database = getDatabase(mongo, databaseName); setCollection(database.getCollection(collectionName)); initialized = true; } catch (Exception e) { errorHandler.error("Unexpected exception while initialising MongoDbAppender.", e, ErrorCode.GENERIC_FAILURE); } }
From source file:org.mongeez.dao.MongeezDao.java
License:Apache License
public MongeezDao(Mongo mongo, String databaseName, MongoAuth auth) { final List<MongoCredential> credentials = new LinkedList<MongoCredential>(); if (auth != null) { if (auth.getAuthDb() == null || auth.getAuthDb().equals(databaseName)) { credentials.add(MongoCredential.createCredential(auth.getUsername(), databaseName, auth.getPassword().toCharArray())); } else {/*from w w w .ja v a 2s . c om*/ credentials.add(MongoCredential.createCredential(auth.getUsername(), auth.getAuthDb(), auth.getPassword().toCharArray())); } } final MongoClient client = new MongoClient(mongo.getServerAddressList(), credentials); db = client.getDB(databaseName); configure(); }
From source file:org.mongolink.ConfigProperties.java
License:Open Source License
public Settings addSettings(Settings settings) { ArrayList<MongoCredential> credentials = Lists.newArrayList(); if (!Strings.isNullOrEmpty(getDBUser())) { MongoCredential credential = MongoCredential.createCredential(getDBUser(), "test", getDBPassword().toCharArray()); credentials.add(credential);//from w w w. ja v a 2s .co m } MongoClient mongoClient = new MongoClient(new ServerAddress(getDBHost(), getDBPort()), credentials); return settings.withClient(mongoClient).withDbName("test"); }
From source file:org.opentdc.mongo.AbstractMongodbServiceProvider.java
License:Open Source License
/** * Establish a connection to the MongoDB database. * @throws InternalServerErrorException if the connection could not be established */// ww w .j a va2 s . c om protected static void connect() throws InternalServerErrorException { ServerAddress _serverAddress = new ServerAddress(mongodbHost, mongodbPort); if (mongoClient != null) { logger.warning("re-connecting to an already open mongoDB connection"); } else { try { if (mongodbUser == null || mongodbUser.isEmpty() || mongodbPwd == null || mongodbPwd.isEmpty()) { logger.warning("access to MongoDB is insecure"); mongoClient = new MongoClient(_serverAddress); } else { MongoCredential _credential = MongoCredential.createCredential(mongodbUser, mongodbName, mongodbPwd.toCharArray()); mongoClient = new MongoClient(_serverAddress, Arrays.asList(_credential)); } // default WriteConcern.ACKNOWLEDGED // optionally, choose another WriteConcern eg. _mongoClient.setWriteConcern(WriteConcern.JOURNALED); } catch (MongoException _ex) { logger.info(_ex.getMessage()); logger.info(_ex.getStackTrace().toString()); throw new InternalServerErrorException( "could not establish a connection to the mongodb: MongoException in AbstractMongodbServiceProvider.constructor"); } } }
From source file:org.pentaho.mongo.wrapper.UsernamePasswordMongoClientWrapper.java
License:Open Source License
/** * Create a credentials object/* www .ja va2s .c o m*/ * * @return a configured MongoCredential object */ @Override public List<MongoCredential> getCredentialList() { List<MongoCredential> credList = new ArrayList<MongoCredential>(); String authDatabase = props.get(MongoProp.AUTH_DATABASE); credList.add(MongoCredential.createCredential(props.get(MongoProp.USERNAME), authDatabase == null || authDatabase.equals("") ? // Backward compatibility --Kaa props.get(MongoProp.DBNAME) : authDatabase, props.get(MongoProp.PASSWORD).toCharArray())); return credList; }
From source file:org.radarcns.config.ApplicationConfig.java
License:Apache License
/** * Returns the list of all known MongoDB credentials. * * @return a {@code List} of {@link MongoCredential} *///from ww w.j a v a2s . co m public MongoCredential getMongoDbCredentials() { return MongoCredential.createCredential(mongoUser.get("username"), mongoUser.get("database_name"), mongoUser.get("password").toCharArray()); }
From source file:org.radarcns.connect.mongodb.MongoWrapper.java
License:Apache License
private MongoCredential createCredentials(AbstractConfig config, String dbName) { String userName = config.getString(MONGO_USERNAME); String password = config.getString(MONGO_PASSWORD); if (isValid(userName) && isValid(password)) { return MongoCredential.createCredential(userName, dbName, password.toCharArray()); } else {/*from w w w .j a v a2 s . c o m*/ return null; } }
From source file:org.radarcns.mongodb.MongoWrapper.java
License:Apache License
private List<MongoCredential> createCredentials(AbstractConfig config) { String userName = config.getString(MONGO_USERNAME); String password = config.getString(MONGO_PASSWORD); if (userName != null && password != null) { return Collections .singletonList(MongoCredential.createCredential(userName, dbName, password.toCharArray())); } else {/*from w ww.ja va 2 s .c om*/ return Collections.emptyList(); } }
From source file:org.springframework.boot.autoconfigure.mongo.MongoClientFactory.java
License:Apache License
private MongoCredential getCredentials(MongoProperties properties) { if (!hasCustomCredentials()) { return null; }//from w w w .j av a 2 s . co m String username = properties.getUsername(); String database = getValue(properties.getAuthenticationDatabase(), properties.getMongoClientDatabase()); char[] password = properties.getPassword(); return MongoCredential.createCredential(username, database, password); }
From source file:org.springframework.boot.autoconfigure.mongo.MongoProperties.java
License:Apache License
/** * Creates a {@link MongoClient} using the given {@code options} and * {@code environment}. If the configured port is zero, the value of the * {@code local.mongo.port} property retrieved from the {@code environment} is used to * configure the client./*from www .j a v a2 s .co m*/ * * @param options the options * @param environment the environment * @return the Mongo client * @throws UnknownHostException if the configured host is unknown */ public MongoClient createMongoClient(MongoClientOptions options, Environment environment) throws UnknownHostException { try { if (hasCustomAddress() || hasCustomCredentials()) { if (options == null) { options = MongoClientOptions.builder().build(); } List<MongoCredential> credentials = null; if (hasCustomCredentials()) { String database = this.authenticationDatabase == null ? getMongoClientDatabase() : this.authenticationDatabase; credentials = Arrays .asList(MongoCredential.createCredential(this.username, database, this.password)); } String host = this.host == null ? "localhost" : this.host; int port = determinePort(environment); return new MongoClient(Arrays.asList(new ServerAddress(host, port)), credentials, options); } // The options and credentials are in the URI return new MongoClient(new MongoClientURI(this.uri, builder(options))); } finally { clearPassword(); } }