Skip to content

Commit

Permalink
Refactor handling of request source header
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Gcolon021 committed Mar 8, 2024
1 parent fa86a0d commit 1d8c710
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String>());
}
return resourceWebClient.search(resource.getResourceRSPath(), searchQueryRequest, headers);
return resourceWebClient.search(resource.getResourceRSPath(), searchQueryRequest, requestSource);
}

public PaginatedSearchResult<?> searchGenomicConceptValues(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand All @@ -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.
Expand All @@ -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);
Expand Down Expand Up @@ -371,30 +369,4 @@ private Header[] createHeaders(Map<String, String> resourceCredentials) {
return headers;
}

private Header[] createHeaders(Map<String, String> 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<String, List<String>> entry : headers.getRequestHeaders().entrySet()) {
for (String value : entry.getValue()) {
newHeadersArray[i] = new BasicHeader(entry.getKey(), value);
i++;
}
}

return newHeadersArray;
}

}

0 comments on commit 1d8c710

Please sign in to comment.