1717
1818package org .sourcelab .kafka .connect .apiclient .rest ;
1919
20+ import org .apache .http .Header ;
2021import org .apache .http .HttpHost ;
21- import org .apache .http .NameValuePair ;
2222import org .apache .http .auth .AuthScope ;
2323import org .apache .http .auth .UsernamePasswordCredentials ;
2424import org .apache .http .client .ClientProtocolException ;
5252import java .net .SocketException ;
5353import java .net .URISyntaxException ;
5454import java .nio .charset .StandardCharsets ;
55- import java .util .ArrayList ;
55+ import java .util .Arrays ;
56+ import java .util .Collection ;
5657import java .util .Collections ;
57- import java .util .List ;
5858import java .util .Map ;
5959import java .util .concurrent .TimeUnit ;
6060
6464public class HttpClientRestClient implements RestClient {
6565 private static final Logger logger = LoggerFactory .getLogger (HttpClientRestClient .class );
6666
67+ /**
68+ * Default headers included with every request.
69+ */
70+ private static final Collection <Header > DEFAULT_HEADERS = Collections .unmodifiableCollection (Arrays .asList (
71+ new BasicHeader ("Accept" , "application/json" ),
72+ new BasicHeader ("Content-Type" , "application/json" )
73+ ));
74+
6775 /**
6876 * Save a copy of the configuration.
6977 */
@@ -74,7 +82,6 @@ public class HttpClientRestClient implements RestClient {
7482 */
7583 private CloseableHttpClient httpClient ;
7684
77-
7885 /**
7986 * Constructor.
8087 */
@@ -208,11 +215,8 @@ private <T> T submitGetRequest(final String url, final Map<String, String> getPa
208215 // Build Get Request
209216 final HttpGet get = new HttpGet (uriBuilder .build ());
210217
211- // Add Accept header.
212- get .addHeader (new BasicHeader ("Accept" , "application/json" ));
213-
214- // Conditionally add content-type header?
215- get .addHeader (new BasicHeader ("Content-Type" , "application/json" ));
218+ // Add default headers.
219+ DEFAULT_HEADERS .forEach (get ::addHeader );
216220
217221 logger .debug ("Executing request {}" , get .getRequestLine ());
218222
@@ -239,14 +243,8 @@ private <T> T submitPostRequest(final String url, final Object requestBody, fina
239243 try {
240244 final HttpPost post = new HttpPost (url );
241245
242- // Add Accept header.
243- post .addHeader (new BasicHeader ("Accept" , "application/json" ));
244-
245- // Conditionally add content-type header?
246- post .addHeader (new BasicHeader ("Content-Type" , "application/json" ));
247-
248- // Define required auth params
249- final List <NameValuePair > params = new ArrayList <>();
246+ // Add default headers.
247+ DEFAULT_HEADERS .forEach (post ::addHeader );
250248
251249 // Convert to Json
252250 final String jsonPayloadStr = JacksonFactory .newInstance ().writeValueAsString (requestBody );
@@ -276,17 +274,10 @@ private <T> T submitPostRequest(final String url, final Object requestBody, fina
276274 */
277275 private <T > T submitPutRequest (final String url , final Object requestBody , final ResponseHandler <T > responseHandler ) throws IOException {
278276 try {
279- // Construct URI including our request parameters.
280- final URIBuilder uriBuilder = new URIBuilder (url )
281- .setCharset (StandardCharsets .UTF_8 );
282-
283277 final HttpPut put = new HttpPut (url );
284278
285- // Add Accept header.
286- put .addHeader (new BasicHeader ("Accept" , "application/json" ));
287-
288- // Conditionally add content-type header?
289- put .addHeader (new BasicHeader ("Content-Type" , "application/json" ));
279+ // Add default headers.
280+ DEFAULT_HEADERS .forEach (put ::addHeader );
290281
291282 // Convert to Json and submit as payload.
292283 final String jsonPayloadStr = JacksonFactory .newInstance ().writeValueAsString (requestBody );
@@ -296,7 +287,7 @@ private <T> T submitPutRequest(final String url, final Object requestBody, final
296287
297288 // Execute and return
298289 return httpClient .execute (put , responseHandler );
299- } catch (final ClientProtocolException | SocketException | URISyntaxException connectionException ) {
290+ } catch (final ClientProtocolException | SocketException connectionException ) {
300291 // Typically this is a connection issue.
301292 throw new ConnectionException (connectionException .getMessage (), connectionException );
302293 } catch (final IOException ioException ) {
@@ -315,20 +306,10 @@ private <T> T submitPutRequest(final String url, final Object requestBody, final
315306 */
316307 private <T > T submitDeleteRequest (final String url , final Object requestBody , final ResponseHandler <T > responseHandler ) throws IOException {
317308 try {
318- // Construct URI including our request parameters.
319- final URIBuilder uriBuilder = new URIBuilder (url )
320- .setCharset (StandardCharsets .UTF_8 );
321-
322309 final HttpDelete delete = new HttpDelete (url );
323310
324- // Add Accept header.
325- delete .addHeader (new BasicHeader ("Accept" , "application/json" ));
326-
327- // Conditionally add content-type header?
328- delete .addHeader (new BasicHeader ("Content-Type" , "application/json" ));
329-
330- // Define required auth params
331- final List <NameValuePair > params = new ArrayList <>();
311+ // Add default headers.
312+ DEFAULT_HEADERS .forEach (delete ::addHeader );
332313
333314 // Convert to Json
334315 final String jsonPayloadStr = JacksonFactory .newInstance ().writeValueAsString (requestBody );
@@ -337,7 +318,7 @@ private <T> T submitDeleteRequest(final String url, final Object requestBody, fi
337318
338319 // Execute and return
339320 return httpClient .execute (delete , responseHandler );
340- } catch (final ClientProtocolException | SocketException | URISyntaxException connectionException ) {
321+ } catch (final ClientProtocolException | SocketException connectionException ) {
341322 // Typically this is a connection issue.
342323 throw new ConnectionException (connectionException .getMessage (), connectionException );
343324 } catch (final IOException ioException ) {
0 commit comments