public static HttpSession getMatchingHttpSession(final Collection<HttpSession> sessions,
			List<HttpCookie> cookies, final HttpSessionTokensSet siteTokens) {

		// Pre-checks
		if (sessions.isEmpty()) {
			return null;
		}

		List<HttpSession> matchingSessions = new LinkedList<>(sessions);
		for (String token : siteTokens.getTokensSet()) {
			// Get the corresponding cookie from the cookies list
			HttpCookie matchingCookie = null;
			for (HttpCookie cookie : cookies) {
				if (cookie.getName().equals(token)) {
					matchingCookie = cookie;
					break;
				}
			}
			// Filter the sessions that do not match the cookie value
			Iterator<HttpSession> it = matchingSessions.iterator();
			while (it.hasNext()) {
				if (!it.next().matchesToken(token, matchingCookie)) {
					it.remove();
				}
			}
		}

		// Return the matching session
		if (matchingSessions.size() >= 1) {
			if (matchingSessions.size() > 1) {
				log.warn("Multiple sessions matching the cookies from response. Using first one.");
			}
			return matchingSessions.get(0);
		}
		return null;
	}
public static HttpSession getMatchingHttpSession ( final Collection < HttpSession > sessions , List < HttpCookie > cookies , final HttpSessionTokensSet siteTokens ) { if ( sessions . isEmpty ( ) ) { return null ; } List < HttpSession > matchingSessions = new LinkedList < > ( sessions ) ; for ( String token : siteTokens . getTokensSet ( ) ) { HttpCookie matchingCookie = null ; for ( HttpCookie cookie : cookies ) { if ( cookie . getName ( ) . equals ( token ) ) { matchingCookie = cookie ; break ; } } Iterator < HttpSession > it = matchingSessions . iterator ( ) ; while ( it . hasNext ( ) ) { if ( ! it . next ( ) . matchesToken ( token , matchingCookie ) ) { it . remove ( ) ; } } } if ( matchingSessions . size ( ) >= 1 ) { if ( matchingSessions . size ( ) > 1 ) { log . warn ( "Multiple sessions matching the cookies from response. Using first one." ) ; } return matchingSessions . get ( 0 ) ; } return null ; }
Gets the matching http session, if any, for a particular message containing a list of
cookies, from a set of sessions.

@param sessions the existing sessions
@param cookies the cookies present in the request header of the message
@param siteTokens the tokens
@return the matching http session, if any, or null if no existing session was found to match
all the tokens