Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue NullPointerException in HeaderUtils.getPreferredCookie #4773 #4795

Merged
merged 3 commits into from
Jun 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -303,28 +303,19 @@ public static void checkHeaderChanges(final Map<String, String> headersSnapshot,
/**
* 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) {
tvallin marked this conversation as resolved.
Show resolved Hide resolved

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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -185,7 +185,7 @@ public void testAsHeaderString() throws Exception {
}

@Test
public void testgetPreferredCookie(){
public void testGetPreferredCookie(){

Calendar calendar = Calendar.getInstance();
calendar.set(2000, Calendar.JANUARY, 1);
Expand Down Expand Up @@ -227,4 +227,18 @@ public void testgetPreferredCookie(){

}

@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));
}

}