Example usage for com.mongodb MongoSecurityException MongoSecurityException

List of usage examples for com.mongodb MongoSecurityException MongoSecurityException

Introduction

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

Prototype

public MongoSecurityException(final MongoCredential credential, final String message) 

Source Link

Document

Construct an instance

Usage

From source file:io.awacs.repository.MongoRepository.java

License:Apache License

@Override
public void init(Configuration configuration) throws InitializationException {
    try {//from w  w  w .  java  2s  .  c o  m
        String[] addrs = configuration.getString(ADDRESS, DEFAULT_ADDRESS).split(",");
        List<ServerAddress> addresses = new LinkedList<>();
        for (String addr : addrs) {
            String[] hostport = addr.split(":");
            addresses.add(new ServerAddress(hostport[0], Integer.valueOf(hostport[1])));
        }

        String db = configuration.getString(DB, DEFAULT_DB);

        List<MongoCredential> credentials = new LinkedList<>();
        String[] creds = configuration.getString(CREDENTIAL, DEFAULT_CREDENTIAL).split(",");
        if (!creds[0].equals("")) {
            for (String credential : creds) {
                int _1split = credential.indexOf(':');
                int _2split = credential.indexOf('@');
                if (_1split == -1 || _2split == -1) {
                    throw new MongoSecurityException(null, credential);
                }
                String username = credential.substring(0, _1split);
                String password = credential.substring(_1split + 1, _2split);
                String authDb = credential.substring(_2split + 1);
                credentials.add(MongoCredential.createCredential(username, authDb, password.toCharArray()));
            }
        }
        mongoConnection = new MongoConnection(addresses, credentials, db);
    } catch (Exception e) {
        e.printStackTrace();
        throw new InitializationException();
    }
}