99import java .text .ParseException ;
1010
1111import org .json .JSONArray ;
12- import com .genexus .internet .IGxJSONAble ;
1312import com .genexus .xml .GXXMLSerializable ;
14- import com .genexus .internet .IGxJSONSerializable ;
1513import com .genexus .common .interfaces .SpecificImplementation ;
1614import com .genexus .*;
1715import com .genexus .diagnostics .core .ILogger ;
@@ -32,18 +30,15 @@ public class GXRestAPIClient {
3230 private String responseMessage ;
3331
3432 private String contentType = "application/json; charset=utf-8" ;
35- private String queryString = "" ;
36- private String bodyString = "" ;
33+ static final String CONTENT_TYPE_LABEL = "Content-Type " ;
34+ static final String AUTHORIZTION_LABEL = "Authorization " ;
3735
3836 private JSONObject jsonResponse ;
3937 private HashMap <String , String > queryVars = new HashMap <String , String >();
4038 private HashMap <String , String > bodyVars = new HashMap <String , String >();
4139 private HashMap <String , String > headerVars = new HashMap <String , String >();
42- private HashMap <String , String > responseData = new HashMap <String , String >();
4340
44- static final String DATE_FMT = "yyyy-MM-dd" ;
45- static final String DATETIME_FMT = "yyyy-MM-dd'T'HH:mm:ss" ;
46- static final String DATETIME_FMT_MS = "yyyy-MM-dd'T'HH:mm:ss.SSS" ;
41+
4742 static final String DATE_NULL = "0000-00-00" ;
4843 static final String DATETIME_NULL = "0000-00-00T00:00:00" ;
4944
@@ -55,6 +50,40 @@ public class GXRestAPIClient {
5550 static final String RESPONSE_ERROR_MSG = "Invalid response" ;
5651 static final String PARSING_ERROR_MSG = "Error parsing response" ;
5752 static final String DESERIALIZING_ERROR_MSG = "Error serializing/deserializing object" ;
53+
54+ public enum DateFormat {
55+
56+ DATE_FMT (1 , "yyyy-MM-dd" ),
57+ DATETIME_FMT (3 , "yyyy-MM-dd'T'HH:mm:ss" ),
58+ DATETIME_FMT_MS (4 , "yyyy-MM-dd'T'HH:mm:ss.SSS" );
59+
60+ private final int fmtId ;
61+ private final String fmtString ;
62+
63+ DateFormat (int fmtId , String fmtString ) {
64+ this .fmtId = fmtId ;
65+ this .fmtString = fmtString ;
66+ }
67+
68+ public int getId () {
69+ return fmtId ;
70+ }
71+
72+ public String getFormat () {
73+ return fmtString ;
74+ }
75+
76+ public static DateFormat fromId (int fmtId ) {
77+ for (DateFormat format : DateFormat .values ()) {
78+ if (format .getId () == fmtId ) {
79+ return format ;
80+ }
81+ }
82+ throw new IllegalArgumentException ("Unknown error code: " + fmtId );
83+ }
84+ }
85+
86+
5887
5988 public GXRestAPIClient () {
6089 responseCode = 0 ;
@@ -152,16 +181,11 @@ public void addQueryVar(String varName, double varValue) {
152181 queryVars .put (varName , Double .toString (varValue ));
153182 }
154183
155- public void addQueryVar (String varName , Date varValue ) {
156- SimpleDateFormat df = new SimpleDateFormat (DATE_FMT );
157- queryVars .put (varName , df .format (varValue ));
158- }
184+ public void addQueryVar (String varName , Date varValue , int fmtId ) {
185+ DateFormat fmt = DateFormat .fromId (fmtId );
186+ String fmtString = fmt .getFormat ();
159187
160- public void addQueryVar (String varName , Date varValue , boolean hasMilliseconds ) {
161- String fmt = DATETIME_FMT ;
162- if (hasMilliseconds )
163- fmt = DATETIME_FMT_MS ;
164- SimpleDateFormat df = new SimpleDateFormat (fmt );
188+ SimpleDateFormat df = new SimpleDateFormat (fmtString );
165189 queryVars .put (varName , df .format (varValue ));
166190 }
167191
@@ -213,17 +237,12 @@ public void addHeaderVar(String varName, double varValue) {
213237 headerVars .put (varName , quoteString (Double .toString (varValue )));
214238 }
215239
216- public void addHeaderVar (String varName , Date varValue ) {
217- SimpleDateFormat df = new SimpleDateFormat (DATE_FMT );
218- headerVars .put (varName , quoteString (df .format (varValue )));
219- }
240+ public void addHeaderVar (String varName , Date varValue , int fmtId ) {
241+ DateFormat fmt = DateFormat .fromId (fmtId );
242+ String fmtString = fmt .getFormat ();
220243
221- public void addHeaderVar (String varName , Date varValue , boolean hasMilliseconds ) {
222- String fmt = DATETIME_FMT ;
223- if (hasMilliseconds )
224- fmt = DATETIME_FMT_MS ;
225- SimpleDateFormat df = new SimpleDateFormat (fmt );
226- headerVars .put ( varName , quoteString (df .format (varValue )));
244+ SimpleDateFormat df = new SimpleDateFormat (fmtString );
245+ headerVars .put (varName , df .format (varValue ));
227246 }
228247
229248 public void addHeaderVar (String varName , short varValue ) {
@@ -271,21 +290,11 @@ public void addBodyVar(String varName, GXXMLSerializable varValue) {
271290 public void addBodyVar (String varName , String varValue ) {
272291 bodyVars .put ( varName , quoteString (varValue ));
273292 }
274-
275- public void addBodyVar (String varName , double varValue ) {
276- bodyVars .put (varName , quoteString (Double .toString (varValue )));
277- }
278-
279- public void addBodyVar (String varName , Date varValue ) {
280- SimpleDateFormat df = new SimpleDateFormat (DATE_FMT );
281- bodyVars .put (varName , quoteString (df .format (varValue )));
282- }
283-
284- public void addBodyVar (String varName , Date varValue , boolean hasMilliseconds ) {
285- String fmt = DATETIME_FMT ;
286- if (hasMilliseconds )
287- fmt = DATETIME_FMT_MS ;
288- SimpleDateFormat df = new SimpleDateFormat (fmt );
293+
294+ public void addBodyVar (String varName , Date varValue , int fmtId ) {
295+ DateFormat fmt = DateFormat .fromId (fmtId );
296+ String fmtString = fmt .getFormat ();
297+ SimpleDateFormat df = new SimpleDateFormat (fmtString );
289298 bodyVars .put ( varName , quoteString (df .format (varValue )));
290299 }
291300
@@ -327,7 +336,7 @@ public Date getBodyDate(String varName) {
327336 if (val .startsWith (DATE_NULL ))
328337 return CommonUtil .newNullDate ();
329338 else
330- return new SimpleDateFormat (DATE_FMT ).parse (val );
339+ return new SimpleDateFormat (DateFormat . DATETIME_FMT . getFormat () ).parse (val );
331340 }
332341 catch (ParseException e ) {
333342 return CommonUtil .newNullDate ();
@@ -337,9 +346,9 @@ public Date getBodyDate(String varName) {
337346 public Date getBodyDateTime (String varName , Boolean hasMilliseconds ) {
338347 try {
339348 String val = getJsonStr (varName );
340- String fmt = DATETIME_FMT ;
349+ String fmt = DateFormat . DATETIME_FMT . getFormat () ;
341350 if (hasMilliseconds )
342- fmt = DATETIME_FMT_MS ;
351+ fmt = DateFormat . DATETIME_FMT_MS . getFormat () ;
343352
344353 if (val .startsWith (DATETIME_NULL ))
345354 return CommonUtil .newNullDate ();
@@ -382,16 +391,14 @@ public BigDecimal getBodyNum(String varName) {
382391 return new BigDecimal (getJsonStr (varName ));
383392 }
384393
385- public long getBodyLong (String varName ) {
386- long value =0 ;
394+ public long getBodyLong (String varName ) {
387395 try {
388- value = Long .parseLong (getJsonStr (varName ));
396+ return Long .parseLong (getJsonStr (varName ));
389397 }
390398 catch (NumberFormatException ex )
391399 {
392- value = Double .valueOf (getJsonStr (varName )).longValue ();
393- }
394- return value ;
400+ return Double .valueOf (getJsonStr (varName )).longValue ();
401+ }
395402 }
396403
397404 public int getBodyInt (String varName ) {
@@ -551,46 +558,46 @@ public void addUploadFile(String filePath, String fileName) {
551558
552559 public void RestExecute () {
553560 String separator = "" ;
554- queryString = "" ;
561+ String queryString = "" ;
562+ String bodyString = "" ;
563+
555564 if (queryVars .size () > 0 ) {
556565 separator = "?" ;
557566 for ( Map .Entry <String , String > entry : queryVars .entrySet ()) {
558567 queryString += String .format ("%s%s=%s" , separator , entry .getKey (), entry .getValue ());
559568 separator = "&" ;
560569 }
561570 }
562- bodyString = "" ;
563571 if (bodyVars .size () > 0 ) {
564572 separator = "" ;
565573 for ( Map .Entry <String , String > entry : bodyVars .entrySet ()) {
566- bodyString += separator + "\" " + entry .getKey () + "\" :" + entry .getValue () + "" ;
574+ bodyString += separator + "\" " + entry .getKey () + "\" :" + entry .getValue ();
567575 separator = "," ;
568576 }
569577 }
570578 if (bodyString .length () > 0 ) {
571579 bodyString = "{" + bodyString + "}" ;
572580 httpClient .addString ( bodyString );
573- httpClient .addHeader ( "Content-Type" , contentType );
581+ httpClient .addHeader ( CONTENT_TYPE_LABEL , contentType );
574582 }
575583 else {
576- if (httpMethod == "POST" || httpMethod == "PUT" ) {
584+ if (httpMethod . equals ( "POST" ) || httpMethod . equals ( "PUT" ) ) {
577585 bodyString = "{}" ;
578586 httpClient .addString (bodyString );
579- httpClient .addHeader ("Content-Type" , contentType );
587+ httpClient .addHeader (CONTENT_TYPE_LABEL , contentType );
580588 }
581589 }
582590 if (location .getAuthenticationMethod () == 4 && location .getAccessToken () != null && ! location .getAccessToken ().trim ().isEmpty ()) {
583- httpClient .addHeader ("Authorization" , location .getAccessToken ());
591+ httpClient .addHeader (AUTHORIZTION_LABEL , location .getAccessToken ());
584592 }
585- if (headerVars .size () > 0 ) {
586- separator = "" ;
593+ if (headerVars .size () > 0 ) {
587594 for ( Map .Entry <String , String > entry : headerVars .entrySet ()) {
588595 httpClient .addHeader (entry .getKey (), entry .getValue ());
589596 }
590597 }
591598 headerVars .clear ();
592599 String serviceuri = ((location .getSecure () > 0 ) ? "https" : "http" ) + "://" + location .getHost ();
593- serviceuri += (location .getPort () != 80 ) ? ":" + Integer . toString ( location .getPort () ): "" ;
600+ serviceuri += (location .getPort () != 80 ) ? ":" + location .getPort (): "" ;
594601 serviceuri += "/" + location .getBaseURL () + "/" + location .getResourceName ();
595602 serviceuri += queryString ;
596603 httpClient .execute ( this .httpMethod , serviceuri );
@@ -622,9 +629,9 @@ public void RestExecute() {
622629 }
623630
624631 private void logError (int code , String msg ) {
625- logger .error ("Error: " + Integer . toString ( code ) + " " + msg );
632+ logger .error ("Error: " + code + " " + msg );
626633 }
627634 private void logError (int code , String msg , Exception e ) {
628- logger .error ("Error: " + Integer . toString ( code ) + " " + msg , e );
635+ logger .error ("Error: " + code + " " + msg , e );
629636 }
630637}
0 commit comments