Skip to content

Commit d1bdb18

Browse files
authored
- Add Access token property and processing in Location object (#774)
* - Add Access token property and processing in Location object * - extract Log code from routines in GXRestAPIClient - Fix test case when receiving unwrapped response. * - Removed "this" prefix as per review suggested by Iroqueta
1 parent db11024 commit d1bdb18

2 files changed

Lines changed: 41 additions & 19 deletions

File tree

common/src/main/java/com/genexus/internet/Location.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class Location
2323
private String proxyAuthenticationUser = "";
2424
private String proxyAuthenticationRealm = "";
2525
private String proxyAuthenticationPassword = "";
26+
private String accessToken = "";
2627
private IGXWSAddressing wsAddressing;
2728
private IGXWSSecurity wsSecurity;
2829
private String certificate = "";
@@ -120,7 +121,10 @@ public String getProxyAuthenticationPassword()
120121
{
121122
return proxyAuthenticationPassword;
122123
}
123-
124+
public String getAccessToken()
125+
{
126+
return accessToken;
127+
}
124128
public IGXWSAddressing getWSAddressing()
125129
{
126130
return wsAddressing;
@@ -220,7 +224,9 @@ public void setProxyAuthenticationPassword(String authenticationPassword)
220224
{
221225
this.proxyAuthenticationPassword = authenticationPassword;
222226
}
223-
227+
228+
public void setAccessToken(String token) { this.accessToken = token; }
229+
224230
public void setWSAddressing(IGXWSAddressing wsAddressing)
225231
{
226232
this.wsAddressing = wsAddressing;

java/src/main/java/com/genexus/internet/GXRestAPIClient.java

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ public < T extends IGxJSONSerializable> T getBodyGeospatial(String varName, Clas
297297
catch (Exception e) {
298298
errorCode = DESERIALIZING_ERROR_CODE;
299299
errorMessage = DESERIALIZING_ERROR_MSG;
300-
logger.error(DESERIALIZING_ERROR_MSG + " " + sdtClass, e);
300+
logError(DESERIALIZING_ERROR_CODE, DESERIALIZING_ERROR_MSG + " " + sdtClass, e);
301301
return null;
302302
}
303303
}
@@ -342,13 +342,13 @@ else if (jsonResponse.length() == 1 && jsonResponse.has(""))
342342
else {
343343
errorCode = RESPONSE_ERROR_CODE;
344344
errorMessage = RESPONSE_ERROR_MSG;
345-
logger.error(RESPONSE_ERROR_MSG );
345+
logError(RESPONSE_ERROR_CODE, RESPONSE_ERROR_MSG);
346346
}
347347
}
348348
catch( JSONException e) {
349349
errorCode = PARSING_ERROR_CODE;
350350
errorMessage = PARSING_ERROR_MSG;
351-
logger.error(PARSING_ERROR_MSG, e);
351+
logError(PARSING_ERROR_CODE, PARSING_ERROR_MSG, e);
352352
}
353353
return jsonstr;
354354
}
@@ -381,21 +381,21 @@ else if (jsonResponse.length()>= 1) {
381381
{
382382
errorCode = RESPONSE_ERROR_CODE;
383383
errorMessage = RESPONSE_ERROR_MSG;
384-
logger.error(RESPONSE_ERROR_MSG + " " + sdtClass);
384+
logError( RESPONSE_ERROR_CODE, RESPONSE_ERROR_MSG + " " + sdtClass);
385385
return null;
386386
}
387387
}
388388
else {
389389
errorCode = RESPONSE_ERROR_CODE;
390390
errorMessage = RESPONSE_ERROR_MSG;
391-
logger.error(RESPONSE_ERROR_MSG + " " + sdtClass);
391+
logError( RESPONSE_ERROR_CODE,RESPONSE_ERROR_MSG + " " + sdtClass);
392392
return null;
393393
}
394394
}
395395
catch (json.org.json.JSONException e) {
396396
errorCode = PARSING_ERROR_CODE;
397397
errorMessage = PARSING_ERROR_MSG;
398-
logger.error(PARSING_ERROR_MSG + " " + sdtClass, e);
398+
logError(PARSING_ERROR_CODE, PARSING_ERROR_MSG + " " + sdtClass, e);
399399
return null;
400400
}
401401
return sdt;
@@ -431,18 +431,18 @@ else if (jsonResponse.length() == 1 && jsonResponse.has(""))
431431
else {
432432
errorCode = RESPONSE_ERROR_CODE;
433433
errorMessage = RESPONSE_ERROR_MSG;
434-
logger.error(RESPONSE_ERROR_MSG + " " + elementClass);
434+
logError(RESPONSE_ERROR_CODE,RESPONSE_ERROR_MSG + " " + elementClass);
435435
}
436436
}
437437
catch (json.org.json.JSONException e) {
438438
errorCode = PARSING_ERROR_CODE;
439439
errorMessage = PARSING_ERROR_MSG;
440-
logger.error(PARSING_ERROR_MSG + " " + elementClass ,e );
440+
logError(PARSING_ERROR_CODE,PARSING_ERROR_MSG + " " + elementClass ,e );
441441
}
442442
catch (Exception e) {
443443
errorCode = DESERIALIZING_ERROR_CODE;
444444
errorMessage = DESERIALIZING_ERROR_MSG;
445-
logger.error(DESERIALIZING_ERROR_MSG + " " + elementClass, e);
445+
logError(DESERIALIZING_ERROR_CODE, DESERIALIZING_ERROR_MSG + " " + elementClass, e);
446446
}
447447
}
448448

@@ -465,7 +465,7 @@ else if (jsonResponse.length() == 1 && jsonResponse.has("")) {
465465
catch (json.org.json.JSONException e) {
466466
errorCode = PARSING_ERROR_CODE;
467467
errorMessage = PARSING_ERROR_MSG;
468-
logger.error(PARSING_ERROR_MSG + " " + elementClasss, e);
468+
logError(PARSING_ERROR_CODE,PARSING_ERROR_MSG + " " + elementClasss, e);
469469
}
470470
return coll;
471471
}
@@ -500,17 +500,20 @@ public void RestExecute() {
500500
httpClient.addHeader( "Content-Type", contentType);
501501
}
502502
else {
503-
if (this.httpMethod == "POST" || this.httpMethod == "PUT") {
503+
if (httpMethod == "POST" || httpMethod == "PUT") {
504504
bodyString = "{}";
505505
httpClient.addString(bodyString);
506506
httpClient.addHeader("Content-Type", contentType);
507507
}
508508
}
509-
String serviceuri = ((this.location.getSecure() > 0) ? "https" : "http") + "://" + this.location.getHost();
510-
serviceuri += (this.location.getPort() != 80) ? ":" + Integer.toString(this.location.getPort()): "";
511-
serviceuri += "/" + this.location.getBaseURL() + "/" + this.location.getResourceName();
509+
if (location.getAuthenticationMethod() == 4 && location.getAccessToken() != null && ! location.getAccessToken().trim().isEmpty()) {
510+
httpClient.addHeader("Authorization", location.getAccessToken());
511+
}
512+
String serviceuri = ((location.getSecure() > 0) ? "https" : "http") + "://" + location.getHost();
513+
serviceuri += (location.getPort() != 80) ? ":" + Integer.toString(location.getPort()): "";
514+
serviceuri += "/" + location.getBaseURL() + "/" + location.getResourceName();
512515
serviceuri += queryString;
513-
httpClient.execute( this.httpMethod, serviceuri);
516+
httpClient.execute( httpMethod, serviceuri);
514517

515518
if (httpClient.getStatusCode() >= 300 || httpClient.getErrCode() > 0) {
516519
errorCode = (httpClient.getErrCode() == 0)? 1 : httpClient.getErrCode();
@@ -520,14 +523,27 @@ public void RestExecute() {
520523
else {
521524
statusCode = httpClient.getStatusCode();
522525
try {
523-
jsonResponse = new JSONObject(httpClient.getString());
526+
String response = httpClient.getString();
527+
if (response.trim().startsWith("["))
528+
{
529+
// unwrapped list response
530+
response = "{\"\":" + response + "}";
531+
}
532+
jsonResponse = new JSONObject(response);
524533
}
525534
catch( JSONException e) {
526535
errorCode = PARSING_ERROR_CODE;
527536
errorMessage = PARSING_ERROR_MSG;
528-
logger.error(PARSING_ERROR_MSG, e);
537+
logError(PARSING_ERROR_CODE, PARSING_ERROR_MSG, e);
529538
jsonResponse = new JSONObject();
530539
}
531540
}
532541
}
542+
543+
private void logError(int code, String msg) {
544+
logger.error("Error: " + Integer.toString(code) + " " + msg);
545+
}
546+
private void logError(int code, String msg, Exception e) {
547+
logger.error("Error: " + Integer.toString(code) + " " + msg, e);
548+
}
533549
}

0 commit comments

Comments
 (0)