Skip to content

Commit 319eaea

Browse files
authored
- Add Support for BC parameters in REST (API Object) (#659)
* - Add Support for BC parameters in REST (API Object) * - Add support for reading GXBCcollection from body.
1 parent d02108a commit 319eaea

File tree

1 file changed

+36
-14
lines changed

1 file changed

+36
-14
lines changed

java/src/main/java/com/genexus/internet/GXRestAPIClient.java

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
package com.genexus.internet;
22

3-
import json.org.json.*;
43
import java.util.*;
5-
import java.text.*;
4+
import json.org.json.JSONException;
5+
import json.org.json.JSONObject;
6+
import java.util.HashMap;
7+
import java.util.Iterator;
8+
import java.text.SimpleDateFormat;
9+
import java.text.ParseException;
10+
import java.util.Date;
11+
import json.org.json.IJsonFormattable;
12+
import json.org.json.JSONArray;
13+
import com.genexus.internet.IGxJSONAble;
614
import com.genexus.xml.GXXMLSerializable;
15+
import com.genexus.internet.IGxJSONSerializable;
716
import com.genexus.CommonUtil;
817
import com.genexus.common.interfaces.SpecificImplementation;
918
import com.genexus.*;
@@ -14,14 +23,15 @@ public class GXRestAPIClient {
1423

1524
public static final ILogger logger = LogManager.getLogger(GXRestAPIClient.class);
1625
private HttpClient httpClient;
26+
1727
private String name;
1828
private Location location;
1929
private String protocol = "REST";
2030
private String httpMethod = "GET";
2131
private int statusCode;
2232
private int errorCode;
2333
private String errorMessage;
24-
private int responseCode;
34+
private Integer responseCode;
2535
private String responseMessage;
2636

2737
private String contentType = "application/json; charset=utf-8";
@@ -36,10 +46,13 @@ public class GXRestAPIClient {
3646
static final String DATE_FMT = "yyyy-MM-dd";
3747
static final String DATETIME_FMT = "yyyy-MM-dd'T'HH:mm:ss";
3848
static final String DATETIME_FMT_MS = "yyyy-MM-dd'T'HH:mm:ss.SSS";
49+
3950
/* Error constants */
51+
4052
static final int RESPONSE_ERROR_CODE = 2;
4153
static final int PARSING_ERROR_CODE = 3;
4254
static final int DESERIALIZING_ERROR_CODE = 4;
55+
4356
static final String RESPONSE_ERROR_MSG = "Invalid response";
4457
static final String PARSING_ERROR_MSG = "Error parsing response";
4558
static final String DESERIALIZING_ERROR_MSG = "Error serializing/deserializing object";
@@ -333,7 +346,7 @@ else if (jsonResponse.length()>= 1 && !jsonResponse.has(varName)) {
333346
return null;
334347
}
335348
}
336-
catch (JSONException e) {
349+
catch (json.org.json.JSONException e) {
337350
errorCode = PARSING_ERROR_CODE;
338351
errorMessage = PARSING_ERROR_MSG;
339352
logger.error(PARSING_ERROR_MSG + " " + sdtClass, e);
@@ -342,9 +355,19 @@ else if (jsonResponse.length()>= 1 && !jsonResponse.has(varName)) {
342355
return sdt;
343356
}
344357

345-
public <T extends GXXMLSerializable> GXBaseCollection<T> getBodyObjCollection(String varName, Class<T> elementClasss) {
358+
public <T extends GxSilentTrnSdt> GXBCCollection<T> getBodyBCCollection(String varName, Class<T> elementClass) {
359+
GXBCCollection<T> col = new GXBCCollection<T>();
360+
fillCollection(varName,elementClass, col);
361+
return col;
362+
}
363+
364+
public <T extends GXXMLSerializable> GXBaseCollection<T> getBodyObjCollection(String varName, Class<T> elementClass) {
365+
GXBaseCollection<T> col = new GXBaseCollection<T>();
366+
fillCollection(varName, elementClass, col);
367+
return col;
368+
}
369+
private <T extends GXXMLSerializable> void fillCollection(String varName, Class<T> elementClass, GXBaseCollection col) {
346370
JSONArray jsonarr = new JSONArray();
347-
GXBaseCollection<T> col = new GXBaseCollection<T>();
348371
try {
349372
if (jsonResponse.has(varName))
350373
jsonarr = jsonResponse.getJSONArray(varName);
@@ -354,28 +377,27 @@ else if (jsonResponse.length() == 1 && jsonResponse.has(""))
354377
if (jsonarr != null) {
355378
for (int i=0; i < jsonarr.length(); i++) {
356379
JSONObject o = jsonarr.getJSONObject(i);
357-
T sdt = elementClasss.newInstance();
380+
T sdt = elementClass.newInstance();
358381
sdt.fromJSonString(o.toString(),null);
359382
col.add(sdt);
360383
}
361384
}
362385
else {
363386
errorCode = RESPONSE_ERROR_CODE;
364387
errorMessage = RESPONSE_ERROR_MSG;
365-
logger.error(RESPONSE_ERROR_MSG + " " + elementClasss);
388+
logger.error(RESPONSE_ERROR_MSG + " " + elementClass);
366389
}
367390
}
368-
catch (JSONException e) {
391+
catch (json.org.json.JSONException e) {
369392
errorCode = PARSING_ERROR_CODE;
370393
errorMessage = PARSING_ERROR_MSG;
371-
logger.error(PARSING_ERROR_MSG + " " + elementClasss ,e );
394+
logger.error(PARSING_ERROR_MSG + " " + elementClass ,e );
372395
}
373396
catch (Exception e) {
374397
errorCode = DESERIALIZING_ERROR_CODE;
375398
errorMessage = DESERIALIZING_ERROR_MSG;
376-
logger.error(DESERIALIZING_ERROR_MSG + " " + elementClasss, e);
399+
logger.error(DESERIALIZING_ERROR_MSG + " " + elementClass, e);
377400
}
378-
return col;
379401
}
380402

381403
public <T extends Object> GXSimpleCollection<T> getBodyCollection(String varName, Class<T> elementClasss) {
@@ -394,7 +416,7 @@ else if (jsonResponse.length() == 1 && jsonResponse.has("")) {
394416
coll.fromJSonString(jsonResponse.getString(varName), null);
395417
}
396418
}
397-
catch (JSONException e) {
419+
catch (json.org.json.JSONException e) {
398420
errorCode = PARSING_ERROR_CODE;
399421
errorMessage = PARSING_ERROR_MSG;
400422
logger.error(PARSING_ERROR_MSG + " " + elementClasss, e);
@@ -444,7 +466,7 @@ public void RestExecute() {
444466
serviceuri += queryString;
445467
httpClient.execute( this.httpMethod, serviceuri);
446468

447-
if (httpClient.getStatusCode() >= 300 || httpClient.getErrCode() > 0) {
469+
if (httpClient.getStatusCode() >= 300 || httpClient.getErrCode() > 0) {
448470
errorCode = (httpClient.getErrCode() == 0)? 1 : httpClient.getErrCode();
449471
errorMessage = httpClient.getErrDescription();
450472
statusCode = httpClient.getStatusCode();

0 commit comments

Comments
 (0)