Advertisement
Guest User

Untitled

a guest
May 8th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. import com.example.model.MongoAuthentication;
  2. import com.example.model.UsernamePasswordMongoAuthentication;
  3. import com.example.tenant.MongoConnectionCache;
  4. import com.mongodb.DB;
  5. import com.mongodb.Mongo;
  6. import com.mongodb.MongoClient;
  7. import com.mongodb.MongoCredential;
  8. import com.mongodb.ServerAddress;
  9. import org.slf4j.Logger;
  10. import org.slf4j.LoggerFactory;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.beans.factory.annotation.Qualifier;
  13. import org.springframework.data.mongodb.core.MongoDbUtils;
  14. import org.springframework.security.crypto.encrypt.TextEncryptor;
  15. import org.springframework.stereotype.Component;
  16. import org.springframework.util.Assert;
  17. import org.springframework.web.context.annotation.ApplicationScope;
  18.  
  19. import java.util.Collections;
  20. import javax.validation.constraints.NotNull;
  21.  
  22. /**
  23. * Caches initialized views of logical databases in a MongoDB cluster for faster access.
  24. */
  25. @Component
  26. @ApplicationScope
  27. class CachedMongoConnector implements MongoConnector {
  28.  
  29. private static final Logger LOG = LoggerFactory.getLogger(CachedMongoConnector.class);
  30.  
  31. private final TextEncryptor encryptor;
  32. private final MongoConnectionCache cache;
  33.  
  34. @Autowired
  35. CachedMongoConnector(@Qualifier("mongoTextEncryptor") TextEncryptor encryptor, MongoConnectionCache cache) {
  36. this.encryptor = encryptor;
  37. this.cache = cache;
  38. }
  39.  
  40. @Override
  41. public DB connect(@NotNull MongoConnectionConfig connectionConfig, @NotNull String dbName) {
  42. Assert.notNull(connectionConfig, "Mongo connection configuration must be specified");
  43. Assert.hasLength(connectionConfig.getHost(), "Mongo host must be specified");
  44. Assert.hasLength(dbName, "Mongo database to connect to must be specified");
  45. String configDbName = connectionConfig.getDbName();
  46. Assert.isTrue(
  47. dbName.equals(configDbName),
  48. String.format("Invalid Mongo database to connect to specified: \"%s\" vs. \"%s\"", dbName, configDbName)
  49. );
  50.  
  51. Mongo mongoConnection = cache.get(connectionConfig, config -> {
  52. Mongo newlyCreatedMongo = initMongoConnection(config);
  53. LOG.debug("Initialized connection {}", newlyCreatedMongo);
  54. return newlyCreatedMongo;
  55. });
  56.  
  57. return MongoDbUtils.getDB(mongoConnection, configDbName);
  58. }
  59.  
  60. private Mongo initMongoConnection(MongoConnectionConfig mongoConfig) {
  61. MongoAuthentication mongoAuth = mongoConfig.getAuth();
  62. switch (mongoAuth.getType()) {
  63. case NONE:
  64. return initUnauthenticatedMongoConnection(mongoConfig);
  65. case USERNAME_PASSWORD:
  66. return initAuthenticatedMongoConnection(mongoConfig);
  67. default:
  68. throw new IllegalArgumentException("Unsupported Mongo authentication type: " + mongoAuth.getType());
  69. }
  70. }
  71.  
  72. private Mongo initUnauthenticatedMongoConnection(MongoConnectionConfig mongoConfig) {
  73. return new MongoClient(mongoConfig.getHost(), mongoConfig.getPort());
  74. }
  75.  
  76. private Mongo initAuthenticatedMongoConnection(MongoConnectionConfig mongoConfig) {
  77. UsernamePasswordMongoAuthentication mongoAuth = (UsernamePasswordMongoAuthentication) mongoConfig.getAuth();
  78. String userName = mongoAuth.getUserName();
  79. String password = mongoAuth.getPassword();
  80. String decryptedUserName = encryptor.decrypt(userName);
  81. String decryptedPassword = encryptor.decrypt(password);
  82. MongoCredential credential = MongoCredential.createCredential(
  83. decryptedUserName,
  84. mongoConfig.getDbName(),
  85. decryptedPassword.toCharArray()
  86. );
  87. ServerAddress serverAddress = new ServerAddress(mongoConfig.getHost(), mongoConfig.getPort());
  88. return new MongoClient(serverAddress, Collections.singletonList(credential));
  89. }
  90.  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement