66import java .net .ProtocolException ;
77import java .net .URL ;
88import java .nio .charset .Charset ;
9+ import java .nio .charset .UnsupportedCharsetException ;
910import java .util .HashMap ;
11+ import java .util .Map ;
1012import java .util .Map .Entry ;
1113
1214import javax .net .ssl .HttpsURLConnection ;
1315
14- import org .apache .http .client .ClientProtocolException ;
15- import org .apache .http .client .methods .CloseableHttpResponse ;
16- import org .apache .http .client .methods .HttpDelete ;
17- import org .apache .http .client .methods .HttpPost ;
18- import org .apache .http .entity .AbstractHttpEntity ;
19- import org .apache .http .entity .ByteArrayEntity ;
20- import org .apache .http .entity .StringEntity ;
21- import org .apache .http .impl .client .CloseableHttpClient ;
22- import org .apache .http .impl .client .HttpClients ;
23-
2416import com .bitso .exceptions .BitsoAPIException ;
2517
2618import com .bitso .helpers .Helpers ;
19+ import org .apache .hc .client5 .http .ClientProtocolException ;
20+ import org .apache .hc .client5 .http .classic .methods .HttpDelete ;
21+ import org .apache .hc .client5 .http .classic .methods .HttpPost ;
22+ import org .apache .hc .client5 .http .impl .classic .CloseableHttpClient ;
23+ import org .apache .hc .client5 .http .impl .classic .CloseableHttpResponse ;
24+ import org .apache .hc .client5 .http .impl .classic .HttpClients ;
25+ import org .apache .hc .core5 .http .ContentType ;
26+ import org .apache .hc .core5 .http .io .entity .AbstractHttpEntity ;
27+ import org .apache .hc .core5 .http .io .entity .ByteArrayEntity ;
28+ import org .apache .hc .core5 .http .io .entity .StringEntity ;
2729
2830public class BlockingHttpClient {
31+ public static final String CONTENT_TYPE = "Content-Type" ;
2932 private boolean log = false ;
3033 private long throttleMs = -1 ;
3134 private long lastCallTime = 0 ;
@@ -56,6 +59,7 @@ private void throttle() {
5659 lastCallTime = System .currentTimeMillis ();
5760 } catch (InterruptedException e ) {
5861 log ("Error executing throttle" );
62+ Thread .currentThread ().interrupt ();
5963 }
6064 }
6165
@@ -85,29 +89,37 @@ public String sendPost(String url, String body, HashMap<String, String> headers)
8589
8690 return Helpers .convertInputStreamToString (connection .getInputStream ());
8791 } catch (MalformedURLException e ) {
88- e .printStackTrace ();
8992 throw new BitsoAPIException (322 , "Not a Valid URL" , e );
9093 } catch (ProtocolException e ) {
91- e .printStackTrace ();
9294 throw new BitsoAPIException (901 , "Unsupported HTTP method" , e );
9395 } catch (IOException e ) {
94- e .printStackTrace ();
96+ e .printStackTrace (System . err );
9597 return Helpers .convertInputStreamToString (connection .getErrorStream ());
9698 }
9799 }
98100
99101 public String sendPost (String url , String body , HashMap <String , String > headers , Charset charset )
100- throws ClientProtocolException , IOException {
102+ throws IOException {
101103 return sendPost (url , new StringEntity (body , charset ), headers );
102104 }
103105
104106 public String sendPost (String url , byte [] body , HashMap <String , String > headers )
105- throws ClientProtocolException , IOException {
106- return sendPost (url , new ByteArrayEntity (body ), headers );
107+ throws IOException {
108+ ContentType contentType = ContentType .APPLICATION_JSON ;
109+ if (headers != null && headers .containsKey (CONTENT_TYPE )) {
110+ try {
111+ contentType = ContentType .parse (headers .get (CONTENT_TYPE ));
112+ headers = new HashMap <>(headers );
113+ headers .remove (CONTENT_TYPE );
114+ } catch (UnsupportedCharsetException ex ) {
115+ //Revert to default
116+ }
117+ }
118+ return sendPost (url , new ByteArrayEntity (body , 0 , body .length , contentType ), headers );
107119 }
108120
109121 private String sendPost (String url , AbstractHttpEntity body , HashMap <String , String > headers )
110- throws ClientProtocolException , IOException {
122+ throws IOException {
111123 throttle ();
112124
113125 HttpPost postRequest = new HttpPost (url );
@@ -119,13 +131,13 @@ private String sendPost(String url, AbstractHttpEntity body, HashMap<String, Str
119131
120132 postRequest .setEntity (body );
121133
122- CloseableHttpResponse closeableHttpResponse = HttpClients .createDefault (). execute ( postRequest );
123- String response = Helpers . convertInputStreamToString ( closeableHttpResponse . getEntity (). getContent ());
124-
125- return response ;
134+ try ( CloseableHttpClient client = HttpClients .createDefault ();
135+ CloseableHttpResponse response = client . execute ( postRequest )) {
136+ return Helpers . convertInputStreamToString ( response . getEntity (). getContent ());
137+ }
126138 }
127139
128- public String sendDelete (String url , HashMap <String , String > headers ) throws BitsoAPIException {
140+ public String sendDelete (String url , Map <String , String > headers ) throws BitsoAPIException {
129141 throttle ();
130142 HttpDelete deleteURL = new HttpDelete (url );
131143
@@ -135,16 +147,12 @@ public String sendDelete(String url, HashMap<String, String> headers) throws Bit
135147 }
136148 }
137149
138- CloseableHttpClient closeableHttpClient = HttpClients .createDefault ();
139- CloseableHttpResponse response = null ;
140- try {
141- response = closeableHttpClient .execute (deleteURL );
150+ try (CloseableHttpClient closeableHttpClient = HttpClients .createDefault ();
151+ CloseableHttpResponse response = closeableHttpClient .execute (deleteURL )) {
142152 return Helpers .convertInputStreamToString (response .getEntity ().getContent ());
143153 } catch (ClientProtocolException e ) {
144- e .printStackTrace ();
145154 throw new BitsoAPIException (901 , "Usupported HTTP method" , e );
146155 } catch (IOException e ) {
147- e .printStackTrace ();
148156 throw new BitsoAPIException (101 , "Connection Aborted" , e );
149157 }
150158 }
0 commit comments