use of java.net.HttpCookie in project android-oss by kickstarter.
the class RefTagUtilsTest method testStoredCookieRefTagForProject.
@Test
public void testStoredCookieRefTagForProject() {
final CookieManager cookieManager = new CookieManager();
final CookieStore cookieStore = cookieManager.getCookieStore();
final Project project = ProjectFactory.project();
final RefTag refTag = RefTag.recommended();
// set the cookie and retrieve the ref tag
cookieStore.add(null, new HttpCookie("ref_" + project.id(), refTag.tag() + "%3F" + SystemUtils.secondsSinceEpoch()));
final RefTag retrievedRefTag = RefTagUtils.storedCookieRefTagForProject(project, cookieManager, sharedPreferences);
assertNotNull(retrievedRefTag);
assertEquals(refTag, retrievedRefTag);
}
use of java.net.HttpCookie in project android-oss by kickstarter.
the class RefTagUtilsTest method testFindRefTagCookieForProject_WhenCookieDoesNotExist.
@Test
public void testFindRefTagCookieForProject_WhenCookieDoesNotExist() {
final CookieManager cookieManager = new CookieManager();
final Project project = ProjectFactory.project();
// retrieve the cookie
final HttpCookie cookie = RefTagUtils.findRefTagCookieForProject(project, cookieManager, sharedPreferences);
assertNull(cookie);
}
use of java.net.HttpCookie in project NoHttp by yanzhenjie.
the class CookieEntity method toHttpCookie.
/**
* Into {@link HttpCookie}.
*
* @return {@link HttpCookie}.
*/
public HttpCookie toHttpCookie() {
HttpCookie cookie = new HttpCookie(name, value);
cookie.setComment(comment);
cookie.setCommentURL(commentURL);
cookie.setDiscard(discard);
cookie.setDomain(domain);
if (expiry == -1L)
cookie.setMaxAge(-1L);
else
cookie.setMaxAge((expiry - System.currentTimeMillis()) / 1000L);
cookie.setPath(path);
cookie.setPortlist(portList);
cookie.setSecure(secure);
cookie.setVersion(version);
return cookie;
}
use of java.net.HttpCookie in project zaproxy by zaproxy.
the class CookieBasedSessionManagementHelper method processMessageToMatchSession.
/**
* Modifies a message so its Request Header/Body matches the web session provided.
*
* @param message the message
* @param requestCookies a pre-computed list with the request cookies (for optimization reasons)
* @param session the session
*/
public static void processMessageToMatchSession(HttpMessage message, List<HttpCookie> requestCookies, HttpSession session) {
// Make a copy of the session tokens set, as they will be modified
HttpSessionTokensSet tokensSet = session.getTokensNames();
// If no tokens exists create dummy Object -> NPE
if (tokensSet == null) {
tokensSet = new HttpSessionTokensSet();
}
Set<String> unsetSiteTokens = new LinkedHashSet<>(tokensSet.getTokensSet());
// Iterate through the cookies in the request
Iterator<HttpCookie> it = requestCookies.iterator();
while (it.hasNext()) {
HttpCookie cookie = it.next();
String cookieName = cookie.getName();
// If the cookie is a token
if (tokensSet.isSessionToken(cookieName)) {
String tokenValue = session.getTokenValue(cookieName);
if (log.isDebugEnabled())
log.debug("Changing value of token '" + cookieName + "' to: " + tokenValue);
// Change it's value to the one in the active session, if any
if (tokenValue != null) {
cookie.setValue(tokenValue);
} else // Or delete it, if the active session does not have a token value
{
it.remove();
}
// Remove the token from the token set so we know what tokens still have to be
// added
unsetSiteTokens.remove(cookieName);
}
}
// value
for (String token : unsetSiteTokens) {
String tokenValue = session.getTokenValue(token);
// Change it's value to the one in the active session, if any
if (tokenValue != null) {
if (log.isDebugEnabled())
log.debug("Adding token '" + token + " with value: " + tokenValue);
HttpCookie cookie = new HttpCookie(token, tokenValue);
requestCookies.add(cookie);
}
}
// Store the session in the HttpMessage for caching purpose
message.setHttpSession(session);
// Update the cookies in the message
message.getRequestHeader().setCookies(requestCookies);
}
use of java.net.HttpCookie in project zaproxy by zaproxy.
the class CookieBasedSessionManagementHelper method getMatchingHttpSession.
/**
* 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
*/
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;
}
Aggregations