Fix issue NullPointerException in HeaderUtils.getPreferredCookie #4773 (#4795)
* Fix issue #4773
Signed-off-by: tvallin <thibault.vallin@oracle.com>
diff --git a/core-common/src/main/java/org/glassfish/jersey/message/internal/HeaderUtils.java b/core-common/src/main/java/org/glassfish/jersey/message/internal/HeaderUtils.java
index c8636ce..3c6cfdd 100644
--- a/core-common/src/main/java/org/glassfish/jersey/message/internal/HeaderUtils.java
+++ b/core-common/src/main/java/org/glassfish/jersey/message/internal/HeaderUtils.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2010, 2020 Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2010, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -303,28 +303,19 @@
/**
* Compare two NewCookies having the same name. See documentation RFC.
*
- * @param first NewCookie to be compared.
- * @param second NewCookie to be compared.
+ * @param first NewCookie to be compared.
+ * @param second NewCookie to be compared.
* @return the preferred NewCookie according to rules :
* - the latest maxAge.
- * - if equal, compare the expiry date
- * - if equal, compare name length
+ * - if equal, compare the expiry date.
+ * - if equal, compare path length.
*/
public static NewCookie getPreferredCookie(NewCookie first, NewCookie second) {
-
- if (first == null) {
- return second;
- } else if (second == null) {
- return first;
- }
-
- if (first.getMaxAge() != second.getMaxAge()){
- return Comparator.comparing(NewCookie::getMaxAge).compare(first, second) > 0 ? first : second;
- } else if (first.getExpiry() != null && second.getExpiry() != null && !first.getExpiry().equals(second.getExpiry())) {
- return Comparator.comparing(NewCookie::getExpiry).compare(first, second) > 0 ? first : second;
- } else {
- return first.getPath().length() > second.getPath().length() ? first : second;
- }
+ return Comparator.nullsFirst(
+ Comparator.comparingInt(NewCookie::getMaxAge)
+ .thenComparing(NewCookie::getExpiry, Comparator.nullsLast(Comparator.naturalOrder()))
+ .thenComparing(NewCookie::getPath, Comparator.nullsLast(Comparator.comparing(String::length))))
+ .compare(first, second) > 0 ? first : second;
}
/**
diff --git a/tests/e2e-core-common/src/test/java/org/glassfish/jersey/tests/e2e/common/message/internal/HeaderUtilsTest.java b/tests/e2e-core-common/src/test/java/org/glassfish/jersey/tests/e2e/common/message/internal/HeaderUtilsTest.java
index 589a671..bc370e4 100644
--- a/tests/e2e-core-common/src/test/java/org/glassfish/jersey/tests/e2e/common/message/internal/HeaderUtilsTest.java
+++ b/tests/e2e-core-common/src/test/java/org/glassfish/jersey/tests/e2e/common/message/internal/HeaderUtilsTest.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2013, 2020 Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -185,7 +185,7 @@
}
@Test
- public void testgetPreferredCookie(){
+ public void testGetPreferredCookie(){
Calendar calendar = Calendar.getInstance();
calendar.set(2000, Calendar.JANUARY, 1);
@@ -227,4 +227,18 @@
}
+ @Test
+ public void testGetPreferredCookieWithNullPath() {
+ NewCookie first = new NewCookie("NAME", "VALUE");
+ NewCookie second = new NewCookie("NAME", "VALUE");
+ NewCookie returnedCookie = HeaderUtils.getPreferredCookie(first, second);
+
+ assertEquals(first, returnedCookie);
+ }
+
+ @Test
+ public void testGetPreferredCookieWithNullInput() {
+ assertNull(HeaderUtils.getPreferredCookie(null, null));
+ }
+
}