Skip to content

Commit

Permalink
Handle header names in a case-inseneitive way.
Browse files Browse the repository at this point in the history
  • Loading branch information
samadpaydar3 committed Jan 29, 2024
1 parent a33677f commit a1759a3
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/main/java/com/laserfiche/api/client/model/ProblemDetails.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
* A machine-readable format for specifying errors in HTTP API responses based on <a href="https://tools.ietf.org/html/rfc7807">rfc 7807</a>.
Expand Down Expand Up @@ -187,12 +188,10 @@ public static ProblemDetails create(int statusCode, Map<String, String> headers)

String errorMessage = null;
if (headers != null) {
String operationId = headers.getOrDefault(OPERATION_ID_HEADER,
headers.getOrDefault(OPERATION_ID_HEADER.toLowerCase(), null));
String operationId = getHeaderValue(headers, OPERATION_ID_HEADER);
problemDetails.setOperationId(operationId);

String headerErrorMessage = headers.getOrDefault(API_SERVER_ERROR_MESSAGE_HEADER,
headers.getOrDefault(API_SERVER_ERROR_MESSAGE_HEADER.toLowerCase(), null));
String headerErrorMessage = getHeaderValue(headers, API_SERVER_ERROR_MESSAGE_HEADER);
if (headerErrorMessage != null) {
try {
errorMessage = URLDecoder.decode(headerErrorMessage, StandardCharsets.UTF_8.name());
Expand All @@ -205,4 +204,14 @@ public static ProblemDetails create(int statusCode, Map<String, String> headers)

return problemDetails;
}

private static String getHeaderValue(Map<String, String> headers, String headerName) {
Map<String, String> headersInLowerCase =
headers.entrySet().stream()
.collect(Collectors.toMap(
e -> e.getKey().toLowerCase(),
Map.Entry::getValue
));
return headersInLowerCase.getOrDefault(headerName.toLowerCase(), null);
}
}

0 comments on commit a1759a3

Please sign in to comment.