1717
1818package org .sourcelab .kafka .connect .apiclient .rest ;
1919
20+ import org .apache .http .Header ;
2021import org .apache .http .HttpHost ;
2122import org .apache .http .NameValuePair ;
2223import org .apache .http .auth .AuthScope ;
5354import java .net .URISyntaxException ;
5455import java .nio .charset .StandardCharsets ;
5556import java .util .ArrayList ;
57+ import java .util .Arrays ;
58+ import java .util .Collection ;
5659import java .util .Collections ;
5760import java .util .List ;
5861import java .util .Map ;
6467public class HttpClientRestClient implements RestClient {
6568 private static final Logger logger = LoggerFactory .getLogger (HttpClientRestClient .class );
6669
70+ /**
71+ * Default headers included with every request.
72+ */
73+ private static final Collection <Header > DEFAULT_HEADERS = Collections .unmodifiableCollection (Arrays .asList (
74+ new BasicHeader ("Accept" , "application/json" ),
75+ new BasicHeader ("Content-Type" , "application/json" )
76+ ));
77+
6778 /**
6879 * Save a copy of the configuration.
6980 */
@@ -74,7 +85,6 @@ public class HttpClientRestClient implements RestClient {
7485 */
7586 private CloseableHttpClient httpClient ;
7687
77-
7888 /**
7989 * Constructor.
8090 */
@@ -208,11 +218,8 @@ private <T> T submitGetRequest(final String url, final Map<String, String> getPa
208218 // Build Get Request
209219 final HttpGet get = new HttpGet (uriBuilder .build ());
210220
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" ));
221+ // Add default headers.
222+ DEFAULT_HEADERS .forEach (get ::addHeader );
216223
217224 logger .debug ("Executing request {}" , get .getRequestLine ());
218225
@@ -239,14 +246,8 @@ private <T> T submitPostRequest(final String url, final Object requestBody, fina
239246 try {
240247 final HttpPost post = new HttpPost (url );
241248
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 <>();
249+ // Add default headers.
250+ DEFAULT_HEADERS .forEach (post ::addHeader );
250251
251252 // Convert to Json
252253 final String jsonPayloadStr = JacksonFactory .newInstance ().writeValueAsString (requestBody );
@@ -276,17 +277,10 @@ private <T> T submitPostRequest(final String url, final Object requestBody, fina
276277 */
277278 private <T > T submitPutRequest (final String url , final Object requestBody , final ResponseHandler <T > responseHandler ) throws IOException {
278279 try {
279- // Construct URI including our request parameters.
280- final URIBuilder uriBuilder = new URIBuilder (url )
281- .setCharset (StandardCharsets .UTF_8 );
282-
283280 final HttpPut put = new HttpPut (url );
284281
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" ));
282+ // Add default headers.
283+ DEFAULT_HEADERS .forEach (put ::addHeader );
290284
291285 // Convert to Json and submit as payload.
292286 final String jsonPayloadStr = JacksonFactory .newInstance ().writeValueAsString (requestBody );
@@ -296,7 +290,7 @@ private <T> T submitPutRequest(final String url, final Object requestBody, final
296290
297291 // Execute and return
298292 return httpClient .execute (put , responseHandler );
299- } catch (final ClientProtocolException | SocketException | URISyntaxException connectionException ) {
293+ } catch (final ClientProtocolException | SocketException connectionException ) {
300294 // Typically this is a connection issue.
301295 throw new ConnectionException (connectionException .getMessage (), connectionException );
302296 } catch (final IOException ioException ) {
@@ -315,20 +309,10 @@ private <T> T submitPutRequest(final String url, final Object requestBody, final
315309 */
316310 private <T > T submitDeleteRequest (final String url , final Object requestBody , final ResponseHandler <T > responseHandler ) throws IOException {
317311 try {
318- // Construct URI including our request parameters.
319- final URIBuilder uriBuilder = new URIBuilder (url )
320- .setCharset (StandardCharsets .UTF_8 );
321-
322312 final HttpDelete delete = new HttpDelete (url );
323313
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 <>();
314+ // Add default headers.
315+ DEFAULT_HEADERS .forEach (delete ::addHeader );
332316
333317 // Convert to Json
334318 final String jsonPayloadStr = JacksonFactory .newInstance ().writeValueAsString (requestBody );
@@ -337,7 +321,7 @@ private <T> T submitDeleteRequest(final String url, final Object requestBody, fi
337321
338322 // Execute and return
339323 return httpClient .execute (delete , responseHandler );
340- } catch (final ClientProtocolException | SocketException | URISyntaxException connectionException ) {
324+ } catch (final ClientProtocolException | SocketException connectionException ) {
341325 // Typically this is a connection issue.
342326 throw new ConnectionException (connectionException .getMessage (), connectionException );
343327 } catch (final IOException ioException ) {
0 commit comments