117117 * ...
118118 * }
119119 * }
120- *
121120 * }
122121 *
123122 *
@@ -131,6 +130,9 @@ public class Client implements AutoCloseable {
131130
132131 private final Set <String > endpoints ;
133132 private final Map <String , String > configuration ;
133+
134+ private final Map <String , String > readOnlyConfig ;
135+
134136 private final List <ClickHouseNode > serverNodes = new ArrayList <>();
135137
136138 // POJO serializer mapping (class -> (schema -> (format -> serializer)))
@@ -154,10 +156,10 @@ public class Client implements AutoCloseable {
154156 private final ColumnToMethodMatchingStrategy columnToMethodMatchingStrategy ;
155157
156158 private Client (Set <String > endpoints , Map <String ,String > configuration , boolean useNewImplementation ,
157- ExecutorService sharedOperationExecutor , ColumnToMethodMatchingStrategy columnToMethodMatchingStrategy ,
158- Supplier <String > bearerTokenSupplier ) {
159+ ExecutorService sharedOperationExecutor , ColumnToMethodMatchingStrategy columnToMethodMatchingStrategy ) {
159160 this .endpoints = endpoints ;
160161 this .configuration = configuration ;
162+ this .readOnlyConfig = Collections .unmodifiableMap (this .configuration );
161163 this .endpoints .forEach (endpoint -> {
162164 this .serverNodes .add (ClickHouseNode .of (endpoint , this .configuration ));
163165 });
@@ -172,7 +174,7 @@ private Client(Set<String> endpoints, Map<String,String> configuration, boolean
172174 }
173175 this .useNewImplementation = useNewImplementation ;
174176 if (useNewImplementation ) {
175- this .httpClientHelper = new HttpAPIClientHelper (configuration , bearerTokenSupplier );
177+ this .httpClientHelper = new HttpAPIClientHelper (configuration );
176178 LOG .info ("Using new http client implementation" );
177179 } else {
178180 this .oldClient = ClientV1AdaptorHelper .createClient (configuration );
@@ -226,8 +228,6 @@ public static class Builder {
226228 private ExecutorService sharedOperationExecutor = null ;
227229 private ColumnToMethodMatchingStrategy columnToMethodMatchingStrategy ;
228230
229- private Supplier <String > bearerTokenSupplier = null ;
230-
231231 public Builder () {
232232 this .endpoints = new HashSet <>();
233233 this .configuration = new HashMap <String , String >();
@@ -855,7 +855,7 @@ public Builder allowBinaryReaderToReuseBuffers(boolean reuse) {
855855 * @return same instance of the builder
856856 */
857857 public Builder httpHeader (String key , String value ) {
858- this .configuration .put (ClientConfigProperties .HTTP_HEADER_PREFIX + key . toUpperCase ( Locale . US ), value );
858+ this .configuration .put (ClientConfigProperties .httpHeader ( key ), value );
859859 return this ;
860860 }
861861
@@ -866,7 +866,7 @@ public Builder httpHeader(String key, String value) {
866866 * @return same instance of the builder
867867 */
868868 public Builder httpHeader (String key , Collection <String > values ) {
869- this .configuration .put (ClientConfigProperties .HTTP_HEADER_PREFIX + key . toUpperCase ( Locale . US ), ClientConfigProperties .commaSeparated (values ));
869+ this .configuration .put (ClientConfigProperties .httpHeader ( key ), ClientConfigProperties .commaSeparated (values ));
870870 return this ;
871871 }
872872
@@ -965,21 +965,8 @@ public Builder setOptions(Map<String, String> options) {
965965 * @return same instance of the builder
966966 */
967967 public Builder useBearerTokenAuth (String bearerToken ) {
968- this .httpHeader ("Authorization" , "Bearer " + bearerToken );
969- return this ;
970- }
971-
972- /**
973- * Specifies a supplier for a bearer tokens. It is useful when token should be refreshed.
974- * Supplier is called each time before sending a request.
975- * Supplier should return encoded token.
976- * This configuration cannot be used with {@link #useBearerTokenAuth(String)}.
977- *
978- * @param tokenSupplier - token supplier
979- * @return
980- */
981- public Builder useBearerTokenAuth (Supplier <String > tokenSupplier ) {
982- this .bearerTokenSupplier = tokenSupplier ;
968+ // Most JWT libraries (https://jwt.io/libraries?language=Java) compact tokens in proper way
969+ this .httpHeader (HttpHeaders .AUTHORIZATION , "Bearer " + bearerToken );
983970 return this ;
984971 }
985972
@@ -994,8 +981,7 @@ public Client build() {
994981 if (!this .configuration .containsKey ("access_token" ) &&
995982 (!this .configuration .containsKey ("user" ) || !this .configuration .containsKey ("password" )) &&
996983 !MapUtils .getFlag (this .configuration , "ssl_authentication" , false ) &&
997- !this .configuration .containsKey (ClientConfigProperties .HTTP_HEADER_PREFIX + "Authorization" ) &&
998- this .bearerTokenSupplier == null ) {
984+ !this .configuration .containsKey (ClientConfigProperties .httpHeader (HttpHeaders .AUTHORIZATION ))) {
999985 throw new IllegalArgumentException ("Username and password (or access token or SSL authentication or pre-define Authorization header) are required" );
1000986 }
1001987
@@ -1004,11 +990,6 @@ public Client build() {
1004990 throw new IllegalArgumentException ("Only one of password, access token or SSL authentication can be used per client." );
1005991 }
1006992
1007- if (this .configuration .containsKey (ClientConfigProperties .HTTP_HEADER_PREFIX + "Authorization" ) &&
1008- this .bearerTokenSupplier != null ) {
1009- throw new IllegalArgumentException ("Bearer token supplier cannot be used with a predefined Authorization header" );
1010- }
1011-
1012993 if (this .configuration .containsKey ("ssl_authentication" ) &&
1013994 !this .configuration .containsKey (ClientConfigProperties .SSL_CERTIFICATE .getKey ())) {
1014995 throw new IllegalArgumentException ("SSL authentication requires a client certificate" );
@@ -1047,15 +1028,8 @@ public Client build() {
10471028 throw new IllegalArgumentException ("Nor server timezone nor specific timezone is set" );
10481029 }
10491030
1050- // check for only new implementation configuration
1051- if (!this .useNewImplementation ) {
1052- if (this .bearerTokenSupplier != null ) {
1053- throw new IllegalArgumentException ("Bearer token supplier cannot be used with old implementation" );
1054- }
1055- }
1056-
10571031 return new Client (this .endpoints , this .configuration , this .useNewImplementation , this .sharedOperationExecutor ,
1058- this .columnToMethodMatchingStrategy , this . bearerTokenSupplier );
1032+ this .columnToMethodMatchingStrategy );
10591033 }
10601034
10611035 private static final int DEFAULT_NETWORK_BUFFER_SIZE = 300_000 ;
@@ -2147,7 +2121,7 @@ public String toString() {
21472121 * @return - configuration options
21482122 */
21492123 public Map <String , String > getConfiguration () {
2150- return Collections . unmodifiableMap ( configuration ) ;
2124+ return readOnlyConfig ;
21512125 }
21522126
21532127 /** Returns operation timeout in seconds */
@@ -2194,6 +2168,10 @@ public Collection<String> getDBRoles() {
21942168 return unmodifiableDbRolesView ;
21952169 }
21962170
2171+ public void updateBearerToken (String bearer ) {
2172+ this .configuration .put (ClientConfigProperties .httpHeader (HttpHeaders .AUTHORIZATION ), "Bearer " + bearer );
2173+ }
2174+
21972175 private ClickHouseNode getNextAliveNode () {
21982176 return serverNodes .get (0 );
21992177 }
0 commit comments