Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 36 additions & 14 deletions java/src/main/java/com/genexus/internet/GXRestAPIClient.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
package com.genexus.internet;

import json.org.json.*;
import java.util.*;
import java.text.*;
import json.org.json.JSONException;
import json.org.json.JSONObject;
import java.util.HashMap;
import java.util.Iterator;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.Date;
import json.org.json.IJsonFormattable;
import json.org.json.JSONArray;
import com.genexus.internet.IGxJSONAble;
import com.genexus.xml.GXXMLSerializable;
import com.genexus.internet.IGxJSONSerializable;
import com.genexus.CommonUtil;
import com.genexus.common.interfaces.SpecificImplementation;
import com.genexus.*;
Expand All @@ -14,14 +23,15 @@ public class GXRestAPIClient {

public static final ILogger logger = LogManager.getLogger(GXRestAPIClient.class);
private HttpClient httpClient;

private String name;
private Location location;
private String protocol = "REST";
private String httpMethod = "GET";
private int statusCode;
private int errorCode;
private String errorMessage;
private int responseCode;
private Integer responseCode;
private String responseMessage;

private String contentType = "application/json; charset=utf-8";
Expand All @@ -36,10 +46,13 @@ public class GXRestAPIClient {
static final String DATE_FMT = "yyyy-MM-dd";
static final String DATETIME_FMT = "yyyy-MM-dd'T'HH:mm:ss";
static final String DATETIME_FMT_MS = "yyyy-MM-dd'T'HH:mm:ss.SSS";

/* Error constants */

static final int RESPONSE_ERROR_CODE = 2;
static final int PARSING_ERROR_CODE = 3;
static final int DESERIALIZING_ERROR_CODE = 4;

static final String RESPONSE_ERROR_MSG = "Invalid response";
static final String PARSING_ERROR_MSG = "Error parsing response";
static final String DESERIALIZING_ERROR_MSG = "Error serializing/deserializing object";
Expand Down Expand Up @@ -333,7 +346,7 @@ else if (jsonResponse.length()>= 1 && !jsonResponse.has(varName)) {
return null;
}
}
catch (JSONException e) {
catch (json.org.json.JSONException e) {
errorCode = PARSING_ERROR_CODE;
errorMessage = PARSING_ERROR_MSG;
logger.error(PARSING_ERROR_MSG + " " + sdtClass, e);
Expand All @@ -342,9 +355,19 @@ else if (jsonResponse.length()>= 1 && !jsonResponse.has(varName)) {
return sdt;
}

public <T extends GXXMLSerializable> GXBaseCollection<T> getBodyObjCollection(String varName, Class<T> elementClasss) {
public <T extends GxSilentTrnSdt> GXBCCollection<T> getBodyBCCollection(String varName, Class<T> elementClass) {
GXBCCollection<T> col = new GXBCCollection<T>();
fillCollection(varName,elementClass, col);
return col;
}

public <T extends GXXMLSerializable> GXBaseCollection<T> getBodyObjCollection(String varName, Class<T> elementClass) {
GXBaseCollection<T> col = new GXBaseCollection<T>();
fillCollection(varName, elementClass, col);
return col;
}
private <T extends GXXMLSerializable> void fillCollection(String varName, Class<T> elementClass, GXBaseCollection col) {
JSONArray jsonarr = new JSONArray();
GXBaseCollection<T> col = new GXBaseCollection<T>();
try {
if (jsonResponse.has(varName))
jsonarr = jsonResponse.getJSONArray(varName);
Expand All @@ -354,28 +377,27 @@ else if (jsonResponse.length() == 1 && jsonResponse.has(""))
if (jsonarr != null) {
for (int i=0; i < jsonarr.length(); i++) {
JSONObject o = jsonarr.getJSONObject(i);
T sdt = elementClasss.newInstance();
T sdt = elementClass.newInstance();
sdt.fromJSonString(o.toString(),null);
col.add(sdt);
}
}
else {
errorCode = RESPONSE_ERROR_CODE;
errorMessage = RESPONSE_ERROR_MSG;
logger.error(RESPONSE_ERROR_MSG + " " + elementClasss);
logger.error(RESPONSE_ERROR_MSG + " " + elementClass);
}
}
catch (JSONException e) {
catch (json.org.json.JSONException e) {
errorCode = PARSING_ERROR_CODE;
errorMessage = PARSING_ERROR_MSG;
logger.error(PARSING_ERROR_MSG + " " + elementClasss ,e );
logger.error(PARSING_ERROR_MSG + " " + elementClass ,e );
}
catch (Exception e) {
errorCode = DESERIALIZING_ERROR_CODE;
errorMessage = DESERIALIZING_ERROR_MSG;
logger.error(DESERIALIZING_ERROR_MSG + " " + elementClasss, e);
logger.error(DESERIALIZING_ERROR_MSG + " " + elementClass, e);
}
return col;
}

public <T extends Object> GXSimpleCollection<T> getBodyCollection(String varName, Class<T> elementClasss) {
Expand All @@ -394,7 +416,7 @@ else if (jsonResponse.length() == 1 && jsonResponse.has("")) {
coll.fromJSonString(jsonResponse.getString(varName), null);
}
}
catch (JSONException e) {
catch (json.org.json.JSONException e) {
errorCode = PARSING_ERROR_CODE;
errorMessage = PARSING_ERROR_MSG;
logger.error(PARSING_ERROR_MSG + " " + elementClasss, e);
Expand Down Expand Up @@ -444,7 +466,7 @@ public void RestExecute() {
serviceuri += queryString;
httpClient.execute( this.httpMethod, serviceuri);

if (httpClient.getStatusCode() >= 300 || httpClient.getErrCode() > 0) {
if (httpClient.getStatusCode() >= 300 || httpClient.getErrCode() > 0) {
errorCode = (httpClient.getErrCode() == 0)? 1 : httpClient.getErrCode();
errorMessage = httpClient.getErrDescription();
statusCode = httpClient.getStatusCode();
Expand Down