Example usage for com.mongodb MongoCredential PLAIN_MECHANISM

List of usage examples for com.mongodb MongoCredential PLAIN_MECHANISM

Introduction

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

Prototype

String PLAIN_MECHANISM

To view the source code for com.mongodb MongoCredential PLAIN_MECHANISM.

Click Source Link

Document

The PLAIN mechanism.

Usage

From source file:me.konglong.momei.mongodb.config.MongoCredentialPropertyEditor.java

License:Apache License

@Override
public void setAsText(String text) throws IllegalArgumentException {

    if (!StringUtils.hasText(text)) {
        return;//from  w  ww  .  j ava  2s .  c  o  m
    }

    List<MongoCredential> credentials = new ArrayList<MongoCredential>();

    for (String credentialString : extractCredentialsString(text)) {

        String[] userNameAndPassword = extractUserNameAndPassword(credentialString);
        String database = extractDB(credentialString);
        Properties options = extractOptions(credentialString);

        if (!options.isEmpty()) {

            if (options.containsKey(AUTH_MECHANISM_KEY)) {

                String authMechanism = options.getProperty(AUTH_MECHANISM_KEY);

                if (MongoCredential.GSSAPI_MECHANISM.equals(authMechanism)) {

                    verifyUserNamePresent(userNameAndPassword);
                    credentials.add(MongoCredential.createGSSAPICredential(userNameAndPassword[0]));
                } else if (MongoCredential.MONGODB_CR_MECHANISM.equals(authMechanism)) {

                    verifyUsernameAndPasswordPresent(userNameAndPassword);
                    verifyDatabasePresent(database);
                    credentials.add(MongoCredential.createMongoCRCredential(userNameAndPassword[0], database,
                            userNameAndPassword[1].toCharArray()));
                } else if (MongoCredential.MONGODB_X509_MECHANISM.equals(authMechanism)) {

                    verifyUserNamePresent(userNameAndPassword);
                    credentials.add(MongoCredential.createMongoX509Credential(userNameAndPassword[0]));
                } else if (MongoCredential.PLAIN_MECHANISM.equals(authMechanism)) {

                    verifyUsernameAndPasswordPresent(userNameAndPassword);
                    verifyDatabasePresent(database);
                    credentials.add(MongoCredential.createPlainCredential(userNameAndPassword[0], database,
                            userNameAndPassword[1].toCharArray()));
                } else if (MongoCredential.SCRAM_SHA_1_MECHANISM.equals(authMechanism)) {

                    verifyUsernameAndPasswordPresent(userNameAndPassword);
                    verifyDatabasePresent(database);
                    credentials.add(MongoCredential.createScramSha1Credential(userNameAndPassword[0], database,
                            userNameAndPassword[1].toCharArray()));
                } else {
                    throw new IllegalArgumentException(String.format(
                            "Cannot create MongoCredentials for unknown auth mechanism '%s'!", authMechanism));
                }
            }
        } else {

            verifyUsernameAndPasswordPresent(userNameAndPassword);
            verifyDatabasePresent(database);
            credentials.add(MongoCredential.createCredential(userNameAndPassword[0], database,
                    userNameAndPassword[1].toCharArray()));
        }
    }

    setValue(credentials);
}