Example usage for com.mongodb MongoCredential createCredential

List of usage examples for com.mongodb MongoCredential createCredential

Introduction

In this page you can find the example usage for com.mongodb MongoCredential createCredential.

Prototype

public static MongoCredential createCredential(final String userName, final String database,
        final char[] password) 

Source Link

Document

Creates a MongoCredential instance with an unspecified mechanism.

Usage

From source file:anuncius.singleton.MongoHandler.java

public final MongoClient connectToDatabase() {
    if (secureMode) {
        //If MongoDB in secure mode, authentication is required.
        //... missing procedure
        System.out.println("Connecting to Mongo DB instance on " + MONGO_HOST + " at " + MONGO_PORT
                + " using user " + USERNAME);
        MongoCredential credential = MongoCredential.createCredential(USERNAME, database,
                PASSWORD.toCharArray());
        mongo = new MongoClient(new ServerAddress(MONGO_HOST), Arrays.asList(credential));
    } else {//from w ww . ja v a2 s.  c  om
        // Since 2.10.0, uses MongoClient
        System.out.println("Connecting to Mongo DB instance on " + MONGO_HOST + " at " + MONGO_PORT);
        mongo = new MongoClient(MONGO_HOST, MONGO_PORT);
    }
    return mongo;
}

From source file:com.ait.tooling.server.mongodb.support.spring.MongoDBDescriptor.java

License:Open Source License

private final boolean init() {
    try {/* w  ww .  j  a  va  2 s  .c o  m*/
        if (null == m_baseprop) {
            m_baseprop = MongoDBContextInstance.getMongoDBContextInstance().getMongoDBProvider()
                    .getMongoDBDefaultPropertiesBase();
        }
        final IPropertiesResolver prop = ServerContextInstance.getServerContextInstance()
                .getPropertiesResolver();

        setName(prop.getPropertyByName(m_baseprop + ".name"));

        setDefaultDB(prop.getPropertyByName(m_baseprop + ".db"));

        setReplicas(Boolean.valueOf(prop.getPropertyByName(m_baseprop + ".replicas", "false")));

        setCreateID(Boolean.valueOf(prop.getPropertyByName(m_baseprop + ".createid", "false")));

        final ArrayList<ServerAddress> addrlist = new ArrayList<ServerAddress>();

        for (String name : StringOps.requireTrimOrNull(prop.getPropertyByName(m_baseprop + ".host.list"))
                .split(",")) {
            name = StringOps.toTrimOrNull(name);

            if (null != name) {
                final String addr = StringOps
                        .requireTrimOrNull(prop.getPropertyByName(m_baseprop + ".host." + name + ".addr"));

                final String port = StringOps
                        .requireTrimOrNull(prop.getPropertyByName(m_baseprop + ".host." + name + ".port"));

                addrlist.add(new ServerAddress(addr, Integer.valueOf(port)));
            }
        }
        if (addrlist.isEmpty()) {
            throw new IllegalArgumentException("no MongoDB server address");
        }
        m_addrlist = addrlist;

        m_authlist = new ArrayList<MongoCredential>();

        final String temp = StringOps.toTrimOrNull(prop.getPropertyByName(m_baseprop + ".auth.list"));

        if (null != temp) {
            for (String name : temp.split(",")) {
                name = StringOps.toTrimOrNull(name);

                if (null != name) {
                    final String user = StringOps
                            .requireTrimOrNull(prop.getPropertyByName(m_baseprop + ".auth." + name + ".user"));

                    final String pass = StringOps
                            .requireTrimOrNull(prop.getPropertyByName(m_baseprop + ".auth." + name + ".pass"));

                    final String data = StringOps
                            .requireTrimOrNull(prop.getPropertyByName(m_baseprop + ".auth." + name + ".db"));

                    m_authlist.add(MongoCredential.createCredential(user, data, pass.toCharArray()));
                }
            }
        }
        if (null == getClientOptions()) {
            setClientOptions(MongoClientOptions.builder().connectionsPerHost(getConnectionPoolSize())
                    .threadsAllowedToBlockForConnectionMultiplier(getConnectionMultiplier())
                    .connectTimeout(getConnectionTimeout()).build());
        }
        m_doptions = new LinkedHashMap<String, IMongoDBOptions>();

        final String conf = StringOps.toTrimOrNull(prop.getPropertyByName(m_baseprop + ".dbconfig.list"));

        if (null != conf) {
            for (String name : conf.split(",")) {
                name = StringOps.toTrimOrNull(name);

                if (null != name) {
                    if (null == m_doptions.get(name)) {
                        boolean doid = isCreateID();

                        final ArrayList<IMongoDBCollectionOptions> list = new ArrayList<IMongoDBCollectionOptions>();

                        final String dbid = StringOps.toTrimOrNull(
                                prop.getPropertyByName(m_baseprop + ".dbconfig." + name + ".createid"));

                        if (null != dbid) {
                            doid = Boolean.valueOf(dbid);
                        }
                        final String base = m_baseprop + ".dbconfig." + name + ".collections";

                        final String cols = StringOps.toTrimOrNull(prop.getPropertyByName(base));

                        if (null != cols) {
                            for (String coln : cols.split(",")) {
                                coln = StringOps.toTrimOrNull(coln);

                                if (null != coln) {
                                    final String icid = StringOps.toTrimOrNull(
                                            prop.getPropertyByName(base + "." + coln + ".createid"));

                                    if (null != icid) {
                                        list.add(new MongoDBCollectionOptions(coln, Boolean.valueOf(icid)));
                                    } else {
                                        list.add(new MongoDBCollectionOptions(coln, doid));
                                    }
                                }
                            }
                        }
                        m_doptions.put(name, new MongoDBOptions(name, doid, list));
                    }
                }
            }
        }
        return true;
    } catch (Exception e) {
        logger.error("error initializing MongoDBDescriptor", e);

        return false;
    }
}

From source file:com.analogmountains.flume.MongoSink.java

License:Open Source License

private MongoCredential getCredential(Context context) {
    String user = context.getString(USER);
    String database = context.getString(DATABASE);
    String password = context.getString(PASSWORD);
    return MongoCredential.createCredential(user, database, password.toCharArray());
}

From source file:com.appdynamics.monitors.mongo.MongoDBMonitor.java

License:Apache License

private List<MongoCredential> getMongoCredentials(Configuration config) {
    List<MongoCredential> credentials = Lists.newArrayList();
    if (!Strings.isNullOrEmpty(config.getAdminDBUsername())
            && !Strings.isNullOrEmpty(config.getAdminDBPassword())) {
        MongoCredential adminDBCredential = MongoCredential.createCredential(config.getAdminDBUsername(),
                ADMIN_DB, getAdminDBPassword(config).toCharArray());
        credentials.add(adminDBCredential);
    } else {/*  w  w  w .  java2s .  c o  m*/
        logger.info("adminDBUsername and adminDBPassword in config.yml is null or empty");
    }
    return credentials;
}

From source file:com.bc.fiduceo.db.MongoDbDriver.java

License:Open Source License

@Override
public void open(BasicDataSource dataSource) throws SQLException {
    final String address = parseAddress(dataSource.getUrl());
    final String port = parsePort(dataSource.getUrl());
    final ServerAddress serverAddress = new ServerAddress(address, Integer.parseInt(port));

    final String username = dataSource.getUsername();
    final String password = dataSource.getPassword();
    if (StringUtils.isNotNullAndNotEmpty(password) && StringUtils.isNotNullAndNotEmpty(username)) {
        final MongoCredential credential = MongoCredential.createCredential(username, DATABASE_NAME,
                password.toCharArray());
        final List<MongoCredential> credentialsList = new ArrayList<>();
        credentialsList.add(credential);
        mongoClient = new MongoClient(serverAddress, credentialsList);
    } else {//www.j  a v  a2s  .  c  o  m
        mongoClient = new MongoClient(serverAddress);
    }
    database = mongoClient.getDatabase(DATABASE_NAME);
}

From source file:com.bluedragon.mongo.MongoDSN.java

License:Open Source License

public static MongoClient newClient(String server, String user, String pass, String db)
        throws UnknownHostException {

    MongoClientOptions options = MongoClientOptions.builder()
            .readPreference(ReadPreference.secondaryPreferred()).build();

    List<InetSocketAddress> serverList = AddrUtil.getAddresses(server);
    List<ServerAddress> addrs = new ArrayList<ServerAddress>();

    Iterator<InetSocketAddress> it = serverList.iterator();
    while (it.hasNext()) {
        InetSocketAddress isa = it.next();
        addrs.add(new ServerAddress(isa.getAddress(), isa.getPort()));
    }//ww  w .  j  a v  a2  s  . co  m

    if (user != null) {
        MongoCredential cred = MongoCredential.createCredential(user, db, pass.toCharArray());
        List<MongoCredential> creds = new ArrayList<MongoCredential>();
        creds.add(cred);

        return new MongoClient(addrs, creds, options);
    } else {
        return new MongoClient(addrs, options);
    }

}

From source file:com.callidusrobotics.droptables.configuration.MongoFactory.java

License:Open Source License

private MongoClient buildClient(Environment env, String username, String password) throws UnknownHostException {
    final MongoClient mongoClient;
    if (StringUtils.isBlank(username)) {
        mongoClient = new MongoClient(new ServerAddress(host, port));
    } else {/* w  w w  .j  a v  a2s . co m*/
        char[] passwordChars = password == null ? new char[0] : password.toCharArray();
        mongoClient = new MongoClient(new ServerAddress(host, port),
                Arrays.asList(MongoCredential.createCredential(username, dbName, passwordChars)));
    }

    env.lifecycle().manage(new Managed() {
        @Override
        public void start() throws Exception {
        }

        @Override
        public void stop() throws Exception {
            mongoClient.close();
        }
    });

    return mongoClient;
}

From source file:com.eclipsesource.connect.persistence.util.MongoDBClientFactory.java

License:Open Source License

private MongoDatabase doCreateDB(String host, String dbName, int port, String user, String password)
        throws UnknownHostException {
    if (client != null) {
        client.close();//from w ww.  ja  v  a2s .com
    }
    if (user != null && password != null) {
        MongoCredential credential = MongoCredential.createCredential(user, "admin", password.toCharArray());
        client = new MongoClient(new ServerAddress(host, port), Lists.newArrayList(credential));
    } else {
        client = new MongoClient(host, port);
    }
    return client.getDatabase(dbName);
}

From source file:com.effektif.mongo.MongoConfiguration.java

License:Apache License

public MongoConfiguration authentication(String username, String password, String database) {
    if (credentials == null) {
        credentials = new ArrayList<>();
    }/*from www.  j a  va2 s  . c om*/
    credentials.add(MongoCredential.createCredential(username, database, password.toCharArray()));
    return this;
}

From source file:com.eightkdata.mongowp.client.wrapper.MongoClientWrapper.java

License:Open Source License

private MongoCredential toMongoCredential(MongoAuthenticationConfiguration authConfiguration) {
    switch (authConfiguration.getMechanism()) {
    case cr:/*from www . j a  va 2s .  co  m*/
        return MongoCredential.createMongoCRCredential(authConfiguration.getUser(),
                authConfiguration.getSource(), authConfiguration.getPassword().toCharArray());
    case scram_sha1:
        return MongoCredential.createScramSha1Credential(authConfiguration.getUser(),
                authConfiguration.getSource(), authConfiguration.getPassword().toCharArray());
    case negotiate:
        return MongoCredential.createCredential(authConfiguration.getUser(), authConfiguration.getSource(),
                authConfiguration.getPassword().toCharArray());
    case x509:
        return MongoCredential.createMongoX509Credential(authConfiguration.getUser());
    default:
        throw new UnsupportedOperationException(
                "Authentication mechanism " + authConfiguration.getMechanism() + " not supported");
    }
}