|
| 1 | +package io.r2dbc.postgresql; |
| 2 | + |
| 3 | +import io.r2dbc.postgresql.authentication.AuthenticationHandler; |
| 4 | +import io.r2dbc.postgresql.authentication.PasswordAuthenticationHandler; |
| 5 | +import io.r2dbc.postgresql.authentication.SASLAuthenticationHandler; |
| 6 | +import io.r2dbc.postgresql.client.Client; |
| 7 | +import io.r2dbc.postgresql.client.ConnectionSettings; |
| 8 | +import io.r2dbc.postgresql.client.StartupMessageFlow; |
| 9 | +import io.r2dbc.postgresql.message.backend.AuthenticationMessage; |
| 10 | +import io.r2dbc.postgresql.util.Assert; |
| 11 | +import reactor.core.publisher.Mono; |
| 12 | + |
| 13 | +import javax.annotation.Nullable; |
| 14 | +import java.net.SocketAddress; |
| 15 | +import java.util.Map; |
| 16 | + |
| 17 | +public class DefaultConnectionStrategy implements ConnectionStrategy { |
| 18 | + |
| 19 | + private final ClientSupplier clientSupplier; |
| 20 | + private final SocketAddress address; |
| 21 | + private final PostgresqlConnectionConfiguration configuration; |
| 22 | + private final ConnectionSettings connectionSettings; |
| 23 | + private final Map<String, String> options; |
| 24 | + |
| 25 | + DefaultConnectionStrategy( |
| 26 | + ClientSupplier clientSupplier, |
| 27 | + @Nullable SocketAddress address, |
| 28 | + PostgresqlConnectionConfiguration configuration, |
| 29 | + ConnectionSettings connectionSettings, |
| 30 | + @Nullable Map<String, String> options |
| 31 | + ) { |
| 32 | + this.clientSupplier = clientSupplier; |
| 33 | + this.address = address; |
| 34 | + this.configuration = configuration; |
| 35 | + this.connectionSettings = connectionSettings; |
| 36 | + this.options = options; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public Mono<Client> connect() { |
| 41 | + Assert.requireNonNull(this.address, "address must not be null"); |
| 42 | + return this.clientSupplier.connect(this.address, this.connectionSettings) |
| 43 | + .delayUntil(client -> StartupMessageFlow |
| 44 | + .exchange(this.configuration.getApplicationName(), this::getAuthenticationHandler, client, this.configuration.getDatabase(), this.configuration.getUsername(), this.options) |
| 45 | + .handle(ExceptionFactory.INSTANCE::handleErrorResponse)); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public ConnectionStrategy withConnectionSettings(ConnectionSettings connectionSettings) { |
| 50 | + return new DefaultConnectionStrategy(this.clientSupplier, this.address, this.configuration, connectionSettings, this.options); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public ConnectionStrategy withAddress(SocketAddress address) { |
| 55 | + return new DefaultConnectionStrategy(this.clientSupplier, address, this.configuration, this.connectionSettings, this.options); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public ConnectionStrategy withOptions(Map<String, String> options) { |
| 60 | + return new DefaultConnectionStrategy(this.clientSupplier, this.address, this.configuration, this.connectionSettings, options); |
| 61 | + } |
| 62 | + |
| 63 | + protected AuthenticationHandler getAuthenticationHandler(AuthenticationMessage message) { |
| 64 | + if (PasswordAuthenticationHandler.supports(message)) { |
| 65 | + CharSequence password = Assert.requireNonNull(this.configuration.getPassword(), "Password must not be null"); |
| 66 | + return new PasswordAuthenticationHandler(password, this.configuration.getUsername()); |
| 67 | + } else if (SASLAuthenticationHandler.supports(message)) { |
| 68 | + CharSequence password = Assert.requireNonNull(this.configuration.getPassword(), "Password must not be null"); |
| 69 | + return new SASLAuthenticationHandler(password, this.configuration.getUsername()); |
| 70 | + } else { |
| 71 | + throw new IllegalStateException(String.format("Unable to provide AuthenticationHandler capable of handling %s", message)); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | +} |
0 commit comments