2626import com .clickhouse .client .http .config .ClickHouseHttpOption ;
2727
2828public abstract class ClickHouseHttpConnection implements AutoCloseable {
29+ private static StringBuilder appendQueryParameter (StringBuilder builder , String key , String value ) {
30+ return builder .append (urlEncode (key , StandardCharsets .UTF_8 )).append ('=' )
31+ .append (urlEncode (value , StandardCharsets .UTF_8 )).append ('&' );
32+ }
33+
2934 static String urlEncode (String str , Charset charset ) {
3035 if (charset == null ) {
3136 charset = StandardCharsets .UTF_8 ;
@@ -39,11 +44,6 @@ static String urlEncode(String str, Charset charset) {
3944 }
4045 }
4146
42- private static StringBuilder appendQueryParameter (StringBuilder builder , String key , String value ) {
43- return builder .append (urlEncode (key , StandardCharsets .UTF_8 )).append ('=' )
44- .append (urlEncode (value , StandardCharsets .UTF_8 )).append ('&' );
45- }
46-
4747 static String buildQueryParams (ClickHouseRequest <?> request ) {
4848 if (request == null ) {
4949 return "" ;
@@ -146,26 +146,7 @@ static String buildUrl(String baseUrl, ClickHouseRequest<?> request) {
146146 return builder .toString ();
147147 }
148148
149- protected final ClickHouseConfig config ;
150- protected final ClickHouseNode server ;
151- protected final Map <String , String > defaultHeaders ;
152-
153- protected final ClickHouseOutputStream output ;
154-
155- protected final String url ;
156-
157- protected ClickHouseHttpConnection (ClickHouseNode server , ClickHouseRequest <?> request ) {
158- if (server == null || request == null ) {
159- throw new IllegalArgumentException ("Non-null server and request are required" );
160- }
161-
162- this .config = request .getConfig ();
163- this .server = server ;
164-
165- this .output = request .getOutputStream ().orElse (null );
166-
167- this .url = buildUrl (server .getBaseUri (), request );
168-
149+ protected static Map <String , String > createDefaultHeaders (ClickHouseConfig config , ClickHouseNode server ) {
169150 Map <String , String > map = new LinkedHashMap <>();
170151 // add customer headers
171152 map .putAll (ClickHouseUtils .getKeyValuePairs ((String ) config .getOption (ClickHouseHttpOption .CUSTOM_HEADERS )));
@@ -201,8 +182,25 @@ protected ClickHouseHttpConnection(ClickHouseNode server, ClickHouseRequest<?> r
201182 && config .getRequestCompressAlgorithm () != ClickHouseCompression .LZ4 ) {
202183 map .put ("Content-Encoding" , config .getRequestCompressAlgorithm ().encoding ());
203184 }
185+ return map ;
186+ }
204187
205- this .defaultHeaders = Collections .unmodifiableMap (map );
188+ protected final ClickHouseConfig config ;
189+ protected final ClickHouseNode server ;
190+ protected final ClickHouseOutputStream output ;
191+ protected final String url ;
192+ protected final Map <String , String > defaultHeaders ;
193+
194+ protected ClickHouseHttpConnection (ClickHouseNode server , ClickHouseRequest <?> request ) {
195+ if (server == null || request == null ) {
196+ throw new IllegalArgumentException ("Non-null server and request are required" );
197+ }
198+
199+ this .config = request .getConfig ();
200+ this .server = server ;
201+ this .output = request .getOutputStream ().orElse (null );
202+ this .url = buildUrl (server .getBaseUri (), request );
203+ this .defaultHeaders = Collections .unmodifiableMap (createDefaultHeaders (config , server ));
206204 }
207205
208206 protected void closeQuietly () {
@@ -231,11 +229,13 @@ protected String getBaseUrl() {
231229 * Creates a merged map.
232230 *
233231 * @param requestHeaders request headers
234- * @return
232+ * @return non-null merged headers
235233 */
236234 protected Map <String , String > mergeHeaders (Map <String , String > requestHeaders ) {
237235 if (requestHeaders == null || requestHeaders .isEmpty ()) {
238236 return defaultHeaders ;
237+ } else if (isReusable ()) {
238+ return requestHeaders ;
239239 }
240240
241241 Map <String , String > merged = new LinkedHashMap <>();
@@ -256,13 +256,15 @@ protected Map<String, String> mergeHeaders(Map<String, String> requestHeaders) {
256256 * @param query non-blank query
257257 * @param data optionally input stream for batch updating
258258 * @param tables optionally external tables for query
259+ * @param url optionally url
259260 * @param headers optionally request headers
261+ * @param config optionally configuration
260262 * @return response
261263 * @throws IOException when error occured posting request and/or server failed
262264 * to respond
263265 */
264266 protected abstract ClickHouseHttpResponse post (String query , InputStream data , List <ClickHouseExternalTable > tables ,
265- Map <String , String > headers ) throws IOException ;
267+ String url , Map <String , String > headers , ClickHouseConfig config ) throws IOException ;
266268
267269 /**
268270 * Checks whether the connection is reusable or not. This method will be called
@@ -287,36 +289,36 @@ protected boolean isReusable() {
287289 public abstract boolean ping (int timeout );
288290
289291 public ClickHouseHttpResponse update (String query ) throws IOException {
290- return post (query , null , null , null );
292+ return post (query , null , null , null , null , null );
291293 }
292294
293295 public ClickHouseHttpResponse update (String query , Map <String , String > headers ) throws IOException {
294- return post (query , null , null , headers );
296+ return post (query , null , null , null , headers , null );
295297 }
296298
297299 public ClickHouseHttpResponse update (String query , InputStream data ) throws IOException {
298- return post (query , data , null , null );
300+ return post (query , data , null , null , null , null );
299301 }
300302
301303 public ClickHouseHttpResponse update (String query , InputStream data , Map <String , String > headers )
302304 throws IOException {
303- return post (query , data , null , headers );
305+ return post (query , data , null , null , headers , null );
304306 }
305307
306308 public ClickHouseHttpResponse query (String query ) throws IOException {
307- return post (query , null , null , null );
309+ return post (query , null , null , null , null , null );
308310 }
309311
310312 public ClickHouseHttpResponse query (String query , Map <String , String > headers ) throws IOException {
311- return post (query , null , null , headers );
313+ return post (query , null , null , null , headers , null );
312314 }
313315
314316 public ClickHouseHttpResponse query (String query , List <ClickHouseExternalTable > tables ) throws IOException {
315- return post (query , null , tables , null );
317+ return post (query , null , tables , null , null , null );
316318 }
317319
318320 public ClickHouseHttpResponse query (String query , List <ClickHouseExternalTable > tables , Map <String , String > headers )
319321 throws IOException {
320- return post (query , null , tables , headers );
322+ return post (query , null , tables , null , headers , null );
321323 }
322324}
0 commit comments