From 53458cea44b644fcc62c9ec0b13825c13facb36c Mon Sep 17 00:00:00 2001 From: AlejandroP Date: Fri, 22 Jul 2022 16:42:37 -0300 Subject: [PATCH 01/15] - Add Rest API Client implementation. --- .../com/genexus/internet/GXRestAPIClient.java | 450 ++++++++++++++++++ 1 file changed, 450 insertions(+) create mode 100644 java/src/main/java/com/genexus/internet/GXRestAPIClient.java diff --git a/java/src/main/java/com/genexus/internet/GXRestAPIClient.java b/java/src/main/java/com/genexus/internet/GXRestAPIClient.java new file mode 100644 index 000000000..7588baba0 --- /dev/null +++ b/java/src/main/java/com/genexus/internet/GXRestAPIClient.java @@ -0,0 +1,450 @@ +package com.genexus.internet; + +import java.util.*; +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.CommonUtil; +import com.genexus.*; + +public class GXRestAPIClient{ + + private HttpClient httpClient; + + private String name; + private Location location; + private String protocol = "REST"; + private String httpMethod = "GET"; + private short errorCode; + private String errorMessage; + private Integer responseCode; + private String responseMessage; + + private String contentType = "application/json; charset=utf-8"; + private String queryString = ""; + private String bodyString = ""; + + private JSONObject jsonResponse; + private HashMap queryVars = new HashMap(); + private HashMap bodyVars = new HashMap(); + //private HashMap pathVars = new HashMap(); + private HashMap responseData = new HashMap(); + + public GXRestAPIClient() + { + responseCode = 0; + responseMessage = ""; + httpClient = new HttpClient(); + location = new Location(); + location.setBaseURL("api"); + location.setHost( "www.example.com"); + location.setResourceName("service"); + location.setPort(80); + } + + /* Gets */ + + public String getName() { + return name; + } + + public Location getLocation() { + return location; + } + + public String getProtocol() { + return protocol; + } + + public String getHttpMethod() { + return httpMethod; + } + + public short getErrorCode() { + return errorCode; + } + + public String getErrorMessage() { + return errorMessage; + } + + /* Sets */ + + public void setName( String value) { + name = value; + } + + public void setLocation( Location value) { + location = value; + } + + public void setProtocol( String value) { + protocol = value; + } + + public void setHttpMethod( String value) { + httpMethod = value; + } + + public void setErrorCode( short value) { + errorCode = value; + } + + public void setErrorMessage( String value) { + errorMessage = value; + } + + + public void addQueryVar(String varName, String varValue) + { + queryVars.put(varName, varValue); + } + + public void addQueryVar(String varName, int varValue) + { + queryVars.put(varName, Integer.toString(varValue)); + } + + public void addQueryVar(String varName, short varValue) + { + queryVars.put(varName, String.valueOf(varValue)); + } + + public void addQueryVar(String varName, double varValue) + { + queryVars.put(varName, Double.toString(varValue)); + } + + public void addQueryVar(String varName, Date varValue) + { + SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); + queryVars.put(varName, df.format(varValue)); + } + + public void addQueryVar(String varName, Date varValue, boolean hasMilliseconds) + { + String fmt = "yyyy-MM-dd'T'HH:mm:ss"; + if (hasMilliseconds) + fmt = "yyyy-MM-dd'T'HH:mm:ss.fff"; + SimpleDateFormat df = new SimpleDateFormat(fmt); + queryVars.put(varName, df.format(varValue)); + } + + public void addQueryVar(String varName, java.util.UUID varValue) + { + queryVars.put(varName, varValue.toString()); + } + + public void addQueryVar(String varName, Boolean varValue) + { + queryVars.put(varName, varValue.toString()); + } + + + private String quoteString(String value) + { + return "\"" + value + "\""; + } + + public void addBodyVar(String varName, GxUserType varValue) + { + if ( varValue != null) + { + bodyVars.put(varName, varValue.toJSonString(false)); + } + } + + public void addBodyVar(String varName, GXBaseCollection varValue) + { + if ( varValue != null) + { + bodyVars.put(varName, varValue.toJSonString(false)); + } + } + + public void addBodyVar(String varName, String varValue) + { + bodyVars.put( varName, quoteString(varValue)); + } + + public void addBodyVar(String varName, double varValue) + { + bodyVars.put(varName, quoteString(Double.toString(varValue))); + } + + public void addBodyVar(String varName, Date varValue) + { + SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); + bodyVars.put(varName, quoteString(df.format(varValue))); + } + + public void addBodyVar(String varName, Date varValue, boolean hasMilliseconds) + { + String fmt = "yyyy-MM-dd'T'HH:mm:ss"; + if (hasMilliseconds) + fmt = "yyyy-MM-dd'T'YHH:mm:ss.fff"; + SimpleDateFormat df = new SimpleDateFormat(fmt); + bodyVars.put( varName, quoteString(df.format(varValue))); + } + + public void addBodyVar(String varName, int varValue) + { + bodyVars.put( varName, Integer.toString(varValue)); + } + + public void addBodyVar(String varName, Boolean varValue) + { + bodyVars.put( varName, varValue.toString()); + } + + public void addBodyVar(String varName, java.util.UUID varValue) + { + bodyVars.put( varName, quoteString(varValue.toString())); + } + + public String getBodyString(String varName) + { + return getJsonStr(varName); + } + + public Date getBodyDate(String varName) + { + try{ + return new SimpleDateFormat("yyyy-MM-dd").parse(getJsonStr(varName)); + } + catch (ParseException e) + { + return CommonUtil.newNullDate(); + } + } + + public Date getBodyDateTime(String varName, Boolean hasMilliseconds) + { + try{ + String fmt = "yyyy-MM-dd'T'HH:mm:ss"; + if (hasMilliseconds) + fmt = "yyyy-MM-dd'T'YHH:mm:ss.fff"; + return new SimpleDateFormat(fmt).parse(getJsonStr(varName)); + } + catch (ParseException e) + { + return CommonUtil.newNullDate(); + } + } + + public boolean getBodyBool(String varName) + { + return Boolean.parseBoolean(getJsonStr(varName)); + } + + public java.util.UUID getBodyGuid(String varName) + { + return java.util.UUID.fromString(getJsonStr(varName)); + } + + public Double getBodyNum(String varName) + { + return Double.parseDouble(getJsonStr(varName)); + } + + public int getBodyInt(String varName) + { + return Integer.parseInt(getJsonStr(varName)); + } + + public short getBodyShort(String varName) + { + return Short.parseShort(getJsonStr(varName)); + } + + public String getJsonStr(String varName) + { + String jsonstr = ""; + try{ + if (jsonResponse != null) { + if (jsonResponse.has(varName)) + jsonstr = jsonResponse.getString(varName); + else if (jsonResponse.length() == 1 && jsonResponse.has("")) + jsonstr = jsonResponse.getString(""); + } + else { + errorCode = 1; + errorMessage = "Invalid response"; + } + } + catch( JSONException e) + { + errorCode = 1; + errorMessage = "Invalid response"; + } + return jsonstr; + } + + public T getBodySdt(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), null); + } + else if (jsonResponse.length() == 1 && jsonResponse.has("")) { + sdt.fromJSonString(jsonResponse.getString(""), null); + } + else if (jsonResponse.length()>= 1 && !jsonResponse.has(varName)) + { + sdt.fromJSonString(httpClient.getString(), null); + } + } + else { + errorCode = 1; + errorMessage = "Invalid response"; + return null; + } + } + catch (json.org.json.JSONException e) { + errorCode = 1; + errorMessage = "Invalid response"; + return null; + } + return sdt; + } + + public GXBaseCollection getBodySdtCollection(String varName, Class elementClasss) + { + JSONArray jsonarr = new JSONArray(); + GXBaseCollection col = new GXBaseCollection(); + 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.newInstance(); + sdt.fromJSonString(o.toString(),null); + col.add(sdt); + } + } + else { + errorCode = 1; + errorMessage = "Invalid response"; + } + } + catch (json.org.json.JSONException e) + { + errorCode = 1; + errorMessage = "Invalid response" + e.toString(); + } + catch (Exception e) { + errorCode = 1; + errorMessage = "Invalid response" + e.toString(); + } + return col; + } + + public GXSimpleCollection getBodyCollection(String varName, Class elementClasss) + { + GXSimpleCollection coll; + try { + coll = new GXSimpleCollection(); + } + catch (Exception e) { + return null; + } + try { + if (jsonResponse.has(varName)) { + coll.fromJSonString(jsonResponse.getString(varName), null); + } + else if (jsonResponse.length() == 1 && jsonResponse.has("")) { + coll.fromJSonString(jsonResponse.getString(varName), null); + } + } + catch (json.org.json.JSONException e) + { + return null; + } + return coll; + } + + public void RestExecute() + { + String separator = ""; + queryString = ""; + if (queryVars.size() > 0) + { + separator = "?"; + for( Map.Entry entry : queryVars.entrySet()) + { + queryString += String.format("%s%s=%s", separator, entry.getKey(), entry.getValue()); + separator = "&"; + } + } + bodyString = ""; + if (bodyVars.size() > 0) + { + separator = ""; + for( Map.Entry entry : bodyVars.entrySet()) { + bodyString += separator + "\"" + entry.getKey() + "\":" + entry.getValue() + ""; + separator = ","; + } + } + if (bodyString.length() > 0) + { + bodyString = "{" + bodyString + "}"; + httpClient.addString( bodyString); + httpClient.addHeader( "Content-Type", contentType); + } + else + { + if (this.httpMethod == "POST" || this.httpMethod == "PUT") + { + bodyString = "{}"; + httpClient.addString(bodyString); + httpClient.addHeader("Content-Type", contentType); + } + } +System.out.println(" BODY " + bodyString); + String serviceuri = ((this.location.getSecure() > 0) ? "https" : "http") + "://" + this.location.getHost(); + serviceuri += (this.location.getPort() != 80) ? ":" + Integer.toString(this.location.getPort()): ""; + serviceuri += "/" + this.location.getBaseURL() + "/" + this.location.getResourceName(); + serviceuri += queryString; + + this.httpClient.execute( this.httpMethod, serviceuri); + if (httpClient.getStatusCode() >= 300 || httpClient.getErrCode() > 0) + { + this.errorCode = httpClient.getErrCode(); + this.errorMessage = httpClient.getErrDescription(); + } + else + { + try{ + System.out.println( httpClient.getString()); + jsonResponse = new JSONObject(httpClient.getString()); + } + catch(JSONException e) + { + System.out.println( e.toString()); + jsonResponse = new JSONObject(); + } + } + + } +} From fe2cd7e73ea996f1892f9bb030ebae1b4f700484 Mon Sep 17 00:00:00 2001 From: AlejandroP Date: Sun, 24 Jul 2022 23:36:48 -0300 Subject: [PATCH 02/15] - Eliminate debug/trace messages. --- java/src/main/java/com/genexus/internet/GXRestAPIClient.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/java/src/main/java/com/genexus/internet/GXRestAPIClient.java b/java/src/main/java/com/genexus/internet/GXRestAPIClient.java index 7588baba0..895542599 100644 --- a/java/src/main/java/com/genexus/internet/GXRestAPIClient.java +++ b/java/src/main/java/com/genexus/internet/GXRestAPIClient.java @@ -379,6 +379,7 @@ else if (jsonResponse.length() == 1 && jsonResponse.has("")) { } catch (json.org.json.JSONException e) { + System.out.println( e.toString()); return null; } return coll; @@ -421,7 +422,7 @@ public void RestExecute() httpClient.addHeader("Content-Type", contentType); } } -System.out.println(" BODY " + bodyString); + String serviceuri = ((this.location.getSecure() > 0) ? "https" : "http") + "://" + this.location.getHost(); serviceuri += (this.location.getPort() != 80) ? ":" + Integer.toString(this.location.getPort()): ""; serviceuri += "/" + this.location.getBaseURL() + "/" + this.location.getResourceName(); @@ -436,7 +437,6 @@ public void RestExecute() else { try{ - System.out.println( httpClient.getString()); jsonResponse = new JSONObject(httpClient.getString()); } catch(JSONException e) From e3b2f742e7a72d60af5860409bc8fbebed9620e7 Mon Sep 17 00:00:00 2001 From: AlejandroP Date: Wed, 17 Aug 2022 20:19:22 -0300 Subject: [PATCH 03/15] - Add remote object property management for APIObject calls --- .../genexus/properties/GXObjectProperties.java | 16 ++++++++++++++++ .../properties/GXObjectsConfiguration.java | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 java/src/main/java/com/genexus/properties/GXObjectProperties.java create mode 100644 java/src/main/java/com/genexus/properties/GXObjectsConfiguration.java diff --git a/java/src/main/java/com/genexus/properties/GXObjectProperties.java b/java/src/main/java/com/genexus/properties/GXObjectProperties.java new file mode 100644 index 000000000..41257f6cb --- /dev/null +++ b/java/src/main/java/com/genexus/properties/GXObjectProperties.java @@ -0,0 +1,16 @@ +package com.genexus.properties; + +import com.genexus.internet.*; + +public class GXObjectProperties +{ + private Location location = null; + public Location getLocation() + { + return location; + } + public void setLocation(Location value) + { + location = value; + } +} \ No newline at end of file diff --git a/java/src/main/java/com/genexus/properties/GXObjectsConfiguration.java b/java/src/main/java/com/genexus/properties/GXObjectsConfiguration.java new file mode 100644 index 000000000..ee47ac7ba --- /dev/null +++ b/java/src/main/java/com/genexus/properties/GXObjectsConfiguration.java @@ -0,0 +1,16 @@ +package com.genexus.properties; + +import com.genexus.internet.*; +import java.util.HashMap; + +public class GXObjectsConfiguration { + + private HashMap properties = new HashMap(); + + public GXObjectProperties propertiesFor(String objName) + { + if (!properties.containsKey(objName)) + properties.put(objName, new GXObjectProperties()); + return properties.get(objName); + } +} From 68b6912bf8c17c18e7d366d472873ca95ebb2b9c Mon Sep 17 00:00:00 2001 From: AlejandroP Date: Thu, 18 Aug 2022 04:01:44 -0300 Subject: [PATCH 04/15] - Added properties to handle errors. --- .../com/genexus/internet/GXRestAPIClient.java | 29 ++++++++++++----- .../properties/GXObjectProperties.java | 31 +++++++++++++++++++ 2 files changed, 52 insertions(+), 8 deletions(-) diff --git a/java/src/main/java/com/genexus/internet/GXRestAPIClient.java b/java/src/main/java/com/genexus/internet/GXRestAPIClient.java index 895542599..eae126982 100644 --- a/java/src/main/java/com/genexus/internet/GXRestAPIClient.java +++ b/java/src/main/java/com/genexus/internet/GXRestAPIClient.java @@ -23,7 +23,8 @@ public class GXRestAPIClient{ private Location location; private String protocol = "REST"; private String httpMethod = "GET"; - private short errorCode; + private int statusCode; + private int errorCode; private String errorMessage; private Integer responseCode; private String responseMessage; @@ -68,10 +69,14 @@ public String getHttpMethod() { return httpMethod; } - public short getErrorCode() { + public int getErrorCode() { return errorCode; } + public int getStatusCode() { + return statusCode; + } + public String getErrorMessage() { return errorMessage; } @@ -94,10 +99,14 @@ public void setHttpMethod( String value) { httpMethod = value; } - public void setErrorCode( short value) { + public void setStatusCode( int value) { + statusCode = value; + } + + public void setErrorCode( int value) { errorCode = value; } - + public void setErrorMessage( String value) { errorMessage = value; } @@ -428,14 +437,18 @@ public void RestExecute() serviceuri += "/" + this.location.getBaseURL() + "/" + this.location.getResourceName(); serviceuri += queryString; - this.httpClient.execute( this.httpMethod, serviceuri); + httpClient.execute( this.httpMethod, serviceuri); + if (httpClient.getStatusCode() >= 300 || httpClient.getErrCode() > 0) - { - this.errorCode = httpClient.getErrCode(); - this.errorMessage = httpClient.getErrDescription(); + { + + errorCode = (httpClient.getErrCode() == 0)? 1 : httpClient.getErrCode(); + errorMessage = httpClient.getErrDescription(); + statusCode = httpClient.getStatusCode(); } else { + statusCode = httpClient.getStatusCode(); try{ jsonResponse = new JSONObject(httpClient.getString()); } diff --git a/java/src/main/java/com/genexus/properties/GXObjectProperties.java b/java/src/main/java/com/genexus/properties/GXObjectProperties.java index 41257f6cb..b22405cd2 100644 --- a/java/src/main/java/com/genexus/properties/GXObjectProperties.java +++ b/java/src/main/java/com/genexus/properties/GXObjectProperties.java @@ -5,6 +5,10 @@ public class GXObjectProperties { private Location location = null; + private String errorMessage = ""; + private int errorCode = 0; + private int statusCode = 0; + public Location getLocation() { return location; @@ -13,4 +17,31 @@ public void setLocation(Location value) { location = value; } + + public int getErrorCode() + { + return errorCode; + } + public void setErrorCode(int value) + { + errorCode = value; + } + + public int getStatusCode() + { + return statusCode; + } + public void setStatusCode(int value) + { + statusCode = value; + } + + public String getErrorMessage() + { + return errorMessage; + } + public void setErrorMessage(String value) + { + errorMessage = value; + } } \ No newline at end of file From 34a2799b6dbf6875823bdace00aef8dc00e4099b Mon Sep 17 00:00:00 2001 From: AlejandroP Date: Wed, 31 Aug 2022 17:12:03 -0300 Subject: [PATCH 05/15] - Add method for file upload in Rest API Client --- .../java/com/genexus/internet/GXRestAPIClient.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/java/src/main/java/com/genexus/internet/GXRestAPIClient.java b/java/src/main/java/com/genexus/internet/GXRestAPIClient.java index eae126982..688f7b422 100644 --- a/java/src/main/java/com/genexus/internet/GXRestAPIClient.java +++ b/java/src/main/java/com/genexus/internet/GXRestAPIClient.java @@ -13,6 +13,7 @@ import com.genexus.internet.IGxJSONAble; import com.genexus.xml.GXXMLSerializable; import com.genexus.CommonUtil; +import com.genexus.common.interfaces.SpecificImplementation; import com.genexus.*; public class GXRestAPIClient{ @@ -388,12 +389,19 @@ else if (jsonResponse.length() == 1 && jsonResponse.has("")) { } catch (json.org.json.JSONException e) { - System.out.println( e.toString()); + System.err.println( e.toString()); return null; } return coll; } + public void addUploadFile(String filePath, String fileName) + { + httpClient.addFile(filePath, fileName); + String mimeType = SpecificImplementation.Application.getContentType(filePath); + contentType = mimeType; + } + public void RestExecute() { String separator = ""; @@ -431,7 +439,6 @@ public void RestExecute() httpClient.addHeader("Content-Type", contentType); } } - String serviceuri = ((this.location.getSecure() > 0) ? "https" : "http") + "://" + this.location.getHost(); serviceuri += (this.location.getPort() != 80) ? ":" + Integer.toString(this.location.getPort()): ""; serviceuri += "/" + this.location.getBaseURL() + "/" + this.location.getResourceName(); From cb03128ca542a08b3efbd295023ff5815ea69743 Mon Sep 17 00:00:00 2001 From: AlejandroP Date: Fri, 2 Sep 2022 16:20:41 -0300 Subject: [PATCH 06/15] - Fix for datetime format with milliseconds , the pattern was wrong --- .../src/main/java/com/genexus/internet/GXRestAPIClient.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/java/src/main/java/com/genexus/internet/GXRestAPIClient.java b/java/src/main/java/com/genexus/internet/GXRestAPIClient.java index 688f7b422..3218a6d40 100644 --- a/java/src/main/java/com/genexus/internet/GXRestAPIClient.java +++ b/java/src/main/java/com/genexus/internet/GXRestAPIClient.java @@ -143,7 +143,7 @@ public void addQueryVar(String varName, Date varValue, boolean hasMilliseconds) { String fmt = "yyyy-MM-dd'T'HH:mm:ss"; if (hasMilliseconds) - fmt = "yyyy-MM-dd'T'HH:mm:ss.fff"; + fmt = "yyyy-MM-dd'T'HH:mm:ss.SSS"; SimpleDateFormat df = new SimpleDateFormat(fmt); queryVars.put(varName, df.format(varValue)); } @@ -200,7 +200,7 @@ public void addBodyVar(String varName, Date varValue, boolean hasMilliseconds) { String fmt = "yyyy-MM-dd'T'HH:mm:ss"; if (hasMilliseconds) - fmt = "yyyy-MM-dd'T'YHH:mm:ss.fff"; + fmt = "yyyy-MM-dd'T'YHH:mm:ss.SSS"; SimpleDateFormat df = new SimpleDateFormat(fmt); bodyVars.put( varName, quoteString(df.format(varValue))); } @@ -241,7 +241,7 @@ public Date getBodyDateTime(String varName, Boolean hasMilliseconds) try{ String fmt = "yyyy-MM-dd'T'HH:mm:ss"; if (hasMilliseconds) - fmt = "yyyy-MM-dd'T'YHH:mm:ss.fff"; + fmt = "yyyy-MM-dd'T'YHH:mm:ss.SSS"; return new SimpleDateFormat(fmt).parse(getJsonStr(varName)); } catch (ParseException e) From 2a1c407c2015734e7eb62e8ed552e30c0f686b71 Mon Sep 17 00:00:00 2001 From: AlejandroP Date: Tue, 6 Sep 2022 11:37:23 -0300 Subject: [PATCH 07/15] -Added methods for BC objects and Decimal values in GXRestAPI --- .../com/genexus/internet/GXRestAPIClient.java | 104 +++++++++++++++++- 1 file changed, 101 insertions(+), 3 deletions(-) diff --git a/java/src/main/java/com/genexus/internet/GXRestAPIClient.java b/java/src/main/java/com/genexus/internet/GXRestAPIClient.java index 3218a6d40..1d7dd0a3a 100644 --- a/java/src/main/java/com/genexus/internet/GXRestAPIClient.java +++ b/java/src/main/java/com/genexus/internet/GXRestAPIClient.java @@ -158,18 +158,23 @@ public void addQueryVar(String varName, Boolean varValue) queryVars.put(varName, varValue.toString()); } + public void addQueryVar(String varName, java.math.BigDecimal varValue) + { + queryVars.put(varName, varValue.toString()); + } + private String quoteString(String value) { return "\"" + value + "\""; } - public void addBodyVar(String varName, GxUserType varValue) + public void addBodyVarBC(String varName, GXBaseCollection varValue) { if ( varValue != null) { bodyVars.put(varName, varValue.toJSonString(false)); - } + } } public void addBodyVar(String varName, GXBaseCollection varValue) @@ -179,6 +184,22 @@ public void addBodyVar(String varName, GXBaseCollection T getBodySdtTrn(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), null); + } + else if (jsonResponse.length() == 1 && jsonResponse.has("")) { + sdt.fromJSonString(jsonResponse.getString(""), null); + } + else if (jsonResponse.length()>= 1 && !jsonResponse.has(varName)) + { + sdt.fromJSonString(httpClient.getString(), null); + } + } + else { + errorCode = 1; + errorMessage = "Invalid response"; + return null; + } + } + catch (json.org.json.JSONException e) { + errorCode = 1; + errorMessage = "Invalid response"; + return null; + } + return sdt; + } + public T getBodySdt(String varName, Class sdtClass) { T sdt; @@ -335,7 +398,42 @@ else if (jsonResponse.length()>= 1 && !jsonResponse.has(varName)) return sdt; } - public GXBaseCollection getBodySdtCollection(String varName, Class elementClasss) + public GXBaseCollection getBodySdtCollection(String varName, Class elementClasss) + { + JSONArray jsonarr = new JSONArray(); + GXBaseCollection col = new GXBaseCollection(); + 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.newInstance(); + sdt.fromJSonString(o.toString(),null); + col.add(sdt); + } + } + else { + errorCode = 1; + errorMessage = "Invalid response"; + } + } + catch (json.org.json.JSONException e) + { + errorCode = 1; + errorMessage = "Invalid response" + e.toString(); + } + catch (Exception e) { + errorCode = 1; + errorMessage = "Invalid response" + e.toString(); + } + return col; + } + + public GXBaseCollection getBodySdtTrnCollection(String varName, Class elementClasss) { JSONArray jsonarr = new JSONArray(); GXBaseCollection col = new GXBaseCollection(); From 4c5c9f1ae90893d991d049c335b01438fe33247b Mon Sep 17 00:00:00 2001 From: AlejandroP Date: Tue, 6 Sep 2022 17:42:43 -0300 Subject: [PATCH 08/15] - Fixed type error in GetBodySdt methods names --- java/src/main/java/com/genexus/internet/GXRestAPIClient.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/java/src/main/java/com/genexus/internet/GXRestAPIClient.java b/java/src/main/java/com/genexus/internet/GXRestAPIClient.java index 1d7dd0a3a..6291a9ac3 100644 --- a/java/src/main/java/com/genexus/internet/GXRestAPIClient.java +++ b/java/src/main/java/com/genexus/internet/GXRestAPIClient.java @@ -398,7 +398,7 @@ else if (jsonResponse.length()>= 1 && !jsonResponse.has(varName)) return sdt; } - public GXBaseCollection getBodySdtCollection(String varName, Class elementClasss) + public GXBaseCollection getBodySdtTrnCollection(String varName, Class elementClasss) { JSONArray jsonarr = new JSONArray(); GXBaseCollection col = new GXBaseCollection(); @@ -433,7 +433,7 @@ else if (jsonResponse.length() == 1 && jsonResponse.has("")) return col; } - public GXBaseCollection getBodySdtTrnCollection(String varName, Class elementClasss) + public GXBaseCollection getBodySdtCollection(String varName, Class elementClasss) { JSONArray jsonarr = new JSONArray(); GXBaseCollection col = new GXBaseCollection(); From eef308c6d6819f13200a36a1b9247d1f9a2de410 Mon Sep 17 00:00:00 2001 From: AlejandroP Date: Tue, 6 Sep 2022 23:01:00 -0300 Subject: [PATCH 09/15] - Added Body Method for special objects in GXRestAPIClient --- .../com/genexus/internet/GXRestAPIClient.java | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/java/src/main/java/com/genexus/internet/GXRestAPIClient.java b/java/src/main/java/com/genexus/internet/GXRestAPIClient.java index 6291a9ac3..ce2e1f5a6 100644 --- a/java/src/main/java/com/genexus/internet/GXRestAPIClient.java +++ b/java/src/main/java/com/genexus/internet/GXRestAPIClient.java @@ -398,6 +398,44 @@ else if (jsonResponse.length()>= 1 && !jsonResponse.has(varName)) return sdt; } + + public T getBodyObj(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), null); + } + else if (jsonResponse.length() == 1 && jsonResponse.has("")) { + sdt.fromJSonString(jsonResponse.getString(""), null); + } + else if (jsonResponse.length()>= 1 && !jsonResponse.has(varName)) + { + sdt.fromJSonString(httpClient.getString(), null); + } + } + else { + errorCode = 1; + errorMessage = "Invalid response"; + return null; + } + } + catch (json.org.json.JSONException e) { + errorCode = 1; + errorMessage = "Invalid response"; + return null; + } + return sdt; + } + public GXBaseCollection getBodySdtTrnCollection(String varName, Class elementClasss) { JSONArray jsonarr = new JSONArray(); @@ -468,6 +506,42 @@ else if (jsonResponse.length() == 1 && jsonResponse.has("")) return col; } + public GXBaseCollection getBodyObjCollection(String varName, Class elementClasss) + { + JSONArray jsonarr = new JSONArray(); + GXBaseCollection col = new GXBaseCollection(); + 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.newInstance(); + sdt.fromJSonString(o.toString(),null); + col.add(sdt); + } + } + else { + errorCode = 1; + errorMessage = "Invalid response"; + } + } + catch (json.org.json.JSONException e) + { + errorCode = 1; + errorMessage = "Invalid response" + e.toString(); + } + catch (Exception e) { + errorCode = 1; + errorMessage = "Invalid response" + e.toString(); + } + return col; + } + + public GXSimpleCollection getBodyCollection(String varName, Class elementClasss) { GXSimpleCollection coll; From 37f9d00a02fb493862de02deb1f66424f9bc81e6 Mon Sep 17 00:00:00 2001 From: AlejandroP Date: Wed, 7 Sep 2022 14:50:32 -0300 Subject: [PATCH 10/15] - Fix format for datetime with milliseconds --- java/src/main/java/com/genexus/internet/GXRestAPIClient.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/java/src/main/java/com/genexus/internet/GXRestAPIClient.java b/java/src/main/java/com/genexus/internet/GXRestAPIClient.java index 3218a6d40..c720e4b3d 100644 --- a/java/src/main/java/com/genexus/internet/GXRestAPIClient.java +++ b/java/src/main/java/com/genexus/internet/GXRestAPIClient.java @@ -200,7 +200,7 @@ public void addBodyVar(String varName, Date varValue, boolean hasMilliseconds) { String fmt = "yyyy-MM-dd'T'HH:mm:ss"; if (hasMilliseconds) - fmt = "yyyy-MM-dd'T'YHH:mm:ss.SSS"; + fmt = "yyyy-MM-dd'T'HH:mm:ss.SSS"; SimpleDateFormat df = new SimpleDateFormat(fmt); bodyVars.put( varName, quoteString(df.format(varValue))); } @@ -241,7 +241,7 @@ public Date getBodyDateTime(String varName, Boolean hasMilliseconds) try{ String fmt = "yyyy-MM-dd'T'HH:mm:ss"; if (hasMilliseconds) - fmt = "yyyy-MM-dd'T'YHH:mm:ss.SSS"; + fmt = "yyyy-MM-dd'T'HH:mm:ss.SSS"; return new SimpleDateFormat(fmt).parse(getJsonStr(varName)); } catch (ParseException e) From 4e179fef735e922d53cfced547f598d1cc17d0b0 Mon Sep 17 00:00:00 2001 From: AlejandroP Date: Thu, 8 Sep 2022 14:14:17 -0300 Subject: [PATCH 11/15] - Add geography type routines for RestAPI calls --- .../main/java/com/genexus/GXGeospatial.java | 4 +-- .../internet/IGxGeoJSONSerializable.java | 11 ++++++++ .../main/java/com/genexus/GXGeospatial.java | 4 +-- .../com/genexus/internet/GXRestAPIClient.java | 27 +++++++++++++++++++ 4 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 common/src/main/java/com/genexus/internet/IGxGeoJSONSerializable.java diff --git a/android/src/main/java/com/genexus/GXGeospatial.java b/android/src/main/java/com/genexus/GXGeospatial.java index a3cd83415..f1082a7eb 100644 --- a/android/src/main/java/com/genexus/GXGeospatial.java +++ b/android/src/main/java/com/genexus/GXGeospatial.java @@ -18,12 +18,12 @@ //import org.noggit.JSONParser.*; import org.simpleframework.xml.Root; import org.simpleframework.xml.Text; - +import com.genexus.internet.IGxGeoJSONSerializable; import com.genexus.internet.IGxJSONSerializable; @Root -public final class GXGeospatial implements java.io.Serializable, IGxJSONSerializable{ +public final class GXGeospatial implements java.io.Serializable, IGxJSONSerializable, IGxGeoJSONSerializable{ private Shape innerShape; private String lastError; diff --git a/common/src/main/java/com/genexus/internet/IGxGeoJSONSerializable.java b/common/src/main/java/com/genexus/internet/IGxGeoJSONSerializable.java new file mode 100644 index 000000000..ee0a1ac2a --- /dev/null +++ b/common/src/main/java/com/genexus/internet/IGxGeoJSONSerializable.java @@ -0,0 +1,11 @@ +package com.genexus.internet; + +public interface IGxGeoJSONSerializable +{ + + String toJSonString(); + boolean fromJSonString(String s); + + String toGeoJSON(); + void fromGeoJSON(String s); +} \ No newline at end of file diff --git a/gxgeospatial/src/main/java/com/genexus/GXGeospatial.java b/gxgeospatial/src/main/java/com/genexus/GXGeospatial.java index 63142d891..054dc42e4 100644 --- a/gxgeospatial/src/main/java/com/genexus/GXGeospatial.java +++ b/gxgeospatial/src/main/java/com/genexus/GXGeospatial.java @@ -9,7 +9,7 @@ import com.genexus.internet.IGxJSONSerializable; import org.simpleframework.xml.*; import org.noggit.JSONParser.*; - +import com.genexus.internet.IGxGeoJSONSerializable; import org.locationtech.spatial4j.context.SpatialContext; import org.locationtech.spatial4j.context.SpatialContextFactory; import org.locationtech.spatial4j.context.jts.JtsSpatialContextFactory; @@ -31,7 +31,7 @@ @Root -public final class GXGeospatial implements java.io.Serializable, IGxJSONSerializable { +public final class GXGeospatial implements java.io.Serializable, IGxJSONSerializable, IGxGeoJSONSerializable { private Shape innerShape; private String lastError; diff --git a/java/src/main/java/com/genexus/internet/GXRestAPIClient.java b/java/src/main/java/com/genexus/internet/GXRestAPIClient.java index 0727d829a..b5b94b52b 100644 --- a/java/src/main/java/com/genexus/internet/GXRestAPIClient.java +++ b/java/src/main/java/com/genexus/internet/GXRestAPIClient.java @@ -11,6 +11,7 @@ import json.org.json.IJsonFormattable; import json.org.json.JSONArray; import com.genexus.internet.IGxJSONAble; +import com.genexus.internet.IGxGeoJSONSerializable; import com.genexus.xml.GXXMLSerializable; import com.genexus.CommonUtil; import com.genexus.common.interfaces.SpecificImplementation; @@ -153,6 +154,11 @@ public void addQueryVar(String varName, java.util.UUID varValue) queryVars.put(varName, varValue.toString()); } + public void addQueryVar(String varName, IGxGeoJSONSerializable varValue) + { + queryVars.put(varName, varValue.toJSonString()); + } + public void addQueryVar(String varName, Boolean varValue) { queryVars.put(varName, varValue.toString()); @@ -246,6 +252,11 @@ public void addBodyVar(String varName, java.util.UUID varValue) bodyVars.put( varName, quoteString(varValue.toString())); } + public void addBodyVar(String varName, IGxGeoJSONSerializable varValue) + { + bodyVars.put( varName, quoteString(varValue.toJSonString())); + } + public String getBodyString(String varName) { return getJsonStr(varName); @@ -286,6 +297,22 @@ public java.util.UUID getBodyGuid(String varName) return java.util.UUID.fromString(getJsonStr(varName)); } + public < T extends IGxGeoJSONSerializable> T getBodyGeospatial(String varName, Class sdtClass) + { + T sdt; + try { + sdt = sdtClass.newInstance(); + sdt.fromJSonString(getJsonStr(varName)); + return sdt; + } + catch (IllegalAccessException e) { + return null; + } + catch (InstantiationException e) { + return null; + } + } + public Double getBodyNum(String varName) { return Double.parseDouble(getJsonStr(varName)); From 6d7ab068727c848d871d0545b2b42bd8edec019d Mon Sep 17 00:00:00 2001 From: AlejandroP Date: Thu, 8 Sep 2022 14:52:22 -0300 Subject: [PATCH 12/15] - Fix for SDT o BC input vars in REST GET (APi Object) --- java/sample.db | Bin 8192 -> 8192 bytes .../com/genexus/internet/GXRestAPIClient.java | 15 +++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/java/sample.db b/java/sample.db index 98160152f4258944023c84074d9bb2716abd548f..ac3e81ee84f0209f9f4616675da8798f11fc8fc6 100644 GIT binary patch delta 17 YcmZp0XmFSy%_uTa#+gxMW5NP?04{R{761SM delta 17 YcmZp0XmFSy%_uxk#+gxgW5NP?04`(&6aWAK diff --git a/java/src/main/java/com/genexus/internet/GXRestAPIClient.java b/java/src/main/java/com/genexus/internet/GXRestAPIClient.java index b5b94b52b..817c281b6 100644 --- a/java/src/main/java/com/genexus/internet/GXRestAPIClient.java +++ b/java/src/main/java/com/genexus/internet/GXRestAPIClient.java @@ -169,6 +169,21 @@ public void addQueryVar(String varName, java.math.BigDecimal varValue) queryVars.put(varName, varValue.toString()); } + public void addQueryVar(String varName, GxSilentTrnSdt varValue) + { + if ( varValue != null) + { + queryVars.put(varName, varValue.toJSonString(false)); + } + } + + public void addQueryVar(String varName, GxUserType varValue) + { + if ( varValue != null) + { + queryVars.put(varName, varValue.toJSonString(false)); + } + } private String quoteString(String value) { From 835ffce1b493c459bf0ac4a51643dc6725e984ec Mon Sep 17 00:00:00 2001 From: AlejandroP Date: Fri, 9 Sep 2022 00:14:22 -0300 Subject: [PATCH 13/15] - Escape String and geography types in GET and DELETE Rest Services (API object) --- java/src/main/java/com/genexus/internet/GXRestAPIClient.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/java/src/main/java/com/genexus/internet/GXRestAPIClient.java b/java/src/main/java/com/genexus/internet/GXRestAPIClient.java index 817c281b6..f4db83db0 100644 --- a/java/src/main/java/com/genexus/internet/GXRestAPIClient.java +++ b/java/src/main/java/com/genexus/internet/GXRestAPIClient.java @@ -38,7 +38,6 @@ public class GXRestAPIClient{ private JSONObject jsonResponse; private HashMap queryVars = new HashMap(); private HashMap bodyVars = new HashMap(); - //private HashMap pathVars = new HashMap(); private HashMap responseData = new HashMap(); public GXRestAPIClient() @@ -116,7 +115,7 @@ public void setErrorMessage( String value) { public void addQueryVar(String varName, String varValue) { - queryVars.put(varName, varValue); + queryVars.put(varName, GXutil.URLEncode(varValue)); } public void addQueryVar(String varName, int varValue) @@ -156,7 +155,7 @@ public void addQueryVar(String varName, java.util.UUID varValue) public void addQueryVar(String varName, IGxGeoJSONSerializable varValue) { - queryVars.put(varName, varValue.toJSonString()); + queryVars.put(varName, GXutil.URLEncode(varValue.toJSonString())); } public void addQueryVar(String varName, Boolean varValue) From 845b8733fef25d6dc6f1818fec6919e87816e61c Mon Sep 17 00:00:00 2001 From: AlejandroP Date: Thu, 15 Sep 2022 15:33:58 -0300 Subject: [PATCH 14/15] - Remove IGeoJson interface and unify object parse routines. - Add constants and log for error management - Fix code style - Move GXObjectProperties implementation to gxcommon --- .../main/java/com/genexus/GXGeospatial.java | 4 +- .../internet/IGxGeoJSONSerializable.java | 11 - .../properties/GXObjectProperties.java | 2 +- .../properties/GXObjectsConfiguration.java | 1 - .../main/java/com/genexus/GXGeospatial.java | 3 +- .../com/genexus/internet/GXRestAPIClient.java | 459 +++++------------- 6 files changed, 130 insertions(+), 350 deletions(-) delete mode 100644 common/src/main/java/com/genexus/internet/IGxGeoJSONSerializable.java rename {java => common}/src/main/java/com/genexus/properties/GXObjectProperties.java (94%) rename {java => common}/src/main/java/com/genexus/properties/GXObjectsConfiguration.java (92%) diff --git a/android/src/main/java/com/genexus/GXGeospatial.java b/android/src/main/java/com/genexus/GXGeospatial.java index f1082a7eb..02a9cab58 100644 --- a/android/src/main/java/com/genexus/GXGeospatial.java +++ b/android/src/main/java/com/genexus/GXGeospatial.java @@ -18,12 +18,10 @@ //import org.noggit.JSONParser.*; import org.simpleframework.xml.Root; import org.simpleframework.xml.Text; -import com.genexus.internet.IGxGeoJSONSerializable; import com.genexus.internet.IGxJSONSerializable; - @Root -public final class GXGeospatial implements java.io.Serializable, IGxJSONSerializable, IGxGeoJSONSerializable{ +public final class GXGeospatial implements java.io.Serializable, IGxJSONSerializable{ private Shape innerShape; private String lastError; diff --git a/common/src/main/java/com/genexus/internet/IGxGeoJSONSerializable.java b/common/src/main/java/com/genexus/internet/IGxGeoJSONSerializable.java deleted file mode 100644 index ee0a1ac2a..000000000 --- a/common/src/main/java/com/genexus/internet/IGxGeoJSONSerializable.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.genexus.internet; - -public interface IGxGeoJSONSerializable -{ - - String toJSonString(); - boolean fromJSonString(String s); - - String toGeoJSON(); - void fromGeoJSON(String s); -} \ No newline at end of file diff --git a/java/src/main/java/com/genexus/properties/GXObjectProperties.java b/common/src/main/java/com/genexus/properties/GXObjectProperties.java similarity index 94% rename from java/src/main/java/com/genexus/properties/GXObjectProperties.java rename to common/src/main/java/com/genexus/properties/GXObjectProperties.java index b22405cd2..db9391d74 100644 --- a/java/src/main/java/com/genexus/properties/GXObjectProperties.java +++ b/common/src/main/java/com/genexus/properties/GXObjectProperties.java @@ -1,6 +1,6 @@ package com.genexus.properties; -import com.genexus.internet.*; +import com.genexus.internet.Location; public class GXObjectProperties { diff --git a/java/src/main/java/com/genexus/properties/GXObjectsConfiguration.java b/common/src/main/java/com/genexus/properties/GXObjectsConfiguration.java similarity index 92% rename from java/src/main/java/com/genexus/properties/GXObjectsConfiguration.java rename to common/src/main/java/com/genexus/properties/GXObjectsConfiguration.java index ee47ac7ba..d4edf1f90 100644 --- a/java/src/main/java/com/genexus/properties/GXObjectsConfiguration.java +++ b/common/src/main/java/com/genexus/properties/GXObjectsConfiguration.java @@ -1,6 +1,5 @@ package com.genexus.properties; -import com.genexus.internet.*; import java.util.HashMap; public class GXObjectsConfiguration { diff --git a/gxgeospatial/src/main/java/com/genexus/GXGeospatial.java b/gxgeospatial/src/main/java/com/genexus/GXGeospatial.java index 054dc42e4..3e72cc0de 100644 --- a/gxgeospatial/src/main/java/com/genexus/GXGeospatial.java +++ b/gxgeospatial/src/main/java/com/genexus/GXGeospatial.java @@ -9,7 +9,6 @@ import com.genexus.internet.IGxJSONSerializable; import org.simpleframework.xml.*; import org.noggit.JSONParser.*; -import com.genexus.internet.IGxGeoJSONSerializable; import org.locationtech.spatial4j.context.SpatialContext; import org.locationtech.spatial4j.context.SpatialContextFactory; import org.locationtech.spatial4j.context.jts.JtsSpatialContextFactory; @@ -31,7 +30,7 @@ @Root -public final class GXGeospatial implements java.io.Serializable, IGxJSONSerializable, IGxGeoJSONSerializable { +public final class GXGeospatial implements java.io.Serializable, IGxJSONSerializable { private Shape innerShape; private String lastError; diff --git a/java/src/main/java/com/genexus/internet/GXRestAPIClient.java b/java/src/main/java/com/genexus/internet/GXRestAPIClient.java index f4db83db0..7df1a1bdd 100644 --- a/java/src/main/java/com/genexus/internet/GXRestAPIClient.java +++ b/java/src/main/java/com/genexus/internet/GXRestAPIClient.java @@ -11,14 +11,17 @@ import json.org.json.IJsonFormattable; import json.org.json.JSONArray; import com.genexus.internet.IGxJSONAble; -import com.genexus.internet.IGxGeoJSONSerializable; 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; -public class GXRestAPIClient{ +public class GXRestAPIClient { + public static final ILogger logger = LogManager.getLogger(GXRestAPIClient.class); private HttpClient httpClient; private String name; @@ -40,16 +43,25 @@ public class GXRestAPIClient{ private HashMap bodyVars = new HashMap(); private HashMap responseData = new HashMap(); - public 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"; + + public GXRestAPIClient() { responseCode = 0; responseMessage = ""; httpClient = new HttpClient(); - location = new Location(); - location.setBaseURL("api"); - location.setHost( "www.example.com"); - location.setResourceName("service"); - location.setPort(80); + location = new Location(); } /* Gets */ @@ -113,239 +125,177 @@ public void setErrorMessage( String value) { } - public void addQueryVar(String varName, String varValue) - { + public void addQueryVar(String varName, String varValue) { queryVars.put(varName, GXutil.URLEncode(varValue)); } - public void addQueryVar(String varName, int varValue) - { + public void addQueryVar(String varName, int varValue) { queryVars.put(varName, Integer.toString(varValue)); } - public void addQueryVar(String varName, short varValue) - { + public void addQueryVar(String varName, short varValue) { queryVars.put(varName, String.valueOf(varValue)); } - public void addQueryVar(String varName, double varValue) - { + public void addQueryVar(String varName, double varValue) { queryVars.put(varName, Double.toString(varValue)); } - public void addQueryVar(String varName, Date varValue) - { - SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); + public void addQueryVar(String varName, Date varValue) { + SimpleDateFormat df = new SimpleDateFormat(DATE_FMT); queryVars.put(varName, df.format(varValue)); } - public void addQueryVar(String varName, Date varValue, boolean hasMilliseconds) - { - String fmt = "yyyy-MM-dd'T'HH:mm:ss"; + public void addQueryVar(String varName, Date varValue, boolean hasMilliseconds) { + String fmt = DATETIME_FMT; if (hasMilliseconds) - fmt = "yyyy-MM-dd'T'HH:mm:ss.SSS"; + fmt = DATETIME_FMT_MS; SimpleDateFormat df = new SimpleDateFormat(fmt); queryVars.put(varName, df.format(varValue)); } - public void addQueryVar(String varName, java.util.UUID varValue) - { + public void addQueryVar(String varName, java.util.UUID varValue) { queryVars.put(varName, varValue.toString()); } - public void addQueryVar(String varName, IGxGeoJSONSerializable varValue) - { - queryVars.put(varName, GXutil.URLEncode(varValue.toJSonString())); - } - - public void addQueryVar(String varName, Boolean varValue) - { + public void addQueryVar(String varName, Boolean varValue) { queryVars.put(varName, varValue.toString()); } - public void addQueryVar(String varName, java.math.BigDecimal varValue) - { + public void addQueryVar(String varName, java.math.BigDecimal varValue) { queryVars.put(varName, varValue.toString()); } - public void addQueryVar(String varName, GxSilentTrnSdt varValue) - { - if ( varValue != null) - { + public void addQueryVar(String varName, GXXMLSerializable varValue) { + if ( varValue != null) { queryVars.put(varName, varValue.toJSonString(false)); } } - public void addQueryVar(String varName, GxUserType varValue) - { - if ( varValue != null) - { - queryVars.put(varName, varValue.toJSonString(false)); - } + public void addQueryVar(String varName, IGxJSONSerializable varValue) { + queryVars.put(varName, GXutil.URLEncode(varValue.toJSonString())); } - private String quoteString(String value) - { + private String quoteString(String value) { return "\"" + value + "\""; } - public void addBodyVarBC(String varName, GXBaseCollection varValue) - { - if ( varValue != null) - { + public void addBodyVar(String varName, GXBaseCollection varValue) { + if ( varValue != null) { bodyVars.put(varName, varValue.toJSonString(false)); } } - public void addBodyVar(String varName, GXBaseCollection varValue) - { - if ( varValue != null) - { + public void addBodyVar(String varName, GXXMLSerializable varValue) { + if ( varValue != null) { bodyVars.put(varName, varValue.toJSonString(false)); } } - public void addBodyVar(String varName, GxSilentTrnSdt varValue) - { - if ( varValue != null) - { - bodyVars.put(varName, varValue.toJSonString(false)); - } - } - - public void addBodyVar(String varName, GxUserType varValue) - { - if ( varValue != null) - { - bodyVars.put(varName, varValue.toJSonString(false)); - } - } - - public void addBodyVar(String varName, String varValue) - { + public void addBodyVar(String varName, String varValue) { bodyVars.put( varName, quoteString(varValue)); } - public void addBodyVar(String varName, double varValue) - { + public void addBodyVar(String varName, double varValue) { bodyVars.put(varName, quoteString(Double.toString(varValue))); } - public void addBodyVar(String varName, Date varValue) - { - SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); + public void addBodyVar(String varName, Date varValue) { + SimpleDateFormat df = new SimpleDateFormat(DATE_FMT); bodyVars.put(varName, quoteString(df.format(varValue))); } - public void addBodyVar(String varName, Date varValue, boolean hasMilliseconds) - { - String fmt = "yyyy-MM-dd'T'HH:mm:ss"; + public void addBodyVar(String varName, Date varValue, boolean hasMilliseconds) { + String fmt = DATETIME_FMT; if (hasMilliseconds) - fmt = "yyyy-MM-dd'T'HH:mm:ss.SSS"; + fmt = DATETIME_FMT_MS; SimpleDateFormat df = new SimpleDateFormat(fmt); bodyVars.put( varName, quoteString(df.format(varValue))); } - public void addBodyVar(String varName, int varValue) - { + public void addBodyVar(String varName, int varValue) { bodyVars.put( varName, Integer.toString(varValue)); } - public void addBodyVar(String varName, Boolean varValue) - { + public void addBodyVar(String varName, Boolean varValue) { bodyVars.put( varName, varValue.toString()); } - public void addBodyVar(String varName, java.math.BigDecimal varValue) - { + public void addBodyVar(String varName, java.math.BigDecimal varValue) { bodyVars.put( varName, varValue.toString()); } - public void addBodyVar(String varName, java.util.UUID varValue) - { + public void addBodyVar(String varName, java.util.UUID varValue) { bodyVars.put( varName, quoteString(varValue.toString())); } - public void addBodyVar(String varName, IGxGeoJSONSerializable varValue) - { + public void addBodyVar(String varName, IGxJSONSerializable varValue) { bodyVars.put( varName, quoteString(varValue.toJSonString())); } - public String getBodyString(String varName) - { + public String getBodyString(String varName) { return getJsonStr(varName); } - public Date getBodyDate(String varName) - { - try{ - return new SimpleDateFormat("yyyy-MM-dd").parse(getJsonStr(varName)); + public Date getBodyDate(String varName) { + try { + return new SimpleDateFormat(DATE_FMT).parse(getJsonStr(varName)); } - catch (ParseException e) - { + catch (ParseException e) { return CommonUtil.newNullDate(); } } - public Date getBodyDateTime(String varName, Boolean hasMilliseconds) - { + public Date getBodyDateTime(String varName, Boolean hasMilliseconds) { try{ - String fmt = "yyyy-MM-dd'T'HH:mm:ss"; + String fmt = DATETIME_FMT; if (hasMilliseconds) - fmt = "yyyy-MM-dd'T'HH:mm:ss.SSS"; + fmt = DATETIME_FMT_MS; return new SimpleDateFormat(fmt).parse(getJsonStr(varName)); } - catch (ParseException e) - { + catch (ParseException e) { return CommonUtil.newNullDate(); } } - public boolean getBodyBool(String varName) - { + public boolean getBodyBool(String varName) { return Boolean.parseBoolean(getJsonStr(varName)); } - public java.util.UUID getBodyGuid(String varName) - { + public java.util.UUID getBodyGuid(String varName) { return java.util.UUID.fromString(getJsonStr(varName)); } - public < T extends IGxGeoJSONSerializable> T getBodyGeospatial(String varName, Class sdtClass) - { + public < T extends IGxJSONSerializable> T getBodyGeospatial(String varName, Class sdtClass) { T sdt; try { sdt = sdtClass.newInstance(); sdt.fromJSonString(getJsonStr(varName)); return sdt; } - catch (IllegalAccessException e) { + catch (Exception e) { + errorCode = DESERIALIZING_ERROR_CODE; + errorMessage = DESERIALIZING_ERROR_MSG; + logger.error(DESERIALIZING_ERROR_MSG + " " + sdtClass, e); return null; - } - catch (InstantiationException e) { - return null; - } + } } - public Double getBodyNum(String varName) - { + public Double getBodyNum(String varName) { return Double.parseDouble(getJsonStr(varName)); } - public int getBodyInt(String varName) - { + public int getBodyInt(String varName) { return Integer.parseInt(getJsonStr(varName)); } - public short getBodyShort(String varName) - { + public short getBodyShort(String varName) { return Short.parseShort(getJsonStr(varName)); } - public String getJsonStr(String varName) - { + public String getJsonStr(String varName) { String jsonstr = ""; - try{ + try { if (jsonResponse != null) { if (jsonResponse.has(varName)) jsonstr = jsonResponse.getString(varName); @@ -353,101 +303,28 @@ else if (jsonResponse.length() == 1 && jsonResponse.has("")) jsonstr = jsonResponse.getString(""); } else { - errorCode = 1; - errorMessage = "Invalid response"; + errorCode = RESPONSE_ERROR_CODE; + errorMessage = RESPONSE_ERROR_MSG; + logger.error(RESPONSE_ERROR_MSG ); } } - catch( JSONException e) - { - errorCode = 1; - errorMessage = "Invalid response"; + catch( JSONException e) { + errorCode = PARSING_ERROR_CODE; + errorMessage = PARSING_ERROR_MSG; + logger.error(PARSING_ERROR_MSG, e); } return jsonstr; } - public T getBodySdtTrn(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), null); - } - else if (jsonResponse.length() == 1 && jsonResponse.has("")) { - sdt.fromJSonString(jsonResponse.getString(""), null); - } - else if (jsonResponse.length()>= 1 && !jsonResponse.has(varName)) - { - sdt.fromJSonString(httpClient.getString(), null); - } - } - else { - errorCode = 1; - errorMessage = "Invalid response"; - return null; - } - } - catch (json.org.json.JSONException e) { - errorCode = 1; - errorMessage = "Invalid response"; - return null; - } - return sdt; - } - - public T getBodySdt(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), null); - } - else if (jsonResponse.length() == 1 && jsonResponse.has("")) { - sdt.fromJSonString(jsonResponse.getString(""), null); - } - else if (jsonResponse.length()>= 1 && !jsonResponse.has(varName)) - { - sdt.fromJSonString(httpClient.getString(), null); - } - } - else { - errorCode = 1; - errorMessage = "Invalid response"; - return null; - } - } - catch (json.org.json.JSONException e) { - errorCode = 1; - errorMessage = "Invalid response"; - return null; - } - return sdt; - } - - - public T getBodyObj(String varName, Class sdtClass) - { + public T getBodyObj(String varName, Class sdtClass) { T sdt; try { sdt = sdtClass.newInstance(); - } catch (InstantiationException e) { + } + catch (InstantiationException e) { return null; - } catch (IllegalAccessException e) { + } + catch (IllegalAccessException e) { return null; } try { @@ -458,27 +335,27 @@ public T getBodyObj(String varName, Class sdtCl else if (jsonResponse.length() == 1 && jsonResponse.has("")) { sdt.fromJSonString(jsonResponse.getString(""), null); } - else if (jsonResponse.length()>= 1 && !jsonResponse.has(varName)) - { + else if (jsonResponse.length()>= 1 && !jsonResponse.has(varName)) { sdt.fromJSonString(httpClient.getString(), null); } } else { - errorCode = 1; - errorMessage = "Invalid response"; + errorCode = RESPONSE_ERROR_CODE; + errorMessage = RESPONSE_ERROR_MSG; + logger.error(RESPONSE_ERROR_MSG + " " + sdtClass); return null; } } catch (json.org.json.JSONException e) { - errorCode = 1; - errorMessage = "Invalid response"; + errorCode = PARSING_ERROR_CODE; + errorMessage = PARSING_ERROR_MSG; + logger.error(PARSING_ERROR_MSG + " " + sdtClass, e); return null; } return sdt; } - public GXBaseCollection getBodySdtTrnCollection(String varName, Class elementClasss) - { + public GXBaseCollection getBodyObjCollection(String varName, Class elementClasss) { JSONArray jsonarr = new JSONArray(); GXBaseCollection col = new GXBaseCollection(); try { @@ -496,95 +373,25 @@ else if (jsonResponse.length() == 1 && jsonResponse.has("")) } } else { - errorCode = 1; - errorMessage = "Invalid response"; + errorCode = RESPONSE_ERROR_CODE; + errorMessage = RESPONSE_ERROR_MSG; + logger.error(RESPONSE_ERROR_MSG + " " + elementClasss); } } - catch (json.org.json.JSONException e) - { - errorCode = 1; - errorMessage = "Invalid response" + e.toString(); - } - catch (Exception e) { - errorCode = 1; - errorMessage = "Invalid response" + e.toString(); - } - return col; - } - - public GXBaseCollection getBodySdtCollection(String varName, Class elementClasss) - { - JSONArray jsonarr = new JSONArray(); - GXBaseCollection col = new GXBaseCollection(); - 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.newInstance(); - sdt.fromJSonString(o.toString(),null); - col.add(sdt); - } - } - else { - errorCode = 1; - errorMessage = "Invalid response"; - } - } - catch (json.org.json.JSONException e) - { - errorCode = 1; - errorMessage = "Invalid response" + e.toString(); - } - catch (Exception e) { - errorCode = 1; - errorMessage = "Invalid response" + e.toString(); - } - return col; - } - - public GXBaseCollection getBodyObjCollection(String varName, Class elementClasss) - { - JSONArray jsonarr = new JSONArray(); - GXBaseCollection col = new GXBaseCollection(); - 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.newInstance(); - sdt.fromJSonString(o.toString(),null); - col.add(sdt); - } - } - else { - errorCode = 1; - errorMessage = "Invalid response"; - } - } - catch (json.org.json.JSONException e) - { - errorCode = 1; - errorMessage = "Invalid response" + e.toString(); + catch (json.org.json.JSONException e) { + errorCode = PARSING_ERROR_CODE; + errorMessage = PARSING_ERROR_MSG; + logger.error(PARSING_ERROR_MSG + " " + elementClasss ,e ); } catch (Exception e) { - errorCode = 1; - errorMessage = "Invalid response" + e.toString(); + errorCode = DESERIALIZING_ERROR_CODE; + errorMessage = DESERIALIZING_ERROR_MSG; + logger.error(DESERIALIZING_ERROR_MSG + " " + elementClasss, e); } return col; } - - public GXSimpleCollection getBodyCollection(String varName, Class elementClasss) - { + public GXSimpleCollection getBodyCollection(String varName, Class elementClasss) { GXSimpleCollection coll; try { coll = new GXSimpleCollection(); @@ -600,53 +407,45 @@ else if (jsonResponse.length() == 1 && jsonResponse.has("")) { coll.fromJSonString(jsonResponse.getString(varName), null); } } - catch (json.org.json.JSONException e) - { - System.err.println( e.toString()); - return null; + catch (json.org.json.JSONException e) { + errorCode = PARSING_ERROR_CODE; + errorMessage = PARSING_ERROR_MSG; + logger.error(PARSING_ERROR_MSG + " " + elementClasss, e); } return coll; } - public void addUploadFile(String filePath, String fileName) - { + public void addUploadFile(String filePath, String fileName) { httpClient.addFile(filePath, fileName); String mimeType = SpecificImplementation.Application.getContentType(filePath); contentType = mimeType; } - public void RestExecute() - { + public void RestExecute() { String separator = ""; queryString = ""; - if (queryVars.size() > 0) - { + if (queryVars.size() > 0) { separator = "?"; - for( Map.Entry entry : queryVars.entrySet()) - { + for( Map.Entry entry : queryVars.entrySet()) { queryString += String.format("%s%s=%s", separator, entry.getKey(), entry.getValue()); separator = "&"; } } bodyString = ""; - if (bodyVars.size() > 0) - { + if (bodyVars.size() > 0) { separator = ""; for( Map.Entry entry : bodyVars.entrySet()) { bodyString += separator + "\"" + entry.getKey() + "\":" + entry.getValue() + ""; separator = ","; } } - if (bodyString.length() > 0) - { + if (bodyString.length() > 0) { bodyString = "{" + bodyString + "}"; httpClient.addString( bodyString); httpClient.addHeader( "Content-Type", contentType); } - else - { - if (this.httpMethod == "POST" || this.httpMethod == "PUT") - { + else { + if (this.httpMethod == "POST" || this.httpMethod == "PUT") { bodyString = "{}"; httpClient.addString(bodyString); httpClient.addHeader("Content-Type", contentType); @@ -655,29 +454,25 @@ public void RestExecute() String serviceuri = ((this.location.getSecure() > 0) ? "https" : "http") + "://" + this.location.getHost(); serviceuri += (this.location.getPort() != 80) ? ":" + Integer.toString(this.location.getPort()): ""; serviceuri += "/" + this.location.getBaseURL() + "/" + this.location.getResourceName(); - serviceuri += queryString; - + 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(); } - else - { + else { statusCode = httpClient.getStatusCode(); - try{ + try { jsonResponse = new JSONObject(httpClient.getString()); } - catch(JSONException e) - { - System.out.println( e.toString()); + catch( JSONException e) { + errorCode = PARSING_ERROR_CODE; + errorMessage = PARSING_ERROR_MSG; + logger.error(PARSING_ERROR_MSG, e); jsonResponse = new JSONObject(); } } - } } From ad40ae8fb47c8ee9696777ca470b3a935710c9a8 Mon Sep 17 00:00:00 2001 From: AlejandroP Date: Fri, 16 Sep 2022 16:54:58 -0300 Subject: [PATCH 15/15] - Fix code imports. --- .../com/genexus/internet/GXRestAPIClient.java | 25 +++++-------------- 1 file changed, 6 insertions(+), 19 deletions(-) diff --git a/java/src/main/java/com/genexus/internet/GXRestAPIClient.java b/java/src/main/java/com/genexus/internet/GXRestAPIClient.java index 7df1a1bdd..d1183ca00 100644 --- a/java/src/main/java/com/genexus/internet/GXRestAPIClient.java +++ b/java/src/main/java/com/genexus/internet/GXRestAPIClient.java @@ -1,18 +1,9 @@ package com.genexus.internet; +import json.org.json.*; import java.util.*; -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 java.text.*; import com.genexus.xml.GXXMLSerializable; -import com.genexus.internet.IGxJSONSerializable; import com.genexus.CommonUtil; import com.genexus.common.interfaces.SpecificImplementation; import com.genexus.*; @@ -23,7 +14,6 @@ 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"; @@ -31,7 +21,7 @@ public class GXRestAPIClient { private int statusCode; private int errorCode; private String errorMessage; - private Integer responseCode; + private int responseCode; private String responseMessage; private String contentType = "application/json; charset=utf-8"; @@ -46,13 +36,10 @@ 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"; @@ -346,7 +333,7 @@ else if (jsonResponse.length()>= 1 && !jsonResponse.has(varName)) { return null; } } - catch (json.org.json.JSONException e) { + catch (JSONException e) { errorCode = PARSING_ERROR_CODE; errorMessage = PARSING_ERROR_MSG; logger.error(PARSING_ERROR_MSG + " " + sdtClass, e); @@ -378,7 +365,7 @@ else if (jsonResponse.length() == 1 && jsonResponse.has("")) logger.error(RESPONSE_ERROR_MSG + " " + elementClasss); } } - catch (json.org.json.JSONException e) { + catch (JSONException e) { errorCode = PARSING_ERROR_CODE; errorMessage = PARSING_ERROR_MSG; logger.error(PARSING_ERROR_MSG + " " + elementClasss ,e ); @@ -407,7 +394,7 @@ else if (jsonResponse.length() == 1 && jsonResponse.has("")) { coll.fromJSonString(jsonResponse.getString(varName), null); } } - catch (json.org.json.JSONException e) { + catch (JSONException e) { errorCode = PARSING_ERROR_CODE; errorMessage = PARSING_ERROR_MSG; logger.error(PARSING_ERROR_MSG + " " + elementClasss, e);