From 1d8c7107f13ad251c66ae03b1f13ce3be0043e75 Mon Sep 17 00:00:00 2001 From: GeorgeC Date: Fri, 8 Mar 2024 09:44:59 -0500 Subject: [PATCH] Refactor handling of request source header This update refactors how the request source header is handled in the ResourceWebClient class. It introduces a helper method to include the request source header, reducing redundant code in multiple methods. It also removes headers as a parameter in the search method call inside PicsureSearchService, passing only the requestSource string instead. --- .../service/PicsureSearchService.java | 7 +- .../avillach/service/ResourceWebClient.java | 68 ++++++------------- 2 files changed, 24 insertions(+), 51 deletions(-) diff --git a/pic-sure-api-war/src/main/java/edu/harvard/dbmi/avillach/service/PicsureSearchService.java b/pic-sure-api-war/src/main/java/edu/harvard/dbmi/avillach/service/PicsureSearchService.java index 71bf2753..9a05aad0 100644 --- a/pic-sure-api-war/src/main/java/edu/harvard/dbmi/avillach/service/PicsureSearchService.java +++ b/pic-sure-api-war/src/main/java/edu/harvard/dbmi/avillach/service/PicsureSearchService.java @@ -54,15 +54,16 @@ public SearchResults search(UUID resourceId, QueryRequest searchQueryRequest, Ht throw new ProtocolException(ProtocolException.MISSING_DATA); } + String requestSource = Utilities.getRequestSourceFromHeader(headers); logger.info( - "path=/search/{resourceId}, resourceId={}, requestSource={}, searchQueryRequest={}", resourceId, - Utilities.getRequestSourceFromHeader(headers), Utilities.convertQueryRequestToString(mapper, searchQueryRequest) + "path=/search/{resourceId}, resourceId={}, requestSource={}, searchQueryRequest={}", resourceId, requestSource, + Utilities.convertQueryRequestToString(mapper, searchQueryRequest) ); if (searchQueryRequest.getResourceCredentials() == null) { searchQueryRequest.setResourceCredentials(new HashMap()); } - return resourceWebClient.search(resource.getResourceRSPath(), searchQueryRequest, headers); + return resourceWebClient.search(resource.getResourceRSPath(), searchQueryRequest, requestSource); } public PaginatedSearchResult searchGenomicConceptValues( diff --git a/pic-sure-resources/pic-sure-resource-api/src/main/java/edu/harvard/dbmi/avillach/service/ResourceWebClient.java b/pic-sure-resources/pic-sure-resource-api/src/main/java/edu/harvard/dbmi/avillach/service/ResourceWebClient.java index 9b75e8d5..cf80d303 100644 --- a/pic-sure-resources/pic-sure-resource-api/src/main/java/edu/harvard/dbmi/avillach/service/ResourceWebClient.java +++ b/pic-sure-resources/pic-sure-resource-api/src/main/java/edu/harvard/dbmi/avillach/service/ResourceWebClient.java @@ -97,7 +97,7 @@ public PaginatedSearchResult searchConceptValues( } } - public SearchResults search(String rsURL, QueryRequest searchQueryRequest, HttpHeaders headers) { + public SearchResults search(String rsURL, QueryRequest searchQueryRequest, String requestSource) { logger.debug("Calling ResourceWebClient search()"); try { if (searchQueryRequest == null || searchQueryRequest.getQuery() == null) { @@ -113,9 +113,10 @@ public SearchResults search(String rsURL, QueryRequest searchQueryRequest, HttpH String pathName = "/search"; String body = json.writeValueAsString(searchQueryRequest); - HttpResponse resourcesResponse = retrievePostResponse( - composeURL(rsURL, pathName), createHeaders(searchQueryRequest.getResourceCredentials(), headers), body - ); + Header[] headers = createHeaders(searchQueryRequest.getResourceCredentials()); + headers = includeRequestSourceHeader(requestSource, headers); + + HttpResponse resourcesResponse = retrievePostResponse(composeURL(rsURL, pathName), headers, body); if (resourcesResponse.getStatusLine().getStatusCode() != 200) { logger.error("ResourceRS did not return a 200"); throwResponseError(resourcesResponse, rsURL); @@ -268,15 +269,7 @@ public Response querySync(String rsURL, QueryRequest queryRequest, String reques Header[] headers = createHeaders(queryRequest.getResourceCredentials()); - if (requestSource != null) { - Header sourceHeader = new BasicHeader("request-source", requestSource); - - // Add the source header to the headers array. - Header[] newHeaders = new Header[headers.length + 1]; - System.arraycopy(headers, 0, newHeaders, 0, headers.length); - newHeaders[headers.length] = sourceHeader; - headers = newHeaders; - } + headers = includeRequestSourceHeader(requestSource, headers); HttpResponse resourcesResponse = retrievePostResponse(composeURL(rsURL, pathName), headers, body); if (resourcesResponse.getStatusLine().getStatusCode() != 200) { @@ -297,6 +290,19 @@ public Response querySync(String rsURL, QueryRequest queryRequest, String reques } } + private static Header[] includeRequestSourceHeader(String requestSource, Header[] headers) { + if (requestSource != null) { + Header sourceHeader = new BasicHeader("request-source", requestSource); + + // Add the source header to the headers array. + Header[] newHeaders = new Header[headers.length + 1]; + System.arraycopy(headers, 0, newHeaders, 0, headers.length); + newHeaders[headers.length] = sourceHeader; + headers = newHeaders; + } + return headers; + } + /** * This method is used to call the /bin/continuous endpoint on the ResourceRS. The /bin/continuous endpoint is used to retrieve binned * continuous data from the visualization resource. @@ -323,15 +329,7 @@ public Response queryContinuous(String rsURL, QueryRequest queryRequest, String String body = json.writeValueAsString(queryRequest); Header[] headers = createHeaders(queryRequest.getResourceCredentials()); - if (requestSource != null) { - Header sourceHeader = new BasicHeader("request-source", requestSource); - - // Add the source header to the headers array. - Header[] newHeaders = new Header[headers.length + 1]; - System.arraycopy(headers, 0, newHeaders, 0, headers.length); - newHeaders[headers.length] = sourceHeader; - headers = newHeaders; - } + headers = includeRequestSourceHeader(requestSource, headers); logger.debug("Calling ResourceWebClient queryContinuous() with body: " + body + " and headers: " + queryRequest); HttpResponse resourcesResponse = retrievePostResponse(composeURL(rsURL, pathName), headers, body); @@ -371,30 +369,4 @@ private Header[] createHeaders(Map resourceCredentials) { return headers; } - private Header[] createHeaders(Map resourceCredentials, HttpHeaders headers) { - Header[] headersArray = createHeaders(resourceCredentials); - - if (headers == null) { - return headersArray; - } - - // get the count of additional headers - int additionalHeadersCount = headers.getRequestHeaders().size(); - // create a new array with the size of the original array plus the count of additional headers - Header[] newHeadersArray = new Header[headersArray.length + additionalHeadersCount]; - // copy the original headers into the new array - System.arraycopy(headersArray, 0, newHeadersArray, 0, headersArray.length); - // iterate over the additional headers and add them to the new array - int i = headersArray.length; - - for (Map.Entry> entry : headers.getRequestHeaders().entrySet()) { - for (String value : entry.getValue()) { - newHeadersArray[i] = new BasicHeader(entry.getKey(), value); - i++; - } - } - - return newHeadersArray; - } - }