diff --git a/android/src/main/java/com/genexus/GXGeospatial.java b/android/src/main/java/com/genexus/GXGeospatial.java index a3cd83415..02a9cab58 100644 --- a/android/src/main/java/com/genexus/GXGeospatial.java +++ b/android/src/main/java/com/genexus/GXGeospatial.java @@ -18,10 +18,8 @@ //import org.noggit.JSONParser.*; import org.simpleframework.xml.Root; import org.simpleframework.xml.Text; - import com.genexus.internet.IGxJSONSerializable; - @Root public final class GXGeospatial implements java.io.Serializable, IGxJSONSerializable{ diff --git a/common/src/main/java/com/genexus/properties/GXObjectProperties.java b/common/src/main/java/com/genexus/properties/GXObjectProperties.java new file mode 100644 index 000000000..db9391d74 --- /dev/null +++ b/common/src/main/java/com/genexus/properties/GXObjectProperties.java @@ -0,0 +1,47 @@ +package com.genexus.properties; + +import com.genexus.internet.Location; + +public class GXObjectProperties +{ + private Location location = null; + private String errorMessage = ""; + private int errorCode = 0; + private int statusCode = 0; + + public Location getLocation() + { + return location; + } + 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 diff --git a/common/src/main/java/com/genexus/properties/GXObjectsConfiguration.java b/common/src/main/java/com/genexus/properties/GXObjectsConfiguration.java new file mode 100644 index 000000000..d4edf1f90 --- /dev/null +++ b/common/src/main/java/com/genexus/properties/GXObjectsConfiguration.java @@ -0,0 +1,15 @@ +package com.genexus.properties; + +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); + } +} diff --git a/gxgeospatial/src/main/java/com/genexus/GXGeospatial.java b/gxgeospatial/src/main/java/com/genexus/GXGeospatial.java index 63142d891..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 org.locationtech.spatial4j.context.SpatialContext; import org.locationtech.spatial4j.context.SpatialContextFactory; import org.locationtech.spatial4j.context.jts.JtsSpatialContextFactory; diff --git a/java/sample.db b/java/sample.db index 98160152f..ac3e81ee8 100644 Binary files a/java/sample.db and b/java/sample.db differ 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..d1183ca00 --- /dev/null +++ b/java/src/main/java/com/genexus/internet/GXRestAPIClient.java @@ -0,0 +1,465 @@ +package com.genexus.internet; + +import json.org.json.*; +import java.util.*; +import java.text.*; +import com.genexus.xml.GXXMLSerializable; +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 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 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 responseData = new HashMap(); + + 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(); + } + + /* Gets */ + + public String getName() { + return name; + } + + public Location getLocation() { + return location; + } + + public String getProtocol() { + return protocol; + } + + public String getHttpMethod() { + return httpMethod; + } + + public int getErrorCode() { + return errorCode; + } + + public int getStatusCode() { + return statusCode; + } + + 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 setStatusCode( int value) { + statusCode = value; + } + + public void setErrorCode( int value) { + errorCode = value; + } + + public void setErrorMessage( String value) { + errorMessage = value; + } + + + public void addQueryVar(String varName, String varValue) { + queryVars.put(varName, GXutil.URLEncode(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(DATE_FMT); + queryVars.put(varName, df.format(varValue)); + } + + public void addQueryVar(String varName, Date varValue, boolean hasMilliseconds) { + String fmt = DATETIME_FMT; + if (hasMilliseconds) + fmt = DATETIME_FMT_MS; + 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()); + } + + public void addQueryVar(String varName, java.math.BigDecimal varValue) { + queryVars.put(varName, varValue.toString()); + } + + public void addQueryVar(String varName, GXXMLSerializable 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) { + return "\"" + value + "\""; + } + + public void addBodyVar(String varName, GXBaseCollection varValue) { + if ( varValue != null) { + bodyVars.put(varName, varValue.toJSonString(false)); + } + } + + public void addBodyVar(String varName, GXXMLSerializable 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(DATE_FMT); + bodyVars.put(varName, quoteString(df.format(varValue))); + } + + public void addBodyVar(String varName, Date varValue, boolean hasMilliseconds) { + String fmt = DATETIME_FMT; + if (hasMilliseconds) + fmt = DATETIME_FMT_MS; + 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.math.BigDecimal varValue) { + bodyVars.put( varName, varValue.toString()); + } + + public void addBodyVar(String varName, java.util.UUID varValue) { + bodyVars.put( varName, quoteString(varValue.toString())); + } + + public void addBodyVar(String varName, IGxJSONSerializable varValue) { + bodyVars.put( varName, quoteString(varValue.toJSonString())); + } + + public String getBodyString(String varName) { + return getJsonStr(varName); + } + + public Date getBodyDate(String varName) { + try { + return new SimpleDateFormat(DATE_FMT).parse(getJsonStr(varName)); + } + catch (ParseException e) { + return CommonUtil.newNullDate(); + } + } + + public Date getBodyDateTime(String varName, Boolean hasMilliseconds) { + try{ + String fmt = DATETIME_FMT; + if (hasMilliseconds) + fmt = DATETIME_FMT_MS; + 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 < T extends IGxJSONSerializable> T getBodyGeospatial(String varName, Class sdtClass) { + T sdt; + try { + sdt = sdtClass.newInstance(); + sdt.fromJSonString(getJsonStr(varName)); + return sdt; + } + catch (Exception e) { + errorCode = DESERIALIZING_ERROR_CODE; + errorMessage = DESERIALIZING_ERROR_MSG; + logger.error(DESERIALIZING_ERROR_MSG + " " + sdtClass, e); + return null; + } + } + + 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 = RESPONSE_ERROR_CODE; + errorMessage = RESPONSE_ERROR_MSG; + logger.error(RESPONSE_ERROR_MSG ); + } + } + catch( JSONException e) { + errorCode = PARSING_ERROR_CODE; + errorMessage = PARSING_ERROR_MSG; + logger.error(PARSING_ERROR_MSG, e); + } + return jsonstr; + } + + 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 = 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 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 = 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 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 (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) { + httpClient.addFile(filePath, fileName); + String mimeType = SpecificImplementation.Application.getContentType(filePath); + contentType = mimeType; + } + + 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); + } + } + 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; + httpClient.execute( this.httpMethod, serviceuri); + + if (httpClient.getStatusCode() >= 300 || httpClient.getErrCode() > 0) { + errorCode = (httpClient.getErrCode() == 0)? 1 : httpClient.getErrCode(); + errorMessage = httpClient.getErrDescription(); + statusCode = httpClient.getStatusCode(); + } + else { + statusCode = httpClient.getStatusCode(); + try { + jsonResponse = new JSONObject(httpClient.getString()); + } + catch( JSONException e) { + errorCode = PARSING_ERROR_CODE; + errorMessage = PARSING_ERROR_MSG; + logger.error(PARSING_ERROR_MSG, e); + jsonResponse = new JSONObject(); + } + } + } +}