From a33677f1be1eeb5ab58b9bad454ffbe9f5a59450 Mon Sep 17 00:00:00 2001 From: Samad Paydar Date: Fri, 26 Jan 2024 16:13:37 -0500 Subject: [PATCH 1/4] Retrieve header value by lower-case name. --- .../com/laserfiche/api/client/model/ProblemDetails.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 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 e31bebc..bd683e1 100644 --- a/src/main/java/com/laserfiche/api/client/model/ProblemDetails.java +++ b/src/main/java/com/laserfiche/api/client/model/ProblemDetails.java @@ -187,9 +187,12 @@ public static ProblemDetails create(int statusCode, Map headers) String errorMessage = null; if (headers != null) { - problemDetails.setOperationId(headers.getOrDefault(OPERATION_ID_HEADER, null)); + String operationId = headers.getOrDefault(OPERATION_ID_HEADER, + headers.getOrDefault(OPERATION_ID_HEADER.toLowerCase(), null)); + problemDetails.setOperationId(operationId); - String headerErrorMessage = headers.getOrDefault(API_SERVER_ERROR_MESSAGE_HEADER, null); + String headerErrorMessage = headers.getOrDefault(API_SERVER_ERROR_MESSAGE_HEADER, + headers.getOrDefault(API_SERVER_ERROR_MESSAGE_HEADER.toLowerCase(), null)); if (headerErrorMessage != null) { try { errorMessage = URLDecoder.decode(headerErrorMessage, StandardCharsets.UTF_8.name()); From a1759a3e2eac831d4686247dc0a3e9c231d511b1 Mon Sep 17 00:00:00 2001 From: Samad Paydar Date: Mon, 29 Jan 2024 15:53:04 -0500 Subject: [PATCH 2/4] 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); + } } From 4ed272b86c0e1aa34bfa0d0bd3414efa722308c2 Mon Sep 17 00:00:00 2001 From: Samad Paydar Date: Mon, 29 Jan 2024 16:07:37 -0500 Subject: [PATCH 3/4] Fix issue with null values. --- .../com/laserfiche/api/client/model/ProblemDetails.java | 7 +++---- 1 file changed, 3 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 33c083a..06225da 100644 --- a/src/main/java/com/laserfiche/api/client/model/ProblemDetails.java +++ b/src/main/java/com/laserfiche/api/client/model/ProblemDetails.java @@ -208,10 +208,9 @@ public static ProblemDetails create(int statusCode, Map headers) private static String getHeaderValue(Map headers, String headerName) { Map headersInLowerCase = headers.entrySet().stream() - .collect(Collectors.toMap( - e -> e.getKey().toLowerCase(), - Map.Entry::getValue - )); + .collect( + HashMap::new, + (m,v)->m.put(v.getKey().toLowerCase(), v.getValue()), HashMap::putAll); return headersInLowerCase.getOrDefault(headerName.toLowerCase(), null); } } From 0b66d72717c6f509d5a11d61f4a56a57ce9535fd Mon Sep 17 00:00:00 2001 From: Samad Paydar Date: Mon, 29 Jan 2024 16:21:47 -0500 Subject: [PATCH 4/4] Improve performance by not creating a new hashmap. --- .../com/laserfiche/api/client/model/ProblemDetails.java | 9 +++------ 1 file changed, 3 insertions(+), 6 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 06225da..e93fd61 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.Optional; import java.util.stream.Collectors; /** @@ -206,11 +207,7 @@ public static ProblemDetails create(int statusCode, Map headers) } private static String getHeaderValue(Map headers, String headerName) { - Map headersInLowerCase = - headers.entrySet().stream() - .collect( - HashMap::new, - (m,v)->m.put(v.getKey().toLowerCase(), v.getValue()), HashMap::putAll); - return headersInLowerCase.getOrDefault(headerName.toLowerCase(), null); + Optional> result = headers.entrySet().stream().filter(entry -> entry.getKey().equalsIgnoreCase(headerName)).findFirst(); + return result.map(Map.Entry::getValue).orElse(null); } }