Example usage for com.mongodb MongoClientOptions.Builder sslInvalidHostNameAllowed

List of usage examples for com.mongodb MongoClientOptions.Builder sslInvalidHostNameAllowed

Introduction

In this page you can find the example usage for com.mongodb MongoClientOptions.Builder sslInvalidHostNameAllowed.

Prototype

boolean sslInvalidHostNameAllowed

To view the source code for com.mongodb MongoClientOptions.Builder sslInvalidHostNameAllowed.

Click Source Link

Usage

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

License:Open Source License

private MongoClientOptions toMongoClientOptions(MongoClientConfiguration configuration) {
    MongoClientOptions.Builder optionsBuilder = MongoClientOptions.builder();
    if (configuration.isSslEnabled()) {
        optionsBuilder.sslEnabled(configuration.isSslEnabled());
        if (configuration.getSocketFactory() != null) {
            optionsBuilder.socketFactory(configuration.getSocketFactory());
            optionsBuilder.sslInvalidHostNameAllowed(configuration.isSslAllowInvalidHostnames());
        }/*from   w  w  w .j  a v a 2 s  . c o m*/
    }
    return optionsBuilder.build();
}

From source file:org.venice.piazza.servicecontroller.data.mongodb.accessors.MongoAccessor.java

License:Apache License

@PostConstruct
private void initialize() {
    try {/*from   w ww  .  j a  v  a 2 s .  co  m*/
        MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
        // Enable SSL if the `mongossl` Profile is enabled
        if (Arrays.stream(environment.getActiveProfiles()).anyMatch(env -> env.equalsIgnoreCase("mongossl"))) {
            builder.sslEnabled(true);
            builder.sslInvalidHostNameAllowed(true);
        }
        // If a username and password are provided, then associate these credentials with the connection
        if ((!StringUtils.isEmpty(DATABASE_USERNAME)) && (!StringUtils.isEmpty(DATABASE_CREDENTIAL))) {
            mongoClient = new MongoClient(new ServerAddress(DATABASE_HOST, DATABASE_PORT),
                    Arrays.asList(MongoCredential.createCredential(DATABASE_USERNAME, DATABASE_NAME,
                            DATABASE_CREDENTIAL.toCharArray())),
                    builder.threadsAllowedToBlockForConnectionMultiplier(mongoThreadMultiplier).build());
        } else {
            mongoClient = new MongoClient(new ServerAddress(DATABASE_HOST, DATABASE_PORT),
                    builder.threadsAllowedToBlockForConnectionMultiplier(mongoThreadMultiplier).build());
        }

    } catch (Exception exception) {
        LOGGER.error(String.format("Error connecting to MongoDB Instance. %s", exception.getMessage()),
                exception);

    }
}

From source file:org.wso2.carbon.datasource.reader.mongo.MongoDataSourceReaderUtil.java

License:Open Source License

public static MongoClient loadConfiguration(String xmlConfiguration) throws DataSourceException {
    ByteArrayInputStream baos = null;
    try {//from  www .ja  va2 s.c  o  m
        xmlConfiguration = CarbonUtils.replaceSystemVariablesInXml(xmlConfiguration);
        JAXBContext ctx = JAXBContext.newInstance(MongoDataSourceConfiguration.class);
        baos = new ByteArrayInputStream(xmlConfiguration.getBytes());
        MongoDataSourceConfiguration fileConfig = (MongoDataSourceConfiguration) ctx.createUnmarshaller()
                .unmarshal(baos);
        MongoClient result = null;
        MongoClientOptions.Builder builder = MongoClientOptions.builder();
        if (fileConfig.getUrl() != null) {
            if (fileConfig.getSslInvalidHostNameAllowed() != null) {
                builder.sslInvalidHostNameAllowed(fileConfig.getSslInvalidHostNameAllowed());
            }
            MongoClientURI uri = new MongoClientURI(fileConfig.getUrl(), builder);
            result = new MongoClient(uri);
        } else {
            List<ServerAddress> addressList = new ArrayList<ServerAddress>();
            if (fileConfig.getReplicaSetConfig() != null) {
                ServerAddress address1 = new ServerAddress(fileConfig.getReplicaSetConfig().getHost1(),
                        Integer.parseInt(fileConfig.getReplicaSetConfig().getPort1()));
                addressList.add(address1);
                if (fileConfig.getReplicaSetConfig().getHost2() != null
                        && fileConfig.getReplicaSetConfig().getPort2() != null) {
                    ServerAddress address2 = new ServerAddress(fileConfig.getReplicaSetConfig().getHost2(),
                            Integer.parseInt(fileConfig.getReplicaSetConfig().getPort2()));
                    addressList.add(address2);
                }
                if (fileConfig.getReplicaSetConfig().getHost3() != null
                        && fileConfig.getReplicaSetConfig().getPort3() != null) {
                    ServerAddress address3 = new ServerAddress(fileConfig.getReplicaSetConfig().getHost3(),
                            Integer.parseInt(fileConfig.getReplicaSetConfig().getPort3()));
                    addressList.add(address3);
                }
            } else {
                ServerAddress address = new ServerAddress(fileConfig.getHost(),
                        Integer.parseInt(fileConfig.getPort()));
                addressList.add(address);
            }
            MongoCredential credential = null;
            if (fileConfig.getWithSSL() != null) {
                builder.sslEnabled(fileConfig.getWithSSL());
            }
            if (fileConfig.getSslInvalidHostNameAllowed() != null) {
                builder.sslInvalidHostNameAllowed(fileConfig.getSslInvalidHostNameAllowed());
            }
            if (fileConfig.getAuthenticationMethodEnum() != null && fileConfig.getUsername() != null) {
                credential = createCredentials(fileConfig);
            }
            if (credential != null) {
                result = new MongoClient(addressList, Arrays.asList(new MongoCredential[] { credential }),
                        builder.build());
            } else {
                result = new MongoClient(addressList, builder.build());
            }
        }
        return result;
    } catch (Exception e) {
        throw new DataSourceException("Error loading Mongo Datasource configuration: " + e.getMessage(), e);
    } finally {
        if (baos != null) {
            try {
                baos.close();
            } catch (IOException ignore) {
                // ignore
            }
        }
    }
}