use of org.bson.BsonString in project mongo-java-driver by mongodb.
the class IdHoldingBsonWriter method writeString.
@Override
public void writeString(final String name, final String value) {
setCurrentFieldName(name);
addBsonValue(() -> new BsonString(value), () -> getIdBsonWriter().writeString(name, value));
super.writeString(name, value);
}
use of org.bson.BsonString in project mongo-java-driver by mongodb.
the class UnifiedGridFSHelper method executeDownload.
public OperationResult executeDownload(final BsonDocument operation) {
GridFSBucket bucket = entities.getBucket(operation.getString("object").getValue());
BsonDocument arguments = operation.getDocument("arguments");
BsonValue id = arguments.get("id");
if (arguments.size() > 1) {
throw new UnsupportedOperationException("Unexpected arguments");
}
requireNonNull(id);
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bucket.downloadToStream(id, baos);
return OperationResult.of(new BsonString(HexUtils.toHex(baos.toByteArray())));
} catch (Exception e) {
return OperationResult.of(e);
}
}
use of org.bson.BsonString in project mongo-java-driver by mongodb.
the class JsonPoweredCrudTestHelper method getDownloadResult.
BsonDocument getDownloadResult(final BsonDocument collectionOptions, final BsonDocument arguments, @Nullable final ClientSession clientSession) throws IOException {
ByteArrayOutputStream outputStream = null;
try {
outputStream = new ByteArrayOutputStream();
gridFSBucket.downloadToStream(arguments.getObjectId("id").getValue(), outputStream);
} finally {
outputStream.close();
}
return toResult("result", new BsonString(Hex.encode(outputStream.toByteArray()).toLowerCase()));
}
use of org.bson.BsonString in project mongo-java-driver by mongodb.
the class JsonPoweredCrudTestHelper method getOperationResults.
BsonDocument getOperationResults(final BsonDocument operation, @Nullable final ClientSession clientSession) {
BsonDocument collectionOptions = operation.getDocument("collectionOptions", new BsonDocument());
BsonDocument arguments = operation.getDocument("arguments", new BsonDocument());
String methodName = createMethodName(operation.getString("name").getValue(), operation.getString("object", new BsonString("")).getValue());
try {
Method method = getClass().getDeclaredMethod(methodName, BsonDocument.class, BsonDocument.class, ClientSession.class);
return (BsonDocument) method.invoke(this, collectionOptions, arguments, clientSession);
} catch (NoSuchMethodException e) {
throw new UnsupportedOperationException("No handler for operation " + methodName);
} catch (InvocationTargetException e) {
if (e.getTargetException() instanceof MongoException) {
throw (MongoException) e.getTargetException();
}
throw (RuntimeException) e.getTargetException();
} catch (IllegalAccessException e) {
throw new UnsupportedOperationException("Invalid handler access for operation " + methodName);
}
}
use of org.bson.BsonString in project mongo-java-driver by mongodb.
the class ServerDiscoveryAndMonitoringProseTests method testConnectionPoolManagement.
/**
* See
* <a href="https://github.com/mongodb/specifications/blob/master/source/server-discovery-and-monitoring/server-discovery-and-monitoring-tests.rst#connection-pool-management">Connection Pool Management</a>.
*/
@Test
@Ignore
@SuppressWarnings("try")
public void testConnectionPoolManagement() throws InterruptedException {
assumeTrue(serverVersionAtLeast(4, 3));
assumeFalse(isServerlessTest());
BlockingQueue<Object> events = new LinkedBlockingQueue<>();
ServerMonitorListener serverMonitorListener = new ServerMonitorListener() {
@Override
public void serverHeartbeatSucceeded(final ServerHeartbeatSucceededEvent event) {
put(events, event);
}
@Override
public void serverHeartbeatFailed(final ServerHeartbeatFailedEvent event) {
put(events, event);
}
};
ConnectionPoolListener connectionPoolListener = new ConnectionPoolListener() {
@Override
public void connectionPoolReady(final ConnectionPoolReadyEvent event) {
put(events, event);
}
@Override
public void connectionPoolCleared(final ConnectionPoolClearedEvent event) {
put(events, event);
}
};
String appName = "SDAMPoolManagementTest";
MongoClientSettings clientSettings = getMongoClientSettingsBuilder().applicationName(appName).applyToClusterSettings(ClusterFixture::setDirectConnection).applyToServerSettings(builder -> builder.heartbeatFrequency(100, TimeUnit.MILLISECONDS).addServerMonitorListener(serverMonitorListener)).applyToConnectionPoolSettings(builder -> builder.addConnectionPoolListener(connectionPoolListener)).build();
try (MongoClient unused = MongoClients.create(clientSettings)) {
/* Note that ServerHeartbeatSucceededEvent type is sometimes allowed but never required.
* This is because DefaultServerMonitor does not send such events in situations when a server check happens as part
* of a connection handshake. */
assertPoll(events, ServerHeartbeatSucceededEvent.class, singleton(ConnectionPoolReadyEvent.class));
configureFailPoint(new BsonDocument().append("configureFailPoint", new BsonString("failCommand")).append("mode", new BsonDocument().append("times", new BsonInt32(2))).append("data", new BsonDocument().append("failCommands", new BsonArray(asList(new BsonString("isMaster"), new BsonString("hello")))).append("errorCode", new BsonInt32(1234)).append("appName", new BsonString(appName))));
assertPoll(events, ServerHeartbeatSucceededEvent.class, new HashSet<>(asList(ServerHeartbeatFailedEvent.class, ConnectionPoolClearedEvent.class)));
assertPoll(events, null, new HashSet<>(asList(ServerHeartbeatSucceededEvent.class, ConnectionPoolReadyEvent.class)));
} finally {
disableFailPoint("failCommand");
}
}
Aggregations