use of com.mongodb.MongoClientException in project mongo-java-driver by mongodb.
the class DefaultDnsResolver method createDnsDirContext.
/*
It's unfortunate that we take a runtime dependency on com.sun.jndi.dns.DnsContextFactory.
This is not guaranteed to work on all JVMs but in practice is expected to work on most.
*/
private static InitialDirContext createDnsDirContext() {
Hashtable<String, String> envProps = new Hashtable<String, String>();
envProps.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
try {
return new InitialDirContext(envProps);
} catch (NamingException e) {
// Just in case the provider url default has been changed to a non-dns pseudo url, fallback to the JDK default
envProps.put(Context.PROVIDER_URL, "dns:");
try {
return new InitialDirContext(envProps);
} catch (NamingException ex) {
throw new MongoClientException("Unable to support mongodb+srv// style connections as the 'com.sun.jndi.dns.DnsContextFactory' " + "class is not available in this JRE. A JNDI context is required for resolving SRV records.", e);
}
}
}
use of com.mongodb.MongoClientException 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);
}
}
use of com.mongodb.MongoClientException in project mongo-java-driver by mongodb.
the class OperationExecutorImpl method execute.
@Override
public <T> Mono<T> execute(final AsyncReadOperation<T> operation, final ReadPreference readPreference, final ReadConcern readConcern, @Nullable final ClientSession session) {
notNull("operation", operation);
notNull("readPreference", readPreference);
notNull("readConcern", readConcern);
if (session != null) {
session.notifyOperationInitiated(operation);
}
return Mono.from(subscriber -> clientSessionHelper.withClientSession(session, OperationExecutorImpl.this).map(clientSession -> getReadWriteBinding(getContext(subscriber), readPreference, readConcern, clientSession, session == null && clientSession != null)).switchIfEmpty(Mono.fromCallable(() -> getReadWriteBinding(getContext(subscriber), readPreference, readConcern, session, false))).flatMap(binding -> {
if (session != null && session.hasActiveTransaction() && !binding.getReadPreference().equals(primary())) {
binding.release();
return Mono.error(new MongoClientException("Read preference in a transaction must be primary"));
} else {
return Mono.<T>create(sink -> operation.executeAsync(binding, (result, t) -> {
try {
binding.release();
} finally {
sinkToCallback(sink).onResult(result, t);
}
})).doOnError((t) -> {
labelException(session, t);
unpinServerAddressOnTransientTransactionError(session, t);
});
}
}).subscribe(subscriber));
}
use of com.mongodb.MongoClientException in project mongo-java-driver by mongodb.
the class ClientSessionPublisherImpl method startTransaction.
@Override
public void startTransaction(final TransactionOptions transactionOptions) {
notNull("transactionOptions", transactionOptions);
Boolean snapshot = getOptions().isSnapshot();
if (snapshot != null && snapshot) {
throw new IllegalArgumentException("Transactions are not supported in snapshot sessions");
}
if (transactionState == TransactionState.IN) {
throw new IllegalStateException("Transaction already in progress");
}
if (transactionState == TransactionState.COMMITTED) {
cleanupTransaction(TransactionState.IN);
} else {
transactionState = TransactionState.IN;
}
getServerSession().advanceTransactionNumber();
this.transactionOptions = TransactionOptions.merge(transactionOptions, getOptions().getDefaultTransactionOptions());
WriteConcern writeConcern = this.transactionOptions.getWriteConcern();
if (writeConcern == null) {
throw new MongoInternalException("Invariant violated. Transaction options write concern can not be null");
}
if (!writeConcern.isAcknowledged()) {
throw new MongoClientException("Transactions do not support unacknowledged write concern");
}
clearTransactionContext();
}
use of com.mongodb.MongoClientException in project mongo-java-driver by mongodb.
the class NettyStream method addSslHandler.
private void addSslHandler(final SocketChannel channel) {
SSLEngine engine;
if (sslContext == null) {
SSLContext sslContext;
try {
sslContext = (sslSettings.getContext() == null) ? SSLContext.getDefault() : sslSettings.getContext();
} catch (NoSuchAlgorithmException e) {
throw new MongoClientException("Unable to create standard SSLContext", e);
}
engine = sslContext.createSSLEngine(address.getHost(), address.getPort());
} else {
engine = sslContext.newEngine(channel.alloc(), address.getHost(), address.getPort());
}
engine.setUseClientMode(true);
SSLParameters sslParameters = engine.getSSLParameters();
enableSni(address.getHost(), sslParameters);
if (!sslSettings.isInvalidHostNameAllowed()) {
enableHostNameVerification(sslParameters);
}
engine.setSSLParameters(sslParameters);
channel.pipeline().addFirst("ssl", new SslHandler(engine, false));
}
Aggregations