Search in sources :

Example 1 with BsonBinaryWriterSettings

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

the class CryptConnection method command.

@Override
public <T> T command(final String database, final BsonDocument command, final FieldNameValidator commandFieldNameValidator, final ReadPreference readPreference, final Decoder<T> commandResultDecoder, final SessionContext sessionContext, @Nullable final ServerApi serverApi, final RequestContext requestContext, final boolean responseExpected, @Nullable final SplittablePayload payload, @Nullable final FieldNameValidator payloadFieldNameValidator) {
    if (serverIsLessThanVersionFourDotTwo(wrapped.getDescription())) {
        throw new MongoClientException("Auto-encryption requires a minimum MongoDB version of 4.2");
    }
    BasicOutputBuffer bsonOutput = new BasicOutputBuffer();
    BsonBinaryWriter bsonBinaryWriter = new BsonBinaryWriter(new BsonWriterSettings(), new BsonBinaryWriterSettings(getDescription().getMaxDocumentSize()), bsonOutput, getFieldNameValidator(payload, commandFieldNameValidator, payloadFieldNameValidator));
    BsonWriter writer = payload == null ? bsonBinaryWriter : new SplittablePayloadBsonWriter(bsonBinaryWriter, bsonOutput, createSplittablePayloadMessageSettings(), payload, MAX_SPLITTABLE_DOCUMENT_SIZE);
    getEncoder(command).encode(writer, command, EncoderContext.builder().build());
    RawBsonDocument encryptedCommand = crypt.encrypt(database, new RawBsonDocument(bsonOutput.getInternalBuffer(), 0, bsonOutput.getSize()));
    RawBsonDocument encryptedResponse = wrapped.command(database, encryptedCommand, commandFieldNameValidator, readPreference, new RawBsonDocumentCodec(), sessionContext, serverApi, requestContext, responseExpected, null, null);
    RawBsonDocument decryptedResponse = crypt.decrypt(encryptedResponse);
    BsonBinaryReader reader = new BsonBinaryReader(decryptedResponse.getByteBuffer().asNIO());
    return commandResultDecoder.decode(reader, DecoderContext.builder().build());
}
Also used : MongoClientException(com.mongodb.MongoClientException) SplittablePayloadBsonWriter(com.mongodb.internal.connection.SplittablePayloadBsonWriter) RawBsonDocumentCodec(org.bson.codecs.RawBsonDocumentCodec) BsonBinaryReader(org.bson.BsonBinaryReader) SplittablePayloadBsonWriter(com.mongodb.internal.connection.SplittablePayloadBsonWriter) BsonWriter(org.bson.BsonWriter) RawBsonDocument(org.bson.RawBsonDocument) BsonBinaryWriter(org.bson.BsonBinaryWriter) BsonBinaryWriterSettings(org.bson.BsonBinaryWriterSettings) BsonWriterSettings(org.bson.BsonWriterSettings) BasicOutputBuffer(org.bson.io.BasicOutputBuffer)

Example 2 with BsonBinaryWriterSettings

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

the class RequestMessage method addDocument.

@SuppressWarnings("unchecked")
private <T> void addDocument(final T obj, final Encoder<T> encoder, final EncoderContext encoderContext, final BsonOutput bsonOutput, final FieldNameValidator validator, final int maxDocumentSize, final List<BsonElement> extraElements) {
    BsonBinaryWriter bsonBinaryWriter = new BsonBinaryWriter(new BsonWriterSettings(), new BsonBinaryWriterSettings(maxDocumentSize), bsonOutput, validator);
    BsonWriter bsonWriter = extraElements == null ? bsonBinaryWriter : new ElementExtendingBsonWriter(bsonBinaryWriter, extraElements);
    encoder.encode(bsonWriter, obj, encoderContext);
}
Also used : BsonWriter(org.bson.BsonWriter) BsonBinaryWriter(org.bson.BsonBinaryWriter) BsonBinaryWriterSettings(org.bson.BsonBinaryWriterSettings) BsonWriterSettings(org.bson.BsonWriterSettings)

Example 3 with BsonBinaryWriterSettings

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

the class BaseWriteCommandMessage method encodeMessageBodyWithMetadata.

@Override
protected EncodingMetadata encodeMessageBodyWithMetadata(final BsonOutput outputStream, final int messageStartPosition) {
    BaseWriteCommandMessage nextMessage = null;
    writeCommandHeader(outputStream);
    int commandStartPosition = outputStream.getPosition();
    int firstDocumentStartPosition = outputStream.getPosition();
    BsonBinaryWriter writer = new BsonBinaryWriter(new BsonWriterSettings(), new BsonBinaryWriterSettings(getSettings().getMaxDocumentSize() + HEADROOM), outputStream, getFieldNameValidator());
    try {
        writer.writeStartDocument();
        writeCommandPrologue(writer);
        nextMessage = writeTheWrites(outputStream, commandStartPosition, writer);
        writer.writeEndDocument();
    } finally {
        writer.close();
    }
    return new EncodingMetadata(nextMessage, firstDocumentStartPosition);
}
Also used : BsonBinaryWriter(org.bson.BsonBinaryWriter) BsonBinaryWriterSettings(org.bson.BsonBinaryWriterSettings) BsonWriterSettings(org.bson.BsonWriterSettings)

Example 4 with BsonBinaryWriterSettings

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

the class CryptConnection method commandAsync.

@Override
public <T> void commandAsync(final String database, final BsonDocument command, final FieldNameValidator commandFieldNameValidator, final ReadPreference readPreference, final Decoder<T> commandResultDecoder, final SessionContext sessionContext, final ServerApi serverApi, final RequestContext requestContext, final boolean responseExpected, @Nullable final SplittablePayload payload, @Nullable final FieldNameValidator payloadFieldNameValidator, final SingleResultCallback<T> callback) {
    if (serverIsLessThanVersionFourDotTwo(wrapped.getDescription())) {
        callback.onResult(null, new MongoClientException("Auto-encryption requires a minimum MongoDB version of 4.2"));
        return;
    }
    try {
        BasicOutputBuffer bsonOutput = new BasicOutputBuffer();
        BsonBinaryWriter bsonBinaryWriter = new BsonBinaryWriter(new BsonWriterSettings(), new BsonBinaryWriterSettings(getDescription().getMaxDocumentSize()), bsonOutput, getFieldNameValidator(payload, commandFieldNameValidator, payloadFieldNameValidator));
        BsonWriter writer = payload == null ? bsonBinaryWriter : new SplittablePayloadBsonWriter(bsonBinaryWriter, bsonOutput, createSplittablePayloadMessageSettings(), payload, MAX_SPLITTABLE_DOCUMENT_SIZE);
        getEncoder(command).encode(writer, command, EncoderContext.builder().build());
        crypt.encrypt(database, new RawBsonDocument(bsonOutput.getInternalBuffer(), 0, bsonOutput.getSize())).flatMap((Function<RawBsonDocument, Mono<RawBsonDocument>>) encryptedCommand -> Mono.create(sink -> wrapped.commandAsync(database, encryptedCommand, commandFieldNameValidator, readPreference, new RawBsonDocumentCodec(), sessionContext, serverApi, requestContext, responseExpected, null, null, sinkToCallback(sink)))).flatMap(crypt::decrypt).map(decryptedResponse -> commandResultDecoder.decode(new BsonBinaryReader(decryptedResponse.getByteBuffer().asNIO()), DecoderContext.builder().build())).subscribe(decryptedResult -> callback.onResult(decryptedResult, null), e -> callback.onResult(null, e));
    } catch (Throwable t) {
        callback.onResult(null, t);
    }
}
Also used : ReadPreference(com.mongodb.ReadPreference) InsertRequest(com.mongodb.internal.bulk.InsertRequest) MongoClientException(com.mongodb.MongoClientException) ServerApi(com.mongodb.ServerApi) HashMap(java.util.HashMap) CodecRegistry(org.bson.codecs.configuration.CodecRegistry) SplittablePayloadBsonWriter(com.mongodb.internal.connection.SplittablePayloadBsonWriter) BsonBinaryWriter(org.bson.BsonBinaryWriter) Function(java.util.function.Function) BsonDocument(org.bson.BsonDocument) ConnectionDescription(com.mongodb.connection.ConnectionDescription) Connection(com.mongodb.internal.connection.Connection) Map(java.util.Map) WriteConcernResult(com.mongodb.WriteConcernResult) DeleteRequest(com.mongodb.internal.bulk.DeleteRequest) SessionContext(com.mongodb.internal.session.SessionContext) MappedFieldNameValidator(com.mongodb.internal.validator.MappedFieldNameValidator) FieldNameValidator(org.bson.FieldNameValidator) BsonBinaryWriterSettings(org.bson.BsonBinaryWriterSettings) DecoderContext(org.bson.codecs.DecoderContext) RawBsonDocumentCodec(org.bson.codecs.RawBsonDocumentCodec) EncoderContext(org.bson.codecs.EncoderContext) QueryResult(com.mongodb.internal.connection.QueryResult) MongoNamespace(com.mongodb.MongoNamespace) MessageSettings(com.mongodb.internal.connection.MessageSettings) UpdateRequest(com.mongodb.internal.bulk.UpdateRequest) RawBsonDocument(org.bson.RawBsonDocument) CodecRegistries.fromProviders(org.bson.codecs.configuration.CodecRegistries.fromProviders) BasicOutputBuffer(org.bson.io.BasicOutputBuffer) Mono(reactor.core.publisher.Mono) RequestContext(com.mongodb.RequestContext) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) ServerVersionHelper.serverIsLessThanVersionFourDotTwo(com.mongodb.internal.operation.ServerVersionHelper.serverIsLessThanVersionFourDotTwo) SplittablePayload(com.mongodb.internal.connection.SplittablePayload) SingleResultCallback(com.mongodb.internal.async.SingleResultCallback) Decoder(org.bson.codecs.Decoder) List(java.util.List) BsonBinaryReader(org.bson.BsonBinaryReader) Codec(org.bson.codecs.Codec) BsonWriter(org.bson.BsonWriter) MongoOperationPublisher.sinkToCallback(com.mongodb.reactivestreams.client.internal.MongoOperationPublisher.sinkToCallback) Nullable(com.mongodb.lang.Nullable) AsyncConnection(com.mongodb.internal.connection.AsyncConnection) BsonWriterSettings(org.bson.BsonWriterSettings) MongoClientException(com.mongodb.MongoClientException) RawBsonDocumentCodec(org.bson.codecs.RawBsonDocumentCodec) BsonBinaryReader(org.bson.BsonBinaryReader) SplittablePayloadBsonWriter(com.mongodb.internal.connection.SplittablePayloadBsonWriter) BsonWriter(org.bson.BsonWriter) RawBsonDocument(org.bson.RawBsonDocument) Function(java.util.function.Function) SplittablePayloadBsonWriter(com.mongodb.internal.connection.SplittablePayloadBsonWriter) BsonBinaryWriter(org.bson.BsonBinaryWriter) BsonBinaryWriterSettings(org.bson.BsonBinaryWriterSettings) BsonWriterSettings(org.bson.BsonWriterSettings) BasicOutputBuffer(org.bson.io.BasicOutputBuffer)

Aggregations

BsonBinaryWriter (org.bson.BsonBinaryWriter)4 BsonBinaryWriterSettings (org.bson.BsonBinaryWriterSettings)4 BsonWriterSettings (org.bson.BsonWriterSettings)4 BsonWriter (org.bson.BsonWriter)3 MongoClientException (com.mongodb.MongoClientException)2 SplittablePayloadBsonWriter (com.mongodb.internal.connection.SplittablePayloadBsonWriter)2 BsonBinaryReader (org.bson.BsonBinaryReader)2 RawBsonDocument (org.bson.RawBsonDocument)2 RawBsonDocumentCodec (org.bson.codecs.RawBsonDocumentCodec)2 MongoNamespace (com.mongodb.MongoNamespace)1 ReadPreference (com.mongodb.ReadPreference)1 RequestContext (com.mongodb.RequestContext)1 ServerApi (com.mongodb.ServerApi)1 WriteConcernResult (com.mongodb.WriteConcernResult)1 ConnectionDescription (com.mongodb.connection.ConnectionDescription)1 SingleResultCallback (com.mongodb.internal.async.SingleResultCallback)1 DeleteRequest (com.mongodb.internal.bulk.DeleteRequest)1 InsertRequest (com.mongodb.internal.bulk.InsertRequest)1 UpdateRequest (com.mongodb.internal.bulk.UpdateRequest)1 AsyncConnection (com.mongodb.internal.connection.AsyncConnection)1