From a1759a3e2eac831d4686247dc0a3e9c231d511b1 Mon Sep 17 00:00:00 2001 From: Samad Paydar Date: Mon, 29 Jan 2024 15:53:04 -0500 Subject: [PATCH] Handle header names in a case-inseneitive way. --- .../api/client/model/ProblemDetails.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/laserfiche/api/client/model/ProblemDetails.java b/src/main/java/com/laserfiche/api/client/model/ProblemDetails.java index bd683e1..33c083a 100644 --- a/src/main/java/com/laserfiche/api/client/model/ProblemDetails.java +++ b/src/main/java/com/laserfiche/api/client/model/ProblemDetails.java @@ -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 rfc 7807. @@ -187,12 +188,10 @@ public static ProblemDetails create(int statusCode, Map 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()); @@ -205,4 +204,14 @@ public static ProblemDetails create(int statusCode, Map headers) return problemDetails; } + + private static String getHeaderValue(Map headers, String headerName) { + Map headersInLowerCase = + headers.entrySet().stream() + .collect(Collectors.toMap( + e -> e.getKey().toLowerCase(), + Map.Entry::getValue + )); + return headersInLowerCase.getOrDefault(headerName.toLowerCase(), null); + } }