Skip to content

Commit

Permalink
Make get header null safe
Browse files Browse the repository at this point in the history
  • Loading branch information
rpoet-jh committed Jul 18, 2024
1 parent d4591d7 commit 589d4ea
Showing 1 changed file with 10 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ AuthContext authenticate() {
ResponseEntity<Void> csrfResponse = restClient.get()
.uri("/security/csrf")
.exchange((request, response) -> new ResponseEntity<>(null, response.getHeaders(), HttpStatus.OK));
String xsrfToken = csrfResponse.getHeaders().get("DSPACE-XSRF-TOKEN").get(0);
String xsrfToken = getAuthHeaderValue(csrfResponse, "DSPACE-XSRF-TOKEN");

MultiValueMap<String, String> bodyPair = new LinkedMultiValueMap<>();
bodyPair.add("user", dspaceUsername);
Expand All @@ -112,7 +112,7 @@ AuthContext authenticate() {
.body(bodyPair)
.retrieve()
.toBodilessEntity();
String authToken = authResponse.getHeaders().get("Authorization").get(0);
String authToken = getAuthHeaderValue(authResponse, "Authorization");

return new AuthContext(xsrfToken, authToken);
}
Expand Down Expand Up @@ -144,6 +144,14 @@ void deleteDeposit(Deposit deposit, AuthContext authContext) {
}
}

private String getAuthHeaderValue(ResponseEntity<Void> response, String header) {
List<String> values = response.getHeaders().get(header);
if (Objects.isNull(values) || values.isEmpty()) {
throw new RuntimeException("Auth Header not found: " + header);
}
return values.get(0);
}

private String parseHandleFilter(URI accessUrl) {
String handleDelim = dspaceApiProtocol + "://" + dspaceServer + "/handle/";
String[] handleTokens = accessUrl.toString().split(handleDelim);
Expand All @@ -163,7 +171,6 @@ private String findItemUuid(String handleValue, AuthContext authContext, String
.body(String.class);
List<Map<String, ?>> searchArray = JsonPath.parse(searchResponse).read("$..indexableObject[?(@.handle)]");
if (searchArray.size() == 1) {
@SuppressWarnings("unchecked")
Map<String, ?> itemMap = searchArray.get(0);
String itemName = itemMap.get("name").toString();
String itemHandle = itemMap.get("handle").toString();
Expand Down

0 comments on commit 589d4ea

Please sign in to comment.