From 0bad273a82595a45aa7d1eb9b1c02f36bec606c1 Mon Sep 17 00:00:00 2001 From: AlejandroP Date: Wed, 25 Jan 2023 14:50:11 -0300 Subject: [PATCH 1/2] - Add Support for BC parameters in REST (API Object) --- .../com/genexus/internet/GXRestAPIClient.java | 80 ++++++++++++++++++- 1 file changed, 79 insertions(+), 1 deletion(-) diff --git a/java/src/main/java/com/genexus/internet/GXRestAPIClient.java b/java/src/main/java/com/genexus/internet/GXRestAPIClient.java index d1183ca00..4cdbfda8d 100644 --- a/java/src/main/java/com/genexus/internet/GXRestAPIClient.java +++ b/java/src/main/java/com/genexus/internet/GXRestAPIClient.java @@ -9,6 +9,7 @@ import com.genexus.*; import com.genexus.diagnostics.core.ILogger; import com.genexus.diagnostics.core.LogManager; +import com.genexus.specific.java.GXSilentTrnSdt; public class GXRestAPIClient { @@ -303,6 +304,44 @@ else if (jsonResponse.length() == 1 && jsonResponse.has("")) return jsonstr; } + public T getBodyBC(String varName, Class sdtClass) { + T sdt; + try { + sdt = sdtClass.newInstance(); + } + catch (InstantiationException e) { + return null; + } + catch (IllegalAccessException e) { + return null; + } + try { + if (jsonResponse != null) { + if (jsonResponse.has(varName)) { + sdt.fromJSonString(jsonResponse.getString(varName)); + } + else if (jsonResponse.length() == 1 && jsonResponse.has("")) { + sdt.fromJSonString(jsonResponse.getString("")); + } + else if (jsonResponse.length()>= 1 && !jsonResponse.has(varName)) { + sdt.fromJSonString(httpClient.getString()); + } + } + else { + errorCode = RESPONSE_ERROR_CODE; + errorMessage = RESPONSE_ERROR_MSG; + logger.error(RESPONSE_ERROR_MSG + " " + sdtClass); + return null; + } + } + catch (JSONException e) { + errorCode = PARSING_ERROR_CODE; + errorMessage = PARSING_ERROR_MSG; + logger.error(PARSING_ERROR_MSG + " " + sdtClass, e); + return null; + } + return sdt; + } public T getBodyObj(String varName, Class sdtClass) { T sdt; try { @@ -342,6 +381,45 @@ else if (jsonResponse.length()>= 1 && !jsonResponse.has(varName)) { return sdt; } + + + public GXBCCollection getBodyBCCollection(String varName, Class elementClasss) + { + JSONArray jsonarr = new JSONArray(); + GXBCCollection col = new GXBCCollection(); + try { + if (jsonResponse.has(varName)) + jsonarr = jsonResponse.getJSONArray(varName); + else if (jsonResponse.length() == 1 && jsonResponse.has("")) + jsonarr = jsonResponse.getJSONArray(""); + + if (jsonarr != null) { + for (int i=0; i < jsonarr.length(); i++) { + JSONObject o = jsonarr.getJSONObject(i); + T sdt = elementClasss.getDeclaredConstructor().newInstance(); + sdt.fromJSonString(o.toString(),null); + col.add(sdt); + } + } + else { + errorCode = RESPONSE_ERROR_CODE; + errorMessage = RESPONSE_ERROR_MSG; + logger.error(RESPONSE_ERROR_MSG + " " + elementClasss); + } + } + catch (JSONException e) { + errorCode = PARSING_ERROR_CODE; + errorMessage = PARSING_ERROR_MSG; + logger.error(PARSING_ERROR_MSG + " " + elementClasss ,e ); + } + catch (Exception e) { + errorCode = DESERIALIZING_ERROR_CODE; + errorMessage = DESERIALIZING_ERROR_MSG; + logger.error(DESERIALIZING_ERROR_MSG + " " + elementClasss, e); + } + return col; + } + public GXBaseCollection getBodyObjCollection(String varName, Class elementClasss) { JSONArray jsonarr = new JSONArray(); GXBaseCollection col = new GXBaseCollection(); @@ -354,7 +432,7 @@ 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 = elementClasss.getDeclaredConstructor().newInstance(); sdt.fromJSonString(o.toString(),null); col.add(sdt); } From fec9ddfe74b5d73815fe69bc3e1409d08e83d969 Mon Sep 17 00:00:00 2001 From: AlejandroP Date: Wed, 8 Feb 2023 16:41:02 -0300 Subject: [PATCH 2/2] - Add support for reading GXBCcollection from body. --- .../com/genexus/internet/GXRestAPIClient.java | 122 +++++------------- 1 file changed, 33 insertions(+), 89 deletions(-) diff --git a/java/src/main/java/com/genexus/internet/GXRestAPIClient.java b/java/src/main/java/com/genexus/internet/GXRestAPIClient.java index 4cdbfda8d..a581e1925 100644 --- a/java/src/main/java/com/genexus/internet/GXRestAPIClient.java +++ b/java/src/main/java/com/genexus/internet/GXRestAPIClient.java @@ -1,20 +1,29 @@ 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.*; import com.genexus.diagnostics.core.ILogger; import com.genexus.diagnostics.core.LogManager; -import com.genexus.specific.java.GXSilentTrnSdt; 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"; @@ -22,7 +31,7 @@ public class GXRestAPIClient { 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"; @@ -37,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"; @@ -304,44 +316,6 @@ else if (jsonResponse.length() == 1 && jsonResponse.has("")) return jsonstr; } - public T getBodyBC(String varName, Class sdtClass) { - T sdt; - try { - sdt = sdtClass.newInstance(); - } - catch (InstantiationException e) { - return null; - } - catch (IllegalAccessException e) { - return null; - } - try { - if (jsonResponse != null) { - if (jsonResponse.has(varName)) { - sdt.fromJSonString(jsonResponse.getString(varName)); - } - else if (jsonResponse.length() == 1 && jsonResponse.has("")) { - sdt.fromJSonString(jsonResponse.getString("")); - } - else if (jsonResponse.length()>= 1 && !jsonResponse.has(varName)) { - sdt.fromJSonString(httpClient.getString()); - } - } - else { - errorCode = RESPONSE_ERROR_CODE; - errorMessage = RESPONSE_ERROR_MSG; - logger.error(RESPONSE_ERROR_MSG + " " + sdtClass); - return null; - } - } - catch (JSONException e) { - errorCode = PARSING_ERROR_CODE; - errorMessage = PARSING_ERROR_MSG; - logger.error(PARSING_ERROR_MSG + " " + sdtClass, e); - return null; - } - return sdt; - } public T getBodyObj(String varName, Class sdtClass) { T sdt; try { @@ -372,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); @@ -381,48 +355,19 @@ else if (jsonResponse.length()>= 1 && !jsonResponse.has(varName)) { return sdt; } - - - public GXBCCollection getBodyBCCollection(String varName, Class elementClasss) - { - JSONArray jsonarr = new JSONArray(); - GXBCCollection col = new GXBCCollection(); - try { - if (jsonResponse.has(varName)) - jsonarr = jsonResponse.getJSONArray(varName); - else if (jsonResponse.length() == 1 && jsonResponse.has("")) - jsonarr = jsonResponse.getJSONArray(""); - - if (jsonarr != null) { - for (int i=0; i < jsonarr.length(); i++) { - JSONObject o = jsonarr.getJSONObject(i); - T sdt = elementClasss.getDeclaredConstructor().newInstance(); - sdt.fromJSonString(o.toString(),null); - col.add(sdt); - } - } - else { - errorCode = RESPONSE_ERROR_CODE; - errorMessage = RESPONSE_ERROR_MSG; - logger.error(RESPONSE_ERROR_MSG + " " + elementClasss); - } - } - catch (JSONException e) { - errorCode = PARSING_ERROR_CODE; - errorMessage = PARSING_ERROR_MSG; - logger.error(PARSING_ERROR_MSG + " " + elementClasss ,e ); - } - catch (Exception e) { - errorCode = DESERIALIZING_ERROR_CODE; - errorMessage = DESERIALIZING_ERROR_MSG; - logger.error(DESERIALIZING_ERROR_MSG + " " + elementClasss, e); - } + public GXBCCollection getBodyBCCollection(String varName, Class elementClass) { + GXBCCollection col = new GXBCCollection(); + fillCollection(varName,elementClass, col); return col; } - public GXBaseCollection getBodyObjCollection(String varName, Class elementClasss) { + public GXBaseCollection getBodyObjCollection(String varName, Class elementClass) { + GXBaseCollection col = new GXBaseCollection(); + fillCollection(varName, elementClass, col); + return col; + } + private void fillCollection(String varName, Class elementClass, GXBaseCollection col) { JSONArray jsonarr = new JSONArray(); - GXBaseCollection col = new GXBaseCollection(); try { if (jsonResponse.has(varName)) jsonarr = jsonResponse.getJSONArray(varName); @@ -432,7 +377,7 @@ 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.getDeclaredConstructor().newInstance(); + T sdt = elementClass.newInstance(); sdt.fromJSonString(o.toString(),null); col.add(sdt); } @@ -440,20 +385,19 @@ else if (jsonResponse.length() == 1 && jsonResponse.has("")) 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 GXSimpleCollection getBodyCollection(String varName, Class elementClasss) { @@ -472,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); @@ -522,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();