From 473cc9c0ae8474b38872a35d8387a907e55877d9 Mon Sep 17 00:00:00 2001 From: Tim Chamberlain Date: Fri, 28 Mar 2025 16:12:45 -0500 Subject: [PATCH 1/2] turned down some logging, moved getQHttpResponse into its own method in base api action utils, added override constructer to response to read bytes --- .../src/main/resources/log4j2.xml | 3 +- .../module/api/actions/BaseAPIActionUtil.java | 16 +++- .../module/api/actions/QHttpResponse.java | 76 +++++++++++++++++-- .../module/api/model/AuthorizationType.java | 1 + 4 files changed, 89 insertions(+), 7 deletions(-) diff --git a/qqq-backend-core/src/main/resources/log4j2.xml b/qqq-backend-core/src/main/resources/log4j2.xml index 60ffc1f6..83f6782c 100644 --- a/qqq-backend-core/src/main/resources/log4j2.xml +++ b/qqq-backend-core/src/main/resources/log4j2.xml @@ -25,7 +25,8 @@ - + + diff --git a/qqq-backend-module-api/src/main/java/com/kingsrook/qqq/backend/module/api/actions/BaseAPIActionUtil.java b/qqq-backend-module-api/src/main/java/com/kingsrook/qqq/backend/module/api/actions/BaseAPIActionUtil.java index 300a60b7..bdb45aea 100644 --- a/qqq-backend-module-api/src/main/java/com/kingsrook/qqq/backend/module/api/actions/BaseAPIActionUtil.java +++ b/qqq-backend-module-api/src/main/java/com/kingsrook/qqq/backend/module/api/actions/BaseAPIActionUtil.java @@ -743,6 +743,10 @@ public class BaseAPIActionUtil case OAUTH2 -> request.setHeader("Authorization", "Bearer " + getOAuth2Token()); case API_KEY_QUERY_PARAM -> addApiKeyQueryParamToRequest(request); case CUSTOM -> handleCustomAuthorization(request); + case NONE -> + { + /* nothing to do here */ + } default -> throw new IllegalArgumentException("Unexpected authorization type: " + backendMetaData.getAuthorizationType()); } } @@ -1174,6 +1178,16 @@ public class BaseAPIActionUtil + /******************************************************************************* + ** + *******************************************************************************/ + protected QHttpResponse getQHttpResponse(HttpResponse response) throws Exception + { + return (new QHttpResponse(response)); + } + + + /******************************************************************************* ** *******************************************************************************/ @@ -1207,7 +1221,7 @@ public class BaseAPIActionUtil try(CloseableHttpResponse response = executeHttpRequest(request, httpClient)) { - QHttpResponse qResponse = new QHttpResponse(response); + QHttpResponse qResponse = getQHttpResponse(response); logOutboundApiCall(request, qResponse); diff --git a/qqq-backend-module-api/src/main/java/com/kingsrook/qqq/backend/module/api/actions/QHttpResponse.java b/qqq-backend-module-api/src/main/java/com/kingsrook/qqq/backend/module/api/actions/QHttpResponse.java index bb32cfd7..0b100e1e 100644 --- a/qqq-backend-module-api/src/main/java/com/kingsrook/qqq/backend/module/api/actions/QHttpResponse.java +++ b/qqq-backend-module-api/src/main/java/com/kingsrook/qqq/backend/module/api/actions/QHttpResponse.java @@ -40,6 +40,7 @@ public class QHttpResponse private String statusReasonPhrase; private List
headerList; private String content; + private byte[] contentBytes; @@ -53,11 +54,50 @@ public class QHttpResponse + /******************************************************************************* + ** Constructor for QHttpResponse that allows reading content as bytes + ** + *******************************************************************************/ + public QHttpResponse(HttpResponse httpResponse, boolean readContentAsBytes) throws Exception + { + setGeneralHttpResponseData(httpResponse); + + if(this.statusCode == null || this.statusCode != 204) + { + if(readContentAsBytes) + { + this.contentBytes = httpResponse.getEntity().getContent().readAllBytes(); + } + else + { + this.content = EntityUtils.toString(httpResponse.getEntity()); + } + } + } + + + /******************************************************************************* ** Constructor for qHttpResponse ** *******************************************************************************/ public QHttpResponse(HttpResponse httpResponse) throws Exception + { + setGeneralHttpResponseData(httpResponse); + + if(this.statusCode == null || this.statusCode != 204) + { + this.content = EntityUtils.toString(httpResponse.getEntity()); + } + } + + + + /******************************************************************************* + ** Sets data into this entity from an HttpResponse but doesnt read response data + ** + *******************************************************************************/ + private void setGeneralHttpResponseData(HttpResponse httpResponse) throws Exception { this.headerList = Arrays.asList(httpResponse.getAllHeaders()); if(httpResponse.getStatusLine() != null) @@ -69,11 +109,6 @@ public class QHttpResponse this.statusProtocolVersion = httpResponse.getStatusLine().getProtocolVersion().toString(); } } - - if(this.statusCode == null || this.statusCode != 204) - { - this.content = EntityUtils.toString(httpResponse.getEntity()); - } } @@ -242,4 +277,35 @@ public class QHttpResponse return (this); } + + + /******************************************************************************* + ** Getter for contentBytes + *******************************************************************************/ + public byte[] getContentBytes() + { + return (this.contentBytes); + } + + + + /******************************************************************************* + ** Setter for contentBytes + *******************************************************************************/ + public void setContentBytes(byte[] contentBytes) + { + this.contentBytes = contentBytes; + } + + + + /******************************************************************************* + ** Fluent setter for contentBytes + *******************************************************************************/ + public QHttpResponse withContentBytes(byte[] contentBytes) + { + this.contentBytes = contentBytes; + return (this); + } + } diff --git a/qqq-backend-module-api/src/main/java/com/kingsrook/qqq/backend/module/api/model/AuthorizationType.java b/qqq-backend-module-api/src/main/java/com/kingsrook/qqq/backend/module/api/model/AuthorizationType.java index 9a4f750c..614df755 100644 --- a/qqq-backend-module-api/src/main/java/com/kingsrook/qqq/backend/module/api/model/AuthorizationType.java +++ b/qqq-backend-module-api/src/main/java/com/kingsrook/qqq/backend/module/api/model/AuthorizationType.java @@ -33,5 +33,6 @@ public enum AuthorizationType BASIC_AUTH_USERNAME_PASSWORD, CUSTOM, OAUTH2, + NONE, API_KEY_QUERY_PARAM, } From 9cf25ed45c26cac317143f85a584bb0411087d8d Mon Sep 17 00:00:00 2001 From: Tim Chamberlain Date: Fri, 28 Mar 2025 16:47:06 -0500 Subject: [PATCH 2/2] codereview feedback --- qqq-backend-core/src/main/resources/log4j2.xml | 2 +- .../module/api/actions/QHttpResponse.java | 17 +++++++---------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/qqq-backend-core/src/main/resources/log4j2.xml b/qqq-backend-core/src/main/resources/log4j2.xml index 83f6782c..df03564e 100644 --- a/qqq-backend-core/src/main/resources/log4j2.xml +++ b/qqq-backend-core/src/main/resources/log4j2.xml @@ -26,7 +26,7 @@ - + diff --git a/qqq-backend-module-api/src/main/java/com/kingsrook/qqq/backend/module/api/actions/QHttpResponse.java b/qqq-backend-module-api/src/main/java/com/kingsrook/qqq/backend/module/api/actions/QHttpResponse.java index 0b100e1e..72069b3e 100644 --- a/qqq-backend-module-api/src/main/java/com/kingsrook/qqq/backend/module/api/actions/QHttpResponse.java +++ b/qqq-backend-module-api/src/main/java/com/kingsrook/qqq/backend/module/api/actions/QHttpResponse.java @@ -60,18 +60,16 @@ public class QHttpResponse *******************************************************************************/ public QHttpResponse(HttpResponse httpResponse, boolean readContentAsBytes) throws Exception { - setGeneralHttpResponseData(httpResponse); + if(!readContentAsBytes) + { + new QHttpResponse(httpResponse); + return; + } + setGeneralHttpResponseData(httpResponse); if(this.statusCode == null || this.statusCode != 204) { - if(readContentAsBytes) - { - this.contentBytes = httpResponse.getEntity().getContent().readAllBytes(); - } - else - { - this.content = EntityUtils.toString(httpResponse.getEntity()); - } + this.contentBytes = httpResponse.getEntity().getContent().readAllBytes(); } } @@ -84,7 +82,6 @@ public class QHttpResponse public QHttpResponse(HttpResponse httpResponse) throws Exception { setGeneralHttpResponseData(httpResponse); - if(this.statusCode == null || this.statusCode != 204) { this.content = EntityUtils.toString(httpResponse.getEntity());