22
33import exception .*;
44import httpClient .HttpClient ;
5- import org .apache .http .Consts ;
6- import org .apache .http .HttpEntity ;
7- import org .apache .http .HttpStatus ;
8- import org .apache .http .NameValuePair ;
5+ import org .apache .http .*;
96import org .apache .http .client .entity .UrlEncodedFormEntity ;
107import org .apache .http .client .methods .CloseableHttpResponse ;
118import org .apache .http .client .methods .HttpGet ;
@@ -34,55 +31,63 @@ public HttpClientImpl() {
3431 closeableHttpClient = HttpClients .createDefault ();
3532 }
3633
37- public HttpEntity getResponse (HttpRequestBase httpRequest ) throws IndixApiException , IOException {
34+ private CloseableHttpResponse getResponse (HttpRequestBase httpRequest ) throws IndixApiException , IOException {
3835
3936 CloseableHttpResponse response = closeableHttpClient .execute (httpRequest );
40- System .out .print (response );
41- try {
42- String message = response .getStatusLine ().getReasonPhrase ();
43- int status = response .getStatusLine ().getStatusCode ();
44- if (HttpStatus .SC_OK != status ) {
45- switch (status ) {
46- case HttpStatus .SC_BAD_REQUEST :
47- throw new BadRequestException (message );
48- case HttpStatus .SC_INTERNAL_SERVER_ERROR :
49- throw new InternalServerException (message );
50- case HttpStatus .SC_UNAUTHORIZED :
51- throw new UnauthorizedException (message );
52- case HttpStatus .SC_PAYMENT_REQUIRED :
53- throw new PaymentRequiredException (message );
54- case 429 : // too many requests - rate limited error
55- throw new TooManyRequestsException (message );
56- default :
57- throw new IndixApiException (status , message );
58- }
59- }
6037
61- return response .getEntity ();
38+ String message = response .getStatusLine ().getReasonPhrase ();
39+ int status = response .getStatusLine ().getStatusCode ();
40+
41+ if (HttpStatus .SC_OK != status ) {
6242
63- } finally {
43+ // we need to close the resources before we throw an exception
44+ //
6445 response .close ();
46+
47+ switch (status ) {
48+ case HttpStatus .SC_BAD_REQUEST :
49+ throw new BadRequestException (message );
50+ case HttpStatus .SC_INTERNAL_SERVER_ERROR :
51+ throw new InternalServerException (message );
52+ case HttpStatus .SC_UNAUTHORIZED :
53+ throw new UnauthorizedException (message );
54+ case HttpStatus .SC_PAYMENT_REQUIRED :
55+ throw new PaymentRequiredException (message );
56+ case 429 : // too many requests - rate limited error
57+ throw new TooManyRequestsException (message );
58+ default :
59+ throw new IndixApiException (status , message );
60+ }
6561 }
62+
63+ return response ;
6664 }
6765
6866 public String GET (URI uri ) throws IOException , IndixApiException {
6967
7068 HttpGet httpGet = new HttpGet (uri );
71- return EntityUtils .toString (getResponse (httpGet ));
69+
70+ try (CloseableHttpResponse response = getResponse (httpGet )) {
71+ return EntityUtils .toString (response .getEntity ());
72+ }
7273 }
7374
7475 public InputStream GETStream (URI uri ) throws IOException , IndixApiException {
7576
7677 HttpGet httpGet = new HttpGet (uri );
77- HttpEntity httpEntity = getResponse (httpGet );
78- return httpEntity .getContent ();
78+
79+ try (CloseableHttpResponse response = getResponse (httpGet )) {
80+ return response .getEntity ().getContent ();
81+ }
7982 }
8083
8184 public String POST (URI uri , List <NameValuePair > params ) throws IndixApiException , IOException {
8285
8386 HttpPost httpPost = new HttpPost (uri );
8487 httpPost .setEntity (new UrlEncodedFormEntity (params , Consts .UTF_8 ));
85- return EntityUtils .toString (getResponse (httpPost ));
88+ try (CloseableHttpResponse response = getResponse (httpPost )) {
89+ return EntityUtils .toString (response .getEntity ());
90+ }
8691 }
8792
8893 public String POST (URI uri , List <NameValuePair > params , File file ) throws IOException , IndixApiException {
@@ -105,7 +110,9 @@ public String POST(URI uri, List<NameValuePair> params, File file) throws IOExce
105110
106111 // process request
107112 //
108- return EntityUtils .toString (getResponse (httpPost ));
113+ try (CloseableHttpResponse response = getResponse (httpPost )) {
114+ return EntityUtils .toString (response .getEntity ());
115+ }
109116 }
110117
111118 public void close () throws IOException {
0 commit comments