use of com.mongodb.MongoClient in project vcell by virtualcell.
the class VCMongoDbDriver method storeBLOB.
public ObjectId storeBLOB(String blobName, String blobType, byte[] blob) {
try {
// send to MongoDB
if (m == null) {
String mongoDbHost = PropertyLoader.getRequiredProperty(PropertyLoader.mongodbHostInternal);
// default 27017
int mongoDbPort = Integer.parseInt(PropertyLoader.getRequiredProperty(PropertyLoader.mongodbPortInternal));
m = new MongoClient(mongoDbHost, mongoDbPort);
}
MongoDatabase db = m.getDatabase(mongoDbDatabaseName);
GridFSBucket gridFSBucket = GridFSBuckets.create(db);
Document metadata = new Document();
metadata.put("type", blobType);
long msgTime = System.currentTimeMillis();
metadata.put("inserttime", msgTime);
metadata.put("inserttime_nice", new Date(msgTime).toString());
GridFSUploadOptions options = new GridFSUploadOptions().chunkSizeBytes(1024).metadata(metadata);
ByteArrayInputStream in = new ByteArrayInputStream(blob);
ObjectId fileId = gridFSBucket.uploadFromStream(blobName, in, options);
return fileId;
} catch (Exception e) {
e.printStackTrace(System.out);
try {
if (m != null) {
m.close();
}
} catch (Exception e2) {
e2.printStackTrace(System.out);
} finally {
m = null;
}
throw new RuntimeException("failed to store BLOB named " + blobName + ": " + e.getMessage(), e);
}
}
use of com.mongodb.MongoClient in project vcell by virtualcell.
the class VCMongoDbDriver method getBLOB.
public byte[] getBLOB(ObjectId objectId) {
try {
if (m == null) {
String mongoDbHost = PropertyLoader.getRequiredProperty(PropertyLoader.mongodbHostInternal);
// default 27017
int mongoDbPort = Integer.parseInt(PropertyLoader.getRequiredProperty(PropertyLoader.mongodbPortInternal));
m = new MongoClient(mongoDbHost, mongoDbPort);
}
ByteArrayOutputStream streamToDownloadTo = new ByteArrayOutputStream();
MongoDatabase db = m.getDatabase(mongoDbDatabaseName);
GridFSBucket gridFSBucket = GridFSBuckets.create(db);
gridFSBucket.downloadToStream(objectId, streamToDownloadTo);
byte[] blob = streamToDownloadTo.toByteArray();
return blob;
} catch (Exception e) {
e.printStackTrace(System.out);
try {
if (m != null) {
m.close();
}
} catch (Exception e2) {
e2.printStackTrace(System.out);
} finally {
m = null;
}
throw new RuntimeException("failed to retrieve BLOB with ObjectId " + objectId.toHexString() + ": " + e.getMessage(), e);
}
}
use of com.mongodb.MongoClient in project cas by apereo.
the class MongoAuthenticationHandler method getAuthenticator.
@Override
protected Authenticator<UsernamePasswordCredentials> getAuthenticator(final Credential credential) {
final MongoClientURI uri = new MongoClientURI(this.mongoHostUri);
final MongoClient client = new MongoClient(uri);
LOGGER.info("Connected to MongoDb instance @ [{}] using database [{}]", uri.getHosts(), uri.getDatabase());
final MongoAuthenticator mongoAuthenticator = new MongoAuthenticator(client, this.attributes);
mongoAuthenticator.setUsersCollection(this.collectionName);
mongoAuthenticator.setUsersDatabase(uri.getDatabase());
mongoAuthenticator.setUsernameAttribute(this.usernameAttribute);
mongoAuthenticator.setPasswordAttribute(this.passwordAttribute);
mongoAuthenticator.setPasswordEncoder(this.mongoPasswordEncoder);
return mongoAuthenticator;
}
use of com.mongodb.MongoClient in project presto by prestodb.
the class MongoClientModule method createMongoSession.
@Singleton
@Provides
public static MongoSession createMongoSession(TypeManager typeManager, MongoClientConfig config) {
requireNonNull(config, "config is null");
MongoClientOptions.Builder options = MongoClientOptions.builder();
options.connectionsPerHost(config.getConnectionsPerHost()).connectTimeout(config.getConnectionTimeout()).socketTimeout(config.getSocketTimeout()).socketKeepAlive(config.getSocketKeepAlive()).sslEnabled(config.getSslEnabled()).maxWaitTime(config.getMaxWaitTime()).minConnectionsPerHost(config.getMinConnectionsPerHost()).readPreference(config.getReadPreference().getReadPreference()).writeConcern(config.getWriteConcern().getWriteConcern());
if (config.getRequiredReplicaSetName() != null) {
options.requiredReplicaSetName(config.getRequiredReplicaSetName());
}
MongoClient client = new MongoClient(config.getSeeds(), config.getCredentials(), options.build());
return new MongoSession(typeManager, client, config);
}
use of com.mongodb.MongoClient in project graylog2-server by Graylog2.
the class MongoConnectionImpl method connect.
/**
* Connect the instance.
*/
@Override
public synchronized Mongo connect() {
if (m == null) {
final String dbName = mongoClientURI.getDatabase();
if (isNullOrEmpty(dbName)) {
LOG.error("The MongoDB database name must not be null or empty (mongodb_uri was: {})", mongoClientURI);
throw new RuntimeException("MongoDB database name is missing.");
}
m = new MongoClient(mongoClientURI);
db = m.getDB(dbName);
db.setWriteConcern(WriteConcern.ACKNOWLEDGED);
mongoDatabase = m.getDatabase(dbName).withWriteConcern(WriteConcern.ACKNOWLEDGED);
}
try {
db.command("{ ping: 1 }");
} catch (MongoCommandException e) {
if (e.getCode() == 18) {
throw new MongoException("Couldn't connect to MongoDB. Please check the authentication credentials.", e);
} else {
throw new MongoException("Couldn't connect to MongoDB: " + e.getMessage(), e);
}
}
final Version mongoVersion = getMongoVersion(m.getDB("admin"));
if (mongoVersion != null && mongoVersion.lessThan(MINIMUM_MONGODB_VERSION)) {
LOG.warn("You're running MongoDB {} but Graylog requires at least MongoDB {}. Please upgrade.", mongoVersion, MINIMUM_MONGODB_VERSION);
}
return m;
}
Aggregations