diff --git a/common/src/main/java/com/genexus/internet/Location.java b/common/src/main/java/com/genexus/internet/Location.java index 43a2b1715..8daace655 100644 --- a/common/src/main/java/com/genexus/internet/Location.java +++ b/common/src/main/java/com/genexus/internet/Location.java @@ -23,6 +23,7 @@ public class Location private String proxyAuthenticationUser = ""; private String proxyAuthenticationRealm = ""; private String proxyAuthenticationPassword = ""; + private String accessToken = ""; private IGXWSAddressing wsAddressing; private IGXWSSecurity wsSecurity; private String certificate = ""; @@ -120,7 +121,10 @@ public String getProxyAuthenticationPassword() { return proxyAuthenticationPassword; } - + public String getAccessToken() + { + return accessToken; + } public IGXWSAddressing getWSAddressing() { return wsAddressing; @@ -220,7 +224,9 @@ public void setProxyAuthenticationPassword(String authenticationPassword) { this.proxyAuthenticationPassword = authenticationPassword; } - + + public void setAccessToken(String token) { this.accessToken = token; } + public void setWSAddressing(IGXWSAddressing wsAddressing) { this.wsAddressing = wsAddressing; diff --git a/java/src/main/java/com/genexus/internet/GXRestAPIClient.java b/java/src/main/java/com/genexus/internet/GXRestAPIClient.java index 30c1f2a30..ea0f7a748 100644 --- a/java/src/main/java/com/genexus/internet/GXRestAPIClient.java +++ b/java/src/main/java/com/genexus/internet/GXRestAPIClient.java @@ -297,7 +297,7 @@ public < T extends IGxJSONSerializable> T getBodyGeospatial(String varName, Clas catch (Exception e) { errorCode = DESERIALIZING_ERROR_CODE; errorMessage = DESERIALIZING_ERROR_MSG; - logger.error(DESERIALIZING_ERROR_MSG + " " + sdtClass, e); + logError(DESERIALIZING_ERROR_CODE, DESERIALIZING_ERROR_MSG + " " + sdtClass, e); return null; } } @@ -342,13 +342,13 @@ else if (jsonResponse.length() == 1 && jsonResponse.has("")) else { errorCode = RESPONSE_ERROR_CODE; errorMessage = RESPONSE_ERROR_MSG; - logger.error(RESPONSE_ERROR_MSG ); + logError(RESPONSE_ERROR_CODE, RESPONSE_ERROR_MSG); } } catch( JSONException e) { errorCode = PARSING_ERROR_CODE; errorMessage = PARSING_ERROR_MSG; - logger.error(PARSING_ERROR_MSG, e); + logError(PARSING_ERROR_CODE, PARSING_ERROR_MSG, e); } return jsonstr; } @@ -381,21 +381,21 @@ else if (jsonResponse.length()>= 1) { { errorCode = RESPONSE_ERROR_CODE; errorMessage = RESPONSE_ERROR_MSG; - logger.error(RESPONSE_ERROR_MSG + " " + sdtClass); + logError( RESPONSE_ERROR_CODE, RESPONSE_ERROR_MSG + " " + sdtClass); return null; } } else { errorCode = RESPONSE_ERROR_CODE; errorMessage = RESPONSE_ERROR_MSG; - logger.error(RESPONSE_ERROR_MSG + " " + sdtClass); + logError( RESPONSE_ERROR_CODE,RESPONSE_ERROR_MSG + " " + sdtClass); return null; } } catch (json.org.json.JSONException e) { errorCode = PARSING_ERROR_CODE; errorMessage = PARSING_ERROR_MSG; - logger.error(PARSING_ERROR_MSG + " " + sdtClass, e); + logError(PARSING_ERROR_CODE, PARSING_ERROR_MSG + " " + sdtClass, e); return null; } return sdt; @@ -431,18 +431,18 @@ else if (jsonResponse.length() == 1 && jsonResponse.has("")) else { errorCode = RESPONSE_ERROR_CODE; errorMessage = RESPONSE_ERROR_MSG; - logger.error(RESPONSE_ERROR_MSG + " " + elementClass); + logError(RESPONSE_ERROR_CODE,RESPONSE_ERROR_MSG + " " + elementClass); } } catch (json.org.json.JSONException e) { errorCode = PARSING_ERROR_CODE; errorMessage = PARSING_ERROR_MSG; - logger.error(PARSING_ERROR_MSG + " " + elementClass ,e ); + logError(PARSING_ERROR_CODE,PARSING_ERROR_MSG + " " + elementClass ,e ); } catch (Exception e) { errorCode = DESERIALIZING_ERROR_CODE; errorMessage = DESERIALIZING_ERROR_MSG; - logger.error(DESERIALIZING_ERROR_MSG + " " + elementClass, e); + logError(DESERIALIZING_ERROR_CODE, DESERIALIZING_ERROR_MSG + " " + elementClass, e); } } @@ -465,7 +465,7 @@ else if (jsonResponse.length() == 1 && jsonResponse.has("")) { catch (json.org.json.JSONException e) { errorCode = PARSING_ERROR_CODE; errorMessage = PARSING_ERROR_MSG; - logger.error(PARSING_ERROR_MSG + " " + elementClasss, e); + logError(PARSING_ERROR_CODE,PARSING_ERROR_MSG + " " + elementClasss, e); } return coll; } @@ -500,17 +500,20 @@ public void RestExecute() { httpClient.addHeader( "Content-Type", contentType); } else { - if (this.httpMethod == "POST" || this.httpMethod == "PUT") { + if (httpMethod == "POST" || 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(); + if (location.getAuthenticationMethod() == 4 && location.getAccessToken() != null && ! location.getAccessToken().trim().isEmpty()) { + httpClient.addHeader("Authorization", location.getAccessToken()); + } + String serviceuri = ((location.getSecure() > 0) ? "https" : "http") + "://" + location.getHost(); + serviceuri += (location.getPort() != 80) ? ":" + Integer.toString(location.getPort()): ""; + serviceuri += "/" + location.getBaseURL() + "/" + location.getResourceName(); serviceuri += queryString; - httpClient.execute( this.httpMethod, serviceuri); + httpClient.execute( httpMethod, serviceuri); if (httpClient.getStatusCode() >= 300 || httpClient.getErrCode() > 0) { errorCode = (httpClient.getErrCode() == 0)? 1 : httpClient.getErrCode(); @@ -520,14 +523,27 @@ public void RestExecute() { else { statusCode = httpClient.getStatusCode(); try { - jsonResponse = new JSONObject(httpClient.getString()); + String response = httpClient.getString(); + if (response.trim().startsWith("[")) + { + // unwrapped list response + response = "{\"\":" + response + "}"; + } + jsonResponse = new JSONObject(response); } catch( JSONException e) { errorCode = PARSING_ERROR_CODE; errorMessage = PARSING_ERROR_MSG; - logger.error(PARSING_ERROR_MSG, e); + logError(PARSING_ERROR_CODE, PARSING_ERROR_MSG, e); jsonResponse = new JSONObject(); } } } + + private void logError(int code, String msg) { + logger.error("Error: " + Integer.toString(code) + " " + msg); + } + private void logError(int code, String msg, Exception e) { + logger.error("Error: " + Integer.toString(code) + " " + msg, e); + } }