Search in sources :

Example 46 with BsonDocumentCodec

use of org.bson.codecs.BsonDocumentCodec in project mongo-java-driver by mongodb.

the class InternalStreamConnection method sendCommandMessageAsync.

private <T> void sendCommandMessageAsync(final int messageId, final Decoder<T> decoder, final SessionContext sessionContext, final SingleResultCallback<T> callback, final ByteBufferBsonOutput bsonOutput, final CommandEventSender commandEventSender, final boolean responseExpected) {
    sendMessageAsync(bsonOutput.getByteBuffers(), messageId, new SingleResultCallback<Void>() {

        @Override
        public void onResult(final Void result, final Throwable t) {
            bsonOutput.close();
            if (t != null) {
                commandEventSender.sendFailedEvent(t);
                callback.onResult(null, t);
            } else if (!responseExpected) {
                commandEventSender.sendSucceededEventForOneWayCommand();
                callback.onResult(null, null);
            } else {
                readAsync(MESSAGE_HEADER_LENGTH, new MessageHeaderCallback(new SingleResultCallback<ResponseBuffers>() {

                    @Override
                    public void onResult(final ResponseBuffers responseBuffers, final Throwable t) {
                        if (t != null) {
                            commandEventSender.sendFailedEvent(t);
                            callback.onResult(null, t);
                            return;
                        }
                        try {
                            updateSessionContext(sessionContext, responseBuffers);
                            boolean commandOk = isCommandOk(new BsonBinaryReader(new ByteBufferBsonInput(responseBuffers.getBodyByteBuffer())));
                            responseBuffers.reset();
                            if (!commandOk) {
                                MongoException commandFailureException = getCommandFailureException(responseBuffers.getResponseDocument(messageId, new BsonDocumentCodec()), description.getServerAddress());
                                commandEventSender.sendFailedEvent(commandFailureException);
                                throw commandFailureException;
                            }
                            commandEventSender.sendSucceededEvent(responseBuffers);
                            T result = getCommandResult(decoder, responseBuffers, messageId);
                            callback.onResult(result, null);
                        } catch (Throwable localThrowable) {
                            callback.onResult(null, localThrowable);
                        } finally {
                            responseBuffers.close();
                        }
                    }
                }));
            }
        }
    });
}
Also used : MongoException(com.mongodb.MongoException) BsonBinaryReader(org.bson.BsonBinaryReader) SingleResultCallback(com.mongodb.internal.async.SingleResultCallback) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput)

Example 47 with BsonDocumentCodec

use of org.bson.codecs.BsonDocumentCodec in project mongo-java-driver by mongodb.

the class AbstractClientSideEncryptionTest method setUp.

@Before
public void setUp() {
    assumeTrue("Client side encryption tests disabled", hasEncryptionTestsEnabled());
    assumeFalse("runOn requirements not satisfied", skipTest);
    assumeFalse("Skipping count tests", filename.startsWith("count."));
    assumeFalse(definition.getString("skipReason", new BsonString("")).getValue(), definition.containsKey("skipReason"));
    String databaseName = specDocument.getString("database_name").getValue();
    String collectionName = specDocument.getString("collection_name").getValue();
    collectionHelper = new CollectionHelper<BsonDocument>(new BsonDocumentCodec(), new MongoNamespace(databaseName, collectionName));
    MongoDatabase database = getMongoClient().getDatabase(databaseName);
    MongoCollection<BsonDocument> collection = database.getCollection(collectionName, BsonDocument.class);
    collection.drop();
    /* Create the collection for auto encryption. */
    if (specDocument.containsKey("json_schema")) {
        database.createCollection(collectionName, new CreateCollectionOptions().validationOptions(new ValidationOptions().validator(new BsonDocument("$jsonSchema", specDocument.getDocument("json_schema")))));
    }
    /* Insert data into the collection */
    List<BsonDocument> documents = new ArrayList<BsonDocument>();
    if (!data.isEmpty()) {
        for (BsonValue document : data) {
            documents.add(document.asDocument());
        }
        database.getCollection(collectionName, BsonDocument.class).insertMany(documents);
    }
    /* Insert data into the "keyvault.datakeys" key vault. */
    BsonArray data = specDocument.getArray("key_vault_data", new BsonArray());
    collection = getMongoClient().getDatabase("keyvault").getCollection("datakeys", BsonDocument.class).withWriteConcern(WriteConcern.MAJORITY);
    collection.drop();
    if (!data.isEmpty()) {
        documents = new ArrayList<BsonDocument>();
        for (BsonValue document : data) {
            documents.add(document.asDocument());
        }
        collection.insertMany(documents);
    }
    commandListener = new TestCommandListener();
    BsonDocument clientOptions = definition.getDocument("clientOptions");
    BsonDocument cryptOptions = clientOptions.getDocument("autoEncryptOpts");
    BsonDocument kmsProviders = cryptOptions.getDocument("kmsProviders");
    boolean bypassAutoEncryption = cryptOptions.getBoolean("bypassAutoEncryption", BsonBoolean.FALSE).getValue();
    Map<String, BsonDocument> namespaceToSchemaMap = new HashMap<String, BsonDocument>();
    if (cryptOptions.containsKey("schemaMap")) {
        BsonDocument autoEncryptMapDocument = cryptOptions.getDocument("schemaMap");
        for (Map.Entry<String, BsonValue> entries : autoEncryptMapDocument.entrySet()) {
            final BsonDocument autoEncryptOptionsDocument = entries.getValue().asDocument();
            namespaceToSchemaMap.put(entries.getKey(), autoEncryptOptionsDocument);
        }
    }
    Map<String, Object> extraOptions = new HashMap<String, Object>();
    if (cryptOptions.containsKey("extraOptions")) {
        BsonDocument extraOptionsDocument = cryptOptions.getDocument("extraOptions");
        if (extraOptionsDocument.containsKey("mongocryptdSpawnArgs")) {
            List<String> mongocryptdSpawnArgsValue = new ArrayList<String>();
            for (BsonValue cur : extraOptionsDocument.getArray("mongocryptdSpawnArgs")) {
                mongocryptdSpawnArgsValue.add(cur.asString().getValue());
            }
            extraOptions.put("mongocryptdSpawnArgs", mongocryptdSpawnArgsValue);
        }
        if (extraOptionsDocument.containsKey("mongocryptdBypassSpawn")) {
            extraOptions.put("mongocryptdBypassSpawn", extraOptionsDocument.getBoolean("mongocryptdBypassSpawn").getValue());
        }
        if (extraOptionsDocument.containsKey("mongocryptdURI")) {
            extraOptions.put("mongocryptdURI", extraOptionsDocument.getString("mongocryptdURI").getValue());
        }
    }
    Map<String, Map<String, Object>> kmsProvidersMap = new HashMap<>();
    for (String kmsProviderKey : kmsProviders.keySet()) {
        BsonDocument kmsProviderOptions = kmsProviders.get(kmsProviderKey).asDocument();
        Map<String, Object> kmsProviderMap = new HashMap<>();
        kmsProvidersMap.put(kmsProviderKey.startsWith("aws") ? "aws" : kmsProviderKey, kmsProviderMap);
        switch(kmsProviderKey) {
            case "aws":
                kmsProviderMap.put("accessKeyId", System.getProperty("org.mongodb.test.awsAccessKeyId"));
                kmsProviderMap.put("secretAccessKey", System.getProperty("org.mongodb.test.awsSecretAccessKey"));
                break;
            case "awsTemporary":
                kmsProviderMap.put("accessKeyId", System.getProperty("org.mongodb.test.tmpAwsAccessKeyId"));
                kmsProviderMap.put("secretAccessKey", System.getProperty("org.mongodb.test.tmpAwsSecretAccessKey"));
                kmsProviderMap.put("sessionToken", System.getProperty("org.mongodb.test.tmpAwsSessionToken"));
                break;
            case "awsTemporaryNoSessionToken":
                kmsProviderMap.put("accessKeyId", System.getProperty("org.mongodb.test.tmpAwsAccessKeyId"));
                kmsProviderMap.put("secretAccessKey", System.getProperty("org.mongodb.test.tmpAwsSecretAccessKey"));
                break;
            case "azure":
                kmsProviderMap.put("tenantId", System.getProperty("org.mongodb.test.azureTenantId"));
                kmsProviderMap.put("clientId", System.getProperty("org.mongodb.test.azureClientId"));
                kmsProviderMap.put("clientSecret", System.getProperty("org.mongodb.test.azureClientSecret"));
                break;
            case "gcp":
                kmsProviderMap.put("email", System.getProperty("org.mongodb.test.gcpEmail"));
                kmsProviderMap.put("privateKey", System.getProperty("org.mongodb.test.gcpPrivateKey"));
                break;
            case "kmip":
                kmsProviderMap.put("endpoint", System.getProperty("org.mongodb.test.kmipEndpoint", "localhost:5698"));
                break;
            case "local":
                kmsProviderMap.put("key", kmsProviderOptions.getBinary("key").getData());
                break;
            default:
                throw new UnsupportedOperationException("Unsupported KMS provider: " + kmsProviderKey);
        }
    }
    String keyVaultNamespace = "keyvault.datakeys";
    if (cryptOptions.containsKey("keyVaultNamespace")) {
        keyVaultNamespace = cryptOptions.getString("keyVaultNamespace").getValue();
    }
    createMongoClient(AutoEncryptionSettings.builder().keyVaultNamespace(keyVaultNamespace).kmsProviders(kmsProvidersMap).schemaMap(namespaceToSchemaMap).bypassAutoEncryption(bypassAutoEncryption).extraOptions(extraOptions).build(), commandListener);
    database = getDatabase(databaseName);
    helper = new JsonPoweredCrudTestHelper(description, database, database.getCollection("default", BsonDocument.class));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BsonString(org.bson.BsonString) ValidationOptions(com.mongodb.client.model.ValidationOptions) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) TestCommandListener(com.mongodb.internal.connection.TestCommandListener) MongoNamespace(com.mongodb.MongoNamespace) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray) CreateCollectionOptions(com.mongodb.client.model.CreateCollectionOptions) HashMap(java.util.HashMap) Map(java.util.Map) BsonValue(org.bson.BsonValue) Before(org.junit.Before)

Example 48 with BsonDocumentCodec

use of org.bson.codecs.BsonDocumentCodec in project mongo-java-driver by mongodb.

the class AbstractUnifiedTest method shouldPassAllOutcomes.

@Test
public void shouldPassAllOutcomes() {
    try {
        executeOperations(definition.getArray("operations"), false);
    } finally {
        closeAllSessions();
        shutdownAllExecutors();
    }
    if (definition.containsKey("expectations")) {
        List<CommandEvent> expectedEvents = getExpectedEvents(definition.getArray("expectations"), databaseName, null);
        List<CommandEvent> events = commandListener.getCommandStartedEvents();
        assertTrue("Actual number of events is less than expected number of events", events.size() >= expectedEvents.size());
        assertEventsEquality(expectedEvents, events.subList(0, expectedEvents.size()), lsidMap);
    }
    BsonDocument expectedOutcome = definition.getDocument("outcome", new BsonDocument());
    if (expectedOutcome.containsKey("collection")) {
        BsonDocument collectionDocument = expectedOutcome.getDocument("collection");
        List<BsonDocument> collectionData;
        if (collectionDocument.containsKey("name")) {
            collectionData = new CollectionHelper<Document>(new DocumentCodec(), new MongoNamespace(databaseName, collectionDocument.getString("name").getValue())).find(new BsonDocumentCodec());
        } else {
            collectionData = collectionHelper.find(new BsonDocumentCodec());
        }
        assertEquals(expectedOutcome.getDocument("collection").getArray("data").getValues(), collectionData);
    }
}
Also used : BsonDocument(org.bson.BsonDocument) CommandEvent(com.mongodb.event.CommandEvent) DocumentCodec(org.bson.codecs.DocumentCodec) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) CollectionHelper(com.mongodb.client.test.CollectionHelper) MongoNamespace(com.mongodb.MongoNamespace) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) Test(org.junit.Test) ClusterFixture.isDataLakeTest(com.mongodb.ClusterFixture.isDataLakeTest)

Example 49 with BsonDocumentCodec

use of org.bson.codecs.BsonDocumentCodec in project mongo-java-driver by mongodb.

the class AbstractChangeStreamsTest method setUp.

@Before
public void setUp() {
    assumeFalse(skipTest);
    CollectionHelper.dropDatabase(namespace.getDatabaseName(), WriteConcern.MAJORITY);
    collectionHelper = new CollectionHelper<BsonDocument>(new BsonDocumentCodec(), namespace);
    collectionHelper.drop();
    collectionHelper.create();
    if (namespace2 != null) {
        CollectionHelper.dropDatabase(namespace2.getDatabaseName(), WriteConcern.MAJORITY);
        CollectionHelper<BsonDocument> collectionHelper2 = new CollectionHelper<BsonDocument>(new BsonDocumentCodec(), namespace2);
        collectionHelper2.drop();
        collectionHelper2.create();
    }
    if (definition.containsKey("failPoint")) {
        collectionHelper.runAdminCommand(definition.getDocument("failPoint"));
    }
    commandListener = new TestCommandListener();
    mongoClient = createMongoClient(getMongoClientSettingsBuilder().addCommandListener(commandListener).build());
}
Also used : BsonDocument(org.bson.BsonDocument) TestCommandListener(com.mongodb.internal.connection.TestCommandListener) CollectionHelper(com.mongodb.client.test.CollectionHelper) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) Before(org.junit.Before)

Example 50 with BsonDocumentCodec

use of org.bson.codecs.BsonDocumentCodec in project mongo-java-driver by mongodb.

the class TestInternalConnection method sendAndReceive.

@Override
public <T> T sendAndReceive(final CommandMessage message, final Decoder<T> decoder, final SessionContext sessionContext, final RequestContext requestContext) {
    ByteBufferBsonOutput bsonOutput = new ByteBufferBsonOutput(this);
    try {
        message.encode(bsonOutput, sessionContext);
        sendMessage(bsonOutput.getByteBuffers(), message.getId());
    } finally {
        bsonOutput.close();
    }
    ResponseBuffers responseBuffers = receiveMessage(message.getId());
    try {
        boolean commandOk = isCommandOk(new BsonBinaryReader(new ByteBufferBsonInput(responseBuffers.getBodyByteBuffer())));
        responseBuffers.reset();
        if (!commandOk) {
            throw getCommandFailureException(getResponseDocument(responseBuffers, message, new BsonDocumentCodec()), description.getServerAddress());
        }
        return new ReplyMessage<T>(responseBuffers, decoder, message.getId()).getDocuments().get(0);
    } finally {
        responseBuffers.close();
    }
}
Also used : BsonBinaryReader(org.bson.BsonBinaryReader) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput)

Aggregations

BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)56 BsonDocument (org.bson.BsonDocument)29 MongoNamespace (com.mongodb.MongoNamespace)9 BsonBinaryReader (org.bson.BsonBinaryReader)8 BasicOutputBuffer (org.bson.io.BasicOutputBuffer)8 BsonBinaryWriter (org.bson.BsonBinaryWriter)5 ByteBufferBsonInput (org.bson.io.ByteBufferBsonInput)5 JsonReader (org.bson.json.JsonReader)5 Test (org.junit.Test)5 CollectionHelper (com.mongodb.client.test.CollectionHelper)4 NoOpFieldNameValidator (com.mongodb.internal.validator.NoOpFieldNameValidator)4 StringWriter (java.io.StringWriter)4 BsonArray (org.bson.BsonArray)4 BsonString (org.bson.BsonString)4 JsonWriter (org.bson.json.JsonWriter)4 ByteBuffer (java.nio.ByteBuffer)3 BsonDocumentWriter (org.bson.BsonDocumentWriter)3 Before (org.junit.Before)3 MongoClientSettings (com.mongodb.MongoClientSettings)2 MongoCommandException (com.mongodb.MongoCommandException)2