public ArrowFlightSqlClientHandler build()

in flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/client/ArrowFlightSqlClientHandler.java [855:950]


    public ArrowFlightSqlClientHandler build() throws SQLException {
      // Copy middleware so that the build method doesn't change the state of the builder fields
      // itself.
      Set<FlightClientMiddleware.Factory> buildTimeMiddlewareFactories =
          new HashSet<>(this.middlewareFactories);
      FlightClient client = null;
      boolean isUsingUserPasswordAuth = username != null && token == null;

      try {
        // Token should take priority since some apps pass in a username/password even when a token
        // is provided
        if (isUsingUserPasswordAuth) {
          buildTimeMiddlewareFactories.add(authFactory);
        }
        final NettyClientBuilder clientBuilder = new NettyClientBuilder();
        clientBuilder.allocator(allocator);

        buildTimeMiddlewareFactories.add(new ClientCookieMiddleware.Factory());
        buildTimeMiddlewareFactories.forEach(clientBuilder::intercept);
        if (useEncryption) {
          clientBuilder.useTls();
        }
        Location location = getLocation();
        clientBuilder.location(location);

        if (useEncryption) {
          if (disableCertificateVerification) {
            clientBuilder.verifyServer(false);
          } else {
            if (tlsRootCertificatesPath != null) {
              clientBuilder.trustedCertificates(
                  ClientAuthenticationUtils.getTlsRootCertificatesStream(tlsRootCertificatesPath));
            } else if (useSystemTrustStore) {
              clientBuilder.trustedCertificates(
                  ClientAuthenticationUtils.getCertificateInputStreamFromSystem(
                      trustStorePassword));
            } else if (trustStorePath != null) {
              clientBuilder.trustedCertificates(
                  ClientAuthenticationUtils.getCertificateStream(
                      trustStorePath, trustStorePassword));
            }
          }

          if (clientCertificatePath != null && clientKeyPath != null) {
            clientBuilder.clientCertificate(
                ClientAuthenticationUtils.getClientCertificateStream(clientCertificatePath),
                ClientAuthenticationUtils.getClientKeyStream(clientKeyPath));
          }
        }

        NettyChannelBuilder channelBuilder = clientBuilder.build();
        if (connectTimeout != null) {
          channelBuilder.withOption(
              ChannelOption.CONNECT_TIMEOUT_MILLIS, (int) connectTimeout.toMillis());
        }
        client =
            FlightGrpcUtils.createFlightClient(
                allocator, channelBuilder.build(), clientBuilder.middleware());
        final ArrayList<CallOption> credentialOptions = new ArrayList<>();
        if (isUsingUserPasswordAuth) {
          // If the authFactory has already been used for a handshake, use the existing token.
          // This can occur if the authFactory is being re-used for a new connection spawned for
          // getStream().
          if (authFactory.getCredentialCallOption() != null) {
            credentialOptions.add(authFactory.getCredentialCallOption());
          } else {
            // Otherwise do the handshake and get the token if possible.
            credentialOptions.add(
                ClientAuthenticationUtils.getAuthenticate(
                    client, username, password, authFactory, options.toArray(new CallOption[0])));
          }
        } else if (token != null) {
          credentialOptions.add(
              ClientAuthenticationUtils.getAuthenticate(
                  client,
                  new CredentialCallOption(new BearerCredentialWriter(token)),
                  options.toArray(new CallOption[0])));
        }
        return ArrowFlightSqlClientHandler.createNewHandler(
            client, this, credentialOptions, catalog);

      } catch (final IllegalArgumentException
          | GeneralSecurityException
          | IOException
          | FlightRuntimeException e) {
        final SQLException originalException = new SQLException(e);
        if (client != null) {
          try {
            client.close();
          } catch (final InterruptedException interruptedException) {
            originalException.addSuppressed(interruptedException);
          }
        }
        throw originalException;
      }
    }