package com.mongodb.internal.connection;
import com.mongodb.MongoCompressor;
import com.mongodb.MongoDriverInformation;
import com.mongodb.connection.ServerId;
import com.mongodb.connection.StreamFactory;
import com.mongodb.event.CommandListener;
import org.bson.BsonDocument;
import java.util.List;
import static com.mongodb.assertions.Assertions.notNull;
import static com.mongodb.internal.connection.ClientMetadataHelper.createClientMetadataDocument;
class InternalStreamConnectionFactory implements InternalConnectionFactory {
private final StreamFactory streamFactory;
private final BsonDocument clientMetadataDocument;
private final List<MongoCompressor> compressorList;
private final CommandListener commandListener;
private final MongoCredentialWithCache credential;
InternalStreamConnectionFactory(final StreamFactory streamFactory, final MongoCredentialWithCache credential,
final String applicationName, final MongoDriverInformation mongoDriverInformation,
final List<MongoCompressor> compressorList,
final CommandListener commandListener) {
this.streamFactory = notNull("streamFactory", streamFactory);
this.compressorList = notNull("compressorList", compressorList);
this.commandListener = commandListener;
this.clientMetadataDocument = createClientMetadataDocument(applicationName, mongoDriverInformation);
this.credential = credential;
}
@Override
public InternalConnection create(final ServerId serverId) {
Authenticator authenticator = credential == null ? null : createAuthenticator(credential);
return new InternalStreamConnection(serverId, streamFactory, compressorList, commandListener,
new InternalStreamConnectionInitializer(authenticator, clientMetadataDocument,
compressorList));
}
private Authenticator createAuthenticator(final MongoCredentialWithCache credential) {
if (credential.getAuthenticationMechanism() == null) {
return new DefaultAuthenticator(credential);
}
switch (credential.getAuthenticationMechanism()) {
case GSSAPI:
return new GSSAPIAuthenticator(credential);
case PLAIN:
return new PlainAuthenticator(credential);
case MONGODB_X509:
return new X509Authenticator(credential);
case SCRAM_SHA_1:
case SCRAM_SHA_256:
return new ScramShaAuthenticator(credential);
case MONGODB_AWS:
return new AwsAuthenticator(credential);
default:
throw new IllegalArgumentException("Unsupported authentication mechanism " + credential.getAuthenticationMechanism());
}
}
}