66import java .io .ByteArrayInputStream ;
77import java .io .IOException ;
88import java .io .InputStream ;
9+ import java .net .URI ;
910import java .util .Map ;
1011import java .util .concurrent .ExecutionException ;
1112import java .util .concurrent .Future ;
1920import org .apache .http .HttpStatus ;
2021import org .apache .http .client .config .RequestConfig ;
2122import org .apache .http .client .methods .HttpDelete ;
23+ import org .apache .http .client .methods .HttpEntityEnclosingRequestBase ;
2224import org .apache .http .client .methods .HttpGet ;
2325import org .apache .http .client .methods .HttpPost ;
26+ import org .apache .http .client .methods .HttpPut ;
2427import org .apache .http .entity .BufferedHttpEntity ;
2528import org .apache .http .entity .ContentType ;
2629import org .apache .http .entity .StringEntity ;
3336/**
3437 * ResponseHandler Clase mediante la cual se hacen las peticiones y
3538 * des-serializaciones de respuestas.
39+ *
3640 * @param <T> IResponse subclasses
3741 * @author Juan Gamez
3842 * @version 0.0.0.1
@@ -46,6 +50,7 @@ public abstract class ResponseHandler<T> {
4650
4751 /**
4852 * Este método realiza un HTTP POST con la configuracion enviada.
53+ *
4954 * @param url String url o host.
5055 * @param path String path
5156 * @param headers Map String String con headers.
@@ -79,7 +84,80 @@ public T postHTTPJson(String url, String path, Map<String, String> headers, Stri
7984 HttpEntity responseEntity = response .getEntity ();
8085 String responseBody = new String ();
8186 if (responseEntity != null ) {
82- responseBody = EntityUtils .toString (responseEntity );
87+ responseBody = EntityUtils .toString (responseEntity ,"UTF-8" );
88+ return deserialize (responseBody , contentClass );
89+ } else {
90+ throw new GeneralException (
91+ response .getStatusLine () != null ? response .getStatusLine ().getStatusCode ()
92+ : HttpStatus .SC_UNPROCESSABLE_ENTITY ,
93+ "Can´t get body from the request made." );
94+ }
95+ } else {
96+ throw new GeneralException (response .getStatusLine () != null ? response .getStatusLine ().getStatusCode ()
97+ : HttpStatus .SC_INTERNAL_SERVER_ERROR , "Error > 500 calling the service." );
98+ }
99+ } catch (IllegalArgumentException e ) {
100+ return handleException (e );
101+ } catch (GeneralException e ) {
102+ return handleException (e );
103+ } catch (InterruptedException e ) {
104+ return handleException (e );
105+ } catch (ExecutionException e ) {
106+ return handleException (e );
107+ } catch (TimeoutException e ) {
108+ return handleException (e );
109+ } catch (IOException e ) {
110+ return handleException (e );
111+ } catch (JsonSyntaxException e ) {
112+ return handleException (e );
113+ } catch (ServicesException e ) {
114+ return handleException (e );
115+ } finally {
116+ try {
117+ client .close ();
118+ } catch (IOException e ) {
119+ return handleException (e );
120+ }
121+ }
122+ }
123+
124+ /**
125+ * Este método realiza un HTTP POST con la configuracion enviada.
126+ *
127+ * @param url String url o host.
128+ * @param path String path
129+ * @param headers Map String String con headers.
130+ * @param jsonBody String body
131+ * @param configHTTP RequestConfig objeto de configuración.
132+ * @param contentClass Clase de respuesta esperada.
133+ * @return T
134+ */
135+ public T putHTTPJson (String url , String path , Map <String , String > headers , String jsonBody ,
136+ RequestConfig configHTTP , Class <T > contentClass ) {
137+ CloseableHttpAsyncClient client = HttpAsyncClients .createDefault ();
138+ try {
139+ client .start ();
140+ HttpPut request = new HttpPut (url + path );
141+ if (headers != null ) {
142+ for (Map .Entry <String , String > entry : headers .entrySet ()) {
143+ request .addHeader (new BasicHeader (entry .getKey (), entry .getValue ()));
144+ }
145+ }
146+ if (configHTTP != null ) {
147+ request .setConfig (configHTTP );
148+ }
149+ if (jsonBody != null ) {
150+ StringEntity sEntity = new StringEntity (jsonBody , "UTF-8" );
151+ request .setEntity (sEntity );
152+ }
153+ Future <HttpResponse > future = client .execute (request , null );
154+ HttpResponse response = future .get (MAX_EXECUTION_TIME , TimeUnit .MINUTES );
155+ if (response .getStatusLine () != null
156+ && response .getStatusLine ().getStatusCode () < HttpStatus .SC_INTERNAL_SERVER_ERROR ) {
157+ HttpEntity responseEntity = response .getEntity ();
158+ String responseBody = new String ();
159+ if (responseEntity != null ) {
160+ responseBody = EntityUtils .toString (responseEntity , "UTF-8" );
83161 return deserialize (responseBody , contentClass );
84162 } else {
85163 throw new GeneralException (
@@ -119,6 +197,7 @@ public T postHTTPJson(String url, String path, Map<String, String> headers, Stri
119197 /**
120198 * Este método realiza un HTTP POST (modo Multipart form data) con la
121199 * configuracion enviada.
200+ *
122201 * @param url String url o host.
123202 * @param path String path.
124203 * @param headers Map String String con headers.
@@ -155,7 +234,7 @@ public T postHTTPMultipart(String url, String path, Map<String, String> headers,
155234 HttpEntity responseEntity = response .getEntity ();
156235 String responseBody = new String ();
157236 if (responseEntity != null ) {
158- responseBody = EntityUtils .toString (responseEntity );
237+ responseBody = EntityUtils .toString (responseEntity , "UTF-8" );
159238 return deserialize (responseBody , contentClass );
160239 } else {
161240 throw new GeneralException (
@@ -198,6 +277,7 @@ public T postHTTPMultipart(String url, String path, Map<String, String> headers,
198277
199278 /**
200279 * Este método realiza un HTTP GET con la configuracion enviada.
280+ *
201281 * @param url String url o host.
202282 * @param path String path.
203283 * @param headers Map String String con headers.
@@ -269,6 +349,7 @@ public T getHTTP(String url, String path, Map<String, String> headers, RequestCo
269349
270350 /**
271351 * Este método realiza un HTTP DELTE con la configuracion enviada.
352+ *
272353 * @param url String url o host.
273354 * @param path String path.
274355 * @param headers Map String String con headers.
@@ -290,26 +371,73 @@ public T deleteHTTP(String url, String path, Map<String, String> headers, Reques
290371 if (configHTTP != null ) {
291372 request .setConfig (configHTTP );
292373 }
374+
375+ Future <HttpResponse > future = client .execute (request , null );
376+ HttpResponse response = future .get (MAX_EXECUTION_TIME , TimeUnit .MINUTES );
377+ int statusCode = response .getStatusLine ().getStatusCode ();
378+ if (statusCode == 204 ) { // 204 No Content
379+ String responseBody = "{ \" data\" : \" Usuario borrado exitosamente.\" , \" status\" : \" success\" }" ;
380+ return deserialize (responseBody , contentClass );
381+ } else if (statusCode < HttpStatus .SC_INTERNAL_SERVER_ERROR ) {
382+ HttpEntity responseEntity = response .getEntity ();
383+ String responseBody = "" ;
384+ if (responseEntity != null ) {
385+ responseBody = EntityUtils .toString (responseEntity , "UTF-8" );
386+ return deserialize (responseBody , contentClass );
387+ } else {
388+ throw new GeneralException (statusCode , "Can´t get body from the request made." );
389+ }
390+ } else {
391+ throw new GeneralException (statusCode , "Error > 500 calling the service." );
392+ }
393+ } catch (IllegalArgumentException | GeneralException | InterruptedException | ExecutionException
394+ | TimeoutException | IOException | JsonSyntaxException | ServicesException e ) {
395+ return handleException (e );
396+ } finally {
397+ try {
398+ client .close ();
399+ } catch (IOException e ) {
400+ return handleException (e );
401+ }
402+ }
403+ }
404+
405+ public T deleteHTTPJson (String url , String path , Map <String , String > headers , String jsonBody ,
406+ RequestConfig configHTTP , Class <T > contentClass ) {
407+ CloseableHttpAsyncClient client = HttpAsyncClients .createDefault ();
408+ try {
409+ client .start ();
410+ HttpDeleteWithBody request = new HttpDeleteWithBody (url + path );
411+ if (headers != null ) {
412+ for (Map .Entry <String , String > entry : headers .entrySet ()) {
413+ request .addHeader (new BasicHeader (entry .getKey (), entry .getValue ()));
414+ }
415+ }
416+ if (configHTTP != null ) {
417+ request .setConfig (configHTTP );
418+ }
419+ if (jsonBody != null ) {
420+ StringEntity sEntity = new StringEntity (jsonBody , "UTF-8" );
421+ request .setEntity (sEntity );
422+ }
293423 Future <HttpResponse > future = client .execute (request , null );
294424 HttpResponse response = future .get (MAX_EXECUTION_TIME , TimeUnit .MINUTES );
295425 if (response .getStatusLine () != null
296426 && response .getStatusLine ().getStatusCode () < HttpStatus .SC_INTERNAL_SERVER_ERROR ) {
297427 HttpEntity responseEntity = response .getEntity ();
298428 String responseBody = new String ();
299429 if (responseEntity != null ) {
300- responseBody = EntityUtils .toString (responseEntity , "UTF-8" );
430+ responseBody = EntityUtils .toString (responseEntity ,"UTF-8" );
301431 return deserialize (responseBody , contentClass );
302432 } else {
303433 throw new GeneralException (
304- response .getStatusLine () != null
305- ? response .getStatusLine ().getStatusCode ()
434+ response .getStatusLine () != null ? response .getStatusLine ().getStatusCode ()
306435 : HttpStatus .SC_UNPROCESSABLE_ENTITY ,
307- "Can´ t get body from the request made." );
436+ "Can' t get body from the request made." );
308437 }
309438 } else {
310439 throw new GeneralException (
311- response .getStatusLine () != null
312- ? response .getStatusLine ().getStatusCode ()
440+ response .getStatusLine () != null ? response .getStatusLine ().getStatusCode ()
313441 : HttpStatus .SC_INTERNAL_SERVER_ERROR ,
314442 "Error > 500 calling the service." );
315443 }
@@ -338,8 +466,23 @@ public T deleteHTTP(String url, String path, Map<String, String> headers, Reques
338466 }
339467 }
340468
469+ class HttpDeleteWithBody extends HttpEntityEnclosingRequestBase {
470+ public static final String METHOD_NAME = "DELETE" ;
471+
472+ public HttpDeleteWithBody (final String uri ) {
473+ super ();
474+ setURI (URI .create (uri ));
475+ }
476+
477+ @ Override
478+ public String getMethod () {
479+ return METHOD_NAME ;
480+ }
481+ }
482+
341483 /**
342484 * Este método realiza una deserializacion de un JSON al tipo de clase T.
485+ *
343486 * @param json String json.
344487 * @param contentClass Clase esperada de respuesta.
345488 * @return T
0 commit comments